LCOV - code coverage report
Current view: top level - src/backend/utils/adt - varlena.c (source / functions) Coverage Total Hit
Test: PostgreSQL 19devel Lines: 90.4 % 1910 1727
Test Date: 2026-02-17 17:20:33 Functions: 92.4 % 144 133
Legend: Lines:     hit not hit

            Line data    Source code
       1              : /*-------------------------------------------------------------------------
       2              :  *
       3              :  * varlena.c
       4              :  *    Functions for the variable-length built-in types.
       5              :  *
       6              :  * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
       7              :  * Portions Copyright (c) 1994, Regents of the University of California
       8              :  *
       9              :  *
      10              :  * IDENTIFICATION
      11              :  *    src/backend/utils/adt/varlena.c
      12              :  *
      13              :  *-------------------------------------------------------------------------
      14              :  */
      15              : #include "postgres.h"
      16              : 
      17              : #include <ctype.h>
      18              : #include <limits.h>
      19              : 
      20              : #include "access/detoast.h"
      21              : #include "access/toast_compression.h"
      22              : #include "catalog/pg_collation.h"
      23              : #include "catalog/pg_type.h"
      24              : #include "common/hashfn.h"
      25              : #include "common/int.h"
      26              : #include "common/unicode_category.h"
      27              : #include "common/unicode_norm.h"
      28              : #include "common/unicode_version.h"
      29              : #include "funcapi.h"
      30              : #include "lib/hyperloglog.h"
      31              : #include "libpq/pqformat.h"
      32              : #include "miscadmin.h"
      33              : #include "nodes/execnodes.h"
      34              : #include "parser/scansup.h"
      35              : #include "port/pg_bswap.h"
      36              : #include "regex/regex.h"
      37              : #include "utils/builtins.h"
      38              : #include "utils/guc.h"
      39              : #include "utils/lsyscache.h"
      40              : #include "utils/memutils.h"
      41              : #include "utils/pg_locale.h"
      42              : #include "utils/sortsupport.h"
      43              : #include "utils/varlena.h"
      44              : 
      45              : typedef varlena VarString;
      46              : 
      47              : /*
      48              :  * State for text_position_* functions.
      49              :  */
      50              : typedef struct
      51              : {
      52              :     pg_locale_t locale;         /* collation used for substring matching */
      53              :     bool        is_multibyte_char_in_char;  /* need to check char boundaries? */
      54              :     bool        greedy;         /* find longest possible substring? */
      55              : 
      56              :     char       *str1;           /* haystack string */
      57              :     char       *str2;           /* needle string */
      58              :     int         len1;           /* string lengths in bytes */
      59              :     int         len2;
      60              : 
      61              :     /* Skip table for Boyer-Moore-Horspool search algorithm: */
      62              :     int         skiptablemask;  /* mask for ANDing with skiptable subscripts */
      63              :     int         skiptable[256]; /* skip distance for given mismatched char */
      64              : 
      65              :     /*
      66              :      * Note that with nondeterministic collations, the length of the last
      67              :      * match is not necessarily equal to the length of the "needle" passed in.
      68              :      */
      69              :     char       *last_match;     /* pointer to last match in 'str1' */
      70              :     int         last_match_len; /* length of last match */
      71              :     int         last_match_len_tmp; /* same but for internal use */
      72              : 
      73              :     /*
      74              :      * Sometimes we need to convert the byte position of a match to a
      75              :      * character position.  These store the last position that was converted,
      76              :      * so that on the next call, we can continue from that point, rather than
      77              :      * count characters from the very beginning.
      78              :      */
      79              :     char       *refpoint;       /* pointer within original haystack string */
      80              :     int         refpos;         /* 0-based character offset of the same point */
      81              : } TextPositionState;
      82              : 
      83              : typedef struct
      84              : {
      85              :     char       *buf1;           /* 1st string, or abbreviation original string
      86              :                                  * buf */
      87              :     char       *buf2;           /* 2nd string, or abbreviation strxfrm() buf */
      88              :     int         buflen1;        /* Allocated length of buf1 */
      89              :     int         buflen2;        /* Allocated length of buf2 */
      90              :     int         last_len1;      /* Length of last buf1 string/strxfrm() input */
      91              :     int         last_len2;      /* Length of last buf2 string/strxfrm() blob */
      92              :     int         last_returned;  /* Last comparison result (cache) */
      93              :     bool        cache_blob;     /* Does buf2 contain strxfrm() blob, etc? */
      94              :     bool        collate_c;
      95              :     Oid         typid;          /* Actual datatype (text/bpchar/name) */
      96              :     hyperLogLogState abbr_card; /* Abbreviated key cardinality state */
      97              :     hyperLogLogState full_card; /* Full key cardinality state */
      98              :     double      prop_card;      /* Required cardinality proportion */
      99              :     pg_locale_t locale;
     100              : } VarStringSortSupport;
     101              : 
     102              : /*
     103              :  * Output data for split_text(): we output either to an array or a table.
     104              :  * tupstore and tupdesc must be set up in advance to output to a table.
     105              :  */
     106              : typedef struct
     107              : {
     108              :     ArrayBuildState *astate;
     109              :     Tuplestorestate *tupstore;
     110              :     TupleDesc   tupdesc;
     111              : } SplitTextOutputData;
     112              : 
     113              : /*
     114              :  * This should be large enough that most strings will fit, but small enough
     115              :  * that we feel comfortable putting it on the stack
     116              :  */
     117              : #define TEXTBUFLEN      1024
     118              : 
     119              : #define DatumGetVarStringP(X)       ((VarString *) PG_DETOAST_DATUM(X))
     120              : #define DatumGetVarStringPP(X)      ((VarString *) PG_DETOAST_DATUM_PACKED(X))
     121              : 
     122              : static int  varstrfastcmp_c(Datum x, Datum y, SortSupport ssup);
     123              : static int  bpcharfastcmp_c(Datum x, Datum y, SortSupport ssup);
     124              : static int  namefastcmp_c(Datum x, Datum y, SortSupport ssup);
     125              : static int  varlenafastcmp_locale(Datum x, Datum y, SortSupport ssup);
     126              : static int  namefastcmp_locale(Datum x, Datum y, SortSupport ssup);
     127              : static int  varstrfastcmp_locale(char *a1p, int len1, char *a2p, int len2, SortSupport ssup);
     128              : static Datum varstr_abbrev_convert(Datum original, SortSupport ssup);
     129              : static bool varstr_abbrev_abort(int memtupcount, SortSupport ssup);
     130              : static int32 text_length(Datum str);
     131              : static text *text_catenate(text *t1, text *t2);
     132              : static text *text_substring(Datum str,
     133              :                             int32 start,
     134              :                             int32 length,
     135              :                             bool length_not_specified);
     136              : static int  pg_mbcharcliplen_chars(const char *mbstr, int len, int limit);
     137              : static text *text_overlay(text *t1, text *t2, int sp, int sl);
     138              : static int  text_position(text *t1, text *t2, Oid collid);
     139              : static void text_position_setup(text *t1, text *t2, Oid collid, TextPositionState *state);
     140              : static bool text_position_next(TextPositionState *state);
     141              : static char *text_position_next_internal(char *start_ptr, TextPositionState *state);
     142              : static char *text_position_get_match_ptr(TextPositionState *state);
     143              : static int  text_position_get_match_pos(TextPositionState *state);
     144              : static void text_position_cleanup(TextPositionState *state);
     145              : static void check_collation_set(Oid collid);
     146              : static int  text_cmp(text *arg1, text *arg2, Oid collid);
     147              : static void appendStringInfoText(StringInfo str, const text *t);
     148              : static bool split_text(FunctionCallInfo fcinfo, SplitTextOutputData *tstate);
     149              : static void split_text_accum_result(SplitTextOutputData *tstate,
     150              :                                     text *field_value,
     151              :                                     text *null_string,
     152              :                                     Oid collation);
     153              : static text *array_to_text_internal(FunctionCallInfo fcinfo, ArrayType *v,
     154              :                                     const char *fldsep, const char *null_string);
     155              : static StringInfo makeStringAggState(FunctionCallInfo fcinfo);
     156              : static bool text_format_parse_digits(const char **ptr, const char *end_ptr,
     157              :                                      int *value);
     158              : static const char *text_format_parse_format(const char *start_ptr,
     159              :                                             const char *end_ptr,
     160              :                                             int *argpos, int *widthpos,
     161              :                                             int *flags, int *width);
     162              : static void text_format_string_conversion(StringInfo buf, char conversion,
     163              :                                           FmgrInfo *typOutputInfo,
     164              :                                           Datum value, bool isNull,
     165              :                                           int flags, int width);
     166              : static void text_format_append_string(StringInfo buf, const char *str,
     167              :                                       int flags, int width);
     168              : 
     169              : 
     170              : /*****************************************************************************
     171              :  *   CONVERSION ROUTINES EXPORTED FOR USE BY C CODE                          *
     172              :  *****************************************************************************/
     173              : 
     174              : /*
     175              :  * cstring_to_text
     176              :  *
     177              :  * Create a text value from a null-terminated C string.
     178              :  *
     179              :  * The new text value is freshly palloc'd with a full-size VARHDR.
     180              :  */
     181              : text *
     182     12871951 : cstring_to_text(const char *s)
     183              : {
     184     12871951 :     return cstring_to_text_with_len(s, strlen(s));
     185              : }
     186              : 
     187              : /*
     188              :  * cstring_to_text_with_len
     189              :  *
     190              :  * Same as cstring_to_text except the caller specifies the string length;
     191              :  * the string need not be null_terminated.
     192              :  */
     193              : text *
     194     14237867 : cstring_to_text_with_len(const char *s, int len)
     195              : {
     196     14237867 :     text       *result = (text *) palloc(len + VARHDRSZ);
     197              : 
     198     14237867 :     SET_VARSIZE(result, len + VARHDRSZ);
     199     14237867 :     memcpy(VARDATA(result), s, len);
     200              : 
     201     14237867 :     return result;
     202              : }
     203              : 
     204              : /*
     205              :  * text_to_cstring
     206              :  *
     207              :  * Create a palloc'd, null-terminated C string from a text value.
     208              :  *
     209              :  * We support being passed a compressed or toasted text value.
     210              :  * This is a bit bogus since such values shouldn't really be referred to as
     211              :  * "text *", but it seems useful for robustness.  If we didn't handle that
     212              :  * case here, we'd need another routine that did, anyway.
     213              :  */
     214              : char *
     215      9202727 : text_to_cstring(const text *t)
     216              : {
     217              :     /* must cast away the const, unfortunately */
     218      9202727 :     text       *tunpacked = pg_detoast_datum_packed(unconstify(text *, t));
     219      9202727 :     int         len = VARSIZE_ANY_EXHDR(tunpacked);
     220              :     char       *result;
     221              : 
     222      9202727 :     result = (char *) palloc(len + 1);
     223      9202727 :     memcpy(result, VARDATA_ANY(tunpacked), len);
     224      9202727 :     result[len] = '\0';
     225              : 
     226      9202727 :     if (tunpacked != t)
     227        22973 :         pfree(tunpacked);
     228              : 
     229      9202727 :     return result;
     230              : }
     231              : 
     232              : /*
     233              :  * text_to_cstring_buffer
     234              :  *
     235              :  * Copy a text value into a caller-supplied buffer of size dst_len.
     236              :  *
     237              :  * The text string is truncated if necessary to fit.  The result is
     238              :  * guaranteed null-terminated (unless dst_len == 0).
     239              :  *
     240              :  * We support being passed a compressed or toasted text value.
     241              :  * This is a bit bogus since such values shouldn't really be referred to as
     242              :  * "text *", but it seems useful for robustness.  If we didn't handle that
     243              :  * case here, we'd need another routine that did, anyway.
     244              :  */
     245              : void
     246          503 : text_to_cstring_buffer(const text *src, char *dst, size_t dst_len)
     247              : {
     248              :     /* must cast away the const, unfortunately */
     249          503 :     text       *srcunpacked = pg_detoast_datum_packed(unconstify(text *, src));
     250          503 :     size_t      src_len = VARSIZE_ANY_EXHDR(srcunpacked);
     251              : 
     252          503 :     if (dst_len > 0)
     253              :     {
     254          503 :         dst_len--;
     255          503 :         if (dst_len >= src_len)
     256          503 :             dst_len = src_len;
     257              :         else                    /* ensure truncation is encoding-safe */
     258            0 :             dst_len = pg_mbcliplen(VARDATA_ANY(srcunpacked), src_len, dst_len);
     259          503 :         memcpy(dst, VARDATA_ANY(srcunpacked), dst_len);
     260          503 :         dst[dst_len] = '\0';
     261              :     }
     262              : 
     263          503 :     if (srcunpacked != src)
     264            0 :         pfree(srcunpacked);
     265          503 : }
     266              : 
     267              : 
     268              : /*****************************************************************************
     269              :  *   USER I/O ROUTINES                                                       *
     270              :  *****************************************************************************/
     271              : 
     272              : /*
     273              :  *      textin          - converts cstring to internal representation
     274              :  */
     275              : Datum
     276     11199098 : textin(PG_FUNCTION_ARGS)
     277              : {
     278     11199098 :     char       *inputText = PG_GETARG_CSTRING(0);
     279              : 
     280     11199098 :     PG_RETURN_TEXT_P(cstring_to_text(inputText));
     281              : }
     282              : 
     283              : /*
     284              :  *      textout         - converts internal representation to cstring
     285              :  */
     286              : Datum
     287      4150689 : textout(PG_FUNCTION_ARGS)
     288              : {
     289      4150689 :     Datum       txt = PG_GETARG_DATUM(0);
     290              : 
     291      4150689 :     PG_RETURN_CSTRING(TextDatumGetCString(txt));
     292              : }
     293              : 
     294              : /*
     295              :  *      textrecv            - converts external binary format to text
     296              :  */
     297              : Datum
     298           24 : textrecv(PG_FUNCTION_ARGS)
     299              : {
     300           24 :     StringInfo  buf = (StringInfo) PG_GETARG_POINTER(0);
     301              :     text       *result;
     302              :     char       *str;
     303              :     int         nbytes;
     304              : 
     305           24 :     str = pq_getmsgtext(buf, buf->len - buf->cursor, &nbytes);
     306              : 
     307           24 :     result = cstring_to_text_with_len(str, nbytes);
     308           24 :     pfree(str);
     309           24 :     PG_RETURN_TEXT_P(result);
     310              : }
     311              : 
     312              : /*
     313              :  *      textsend            - converts text to binary format
     314              :  */
     315              : Datum
     316         2359 : textsend(PG_FUNCTION_ARGS)
     317              : {
     318         2359 :     text       *t = PG_GETARG_TEXT_PP(0);
     319              :     StringInfoData buf;
     320              : 
     321         2359 :     pq_begintypsend(&buf);
     322         2359 :     pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t));
     323         2359 :     PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
     324              : }
     325              : 
     326              : 
     327              : /*
     328              :  *      unknownin           - converts cstring to internal representation
     329              :  */
     330              : Datum
     331            0 : unknownin(PG_FUNCTION_ARGS)
     332              : {
     333            0 :     char       *str = PG_GETARG_CSTRING(0);
     334              : 
     335              :     /* representation is same as cstring */
     336            0 :     PG_RETURN_CSTRING(pstrdup(str));
     337              : }
     338              : 
     339              : /*
     340              :  *      unknownout          - converts internal representation to cstring
     341              :  */
     342              : Datum
     343          470 : unknownout(PG_FUNCTION_ARGS)
     344              : {
     345              :     /* representation is same as cstring */
     346          470 :     char       *str = PG_GETARG_CSTRING(0);
     347              : 
     348          470 :     PG_RETURN_CSTRING(pstrdup(str));
     349              : }
     350              : 
     351              : /*
     352              :  *      unknownrecv         - converts external binary format to unknown
     353              :  */
     354              : Datum
     355            0 : unknownrecv(PG_FUNCTION_ARGS)
     356              : {
     357            0 :     StringInfo  buf = (StringInfo) PG_GETARG_POINTER(0);
     358              :     char       *str;
     359              :     int         nbytes;
     360              : 
     361            0 :     str = pq_getmsgtext(buf, buf->len - buf->cursor, &nbytes);
     362              :     /* representation is same as cstring */
     363            0 :     PG_RETURN_CSTRING(str);
     364              : }
     365              : 
     366              : /*
     367              :  *      unknownsend         - converts unknown to binary format
     368              :  */
     369              : Datum
     370            0 : unknownsend(PG_FUNCTION_ARGS)
     371              : {
     372              :     /* representation is same as cstring */
     373            0 :     char       *str = PG_GETARG_CSTRING(0);
     374              :     StringInfoData buf;
     375              : 
     376            0 :     pq_begintypsend(&buf);
     377            0 :     pq_sendtext(&buf, str, strlen(str));
     378            0 :     PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
     379              : }
     380              : 
     381              : 
     382              : /* ========== PUBLIC ROUTINES ========== */
     383              : 
     384              : /*
     385              :  * textlen -
     386              :  *    returns the logical length of a text*
     387              :  *     (which is less than the VARSIZE of the text*)
     388              :  */
     389              : Datum
     390       215448 : textlen(PG_FUNCTION_ARGS)
     391              : {
     392       215448 :     Datum       str = PG_GETARG_DATUM(0);
     393              : 
     394              :     /* try to avoid decompressing argument */
     395       215448 :     PG_RETURN_INT32(text_length(str));
     396              : }
     397              : 
     398              : /*
     399              :  * text_length -
     400              :  *  Does the real work for textlen()
     401              :  *
     402              :  *  This is broken out so it can be called directly by other string processing
     403              :  *  functions.  Note that the argument is passed as a Datum, to indicate that
     404              :  *  it may still be in compressed form.  We can avoid decompressing it at all
     405              :  *  in some cases.
     406              :  */
     407              : static int32
     408       215454 : text_length(Datum str)
     409              : {
     410              :     /* fastpath when max encoding length is one */
     411       215454 :     if (pg_database_encoding_max_length() == 1)
     412           10 :         return (toast_raw_datum_size(str) - VARHDRSZ);
     413              :     else
     414              :     {
     415       215444 :         text       *t = DatumGetTextPP(str);
     416              : 
     417       215444 :         return (pg_mbstrlen_with_len(VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)));
     418              :     }
     419              : }
     420              : 
     421              : /*
     422              :  * textoctetlen -
     423              :  *    returns the physical length of a text*
     424              :  *     (which is less than the VARSIZE of the text*)
     425              :  */
     426              : Datum
     427           35 : textoctetlen(PG_FUNCTION_ARGS)
     428              : {
     429           35 :     Datum       str = PG_GETARG_DATUM(0);
     430              : 
     431              :     /* We need not detoast the input at all */
     432           35 :     PG_RETURN_INT32(toast_raw_datum_size(str) - VARHDRSZ);
     433              : }
     434              : 
     435              : /*
     436              :  * textcat -
     437              :  *    takes two text* and returns a text* that is the concatenation of
     438              :  *    the two.
     439              :  *
     440              :  * Rewritten by Sapa, sapa@hq.icb.chel.su. 8-Jul-96.
     441              :  * Updated by Thomas, Thomas.Lockhart@jpl.nasa.gov 1997-07-10.
     442              :  * Allocate space for output in all cases.
     443              :  * XXX - thomas 1997-07-10
     444              :  */
     445              : Datum
     446       981602 : textcat(PG_FUNCTION_ARGS)
     447              : {
     448       981602 :     text       *t1 = PG_GETARG_TEXT_PP(0);
     449       981602 :     text       *t2 = PG_GETARG_TEXT_PP(1);
     450              : 
     451       981602 :     PG_RETURN_TEXT_P(text_catenate(t1, t2));
     452              : }
     453              : 
     454              : /*
     455              :  * text_catenate
     456              :  *  Guts of textcat(), broken out so it can be used by other functions
     457              :  *
     458              :  * Arguments can be in short-header form, but not compressed or out-of-line
     459              :  */
     460              : static text *
     461       981642 : text_catenate(text *t1, text *t2)
     462              : {
     463              :     text       *result;
     464              :     int         len1,
     465              :                 len2,
     466              :                 len;
     467              :     char       *ptr;
     468              : 
     469       981642 :     len1 = VARSIZE_ANY_EXHDR(t1);
     470       981642 :     len2 = VARSIZE_ANY_EXHDR(t2);
     471              : 
     472              :     /* paranoia ... probably should throw error instead? */
     473       981642 :     if (len1 < 0)
     474            0 :         len1 = 0;
     475       981642 :     if (len2 < 0)
     476            0 :         len2 = 0;
     477              : 
     478       981642 :     len = len1 + len2 + VARHDRSZ;
     479       981642 :     result = (text *) palloc(len);
     480              : 
     481              :     /* Set size of result string... */
     482       981642 :     SET_VARSIZE(result, len);
     483              : 
     484              :     /* Fill data field of result string... */
     485       981642 :     ptr = VARDATA(result);
     486       981642 :     if (len1 > 0)
     487       981230 :         memcpy(ptr, VARDATA_ANY(t1), len1);
     488       981642 :     if (len2 > 0)
     489       981537 :         memcpy(ptr + len1, VARDATA_ANY(t2), len2);
     490              : 
     491       981642 :     return result;
     492              : }
     493              : 
     494              : /*
     495              :  * charlen_to_bytelen()
     496              :  *  Compute the number of bytes occupied by n characters starting at *p
     497              :  *
     498              :  * The caller shall ensure there are n complete characters.  Callers achieve
     499              :  * this by deriving "n" from regmatch_t findings from searching a wchar array.
     500              :  * pg_mb2wchar_with_len() skips any trailing incomplete character, so regex
     501              :  * matches will end no later than the last complete character.  (The string
     502              :  * need not be null-terminated.)
     503              :  */
     504              : static int
     505         8656 : charlen_to_bytelen(const char *p, int n)
     506              : {
     507         8656 :     if (pg_database_encoding_max_length() == 1)
     508              :     {
     509              :         /* Optimization for single-byte encodings */
     510           90 :         return n;
     511              :     }
     512              :     else
     513              :     {
     514              :         const char *s;
     515              : 
     516      3031409 :         for (s = p; n > 0; n--)
     517      3022843 :             s += pg_mblen_unbounded(s); /* caller verified encoding */
     518              : 
     519         8566 :         return s - p;
     520              :     }
     521              : }
     522              : 
     523              : /*
     524              :  * text_substr()
     525              :  * Return a substring starting at the specified position.
     526              :  * - thomas 1997-12-31
     527              :  *
     528              :  * Input:
     529              :  *  - string
     530              :  *  - starting position (is one-based)
     531              :  *  - string length
     532              :  *
     533              :  * If the starting position is zero or less, then return from the start of the string
     534              :  *  adjusting the length to be consistent with the "negative start" per SQL.
     535              :  * If the length is less than zero, return the remaining string.
     536              :  *
     537              :  * Added multibyte support.
     538              :  * - Tatsuo Ishii 1998-4-21
     539              :  * Changed behavior if starting position is less than one to conform to SQL behavior.
     540              :  * Formerly returned the entire string; now returns a portion.
     541              :  * - Thomas Lockhart 1998-12-10
     542              :  * Now uses faster TOAST-slicing interface
     543              :  * - John Gray 2002-02-22
     544              :  * Remove "#ifdef MULTIBYTE" and test for encoding_max_length instead. Change
     545              :  * behaviors conflicting with SQL to meet SQL (if E = S + L < S throw
     546              :  * error; if E < 1, return '', not entire string). Fixed MB related bug when
     547              :  * S > LC and < LC + 4 sometimes garbage characters are returned.
     548              :  * - Joe Conway 2002-08-10
     549              :  */
     550              : Datum
     551       331104 : text_substr(PG_FUNCTION_ARGS)
     552              : {
     553       331104 :     PG_RETURN_TEXT_P(text_substring(PG_GETARG_DATUM(0),
     554              :                                     PG_GETARG_INT32(1),
     555              :                                     PG_GETARG_INT32(2),
     556              :                                     false));
     557              : }
     558              : 
     559              : /*
     560              :  * text_substr_no_len -
     561              :  *    Wrapper to avoid opr_sanity failure due to
     562              :  *    one function accepting a different number of args.
     563              :  */
     564              : Datum
     565           18 : text_substr_no_len(PG_FUNCTION_ARGS)
     566              : {
     567           18 :     PG_RETURN_TEXT_P(text_substring(PG_GETARG_DATUM(0),
     568              :                                     PG_GETARG_INT32(1),
     569              :                                     -1, true));
     570              : }
     571              : 
     572              : /*
     573              :  * text_substring -
     574              :  *  Does the real work for text_substr() and text_substr_no_len()
     575              :  *
     576              :  *  This is broken out so it can be called directly by other string processing
     577              :  *  functions.  Note that the argument is passed as a Datum, to indicate that
     578              :  *  it may still be in compressed/toasted form.  We can avoid detoasting all
     579              :  *  of it in some cases.
     580              :  *
     581              :  *  The result is always a freshly palloc'd datum.
     582              :  */
     583              : static text *
     584       351178 : text_substring(Datum str, int32 start, int32 length, bool length_not_specified)
     585              : {
     586       351178 :     int32       eml = pg_database_encoding_max_length();
     587       351178 :     int32       S = start;      /* start position */
     588              :     int32       S1;             /* adjusted start position */
     589              :     int32       L1;             /* adjusted substring length */
     590              :     int32       E;              /* end position, exclusive */
     591              : 
     592              :     /*
     593              :      * SQL99 says S can be zero or negative (which we don't document), but we
     594              :      * still must fetch from the start of the string.
     595              :      * https://www.postgresql.org/message-id/170905442373.643.11536838320909376197%40wrigleys.postgresql.org
     596              :      */
     597       351178 :     S1 = Max(S, 1);
     598              : 
     599              :     /* life is easy if the encoding max length is 1 */
     600       351178 :     if (eml == 1)
     601              :     {
     602           11 :         if (length_not_specified)   /* special case - get length to end of
     603              :                                      * string */
     604            0 :             L1 = -1;
     605           11 :         else if (length < 0)
     606              :         {
     607              :             /* SQL99 says to throw an error for E < S, i.e., negative length */
     608            0 :             ereport(ERROR,
     609              :                     (errcode(ERRCODE_SUBSTRING_ERROR),
     610              :                      errmsg("negative substring length not allowed")));
     611              :             L1 = -1;            /* silence stupider compilers */
     612              :         }
     613           11 :         else if (pg_add_s32_overflow(S, length, &E))
     614              :         {
     615              :             /*
     616              :              * L could be large enough for S + L to overflow, in which case
     617              :              * the substring must run to end of string.
     618              :              */
     619            0 :             L1 = -1;
     620              :         }
     621              :         else
     622              :         {
     623              :             /*
     624              :              * A zero or negative value for the end position can happen if the
     625              :              * start was negative or one. SQL99 says to return a zero-length
     626              :              * string.
     627              :              */
     628           11 :             if (E < 1)
     629            0 :                 return cstring_to_text("");
     630              : 
     631           11 :             L1 = E - S1;
     632              :         }
     633              : 
     634              :         /*
     635              :          * If the start position is past the end of the string, SQL99 says to
     636              :          * return a zero-length string -- DatumGetTextPSlice() will do that
     637              :          * for us.  We need only convert S1 to zero-based starting position.
     638              :          */
     639           11 :         return DatumGetTextPSlice(str, S1 - 1, L1);
     640              :     }
     641       351167 :     else if (eml > 1)
     642              :     {
     643              :         /*
     644              :          * When encoding max length is > 1, we can't get LC without
     645              :          * detoasting, so we'll grab a conservatively large slice now and go
     646              :          * back later to do the right thing
     647              :          */
     648              :         int32       slice_start;
     649              :         int32       slice_size;
     650              :         int32       slice_strlen;
     651              :         int32       slice_len;
     652              :         text       *slice;
     653              :         int32       E1;
     654              :         int32       i;
     655              :         char       *p;
     656              :         char       *s;
     657              :         text       *ret;
     658              : 
     659              :         /*
     660              :          * We need to start at position zero because there is no way to know
     661              :          * in advance which byte offset corresponds to the supplied start
     662              :          * position.
     663              :          */
     664       351167 :         slice_start = 0;
     665              : 
     666       351167 :         if (length_not_specified)   /* special case - get length to end of
     667              :                                      * string */
     668           38 :             E = slice_size = L1 = -1;
     669       351129 :         else if (length < 0)
     670              :         {
     671              :             /* SQL99 says to throw an error for E < S, i.e., negative length */
     672            6 :             ereport(ERROR,
     673              :                     (errcode(ERRCODE_SUBSTRING_ERROR),
     674              :                      errmsg("negative substring length not allowed")));
     675              :             E = slice_size = L1 = -1;   /* silence stupider compilers */
     676              :         }
     677       351123 :         else if (pg_add_s32_overflow(S, length, &E))
     678              :         {
     679              :             /*
     680              :              * L could be large enough for S + L to overflow, in which case
     681              :              * the substring must run to end of string.
     682              :              */
     683            3 :             slice_size = L1 = -1;
     684              :         }
     685              :         else
     686              :         {
     687              :             /*
     688              :              * Ending at position 1, exclusive, obviously yields an empty
     689              :              * string.  A zero or negative value can happen if the start was
     690              :              * negative or one. SQL99 says to return a zero-length string.
     691              :              */
     692       351120 :             if (E <= 1)
     693            6 :                 return cstring_to_text("");
     694              : 
     695              :             /*
     696              :              * if E is past the end of the string, the tuple toaster will
     697              :              * truncate the length for us
     698              :              */
     699       351114 :             L1 = E - S1;
     700              : 
     701              :             /*
     702              :              * Total slice size in bytes can't be any longer than the
     703              :              * inclusive end position times the encoding max length.  If that
     704              :              * overflows, we can just use -1.
     705              :              */
     706       351114 :             if (pg_mul_s32_overflow(E - 1, eml, &slice_size))
     707            3 :                 slice_size = -1;
     708              :         }
     709              : 
     710              :         /*
     711              :          * If we're working with an untoasted source, no need to do an extra
     712              :          * copying step.
     713              :          */
     714       702262 :         if (VARATT_IS_COMPRESSED(DatumGetPointer(str)) ||
     715       351107 :             VARATT_IS_EXTERNAL(DatumGetPointer(str)))
     716          201 :             slice = DatumGetTextPSlice(str, slice_start, slice_size);
     717              :         else
     718       350954 :             slice = (text *) DatumGetPointer(str);
     719              : 
     720              :         /* see if we got back an empty string */
     721       351155 :         slice_len = VARSIZE_ANY_EXHDR(slice);
     722       351155 :         if (slice_len == 0)
     723              :         {
     724            0 :             if (slice != (text *) DatumGetPointer(str))
     725            0 :                 pfree(slice);
     726            0 :             return cstring_to_text("");
     727              :         }
     728              : 
     729              :         /*
     730              :          * Now we can get the actual length of the slice in MB characters,
     731              :          * stopping at the end of the substring.  Continuing beyond the
     732              :          * substring end could find an incomplete character attributable
     733              :          * solely to DatumGetTextPSlice() chopping in the middle of a
     734              :          * character, and it would be superfluous work at best.
     735              :          */
     736       351149 :         slice_strlen =
     737       351155 :             (slice_size == -1 ?
     738       351155 :              pg_mbstrlen_with_len(VARDATA_ANY(slice), slice_len) :
     739       351111 :              pg_mbcharcliplen_chars(VARDATA_ANY(slice), slice_len, E - 1));
     740              : 
     741              :         /*
     742              :          * Check that the start position wasn't > slice_strlen. If so, SQL99
     743              :          * says to return a zero-length string.
     744              :          */
     745       351149 :         if (S1 > slice_strlen)
     746              :         {
     747           20 :             if (slice != (text *) DatumGetPointer(str))
     748            3 :                 pfree(slice);
     749           20 :             return cstring_to_text("");
     750              :         }
     751              : 
     752              :         /*
     753              :          * Adjust L1 and E1 now that we know the slice string length. Again
     754              :          * remember that S1 is one based, and slice_start is zero based.
     755              :          */
     756       351129 :         if (L1 > -1)
     757       351099 :             E1 = Min(S1 + L1, slice_start + 1 + slice_strlen);
     758              :         else
     759           30 :             E1 = slice_start + 1 + slice_strlen;
     760              : 
     761              :         /*
     762              :          * Find the start position in the slice; remember S1 is not zero based
     763              :          */
     764       351129 :         p = VARDATA_ANY(slice);
     765      3379462 :         for (i = 0; i < S1 - 1; i++)
     766      3028333 :             p += pg_mblen_unbounded(p);
     767              : 
     768              :         /* hang onto a pointer to our start position */
     769       351129 :         s = p;
     770              : 
     771              :         /*
     772              :          * Count the actual bytes used by the substring of the requested
     773              :          * length.
     774              :          */
     775      4982588 :         for (i = S1; i < E1; i++)
     776      4631459 :             p += pg_mblen_unbounded(p);
     777              : 
     778       351129 :         ret = (text *) palloc(VARHDRSZ + (p - s));
     779       351129 :         SET_VARSIZE(ret, VARHDRSZ + (p - s));
     780       351129 :         memcpy(VARDATA(ret), s, (p - s));
     781              : 
     782       351129 :         if (slice != (text *) DatumGetPointer(str))
     783          195 :             pfree(slice);
     784              : 
     785       351129 :         return ret;
     786              :     }
     787              :     else
     788            0 :         elog(ERROR, "invalid backend encoding: encoding max length < 1");
     789              : 
     790              :     /* not reached: suppress compiler warning */
     791              :     return NULL;
     792              : }
     793              : 
     794              : /*
     795              :  * pg_mbcharcliplen_chars -
     796              :  *  Mirror pg_mbcharcliplen(), except return value unit is chars, not bytes.
     797              :  *
     798              :  *  This mirrors all the dubious historical behavior, so it's static to
     799              :  *  discourage proliferation.  The assertions are specific to the one caller.
     800              :  */
     801              : static int
     802       351111 : pg_mbcharcliplen_chars(const char *mbstr, int len, int limit)
     803              : {
     804       351111 :     int         nch = 0;
     805              :     int         l;
     806              : 
     807              :     Assert(len > 0);
     808              :     Assert(limit > 0);
     809              :     Assert(pg_database_encoding_max_length() > 1);
     810              : 
     811      6484001 :     while (len > 0 && *mbstr)
     812              :     {
     813      6483686 :         l = pg_mblen_with_len(mbstr, len);
     814      6483680 :         nch++;
     815      6483680 :         if (nch == limit)
     816       350790 :             break;
     817      6132890 :         len -= l;
     818      6132890 :         mbstr += l;
     819              :     }
     820       351105 :     return nch;
     821              : }
     822              : 
     823              : /*
     824              :  * textoverlay
     825              :  *  Replace specified substring of first string with second
     826              :  *
     827              :  * The SQL standard defines OVERLAY() in terms of substring and concatenation.
     828              :  * This code is a direct implementation of what the standard says.
     829              :  */
     830              : Datum
     831           14 : textoverlay(PG_FUNCTION_ARGS)
     832              : {
     833           14 :     text       *t1 = PG_GETARG_TEXT_PP(0);
     834           14 :     text       *t2 = PG_GETARG_TEXT_PP(1);
     835           14 :     int         sp = PG_GETARG_INT32(2);    /* substring start position */
     836           14 :     int         sl = PG_GETARG_INT32(3);    /* substring length */
     837              : 
     838           14 :     PG_RETURN_TEXT_P(text_overlay(t1, t2, sp, sl));
     839              : }
     840              : 
     841              : Datum
     842            6 : textoverlay_no_len(PG_FUNCTION_ARGS)
     843              : {
     844            6 :     text       *t1 = PG_GETARG_TEXT_PP(0);
     845            6 :     text       *t2 = PG_GETARG_TEXT_PP(1);
     846            6 :     int         sp = PG_GETARG_INT32(2);    /* substring start position */
     847              :     int         sl;
     848              : 
     849            6 :     sl = text_length(PointerGetDatum(t2));  /* defaults to length(t2) */
     850            6 :     PG_RETURN_TEXT_P(text_overlay(t1, t2, sp, sl));
     851              : }
     852              : 
     853              : static text *
     854           20 : text_overlay(text *t1, text *t2, int sp, int sl)
     855              : {
     856              :     text       *result;
     857              :     text       *s1;
     858              :     text       *s2;
     859              :     int         sp_pl_sl;
     860              : 
     861              :     /*
     862              :      * Check for possible integer-overflow cases.  For negative sp, throw a
     863              :      * "substring length" error because that's what should be expected
     864              :      * according to the spec's definition of OVERLAY().
     865              :      */
     866           20 :     if (sp <= 0)
     867            0 :         ereport(ERROR,
     868              :                 (errcode(ERRCODE_SUBSTRING_ERROR),
     869              :                  errmsg("negative substring length not allowed")));
     870           20 :     if (pg_add_s32_overflow(sp, sl, &sp_pl_sl))
     871            0 :         ereport(ERROR,
     872              :                 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
     873              :                  errmsg("integer out of range")));
     874              : 
     875           20 :     s1 = text_substring(PointerGetDatum(t1), 1, sp - 1, false);
     876           20 :     s2 = text_substring(PointerGetDatum(t1), sp_pl_sl, -1, true);
     877           20 :     result = text_catenate(s1, t2);
     878           20 :     result = text_catenate(result, s2);
     879              : 
     880           20 :     return result;
     881              : }
     882              : 
     883              : /*
     884              :  * textpos -
     885              :  *    Return the position of the specified substring.
     886              :  *    Implements the SQL POSITION() function.
     887              :  *    Ref: A Guide To The SQL Standard, Date & Darwen, 1997
     888              :  * - thomas 1997-07-27
     889              :  */
     890              : Datum
     891           68 : textpos(PG_FUNCTION_ARGS)
     892              : {
     893           68 :     text       *str = PG_GETARG_TEXT_PP(0);
     894           68 :     text       *search_str = PG_GETARG_TEXT_PP(1);
     895              : 
     896           68 :     PG_RETURN_INT32((int32) text_position(str, search_str, PG_GET_COLLATION()));
     897              : }
     898              : 
     899              : /*
     900              :  * text_position -
     901              :  *  Does the real work for textpos()
     902              :  *
     903              :  * Inputs:
     904              :  *      t1 - string to be searched
     905              :  *      t2 - pattern to match within t1
     906              :  * Result:
     907              :  *      Character index of the first matched char, starting from 1,
     908              :  *      or 0 if no match.
     909              :  *
     910              :  *  This is broken out so it can be called directly by other string processing
     911              :  *  functions.
     912              :  */
     913              : static int
     914           68 : text_position(text *t1, text *t2, Oid collid)
     915              : {
     916              :     TextPositionState state;
     917              :     int         result;
     918              : 
     919           68 :     check_collation_set(collid);
     920              : 
     921              :     /* Empty needle always matches at position 1 */
     922           68 :     if (VARSIZE_ANY_EXHDR(t2) < 1)
     923            6 :         return 1;
     924              : 
     925              :     /* Otherwise, can't match if haystack is shorter than needle */
     926           62 :     if (VARSIZE_ANY_EXHDR(t1) < VARSIZE_ANY_EXHDR(t2) &&
     927           11 :         pg_newlocale_from_collation(collid)->deterministic)
     928           11 :         return 0;
     929              : 
     930           51 :     text_position_setup(t1, t2, collid, &state);
     931              :     /* don't need greedy mode here */
     932           51 :     state.greedy = false;
     933              : 
     934           51 :     if (!text_position_next(&state))
     935           12 :         result = 0;
     936              :     else
     937           39 :         result = text_position_get_match_pos(&state);
     938           51 :     text_position_cleanup(&state);
     939           51 :     return result;
     940              : }
     941              : 
     942              : 
     943              : /*
     944              :  * text_position_setup, text_position_next, text_position_cleanup -
     945              :  *  Component steps of text_position()
     946              :  *
     947              :  * These are broken out so that a string can be efficiently searched for
     948              :  * multiple occurrences of the same pattern.  text_position_next may be
     949              :  * called multiple times, and it advances to the next match on each call.
     950              :  * text_position_get_match_ptr() and text_position_get_match_pos() return
     951              :  * a pointer or 1-based character position of the last match, respectively.
     952              :  *
     953              :  * The "state" variable is normally just a local variable in the caller.
     954              :  *
     955              :  * NOTE: text_position_next skips over the matched portion.  For example,
     956              :  * searching for "xx" in "xxx" returns only one match, not two.
     957              :  */
     958              : 
     959              : static void
     960          965 : text_position_setup(text *t1, text *t2, Oid collid, TextPositionState *state)
     961              : {
     962          965 :     int         len1 = VARSIZE_ANY_EXHDR(t1);
     963          965 :     int         len2 = VARSIZE_ANY_EXHDR(t2);
     964              : 
     965          965 :     check_collation_set(collid);
     966              : 
     967          965 :     state->locale = pg_newlocale_from_collation(collid);
     968              : 
     969              :     /*
     970              :      * Most callers need greedy mode, but some might want to unset this to
     971              :      * optimize.
     972              :      */
     973          965 :     state->greedy = true;
     974              : 
     975              :     Assert(len2 > 0);
     976              : 
     977              :     /*
     978              :      * Even with a multi-byte encoding, we perform the search using the raw
     979              :      * byte sequence, ignoring multibyte issues.  For UTF-8, that works fine,
     980              :      * because in UTF-8 the byte sequence of one character cannot contain
     981              :      * another character.  For other multi-byte encodings, we do the search
     982              :      * initially as a simple byte search, ignoring multibyte issues, but
     983              :      * verify afterwards that the match we found is at a character boundary,
     984              :      * and continue the search if it was a false match.
     985              :      */
     986          965 :     if (pg_database_encoding_max_length() == 1)
     987           54 :         state->is_multibyte_char_in_char = false;
     988          911 :     else if (GetDatabaseEncoding() == PG_UTF8)
     989          911 :         state->is_multibyte_char_in_char = false;
     990              :     else
     991            0 :         state->is_multibyte_char_in_char = true;
     992              : 
     993          965 :     state->str1 = VARDATA_ANY(t1);
     994          965 :     state->str2 = VARDATA_ANY(t2);
     995          965 :     state->len1 = len1;
     996          965 :     state->len2 = len2;
     997          965 :     state->last_match = NULL;
     998          965 :     state->refpoint = state->str1;
     999          965 :     state->refpos = 0;
    1000              : 
    1001              :     /*
    1002              :      * Prepare the skip table for Boyer-Moore-Horspool searching.  In these
    1003              :      * notes we use the terminology that the "haystack" is the string to be
    1004              :      * searched (t1) and the "needle" is the pattern being sought (t2).
    1005              :      *
    1006              :      * If the needle is empty or bigger than the haystack then there is no
    1007              :      * point in wasting cycles initializing the table.  We also choose not to
    1008              :      * use B-M-H for needles of length 1, since the skip table can't possibly
    1009              :      * save anything in that case.
    1010              :      *
    1011              :      * (With nondeterministic collations, the search is already
    1012              :      * multibyte-aware, so we don't need this.)
    1013              :      */
    1014          965 :     if (len1 >= len2 && len2 > 1 && state->locale->deterministic)
    1015              :     {
    1016          798 :         int         searchlength = len1 - len2;
    1017              :         int         skiptablemask;
    1018              :         int         last;
    1019              :         int         i;
    1020          798 :         const char *str2 = state->str2;
    1021              : 
    1022              :         /*
    1023              :          * First we must determine how much of the skip table to use.  The
    1024              :          * declaration of TextPositionState allows up to 256 elements, but for
    1025              :          * short search problems we don't really want to have to initialize so
    1026              :          * many elements --- it would take too long in comparison to the
    1027              :          * actual search time.  So we choose a useful skip table size based on
    1028              :          * the haystack length minus the needle length.  The closer the needle
    1029              :          * length is to the haystack length the less useful skipping becomes.
    1030              :          *
    1031              :          * Note: since we use bit-masking to select table elements, the skip
    1032              :          * table size MUST be a power of 2, and so the mask must be 2^N-1.
    1033              :          */
    1034          798 :         if (searchlength < 16)
    1035           57 :             skiptablemask = 3;
    1036          741 :         else if (searchlength < 64)
    1037           17 :             skiptablemask = 7;
    1038          724 :         else if (searchlength < 128)
    1039           13 :             skiptablemask = 15;
    1040          711 :         else if (searchlength < 512)
    1041          166 :             skiptablemask = 31;
    1042          545 :         else if (searchlength < 2048)
    1043          404 :             skiptablemask = 63;
    1044          141 :         else if (searchlength < 4096)
    1045           99 :             skiptablemask = 127;
    1046              :         else
    1047           42 :             skiptablemask = 255;
    1048          798 :         state->skiptablemask = skiptablemask;
    1049              : 
    1050              :         /*
    1051              :          * Initialize the skip table.  We set all elements to the needle
    1052              :          * length, since this is the correct skip distance for any character
    1053              :          * not found in the needle.
    1054              :          */
    1055        55962 :         for (i = 0; i <= skiptablemask; i++)
    1056        55164 :             state->skiptable[i] = len2;
    1057              : 
    1058              :         /*
    1059              :          * Now examine the needle.  For each character except the last one,
    1060              :          * set the corresponding table element to the appropriate skip
    1061              :          * distance.  Note that when two characters share the same skip table
    1062              :          * entry, the one later in the needle must determine the skip
    1063              :          * distance.
    1064              :          */
    1065          798 :         last = len2 - 1;
    1066              : 
    1067        10181 :         for (i = 0; i < last; i++)
    1068         9383 :             state->skiptable[(unsigned char) str2[i] & skiptablemask] = last - i;
    1069              :     }
    1070          965 : }
    1071              : 
    1072              : /*
    1073              :  * Advance to the next match, starting from the end of the previous match
    1074              :  * (or the beginning of the string, on first call).  Returns true if a match
    1075              :  * is found.
    1076              :  *
    1077              :  * Note that this refuses to match an empty-string needle.  Most callers
    1078              :  * will have handled that case specially and we'll never see it here.
    1079              :  */
    1080              : static bool
    1081         4886 : text_position_next(TextPositionState *state)
    1082              : {
    1083         4886 :     int         needle_len = state->len2;
    1084              :     char       *start_ptr;
    1085              :     char       *matchptr;
    1086              : 
    1087         4886 :     if (needle_len <= 0)
    1088            0 :         return false;           /* result for empty pattern */
    1089              : 
    1090              :     /* Start from the point right after the previous match. */
    1091         4886 :     if (state->last_match)
    1092         3915 :         start_ptr = state->last_match + state->last_match_len;
    1093              :     else
    1094          971 :         start_ptr = state->str1;
    1095              : 
    1096         4886 : retry:
    1097         4886 :     matchptr = text_position_next_internal(start_ptr, state);
    1098              : 
    1099         4886 :     if (!matchptr)
    1100          920 :         return false;
    1101              : 
    1102              :     /*
    1103              :      * Found a match for the byte sequence.  If this is a multibyte encoding,
    1104              :      * where one character's byte sequence can appear inside a longer
    1105              :      * multi-byte character, we need to verify that the match was at a
    1106              :      * character boundary, not in the middle of a multi-byte character.
    1107              :      */
    1108         3966 :     if (state->is_multibyte_char_in_char && state->locale->deterministic)
    1109              :     {
    1110            0 :         const char *haystack_end = state->str1 + state->len1;
    1111              : 
    1112              :         /* Walk one character at a time, until we reach the match. */
    1113              : 
    1114              :         /* the search should never move backwards. */
    1115              :         Assert(state->refpoint <= matchptr);
    1116              : 
    1117            0 :         while (state->refpoint < matchptr)
    1118              :         {
    1119              :             /* step to next character. */
    1120            0 :             state->refpoint += pg_mblen_range(state->refpoint, haystack_end);
    1121            0 :             state->refpos++;
    1122              : 
    1123              :             /*
    1124              :              * If we stepped over the match's start position, then it was a
    1125              :              * false positive, where the byte sequence appeared in the middle
    1126              :              * of a multi-byte character.  Skip it, and continue the search at
    1127              :              * the next character boundary.
    1128              :              */
    1129            0 :             if (state->refpoint > matchptr)
    1130              :             {
    1131            0 :                 start_ptr = state->refpoint;
    1132            0 :                 goto retry;
    1133              :             }
    1134              :         }
    1135              :     }
    1136              : 
    1137         3966 :     state->last_match = matchptr;
    1138         3966 :     state->last_match_len = state->last_match_len_tmp;
    1139         3966 :     return true;
    1140              : }
    1141              : 
    1142              : /*
    1143              :  * Subroutine of text_position_next().  This searches for the raw byte
    1144              :  * sequence, ignoring any multi-byte encoding issues.  Returns the first
    1145              :  * match starting at 'start_ptr', or NULL if no match is found.
    1146              :  */
    1147              : static char *
    1148         4886 : text_position_next_internal(char *start_ptr, TextPositionState *state)
    1149              : {
    1150         4886 :     int         haystack_len = state->len1;
    1151         4886 :     int         needle_len = state->len2;
    1152         4886 :     int         skiptablemask = state->skiptablemask;
    1153         4886 :     const char *haystack = state->str1;
    1154         4886 :     const char *needle = state->str2;
    1155         4886 :     const char *haystack_end = &haystack[haystack_len];
    1156              :     const char *hptr;
    1157              : 
    1158              :     Assert(start_ptr >= haystack && start_ptr <= haystack_end);
    1159              :     Assert(needle_len > 0);
    1160              : 
    1161         4886 :     state->last_match_len_tmp = needle_len;
    1162              : 
    1163         4886 :     if (!state->locale->deterministic)
    1164              :     {
    1165              :         /*
    1166              :          * With a nondeterministic collation, we have to use an unoptimized
    1167              :          * route.  We walk through the haystack and see if at each position
    1168              :          * there is a substring of the remaining string that is equal to the
    1169              :          * needle under the given collation.
    1170              :          *
    1171              :          * Note, the found substring could have a different length than the
    1172              :          * needle.  Callers that want to skip over the found string need to
    1173              :          * read the length of the found substring from last_match_len rather
    1174              :          * than just using the length of their needle.
    1175              :          *
    1176              :          * Most callers will require "greedy" semantics, meaning that we need
    1177              :          * to find the longest such substring, not the shortest.  For callers
    1178              :          * that don't need greedy semantics, we can finish on the first match.
    1179              :          *
    1180              :          * This loop depends on the assumption that the needle is nonempty and
    1181              :          * any matching substring must also be nonempty.  (Even if the
    1182              :          * collation would accept an empty match, returning one would send
    1183              :          * callers that search for successive matches into an infinite loop.)
    1184              :          */
    1185          126 :         const char *result_hptr = NULL;
    1186              : 
    1187          126 :         hptr = start_ptr;
    1188          339 :         while (hptr < haystack_end)
    1189              :         {
    1190              :             const char *test_end;
    1191              : 
    1192              :             /*
    1193              :              * First check the common case that there is a match in the
    1194              :              * haystack of exactly the length of the needle.
    1195              :              */
    1196          282 :             if (!state->greedy &&
    1197           54 :                 haystack_end - hptr >= needle_len &&
    1198           27 :                 pg_strncoll(hptr, needle_len, needle, needle_len, state->locale) == 0)
    1199            6 :                 return (char *) hptr;
    1200              : 
    1201              :             /*
    1202              :              * Else check if any of the non-empty substrings starting at hptr
    1203              :              * compare equal to the needle.
    1204              :              */
    1205          276 :             test_end = hptr;
    1206              :             do
    1207              :             {
    1208         1077 :                 test_end += pg_mblen_range(test_end, haystack_end);
    1209         1077 :                 if (pg_strncoll(hptr, (test_end - hptr), needle, needle_len, state->locale) == 0)
    1210              :                 {
    1211           69 :                     state->last_match_len_tmp = (test_end - hptr);
    1212           69 :                     result_hptr = hptr;
    1213           69 :                     if (!state->greedy)
    1214            0 :                         break;
    1215              :                 }
    1216         1077 :             } while (test_end < haystack_end);
    1217              : 
    1218          276 :             if (result_hptr)
    1219           63 :                 break;
    1220              : 
    1221          213 :             hptr += pg_mblen_range(hptr, haystack_end);
    1222              :         }
    1223              : 
    1224          120 :         return (char *) result_hptr;
    1225              :     }
    1226         4760 :     else if (needle_len == 1)
    1227              :     {
    1228              :         /* No point in using B-M-H for a one-character needle */
    1229          380 :         char        nchar = *needle;
    1230              : 
    1231          380 :         hptr = start_ptr;
    1232         2939 :         while (hptr < haystack_end)
    1233              :         {
    1234         2856 :             if (*hptr == nchar)
    1235          297 :                 return (char *) hptr;
    1236         2559 :             hptr++;
    1237              :         }
    1238              :     }
    1239              :     else
    1240              :     {
    1241         4380 :         const char *needle_last = &needle[needle_len - 1];
    1242              : 
    1243              :         /* Start at startpos plus the length of the needle */
    1244         4380 :         hptr = start_ptr + needle_len - 1;
    1245       108360 :         while (hptr < haystack_end)
    1246              :         {
    1247              :             /* Match the needle scanning *backward* */
    1248              :             const char *nptr;
    1249              :             const char *p;
    1250              : 
    1251       107580 :             nptr = needle_last;
    1252       107580 :             p = hptr;
    1253       161573 :             while (*nptr == *p)
    1254              :             {
    1255              :                 /* Matched it all?  If so, return 1-based position */
    1256        57593 :                 if (nptr == needle)
    1257         3600 :                     return (char *) p;
    1258        53993 :                 nptr--, p--;
    1259              :             }
    1260              : 
    1261              :             /*
    1262              :              * No match, so use the haystack char at hptr to decide how far to
    1263              :              * advance.  If the needle had any occurrence of that character
    1264              :              * (or more precisely, one sharing the same skiptable entry)
    1265              :              * before its last character, then we advance far enough to align
    1266              :              * the last such needle character with that haystack position.
    1267              :              * Otherwise we can advance by the whole needle length.
    1268              :              */
    1269       103980 :             hptr += state->skiptable[(unsigned char) *hptr & skiptablemask];
    1270              :         }
    1271              :     }
    1272              : 
    1273          863 :     return 0;                   /* not found */
    1274              : }
    1275              : 
    1276              : /*
    1277              :  * Return a pointer to the current match.
    1278              :  *
    1279              :  * The returned pointer points into the original haystack string.
    1280              :  */
    1281              : static char *
    1282         3912 : text_position_get_match_ptr(TextPositionState *state)
    1283              : {
    1284         3912 :     return state->last_match;
    1285              : }
    1286              : 
    1287              : /*
    1288              :  * Return the offset of the current match.
    1289              :  *
    1290              :  * The offset is in characters, 1-based.
    1291              :  */
    1292              : static int
    1293           39 : text_position_get_match_pos(TextPositionState *state)
    1294              : {
    1295              :     /* Convert the byte position to char position. */
    1296           78 :     state->refpos += pg_mbstrlen_with_len(state->refpoint,
    1297           39 :                                           state->last_match - state->refpoint);
    1298           39 :     state->refpoint = state->last_match;
    1299           39 :     return state->refpos + 1;
    1300              : }
    1301              : 
    1302              : /*
    1303              :  * Reset search state to the initial state installed by text_position_setup.
    1304              :  *
    1305              :  * The next call to text_position_next will search from the beginning
    1306              :  * of the string.
    1307              :  */
    1308              : static void
    1309            6 : text_position_reset(TextPositionState *state)
    1310              : {
    1311            6 :     state->last_match = NULL;
    1312            6 :     state->refpoint = state->str1;
    1313            6 :     state->refpos = 0;
    1314            6 : }
    1315              : 
    1316              : static void
    1317          965 : text_position_cleanup(TextPositionState *state)
    1318              : {
    1319              :     /* no cleanup needed */
    1320          965 : }
    1321              : 
    1322              : 
    1323              : static void
    1324      8697842 : check_collation_set(Oid collid)
    1325              : {
    1326      8697842 :     if (!OidIsValid(collid))
    1327              :     {
    1328              :         /*
    1329              :          * This typically means that the parser could not resolve a conflict
    1330              :          * of implicit collations, so report it that way.
    1331              :          */
    1332           15 :         ereport(ERROR,
    1333              :                 (errcode(ERRCODE_INDETERMINATE_COLLATION),
    1334              :                  errmsg("could not determine which collation to use for string comparison"),
    1335              :                  errhint("Use the COLLATE clause to set the collation explicitly.")));
    1336              :     }
    1337      8697827 : }
    1338              : 
    1339              : /*
    1340              :  * varstr_cmp()
    1341              :  *
    1342              :  * Comparison function for text strings with given lengths, using the
    1343              :  * appropriate locale. Returns an integer less than, equal to, or greater than
    1344              :  * zero, indicating whether arg1 is less than, equal to, or greater than arg2.
    1345              :  *
    1346              :  * Note: many functions that depend on this are marked leakproof; therefore,
    1347              :  * avoid reporting the actual contents of the input when throwing errors.
    1348              :  * All errors herein should be things that can't happen except on corrupt
    1349              :  * data, anyway; otherwise we will have trouble with indexing strings that
    1350              :  * would cause them.
    1351              :  */
    1352              : int
    1353      4931775 : varstr_cmp(const char *arg1, int len1, const char *arg2, int len2, Oid collid)
    1354              : {
    1355              :     int         result;
    1356              :     pg_locale_t mylocale;
    1357              : 
    1358      4931775 :     check_collation_set(collid);
    1359              : 
    1360      4931766 :     mylocale = pg_newlocale_from_collation(collid);
    1361              : 
    1362      4931766 :     if (mylocale->collate_is_c)
    1363              :     {
    1364      1900777 :         result = memcmp(arg1, arg2, Min(len1, len2));
    1365      1900777 :         if ((result == 0) && (len1 != len2))
    1366        67130 :             result = (len1 < len2) ? -1 : 1;
    1367              :     }
    1368              :     else
    1369              :     {
    1370              :         /*
    1371              :          * memcmp() can't tell us which of two unequal strings sorts first,
    1372              :          * but it's a cheap way to tell if they're equal.  Testing shows that
    1373              :          * memcmp() followed by strcoll() is only trivially slower than
    1374              :          * strcoll() by itself, so we don't lose much if this doesn't work out
    1375              :          * very often, and if it does - for example, because there are many
    1376              :          * equal strings in the input - then we win big by avoiding expensive
    1377              :          * collation-aware comparisons.
    1378              :          */
    1379      3030989 :         if (len1 == len2 && memcmp(arg1, arg2, len1) == 0)
    1380       783435 :             return 0;
    1381              : 
    1382      2247554 :         result = pg_strncoll(arg1, len1, arg2, len2, mylocale);
    1383              : 
    1384              :         /* Break tie if necessary. */
    1385      2247554 :         if (result == 0 && mylocale->deterministic)
    1386              :         {
    1387            0 :             result = memcmp(arg1, arg2, Min(len1, len2));
    1388            0 :             if ((result == 0) && (len1 != len2))
    1389            0 :                 result = (len1 < len2) ? -1 : 1;
    1390              :         }
    1391              :     }
    1392              : 
    1393      4148331 :     return result;
    1394              : }
    1395              : 
    1396              : /* text_cmp()
    1397              :  * Internal comparison function for text strings.
    1398              :  * Returns -1, 0 or 1
    1399              :  */
    1400              : static int
    1401      3960253 : text_cmp(text *arg1, text *arg2, Oid collid)
    1402              : {
    1403              :     char       *a1p,
    1404              :                *a2p;
    1405              :     int         len1,
    1406              :                 len2;
    1407              : 
    1408      3960253 :     a1p = VARDATA_ANY(arg1);
    1409      3960253 :     a2p = VARDATA_ANY(arg2);
    1410              : 
    1411      3960253 :     len1 = VARSIZE_ANY_EXHDR(arg1);
    1412      3960253 :     len2 = VARSIZE_ANY_EXHDR(arg2);
    1413              : 
    1414      3960253 :     return varstr_cmp(a1p, len1, a2p, len2, collid);
    1415              : }
    1416              : 
    1417              : /*
    1418              :  * Comparison functions for text strings.
    1419              :  *
    1420              :  * Note: btree indexes need these routines not to leak memory; therefore,
    1421              :  * be careful to free working copies of toasted datums.  Most places don't
    1422              :  * need to be so careful.
    1423              :  */
    1424              : 
    1425              : Datum
    1426      3350390 : texteq(PG_FUNCTION_ARGS)
    1427              : {
    1428      3350390 :     Oid         collid = PG_GET_COLLATION();
    1429      3350390 :     pg_locale_t mylocale = 0;
    1430              :     bool        result;
    1431              : 
    1432      3350390 :     check_collation_set(collid);
    1433              : 
    1434      3350390 :     mylocale = pg_newlocale_from_collation(collid);
    1435              : 
    1436      3350390 :     if (mylocale->deterministic)
    1437              :     {
    1438      3348210 :         Datum       arg1 = PG_GETARG_DATUM(0);
    1439      3348210 :         Datum       arg2 = PG_GETARG_DATUM(1);
    1440              :         Size        len1,
    1441              :                     len2;
    1442              : 
    1443              :         /*
    1444              :          * Since we only care about equality or not-equality, we can avoid all
    1445              :          * the expense of strcoll() here, and just do bitwise comparison.  In
    1446              :          * fact, we don't even have to do a bitwise comparison if we can show
    1447              :          * the lengths of the strings are unequal; which might save us from
    1448              :          * having to detoast one or both values.
    1449              :          */
    1450      3348210 :         len1 = toast_raw_datum_size(arg1);
    1451      3348210 :         len2 = toast_raw_datum_size(arg2);
    1452      3348210 :         if (len1 != len2)
    1453      1592363 :             result = false;
    1454              :         else
    1455              :         {
    1456      1755847 :             text       *targ1 = DatumGetTextPP(arg1);
    1457      1755847 :             text       *targ2 = DatumGetTextPP(arg2);
    1458              : 
    1459      1755847 :             result = (memcmp(VARDATA_ANY(targ1), VARDATA_ANY(targ2),
    1460              :                              len1 - VARHDRSZ) == 0);
    1461              : 
    1462      1755847 :             PG_FREE_IF_COPY(targ1, 0);
    1463      1755847 :             PG_FREE_IF_COPY(targ2, 1);
    1464              :         }
    1465              :     }
    1466              :     else
    1467              :     {
    1468         2180 :         text       *arg1 = PG_GETARG_TEXT_PP(0);
    1469         2180 :         text       *arg2 = PG_GETARG_TEXT_PP(1);
    1470              : 
    1471         2180 :         result = (text_cmp(arg1, arg2, collid) == 0);
    1472              : 
    1473         2180 :         PG_FREE_IF_COPY(arg1, 0);
    1474         2180 :         PG_FREE_IF_COPY(arg2, 1);
    1475              :     }
    1476              : 
    1477      3350390 :     PG_RETURN_BOOL(result);
    1478              : }
    1479              : 
    1480              : Datum
    1481       204301 : textne(PG_FUNCTION_ARGS)
    1482              : {
    1483       204301 :     Oid         collid = PG_GET_COLLATION();
    1484              :     pg_locale_t mylocale;
    1485              :     bool        result;
    1486              : 
    1487       204301 :     check_collation_set(collid);
    1488              : 
    1489       204301 :     mylocale = pg_newlocale_from_collation(collid);
    1490              : 
    1491       204301 :     if (mylocale->deterministic)
    1492              :     {
    1493       204289 :         Datum       arg1 = PG_GETARG_DATUM(0);
    1494       204289 :         Datum       arg2 = PG_GETARG_DATUM(1);
    1495              :         Size        len1,
    1496              :                     len2;
    1497              : 
    1498              :         /* See comment in texteq() */
    1499       204289 :         len1 = toast_raw_datum_size(arg1);
    1500       204289 :         len2 = toast_raw_datum_size(arg2);
    1501       204289 :         if (len1 != len2)
    1502        11185 :             result = true;
    1503              :         else
    1504              :         {
    1505       193104 :             text       *targ1 = DatumGetTextPP(arg1);
    1506       193104 :             text       *targ2 = DatumGetTextPP(arg2);
    1507              : 
    1508       193104 :             result = (memcmp(VARDATA_ANY(targ1), VARDATA_ANY(targ2),
    1509              :                              len1 - VARHDRSZ) != 0);
    1510              : 
    1511       193104 :             PG_FREE_IF_COPY(targ1, 0);
    1512       193104 :             PG_FREE_IF_COPY(targ2, 1);
    1513              :         }
    1514              :     }
    1515              :     else
    1516              :     {
    1517           12 :         text       *arg1 = PG_GETARG_TEXT_PP(0);
    1518           12 :         text       *arg2 = PG_GETARG_TEXT_PP(1);
    1519              : 
    1520           12 :         result = (text_cmp(arg1, arg2, collid) != 0);
    1521              : 
    1522           12 :         PG_FREE_IF_COPY(arg1, 0);
    1523           12 :         PG_FREE_IF_COPY(arg2, 1);
    1524              :     }
    1525              : 
    1526       204301 :     PG_RETURN_BOOL(result);
    1527              : }
    1528              : 
    1529              : Datum
    1530       104647 : text_lt(PG_FUNCTION_ARGS)
    1531              : {
    1532       104647 :     text       *arg1 = PG_GETARG_TEXT_PP(0);
    1533       104647 :     text       *arg2 = PG_GETARG_TEXT_PP(1);
    1534              :     bool        result;
    1535              : 
    1536       104647 :     result = (text_cmp(arg1, arg2, PG_GET_COLLATION()) < 0);
    1537              : 
    1538       104638 :     PG_FREE_IF_COPY(arg1, 0);
    1539       104638 :     PG_FREE_IF_COPY(arg2, 1);
    1540              : 
    1541       104638 :     PG_RETURN_BOOL(result);
    1542              : }
    1543              : 
    1544              : Datum
    1545       158863 : text_le(PG_FUNCTION_ARGS)
    1546              : {
    1547       158863 :     text       *arg1 = PG_GETARG_TEXT_PP(0);
    1548       158863 :     text       *arg2 = PG_GETARG_TEXT_PP(1);
    1549              :     bool        result;
    1550              : 
    1551       158863 :     result = (text_cmp(arg1, arg2, PG_GET_COLLATION()) <= 0);
    1552              : 
    1553       158863 :     PG_FREE_IF_COPY(arg1, 0);
    1554       158863 :     PG_FREE_IF_COPY(arg2, 1);
    1555              : 
    1556       158863 :     PG_RETURN_BOOL(result);
    1557              : }
    1558              : 
    1559              : Datum
    1560        98038 : text_gt(PG_FUNCTION_ARGS)
    1561              : {
    1562        98038 :     text       *arg1 = PG_GETARG_TEXT_PP(0);
    1563        98038 :     text       *arg2 = PG_GETARG_TEXT_PP(1);
    1564              :     bool        result;
    1565              : 
    1566        98038 :     result = (text_cmp(arg1, arg2, PG_GET_COLLATION()) > 0);
    1567              : 
    1568        98038 :     PG_FREE_IF_COPY(arg1, 0);
    1569        98038 :     PG_FREE_IF_COPY(arg2, 1);
    1570              : 
    1571        98038 :     PG_RETURN_BOOL(result);
    1572              : }
    1573              : 
    1574              : Datum
    1575        87860 : text_ge(PG_FUNCTION_ARGS)
    1576              : {
    1577        87860 :     text       *arg1 = PG_GETARG_TEXT_PP(0);
    1578        87860 :     text       *arg2 = PG_GETARG_TEXT_PP(1);
    1579              :     bool        result;
    1580              : 
    1581        87860 :     result = (text_cmp(arg1, arg2, PG_GET_COLLATION()) >= 0);
    1582              : 
    1583        87860 :     PG_FREE_IF_COPY(arg1, 0);
    1584        87860 :     PG_FREE_IF_COPY(arg2, 1);
    1585              : 
    1586        87860 :     PG_RETURN_BOOL(result);
    1587              : }
    1588              : 
    1589              : Datum
    1590        18957 : text_starts_with(PG_FUNCTION_ARGS)
    1591              : {
    1592        18957 :     Datum       arg1 = PG_GETARG_DATUM(0);
    1593        18957 :     Datum       arg2 = PG_GETARG_DATUM(1);
    1594        18957 :     Oid         collid = PG_GET_COLLATION();
    1595              :     pg_locale_t mylocale;
    1596              :     bool        result;
    1597              :     Size        len1,
    1598              :                 len2;
    1599              : 
    1600        18957 :     check_collation_set(collid);
    1601              : 
    1602        18957 :     mylocale = pg_newlocale_from_collation(collid);
    1603              : 
    1604        18957 :     if (!mylocale->deterministic)
    1605            0 :         ereport(ERROR,
    1606              :                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
    1607              :                  errmsg("nondeterministic collations are not supported for substring searches")));
    1608              : 
    1609        18957 :     len1 = toast_raw_datum_size(arg1);
    1610        18957 :     len2 = toast_raw_datum_size(arg2);
    1611        18957 :     if (len2 > len1)
    1612            0 :         result = false;
    1613              :     else
    1614              :     {
    1615        18957 :         text       *targ1 = text_substring(arg1, 1, len2, false);
    1616        18957 :         text       *targ2 = DatumGetTextPP(arg2);
    1617              : 
    1618        18957 :         result = (memcmp(VARDATA_ANY(targ1), VARDATA_ANY(targ2),
    1619              :                          VARSIZE_ANY_EXHDR(targ2)) == 0);
    1620              : 
    1621        18957 :         PG_FREE_IF_COPY(targ1, 0);
    1622        18957 :         PG_FREE_IF_COPY(targ2, 1);
    1623              :     }
    1624              : 
    1625        18957 :     PG_RETURN_BOOL(result);
    1626              : }
    1627              : 
    1628              : Datum
    1629      3350835 : bttextcmp(PG_FUNCTION_ARGS)
    1630              : {
    1631      3350835 :     text       *arg1 = PG_GETARG_TEXT_PP(0);
    1632      3350835 :     text       *arg2 = PG_GETARG_TEXT_PP(1);
    1633              :     int32       result;
    1634              : 
    1635      3350835 :     result = text_cmp(arg1, arg2, PG_GET_COLLATION());
    1636              : 
    1637      3350835 :     PG_FREE_IF_COPY(arg1, 0);
    1638      3350835 :     PG_FREE_IF_COPY(arg2, 1);
    1639              : 
    1640      3350835 :     PG_RETURN_INT32(result);
    1641              : }
    1642              : 
    1643              : Datum
    1644        44518 : bttextsortsupport(PG_FUNCTION_ARGS)
    1645              : {
    1646        44518 :     SortSupport ssup = (SortSupport) PG_GETARG_POINTER(0);
    1647        44518 :     Oid         collid = ssup->ssup_collation;
    1648              :     MemoryContext oldcontext;
    1649              : 
    1650        44518 :     oldcontext = MemoryContextSwitchTo(ssup->ssup_cxt);
    1651              : 
    1652              :     /* Use generic string SortSupport */
    1653        44518 :     varstr_sortsupport(ssup, TEXTOID, collid);
    1654              : 
    1655        44512 :     MemoryContextSwitchTo(oldcontext);
    1656              : 
    1657        44512 :     PG_RETURN_VOID();
    1658              : }
    1659              : 
    1660              : /*
    1661              :  * Generic sortsupport interface for character type's operator classes.
    1662              :  * Includes locale support, and support for BpChar semantics (i.e. removing
    1663              :  * trailing spaces before comparison).
    1664              :  *
    1665              :  * Relies on the assumption that text, VarChar, and BpChar all have the
    1666              :  * same representation.
    1667              :  */
    1668              : void
    1669        70282 : varstr_sortsupport(SortSupport ssup, Oid typid, Oid collid)
    1670              : {
    1671        70282 :     bool        abbreviate = ssup->abbreviate;
    1672        70282 :     bool        collate_c = false;
    1673              :     VarStringSortSupport *sss;
    1674              :     pg_locale_t locale;
    1675              : 
    1676        70282 :     check_collation_set(collid);
    1677              : 
    1678        70276 :     locale = pg_newlocale_from_collation(collid);
    1679              : 
    1680              :     /*
    1681              :      * If possible, set ssup->comparator to a function which can be used to
    1682              :      * directly compare two datums.  If we can do this, we'll avoid the
    1683              :      * overhead of a trip through the fmgr layer for every comparison, which
    1684              :      * can be substantial.
    1685              :      *
    1686              :      * Most typically, we'll set the comparator to varlenafastcmp_locale,
    1687              :      * which uses strcoll() to perform comparisons.  We use that for the
    1688              :      * BpChar case too, but type NAME uses namefastcmp_locale. However, if
    1689              :      * LC_COLLATE = C, we can make things quite a bit faster with
    1690              :      * varstrfastcmp_c, bpcharfastcmp_c, or namefastcmp_c, all of which use
    1691              :      * memcmp() rather than strcoll().
    1692              :      */
    1693        70276 :     if (locale->collate_is_c)
    1694              :     {
    1695        46927 :         if (typid == BPCHAROID)
    1696          154 :             ssup->comparator = bpcharfastcmp_c;
    1697        46773 :         else if (typid == NAMEOID)
    1698              :         {
    1699        25242 :             ssup->comparator = namefastcmp_c;
    1700              :             /* Not supporting abbreviation with type NAME, for now */
    1701        25242 :             abbreviate = false;
    1702              :         }
    1703              :         else
    1704        21531 :             ssup->comparator = varstrfastcmp_c;
    1705              : 
    1706        46927 :         collate_c = true;
    1707              :     }
    1708              :     else
    1709              :     {
    1710              :         /*
    1711              :          * We use varlenafastcmp_locale except for type NAME.
    1712              :          */
    1713        23349 :         if (typid == NAMEOID)
    1714              :         {
    1715            0 :             ssup->comparator = namefastcmp_locale;
    1716              :             /* Not supporting abbreviation with type NAME, for now */
    1717            0 :             abbreviate = false;
    1718              :         }
    1719              :         else
    1720        23349 :             ssup->comparator = varlenafastcmp_locale;
    1721              : 
    1722              :         /*
    1723              :          * Unfortunately, it seems that abbreviation for non-C collations is
    1724              :          * broken on many common platforms; see pg_strxfrm_enabled().
    1725              :          *
    1726              :          * Even apart from the risk of broken locales, it's possible that
    1727              :          * there are platforms where the use of abbreviated keys should be
    1728              :          * disabled at compile time.  For example, macOS's strxfrm()
    1729              :          * implementation is known to not effectively concentrate a
    1730              :          * significant amount of entropy from the original string in earlier
    1731              :          * transformed blobs.  It's possible that other supported platforms
    1732              :          * are similarly encumbered.  So, if we ever get past disabling this
    1733              :          * categorically, we may still want or need to disable it for
    1734              :          * particular platforms.
    1735              :          */
    1736        23349 :         if (!pg_strxfrm_enabled(locale))
    1737        22951 :             abbreviate = false;
    1738              :     }
    1739              : 
    1740              :     /*
    1741              :      * If we're using abbreviated keys, or if we're using a locale-aware
    1742              :      * comparison, we need to initialize a VarStringSortSupport object. Both
    1743              :      * cases will make use of the temporary buffers we initialize here for
    1744              :      * scratch space (and to detect requirement for BpChar semantics from
    1745              :      * caller), and the abbreviation case requires additional state.
    1746              :      */
    1747        70276 :     if (abbreviate || !collate_c)
    1748              :     {
    1749        35378 :         sss = palloc_object(VarStringSortSupport);
    1750        35378 :         sss->buf1 = palloc(TEXTBUFLEN);
    1751        35378 :         sss->buflen1 = TEXTBUFLEN;
    1752        35378 :         sss->buf2 = palloc(TEXTBUFLEN);
    1753        35378 :         sss->buflen2 = TEXTBUFLEN;
    1754              :         /* Start with invalid values */
    1755        35378 :         sss->last_len1 = -1;
    1756        35378 :         sss->last_len2 = -1;
    1757              :         /* Initialize */
    1758        35378 :         sss->last_returned = 0;
    1759        35378 :         if (collate_c)
    1760        12029 :             sss->locale = NULL;
    1761              :         else
    1762        23349 :             sss->locale = locale;
    1763              : 
    1764              :         /*
    1765              :          * To avoid somehow confusing a strxfrm() blob and an original string,
    1766              :          * constantly keep track of the variety of data that buf1 and buf2
    1767              :          * currently contain.
    1768              :          *
    1769              :          * Comparisons may be interleaved with conversion calls.  Frequently,
    1770              :          * conversions and comparisons are batched into two distinct phases,
    1771              :          * but the correctness of caching cannot hinge upon this.  For
    1772              :          * comparison caching, buffer state is only trusted if cache_blob is
    1773              :          * found set to false, whereas strxfrm() caching only trusts the state
    1774              :          * when cache_blob is found set to true.
    1775              :          *
    1776              :          * Arbitrarily initialize cache_blob to true.
    1777              :          */
    1778        35378 :         sss->cache_blob = true;
    1779        35378 :         sss->collate_c = collate_c;
    1780        35378 :         sss->typid = typid;
    1781        35378 :         ssup->ssup_extra = sss;
    1782              : 
    1783              :         /*
    1784              :          * If possible, plan to use the abbreviated keys optimization.  The
    1785              :          * core code may switch back to authoritative comparator should
    1786              :          * abbreviation be aborted.
    1787              :          */
    1788        35378 :         if (abbreviate)
    1789              :         {
    1790        12328 :             sss->prop_card = 0.20;
    1791        12328 :             initHyperLogLog(&sss->abbr_card, 10);
    1792        12328 :             initHyperLogLog(&sss->full_card, 10);
    1793        12328 :             ssup->abbrev_full_comparator = ssup->comparator;
    1794        12328 :             ssup->comparator = ssup_datum_unsigned_cmp;
    1795        12328 :             ssup->abbrev_converter = varstr_abbrev_convert;
    1796        12328 :             ssup->abbrev_abort = varstr_abbrev_abort;
    1797              :         }
    1798              :     }
    1799        70276 : }
    1800              : 
    1801              : /*
    1802              :  * sortsupport comparison func (for C locale case)
    1803              :  */
    1804              : static int
    1805     23362237 : varstrfastcmp_c(Datum x, Datum y, SortSupport ssup)
    1806              : {
    1807     23362237 :     VarString  *arg1 = DatumGetVarStringPP(x);
    1808     23362237 :     VarString  *arg2 = DatumGetVarStringPP(y);
    1809              :     char       *a1p,
    1810              :                *a2p;
    1811              :     int         len1,
    1812              :                 len2,
    1813              :                 result;
    1814              : 
    1815     23362237 :     a1p = VARDATA_ANY(arg1);
    1816     23362237 :     a2p = VARDATA_ANY(arg2);
    1817              : 
    1818     23362237 :     len1 = VARSIZE_ANY_EXHDR(arg1);
    1819     23362237 :     len2 = VARSIZE_ANY_EXHDR(arg2);
    1820              : 
    1821     23362237 :     result = memcmp(a1p, a2p, Min(len1, len2));
    1822     23362237 :     if ((result == 0) && (len1 != len2))
    1823       656179 :         result = (len1 < len2) ? -1 : 1;
    1824              : 
    1825              :     /* We can't afford to leak memory here. */
    1826     23362237 :     if (PointerGetDatum(arg1) != x)
    1827            0 :         pfree(arg1);
    1828     23362237 :     if (PointerGetDatum(arg2) != y)
    1829            0 :         pfree(arg2);
    1830              : 
    1831     23362237 :     return result;
    1832              : }
    1833              : 
    1834              : /*
    1835              :  * sortsupport comparison func (for BpChar C locale case)
    1836              :  *
    1837              :  * BpChar outsources its sortsupport to this module.  Specialization for the
    1838              :  * varstr_sortsupport BpChar case, modeled on
    1839              :  * internal_bpchar_pattern_compare().
    1840              :  */
    1841              : static int
    1842        31161 : bpcharfastcmp_c(Datum x, Datum y, SortSupport ssup)
    1843              : {
    1844        31161 :     BpChar     *arg1 = DatumGetBpCharPP(x);
    1845        31161 :     BpChar     *arg2 = DatumGetBpCharPP(y);
    1846              :     char       *a1p,
    1847              :                *a2p;
    1848              :     int         len1,
    1849              :                 len2,
    1850              :                 result;
    1851              : 
    1852        31161 :     a1p = VARDATA_ANY(arg1);
    1853        31161 :     a2p = VARDATA_ANY(arg2);
    1854              : 
    1855        31161 :     len1 = bpchartruelen(a1p, VARSIZE_ANY_EXHDR(arg1));
    1856        31161 :     len2 = bpchartruelen(a2p, VARSIZE_ANY_EXHDR(arg2));
    1857              : 
    1858        31161 :     result = memcmp(a1p, a2p, Min(len1, len2));
    1859        31161 :     if ((result == 0) && (len1 != len2))
    1860            2 :         result = (len1 < len2) ? -1 : 1;
    1861              : 
    1862              :     /* We can't afford to leak memory here. */
    1863        31161 :     if (PointerGetDatum(arg1) != x)
    1864            0 :         pfree(arg1);
    1865        31161 :     if (PointerGetDatum(arg2) != y)
    1866            0 :         pfree(arg2);
    1867              : 
    1868        31161 :     return result;
    1869              : }
    1870              : 
    1871              : /*
    1872              :  * sortsupport comparison func (for NAME C locale case)
    1873              :  */
    1874              : static int
    1875     21648794 : namefastcmp_c(Datum x, Datum y, SortSupport ssup)
    1876              : {
    1877     21648794 :     Name        arg1 = DatumGetName(x);
    1878     21648794 :     Name        arg2 = DatumGetName(y);
    1879              : 
    1880     21648794 :     return strncmp(NameStr(*arg1), NameStr(*arg2), NAMEDATALEN);
    1881              : }
    1882              : 
    1883              : /*
    1884              :  * sortsupport comparison func (for locale case with all varlena types)
    1885              :  */
    1886              : static int
    1887     18714290 : varlenafastcmp_locale(Datum x, Datum y, SortSupport ssup)
    1888              : {
    1889     18714290 :     VarString  *arg1 = DatumGetVarStringPP(x);
    1890     18714290 :     VarString  *arg2 = DatumGetVarStringPP(y);
    1891              :     char       *a1p,
    1892              :                *a2p;
    1893              :     int         len1,
    1894              :                 len2,
    1895              :                 result;
    1896              : 
    1897     18714290 :     a1p = VARDATA_ANY(arg1);
    1898     18714290 :     a2p = VARDATA_ANY(arg2);
    1899              : 
    1900     18714290 :     len1 = VARSIZE_ANY_EXHDR(arg1);
    1901     18714290 :     len2 = VARSIZE_ANY_EXHDR(arg2);
    1902              : 
    1903     18714290 :     result = varstrfastcmp_locale(a1p, len1, a2p, len2, ssup);
    1904              : 
    1905              :     /* We can't afford to leak memory here. */
    1906     18714290 :     if (PointerGetDatum(arg1) != x)
    1907            0 :         pfree(arg1);
    1908     18714290 :     if (PointerGetDatum(arg2) != y)
    1909            0 :         pfree(arg2);
    1910              : 
    1911     18714290 :     return result;
    1912              : }
    1913              : 
    1914              : /*
    1915              :  * sortsupport comparison func (for locale case with NAME type)
    1916              :  */
    1917              : static int
    1918            0 : namefastcmp_locale(Datum x, Datum y, SortSupport ssup)
    1919              : {
    1920            0 :     Name        arg1 = DatumGetName(x);
    1921            0 :     Name        arg2 = DatumGetName(y);
    1922              : 
    1923            0 :     return varstrfastcmp_locale(NameStr(*arg1), strlen(NameStr(*arg1)),
    1924            0 :                                 NameStr(*arg2), strlen(NameStr(*arg2)),
    1925              :                                 ssup);
    1926              : }
    1927              : 
    1928              : /*
    1929              :  * sortsupport comparison func for locale cases
    1930              :  */
    1931              : static int
    1932     18714290 : varstrfastcmp_locale(char *a1p, int len1, char *a2p, int len2, SortSupport ssup)
    1933              : {
    1934     18714290 :     VarStringSortSupport *sss = (VarStringSortSupport *) ssup->ssup_extra;
    1935              :     int         result;
    1936              :     bool        arg1_match;
    1937              : 
    1938              :     /* Fast pre-check for equality, as discussed in varstr_cmp() */
    1939     18714290 :     if (len1 == len2 && memcmp(a1p, a2p, len1) == 0)
    1940              :     {
    1941              :         /*
    1942              :          * No change in buf1 or buf2 contents, so avoid changing last_len1 or
    1943              :          * last_len2.  Existing contents of buffers might still be used by
    1944              :          * next call.
    1945              :          *
    1946              :          * It's fine to allow the comparison of BpChar padding bytes here,
    1947              :          * even though that implies that the memcmp() will usually be
    1948              :          * performed for BpChar callers (though multibyte characters could
    1949              :          * still prevent that from occurring).  The memcmp() is still very
    1950              :          * cheap, and BpChar's funny semantics have us remove trailing spaces
    1951              :          * (not limited to padding), so we need make no distinction between
    1952              :          * padding space characters and "real" space characters.
    1953              :          */
    1954      4687160 :         return 0;
    1955              :     }
    1956              : 
    1957     14027130 :     if (sss->typid == BPCHAROID)
    1958              :     {
    1959              :         /* Get true number of bytes, ignoring trailing spaces */
    1960        18543 :         len1 = bpchartruelen(a1p, len1);
    1961        18543 :         len2 = bpchartruelen(a2p, len2);
    1962              :     }
    1963              : 
    1964     14027130 :     if (len1 >= sss->buflen1)
    1965              :     {
    1966            5 :         sss->buflen1 = Max(len1 + 1, Min(sss->buflen1 * 2, MaxAllocSize));
    1967            5 :         sss->buf1 = repalloc(sss->buf1, sss->buflen1);
    1968              :     }
    1969     14027130 :     if (len2 >= sss->buflen2)
    1970              :     {
    1971            3 :         sss->buflen2 = Max(len2 + 1, Min(sss->buflen2 * 2, MaxAllocSize));
    1972            3 :         sss->buf2 = repalloc(sss->buf2, sss->buflen2);
    1973              :     }
    1974              : 
    1975              :     /*
    1976              :      * We're likely to be asked to compare the same strings repeatedly, and
    1977              :      * memcmp() is so much cheaper than strcoll() that it pays to try to cache
    1978              :      * comparisons, even though in general there is no reason to think that
    1979              :      * that will work out (every string datum may be unique).  Caching does
    1980              :      * not slow things down measurably when it doesn't work out, and can speed
    1981              :      * things up by rather a lot when it does.  In part, this is because the
    1982              :      * memcmp() compares data from cachelines that are needed in L1 cache even
    1983              :      * when the last comparison's result cannot be reused.
    1984              :      */
    1985     14027130 :     arg1_match = true;
    1986     14027130 :     if (len1 != sss->last_len1 || memcmp(sss->buf1, a1p, len1) != 0)
    1987              :     {
    1988     12962797 :         arg1_match = false;
    1989     12962797 :         memcpy(sss->buf1, a1p, len1);
    1990     12962797 :         sss->buf1[len1] = '\0';
    1991     12962797 :         sss->last_len1 = len1;
    1992              :     }
    1993              : 
    1994              :     /*
    1995              :      * If we're comparing the same two strings as last time, we can return the
    1996              :      * same answer without calling strcoll() again.  This is more likely than
    1997              :      * it seems (at least with moderate to low cardinality sets), because
    1998              :      * quicksort compares the same pivot against many values.
    1999              :      */
    2000     14027130 :     if (len2 != sss->last_len2 || memcmp(sss->buf2, a2p, len2) != 0)
    2001              :     {
    2002      2150654 :         memcpy(sss->buf2, a2p, len2);
    2003      2150654 :         sss->buf2[len2] = '\0';
    2004      2150654 :         sss->last_len2 = len2;
    2005              :     }
    2006     11876476 :     else if (arg1_match && !sss->cache_blob)
    2007              :     {
    2008              :         /* Use result cached following last actual strcoll() call */
    2009       839644 :         return sss->last_returned;
    2010              :     }
    2011              : 
    2012     13187486 :     result = pg_strcoll(sss->buf1, sss->buf2, sss->locale);
    2013              : 
    2014              :     /* Break tie if necessary. */
    2015     13187486 :     if (result == 0 && sss->locale->deterministic)
    2016            0 :         result = strcmp(sss->buf1, sss->buf2);
    2017              : 
    2018              :     /* Cache result, perhaps saving an expensive strcoll() call next time */
    2019     13187486 :     sss->cache_blob = false;
    2020     13187486 :     sss->last_returned = result;
    2021     13187486 :     return result;
    2022              : }
    2023              : 
    2024              : /*
    2025              :  * Conversion routine for sortsupport.  Converts original to abbreviated key
    2026              :  * representation.  Our encoding strategy is simple -- pack the first 8 bytes
    2027              :  * of a strxfrm() blob into a Datum (on little-endian machines, the 8 bytes are
    2028              :  * stored in reverse order), and treat it as an unsigned integer.  When the "C"
    2029              :  * locale is used just memcpy() from original instead.
    2030              :  */
    2031              : static Datum
    2032       422327 : varstr_abbrev_convert(Datum original, SortSupport ssup)
    2033              : {
    2034       422327 :     const size_t max_prefix_bytes = sizeof(Datum);
    2035       422327 :     VarStringSortSupport *sss = (VarStringSortSupport *) ssup->ssup_extra;
    2036       422327 :     VarString  *authoritative = DatumGetVarStringPP(original);
    2037       422327 :     char       *authoritative_data = VARDATA_ANY(authoritative);
    2038              : 
    2039              :     /* working state */
    2040              :     Datum       res;
    2041              :     char       *pres;
    2042              :     int         len;
    2043              :     uint32      hash;
    2044              : 
    2045       422327 :     pres = (char *) &res;
    2046              :     /* memset(), so any non-overwritten bytes are NUL */
    2047       422327 :     memset(pres, 0, max_prefix_bytes);
    2048       422327 :     len = VARSIZE_ANY_EXHDR(authoritative);
    2049              : 
    2050              :     /* Get number of bytes, ignoring trailing spaces */
    2051       422327 :     if (sss->typid == BPCHAROID)
    2052          505 :         len = bpchartruelen(authoritative_data, len);
    2053              : 
    2054              :     /*
    2055              :      * If we're using the C collation, use memcpy(), rather than strxfrm(), to
    2056              :      * abbreviate keys.  The full comparator for the C locale is also
    2057              :      * memcmp().  This should be faster than strxfrm().
    2058              :      */
    2059       422327 :     if (sss->collate_c)
    2060       421409 :         memcpy(pres, authoritative_data, Min(len, max_prefix_bytes));
    2061              :     else
    2062              :     {
    2063              :         Size        bsize;
    2064              : 
    2065              :         /*
    2066              :          * We're not using the C collation, so fall back on strxfrm or ICU
    2067              :          * analogs.
    2068              :          */
    2069              : 
    2070              :         /* By convention, we use buffer 1 to store and NUL-terminate */
    2071          918 :         if (len >= sss->buflen1)
    2072              :         {
    2073            0 :             sss->buflen1 = Max(len + 1, Min(sss->buflen1 * 2, MaxAllocSize));
    2074            0 :             sss->buf1 = repalloc(sss->buf1, sss->buflen1);
    2075              :         }
    2076              : 
    2077              :         /* Might be able to reuse strxfrm() blob from last call */
    2078          918 :         if (sss->last_len1 == len && sss->cache_blob &&
    2079          459 :             memcmp(sss->buf1, authoritative_data, len) == 0)
    2080              :         {
    2081           84 :             memcpy(pres, sss->buf2, Min(max_prefix_bytes, sss->last_len2));
    2082              :             /* No change affecting cardinality, so no hashing required */
    2083           84 :             goto done;
    2084              :         }
    2085              : 
    2086          834 :         memcpy(sss->buf1, authoritative_data, len);
    2087              : 
    2088              :         /*
    2089              :          * pg_strxfrm() and pg_strxfrm_prefix expect NUL-terminated strings.
    2090              :          */
    2091          834 :         sss->buf1[len] = '\0';
    2092          834 :         sss->last_len1 = len;
    2093              : 
    2094          834 :         if (pg_strxfrm_prefix_enabled(sss->locale))
    2095              :         {
    2096          834 :             if (sss->buflen2 < max_prefix_bytes)
    2097              :             {
    2098            0 :                 sss->buflen2 = Max(max_prefix_bytes,
    2099              :                                    Min(sss->buflen2 * 2, MaxAllocSize));
    2100            0 :                 sss->buf2 = repalloc(sss->buf2, sss->buflen2);
    2101              :             }
    2102              : 
    2103          834 :             bsize = pg_strxfrm_prefix(sss->buf2, sss->buf1,
    2104              :                                       max_prefix_bytes, sss->locale);
    2105          834 :             sss->last_len2 = bsize;
    2106              :         }
    2107              :         else
    2108              :         {
    2109              :             /*
    2110              :              * Loop: Call pg_strxfrm(), possibly enlarge buffer, and try
    2111              :              * again.  The pg_strxfrm() function leaves the result buffer
    2112              :              * content undefined if the result did not fit, so we need to
    2113              :              * retry until everything fits, even though we only need the first
    2114              :              * few bytes in the end.
    2115              :              */
    2116              :             for (;;)
    2117              :             {
    2118            0 :                 bsize = pg_strxfrm(sss->buf2, sss->buf1, sss->buflen2,
    2119              :                                    sss->locale);
    2120              : 
    2121            0 :                 sss->last_len2 = bsize;
    2122            0 :                 if (bsize < sss->buflen2)
    2123            0 :                     break;
    2124              : 
    2125              :                 /*
    2126              :                  * Grow buffer and retry.
    2127              :                  */
    2128            0 :                 sss->buflen2 = Max(bsize + 1,
    2129              :                                    Min(sss->buflen2 * 2, MaxAllocSize));
    2130            0 :                 sss->buf2 = repalloc(sss->buf2, sss->buflen2);
    2131              :             }
    2132              :         }
    2133              : 
    2134              :         /*
    2135              :          * Every Datum byte is always compared.  This is safe because the
    2136              :          * strxfrm() blob is itself NUL terminated, leaving no danger of
    2137              :          * misinterpreting any NUL bytes not intended to be interpreted as
    2138              :          * logically representing termination.
    2139              :          */
    2140          834 :         memcpy(pres, sss->buf2, Min(max_prefix_bytes, bsize));
    2141              :     }
    2142              : 
    2143              :     /*
    2144              :      * Maintain approximate cardinality of both abbreviated keys and original,
    2145              :      * authoritative keys using HyperLogLog.  Used as cheap insurance against
    2146              :      * the worst case, where we do many string transformations for no saving
    2147              :      * in full strcoll()-based comparisons.  These statistics are used by
    2148              :      * varstr_abbrev_abort().
    2149              :      *
    2150              :      * First, Hash key proper, or a significant fraction of it.  Mix in length
    2151              :      * in order to compensate for cases where differences are past
    2152              :      * PG_CACHE_LINE_SIZE bytes, so as to limit the overhead of hashing.
    2153              :      */
    2154       422243 :     hash = DatumGetUInt32(hash_any((unsigned char *) authoritative_data,
    2155              :                                    Min(len, PG_CACHE_LINE_SIZE)));
    2156              : 
    2157       422243 :     if (len > PG_CACHE_LINE_SIZE)
    2158           96 :         hash ^= DatumGetUInt32(hash_uint32((uint32) len));
    2159              : 
    2160       422243 :     addHyperLogLog(&sss->full_card, hash);
    2161              : 
    2162              :     /* Hash abbreviated key */
    2163              :     {
    2164              :         uint32      tmp;
    2165              : 
    2166       422243 :         tmp = DatumGetUInt32(res) ^ (uint32) (DatumGetUInt64(res) >> 32);
    2167       422243 :         hash = DatumGetUInt32(hash_uint32(tmp));
    2168              :     }
    2169              : 
    2170       422243 :     addHyperLogLog(&sss->abbr_card, hash);
    2171              : 
    2172              :     /* Cache result, perhaps saving an expensive strxfrm() call next time */
    2173       422243 :     sss->cache_blob = true;
    2174       422327 : done:
    2175              : 
    2176              :     /*
    2177              :      * Byteswap on little-endian machines.
    2178              :      *
    2179              :      * This is needed so that ssup_datum_unsigned_cmp() (an unsigned integer
    2180              :      * 3-way comparator) works correctly on all platforms.  If we didn't do
    2181              :      * this, the comparator would have to call memcmp() with a pair of
    2182              :      * pointers to the first byte of each abbreviated key, which is slower.
    2183              :      */
    2184       422327 :     res = DatumBigEndianToNative(res);
    2185              : 
    2186              :     /* Don't leak memory here */
    2187       422327 :     if (PointerGetDatum(authoritative) != original)
    2188            1 :         pfree(authoritative);
    2189              : 
    2190       422327 :     return res;
    2191              : }
    2192              : 
    2193              : /*
    2194              :  * Callback for estimating effectiveness of abbreviated key optimization, using
    2195              :  * heuristic rules.  Returns value indicating if the abbreviation optimization
    2196              :  * should be aborted, based on its projected effectiveness.
    2197              :  */
    2198              : static bool
    2199         1193 : varstr_abbrev_abort(int memtupcount, SortSupport ssup)
    2200              : {
    2201         1193 :     VarStringSortSupport *sss = (VarStringSortSupport *) ssup->ssup_extra;
    2202              :     double      abbrev_distinct,
    2203              :                 key_distinct;
    2204              : 
    2205              :     Assert(ssup->abbreviate);
    2206              : 
    2207              :     /* Have a little patience */
    2208         1193 :     if (memtupcount < 100)
    2209          694 :         return false;
    2210              : 
    2211          499 :     abbrev_distinct = estimateHyperLogLog(&sss->abbr_card);
    2212          499 :     key_distinct = estimateHyperLogLog(&sss->full_card);
    2213              : 
    2214              :     /*
    2215              :      * Clamp cardinality estimates to at least one distinct value.  While
    2216              :      * NULLs are generally disregarded, if only NULL values were seen so far,
    2217              :      * that might misrepresent costs if we failed to clamp.
    2218              :      */
    2219          499 :     if (abbrev_distinct < 1.0)
    2220            0 :         abbrev_distinct = 1.0;
    2221              : 
    2222          499 :     if (key_distinct < 1.0)
    2223            0 :         key_distinct = 1.0;
    2224              : 
    2225              :     /*
    2226              :      * In the worst case all abbreviated keys are identical, while at the same
    2227              :      * time there are differences within full key strings not captured in
    2228              :      * abbreviations.
    2229              :      */
    2230          499 :     if (trace_sort)
    2231              :     {
    2232            0 :         double      norm_abbrev_card = abbrev_distinct / (double) memtupcount;
    2233              : 
    2234            0 :         elog(LOG, "varstr_abbrev: abbrev_distinct after %d: %f "
    2235              :              "(key_distinct: %f, norm_abbrev_card: %f, prop_card: %f)",
    2236              :              memtupcount, abbrev_distinct, key_distinct, norm_abbrev_card,
    2237              :              sss->prop_card);
    2238              :     }
    2239              : 
    2240              :     /*
    2241              :      * If the number of distinct abbreviated keys approximately matches the
    2242              :      * number of distinct authoritative original keys, that's reason enough to
    2243              :      * proceed.  We can win even with a very low cardinality set if most
    2244              :      * tie-breakers only memcmp().  This is by far the most important
    2245              :      * consideration.
    2246              :      *
    2247              :      * While comparisons that are resolved at the abbreviated key level are
    2248              :      * considerably cheaper than tie-breakers resolved with memcmp(), both of
    2249              :      * those two outcomes are so much cheaper than a full strcoll() once
    2250              :      * sorting is underway that it doesn't seem worth it to weigh abbreviated
    2251              :      * cardinality against the overall size of the set in order to more
    2252              :      * accurately model costs.  Assume that an abbreviated comparison, and an
    2253              :      * abbreviated comparison with a cheap memcmp()-based authoritative
    2254              :      * resolution are equivalent.
    2255              :      */
    2256          499 :     if (abbrev_distinct > key_distinct * sss->prop_card)
    2257              :     {
    2258              :         /*
    2259              :          * When we have exceeded 10,000 tuples, decay required cardinality
    2260              :          * aggressively for next call.
    2261              :          *
    2262              :          * This is useful because the number of comparisons required on
    2263              :          * average increases at a linearithmic rate, and at roughly 10,000
    2264              :          * tuples that factor will start to dominate over the linear costs of
    2265              :          * string transformation (this is a conservative estimate).  The decay
    2266              :          * rate is chosen to be a little less aggressive than halving -- which
    2267              :          * (since we're called at points at which memtupcount has doubled)
    2268              :          * would never see the cost model actually abort past the first call
    2269              :          * following a decay.  This decay rate is mostly a precaution against
    2270              :          * a sudden, violent swing in how well abbreviated cardinality tracks
    2271              :          * full key cardinality.  The decay also serves to prevent a marginal
    2272              :          * case from being aborted too late, when too much has already been
    2273              :          * invested in string transformation.
    2274              :          *
    2275              :          * It's possible for sets of several million distinct strings with
    2276              :          * mere tens of thousands of distinct abbreviated keys to still
    2277              :          * benefit very significantly.  This will generally occur provided
    2278              :          * each abbreviated key is a proxy for a roughly uniform number of the
    2279              :          * set's full keys. If it isn't so, we hope to catch that early and
    2280              :          * abort.  If it isn't caught early, by the time the problem is
    2281              :          * apparent it's probably not worth aborting.
    2282              :          */
    2283          499 :         if (memtupcount > 10000)
    2284            2 :             sss->prop_card *= 0.65;
    2285              : 
    2286          499 :         return false;
    2287              :     }
    2288              : 
    2289              :     /*
    2290              :      * Abort abbreviation strategy.
    2291              :      *
    2292              :      * The worst case, where all abbreviated keys are identical while all
    2293              :      * original strings differ will typically only see a regression of about
    2294              :      * 10% in execution time for small to medium sized lists of strings.
    2295              :      * Whereas on modern CPUs where cache stalls are the dominant cost, we can
    2296              :      * often expect very large improvements, particularly with sets of strings
    2297              :      * of moderately high to high abbreviated cardinality.  There is little to
    2298              :      * lose but much to gain, which our strategy reflects.
    2299              :      */
    2300            0 :     if (trace_sort)
    2301            0 :         elog(LOG, "varstr_abbrev: aborted abbreviation at %d "
    2302              :              "(abbrev_distinct: %f, key_distinct: %f, prop_card: %f)",
    2303              :              memtupcount, abbrev_distinct, key_distinct, sss->prop_card);
    2304              : 
    2305            0 :     return true;
    2306              : }
    2307              : 
    2308              : /*
    2309              :  * Generic equalimage support function for character type's operator classes.
    2310              :  * Disables the use of deduplication with nondeterministic collations.
    2311              :  */
    2312              : Datum
    2313         4599 : btvarstrequalimage(PG_FUNCTION_ARGS)
    2314              : {
    2315              : #ifdef NOT_USED
    2316              :     Oid         opcintype = PG_GETARG_OID(0);
    2317              : #endif
    2318         4599 :     Oid         collid = PG_GET_COLLATION();
    2319              :     pg_locale_t locale;
    2320              : 
    2321         4599 :     check_collation_set(collid);
    2322              : 
    2323         4599 :     locale = pg_newlocale_from_collation(collid);
    2324              : 
    2325         4599 :     PG_RETURN_BOOL(locale->deterministic);
    2326              : }
    2327              : 
    2328              : Datum
    2329       114780 : text_larger(PG_FUNCTION_ARGS)
    2330              : {
    2331       114780 :     text       *arg1 = PG_GETARG_TEXT_PP(0);
    2332       114780 :     text       *arg2 = PG_GETARG_TEXT_PP(1);
    2333              :     text       *result;
    2334              : 
    2335       114780 :     result = ((text_cmp(arg1, arg2, PG_GET_COLLATION()) > 0) ? arg1 : arg2);
    2336              : 
    2337       114780 :     PG_RETURN_TEXT_P(result);
    2338              : }
    2339              : 
    2340              : Datum
    2341        43038 : text_smaller(PG_FUNCTION_ARGS)
    2342              : {
    2343        43038 :     text       *arg1 = PG_GETARG_TEXT_PP(0);
    2344        43038 :     text       *arg2 = PG_GETARG_TEXT_PP(1);
    2345              :     text       *result;
    2346              : 
    2347        43038 :     result = ((text_cmp(arg1, arg2, PG_GET_COLLATION()) < 0) ? arg1 : arg2);
    2348              : 
    2349        43038 :     PG_RETURN_TEXT_P(result);
    2350              : }
    2351              : 
    2352              : 
    2353              : /*
    2354              :  * Cross-type comparison functions for types text and name.
    2355              :  */
    2356              : 
    2357              : Datum
    2358       112495 : nameeqtext(PG_FUNCTION_ARGS)
    2359              : {
    2360       112495 :     Name        arg1 = PG_GETARG_NAME(0);
    2361       112495 :     text       *arg2 = PG_GETARG_TEXT_PP(1);
    2362       112495 :     size_t      len1 = strlen(NameStr(*arg1));
    2363       112495 :     size_t      len2 = VARSIZE_ANY_EXHDR(arg2);
    2364       112495 :     Oid         collid = PG_GET_COLLATION();
    2365              :     bool        result;
    2366              : 
    2367       112495 :     check_collation_set(collid);
    2368              : 
    2369       112495 :     if (collid == C_COLLATION_OID)
    2370       127904 :         result = (len1 == len2 &&
    2371        62077 :                   memcmp(NameStr(*arg1), VARDATA_ANY(arg2), len1) == 0);
    2372              :     else
    2373        46668 :         result = (varstr_cmp(NameStr(*arg1), len1,
    2374        46668 :                              VARDATA_ANY(arg2), len2,
    2375              :                              collid) == 0);
    2376              : 
    2377       112495 :     PG_FREE_IF_COPY(arg2, 1);
    2378              : 
    2379       112495 :     PG_RETURN_BOOL(result);
    2380              : }
    2381              : 
    2382              : Datum
    2383         3992 : texteqname(PG_FUNCTION_ARGS)
    2384              : {
    2385         3992 :     text       *arg1 = PG_GETARG_TEXT_PP(0);
    2386         3992 :     Name        arg2 = PG_GETARG_NAME(1);
    2387         3992 :     size_t      len1 = VARSIZE_ANY_EXHDR(arg1);
    2388         3992 :     size_t      len2 = strlen(NameStr(*arg2));
    2389         3992 :     Oid         collid = PG_GET_COLLATION();
    2390              :     bool        result;
    2391              : 
    2392         3992 :     check_collation_set(collid);
    2393              : 
    2394         3992 :     if (collid == C_COLLATION_OID)
    2395          284 :         result = (len1 == len2 &&
    2396           91 :                   memcmp(VARDATA_ANY(arg1), NameStr(*arg2), len1) == 0);
    2397              :     else
    2398         3799 :         result = (varstr_cmp(VARDATA_ANY(arg1), len1,
    2399         3799 :                              NameStr(*arg2), len2,
    2400              :                              collid) == 0);
    2401              : 
    2402         3992 :     PG_FREE_IF_COPY(arg1, 0);
    2403              : 
    2404         3992 :     PG_RETURN_BOOL(result);
    2405              : }
    2406              : 
    2407              : Datum
    2408            9 : namenetext(PG_FUNCTION_ARGS)
    2409              : {
    2410            9 :     Name        arg1 = PG_GETARG_NAME(0);
    2411            9 :     text       *arg2 = PG_GETARG_TEXT_PP(1);
    2412            9 :     size_t      len1 = strlen(NameStr(*arg1));
    2413            9 :     size_t      len2 = VARSIZE_ANY_EXHDR(arg2);
    2414            9 :     Oid         collid = PG_GET_COLLATION();
    2415              :     bool        result;
    2416              : 
    2417            9 :     check_collation_set(collid);
    2418              : 
    2419            9 :     if (collid == C_COLLATION_OID)
    2420            0 :         result = !(len1 == len2 &&
    2421            0 :                    memcmp(NameStr(*arg1), VARDATA_ANY(arg2), len1) == 0);
    2422              :     else
    2423            9 :         result = !(varstr_cmp(NameStr(*arg1), len1,
    2424            9 :                               VARDATA_ANY(arg2), len2,
    2425              :                               collid) == 0);
    2426              : 
    2427            9 :     PG_FREE_IF_COPY(arg2, 1);
    2428              : 
    2429            9 :     PG_RETURN_BOOL(result);
    2430              : }
    2431              : 
    2432              : Datum
    2433            9 : textnename(PG_FUNCTION_ARGS)
    2434              : {
    2435            9 :     text       *arg1 = PG_GETARG_TEXT_PP(0);
    2436            9 :     Name        arg2 = PG_GETARG_NAME(1);
    2437            9 :     size_t      len1 = VARSIZE_ANY_EXHDR(arg1);
    2438            9 :     size_t      len2 = strlen(NameStr(*arg2));
    2439            9 :     Oid         collid = PG_GET_COLLATION();
    2440              :     bool        result;
    2441              : 
    2442            9 :     check_collation_set(collid);
    2443              : 
    2444            9 :     if (collid == C_COLLATION_OID)
    2445            0 :         result = !(len1 == len2 &&
    2446            0 :                    memcmp(VARDATA_ANY(arg1), NameStr(*arg2), len1) == 0);
    2447              :     else
    2448            9 :         result = !(varstr_cmp(VARDATA_ANY(arg1), len1,
    2449            9 :                               NameStr(*arg2), len2,
    2450              :                               collid) == 0);
    2451              : 
    2452            9 :     PG_FREE_IF_COPY(arg1, 0);
    2453              : 
    2454            9 :     PG_RETURN_BOOL(result);
    2455              : }
    2456              : 
    2457              : Datum
    2458        69966 : btnametextcmp(PG_FUNCTION_ARGS)
    2459              : {
    2460        69966 :     Name        arg1 = PG_GETARG_NAME(0);
    2461        69966 :     text       *arg2 = PG_GETARG_TEXT_PP(1);
    2462              :     int32       result;
    2463              : 
    2464        69966 :     result = varstr_cmp(NameStr(*arg1), strlen(NameStr(*arg1)),
    2465        69966 :                         VARDATA_ANY(arg2), VARSIZE_ANY_EXHDR(arg2),
    2466              :                         PG_GET_COLLATION());
    2467              : 
    2468        69966 :     PG_FREE_IF_COPY(arg2, 1);
    2469              : 
    2470        69966 :     PG_RETURN_INT32(result);
    2471              : }
    2472              : 
    2473              : Datum
    2474           22 : bttextnamecmp(PG_FUNCTION_ARGS)
    2475              : {
    2476           22 :     text       *arg1 = PG_GETARG_TEXT_PP(0);
    2477           22 :     Name        arg2 = PG_GETARG_NAME(1);
    2478              :     int32       result;
    2479              : 
    2480           22 :     result = varstr_cmp(VARDATA_ANY(arg1), VARSIZE_ANY_EXHDR(arg1),
    2481           22 :                         NameStr(*arg2), strlen(NameStr(*arg2)),
    2482              :                         PG_GET_COLLATION());
    2483              : 
    2484           22 :     PG_FREE_IF_COPY(arg1, 0);
    2485              : 
    2486           22 :     PG_RETURN_INT32(result);
    2487              : }
    2488              : 
    2489              : #define CmpCall(cmpfunc) \
    2490              :     DatumGetInt32(DirectFunctionCall2Coll(cmpfunc, \
    2491              :                                           PG_GET_COLLATION(), \
    2492              :                                           PG_GETARG_DATUM(0), \
    2493              :                                           PG_GETARG_DATUM(1)))
    2494              : 
    2495              : Datum
    2496        33968 : namelttext(PG_FUNCTION_ARGS)
    2497              : {
    2498        33968 :     PG_RETURN_BOOL(CmpCall(btnametextcmp) < 0);
    2499              : }
    2500              : 
    2501              : Datum
    2502            0 : nameletext(PG_FUNCTION_ARGS)
    2503              : {
    2504            0 :     PG_RETURN_BOOL(CmpCall(btnametextcmp) <= 0);
    2505              : }
    2506              : 
    2507              : Datum
    2508            0 : namegttext(PG_FUNCTION_ARGS)
    2509              : {
    2510            0 :     PG_RETURN_BOOL(CmpCall(btnametextcmp) > 0);
    2511              : }
    2512              : 
    2513              : Datum
    2514        29632 : namegetext(PG_FUNCTION_ARGS)
    2515              : {
    2516        29632 :     PG_RETURN_BOOL(CmpCall(btnametextcmp) >= 0);
    2517              : }
    2518              : 
    2519              : Datum
    2520            0 : textltname(PG_FUNCTION_ARGS)
    2521              : {
    2522            0 :     PG_RETURN_BOOL(CmpCall(bttextnamecmp) < 0);
    2523              : }
    2524              : 
    2525              : Datum
    2526            0 : textlename(PG_FUNCTION_ARGS)
    2527              : {
    2528            0 :     PG_RETURN_BOOL(CmpCall(bttextnamecmp) <= 0);
    2529              : }
    2530              : 
    2531              : Datum
    2532            0 : textgtname(PG_FUNCTION_ARGS)
    2533              : {
    2534            0 :     PG_RETURN_BOOL(CmpCall(bttextnamecmp) > 0);
    2535              : }
    2536              : 
    2537              : Datum
    2538            0 : textgename(PG_FUNCTION_ARGS)
    2539              : {
    2540            0 :     PG_RETURN_BOOL(CmpCall(bttextnamecmp) >= 0);
    2541              : }
    2542              : 
    2543              : #undef CmpCall
    2544              : 
    2545              : 
    2546              : /*
    2547              :  * The following operators support character-by-character comparison
    2548              :  * of text datums, to allow building indexes suitable for LIKE clauses.
    2549              :  * Note that the regular texteq/textne comparison operators, and regular
    2550              :  * support functions 1 and 2 with "C" collation are assumed to be
    2551              :  * compatible with these!
    2552              :  */
    2553              : 
    2554              : static int
    2555        80222 : internal_text_pattern_compare(text *arg1, text *arg2)
    2556              : {
    2557              :     int         result;
    2558              :     int         len1,
    2559              :                 len2;
    2560              : 
    2561        80222 :     len1 = VARSIZE_ANY_EXHDR(arg1);
    2562        80222 :     len2 = VARSIZE_ANY_EXHDR(arg2);
    2563              : 
    2564        80222 :     result = memcmp(VARDATA_ANY(arg1), VARDATA_ANY(arg2), Min(len1, len2));
    2565        80222 :     if (result != 0)
    2566        80156 :         return result;
    2567           66 :     else if (len1 < len2)
    2568            0 :         return -1;
    2569           66 :     else if (len1 > len2)
    2570           42 :         return 1;
    2571              :     else
    2572           24 :         return 0;
    2573              : }
    2574              : 
    2575              : 
    2576              : Datum
    2577        23933 : text_pattern_lt(PG_FUNCTION_ARGS)
    2578              : {
    2579        23933 :     text       *arg1 = PG_GETARG_TEXT_PP(0);
    2580        23933 :     text       *arg2 = PG_GETARG_TEXT_PP(1);
    2581              :     int         result;
    2582              : 
    2583        23933 :     result = internal_text_pattern_compare(arg1, arg2);
    2584              : 
    2585        23933 :     PG_FREE_IF_COPY(arg1, 0);
    2586        23933 :     PG_FREE_IF_COPY(arg2, 1);
    2587              : 
    2588        23933 :     PG_RETURN_BOOL(result < 0);
    2589              : }
    2590              : 
    2591              : 
    2592              : Datum
    2593        18755 : text_pattern_le(PG_FUNCTION_ARGS)
    2594              : {
    2595        18755 :     text       *arg1 = PG_GETARG_TEXT_PP(0);
    2596        18755 :     text       *arg2 = PG_GETARG_TEXT_PP(1);
    2597              :     int         result;
    2598              : 
    2599        18755 :     result = internal_text_pattern_compare(arg1, arg2);
    2600              : 
    2601        18755 :     PG_FREE_IF_COPY(arg1, 0);
    2602        18755 :     PG_FREE_IF_COPY(arg2, 1);
    2603              : 
    2604        18755 :     PG_RETURN_BOOL(result <= 0);
    2605              : }
    2606              : 
    2607              : 
    2608              : Datum
    2609        18767 : text_pattern_ge(PG_FUNCTION_ARGS)
    2610              : {
    2611        18767 :     text       *arg1 = PG_GETARG_TEXT_PP(0);
    2612        18767 :     text       *arg2 = PG_GETARG_TEXT_PP(1);
    2613              :     int         result;
    2614              : 
    2615        18767 :     result = internal_text_pattern_compare(arg1, arg2);
    2616              : 
    2617        18767 :     PG_FREE_IF_COPY(arg1, 0);
    2618        18767 :     PG_FREE_IF_COPY(arg2, 1);
    2619              : 
    2620        18767 :     PG_RETURN_BOOL(result >= 0);
    2621              : }
    2622              : 
    2623              : 
    2624              : Datum
    2625        18755 : text_pattern_gt(PG_FUNCTION_ARGS)
    2626              : {
    2627        18755 :     text       *arg1 = PG_GETARG_TEXT_PP(0);
    2628        18755 :     text       *arg2 = PG_GETARG_TEXT_PP(1);
    2629              :     int         result;
    2630              : 
    2631        18755 :     result = internal_text_pattern_compare(arg1, arg2);
    2632              : 
    2633        18755 :     PG_FREE_IF_COPY(arg1, 0);
    2634        18755 :     PG_FREE_IF_COPY(arg2, 1);
    2635              : 
    2636        18755 :     PG_RETURN_BOOL(result > 0);
    2637              : }
    2638              : 
    2639              : 
    2640              : Datum
    2641           12 : bttext_pattern_cmp(PG_FUNCTION_ARGS)
    2642              : {
    2643           12 :     text       *arg1 = PG_GETARG_TEXT_PP(0);
    2644           12 :     text       *arg2 = PG_GETARG_TEXT_PP(1);
    2645              :     int         result;
    2646              : 
    2647           12 :     result = internal_text_pattern_compare(arg1, arg2);
    2648              : 
    2649           12 :     PG_FREE_IF_COPY(arg1, 0);
    2650           12 :     PG_FREE_IF_COPY(arg2, 1);
    2651              : 
    2652           12 :     PG_RETURN_INT32(result);
    2653              : }
    2654              : 
    2655              : 
    2656              : Datum
    2657           58 : bttext_pattern_sortsupport(PG_FUNCTION_ARGS)
    2658              : {
    2659           58 :     SortSupport ssup = (SortSupport) PG_GETARG_POINTER(0);
    2660              :     MemoryContext oldcontext;
    2661              : 
    2662           58 :     oldcontext = MemoryContextSwitchTo(ssup->ssup_cxt);
    2663              : 
    2664              :     /* Use generic string SortSupport, forcing "C" collation */
    2665           58 :     varstr_sortsupport(ssup, TEXTOID, C_COLLATION_OID);
    2666              : 
    2667           58 :     MemoryContextSwitchTo(oldcontext);
    2668              : 
    2669           58 :     PG_RETURN_VOID();
    2670              : }
    2671              : 
    2672              : 
    2673              : /* text_name()
    2674              :  * Converts a text type to a Name type.
    2675              :  */
    2676              : Datum
    2677        15441 : text_name(PG_FUNCTION_ARGS)
    2678              : {
    2679        15441 :     text       *s = PG_GETARG_TEXT_PP(0);
    2680              :     Name        result;
    2681              :     int         len;
    2682              : 
    2683        15441 :     len = VARSIZE_ANY_EXHDR(s);
    2684              : 
    2685              :     /* Truncate oversize input */
    2686        15441 :     if (len >= NAMEDATALEN)
    2687            3 :         len = pg_mbcliplen(VARDATA_ANY(s), len, NAMEDATALEN - 1);
    2688              : 
    2689              :     /* We use palloc0 here to ensure result is zero-padded */
    2690        15441 :     result = (Name) palloc0(NAMEDATALEN);
    2691        15441 :     memcpy(NameStr(*result), VARDATA_ANY(s), len);
    2692              : 
    2693        15441 :     PG_RETURN_NAME(result);
    2694              : }
    2695              : 
    2696              : /* name_text()
    2697              :  * Converts a Name type to a text type.
    2698              :  */
    2699              : Datum
    2700       328162 : name_text(PG_FUNCTION_ARGS)
    2701              : {
    2702       328162 :     Name        s = PG_GETARG_NAME(0);
    2703              : 
    2704       328162 :     PG_RETURN_TEXT_P(cstring_to_text(NameStr(*s)));
    2705              : }
    2706              : 
    2707              : 
    2708              : /*
    2709              :  * textToQualifiedNameList - convert a text object to list of names
    2710              :  *
    2711              :  * This implements the input parsing needed by nextval() and other
    2712              :  * functions that take a text parameter representing a qualified name.
    2713              :  * We split the name at dots, downcase if not double-quoted, and
    2714              :  * truncate names if they're too long.
    2715              :  */
    2716              : List *
    2717         2716 : textToQualifiedNameList(text *textval)
    2718              : {
    2719              :     char       *rawname;
    2720         2716 :     List       *result = NIL;
    2721              :     List       *namelist;
    2722              :     ListCell   *l;
    2723              : 
    2724              :     /* Convert to C string (handles possible detoasting). */
    2725              :     /* Note we rely on being able to modify rawname below. */
    2726         2716 :     rawname = text_to_cstring(textval);
    2727              : 
    2728         2716 :     if (!SplitIdentifierString(rawname, '.', &namelist))
    2729            0 :         ereport(ERROR,
    2730              :                 (errcode(ERRCODE_INVALID_NAME),
    2731              :                  errmsg("invalid name syntax")));
    2732              : 
    2733         2716 :     if (namelist == NIL)
    2734            0 :         ereport(ERROR,
    2735              :                 (errcode(ERRCODE_INVALID_NAME),
    2736              :                  errmsg("invalid name syntax")));
    2737              : 
    2738         5490 :     foreach(l, namelist)
    2739              :     {
    2740         2774 :         char       *curname = (char *) lfirst(l);
    2741              : 
    2742         2774 :         result = lappend(result, makeString(pstrdup(curname)));
    2743              :     }
    2744              : 
    2745         2716 :     pfree(rawname);
    2746         2716 :     list_free(namelist);
    2747              : 
    2748         2716 :     return result;
    2749              : }
    2750              : 
    2751              : /*
    2752              :  * SplitIdentifierString --- parse a string containing identifiers
    2753              :  *
    2754              :  * This is the guts of textToQualifiedNameList, and is exported for use in
    2755              :  * other situations such as parsing GUC variables.  In the GUC case, it's
    2756              :  * important to avoid memory leaks, so the API is designed to minimize the
    2757              :  * amount of stuff that needs to be allocated and freed.
    2758              :  *
    2759              :  * Inputs:
    2760              :  *  rawstring: the input string; must be overwritable!  On return, it's
    2761              :  *             been modified to contain the separated identifiers.
    2762              :  *  separator: the separator punctuation expected between identifiers
    2763              :  *             (typically '.' or ',').  Whitespace may also appear around
    2764              :  *             identifiers.
    2765              :  * Outputs:
    2766              :  *  namelist: filled with a palloc'd list of pointers to identifiers within
    2767              :  *            rawstring.  Caller should list_free() this even on error return.
    2768              :  *
    2769              :  * Returns true if okay, false if there is a syntax error in the string.
    2770              :  *
    2771              :  * Note that an empty string is considered okay here, though not in
    2772              :  * textToQualifiedNameList.
    2773              :  */
    2774              : bool
    2775       191418 : SplitIdentifierString(char *rawstring, char separator,
    2776              :                       List **namelist)
    2777              : {
    2778       191418 :     char       *nextp = rawstring;
    2779       191418 :     bool        done = false;
    2780              : 
    2781       191418 :     *namelist = NIL;
    2782              : 
    2783       191421 :     while (scanner_isspace(*nextp))
    2784            3 :         nextp++;                /* skip leading whitespace */
    2785              : 
    2786       191418 :     if (*nextp == '\0')
    2787        15870 :         return true;            /* empty string represents empty list */
    2788              : 
    2789              :     /* At the top of the loop, we are at start of a new identifier. */
    2790              :     do
    2791              :     {
    2792              :         char       *curname;
    2793              :         char       *endp;
    2794              : 
    2795       328466 :         if (*nextp == '"')
    2796              :         {
    2797              :             /* Quoted name --- collapse quote-quote pairs, no downcasing */
    2798        21332 :             curname = nextp + 1;
    2799              :             for (;;)
    2800              :             {
    2801        21334 :                 endp = strchr(nextp + 1, '"');
    2802        21333 :                 if (endp == NULL)
    2803            0 :                     return false;   /* mismatched quotes */
    2804        21333 :                 if (endp[1] != '"')
    2805        21332 :                     break;      /* found end of quoted name */
    2806              :                 /* Collapse adjacent quotes into one quote, and look again */
    2807            1 :                 memmove(endp, endp + 1, strlen(endp));
    2808            1 :                 nextp = endp;
    2809              :             }
    2810              :             /* endp now points at the terminating quote */
    2811        21332 :             nextp = endp + 1;
    2812              :         }
    2813              :         else
    2814              :         {
    2815              :             /* Unquoted name --- extends to separator or whitespace */
    2816              :             char       *downname;
    2817              :             int         len;
    2818              : 
    2819       307134 :             curname = nextp;
    2820      2813518 :             while (*nextp && *nextp != separator &&
    2821      2506385 :                    !scanner_isspace(*nextp))
    2822      2506384 :                 nextp++;
    2823       307134 :             endp = nextp;
    2824       307134 :             if (curname == nextp)
    2825            0 :                 return false;   /* empty unquoted name not allowed */
    2826              : 
    2827              :             /*
    2828              :              * Downcase the identifier, using same code as main lexer does.
    2829              :              *
    2830              :              * XXX because we want to overwrite the input in-place, we cannot
    2831              :              * support a downcasing transformation that increases the string
    2832              :              * length.  This is not a problem given the current implementation
    2833              :              * of downcase_truncate_identifier, but we'll probably have to do
    2834              :              * something about this someday.
    2835              :              */
    2836       307134 :             len = endp - curname;
    2837       307134 :             downname = downcase_truncate_identifier(curname, len, false);
    2838              :             Assert(strlen(downname) <= len);
    2839       307134 :             strncpy(curname, downname, len);    /* strncpy is required here */
    2840       307134 :             pfree(downname);
    2841              :         }
    2842              : 
    2843       328467 :         while (scanner_isspace(*nextp))
    2844            1 :             nextp++;            /* skip trailing whitespace */
    2845              : 
    2846       328466 :         if (*nextp == separator)
    2847              :         {
    2848       152918 :             nextp++;
    2849       293122 :             while (scanner_isspace(*nextp))
    2850       140204 :                 nextp++;        /* skip leading whitespace for next */
    2851              :             /* we expect another name, so done remains false */
    2852              :         }
    2853       175548 :         else if (*nextp == '\0')
    2854       175547 :             done = true;
    2855              :         else
    2856            1 :             return false;       /* invalid syntax */
    2857              : 
    2858              :         /* Now safe to overwrite separator with a null */
    2859       328465 :         *endp = '\0';
    2860              : 
    2861              :         /* Truncate name if it's overlength */
    2862       328465 :         truncate_identifier(curname, strlen(curname), false);
    2863              : 
    2864              :         /*
    2865              :          * Finished isolating current name --- add it to list
    2866              :          */
    2867       328465 :         *namelist = lappend(*namelist, curname);
    2868              : 
    2869              :         /* Loop back if we didn't reach end of string */
    2870       328465 :     } while (!done);
    2871              : 
    2872       175547 :     return true;
    2873              : }
    2874              : 
    2875              : 
    2876              : /*
    2877              :  * SplitDirectoriesString --- parse a string containing file/directory names
    2878              :  *
    2879              :  * This works fine on file names too; the function name is historical.
    2880              :  *
    2881              :  * This is similar to SplitIdentifierString, except that the parsing
    2882              :  * rules are meant to handle pathnames instead of identifiers: there is
    2883              :  * no downcasing, embedded spaces are allowed, the max length is MAXPGPATH-1,
    2884              :  * and we apply canonicalize_path() to each extracted string.  Because of the
    2885              :  * last, the returned strings are separately palloc'd rather than being
    2886              :  * pointers into rawstring --- but we still scribble on rawstring.
    2887              :  *
    2888              :  * Inputs:
    2889              :  *  rawstring: the input string; must be modifiable!
    2890              :  *  separator: the separator punctuation expected between directories
    2891              :  *             (typically ',' or ';').  Whitespace may also appear around
    2892              :  *             directories.
    2893              :  * Outputs:
    2894              :  *  namelist: filled with a palloc'd list of directory names.
    2895              :  *            Caller should list_free_deep() this even on error return.
    2896              :  *
    2897              :  * Returns true if okay, false if there is a syntax error in the string.
    2898              :  *
    2899              :  * Note that an empty string is considered okay here.
    2900              :  */
    2901              : bool
    2902          954 : SplitDirectoriesString(char *rawstring, char separator,
    2903              :                        List **namelist)
    2904              : {
    2905          954 :     char       *nextp = rawstring;
    2906          954 :     bool        done = false;
    2907              : 
    2908          954 :     *namelist = NIL;
    2909              : 
    2910          954 :     while (scanner_isspace(*nextp))
    2911            0 :         nextp++;                /* skip leading whitespace */
    2912              : 
    2913          954 :     if (*nextp == '\0')
    2914            1 :         return true;            /* empty string represents empty list */
    2915              : 
    2916              :     /* At the top of the loop, we are at start of a new directory. */
    2917              :     do
    2918              :     {
    2919              :         char       *curname;
    2920              :         char       *endp;
    2921              : 
    2922          958 :         if (*nextp == '"')
    2923              :         {
    2924              :             /* Quoted name --- collapse quote-quote pairs */
    2925            0 :             curname = nextp + 1;
    2926              :             for (;;)
    2927              :             {
    2928            0 :                 endp = strchr(nextp + 1, '"');
    2929            0 :                 if (endp == NULL)
    2930            0 :                     return false;   /* mismatched quotes */
    2931            0 :                 if (endp[1] != '"')
    2932            0 :                     break;      /* found end of quoted name */
    2933              :                 /* Collapse adjacent quotes into one quote, and look again */
    2934            0 :                 memmove(endp, endp + 1, strlen(endp));
    2935            0 :                 nextp = endp;
    2936              :             }
    2937              :             /* endp now points at the terminating quote */
    2938            0 :             nextp = endp + 1;
    2939              :         }
    2940              :         else
    2941              :         {
    2942              :             /* Unquoted name --- extends to separator or end of string */
    2943          958 :             curname = endp = nextp;
    2944        16038 :             while (*nextp && *nextp != separator)
    2945              :             {
    2946              :                 /* trailing whitespace should not be included in name */
    2947        15080 :                 if (!scanner_isspace(*nextp))
    2948        15080 :                     endp = nextp + 1;
    2949        15080 :                 nextp++;
    2950              :             }
    2951          958 :             if (curname == endp)
    2952            0 :                 return false;   /* empty unquoted name not allowed */
    2953              :         }
    2954              : 
    2955          958 :         while (scanner_isspace(*nextp))
    2956            0 :             nextp++;            /* skip trailing whitespace */
    2957              : 
    2958          958 :         if (*nextp == separator)
    2959              :         {
    2960            5 :             nextp++;
    2961            8 :             while (scanner_isspace(*nextp))
    2962            3 :                 nextp++;        /* skip leading whitespace for next */
    2963              :             /* we expect another name, so done remains false */
    2964              :         }
    2965          953 :         else if (*nextp == '\0')
    2966          953 :             done = true;
    2967              :         else
    2968            0 :             return false;       /* invalid syntax */
    2969              : 
    2970              :         /* Now safe to overwrite separator with a null */
    2971          958 :         *endp = '\0';
    2972              : 
    2973              :         /* Truncate path if it's overlength */
    2974          958 :         if (strlen(curname) >= MAXPGPATH)
    2975            0 :             curname[MAXPGPATH - 1] = '\0';
    2976              : 
    2977              :         /*
    2978              :          * Finished isolating current name --- add it to list
    2979              :          */
    2980          958 :         curname = pstrdup(curname);
    2981          958 :         canonicalize_path(curname);
    2982          958 :         *namelist = lappend(*namelist, curname);
    2983              : 
    2984              :         /* Loop back if we didn't reach end of string */
    2985          958 :     } while (!done);
    2986              : 
    2987          953 :     return true;
    2988              : }
    2989              : 
    2990              : 
    2991              : /*
    2992              :  * SplitGUCList --- parse a string containing identifiers or file names
    2993              :  *
    2994              :  * This is used to split the value of a GUC_LIST_QUOTE GUC variable, without
    2995              :  * presuming whether the elements will be taken as identifiers or file names.
    2996              :  * We assume the input has already been through flatten_set_variable_args(),
    2997              :  * so that we need never downcase (if appropriate, that was done already).
    2998              :  * Nor do we ever truncate, since we don't know the correct max length.
    2999              :  * We disallow embedded whitespace for simplicity (it shouldn't matter,
    3000              :  * because any embedded whitespace should have led to double-quoting).
    3001              :  * Otherwise the API is identical to SplitIdentifierString.
    3002              :  *
    3003              :  * XXX it's annoying to have so many copies of this string-splitting logic.
    3004              :  * However, it's not clear that having one function with a bunch of option
    3005              :  * flags would be much better.
    3006              :  *
    3007              :  * XXX there is a version of this function in src/bin/pg_dump/dumputils.c.
    3008              :  * Be sure to update that if you have to change this.
    3009              :  *
    3010              :  * Inputs:
    3011              :  *  rawstring: the input string; must be overwritable!  On return, it's
    3012              :  *             been modified to contain the separated identifiers.
    3013              :  *  separator: the separator punctuation expected between identifiers
    3014              :  *             (typically '.' or ',').  Whitespace may also appear around
    3015              :  *             identifiers.
    3016              :  * Outputs:
    3017              :  *  namelist: filled with a palloc'd list of pointers to identifiers within
    3018              :  *            rawstring.  Caller should list_free() this even on error return.
    3019              :  *
    3020              :  * Returns true if okay, false if there is a syntax error in the string.
    3021              :  */
    3022              : bool
    3023         3700 : SplitGUCList(char *rawstring, char separator,
    3024              :              List **namelist)
    3025              : {
    3026         3700 :     char       *nextp = rawstring;
    3027         3700 :     bool        done = false;
    3028              : 
    3029         3700 :     *namelist = NIL;
    3030              : 
    3031         3700 :     while (scanner_isspace(*nextp))
    3032            0 :         nextp++;                /* skip leading whitespace */
    3033              : 
    3034         3700 :     if (*nextp == '\0')
    3035         2062 :         return true;            /* empty string represents empty list */
    3036              : 
    3037              :     /* At the top of the loop, we are at start of a new identifier. */
    3038              :     do
    3039              :     {
    3040              :         char       *curname;
    3041              :         char       *endp;
    3042              : 
    3043         1699 :         if (*nextp == '"')
    3044              :         {
    3045              :             /* Quoted name --- collapse quote-quote pairs */
    3046           12 :             curname = nextp + 1;
    3047              :             for (;;)
    3048              :             {
    3049           18 :                 endp = strchr(nextp + 1, '"');
    3050           15 :                 if (endp == NULL)
    3051            0 :                     return false;   /* mismatched quotes */
    3052           15 :                 if (endp[1] != '"')
    3053           12 :                     break;      /* found end of quoted name */
    3054              :                 /* Collapse adjacent quotes into one quote, and look again */
    3055            3 :                 memmove(endp, endp + 1, strlen(endp));
    3056            3 :                 nextp = endp;
    3057              :             }
    3058              :             /* endp now points at the terminating quote */
    3059           12 :             nextp = endp + 1;
    3060              :         }
    3061              :         else
    3062              :         {
    3063              :             /* Unquoted name --- extends to separator or whitespace */
    3064         1687 :             curname = nextp;
    3065        13671 :             while (*nextp && *nextp != separator &&
    3066        11984 :                    !scanner_isspace(*nextp))
    3067        11984 :                 nextp++;
    3068         1687 :             endp = nextp;
    3069         1687 :             if (curname == nextp)
    3070            0 :                 return false;   /* empty unquoted name not allowed */
    3071              :         }
    3072              : 
    3073         1699 :         while (scanner_isspace(*nextp))
    3074            0 :             nextp++;            /* skip trailing whitespace */
    3075              : 
    3076         1699 :         if (*nextp == separator)
    3077              :         {
    3078           61 :             nextp++;
    3079          118 :             while (scanner_isspace(*nextp))
    3080           57 :                 nextp++;        /* skip leading whitespace for next */
    3081              :             /* we expect another name, so done remains false */
    3082              :         }
    3083         1638 :         else if (*nextp == '\0')
    3084         1638 :             done = true;
    3085              :         else
    3086            0 :             return false;       /* invalid syntax */
    3087              : 
    3088              :         /* Now safe to overwrite separator with a null */
    3089         1699 :         *endp = '\0';
    3090              : 
    3091              :         /*
    3092              :          * Finished isolating current name --- add it to list
    3093              :          */
    3094         1699 :         *namelist = lappend(*namelist, curname);
    3095              : 
    3096              :         /* Loop back if we didn't reach end of string */
    3097         1699 :     } while (!done);
    3098              : 
    3099         1638 :     return true;
    3100              : }
    3101              : 
    3102              : /*
    3103              :  * appendStringInfoText
    3104              :  *
    3105              :  * Append a text to str.
    3106              :  * Like appendStringInfoString(str, text_to_cstring(t)) but faster.
    3107              :  */
    3108              : static void
    3109      1087718 : appendStringInfoText(StringInfo str, const text *t)
    3110              : {
    3111      1087718 :     appendBinaryStringInfo(str, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t));
    3112      1087718 : }
    3113              : 
    3114              : /*
    3115              :  * replace_text
    3116              :  * replace all occurrences of 'old_sub_str' in 'orig_str'
    3117              :  * with 'new_sub_str' to form 'new_str'
    3118              :  *
    3119              :  * returns 'orig_str' if 'old_sub_str' == '' or 'orig_str' == ''
    3120              :  * otherwise returns 'new_str'
    3121              :  */
    3122              : Datum
    3123          784 : replace_text(PG_FUNCTION_ARGS)
    3124              : {
    3125          784 :     text       *src_text = PG_GETARG_TEXT_PP(0);
    3126          784 :     text       *from_sub_text = PG_GETARG_TEXT_PP(1);
    3127          784 :     text       *to_sub_text = PG_GETARG_TEXT_PP(2);
    3128              :     int         src_text_len;
    3129              :     int         from_sub_text_len;
    3130              :     TextPositionState state;
    3131              :     text       *ret_text;
    3132              :     int         chunk_len;
    3133              :     char       *curr_ptr;
    3134              :     char       *start_ptr;
    3135              :     StringInfoData str;
    3136              :     bool        found;
    3137              : 
    3138          784 :     src_text_len = VARSIZE_ANY_EXHDR(src_text);
    3139          784 :     from_sub_text_len = VARSIZE_ANY_EXHDR(from_sub_text);
    3140              : 
    3141              :     /* Return unmodified source string if empty source or pattern */
    3142          784 :     if (src_text_len < 1 || from_sub_text_len < 1)
    3143              :     {
    3144            0 :         PG_RETURN_TEXT_P(src_text);
    3145              :     }
    3146              : 
    3147          784 :     text_position_setup(src_text, from_sub_text, PG_GET_COLLATION(), &state);
    3148              : 
    3149          784 :     found = text_position_next(&state);
    3150              : 
    3151              :     /* When the from_sub_text is not found, there is nothing to do. */
    3152          784 :     if (!found)
    3153              :     {
    3154          167 :         text_position_cleanup(&state);
    3155          167 :         PG_RETURN_TEXT_P(src_text);
    3156              :     }
    3157          617 :     curr_ptr = text_position_get_match_ptr(&state);
    3158          617 :     start_ptr = VARDATA_ANY(src_text);
    3159              : 
    3160          617 :     initStringInfo(&str);
    3161              : 
    3162              :     do
    3163              :     {
    3164         3608 :         CHECK_FOR_INTERRUPTS();
    3165              : 
    3166              :         /* copy the data skipped over by last text_position_next() */
    3167         3608 :         chunk_len = curr_ptr - start_ptr;
    3168         3608 :         appendBinaryStringInfo(&str, start_ptr, chunk_len);
    3169              : 
    3170         3608 :         appendStringInfoText(&str, to_sub_text);
    3171              : 
    3172         3608 :         start_ptr = curr_ptr + state.last_match_len;
    3173              : 
    3174         3608 :         found = text_position_next(&state);
    3175         3608 :         if (found)
    3176         2991 :             curr_ptr = text_position_get_match_ptr(&state);
    3177              :     }
    3178         3608 :     while (found);
    3179              : 
    3180              :     /* copy trailing data */
    3181          617 :     chunk_len = ((char *) src_text + VARSIZE_ANY(src_text)) - start_ptr;
    3182          617 :     appendBinaryStringInfo(&str, start_ptr, chunk_len);
    3183              : 
    3184          617 :     text_position_cleanup(&state);
    3185              : 
    3186          617 :     ret_text = cstring_to_text_with_len(str.data, str.len);
    3187          617 :     pfree(str.data);
    3188              : 
    3189          617 :     PG_RETURN_TEXT_P(ret_text);
    3190              : }
    3191              : 
    3192              : /*
    3193              :  * check_replace_text_has_escape
    3194              :  *
    3195              :  * Returns 0 if text contains no backslashes that need processing.
    3196              :  * Returns 1 if text contains backslashes, but not regexp submatch specifiers.
    3197              :  * Returns 2 if text contains regexp submatch specifiers (\1 .. \9).
    3198              :  */
    3199              : static int
    3200         9393 : check_replace_text_has_escape(const text *replace_text)
    3201              : {
    3202         9393 :     int         result = 0;
    3203         9393 :     const char *p = VARDATA_ANY(replace_text);
    3204         9393 :     const char *p_end = p + VARSIZE_ANY_EXHDR(replace_text);
    3205              : 
    3206        18808 :     while (p < p_end)
    3207              :     {
    3208              :         /* Find next escape char, if any. */
    3209         8834 :         p = memchr(p, '\\', p_end - p);
    3210         8834 :         if (p == NULL)
    3211         8410 :             break;
    3212          424 :         p++;
    3213              :         /* Note: a backslash at the end doesn't require extra processing. */
    3214          424 :         if (p < p_end)
    3215              :         {
    3216          424 :             if (*p >= '1' && *p <= '9')
    3217          402 :                 return 2;       /* Found a submatch specifier, so done */
    3218           22 :             result = 1;         /* Found some other sequence, keep looking */
    3219           22 :             p++;
    3220              :         }
    3221              :     }
    3222         8991 :     return result;
    3223              : }
    3224              : 
    3225              : /*
    3226              :  * appendStringInfoRegexpSubstr
    3227              :  *
    3228              :  * Append replace_text to str, substituting regexp back references for
    3229              :  * \n escapes.  start_ptr is the start of the match in the source string,
    3230              :  * at logical character position data_pos.
    3231              :  */
    3232              : static void
    3233          127 : appendStringInfoRegexpSubstr(StringInfo str, text *replace_text,
    3234              :                              regmatch_t *pmatch,
    3235              :                              char *start_ptr, int data_pos)
    3236              : {
    3237          127 :     const char *p = VARDATA_ANY(replace_text);
    3238          127 :     const char *p_end = p + VARSIZE_ANY_EXHDR(replace_text);
    3239              : 
    3240          305 :     while (p < p_end)
    3241              :     {
    3242          268 :         const char *chunk_start = p;
    3243              :         int         so;
    3244              :         int         eo;
    3245              : 
    3246              :         /* Find next escape char, if any. */
    3247          268 :         p = memchr(p, '\\', p_end - p);
    3248          268 :         if (p == NULL)
    3249           87 :             p = p_end;
    3250              : 
    3251              :         /* Copy the text we just scanned over, if any. */
    3252          268 :         if (p > chunk_start)
    3253          159 :             appendBinaryStringInfo(str, chunk_start, p - chunk_start);
    3254              : 
    3255              :         /* Done if at end of string, else advance over escape char. */
    3256          268 :         if (p >= p_end)
    3257           87 :             break;
    3258          181 :         p++;
    3259              : 
    3260          181 :         if (p >= p_end)
    3261              :         {
    3262              :             /* Escape at very end of input.  Treat same as unexpected char */
    3263            3 :             appendStringInfoChar(str, '\\');
    3264            3 :             break;
    3265              :         }
    3266              : 
    3267          178 :         if (*p >= '1' && *p <= '9')
    3268          148 :         {
    3269              :             /* Use the back reference of regexp. */
    3270          148 :             int         idx = *p - '0';
    3271              : 
    3272          148 :             so = pmatch[idx].rm_so;
    3273          148 :             eo = pmatch[idx].rm_eo;
    3274          148 :             p++;
    3275              :         }
    3276           30 :         else if (*p == '&')
    3277              :         {
    3278              :             /* Use the entire matched string. */
    3279            9 :             so = pmatch[0].rm_so;
    3280            9 :             eo = pmatch[0].rm_eo;
    3281            9 :             p++;
    3282              :         }
    3283           21 :         else if (*p == '\\')
    3284              :         {
    3285              :             /* \\ means transfer one \ to output. */
    3286           18 :             appendStringInfoChar(str, '\\');
    3287           18 :             p++;
    3288           18 :             continue;
    3289              :         }
    3290              :         else
    3291              :         {
    3292              :             /*
    3293              :              * If escape char is not followed by any expected char, just treat
    3294              :              * it as ordinary data to copy.  (XXX would it be better to throw
    3295              :              * an error?)
    3296              :              */
    3297            3 :             appendStringInfoChar(str, '\\');
    3298            3 :             continue;
    3299              :         }
    3300              : 
    3301          157 :         if (so >= 0 && eo >= 0)
    3302              :         {
    3303              :             /*
    3304              :              * Copy the text that is back reference of regexp.  Note so and eo
    3305              :              * are counted in characters not bytes.
    3306              :              */
    3307              :             char       *chunk_start;
    3308              :             int         chunk_len;
    3309              : 
    3310              :             Assert(so >= data_pos);
    3311          157 :             chunk_start = start_ptr;
    3312          157 :             chunk_start += charlen_to_bytelen(chunk_start, so - data_pos);
    3313          157 :             chunk_len = charlen_to_bytelen(chunk_start, eo - so);
    3314          157 :             appendBinaryStringInfo(str, chunk_start, chunk_len);
    3315              :         }
    3316              :     }
    3317          127 : }
    3318              : 
    3319              : /*
    3320              :  * replace_text_regexp
    3321              :  *
    3322              :  * replace substring(s) in src_text that match pattern with replace_text.
    3323              :  * The replace_text can contain backslash markers to substitute
    3324              :  * (parts of) the matched text.
    3325              :  *
    3326              :  * cflags: regexp compile flags.
    3327              :  * collation: collation to use.
    3328              :  * search_start: the character (not byte) offset in src_text at which to
    3329              :  * begin searching.
    3330              :  * n: if 0, replace all matches; if > 0, replace only the N'th match.
    3331              :  */
    3332              : text *
    3333         9393 : replace_text_regexp(text *src_text, text *pattern_text,
    3334              :                     text *replace_text,
    3335              :                     int cflags, Oid collation,
    3336              :                     int search_start, int n)
    3337              : {
    3338              :     text       *ret_text;
    3339              :     regex_t    *re;
    3340         9393 :     int         src_text_len = VARSIZE_ANY_EXHDR(src_text);
    3341         9393 :     int         nmatches = 0;
    3342              :     StringInfoData buf;
    3343              :     regmatch_t  pmatch[10];     /* main match, plus \1 to \9 */
    3344         9393 :     int         nmatch = lengthof(pmatch);
    3345              :     pg_wchar   *data;
    3346              :     size_t      data_len;
    3347              :     int         data_pos;
    3348              :     char       *start_ptr;
    3349              :     int         escape_status;
    3350              : 
    3351         9393 :     initStringInfo(&buf);
    3352              : 
    3353              :     /* Convert data string to wide characters. */
    3354         9393 :     data = (pg_wchar *) palloc((src_text_len + 1) * sizeof(pg_wchar));
    3355         9393 :     data_len = pg_mb2wchar_with_len(VARDATA_ANY(src_text), data, src_text_len);
    3356              : 
    3357              :     /* Check whether replace_text has escapes, especially regexp submatches. */
    3358         9393 :     escape_status = check_replace_text_has_escape(replace_text);
    3359              : 
    3360              :     /* If no regexp submatches, we can use REG_NOSUB. */
    3361         9393 :     if (escape_status < 2)
    3362              :     {
    3363         8991 :         cflags |= REG_NOSUB;
    3364              :         /* Also tell pg_regexec we only want the whole-match location. */
    3365         8991 :         nmatch = 1;
    3366              :     }
    3367              : 
    3368              :     /* Prepare the regexp. */
    3369         9393 :     re = RE_compile_and_cache(pattern_text, cflags, collation);
    3370              : 
    3371              :     /* start_ptr points to the data_pos'th character of src_text */
    3372         9393 :     start_ptr = (char *) VARDATA_ANY(src_text);
    3373         9393 :     data_pos = 0;
    3374              : 
    3375        12603 :     while (search_start <= data_len)
    3376              :     {
    3377              :         int         regexec_result;
    3378              : 
    3379        12600 :         CHECK_FOR_INTERRUPTS();
    3380              : 
    3381        12600 :         regexec_result = pg_regexec(re,
    3382              :                                     data,
    3383              :                                     data_len,
    3384              :                                     search_start,
    3385              :                                     NULL,   /* no details */
    3386              :                                     nmatch,
    3387              :                                     pmatch,
    3388              :                                     0);
    3389              : 
    3390        12600 :         if (regexec_result == REG_NOMATCH)
    3391         8355 :             break;
    3392              : 
    3393         4245 :         if (regexec_result != REG_OKAY)
    3394              :         {
    3395              :             char        errMsg[100];
    3396              : 
    3397            0 :             pg_regerror(regexec_result, re, errMsg, sizeof(errMsg));
    3398            0 :             ereport(ERROR,
    3399              :                     (errcode(ERRCODE_INVALID_REGULAR_EXPRESSION),
    3400              :                      errmsg("regular expression failed: %s", errMsg)));
    3401              :         }
    3402              : 
    3403              :         /*
    3404              :          * Count matches, and decide whether to replace this match.
    3405              :          */
    3406         4245 :         nmatches++;
    3407         4245 :         if (n > 0 && nmatches != n)
    3408              :         {
    3409              :             /*
    3410              :              * No, so advance search_start, but not start_ptr/data_pos. (Thus,
    3411              :              * we treat the matched text as if it weren't matched, and copy it
    3412              :              * to the output later.)
    3413              :              */
    3414           30 :             search_start = pmatch[0].rm_eo;
    3415           30 :             if (pmatch[0].rm_so == pmatch[0].rm_eo)
    3416            0 :                 search_start++;
    3417           30 :             continue;
    3418              :         }
    3419              : 
    3420              :         /*
    3421              :          * Copy the text to the left of the match position.  Note we are given
    3422              :          * character not byte indexes.
    3423              :          */
    3424         4215 :         if (pmatch[0].rm_so - data_pos > 0)
    3425              :         {
    3426              :             int         chunk_len;
    3427              : 
    3428         4127 :             chunk_len = charlen_to_bytelen(start_ptr,
    3429         4127 :                                            pmatch[0].rm_so - data_pos);
    3430         4127 :             appendBinaryStringInfo(&buf, start_ptr, chunk_len);
    3431              : 
    3432              :             /*
    3433              :              * Advance start_ptr over that text, to avoid multiple rescans of
    3434              :              * it if the replace_text contains multiple back-references.
    3435              :              */
    3436         4127 :             start_ptr += chunk_len;
    3437         4127 :             data_pos = pmatch[0].rm_so;
    3438              :         }
    3439              : 
    3440              :         /*
    3441              :          * Copy the replace_text, processing escapes if any are present.
    3442              :          */
    3443         4215 :         if (escape_status > 0)
    3444          127 :             appendStringInfoRegexpSubstr(&buf, replace_text, pmatch,
    3445              :                                          start_ptr, data_pos);
    3446              :         else
    3447         4088 :             appendStringInfoText(&buf, replace_text);
    3448              : 
    3449              :         /* Advance start_ptr and data_pos over the matched text. */
    3450         8430 :         start_ptr += charlen_to_bytelen(start_ptr,
    3451         4215 :                                         pmatch[0].rm_eo - data_pos);
    3452         4215 :         data_pos = pmatch[0].rm_eo;
    3453              : 
    3454              :         /*
    3455              :          * If we only want to replace one occurrence, we're done.
    3456              :          */
    3457         4215 :         if (n > 0)
    3458         1035 :             break;
    3459              : 
    3460              :         /*
    3461              :          * Advance search position.  Normally we start the next search at the
    3462              :          * end of the previous match; but if the match was of zero length, we
    3463              :          * have to advance by one character, or we'd just find the same match
    3464              :          * again.
    3465              :          */
    3466         3180 :         search_start = data_pos;
    3467         3180 :         if (pmatch[0].rm_so == pmatch[0].rm_eo)
    3468            6 :             search_start++;
    3469              :     }
    3470              : 
    3471              :     /*
    3472              :      * Copy the text to the right of the last match.
    3473              :      */
    3474         9393 :     if (data_pos < data_len)
    3475              :     {
    3476              :         int         chunk_len;
    3477              : 
    3478         8946 :         chunk_len = ((char *) src_text + VARSIZE_ANY(src_text)) - start_ptr;
    3479         8946 :         appendBinaryStringInfo(&buf, start_ptr, chunk_len);
    3480              :     }
    3481              : 
    3482         9393 :     ret_text = cstring_to_text_with_len(buf.data, buf.len);
    3483         9393 :     pfree(buf.data);
    3484         9393 :     pfree(data);
    3485              : 
    3486         9393 :     return ret_text;
    3487              : }
    3488              : 
    3489              : /*
    3490              :  * split_part
    3491              :  * parse input string based on provided field separator
    3492              :  * return N'th item (1 based, negative counts from end)
    3493              :  */
    3494              : Datum
    3495           75 : split_part(PG_FUNCTION_ARGS)
    3496              : {
    3497           75 :     text       *inputstring = PG_GETARG_TEXT_PP(0);
    3498           75 :     text       *fldsep = PG_GETARG_TEXT_PP(1);
    3499           75 :     int         fldnum = PG_GETARG_INT32(2);
    3500              :     int         inputstring_len;
    3501              :     int         fldsep_len;
    3502              :     TextPositionState state;
    3503              :     char       *start_ptr;
    3504              :     char       *end_ptr;
    3505              :     text       *result_text;
    3506              :     bool        found;
    3507              : 
    3508              :     /* field number is 1 based */
    3509           75 :     if (fldnum == 0)
    3510            3 :         ereport(ERROR,
    3511              :                 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
    3512              :                  errmsg("field position must not be zero")));
    3513              : 
    3514           72 :     inputstring_len = VARSIZE_ANY_EXHDR(inputstring);
    3515           72 :     fldsep_len = VARSIZE_ANY_EXHDR(fldsep);
    3516              : 
    3517              :     /* return empty string for empty input string */
    3518           72 :     if (inputstring_len < 1)
    3519            6 :         PG_RETURN_TEXT_P(cstring_to_text(""));
    3520              : 
    3521              :     /* handle empty field separator */
    3522           66 :     if (fldsep_len < 1)
    3523              :     {
    3524              :         /* if first or last field, return input string, else empty string */
    3525           12 :         if (fldnum == 1 || fldnum == -1)
    3526            6 :             PG_RETURN_TEXT_P(inputstring);
    3527              :         else
    3528            6 :             PG_RETURN_TEXT_P(cstring_to_text(""));
    3529              :     }
    3530              : 
    3531              :     /* find the first field separator */
    3532           54 :     text_position_setup(inputstring, fldsep, PG_GET_COLLATION(), &state);
    3533              : 
    3534           54 :     found = text_position_next(&state);
    3535              : 
    3536              :     /* special case if fldsep not found at all */
    3537           54 :     if (!found)
    3538              :     {
    3539           12 :         text_position_cleanup(&state);
    3540              :         /* if first or last field, return input string, else empty string */
    3541           12 :         if (fldnum == 1 || fldnum == -1)
    3542            6 :             PG_RETURN_TEXT_P(inputstring);
    3543              :         else
    3544            6 :             PG_RETURN_TEXT_P(cstring_to_text(""));
    3545              :     }
    3546              : 
    3547              :     /*
    3548              :      * take care of a negative field number (i.e. count from the right) by
    3549              :      * converting to a positive field number; we need total number of fields
    3550              :      */
    3551           42 :     if (fldnum < 0)
    3552              :     {
    3553              :         /* we found a fldsep, so there are at least two fields */
    3554           21 :         int         numfields = 2;
    3555              : 
    3556           27 :         while (text_position_next(&state))
    3557            6 :             numfields++;
    3558              : 
    3559              :         /* special case of last field does not require an extra pass */
    3560           21 :         if (fldnum == -1)
    3561              :         {
    3562           12 :             start_ptr = text_position_get_match_ptr(&state) + state.last_match_len;
    3563           12 :             end_ptr = VARDATA_ANY(inputstring) + inputstring_len;
    3564           12 :             text_position_cleanup(&state);
    3565           12 :             PG_RETURN_TEXT_P(cstring_to_text_with_len(start_ptr,
    3566              :                                                       end_ptr - start_ptr));
    3567              :         }
    3568              : 
    3569              :         /* else, convert fldnum to positive notation */
    3570            9 :         fldnum += numfields + 1;
    3571              : 
    3572              :         /* if nonexistent field, return empty string */
    3573            9 :         if (fldnum <= 0)
    3574              :         {
    3575            3 :             text_position_cleanup(&state);
    3576            3 :             PG_RETURN_TEXT_P(cstring_to_text(""));
    3577              :         }
    3578              : 
    3579              :         /* reset to pointing at first match, but now with positive fldnum */
    3580            6 :         text_position_reset(&state);
    3581            6 :         found = text_position_next(&state);
    3582              :         Assert(found);
    3583              :     }
    3584              : 
    3585              :     /* identify bounds of first field */
    3586           27 :     start_ptr = VARDATA_ANY(inputstring);
    3587           27 :     end_ptr = text_position_get_match_ptr(&state);
    3588              : 
    3589           51 :     while (found && --fldnum > 0)
    3590              :     {
    3591              :         /* identify bounds of next field */
    3592           24 :         start_ptr = end_ptr + state.last_match_len;
    3593           24 :         found = text_position_next(&state);
    3594           24 :         if (found)
    3595            9 :             end_ptr = text_position_get_match_ptr(&state);
    3596              :     }
    3597              : 
    3598           27 :     text_position_cleanup(&state);
    3599              : 
    3600           27 :     if (fldnum > 0)
    3601              :     {
    3602              :         /* N'th field separator not found */
    3603              :         /* if last field requested, return it, else empty string */
    3604           15 :         if (fldnum == 1)
    3605              :         {
    3606           12 :             int         last_len = start_ptr - VARDATA_ANY(inputstring);
    3607              : 
    3608           12 :             result_text = cstring_to_text_with_len(start_ptr,
    3609              :                                                    inputstring_len - last_len);
    3610              :         }
    3611              :         else
    3612            3 :             result_text = cstring_to_text("");
    3613              :     }
    3614              :     else
    3615              :     {
    3616              :         /* non-last field requested */
    3617           12 :         result_text = cstring_to_text_with_len(start_ptr, end_ptr - start_ptr);
    3618              :     }
    3619              : 
    3620           27 :     PG_RETURN_TEXT_P(result_text);
    3621              : }
    3622              : 
    3623              : /*
    3624              :  * Convenience function to return true when two text params are equal.
    3625              :  */
    3626              : static bool
    3627          192 : text_isequal(text *txt1, text *txt2, Oid collid)
    3628              : {
    3629          192 :     return DatumGetBool(DirectFunctionCall2Coll(texteq,
    3630              :                                                 collid,
    3631              :                                                 PointerGetDatum(txt1),
    3632              :                                                 PointerGetDatum(txt2)));
    3633              : }
    3634              : 
    3635              : /*
    3636              :  * text_to_array
    3637              :  * parse input string and return text array of elements,
    3638              :  * based on provided field separator
    3639              :  */
    3640              : Datum
    3641           85 : text_to_array(PG_FUNCTION_ARGS)
    3642              : {
    3643              :     SplitTextOutputData tstate;
    3644              : 
    3645              :     /* For array output, tstate should start as all zeroes */
    3646           85 :     memset(&tstate, 0, sizeof(tstate));
    3647              : 
    3648           85 :     if (!split_text(fcinfo, &tstate))
    3649            3 :         PG_RETURN_NULL();
    3650              : 
    3651           82 :     if (tstate.astate == NULL)
    3652            3 :         PG_RETURN_ARRAYTYPE_P(construct_empty_array(TEXTOID));
    3653              : 
    3654           79 :     PG_RETURN_DATUM(makeArrayResult(tstate.astate,
    3655              :                                     CurrentMemoryContext));
    3656              : }
    3657              : 
    3658              : /*
    3659              :  * text_to_array_null
    3660              :  * parse input string and return text array of elements,
    3661              :  * based on provided field separator and null string
    3662              :  *
    3663              :  * This is a separate entry point only to prevent the regression tests from
    3664              :  * complaining about different argument sets for the same internal function.
    3665              :  */
    3666              : Datum
    3667           30 : text_to_array_null(PG_FUNCTION_ARGS)
    3668              : {
    3669           30 :     return text_to_array(fcinfo);
    3670              : }
    3671              : 
    3672              : /*
    3673              :  * text_to_table
    3674              :  * parse input string and return table of elements,
    3675              :  * based on provided field separator
    3676              :  */
    3677              : Datum
    3678           42 : text_to_table(PG_FUNCTION_ARGS)
    3679              : {
    3680           42 :     ReturnSetInfo *rsi = (ReturnSetInfo *) fcinfo->resultinfo;
    3681              :     SplitTextOutputData tstate;
    3682              : 
    3683           42 :     tstate.astate = NULL;
    3684           42 :     InitMaterializedSRF(fcinfo, MAT_SRF_USE_EXPECTED_DESC);
    3685           42 :     tstate.tupstore = rsi->setResult;
    3686           42 :     tstate.tupdesc = rsi->setDesc;
    3687              : 
    3688           42 :     (void) split_text(fcinfo, &tstate);
    3689              : 
    3690           42 :     return (Datum) 0;
    3691              : }
    3692              : 
    3693              : /*
    3694              :  * text_to_table_null
    3695              :  * parse input string and return table of elements,
    3696              :  * based on provided field separator and null string
    3697              :  *
    3698              :  * This is a separate entry point only to prevent the regression tests from
    3699              :  * complaining about different argument sets for the same internal function.
    3700              :  */
    3701              : Datum
    3702           12 : text_to_table_null(PG_FUNCTION_ARGS)
    3703              : {
    3704           12 :     return text_to_table(fcinfo);
    3705              : }
    3706              : 
    3707              : /*
    3708              :  * Common code for text_to_array, text_to_array_null, text_to_table
    3709              :  * and text_to_table_null functions.
    3710              :  *
    3711              :  * These are not strict so we have to test for null inputs explicitly.
    3712              :  * Returns false if result is to be null, else returns true.
    3713              :  *
    3714              :  * Note that if the result is valid but empty (zero elements), we return
    3715              :  * without changing *tstate --- caller must handle that case, too.
    3716              :  */
    3717              : static bool
    3718          127 : split_text(FunctionCallInfo fcinfo, SplitTextOutputData *tstate)
    3719              : {
    3720              :     text       *inputstring;
    3721              :     text       *fldsep;
    3722              :     text       *null_string;
    3723          127 :     Oid         collation = PG_GET_COLLATION();
    3724              :     int         inputstring_len;
    3725              :     int         fldsep_len;
    3726              :     char       *start_ptr;
    3727              :     text       *result_text;
    3728              : 
    3729              :     /* when input string is NULL, then result is NULL too */
    3730          127 :     if (PG_ARGISNULL(0))
    3731            6 :         return false;
    3732              : 
    3733          121 :     inputstring = PG_GETARG_TEXT_PP(0);
    3734              : 
    3735              :     /* fldsep can be NULL */
    3736          121 :     if (!PG_ARGISNULL(1))
    3737          106 :         fldsep = PG_GETARG_TEXT_PP(1);
    3738              :     else
    3739           15 :         fldsep = NULL;
    3740              : 
    3741              :     /* null_string can be NULL or omitted */
    3742          121 :     if (PG_NARGS() > 2 && !PG_ARGISNULL(2))
    3743           42 :         null_string = PG_GETARG_TEXT_PP(2);
    3744              :     else
    3745           79 :         null_string = NULL;
    3746              : 
    3747          121 :     if (fldsep != NULL)
    3748              :     {
    3749              :         /*
    3750              :          * Normal case with non-null fldsep.  Use the text_position machinery
    3751              :          * to search for occurrences of fldsep.
    3752              :          */
    3753              :         TextPositionState state;
    3754              : 
    3755          106 :         inputstring_len = VARSIZE_ANY_EXHDR(inputstring);
    3756          106 :         fldsep_len = VARSIZE_ANY_EXHDR(fldsep);
    3757              : 
    3758              :         /* return empty set for empty input string */
    3759          106 :         if (inputstring_len < 1)
    3760           30 :             return true;
    3761              : 
    3762              :         /* empty field separator: return input string as a one-element set */
    3763          100 :         if (fldsep_len < 1)
    3764              :         {
    3765           24 :             split_text_accum_result(tstate, inputstring,
    3766              :                                     null_string, collation);
    3767           24 :             return true;
    3768              :         }
    3769              : 
    3770           76 :         text_position_setup(inputstring, fldsep, collation, &state);
    3771              : 
    3772           76 :         start_ptr = VARDATA_ANY(inputstring);
    3773              : 
    3774              :         for (;;)
    3775          256 :         {
    3776              :             bool        found;
    3777              :             char       *end_ptr;
    3778              :             int         chunk_len;
    3779              : 
    3780          332 :             CHECK_FOR_INTERRUPTS();
    3781              : 
    3782          332 :             found = text_position_next(&state);
    3783          332 :             if (!found)
    3784              :             {
    3785              :                 /* fetch last field */
    3786           76 :                 chunk_len = ((char *) inputstring + VARSIZE_ANY(inputstring)) - start_ptr;
    3787           76 :                 end_ptr = NULL; /* not used, but some compilers complain */
    3788              :             }
    3789              :             else
    3790              :             {
    3791              :                 /* fetch non-last field */
    3792          256 :                 end_ptr = text_position_get_match_ptr(&state);
    3793          256 :                 chunk_len = end_ptr - start_ptr;
    3794              :             }
    3795              : 
    3796              :             /* build a temp text datum to pass to split_text_accum_result */
    3797          332 :             result_text = cstring_to_text_with_len(start_ptr, chunk_len);
    3798              : 
    3799              :             /* stash away this field */
    3800          332 :             split_text_accum_result(tstate, result_text,
    3801              :                                     null_string, collation);
    3802              : 
    3803          332 :             pfree(result_text);
    3804              : 
    3805          332 :             if (!found)
    3806           76 :                 break;
    3807              : 
    3808          256 :             start_ptr = end_ptr + state.last_match_len;
    3809              :         }
    3810              : 
    3811           76 :         text_position_cleanup(&state);
    3812              :     }
    3813              :     else
    3814              :     {
    3815              :         const char *end_ptr;
    3816              : 
    3817              :         /*
    3818              :          * When fldsep is NULL, each character in the input string becomes a
    3819              :          * separate element in the result set.  The separator is effectively
    3820              :          * the space between characters.
    3821              :          */
    3822           15 :         inputstring_len = VARSIZE_ANY_EXHDR(inputstring);
    3823              : 
    3824           15 :         start_ptr = VARDATA_ANY(inputstring);
    3825           15 :         end_ptr = start_ptr + inputstring_len;
    3826              : 
    3827          126 :         while (inputstring_len > 0)
    3828              :         {
    3829          111 :             int         chunk_len = pg_mblen_range(start_ptr, end_ptr);
    3830              : 
    3831          111 :             CHECK_FOR_INTERRUPTS();
    3832              : 
    3833              :             /* build a temp text datum to pass to split_text_accum_result */
    3834          111 :             result_text = cstring_to_text_with_len(start_ptr, chunk_len);
    3835              : 
    3836              :             /* stash away this field */
    3837          111 :             split_text_accum_result(tstate, result_text,
    3838              :                                     null_string, collation);
    3839              : 
    3840          111 :             pfree(result_text);
    3841              : 
    3842          111 :             start_ptr += chunk_len;
    3843          111 :             inputstring_len -= chunk_len;
    3844              :         }
    3845              :     }
    3846              : 
    3847           91 :     return true;
    3848              : }
    3849              : 
    3850              : /*
    3851              :  * Add text item to result set (table or array).
    3852              :  *
    3853              :  * This is also responsible for checking to see if the item matches
    3854              :  * the null_string, in which case we should emit NULL instead.
    3855              :  */
    3856              : static void
    3857          467 : split_text_accum_result(SplitTextOutputData *tstate,
    3858              :                         text *field_value,
    3859              :                         text *null_string,
    3860              :                         Oid collation)
    3861              : {
    3862          467 :     bool        is_null = false;
    3863              : 
    3864          467 :     if (null_string && text_isequal(field_value, null_string, collation))
    3865           36 :         is_null = true;
    3866              : 
    3867          467 :     if (tstate->tupstore)
    3868              :     {
    3869              :         Datum       values[1];
    3870              :         bool        nulls[1];
    3871              : 
    3872          114 :         values[0] = PointerGetDatum(field_value);
    3873          114 :         nulls[0] = is_null;
    3874              : 
    3875          114 :         tuplestore_putvalues(tstate->tupstore,
    3876              :                              tstate->tupdesc,
    3877              :                              values,
    3878              :                              nulls);
    3879              :     }
    3880              :     else
    3881              :     {
    3882          353 :         tstate->astate = accumArrayResult(tstate->astate,
    3883              :                                           PointerGetDatum(field_value),
    3884              :                                           is_null,
    3885              :                                           TEXTOID,
    3886              :                                           CurrentMemoryContext);
    3887              :     }
    3888          467 : }
    3889              : 
    3890              : /*
    3891              :  * array_to_text
    3892              :  * concatenate Cstring representation of input array elements
    3893              :  * using provided field separator
    3894              :  */
    3895              : Datum
    3896        38547 : array_to_text(PG_FUNCTION_ARGS)
    3897              : {
    3898        38547 :     ArrayType  *v = PG_GETARG_ARRAYTYPE_P(0);
    3899        38547 :     char       *fldsep = text_to_cstring(PG_GETARG_TEXT_PP(1));
    3900              : 
    3901        38547 :     PG_RETURN_TEXT_P(array_to_text_internal(fcinfo, v, fldsep, NULL));
    3902              : }
    3903              : 
    3904              : /*
    3905              :  * array_to_text_null
    3906              :  * concatenate Cstring representation of input array elements
    3907              :  * using provided field separator and null string
    3908              :  *
    3909              :  * This version is not strict so we have to test for null inputs explicitly.
    3910              :  */
    3911              : Datum
    3912            6 : array_to_text_null(PG_FUNCTION_ARGS)
    3913              : {
    3914              :     ArrayType  *v;
    3915              :     char       *fldsep;
    3916              :     char       *null_string;
    3917              : 
    3918              :     /* returns NULL when first or second parameter is NULL */
    3919            6 :     if (PG_ARGISNULL(0) || PG_ARGISNULL(1))
    3920            0 :         PG_RETURN_NULL();
    3921              : 
    3922            6 :     v = PG_GETARG_ARRAYTYPE_P(0);
    3923            6 :     fldsep = text_to_cstring(PG_GETARG_TEXT_PP(1));
    3924              : 
    3925              :     /* NULL null string is passed through as a null pointer */
    3926            6 :     if (!PG_ARGISNULL(2))
    3927            3 :         null_string = text_to_cstring(PG_GETARG_TEXT_PP(2));
    3928              :     else
    3929            3 :         null_string = NULL;
    3930              : 
    3931            6 :     PG_RETURN_TEXT_P(array_to_text_internal(fcinfo, v, fldsep, null_string));
    3932              : }
    3933              : 
    3934              : /*
    3935              :  * common code for array_to_text and array_to_text_null functions
    3936              :  */
    3937              : static text *
    3938        38562 : array_to_text_internal(FunctionCallInfo fcinfo, ArrayType *v,
    3939              :                        const char *fldsep, const char *null_string)
    3940              : {
    3941              :     text       *result;
    3942              :     int         nitems,
    3943              :                *dims,
    3944              :                 ndims;
    3945              :     Oid         element_type;
    3946              :     int         typlen;
    3947              :     bool        typbyval;
    3948              :     char        typalign;
    3949              :     uint8       typalignby;
    3950              :     StringInfoData buf;
    3951        38562 :     bool        printed = false;
    3952              :     char       *p;
    3953              :     bits8      *bitmap;
    3954              :     int         bitmask;
    3955              :     int         i;
    3956              :     ArrayMetaState *my_extra;
    3957              : 
    3958        38562 :     ndims = ARR_NDIM(v);
    3959        38562 :     dims = ARR_DIMS(v);
    3960        38562 :     nitems = ArrayGetNItems(ndims, dims);
    3961              : 
    3962              :     /* if there are no elements, return an empty string */
    3963        38562 :     if (nitems == 0)
    3964        25965 :         return cstring_to_text_with_len("", 0);
    3965              : 
    3966        12597 :     element_type = ARR_ELEMTYPE(v);
    3967        12597 :     initStringInfo(&buf);
    3968              : 
    3969              :     /*
    3970              :      * We arrange to look up info about element type, including its output
    3971              :      * conversion proc, only once per series of calls, assuming the element
    3972              :      * type doesn't change underneath us.
    3973              :      */
    3974        12597 :     my_extra = (ArrayMetaState *) fcinfo->flinfo->fn_extra;
    3975        12597 :     if (my_extra == NULL)
    3976              :     {
    3977          710 :         fcinfo->flinfo->fn_extra = MemoryContextAlloc(fcinfo->flinfo->fn_mcxt,
    3978              :                                                       sizeof(ArrayMetaState));
    3979          710 :         my_extra = (ArrayMetaState *) fcinfo->flinfo->fn_extra;
    3980          710 :         my_extra->element_type = ~element_type;
    3981              :     }
    3982              : 
    3983        12597 :     if (my_extra->element_type != element_type)
    3984              :     {
    3985              :         /*
    3986              :          * Get info about element type, including its output conversion proc
    3987              :          */
    3988          710 :         get_type_io_data(element_type, IOFunc_output,
    3989              :                          &my_extra->typlen, &my_extra->typbyval,
    3990              :                          &my_extra->typalign, &my_extra->typdelim,
    3991              :                          &my_extra->typioparam, &my_extra->typiofunc);
    3992          710 :         fmgr_info_cxt(my_extra->typiofunc, &my_extra->proc,
    3993          710 :                       fcinfo->flinfo->fn_mcxt);
    3994          710 :         my_extra->element_type = element_type;
    3995              :     }
    3996        12597 :     typlen = my_extra->typlen;
    3997        12597 :     typbyval = my_extra->typbyval;
    3998        12597 :     typalign = my_extra->typalign;
    3999        12597 :     typalignby = typalign_to_alignby(typalign);
    4000              : 
    4001        12597 :     p = ARR_DATA_PTR(v);
    4002        12597 :     bitmap = ARR_NULLBITMAP(v);
    4003        12597 :     bitmask = 1;
    4004              : 
    4005        42828 :     for (i = 0; i < nitems; i++)
    4006              :     {
    4007              :         Datum       itemvalue;
    4008              :         char       *value;
    4009              : 
    4010              :         /* Get source element, checking for NULL */
    4011        30231 :         if (bitmap && (*bitmap & bitmask) == 0)
    4012              :         {
    4013              :             /* if null_string is NULL, we just ignore null elements */
    4014            9 :             if (null_string != NULL)
    4015              :             {
    4016            3 :                 if (printed)
    4017            3 :                     appendStringInfo(&buf, "%s%s", fldsep, null_string);
    4018              :                 else
    4019            0 :                     appendStringInfoString(&buf, null_string);
    4020            3 :                 printed = true;
    4021              :             }
    4022              :         }
    4023              :         else
    4024              :         {
    4025        30222 :             itemvalue = fetch_att(p, typbyval, typlen);
    4026              : 
    4027        30222 :             value = OutputFunctionCall(&my_extra->proc, itemvalue);
    4028              : 
    4029        30222 :             if (printed)
    4030        17625 :                 appendStringInfo(&buf, "%s%s", fldsep, value);
    4031              :             else
    4032        12597 :                 appendStringInfoString(&buf, value);
    4033        30222 :             printed = true;
    4034              : 
    4035        30222 :             p = att_addlength_pointer(p, typlen, p);
    4036        30222 :             p = (char *) att_nominal_alignby(p, typalignby);
    4037              :         }
    4038              : 
    4039              :         /* advance bitmap pointer if any */
    4040        30231 :         if (bitmap)
    4041              :         {
    4042           54 :             bitmask <<= 1;
    4043           54 :             if (bitmask == 0x100)
    4044              :             {
    4045            0 :                 bitmap++;
    4046            0 :                 bitmask = 1;
    4047              :             }
    4048              :         }
    4049              :     }
    4050              : 
    4051        12597 :     result = cstring_to_text_with_len(buf.data, buf.len);
    4052        12597 :     pfree(buf.data);
    4053              : 
    4054        12597 :     return result;
    4055              : }
    4056              : 
    4057              : /*
    4058              :  * Workhorse for to_bin, to_oct, and to_hex.  Note that base must be > 1 and <=
    4059              :  * 16.
    4060              :  */
    4061              : static inline text *
    4062        19375 : convert_to_base(uint64 value, int base)
    4063              : {
    4064        19375 :     const char *digits = "0123456789abcdef";
    4065              : 
    4066              :     /* We size the buffer for to_bin's longest possible return value. */
    4067              :     char        buf[sizeof(uint64) * BITS_PER_BYTE];
    4068        19375 :     char       *const end = buf + sizeof(buf);
    4069        19375 :     char       *ptr = end;
    4070              : 
    4071              :     Assert(base > 1);
    4072              :     Assert(base <= 16);
    4073              : 
    4074              :     do
    4075              :     {
    4076        37987 :         *--ptr = digits[value % base];
    4077        37987 :         value /= base;
    4078        37987 :     } while (ptr > buf && value);
    4079              : 
    4080        19375 :     return cstring_to_text_with_len(ptr, end - ptr);
    4081              : }
    4082              : 
    4083              : /*
    4084              :  * Convert an integer to a string containing a base-2 (binary) representation
    4085              :  * of the number.
    4086              :  */
    4087              : Datum
    4088            6 : to_bin32(PG_FUNCTION_ARGS)
    4089              : {
    4090            6 :     uint64      value = (uint32) PG_GETARG_INT32(0);
    4091              : 
    4092            6 :     PG_RETURN_TEXT_P(convert_to_base(value, 2));
    4093              : }
    4094              : Datum
    4095            6 : to_bin64(PG_FUNCTION_ARGS)
    4096              : {
    4097            6 :     uint64      value = (uint64) PG_GETARG_INT64(0);
    4098              : 
    4099            6 :     PG_RETURN_TEXT_P(convert_to_base(value, 2));
    4100              : }
    4101              : 
    4102              : /*
    4103              :  * Convert an integer to a string containing a base-8 (oct) representation of
    4104              :  * the number.
    4105              :  */
    4106              : Datum
    4107            6 : to_oct32(PG_FUNCTION_ARGS)
    4108              : {
    4109            6 :     uint64      value = (uint32) PG_GETARG_INT32(0);
    4110              : 
    4111            6 :     PG_RETURN_TEXT_P(convert_to_base(value, 8));
    4112              : }
    4113              : Datum
    4114            6 : to_oct64(PG_FUNCTION_ARGS)
    4115              : {
    4116            6 :     uint64      value = (uint64) PG_GETARG_INT64(0);
    4117              : 
    4118            6 :     PG_RETURN_TEXT_P(convert_to_base(value, 8));
    4119              : }
    4120              : 
    4121              : /*
    4122              :  * Convert an integer to a string containing a base-16 (hex) representation of
    4123              :  * the number.
    4124              :  */
    4125              : Datum
    4126        19345 : to_hex32(PG_FUNCTION_ARGS)
    4127              : {
    4128        19345 :     uint64      value = (uint32) PG_GETARG_INT32(0);
    4129              : 
    4130        19345 :     PG_RETURN_TEXT_P(convert_to_base(value, 16));
    4131              : }
    4132              : Datum
    4133            6 : to_hex64(PG_FUNCTION_ARGS)
    4134              : {
    4135            6 :     uint64      value = (uint64) PG_GETARG_INT64(0);
    4136              : 
    4137            6 :     PG_RETURN_TEXT_P(convert_to_base(value, 16));
    4138              : }
    4139              : 
    4140              : /*
    4141              :  * Return the size of a datum, possibly compressed
    4142              :  *
    4143              :  * Works on any data type
    4144              :  */
    4145              : Datum
    4146           61 : pg_column_size(PG_FUNCTION_ARGS)
    4147              : {
    4148           61 :     Datum       value = PG_GETARG_DATUM(0);
    4149              :     int32       result;
    4150              :     int         typlen;
    4151              : 
    4152              :     /* On first call, get the input type's typlen, and save at *fn_extra */
    4153           61 :     if (fcinfo->flinfo->fn_extra == NULL)
    4154              :     {
    4155              :         /* Lookup the datatype of the supplied argument */
    4156           61 :         Oid         argtypeid = get_fn_expr_argtype(fcinfo->flinfo, 0);
    4157              : 
    4158           61 :         typlen = get_typlen(argtypeid);
    4159           61 :         if (typlen == 0)        /* should not happen */
    4160            0 :             elog(ERROR, "cache lookup failed for type %u", argtypeid);
    4161              : 
    4162           61 :         fcinfo->flinfo->fn_extra = MemoryContextAlloc(fcinfo->flinfo->fn_mcxt,
    4163              :                                                       sizeof(int));
    4164           61 :         *((int *) fcinfo->flinfo->fn_extra) = typlen;
    4165              :     }
    4166              :     else
    4167            0 :         typlen = *((int *) fcinfo->flinfo->fn_extra);
    4168              : 
    4169           61 :     if (typlen == -1)
    4170              :     {
    4171              :         /* varlena type, possibly toasted */
    4172           61 :         result = toast_datum_size(value);
    4173              :     }
    4174            0 :     else if (typlen == -2)
    4175              :     {
    4176              :         /* cstring */
    4177            0 :         result = strlen(DatumGetCString(value)) + 1;
    4178              :     }
    4179              :     else
    4180              :     {
    4181              :         /* ordinary fixed-width type */
    4182            0 :         result = typlen;
    4183              :     }
    4184              : 
    4185           61 :     PG_RETURN_INT32(result);
    4186              : }
    4187              : 
    4188              : /*
    4189              :  * Return the compression method stored in the compressed attribute.  Return
    4190              :  * NULL for non varlena type or uncompressed data.
    4191              :  */
    4192              : Datum
    4193           96 : pg_column_compression(PG_FUNCTION_ARGS)
    4194              : {
    4195              :     int         typlen;
    4196              :     char       *result;
    4197              :     ToastCompressionId cmid;
    4198              : 
    4199              :     /* On first call, get the input type's typlen, and save at *fn_extra */
    4200           96 :     if (fcinfo->flinfo->fn_extra == NULL)
    4201              :     {
    4202              :         /* Lookup the datatype of the supplied argument */
    4203           78 :         Oid         argtypeid = get_fn_expr_argtype(fcinfo->flinfo, 0);
    4204              : 
    4205           78 :         typlen = get_typlen(argtypeid);
    4206           78 :         if (typlen == 0)        /* should not happen */
    4207            0 :             elog(ERROR, "cache lookup failed for type %u", argtypeid);
    4208              : 
    4209           78 :         fcinfo->flinfo->fn_extra = MemoryContextAlloc(fcinfo->flinfo->fn_mcxt,
    4210              :                                                       sizeof(int));
    4211           78 :         *((int *) fcinfo->flinfo->fn_extra) = typlen;
    4212              :     }
    4213              :     else
    4214           18 :         typlen = *((int *) fcinfo->flinfo->fn_extra);
    4215              : 
    4216           96 :     if (typlen != -1)
    4217            0 :         PG_RETURN_NULL();
    4218              : 
    4219              :     /* get the compression method id stored in the compressed varlena */
    4220           96 :     cmid = toast_get_compression_id((varlena *)
    4221           96 :                                     DatumGetPointer(PG_GETARG_DATUM(0)));
    4222           96 :     if (cmid == TOAST_INVALID_COMPRESSION_ID)
    4223           21 :         PG_RETURN_NULL();
    4224              : 
    4225              :     /* convert compression method id to compression method name */
    4226           75 :     switch (cmid)
    4227              :     {
    4228           42 :         case TOAST_PGLZ_COMPRESSION_ID:
    4229           42 :             result = "pglz";
    4230           42 :             break;
    4231           33 :         case TOAST_LZ4_COMPRESSION_ID:
    4232           33 :             result = "lz4";
    4233           33 :             break;
    4234            0 :         default:
    4235            0 :             elog(ERROR, "invalid compression method id %d", cmid);
    4236              :     }
    4237              : 
    4238           75 :     PG_RETURN_TEXT_P(cstring_to_text(result));
    4239              : }
    4240              : 
    4241              : /*
    4242              :  * Return the chunk_id of the on-disk TOASTed value.  Return NULL if the value
    4243              :  * is un-TOASTed or not on-disk.
    4244              :  */
    4245              : Datum
    4246           26 : pg_column_toast_chunk_id(PG_FUNCTION_ARGS)
    4247              : {
    4248              :     int         typlen;
    4249              :     varlena    *attr;
    4250              :     varatt_external toast_pointer;
    4251              : 
    4252              :     /* On first call, get the input type's typlen, and save at *fn_extra */
    4253           26 :     if (fcinfo->flinfo->fn_extra == NULL)
    4254              :     {
    4255              :         /* Lookup the datatype of the supplied argument */
    4256           20 :         Oid         argtypeid = get_fn_expr_argtype(fcinfo->flinfo, 0);
    4257              : 
    4258           20 :         typlen = get_typlen(argtypeid);
    4259           20 :         if (typlen == 0)        /* should not happen */
    4260            0 :             elog(ERROR, "cache lookup failed for type %u", argtypeid);
    4261              : 
    4262           20 :         fcinfo->flinfo->fn_extra = MemoryContextAlloc(fcinfo->flinfo->fn_mcxt,
    4263              :                                                       sizeof(int));
    4264           20 :         *((int *) fcinfo->flinfo->fn_extra) = typlen;
    4265              :     }
    4266              :     else
    4267            6 :         typlen = *((int *) fcinfo->flinfo->fn_extra);
    4268              : 
    4269           26 :     if (typlen != -1)
    4270            0 :         PG_RETURN_NULL();
    4271              : 
    4272           26 :     attr = (varlena *) DatumGetPointer(PG_GETARG_DATUM(0));
    4273              : 
    4274           26 :     if (!VARATT_IS_EXTERNAL_ONDISK(attr))
    4275            6 :         PG_RETURN_NULL();
    4276              : 
    4277           20 :     VARATT_EXTERNAL_GET_POINTER(toast_pointer, attr);
    4278              : 
    4279           20 :     PG_RETURN_OID(toast_pointer.va_valueid);
    4280              : }
    4281              : 
    4282              : /*
    4283              :  * string_agg - Concatenates values and returns string.
    4284              :  *
    4285              :  * Syntax: string_agg(value text, delimiter text) RETURNS text
    4286              :  *
    4287              :  * Note: Any NULL values are ignored. The first-call delimiter isn't
    4288              :  * actually used at all, and on subsequent calls the delimiter precedes
    4289              :  * the associated value.
    4290              :  */
    4291              : 
    4292              : /* subroutine to initialize state */
    4293              : static StringInfo
    4294         1216 : makeStringAggState(FunctionCallInfo fcinfo)
    4295              : {
    4296              :     StringInfo  state;
    4297              :     MemoryContext aggcontext;
    4298              :     MemoryContext oldcontext;
    4299              : 
    4300         1216 :     if (!AggCheckCallContext(fcinfo, &aggcontext))
    4301              :     {
    4302              :         /* cannot be called directly because of internal-type argument */
    4303            0 :         elog(ERROR, "string_agg_transfn called in non-aggregate context");
    4304              :     }
    4305              : 
    4306              :     /*
    4307              :      * Create state in aggregate context.  It'll stay there across subsequent
    4308              :      * calls.
    4309              :      */
    4310         1216 :     oldcontext = MemoryContextSwitchTo(aggcontext);
    4311         1216 :     state = makeStringInfo();
    4312         1216 :     MemoryContextSwitchTo(oldcontext);
    4313              : 
    4314         1216 :     return state;
    4315              : }
    4316              : 
    4317              : Datum
    4318       547535 : string_agg_transfn(PG_FUNCTION_ARGS)
    4319              : {
    4320              :     StringInfo  state;
    4321              : 
    4322       547535 :     state = PG_ARGISNULL(0) ? NULL : (StringInfo) PG_GETARG_POINTER(0);
    4323              : 
    4324              :     /* Append the value unless null, preceding it with the delimiter. */
    4325       547535 :     if (!PG_ARGISNULL(1))
    4326              :     {
    4327       540011 :         text       *value = PG_GETARG_TEXT_PP(1);
    4328       540011 :         bool        isfirst = false;
    4329              : 
    4330              :         /*
    4331              :          * You might think we can just throw away the first delimiter, however
    4332              :          * we must keep it as we may be a parallel worker doing partial
    4333              :          * aggregation building a state to send to the main process.  We need
    4334              :          * to keep the delimiter of every aggregation so that the combine
    4335              :          * function can properly join up the strings of two separately
    4336              :          * partially aggregated results.  The first delimiter is only stripped
    4337              :          * off in the final function.  To know how much to strip off the front
    4338              :          * of the string, we store the length of the first delimiter in the
    4339              :          * StringInfo's cursor field, which we don't otherwise need here.
    4340              :          */
    4341       540011 :         if (state == NULL)
    4342              :         {
    4343         1036 :             state = makeStringAggState(fcinfo);
    4344         1036 :             isfirst = true;
    4345              :         }
    4346              : 
    4347       540011 :         if (!PG_ARGISNULL(2))
    4348              :         {
    4349       540011 :             text       *delim = PG_GETARG_TEXT_PP(2);
    4350              : 
    4351       540011 :             appendStringInfoText(state, delim);
    4352       540011 :             if (isfirst)
    4353         1036 :                 state->cursor = VARSIZE_ANY_EXHDR(delim);
    4354              :         }
    4355              : 
    4356       540011 :         appendStringInfoText(state, value);
    4357              :     }
    4358              : 
    4359              :     /*
    4360              :      * The transition type for string_agg() is declared to be "internal",
    4361              :      * which is a pass-by-value type the same size as a pointer.
    4362              :      */
    4363       547535 :     if (state)
    4364       547490 :         PG_RETURN_POINTER(state);
    4365           45 :     PG_RETURN_NULL();
    4366              : }
    4367              : 
    4368              : /*
    4369              :  * string_agg_combine
    4370              :  *      Aggregate combine function for string_agg(text) and string_agg(bytea)
    4371              :  */
    4372              : Datum
    4373          120 : string_agg_combine(PG_FUNCTION_ARGS)
    4374              : {
    4375              :     StringInfo  state1;
    4376              :     StringInfo  state2;
    4377              :     MemoryContext agg_context;
    4378              : 
    4379          120 :     if (!AggCheckCallContext(fcinfo, &agg_context))
    4380            0 :         elog(ERROR, "aggregate function called in non-aggregate context");
    4381              : 
    4382          120 :     state1 = PG_ARGISNULL(0) ? NULL : (StringInfo) PG_GETARG_POINTER(0);
    4383          120 :     state2 = PG_ARGISNULL(1) ? NULL : (StringInfo) PG_GETARG_POINTER(1);
    4384              : 
    4385          120 :     if (state2 == NULL)
    4386              :     {
    4387              :         /*
    4388              :          * NULL state2 is easy, just return state1, which we know is already
    4389              :          * in the agg_context
    4390              :          */
    4391            0 :         if (state1 == NULL)
    4392            0 :             PG_RETURN_NULL();
    4393            0 :         PG_RETURN_POINTER(state1);
    4394              :     }
    4395              : 
    4396          120 :     if (state1 == NULL)
    4397              :     {
    4398              :         /* We must copy state2's data into the agg_context */
    4399              :         MemoryContext old_context;
    4400              : 
    4401           60 :         old_context = MemoryContextSwitchTo(agg_context);
    4402           60 :         state1 = makeStringAggState(fcinfo);
    4403           60 :         appendBinaryStringInfo(state1, state2->data, state2->len);
    4404           60 :         state1->cursor = state2->cursor;
    4405           60 :         MemoryContextSwitchTo(old_context);
    4406              :     }
    4407           60 :     else if (state2->len > 0)
    4408              :     {
    4409              :         /* Combine ... state1->cursor does not change in this case */
    4410           60 :         appendBinaryStringInfo(state1, state2->data, state2->len);
    4411              :     }
    4412              : 
    4413          120 :     PG_RETURN_POINTER(state1);
    4414              : }
    4415              : 
    4416              : /*
    4417              :  * string_agg_serialize
    4418              :  *      Aggregate serialize function for string_agg(text) and string_agg(bytea)
    4419              :  *
    4420              :  * This is strict, so we need not handle NULL input
    4421              :  */
    4422              : Datum
    4423          120 : string_agg_serialize(PG_FUNCTION_ARGS)
    4424              : {
    4425              :     StringInfo  state;
    4426              :     StringInfoData buf;
    4427              :     bytea      *result;
    4428              : 
    4429              :     /* cannot be called directly because of internal-type argument */
    4430              :     Assert(AggCheckCallContext(fcinfo, NULL));
    4431              : 
    4432          120 :     state = (StringInfo) PG_GETARG_POINTER(0);
    4433              : 
    4434          120 :     pq_begintypsend(&buf);
    4435              : 
    4436              :     /* cursor */
    4437          120 :     pq_sendint(&buf, state->cursor, 4);
    4438              : 
    4439              :     /* data */
    4440          120 :     pq_sendbytes(&buf, state->data, state->len);
    4441              : 
    4442          120 :     result = pq_endtypsend(&buf);
    4443              : 
    4444          120 :     PG_RETURN_BYTEA_P(result);
    4445              : }
    4446              : 
    4447              : /*
    4448              :  * string_agg_deserialize
    4449              :  *      Aggregate deserial function for string_agg(text) and string_agg(bytea)
    4450              :  *
    4451              :  * This is strict, so we need not handle NULL input
    4452              :  */
    4453              : Datum
    4454          120 : string_agg_deserialize(PG_FUNCTION_ARGS)
    4455              : {
    4456              :     bytea      *sstate;
    4457              :     StringInfo  result;
    4458              :     StringInfoData buf;
    4459              :     char       *data;
    4460              :     int         datalen;
    4461              : 
    4462              :     /* cannot be called directly because of internal-type argument */
    4463              :     Assert(AggCheckCallContext(fcinfo, NULL));
    4464              : 
    4465          120 :     sstate = PG_GETARG_BYTEA_PP(0);
    4466              : 
    4467              :     /*
    4468              :      * Initialize a StringInfo so that we can "receive" it using the standard
    4469              :      * recv-function infrastructure.
    4470              :      */
    4471          120 :     initReadOnlyStringInfo(&buf, VARDATA_ANY(sstate),
    4472          120 :                            VARSIZE_ANY_EXHDR(sstate));
    4473              : 
    4474          120 :     result = makeStringAggState(fcinfo);
    4475              : 
    4476              :     /* cursor */
    4477          120 :     result->cursor = pq_getmsgint(&buf, 4);
    4478              : 
    4479              :     /* data */
    4480          120 :     datalen = VARSIZE_ANY_EXHDR(sstate) - 4;
    4481          120 :     data = (char *) pq_getmsgbytes(&buf, datalen);
    4482          120 :     appendBinaryStringInfo(result, data, datalen);
    4483              : 
    4484          120 :     pq_getmsgend(&buf);
    4485              : 
    4486          120 :     PG_RETURN_POINTER(result);
    4487              : }
    4488              : 
    4489              : Datum
    4490         1048 : string_agg_finalfn(PG_FUNCTION_ARGS)
    4491              : {
    4492              :     StringInfo  state;
    4493              : 
    4494              :     /* cannot be called directly because of internal-type argument */
    4495              :     Assert(AggCheckCallContext(fcinfo, NULL));
    4496              : 
    4497         1048 :     state = PG_ARGISNULL(0) ? NULL : (StringInfo) PG_GETARG_POINTER(0);
    4498              : 
    4499         1048 :     if (state != NULL)
    4500              :     {
    4501              :         /* As per comment in transfn, strip data before the cursor position */
    4502         1006 :         PG_RETURN_TEXT_P(cstring_to_text_with_len(&state->data[state->cursor],
    4503              :                                                   state->len - state->cursor));
    4504              :     }
    4505              :     else
    4506           42 :         PG_RETURN_NULL();
    4507              : }
    4508              : 
    4509              : /*
    4510              :  * Prepare cache with fmgr info for the output functions of the datatypes of
    4511              :  * the arguments of a concat-like function, beginning with argument "argidx".
    4512              :  * (Arguments before that will have corresponding slots in the resulting
    4513              :  * FmgrInfo array, but we don't fill those slots.)
    4514              :  */
    4515              : static FmgrInfo *
    4516           53 : build_concat_foutcache(FunctionCallInfo fcinfo, int argidx)
    4517              : {
    4518              :     FmgrInfo   *foutcache;
    4519              :     int         i;
    4520              : 
    4521              :     /* We keep the info in fn_mcxt so it survives across calls */
    4522           53 :     foutcache = (FmgrInfo *) MemoryContextAlloc(fcinfo->flinfo->fn_mcxt,
    4523           53 :                                                 PG_NARGS() * sizeof(FmgrInfo));
    4524              : 
    4525          200 :     for (i = argidx; i < PG_NARGS(); i++)
    4526              :     {
    4527              :         Oid         valtype;
    4528              :         Oid         typOutput;
    4529              :         bool        typIsVarlena;
    4530              : 
    4531          147 :         valtype = get_fn_expr_argtype(fcinfo->flinfo, i);
    4532          147 :         if (!OidIsValid(valtype))
    4533            0 :             elog(ERROR, "could not determine data type of concat() input");
    4534              : 
    4535          147 :         getTypeOutputInfo(valtype, &typOutput, &typIsVarlena);
    4536          147 :         fmgr_info_cxt(typOutput, &foutcache[i], fcinfo->flinfo->fn_mcxt);
    4537              :     }
    4538              : 
    4539           53 :     fcinfo->flinfo->fn_extra = foutcache;
    4540              : 
    4541           53 :     return foutcache;
    4542              : }
    4543              : 
    4544              : /*
    4545              :  * Implementation of both concat() and concat_ws().
    4546              :  *
    4547              :  * sepstr is the separator string to place between values.
    4548              :  * argidx identifies the first argument to concatenate (counting from zero);
    4549              :  * note that this must be constant across any one series of calls.
    4550              :  *
    4551              :  * Returns NULL if result should be NULL, else text value.
    4552              :  */
    4553              : static text *
    4554          132 : concat_internal(const char *sepstr, int argidx,
    4555              :                 FunctionCallInfo fcinfo)
    4556              : {
    4557              :     text       *result;
    4558              :     StringInfoData str;
    4559              :     FmgrInfo   *foutcache;
    4560          132 :     bool        first_arg = true;
    4561              :     int         i;
    4562              : 
    4563              :     /*
    4564              :      * concat(VARIADIC some-array) is essentially equivalent to
    4565              :      * array_to_text(), ie concat the array elements with the given separator.
    4566              :      * So we just pass the case off to that code.
    4567              :      */
    4568          132 :     if (get_fn_expr_variadic(fcinfo->flinfo))
    4569              :     {
    4570              :         ArrayType  *arr;
    4571              : 
    4572              :         /* Should have just the one argument */
    4573              :         Assert(argidx == PG_NARGS() - 1);
    4574              : 
    4575              :         /* concat(VARIADIC NULL) is defined as NULL */
    4576           15 :         if (PG_ARGISNULL(argidx))
    4577            6 :             return NULL;
    4578              : 
    4579              :         /*
    4580              :          * Non-null argument had better be an array.  We assume that any call
    4581              :          * context that could let get_fn_expr_variadic return true will have
    4582              :          * checked that a VARIADIC-labeled parameter actually is an array.  So
    4583              :          * it should be okay to just Assert that it's an array rather than
    4584              :          * doing a full-fledged error check.
    4585              :          */
    4586              :         Assert(OidIsValid(get_base_element_type(get_fn_expr_argtype(fcinfo->flinfo, argidx))));
    4587              : 
    4588              :         /* OK, safe to fetch the array value */
    4589            9 :         arr = PG_GETARG_ARRAYTYPE_P(argidx);
    4590              : 
    4591              :         /*
    4592              :          * And serialize the array.  We tell array_to_text to ignore null
    4593              :          * elements, which matches the behavior of the loop below.
    4594              :          */
    4595            9 :         return array_to_text_internal(fcinfo, arr, sepstr, NULL);
    4596              :     }
    4597              : 
    4598              :     /* Normal case without explicit VARIADIC marker */
    4599          117 :     initStringInfo(&str);
    4600              : 
    4601              :     /* Get output function info, building it if first time through */
    4602          117 :     foutcache = (FmgrInfo *) fcinfo->flinfo->fn_extra;
    4603          117 :     if (foutcache == NULL)
    4604           53 :         foutcache = build_concat_foutcache(fcinfo, argidx);
    4605              : 
    4606          411 :     for (i = argidx; i < PG_NARGS(); i++)
    4607              :     {
    4608          294 :         if (!PG_ARGISNULL(i))
    4609              :         {
    4610          255 :             Datum       value = PG_GETARG_DATUM(i);
    4611              : 
    4612              :             /* add separator if appropriate */
    4613          255 :             if (first_arg)
    4614          114 :                 first_arg = false;
    4615              :             else
    4616          141 :                 appendStringInfoString(&str, sepstr);
    4617              : 
    4618              :             /* call the appropriate type output function, append the result */
    4619          255 :             appendStringInfoString(&str,
    4620          255 :                                    OutputFunctionCall(&foutcache[i], value));
    4621              :         }
    4622              :     }
    4623              : 
    4624          117 :     result = cstring_to_text_with_len(str.data, str.len);
    4625          117 :     pfree(str.data);
    4626              : 
    4627          117 :     return result;
    4628              : }
    4629              : 
    4630              : /*
    4631              :  * Concatenate all arguments. NULL arguments are ignored.
    4632              :  */
    4633              : Datum
    4634           93 : text_concat(PG_FUNCTION_ARGS)
    4635              : {
    4636              :     text       *result;
    4637              : 
    4638           93 :     result = concat_internal("", 0, fcinfo);
    4639           93 :     if (result == NULL)
    4640            3 :         PG_RETURN_NULL();
    4641           90 :     PG_RETURN_TEXT_P(result);
    4642              : }
    4643              : 
    4644              : /*
    4645              :  * Concatenate all but first argument value with separators. The first
    4646              :  * parameter is used as the separator. NULL arguments are ignored.
    4647              :  */
    4648              : Datum
    4649           42 : text_concat_ws(PG_FUNCTION_ARGS)
    4650              : {
    4651              :     char       *sep;
    4652              :     text       *result;
    4653              : 
    4654              :     /* return NULL when separator is NULL */
    4655           42 :     if (PG_ARGISNULL(0))
    4656            3 :         PG_RETURN_NULL();
    4657           39 :     sep = text_to_cstring(PG_GETARG_TEXT_PP(0));
    4658              : 
    4659           39 :     result = concat_internal(sep, 1, fcinfo);
    4660           39 :     if (result == NULL)
    4661            3 :         PG_RETURN_NULL();
    4662           36 :     PG_RETURN_TEXT_P(result);
    4663              : }
    4664              : 
    4665              : /*
    4666              :  * Return first n characters in the string. When n is negative,
    4667              :  * return all but last |n| characters.
    4668              :  */
    4669              : Datum
    4670         1074 : text_left(PG_FUNCTION_ARGS)
    4671              : {
    4672         1074 :     int         n = PG_GETARG_INT32(1);
    4673              : 
    4674         1074 :     if (n < 0)
    4675              :     {
    4676           15 :         text       *str = PG_GETARG_TEXT_PP(0);
    4677           15 :         const char *p = VARDATA_ANY(str);
    4678           15 :         int         len = VARSIZE_ANY_EXHDR(str);
    4679              :         int         rlen;
    4680              : 
    4681           15 :         n = pg_mbstrlen_with_len(p, len) + n;
    4682           15 :         rlen = pg_mbcharcliplen(p, len, n);
    4683           15 :         PG_RETURN_TEXT_P(cstring_to_text_with_len(p, rlen));
    4684              :     }
    4685              :     else
    4686         1059 :         PG_RETURN_TEXT_P(text_substring(PG_GETARG_DATUM(0), 1, n, false));
    4687              : }
    4688              : 
    4689              : /*
    4690              :  * Return last n characters in the string. When n is negative,
    4691              :  * return all but first |n| characters.
    4692              :  */
    4693              : Datum
    4694           33 : text_right(PG_FUNCTION_ARGS)
    4695              : {
    4696           33 :     text       *str = PG_GETARG_TEXT_PP(0);
    4697           33 :     const char *p = VARDATA_ANY(str);
    4698           33 :     int         len = VARSIZE_ANY_EXHDR(str);
    4699           33 :     int         n = PG_GETARG_INT32(1);
    4700              :     int         off;
    4701              : 
    4702           33 :     if (n < 0)
    4703           15 :         n = -n;
    4704              :     else
    4705           18 :         n = pg_mbstrlen_with_len(p, len) - n;
    4706           33 :     off = pg_mbcharcliplen(p, len, n);
    4707              : 
    4708           33 :     PG_RETURN_TEXT_P(cstring_to_text_with_len(p + off, len - off));
    4709              : }
    4710              : 
    4711              : /*
    4712              :  * Return reversed string
    4713              :  */
    4714              : Datum
    4715           21 : text_reverse(PG_FUNCTION_ARGS)
    4716              : {
    4717           21 :     text       *str = PG_GETARG_TEXT_PP(0);
    4718           21 :     const char *p = VARDATA_ANY(str);
    4719           21 :     int         len = VARSIZE_ANY_EXHDR(str);
    4720           21 :     const char *endp = p + len;
    4721              :     text       *result;
    4722              :     char       *dst;
    4723              : 
    4724           21 :     result = palloc(len + VARHDRSZ);
    4725           21 :     dst = (char *) VARDATA(result) + len;
    4726           21 :     SET_VARSIZE(result, len + VARHDRSZ);
    4727              : 
    4728           21 :     if (pg_database_encoding_max_length() > 1)
    4729              :     {
    4730              :         /* multibyte version */
    4731          162 :         while (p < endp)
    4732              :         {
    4733              :             int         sz;
    4734              : 
    4735          144 :             sz = pg_mblen_range(p, endp);
    4736          141 :             dst -= sz;
    4737          141 :             memcpy(dst, p, sz);
    4738          141 :             p += sz;
    4739              :         }
    4740              :     }
    4741              :     else
    4742              :     {
    4743              :         /* single byte version */
    4744            0 :         while (p < endp)
    4745            0 :             *(--dst) = *p++;
    4746              :     }
    4747              : 
    4748           18 :     PG_RETURN_TEXT_P(result);
    4749              : }
    4750              : 
    4751              : 
    4752              : /*
    4753              :  * Support macros for text_format()
    4754              :  */
    4755              : #define TEXT_FORMAT_FLAG_MINUS  0x0001  /* is minus flag present? */
    4756              : 
    4757              : #define ADVANCE_PARSE_POINTER(ptr,end_ptr) \
    4758              :     do { \
    4759              :         if (++(ptr) >= (end_ptr)) \
    4760              :             ereport(ERROR, \
    4761              :                     (errcode(ERRCODE_INVALID_PARAMETER_VALUE), \
    4762              :                      errmsg("unterminated format() type specifier"), \
    4763              :                      errhint("For a single \"%%\" use \"%%%%\"."))); \
    4764              :     } while (0)
    4765              : 
    4766              : /*
    4767              :  * Returns a formatted string
    4768              :  */
    4769              : Datum
    4770        16612 : text_format(PG_FUNCTION_ARGS)
    4771              : {
    4772              :     text       *fmt;
    4773              :     StringInfoData str;
    4774              :     const char *cp;
    4775              :     const char *start_ptr;
    4776              :     const char *end_ptr;
    4777              :     text       *result;
    4778              :     int         arg;
    4779              :     bool        funcvariadic;
    4780              :     int         nargs;
    4781        16612 :     Datum      *elements = NULL;
    4782        16612 :     bool       *nulls = NULL;
    4783        16612 :     Oid         element_type = InvalidOid;
    4784        16612 :     Oid         prev_type = InvalidOid;
    4785        16612 :     Oid         prev_width_type = InvalidOid;
    4786              :     FmgrInfo    typoutputfinfo;
    4787              :     FmgrInfo    typoutputinfo_width;
    4788              : 
    4789              :     /* When format string is null, immediately return null */
    4790        16612 :     if (PG_ARGISNULL(0))
    4791            3 :         PG_RETURN_NULL();
    4792              : 
    4793              :     /* If argument is marked VARIADIC, expand array into elements */
    4794        16609 :     if (get_fn_expr_variadic(fcinfo->flinfo))
    4795              :     {
    4796              :         ArrayType  *arr;
    4797              :         int16       elmlen;
    4798              :         bool        elmbyval;
    4799              :         char        elmalign;
    4800              :         int         nitems;
    4801              : 
    4802              :         /* Should have just the one argument */
    4803              :         Assert(PG_NARGS() == 2);
    4804              : 
    4805              :         /* If argument is NULL, we treat it as zero-length array */
    4806           24 :         if (PG_ARGISNULL(1))
    4807            3 :             nitems = 0;
    4808              :         else
    4809              :         {
    4810              :             /*
    4811              :              * Non-null argument had better be an array.  We assume that any
    4812              :              * call context that could let get_fn_expr_variadic return true
    4813              :              * will have checked that a VARIADIC-labeled parameter actually is
    4814              :              * an array.  So it should be okay to just Assert that it's an
    4815              :              * array rather than doing a full-fledged error check.
    4816              :              */
    4817              :             Assert(OidIsValid(get_base_element_type(get_fn_expr_argtype(fcinfo->flinfo, 1))));
    4818              : 
    4819              :             /* OK, safe to fetch the array value */
    4820           21 :             arr = PG_GETARG_ARRAYTYPE_P(1);
    4821              : 
    4822              :             /* Get info about array element type */
    4823           21 :             element_type = ARR_ELEMTYPE(arr);
    4824           21 :             get_typlenbyvalalign(element_type,
    4825              :                                  &elmlen, &elmbyval, &elmalign);
    4826              : 
    4827              :             /* Extract all array elements */
    4828           21 :             deconstruct_array(arr, element_type, elmlen, elmbyval, elmalign,
    4829              :                               &elements, &nulls, &nitems);
    4830              :         }
    4831              : 
    4832           24 :         nargs = nitems + 1;
    4833           24 :         funcvariadic = true;
    4834              :     }
    4835              :     else
    4836              :     {
    4837              :         /* Non-variadic case, we'll process the arguments individually */
    4838        16585 :         nargs = PG_NARGS();
    4839        16585 :         funcvariadic = false;
    4840              :     }
    4841              : 
    4842              :     /* Setup for main loop. */
    4843        16609 :     fmt = PG_GETARG_TEXT_PP(0);
    4844        16609 :     start_ptr = VARDATA_ANY(fmt);
    4845        16609 :     end_ptr = start_ptr + VARSIZE_ANY_EXHDR(fmt);
    4846        16609 :     initStringInfo(&str);
    4847        16609 :     arg = 1;                    /* next argument position to print */
    4848              : 
    4849              :     /* Scan format string, looking for conversion specifiers. */
    4850       506704 :     for (cp = start_ptr; cp < end_ptr; cp++)
    4851              :     {
    4852              :         int         argpos;
    4853              :         int         widthpos;
    4854              :         int         flags;
    4855              :         int         width;
    4856              :         Datum       value;
    4857              :         bool        isNull;
    4858              :         Oid         typid;
    4859              : 
    4860              :         /*
    4861              :          * If it's not the start of a conversion specifier, just copy it to
    4862              :          * the output buffer.
    4863              :          */
    4864       490125 :         if (*cp != '%')
    4865              :         {
    4866       457187 :             appendStringInfoCharMacro(&str, *cp);
    4867       457196 :             continue;
    4868              :         }
    4869              : 
    4870        32938 :         ADVANCE_PARSE_POINTER(cp, end_ptr);
    4871              : 
    4872              :         /* Easy case: %% outputs a single % */
    4873        32938 :         if (*cp == '%')
    4874              :         {
    4875            9 :             appendStringInfoCharMacro(&str, *cp);
    4876            9 :             continue;
    4877              :         }
    4878              : 
    4879              :         /* Parse the optional portions of the format specifier */
    4880        32929 :         cp = text_format_parse_format(cp, end_ptr,
    4881              :                                       &argpos, &widthpos,
    4882              :                                       &flags, &width);
    4883              : 
    4884              :         /*
    4885              :          * Next we should see the main conversion specifier.  Whether or not
    4886              :          * an argument position was present, it's known that at least one
    4887              :          * character remains in the string at this point.  Experience suggests
    4888              :          * that it's worth checking that that character is one of the expected
    4889              :          * ones before we try to fetch arguments, so as to produce the least
    4890              :          * confusing response to a mis-formatted specifier.
    4891              :          */
    4892        32917 :         if (strchr("sIL", *cp) == NULL)
    4893            3 :             ereport(ERROR,
    4894              :                     (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
    4895              :                      errmsg("unrecognized format() type specifier \"%.*s\"",
    4896              :                             pg_mblen_range(cp, end_ptr), cp),
    4897              :                      errhint("For a single \"%%\" use \"%%%%\".")));
    4898              : 
    4899              :         /* If indirect width was specified, get its value */
    4900        32914 :         if (widthpos >= 0)
    4901              :         {
    4902              :             /* Collect the specified or next argument position */
    4903           21 :             if (widthpos > 0)
    4904           18 :                 arg = widthpos;
    4905           21 :             if (arg >= nargs)
    4906            0 :                 ereport(ERROR,
    4907              :                         (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
    4908              :                          errmsg("too few arguments for format()")));
    4909              : 
    4910              :             /* Get the value and type of the selected argument */
    4911           21 :             if (!funcvariadic)
    4912              :             {
    4913           21 :                 value = PG_GETARG_DATUM(arg);
    4914           21 :                 isNull = PG_ARGISNULL(arg);
    4915           21 :                 typid = get_fn_expr_argtype(fcinfo->flinfo, arg);
    4916              :             }
    4917              :             else
    4918              :             {
    4919            0 :                 value = elements[arg - 1];
    4920            0 :                 isNull = nulls[arg - 1];
    4921            0 :                 typid = element_type;
    4922              :             }
    4923           21 :             if (!OidIsValid(typid))
    4924            0 :                 elog(ERROR, "could not determine data type of format() input");
    4925              : 
    4926           21 :             arg++;
    4927              : 
    4928              :             /* We can treat NULL width the same as zero */
    4929           21 :             if (isNull)
    4930            3 :                 width = 0;
    4931           18 :             else if (typid == INT4OID)
    4932           18 :                 width = DatumGetInt32(value);
    4933            0 :             else if (typid == INT2OID)
    4934            0 :                 width = DatumGetInt16(value);
    4935              :             else
    4936              :             {
    4937              :                 /* For less-usual datatypes, convert to text then to int */
    4938              :                 char       *str;
    4939              : 
    4940            0 :                 if (typid != prev_width_type)
    4941              :                 {
    4942              :                     Oid         typoutputfunc;
    4943              :                     bool        typIsVarlena;
    4944              : 
    4945            0 :                     getTypeOutputInfo(typid, &typoutputfunc, &typIsVarlena);
    4946            0 :                     fmgr_info(typoutputfunc, &typoutputinfo_width);
    4947            0 :                     prev_width_type = typid;
    4948              :                 }
    4949              : 
    4950            0 :                 str = OutputFunctionCall(&typoutputinfo_width, value);
    4951              : 
    4952              :                 /* pg_strtoint32 will complain about bad data or overflow */
    4953            0 :                 width = pg_strtoint32(str);
    4954              : 
    4955            0 :                 pfree(str);
    4956              :             }
    4957              :         }
    4958              : 
    4959              :         /* Collect the specified or next argument position */
    4960        32914 :         if (argpos > 0)
    4961           66 :             arg = argpos;
    4962        32914 :         if (arg >= nargs)
    4963           12 :             ereport(ERROR,
    4964              :                     (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
    4965              :                      errmsg("too few arguments for format()")));
    4966              : 
    4967              :         /* Get the value and type of the selected argument */
    4968        32902 :         if (!funcvariadic)
    4969              :         {
    4970        32266 :             value = PG_GETARG_DATUM(arg);
    4971        32266 :             isNull = PG_ARGISNULL(arg);
    4972        32266 :             typid = get_fn_expr_argtype(fcinfo->flinfo, arg);
    4973              :         }
    4974              :         else
    4975              :         {
    4976          636 :             value = elements[arg - 1];
    4977          636 :             isNull = nulls[arg - 1];
    4978          636 :             typid = element_type;
    4979              :         }
    4980        32902 :         if (!OidIsValid(typid))
    4981            0 :             elog(ERROR, "could not determine data type of format() input");
    4982              : 
    4983        32902 :         arg++;
    4984              : 
    4985              :         /*
    4986              :          * Get the appropriate typOutput function, reusing previous one if
    4987              :          * same type as previous argument.  That's particularly useful in the
    4988              :          * variadic-array case, but often saves work even for ordinary calls.
    4989              :          */
    4990        32902 :         if (typid != prev_type)
    4991              :         {
    4992              :             Oid         typoutputfunc;
    4993              :             bool        typIsVarlena;
    4994              : 
    4995        17143 :             getTypeOutputInfo(typid, &typoutputfunc, &typIsVarlena);
    4996        17143 :             fmgr_info(typoutputfunc, &typoutputfinfo);
    4997        17143 :             prev_type = typid;
    4998              :         }
    4999              : 
    5000              :         /*
    5001              :          * And now we can format the value.
    5002              :          */
    5003        32902 :         switch (*cp)
    5004              :         {
    5005        32902 :             case 's':
    5006              :             case 'I':
    5007              :             case 'L':
    5008        32902 :                 text_format_string_conversion(&str, *cp, &typoutputfinfo,
    5009              :                                               value, isNull,
    5010              :                                               flags, width);
    5011        32899 :                 break;
    5012            0 :             default:
    5013              :                 /* should not get here, because of previous check */
    5014            0 :                 ereport(ERROR,
    5015              :                         (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
    5016              :                          errmsg("unrecognized format() type specifier \"%.*s\"",
    5017              :                                 pg_mblen_range(cp, end_ptr), cp),
    5018              :                          errhint("For a single \"%%\" use \"%%%%\".")));
    5019              :                 break;
    5020              :         }
    5021              :     }
    5022              : 
    5023              :     /* Don't need deconstruct_array results anymore. */
    5024        16579 :     if (elements != NULL)
    5025           21 :         pfree(elements);
    5026        16579 :     if (nulls != NULL)
    5027           21 :         pfree(nulls);
    5028              : 
    5029              :     /* Generate results. */
    5030        16579 :     result = cstring_to_text_with_len(str.data, str.len);
    5031        16579 :     pfree(str.data);
    5032              : 
    5033        16579 :     PG_RETURN_TEXT_P(result);
    5034              : }
    5035              : 
    5036              : /*
    5037              :  * Parse contiguous digits as a decimal number.
    5038              :  *
    5039              :  * Returns true if some digits could be parsed.
    5040              :  * The value is returned into *value, and *ptr is advanced to the next
    5041              :  * character to be parsed.
    5042              :  *
    5043              :  * Note parsing invariant: at least one character is known available before
    5044              :  * string end (end_ptr) at entry, and this is still true at exit.
    5045              :  */
    5046              : static bool
    5047        65840 : text_format_parse_digits(const char **ptr, const char *end_ptr, int *value)
    5048              : {
    5049        65840 :     bool        found = false;
    5050        65840 :     const char *cp = *ptr;
    5051        65840 :     int         val = 0;
    5052              : 
    5053        65996 :     while (*cp >= '0' && *cp <= '9')
    5054              :     {
    5055          159 :         int8        digit = (*cp - '0');
    5056              : 
    5057          159 :         if (unlikely(pg_mul_s32_overflow(val, 10, &val)) ||
    5058          159 :             unlikely(pg_add_s32_overflow(val, digit, &val)))
    5059            0 :             ereport(ERROR,
    5060              :                     (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
    5061              :                      errmsg("number is out of range")));
    5062          159 :         ADVANCE_PARSE_POINTER(cp, end_ptr);
    5063          156 :         found = true;
    5064              :     }
    5065              : 
    5066        65837 :     *ptr = cp;
    5067        65837 :     *value = val;
    5068              : 
    5069        65837 :     return found;
    5070              : }
    5071              : 
    5072              : /*
    5073              :  * Parse a format specifier (generally following the SUS printf spec).
    5074              :  *
    5075              :  * We have already advanced over the initial '%', and we are looking for
    5076              :  * [argpos][flags][width]type (but the type character is not consumed here).
    5077              :  *
    5078              :  * Inputs are start_ptr (the position after '%') and end_ptr (string end + 1).
    5079              :  * Output parameters:
    5080              :  *  argpos: argument position for value to be printed.  -1 means unspecified.
    5081              :  *  widthpos: argument position for width.  Zero means the argument position
    5082              :  *          was unspecified (ie, take the next arg) and -1 means no width
    5083              :  *          argument (width was omitted or specified as a constant).
    5084              :  *  flags: bitmask of flags.
    5085              :  *  width: directly-specified width value.  Zero means the width was omitted
    5086              :  *          (note it's not necessary to distinguish this case from an explicit
    5087              :  *          zero width value).
    5088              :  *
    5089              :  * The function result is the next character position to be parsed, ie, the
    5090              :  * location where the type character is/should be.
    5091              :  *
    5092              :  * Note parsing invariant: at least one character is known available before
    5093              :  * string end (end_ptr) at entry, and this is still true at exit.
    5094              :  */
    5095              : static const char *
    5096        32929 : text_format_parse_format(const char *start_ptr, const char *end_ptr,
    5097              :                          int *argpos, int *widthpos,
    5098              :                          int *flags, int *width)
    5099              : {
    5100        32929 :     const char *cp = start_ptr;
    5101              :     int         n;
    5102              : 
    5103              :     /* set defaults for output parameters */
    5104        32929 :     *argpos = -1;
    5105        32929 :     *widthpos = -1;
    5106        32929 :     *flags = 0;
    5107        32929 :     *width = 0;
    5108              : 
    5109              :     /* try to identify first number */
    5110        32929 :     if (text_format_parse_digits(&cp, end_ptr, &n))
    5111              :     {
    5112           87 :         if (*cp != '$')
    5113              :         {
    5114              :             /* Must be just a width and a type, so we're done */
    5115           12 :             *width = n;
    5116           12 :             return cp;
    5117              :         }
    5118              :         /* The number was argument position */
    5119           75 :         *argpos = n;
    5120              :         /* Explicit 0 for argument index is immediately refused */
    5121           75 :         if (n == 0)
    5122            3 :             ereport(ERROR,
    5123              :                     (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
    5124              :                      errmsg("format specifies argument 0, but arguments are numbered from 1")));
    5125           72 :         ADVANCE_PARSE_POINTER(cp, end_ptr);
    5126              :     }
    5127              : 
    5128              :     /* Handle flags (only minus is supported now) */
    5129        32926 :     while (*cp == '-')
    5130              :     {
    5131           15 :         *flags |= TEXT_FORMAT_FLAG_MINUS;
    5132           15 :         ADVANCE_PARSE_POINTER(cp, end_ptr);
    5133              :     }
    5134              : 
    5135        32911 :     if (*cp == '*')
    5136              :     {
    5137              :         /* Handle indirect width */
    5138           24 :         ADVANCE_PARSE_POINTER(cp, end_ptr);
    5139           24 :         if (text_format_parse_digits(&cp, end_ptr, &n))
    5140              :         {
    5141              :             /* number in this position must be closed by $ */
    5142           21 :             if (*cp != '$')
    5143            0 :                 ereport(ERROR,
    5144              :                         (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
    5145              :                          errmsg("width argument position must be ended by \"$\"")));
    5146              :             /* The number was width argument position */
    5147           21 :             *widthpos = n;
    5148              :             /* Explicit 0 for argument index is immediately refused */
    5149           21 :             if (n == 0)
    5150            3 :                 ereport(ERROR,
    5151              :                         (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
    5152              :                          errmsg("format specifies argument 0, but arguments are numbered from 1")));
    5153           18 :             ADVANCE_PARSE_POINTER(cp, end_ptr);
    5154              :         }
    5155              :         else
    5156            3 :             *widthpos = 0;      /* width's argument position is unspecified */
    5157              :     }
    5158              :     else
    5159              :     {
    5160              :         /* Check for direct width specification */
    5161        32887 :         if (text_format_parse_digits(&cp, end_ptr, &n))
    5162           15 :             *width = n;
    5163              :     }
    5164              : 
    5165              :     /* cp should now be pointing at type character */
    5166        32905 :     return cp;
    5167              : }
    5168              : 
    5169              : /*
    5170              :  * Format a %s, %I, or %L conversion
    5171              :  */
    5172              : static void
    5173        32902 : text_format_string_conversion(StringInfo buf, char conversion,
    5174              :                               FmgrInfo *typOutputInfo,
    5175              :                               Datum value, bool isNull,
    5176              :                               int flags, int width)
    5177              : {
    5178              :     char       *str;
    5179              : 
    5180              :     /* Handle NULL arguments before trying to stringify the value. */
    5181        32902 :     if (isNull)
    5182              :     {
    5183          171 :         if (conversion == 's')
    5184          135 :             text_format_append_string(buf, "", flags, width);
    5185           36 :         else if (conversion == 'L')
    5186           33 :             text_format_append_string(buf, "NULL", flags, width);
    5187            3 :         else if (conversion == 'I')
    5188            3 :             ereport(ERROR,
    5189              :                     (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
    5190              :                      errmsg("null values cannot be formatted as an SQL identifier")));
    5191          168 :         return;
    5192              :     }
    5193              : 
    5194              :     /* Stringify. */
    5195        32731 :     str = OutputFunctionCall(typOutputInfo, value);
    5196              : 
    5197              :     /* Escape. */
    5198        32731 :     if (conversion == 'I')
    5199              :     {
    5200              :         /* quote_identifier may or may not allocate a new string. */
    5201         2453 :         text_format_append_string(buf, quote_identifier(str), flags, width);
    5202              :     }
    5203        30278 :     else if (conversion == 'L')
    5204              :     {
    5205         1626 :         char       *qstr = quote_literal_cstr(str);
    5206              : 
    5207         1626 :         text_format_append_string(buf, qstr, flags, width);
    5208              :         /* quote_literal_cstr() always allocates a new string */
    5209         1626 :         pfree(qstr);
    5210              :     }
    5211              :     else
    5212        28652 :         text_format_append_string(buf, str, flags, width);
    5213              : 
    5214              :     /* Cleanup. */
    5215        32731 :     pfree(str);
    5216              : }
    5217              : 
    5218              : /*
    5219              :  * Append str to buf, padding as directed by flags/width
    5220              :  */
    5221              : static void
    5222        32899 : text_format_append_string(StringInfo buf, const char *str,
    5223              :                           int flags, int width)
    5224              : {
    5225        32899 :     bool        align_to_left = false;
    5226              :     int         len;
    5227              : 
    5228              :     /* fast path for typical easy case */
    5229        32899 :     if (width == 0)
    5230              :     {
    5231        32857 :         appendStringInfoString(buf, str);
    5232        32857 :         return;
    5233              :     }
    5234              : 
    5235           42 :     if (width < 0)
    5236              :     {
    5237              :         /* Negative width: implicit '-' flag, then take absolute value */
    5238            3 :         align_to_left = true;
    5239              :         /* -INT_MIN is undefined */
    5240            3 :         if (width <= INT_MIN)
    5241            0 :             ereport(ERROR,
    5242              :                     (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
    5243              :                      errmsg("number is out of range")));
    5244            3 :         width = -width;
    5245              :     }
    5246           39 :     else if (flags & TEXT_FORMAT_FLAG_MINUS)
    5247           12 :         align_to_left = true;
    5248              : 
    5249           42 :     len = pg_mbstrlen(str);
    5250           42 :     if (align_to_left)
    5251              :     {
    5252              :         /* left justify */
    5253           15 :         appendStringInfoString(buf, str);
    5254           15 :         if (len < width)
    5255           15 :             appendStringInfoSpaces(buf, width - len);
    5256              :     }
    5257              :     else
    5258              :     {
    5259              :         /* right justify */
    5260           27 :         if (len < width)
    5261           27 :             appendStringInfoSpaces(buf, width - len);
    5262           27 :         appendStringInfoString(buf, str);
    5263              :     }
    5264              : }
    5265              : 
    5266              : /*
    5267              :  * text_format_nv - nonvariadic wrapper for text_format function.
    5268              :  *
    5269              :  * note: this wrapper is necessary to pass the sanity check in opr_sanity,
    5270              :  * which checks that all built-in functions that share the implementing C
    5271              :  * function take the same number of arguments.
    5272              :  */
    5273              : Datum
    5274         1905 : text_format_nv(PG_FUNCTION_ARGS)
    5275              : {
    5276         1905 :     return text_format(fcinfo);
    5277              : }
    5278              : 
    5279              : /*
    5280              :  * Helper function for Levenshtein distance functions. Faster than memcmp(),
    5281              :  * for this use case.
    5282              :  */
    5283              : static inline bool
    5284            0 : rest_of_char_same(const char *s1, const char *s2, int len)
    5285              : {
    5286            0 :     while (len > 0)
    5287              :     {
    5288            0 :         len--;
    5289            0 :         if (s1[len] != s2[len])
    5290            0 :             return false;
    5291              :     }
    5292            0 :     return true;
    5293              : }
    5294              : 
    5295              : /* Expand each Levenshtein distance variant */
    5296              : #include "levenshtein.c"
    5297              : #define LEVENSHTEIN_LESS_EQUAL
    5298              : #include "levenshtein.c"
    5299              : 
    5300              : 
    5301              : /*
    5302              :  * The following *ClosestMatch() functions can be used to determine whether a
    5303              :  * user-provided string resembles any known valid values, which is useful for
    5304              :  * providing hints in log messages, among other things.  Use these functions
    5305              :  * like so:
    5306              :  *
    5307              :  *      initClosestMatch(&state, source_string, max_distance);
    5308              :  *
    5309              :  *      for (int i = 0; i < num_valid_strings; i++)
    5310              :  *          updateClosestMatch(&state, valid_strings[i]);
    5311              :  *
    5312              :  *      closestMatch = getClosestMatch(&state);
    5313              :  */
    5314              : 
    5315              : /*
    5316              :  * Initialize the given state with the source string and maximum Levenshtein
    5317              :  * distance to consider.
    5318              :  */
    5319              : void
    5320           39 : initClosestMatch(ClosestMatchState *state, const char *source, int max_d)
    5321              : {
    5322              :     Assert(state);
    5323              :     Assert(max_d >= 0);
    5324              : 
    5325           39 :     state->source = source;
    5326           39 :     state->min_d = -1;
    5327           39 :     state->max_d = max_d;
    5328           39 :     state->match = NULL;
    5329           39 : }
    5330              : 
    5331              : /*
    5332              :  * If the candidate string is a closer match than the current one saved (or
    5333              :  * there is no match saved), save it as the closest match.
    5334              :  *
    5335              :  * If the source or candidate string is NULL, empty, or too long, this function
    5336              :  * takes no action.  Likewise, if the Levenshtein distance exceeds the maximum
    5337              :  * allowed or more than half the characters are different, no action is taken.
    5338              :  */
    5339              : void
    5340          402 : updateClosestMatch(ClosestMatchState *state, const char *candidate)
    5341              : {
    5342              :     int         dist;
    5343              : 
    5344              :     Assert(state);
    5345              : 
    5346          402 :     if (state->source == NULL || state->source[0] == '\0' ||
    5347          402 :         candidate == NULL || candidate[0] == '\0')
    5348            0 :         return;
    5349              : 
    5350              :     /*
    5351              :      * To avoid ERROR-ing, we check the lengths here instead of setting
    5352              :      * 'trusted' to false in the call to varstr_levenshtein_less_equal().
    5353              :      */
    5354          402 :     if (strlen(state->source) > MAX_LEVENSHTEIN_STRLEN ||
    5355          402 :         strlen(candidate) > MAX_LEVENSHTEIN_STRLEN)
    5356            0 :         return;
    5357              : 
    5358          402 :     dist = varstr_levenshtein_less_equal(state->source, strlen(state->source),
    5359          402 :                                          candidate, strlen(candidate), 1, 1, 1,
    5360              :                                          state->max_d, true);
    5361          402 :     if (dist <= state->max_d &&
    5362           31 :         dist <= strlen(state->source) / 2 &&
    5363            7 :         (state->min_d == -1 || dist < state->min_d))
    5364              :     {
    5365            7 :         state->min_d = dist;
    5366            7 :         state->match = candidate;
    5367              :     }
    5368              : }
    5369              : 
    5370              : /*
    5371              :  * Return the closest match.  If no suitable candidates were provided via
    5372              :  * updateClosestMatch(), return NULL.
    5373              :  */
    5374              : const char *
    5375           39 : getClosestMatch(ClosestMatchState *state)
    5376              : {
    5377              :     Assert(state);
    5378              : 
    5379           39 :     return state->match;
    5380              : }
    5381              : 
    5382              : 
    5383              : /*
    5384              :  * Unicode support
    5385              :  */
    5386              : 
    5387              : static UnicodeNormalizationForm
    5388          105 : unicode_norm_form_from_string(const char *formstr)
    5389              : {
    5390          105 :     UnicodeNormalizationForm form = -1;
    5391              : 
    5392              :     /*
    5393              :      * Might as well check this while we're here.
    5394              :      */
    5395          105 :     if (GetDatabaseEncoding() != PG_UTF8)
    5396            0 :         ereport(ERROR,
    5397              :                 (errcode(ERRCODE_SYNTAX_ERROR),
    5398              :                  errmsg("Unicode normalization can only be performed if server encoding is UTF8")));
    5399              : 
    5400          105 :     if (pg_strcasecmp(formstr, "NFC") == 0)
    5401           33 :         form = UNICODE_NFC;
    5402           72 :     else if (pg_strcasecmp(formstr, "NFD") == 0)
    5403           30 :         form = UNICODE_NFD;
    5404           42 :     else if (pg_strcasecmp(formstr, "NFKC") == 0)
    5405           18 :         form = UNICODE_NFKC;
    5406           24 :     else if (pg_strcasecmp(formstr, "NFKD") == 0)
    5407           18 :         form = UNICODE_NFKD;
    5408              :     else
    5409            6 :         ereport(ERROR,
    5410              :                 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
    5411              :                  errmsg("invalid normalization form: %s", formstr)));
    5412              : 
    5413           99 :     return form;
    5414              : }
    5415              : 
    5416              : /*
    5417              :  * Returns version of Unicode used by Postgres in "major.minor" format (the
    5418              :  * same format as the Unicode version reported by ICU). The third component
    5419              :  * ("update version") never involves additions to the character repertoire and
    5420              :  * is unimportant for most purposes.
    5421              :  *
    5422              :  * See: https://unicode.org/versions/
    5423              :  */
    5424              : Datum
    5425           17 : unicode_version(PG_FUNCTION_ARGS)
    5426              : {
    5427           17 :     PG_RETURN_TEXT_P(cstring_to_text(PG_UNICODE_VERSION));
    5428              : }
    5429              : 
    5430              : /*
    5431              :  * Returns version of Unicode used by ICU, if enabled; otherwise NULL.
    5432              :  */
    5433              : Datum
    5434            1 : icu_unicode_version(PG_FUNCTION_ARGS)
    5435              : {
    5436            1 :     const char *version = pg_icu_unicode_version();
    5437              : 
    5438            1 :     if (version)
    5439            1 :         PG_RETURN_TEXT_P(cstring_to_text(version));
    5440              :     else
    5441            0 :         PG_RETURN_NULL();
    5442              : }
    5443              : 
    5444              : /*
    5445              :  * Check whether the string contains only assigned Unicode code
    5446              :  * points. Requires that the database encoding is UTF-8.
    5447              :  */
    5448              : Datum
    5449            6 : unicode_assigned(PG_FUNCTION_ARGS)
    5450              : {
    5451            6 :     text       *input = PG_GETARG_TEXT_PP(0);
    5452              :     unsigned char *p;
    5453              :     int         size;
    5454              : 
    5455            6 :     if (GetDatabaseEncoding() != PG_UTF8)
    5456            0 :         ereport(ERROR,
    5457              :                 (errmsg("Unicode categorization can only be performed if server encoding is UTF8")));
    5458              : 
    5459              :     /* convert to char32_t */
    5460            6 :     size = pg_mbstrlen_with_len(VARDATA_ANY(input), VARSIZE_ANY_EXHDR(input));
    5461            6 :     p = (unsigned char *) VARDATA_ANY(input);
    5462           24 :     for (int i = 0; i < size; i++)
    5463              :     {
    5464           21 :         char32_t    uchar = utf8_to_unicode(p);
    5465           21 :         int         category = unicode_category(uchar);
    5466              : 
    5467           21 :         if (category == PG_U_UNASSIGNED)
    5468            3 :             PG_RETURN_BOOL(false);
    5469              : 
    5470           18 :         p += pg_utf_mblen(p);
    5471              :     }
    5472              : 
    5473            3 :     PG_RETURN_BOOL(true);
    5474              : }
    5475              : 
    5476              : Datum
    5477           36 : unicode_normalize_func(PG_FUNCTION_ARGS)
    5478              : {
    5479           36 :     text       *input = PG_GETARG_TEXT_PP(0);
    5480           36 :     char       *formstr = text_to_cstring(PG_GETARG_TEXT_PP(1));
    5481              :     UnicodeNormalizationForm form;
    5482              :     int         size;
    5483              :     char32_t   *input_chars;
    5484              :     char32_t   *output_chars;
    5485              :     unsigned char *p;
    5486              :     text       *result;
    5487              :     int         i;
    5488              : 
    5489           36 :     form = unicode_norm_form_from_string(formstr);
    5490              : 
    5491              :     /* convert to char32_t */
    5492           33 :     size = pg_mbstrlen_with_len(VARDATA_ANY(input), VARSIZE_ANY_EXHDR(input));
    5493           33 :     input_chars = palloc((size + 1) * sizeof(char32_t));
    5494           33 :     p = (unsigned char *) VARDATA_ANY(input);
    5495          144 :     for (i = 0; i < size; i++)
    5496              :     {
    5497          111 :         input_chars[i] = utf8_to_unicode(p);
    5498          111 :         p += pg_utf_mblen(p);
    5499              :     }
    5500           33 :     input_chars[i] = (char32_t) '\0';
    5501              :     Assert((char *) p == VARDATA_ANY(input) + VARSIZE_ANY_EXHDR(input));
    5502              : 
    5503              :     /* action */
    5504           33 :     output_chars = unicode_normalize(form, input_chars);
    5505              : 
    5506              :     /* convert back to UTF-8 string */
    5507           33 :     size = 0;
    5508          153 :     for (char32_t *wp = output_chars; *wp; wp++)
    5509              :     {
    5510              :         unsigned char buf[4];
    5511              : 
    5512          120 :         unicode_to_utf8(*wp, buf);
    5513          120 :         size += pg_utf_mblen(buf);
    5514              :     }
    5515              : 
    5516           33 :     result = palloc(size + VARHDRSZ);
    5517           33 :     SET_VARSIZE(result, size + VARHDRSZ);
    5518              : 
    5519           33 :     p = (unsigned char *) VARDATA_ANY(result);
    5520          153 :     for (char32_t *wp = output_chars; *wp; wp++)
    5521              :     {
    5522          120 :         unicode_to_utf8(*wp, p);
    5523          120 :         p += pg_utf_mblen(p);
    5524              :     }
    5525              :     Assert((char *) p == (char *) result + size + VARHDRSZ);
    5526              : 
    5527           33 :     PG_RETURN_TEXT_P(result);
    5528              : }
    5529              : 
    5530              : /*
    5531              :  * Check whether the string is in the specified Unicode normalization form.
    5532              :  *
    5533              :  * This is done by converting the string to the specified normal form and then
    5534              :  * comparing that to the original string.  To speed that up, we also apply the
    5535              :  * "quick check" algorithm specified in UAX #15, which can give a yes or no
    5536              :  * answer for many strings by just scanning the string once.
    5537              :  *
    5538              :  * This function should generally be optimized for the case where the string
    5539              :  * is in fact normalized.  In that case, we'll end up looking at the entire
    5540              :  * string, so it's probably not worth doing any incremental conversion etc.
    5541              :  */
    5542              : Datum
    5543           69 : unicode_is_normalized(PG_FUNCTION_ARGS)
    5544              : {
    5545           69 :     text       *input = PG_GETARG_TEXT_PP(0);
    5546           69 :     char       *formstr = text_to_cstring(PG_GETARG_TEXT_PP(1));
    5547              :     UnicodeNormalizationForm form;
    5548              :     int         size;
    5549              :     char32_t   *input_chars;
    5550              :     char32_t   *output_chars;
    5551              :     unsigned char *p;
    5552              :     int         i;
    5553              :     UnicodeNormalizationQC quickcheck;
    5554              :     int         output_size;
    5555              :     bool        result;
    5556              : 
    5557           69 :     form = unicode_norm_form_from_string(formstr);
    5558              : 
    5559              :     /* convert to char32_t */
    5560           66 :     size = pg_mbstrlen_with_len(VARDATA_ANY(input), VARSIZE_ANY_EXHDR(input));
    5561           66 :     input_chars = palloc((size + 1) * sizeof(char32_t));
    5562           66 :     p = (unsigned char *) VARDATA_ANY(input);
    5563          252 :     for (i = 0; i < size; i++)
    5564              :     {
    5565          186 :         input_chars[i] = utf8_to_unicode(p);
    5566          186 :         p += pg_utf_mblen(p);
    5567              :     }
    5568           66 :     input_chars[i] = (char32_t) '\0';
    5569              :     Assert((char *) p == VARDATA_ANY(input) + VARSIZE_ANY_EXHDR(input));
    5570              : 
    5571              :     /* quick check (see UAX #15) */
    5572           66 :     quickcheck = unicode_is_normalized_quickcheck(form, input_chars);
    5573           66 :     if (quickcheck == UNICODE_NORM_QC_YES)
    5574           21 :         PG_RETURN_BOOL(true);
    5575           45 :     else if (quickcheck == UNICODE_NORM_QC_NO)
    5576            6 :         PG_RETURN_BOOL(false);
    5577              : 
    5578              :     /* normalize and compare with original */
    5579           39 :     output_chars = unicode_normalize(form, input_chars);
    5580              : 
    5581           39 :     output_size = 0;
    5582          162 :     for (char32_t *wp = output_chars; *wp; wp++)
    5583          123 :         output_size++;
    5584              : 
    5585           57 :     result = (size == output_size) &&
    5586           18 :         (memcmp(input_chars, output_chars, size * sizeof(char32_t)) == 0);
    5587              : 
    5588           39 :     PG_RETURN_BOOL(result);
    5589              : }
    5590              : 
    5591              : /*
    5592              :  * Check if first n chars are hexadecimal digits
    5593              :  */
    5594              : static bool
    5595           78 : isxdigits_n(const char *instr, size_t n)
    5596              : {
    5597          330 :     for (size_t i = 0; i < n; i++)
    5598          285 :         if (!isxdigit((unsigned char) instr[i]))
    5599           33 :             return false;
    5600              : 
    5601           45 :     return true;
    5602              : }
    5603              : 
    5604              : static unsigned int
    5605          252 : hexval(unsigned char c)
    5606              : {
    5607          252 :     if (c >= '0' && c <= '9')
    5608          192 :         return c - '0';
    5609           60 :     if (c >= 'a' && c <= 'f')
    5610           30 :         return c - 'a' + 0xA;
    5611           30 :     if (c >= 'A' && c <= 'F')
    5612           30 :         return c - 'A' + 0xA;
    5613            0 :     elog(ERROR, "invalid hexadecimal digit");
    5614              :     return 0;                   /* not reached */
    5615              : }
    5616              : 
    5617              : /*
    5618              :  * Translate string with hexadecimal digits to number
    5619              :  */
    5620              : static unsigned int
    5621           45 : hexval_n(const char *instr, size_t n)
    5622              : {
    5623           45 :     unsigned int result = 0;
    5624              : 
    5625          297 :     for (size_t i = 0; i < n; i++)
    5626          252 :         result += hexval(instr[i]) << (4 * (n - i - 1));
    5627              : 
    5628           45 :     return result;
    5629              : }
    5630              : 
    5631              : /*
    5632              :  * Replaces Unicode escape sequences by Unicode characters
    5633              :  */
    5634              : Datum
    5635           33 : unistr(PG_FUNCTION_ARGS)
    5636              : {
    5637           33 :     text       *input_text = PG_GETARG_TEXT_PP(0);
    5638              :     char       *instr;
    5639              :     int         len;
    5640              :     StringInfoData str;
    5641              :     text       *result;
    5642           33 :     char16_t    pair_first = 0;
    5643              :     char        cbuf[MAX_UNICODE_EQUIVALENT_STRING + 1];
    5644              : 
    5645           33 :     instr = VARDATA_ANY(input_text);
    5646           33 :     len = VARSIZE_ANY_EXHDR(input_text);
    5647              : 
    5648           33 :     initStringInfo(&str);
    5649              : 
    5650          255 :     while (len > 0)
    5651              :     {
    5652          243 :         if (instr[0] == '\\')
    5653              :         {
    5654           51 :             if (len >= 2 &&
    5655           51 :                 instr[1] == '\\')
    5656              :             {
    5657            3 :                 if (pair_first)
    5658            0 :                     goto invalid_pair;
    5659            3 :                 appendStringInfoChar(&str, '\\');
    5660            3 :                 instr += 2;
    5661            3 :                 len -= 2;
    5662              :             }
    5663           48 :             else if ((len >= 5 && isxdigits_n(instr + 1, 4)) ||
    5664           33 :                      (len >= 6 && instr[1] == 'u' && isxdigits_n(instr + 2, 4)))
    5665           15 :             {
    5666              :                 char32_t    unicode;
    5667           21 :                 int         offset = instr[1] == 'u' ? 2 : 1;
    5668              : 
    5669           21 :                 unicode = hexval_n(instr + offset, 4);
    5670              : 
    5671           21 :                 if (!is_valid_unicode_codepoint(unicode))
    5672            0 :                     ereport(ERROR,
    5673              :                             errcode(ERRCODE_INVALID_PARAMETER_VALUE),
    5674              :                             errmsg("invalid Unicode code point: %04X", unicode));
    5675              : 
    5676           21 :                 if (pair_first)
    5677              :                 {
    5678            6 :                     if (is_utf16_surrogate_second(unicode))
    5679              :                     {
    5680            0 :                         unicode = surrogate_pair_to_codepoint(pair_first, unicode);
    5681            0 :                         pair_first = 0;
    5682              :                     }
    5683              :                     else
    5684            6 :                         goto invalid_pair;
    5685              :                 }
    5686           15 :                 else if (is_utf16_surrogate_second(unicode))
    5687            0 :                     goto invalid_pair;
    5688              : 
    5689           15 :                 if (is_utf16_surrogate_first(unicode))
    5690            9 :                     pair_first = unicode;
    5691              :                 else
    5692              :                 {
    5693            6 :                     pg_unicode_to_server(unicode, (unsigned char *) cbuf);
    5694            6 :                     appendStringInfoString(&str, cbuf);
    5695              :                 }
    5696              : 
    5697           15 :                 instr += 4 + offset;
    5698           15 :                 len -= 4 + offset;
    5699              :             }
    5700           27 :             else if (len >= 8 && instr[1] == '+' && isxdigits_n(instr + 2, 6))
    5701            6 :             {
    5702              :                 char32_t    unicode;
    5703              : 
    5704           12 :                 unicode = hexval_n(instr + 2, 6);
    5705              : 
    5706           12 :                 if (!is_valid_unicode_codepoint(unicode))
    5707            3 :                     ereport(ERROR,
    5708              :                             errcode(ERRCODE_INVALID_PARAMETER_VALUE),
    5709              :                             errmsg("invalid Unicode code point: %04X", unicode));
    5710              : 
    5711            9 :                 if (pair_first)
    5712              :                 {
    5713            3 :                     if (is_utf16_surrogate_second(unicode))
    5714              :                     {
    5715            0 :                         unicode = surrogate_pair_to_codepoint(pair_first, unicode);
    5716            0 :                         pair_first = 0;
    5717              :                     }
    5718              :                     else
    5719            3 :                         goto invalid_pair;
    5720              :                 }
    5721            6 :                 else if (is_utf16_surrogate_second(unicode))
    5722            0 :                     goto invalid_pair;
    5723              : 
    5724            6 :                 if (is_utf16_surrogate_first(unicode))
    5725            3 :                     pair_first = unicode;
    5726              :                 else
    5727              :                 {
    5728            3 :                     pg_unicode_to_server(unicode, (unsigned char *) cbuf);
    5729            3 :                     appendStringInfoString(&str, cbuf);
    5730              :                 }
    5731              : 
    5732            6 :                 instr += 8;
    5733            6 :                 len -= 8;
    5734              :             }
    5735           15 :             else if (len >= 10 && instr[1] == 'U' && isxdigits_n(instr + 2, 8))
    5736            6 :             {
    5737              :                 char32_t    unicode;
    5738              : 
    5739           12 :                 unicode = hexval_n(instr + 2, 8);
    5740              : 
    5741           12 :                 if (!is_valid_unicode_codepoint(unicode))
    5742            3 :                     ereport(ERROR,
    5743              :                             errcode(ERRCODE_INVALID_PARAMETER_VALUE),
    5744              :                             errmsg("invalid Unicode code point: %04X", unicode));
    5745              : 
    5746            9 :                 if (pair_first)
    5747              :                 {
    5748            3 :                     if (is_utf16_surrogate_second(unicode))
    5749              :                     {
    5750            0 :                         unicode = surrogate_pair_to_codepoint(pair_first, unicode);
    5751            0 :                         pair_first = 0;
    5752              :                     }
    5753              :                     else
    5754            3 :                         goto invalid_pair;
    5755              :                 }
    5756            6 :                 else if (is_utf16_surrogate_second(unicode))
    5757            0 :                     goto invalid_pair;
    5758              : 
    5759            6 :                 if (is_utf16_surrogate_first(unicode))
    5760            3 :                     pair_first = unicode;
    5761              :                 else
    5762              :                 {
    5763            3 :                     pg_unicode_to_server(unicode, (unsigned char *) cbuf);
    5764            3 :                     appendStringInfoString(&str, cbuf);
    5765              :                 }
    5766              : 
    5767            6 :                 instr += 10;
    5768            6 :                 len -= 10;
    5769              :             }
    5770              :             else
    5771            3 :                 ereport(ERROR,
    5772              :                         (errcode(ERRCODE_SYNTAX_ERROR),
    5773              :                          errmsg("invalid Unicode escape"),
    5774              :                          errhint("Unicode escapes must be \\XXXX, \\+XXXXXX, \\uXXXX, or \\UXXXXXXXX.")));
    5775              :         }
    5776              :         else
    5777              :         {
    5778          192 :             if (pair_first)
    5779            0 :                 goto invalid_pair;
    5780              : 
    5781          192 :             appendStringInfoChar(&str, *instr++);
    5782          192 :             len--;
    5783              :         }
    5784              :     }
    5785              : 
    5786              :     /* unfinished surrogate pair? */
    5787           12 :     if (pair_first)
    5788            3 :         goto invalid_pair;
    5789              : 
    5790            9 :     result = cstring_to_text_with_len(str.data, str.len);
    5791            9 :     pfree(str.data);
    5792              : 
    5793            9 :     PG_RETURN_TEXT_P(result);
    5794              : 
    5795           15 : invalid_pair:
    5796           15 :     ereport(ERROR,
    5797              :             (errcode(ERRCODE_SYNTAX_ERROR),
    5798              :              errmsg("invalid Unicode surrogate pair")));
    5799              :     PG_RETURN_NULL();           /* keep compiler quiet */
    5800              : }
        

Generated by: LCOV version 2.0-1