LCOV - differential code coverage report
Current view: top level - src/backend/utils/adt - oracle_compat.c (source / functions) Coverage Total Hit UBC GNC CBC DCB
Current: ba12a202ce1b5581dc0ed149cf3f637d7897ad5d vs 2866d8c7dbfc9d882a7d80fef93fbbe763709932 Lines: 88.7 % 407 361 46 4 357 4
Current Date: 2026-08-27 14:31:44 +0300 Functions: 100.0 % 21 21 1 20
Baseline: lcov-20260827-baseline Branches: 64.1 % 234 150 84 150
Baseline Date: 2026-08-27 14:31:58 +0300 Line coverage date bins:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
(7,30] days: 100.0 % 12 12 4 8
(30,360] days: 100.0 % 14 14 14
(360..) days: 87.9 % 381 335 46 335
Function coverage date bins:
(360..) days: 100.0 % 21 21 1 20
Branch coverage date bins:
(7,30] days: 71.4 % 14 10 4 10
(360..) days: 63.6 % 220 140 80 140

 Age         Owner                    Branch data    TLA  Line data    Source code
                                  1                 :                : /*-------------------------------------------------------------------------
                                  2                 :                :  * oracle_compat.c
                                  3                 :                :  *  Oracle compatible functions.
                                  4                 :                :  *
                                  5                 :                :  * Copyright (c) 1996-2026, PostgreSQL Global Development Group
                                  6                 :                :  *
                                  7                 :                :  *  Author: Edmund Mergl <E.Mergl@bawue.de>
                                  8                 :                :  *  Multibyte enhancement: Tatsuo Ishii <ishii@postgresql.org>
                                  9                 :                :  *
                                 10                 :                :  *
                                 11                 :                :  * IDENTIFICATION
                                 12                 :                :  *  src/backend/utils/adt/oracle_compat.c
                                 13                 :                :  *
                                 14                 :                :  *-------------------------------------------------------------------------
                                 15                 :                :  */
                                 16                 :                : #include "postgres.h"
                                 17                 :                : 
                                 18                 :                : #include "common/int.h"
                                 19                 :                : #include "mb/pg_wchar.h"
                                 20                 :                : #include "miscadmin.h"
                                 21                 :                : #include "utils/builtins.h"
                                 22                 :                : #include "utils/formatting.h"
                                 23                 :                : #include "utils/memutils.h"
                                 24                 :                : #include "varatt.h"
                                 25                 :                : 
                                 26                 :                : 
                                 27                 :                : static text *dotrim(const char *string, int stringlen,
                                 28                 :                :                     const char *set, int setlen,
                                 29                 :                :                     bool doltrim, bool dortrim);
                                 30                 :                : static bytea *dobyteatrim(bytea *string, bytea *set,
                                 31                 :                :                           bool doltrim, bool dortrim);
                                 32                 :                : 
                                 33                 :                : 
                                 34                 :                : /********************************************************************
                                 35                 :                :  *
                                 36                 :                :  * lower
                                 37                 :                :  *
                                 38                 :                :  * Syntax:
                                 39                 :                :  *
                                 40                 :                :  *   text lower(text string)
                                 41                 :                :  *
                                 42                 :                :  * Purpose:
                                 43                 :                :  *
                                 44                 :                :  *   Returns string, with all letters forced to lowercase.
                                 45                 :                :  *
                                 46                 :                :  ********************************************************************/
                                 47                 :                : 
                                 48                 :                : Datum
 9548 tgl@sss.pgh.pa.us          49                 :CBC      101160 : lower(PG_FUNCTION_ARGS)
                                 50                 :                : {
 6286 bruce@momjian.us           51                 :         101160 :     text       *in_string = PG_GETARG_TEXT_PP(0);
                                 52                 :                :     char       *out_string;
                                 53                 :                :     text       *result;
                                 54                 :                : 
 6620 tgl@sss.pgh.pa.us          55                 :         101160 :     out_string = str_tolower(VARDATA_ANY(in_string),
                                 56                 :                :                              VARSIZE_ANY_EXHDR(in_string),
                                 57                 :                :                              PG_GET_COLLATION());
 6639 bruce@momjian.us           58                 :         101160 :     result = cstring_to_text(out_string);
                                 59                 :         101160 :     pfree(out_string);
                                 60                 :                : 
                                 61                 :         101160 :     PG_RETURN_TEXT_P(result);
                                 62                 :                : }
                                 63                 :                : 
                                 64                 :                : 
                                 65                 :                : /********************************************************************
                                 66                 :                :  *
                                 67                 :                :  * upper
                                 68                 :                :  *
                                 69                 :                :  * Syntax:
                                 70                 :                :  *
                                 71                 :                :  *   text upper(text string)
                                 72                 :                :  *
                                 73                 :                :  * Purpose:
                                 74                 :                :  *
                                 75                 :                :  *   Returns string, with all letters forced to uppercase.
                                 76                 :                :  *
                                 77                 :                :  ********************************************************************/
                                 78                 :                : 
                                 79                 :                : Datum
 9548 tgl@sss.pgh.pa.us          80                 :         683772 : upper(PG_FUNCTION_ARGS)
                                 81                 :                : {
 6286 bruce@momjian.us           82                 :         683772 :     text       *in_string = PG_GETARG_TEXT_PP(0);
                                 83                 :                :     char       *out_string;
                                 84                 :                :     text       *result;
                                 85                 :                : 
 6620 tgl@sss.pgh.pa.us          86                 :         683772 :     out_string = str_toupper(VARDATA_ANY(in_string),
                                 87                 :                :                              VARSIZE_ANY_EXHDR(in_string),
                                 88                 :                :                              PG_GET_COLLATION());
 6639 bruce@momjian.us           89                 :         683772 :     result = cstring_to_text(out_string);
                                 90                 :         683772 :     pfree(out_string);
                                 91                 :                : 
                                 92                 :         683772 :     PG_RETURN_TEXT_P(result);
                                 93                 :                : }
                                 94                 :                : 
                                 95                 :                : 
                                 96                 :                : /********************************************************************
                                 97                 :                :  *
                                 98                 :                :  * initcap
                                 99                 :                :  *
                                100                 :                :  * Syntax:
                                101                 :                :  *
                                102                 :                :  *   text initcap(text string)
                                103                 :                :  *
                                104                 :                :  * Purpose:
                                105                 :                :  *
                                106                 :                :  *   Returns string, with first letter of each word in uppercase, all
                                107                 :                :  *   other letters in lowercase. A word is defined as a sequence of
                                108                 :                :  *   alphanumeric characters, delimited by non-alphanumeric
                                109                 :                :  *   characters.
                                110                 :                :  *
                                111                 :                :  ********************************************************************/
                                112                 :                : 
                                113                 :                : Datum
 9548 tgl@sss.pgh.pa.us         114                 :            167 : initcap(PG_FUNCTION_ARGS)
                                115                 :                : {
 6286 bruce@momjian.us          116                 :            167 :     text       *in_string = PG_GETARG_TEXT_PP(0);
                                117                 :                :     char       *out_string;
                                118                 :                :     text       *result;
                                119                 :                : 
 6620 tgl@sss.pgh.pa.us         120                 :            167 :     out_string = str_initcap(VARDATA_ANY(in_string),
                                121                 :                :                              VARSIZE_ANY_EXHDR(in_string),
                                122                 :                :                              PG_GET_COLLATION());
 6639 bruce@momjian.us          123                 :            167 :     result = cstring_to_text(out_string);
                                124                 :            167 :     pfree(out_string);
                                125                 :                : 
                                126                 :            167 :     PG_RETURN_TEXT_P(result);
                                127                 :                : }
                                128                 :                : 
                                129                 :                : Datum
  580 jdavis@postgresql.or      130                 :             20 : casefold(PG_FUNCTION_ARGS)
                                131                 :                : {
                                132                 :             20 :     text       *in_string = PG_GETARG_TEXT_PP(0);
                                133                 :                :     char       *out_string;
                                134                 :                :     text       *result;
                                135                 :                : 
                                136                 :             20 :     out_string = str_casefold(VARDATA_ANY(in_string),
                                137                 :                :                               VARSIZE_ANY_EXHDR(in_string),
                                138                 :                :                               PG_GET_COLLATION());
                                139                 :             20 :     result = cstring_to_text(out_string);
                                140                 :             20 :     pfree(out_string);
                                141                 :                : 
                                142                 :             20 :     PG_RETURN_TEXT_P(result);
                                143                 :                : }
                                144                 :                : 
                                145                 :                : 
                                146                 :                : /********************************************************************
                                147                 :                :  *
                                148                 :                :  * lpad
                                149                 :                :  *
                                150                 :                :  * Syntax:
                                151                 :                :  *
                                152                 :                :  *   text lpad(text string1, int4 len, text string2)
                                153                 :                :  *
                                154                 :                :  * Purpose:
                                155                 :                :  *
                                156                 :                :  *   Returns string1, left-padded to length len with the sequence of
                                157                 :                :  *   characters in string2.  If len is less than the length of string1,
                                158                 :                :  *   instead truncate (on the right) to len.
                                159                 :                :  *
                                160                 :                :  ********************************************************************/
                                161                 :                : 
                                162                 :                : Datum
 9571 tgl@sss.pgh.pa.us         163                 :          16397 : lpad(PG_FUNCTION_ARGS)
                                164                 :                : {
 6915                           165                 :          16397 :     text       *string1 = PG_GETARG_TEXT_PP(0);
 9571                           166                 :          16397 :     int32       len = PG_GETARG_INT32(1);
 6915                           167                 :          16397 :     text       *string2 = PG_GETARG_TEXT_PP(2);
                                168                 :                :     text       *ret;
                                169                 :                :     char       *ptr1,
                                170                 :                :                *ptr2,
                                171                 :                :                *ptr2start,
                                172                 :                :                *ptr_ret;
                                173                 :                :     const char *ptr2end;
                                174                 :                :     int         m,
                                175                 :                :                 s1len,
                                176                 :                :                 s2len;
                                177                 :                :     int         bytelen;
                                178                 :                : 
                                179                 :                :     /* Negative len is silently taken as zero */
 9394                           180         [ +  + ]:          16397 :     if (len < 0)
                                181                 :              5 :         len = 0;
                                182                 :                : 
 6915                           183                 :          16397 :     s1len = VARSIZE_ANY_EXHDR(string1);
 9394                           184         [ -  + ]:          16397 :     if (s1len < 0)
 9394 tgl@sss.pgh.pa.us         185                 :UBC           0 :         s1len = 0;              /* shouldn't happen */
                                186                 :                : 
 6915 tgl@sss.pgh.pa.us         187                 :CBC       16397 :     s2len = VARSIZE_ANY_EXHDR(string2);
 9394                           188         [ -  + ]:          16397 :     if (s2len < 0)
 9394 tgl@sss.pgh.pa.us         189                 :UBC           0 :         s2len = 0;              /* shouldn't happen */
                                190                 :                : 
 6915 tgl@sss.pgh.pa.us         191                 :CBC       16397 :     s1len = pg_mbstrlen_with_len(VARDATA_ANY(string1), s1len);
                                192                 :                : 
 9394                           193         [ +  + ]:          16397 :     if (s1len > len)
                                194                 :             40 :         s1len = len;            /* truncate string1 to len chars */
                                195                 :                : 
                                196         [ +  + ]:          16397 :     if (s2len <= 0)
                                197                 :              5 :         len = s1len;            /* nothing to pad with, so don't pad */
                                198                 :                : 
                                199                 :                :     /* compute worst-case output length */
 1554                           200         [ +  - ]:          16397 :     if (unlikely(pg_mul_s32_overflow(pg_database_encoding_max_length(), len,
                                201                 :          16397 :                                      &bytelen)) ||
                                202         [ +  - ]:          16397 :         unlikely(pg_add_s32_overflow(bytelen, VARHDRSZ, &bytelen)) ||
                                203         [ -  + ]:          16397 :         unlikely(!AllocSizeIsValid(bytelen)))
 8432 tgl@sss.pgh.pa.us         204         [ #  # ]:UBC           0 :         ereport(ERROR,
                                205                 :                :                 (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
                                206                 :                :                  errmsg("requested length too large")));
                                207                 :                : 
 1554 tgl@sss.pgh.pa.us         208                 :CBC       16397 :     ret = (text *) palloc(bytelen);
                                209                 :                : 
 9394                           210                 :          16397 :     m = len - s1len;
                                211                 :                : 
 6915                           212                 :          16397 :     ptr2 = ptr2start = VARDATA_ANY(string2);
 9394                           213                 :          16397 :     ptr2end = ptr2 + s2len;
10581 bruce@momjian.us          214                 :          16397 :     ptr_ret = VARDATA(ret);
                                215                 :                : 
 9104 ishii@postgresql.org      216         [ +  + ]:          17470 :     while (m--)
                                217                 :                :     {
  232 tmunro@postgresql.or      218                 :           1073 :         int         mlen = pg_mblen_range(ptr2, ptr2end);
                                219                 :                : 
 9072 bruce@momjian.us          220                 :           1073 :         memcpy(ptr_ret, ptr2, mlen);
                                221                 :           1073 :         ptr_ret += mlen;
                                222                 :           1073 :         ptr2 += mlen;
                                223         [ +  + ]:           1073 :         if (ptr2 == ptr2end)    /* wrap around at end of s2 */
 6915 tgl@sss.pgh.pa.us         224                 :           1057 :             ptr2 = ptr2start;
                                225                 :                :     }
                                226                 :                : 
                                227                 :          16397 :     ptr1 = VARDATA_ANY(string1);
                                228                 :                : 
 9104 ishii@postgresql.org      229         [ +  + ]:          48153 :     while (s1len--)
                                230                 :                :     {
  232 tmunro@postgresql.or      231                 :          31756 :         int         mlen = pg_mblen_unbounded(ptr1);
                                232                 :                : 
 9072 bruce@momjian.us          233                 :          31756 :         memcpy(ptr_ret, ptr1, mlen);
                                234                 :          31756 :         ptr_ret += mlen;
                                235                 :          31756 :         ptr1 += mlen;
                                236                 :                :     }
                                237                 :                : 
 7121 tgl@sss.pgh.pa.us         238                 :          16397 :     SET_VARSIZE(ret, ptr_ret - (char *) ret);
                                239                 :                : 
 9571                           240                 :          16397 :     PG_RETURN_TEXT_P(ret);
                                241                 :                : }
                                242                 :                : 
                                243                 :                : 
                                244                 :                : /********************************************************************
                                245                 :                :  *
                                246                 :                :  * rpad
                                247                 :                :  *
                                248                 :                :  * Syntax:
                                249                 :                :  *
                                250                 :                :  *   text rpad(text string1, int4 len, text string2)
                                251                 :                :  *
                                252                 :                :  * Purpose:
                                253                 :                :  *
                                254                 :                :  *   Returns string1, right-padded to length len with the sequence of
                                255                 :                :  *   characters in string2.  If len is less than the length of string1,
                                256                 :                :  *   instead truncate (on the right) to len.
                                257                 :                :  *
                                258                 :                :  ********************************************************************/
                                259                 :                : 
                                260                 :                : Datum
                                261                 :            236 : rpad(PG_FUNCTION_ARGS)
                                262                 :                : {
 6915                           263                 :            236 :     text       *string1 = PG_GETARG_TEXT_PP(0);
 9571                           264                 :            236 :     int32       len = PG_GETARG_INT32(1);
 6915                           265                 :            236 :     text       *string2 = PG_GETARG_TEXT_PP(2);
                                266                 :                :     text       *ret;
                                267                 :                :     char       *ptr1,
                                268                 :                :                *ptr2,
                                269                 :                :                *ptr2start,
                                270                 :                :                *ptr_ret;
                                271                 :                :     const char *ptr2end;
                                272                 :                :     int         m,
                                273                 :                :                 s1len,
                                274                 :                :                 s2len;
                                275                 :                :     int         bytelen;
                                276                 :                : 
                                277                 :                :     /* Negative len is silently taken as zero */
 9394                           278         [ +  + ]:            236 :     if (len < 0)
                                279                 :              5 :         len = 0;
                                280                 :                : 
 6915                           281                 :            236 :     s1len = VARSIZE_ANY_EXHDR(string1);
 9394                           282         [ -  + ]:            236 :     if (s1len < 0)
 9394 tgl@sss.pgh.pa.us         283                 :UBC           0 :         s1len = 0;              /* shouldn't happen */
                                284                 :                : 
 6915 tgl@sss.pgh.pa.us         285                 :CBC         236 :     s2len = VARSIZE_ANY_EXHDR(string2);
 9394                           286         [ -  + ]:            236 :     if (s2len < 0)
 9394 tgl@sss.pgh.pa.us         287                 :UBC           0 :         s2len = 0;              /* shouldn't happen */
                                288                 :                : 
 6915 tgl@sss.pgh.pa.us         289                 :CBC         236 :     s1len = pg_mbstrlen_with_len(VARDATA_ANY(string1), s1len);
                                290                 :                : 
 9394                           291         [ +  + ]:            236 :     if (s1len > len)
                                292                 :             11 :         s1len = len;            /* truncate string1 to len chars */
                                293                 :                : 
                                294         [ +  + ]:            236 :     if (s2len <= 0)
                                295                 :              5 :         len = s1len;            /* nothing to pad with, so don't pad */
                                296                 :                : 
                                297                 :                :     /* compute worst-case output length */
 1554                           298         [ +  - ]:            236 :     if (unlikely(pg_mul_s32_overflow(pg_database_encoding_max_length(), len,
                                299                 :            236 :                                      &bytelen)) ||
                                300         [ +  - ]:            236 :         unlikely(pg_add_s32_overflow(bytelen, VARHDRSZ, &bytelen)) ||
                                301         [ -  + ]:            236 :         unlikely(!AllocSizeIsValid(bytelen)))
 8432 tgl@sss.pgh.pa.us         302         [ #  # ]:UBC           0 :         ereport(ERROR,
                                303                 :                :                 (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
                                304                 :                :                  errmsg("requested length too large")));
                                305                 :                : 
 1554 tgl@sss.pgh.pa.us         306                 :CBC         236 :     ret = (text *) palloc(bytelen);
                                307                 :                : 
 9394                           308                 :            236 :     m = len - s1len;
                                309                 :                : 
 6915                           310                 :            236 :     ptr1 = VARDATA_ANY(string1);
                                311                 :                : 
10581 bruce@momjian.us          312                 :            236 :     ptr_ret = VARDATA(ret);
                                313                 :                : 
 9104 ishii@postgresql.org      314         [ +  + ]:           3780 :     while (s1len--)
                                315                 :                :     {
  232 tmunro@postgresql.or      316                 :           3544 :         int         mlen = pg_mblen_unbounded(ptr1);
                                317                 :                : 
 9072 bruce@momjian.us          318                 :           3544 :         memcpy(ptr_ret, ptr1, mlen);
                                319                 :           3544 :         ptr_ret += mlen;
                                320                 :           3544 :         ptr1 += mlen;
                                321                 :                :     }
                                322                 :                : 
 6915 tgl@sss.pgh.pa.us         323                 :            236 :     ptr2 = ptr2start = VARDATA_ANY(string2);
 9394                           324                 :            236 :     ptr2end = ptr2 + s2len;
                                325                 :                : 
 9104 ishii@postgresql.org      326         [ +  + ]:        1602389 :     while (m--)
                                327                 :                :     {
  232 tmunro@postgresql.or      328                 :        1602153 :         int         mlen = pg_mblen_range(ptr2, ptr2end);
                                329                 :                : 
 9072 bruce@momjian.us          330                 :        1602153 :         memcpy(ptr_ret, ptr2, mlen);
                                331                 :        1602153 :         ptr_ret += mlen;
                                332                 :        1602153 :         ptr2 += mlen;
                                333         [ +  + ]:        1602153 :         if (ptr2 == ptr2end)    /* wrap around at end of s2 */
 6915 tgl@sss.pgh.pa.us         334                 :        1602137 :             ptr2 = ptr2start;
                                335                 :                :     }
                                336                 :                : 
 7121                           337                 :            236 :     SET_VARSIZE(ret, ptr_ret - (char *) ret);
                                338                 :                : 
 9571                           339                 :            236 :     PG_RETURN_TEXT_P(ret);
                                340                 :                : }
                                341                 :                : 
                                342                 :                : 
                                343                 :                : /********************************************************************
                                344                 :                :  *
                                345                 :                :  * btrim
                                346                 :                :  *
                                347                 :                :  * Syntax:
                                348                 :                :  *
                                349                 :                :  *   text btrim(text string, text set)
                                350                 :                :  *
                                351                 :                :  * Purpose:
                                352                 :                :  *
                                353                 :                :  *   Returns string with characters removed from the front and back
                                354                 :                :  *   up to the first character not in set.
                                355                 :                :  *
                                356                 :                :  ********************************************************************/
                                357                 :                : 
                                358                 :                : Datum
 9548                           359                 :             20 : btrim(PG_FUNCTION_ARGS)
                                360                 :                : {
 6915                           361                 :             20 :     text       *string = PG_GETARG_TEXT_PP(0);
                                362                 :             20 :     text       *set = PG_GETARG_TEXT_PP(1);
                                363                 :                :     text       *ret;
                                364                 :                : 
                                365                 :             20 :     ret = dotrim(VARDATA_ANY(string), VARSIZE_ANY_EXHDR(string),
                                366                 :             20 :                  VARDATA_ANY(set), VARSIZE_ANY_EXHDR(set),
                                367                 :                :                  true, true);
                                368                 :                : 
 8497                           369                 :             20 :     PG_RETURN_TEXT_P(ret);
                                370                 :                : }
                                371                 :                : 
                                372                 :                : /********************************************************************
                                373                 :                :  *
                                374                 :                :  * btrim1 --- btrim with set fixed as ' '
                                375                 :                :  *
                                376                 :                :  ********************************************************************/
                                377                 :                : 
                                378                 :                : Datum
                                379                 :            383 : btrim1(PG_FUNCTION_ARGS)
                                380                 :                : {
 6915                           381                 :            383 :     text       *string = PG_GETARG_TEXT_PP(0);
                                382                 :                :     text       *ret;
                                383                 :                : 
                                384                 :            383 :     ret = dotrim(VARDATA_ANY(string), VARSIZE_ANY_EXHDR(string),
                                385                 :                :                  " ", 1,
                                386                 :                :                  true, true);
                                387                 :                : 
 8497                           388                 :            383 :     PG_RETURN_TEXT_P(ret);
                                389                 :                : }
                                390                 :                : 
                                391                 :                : /*
                                392                 :                :  * Common implementation for btrim, ltrim, rtrim
                                393                 :                :  */
                                394                 :                : static text *
                                395                 :          22899 : dotrim(const char *string, int stringlen,
                                396                 :                :        const char *set, int setlen,
                                397                 :                :        bool doltrim, bool dortrim)
                                398                 :                : {
                                399                 :                :     int         i;
                                400                 :                : 
                                401                 :                :     /* Nothing to do if either string or set is empty */
                                402   [ +  -  +  - ]:          22899 :     if (stringlen > 0 && setlen > 0)
                                403                 :                :     {
                                404         [ +  + ]:          22899 :         if (pg_database_encoding_max_length() > 1)
                                405                 :                :         {
                                406                 :                :             /*
                                407                 :                :              * In the multibyte-encoding case, build arrays of pointers to
                                408                 :                :              * character starts, so that we can avoid inefficient checks in
                                409                 :                :              * the inner loops.
                                410                 :                :              */
                                411                 :                :             const char **stringchars;
                                412                 :                :             const char **setchars;
                                413                 :                :             const char *setend;
                                414                 :                :             int        *stringmblen;
                                415                 :                :             int        *setmblen;
                                416                 :                :             int         stringnchars;
                                417                 :                :             int         setnchars;
                                418                 :                :             int         resultndx;
                                419                 :                :             int         resultnchars;
                                420                 :                :             const char *p;
                                421                 :                :             const char *pend;
                                422                 :                :             int         len;
                                423                 :                :             int         mblen;
                                424                 :                :             const char *str_pos;
                                425                 :                :             int         str_len;
                                426                 :                : 
   10 michael@paquier.xyz       427                 :GNC       22888 :             stringchars = palloc_array(const char *, stringlen);
                                428                 :          22888 :             stringmblen = palloc_array(int, stringlen);
 8497 tgl@sss.pgh.pa.us         429                 :CBC       22888 :             stringnchars = 0;
                                430                 :          22888 :             p = string;
                                431                 :          22888 :             len = stringlen;
  232 tmunro@postgresql.or      432                 :          22888 :             pend = p + len;
 8497 tgl@sss.pgh.pa.us         433         [ +  + ]:         205650 :             while (len > 0)
                                434                 :                :             {
                                435                 :         182762 :                 stringchars[stringnchars] = p;
  232 tmunro@postgresql.or      436                 :         182762 :                 stringmblen[stringnchars] = mblen = pg_mblen_range(p, pend);
 8497 tgl@sss.pgh.pa.us         437                 :         182762 :                 stringnchars++;
                                438                 :         182762 :                 p += mblen;
                                439                 :         182762 :                 len -= mblen;
                                440                 :                :             }
                                441                 :                : 
   10 michael@paquier.xyz       442                 :GNC       22888 :             setchars = palloc_array(const char *, setlen);
                                443                 :          22888 :             setmblen = palloc_array(int, setlen);
 8497 tgl@sss.pgh.pa.us         444                 :CBC       22888 :             setnchars = 0;
                                445                 :          22888 :             p = set;
                                446                 :          22888 :             len = setlen;
  232 tmunro@postgresql.or      447                 :          22888 :             setend = set + setlen;
 8497 tgl@sss.pgh.pa.us         448         [ +  + ]:          46395 :             while (len > 0)
                                449                 :                :             {
                                450                 :          23507 :                 setchars[setnchars] = p;
  232 tmunro@postgresql.or      451                 :          23507 :                 setmblen[setnchars] = mblen = pg_mblen_range(p, setend);
 8497 tgl@sss.pgh.pa.us         452                 :          23507 :                 setnchars++;
                                453                 :          23507 :                 p += mblen;
                                454                 :          23507 :                 len -= mblen;
                                455                 :                :             }
                                456                 :                : 
                                457                 :          22888 :             resultndx = 0;      /* index in stringchars[] */
                                458                 :          22888 :             resultnchars = stringnchars;
                                459                 :                : 
                                460         [ +  + ]:          22888 :             if (doltrim)
                                461                 :                :             {
                                462         [ +  - ]:          37934 :                 while (resultnchars > 0)
                                463                 :                :                 {
                                464                 :          37934 :                     str_pos = stringchars[resultndx];
                                465                 :          37934 :                     str_len = stringmblen[resultndx];
                                466         [ +  + ]:          57127 :                     for (i = 0; i < setnchars; i++)
                                467                 :                :                     {
                                468         [ +  - ]:          38060 :                         if (str_len == setmblen[i] &&
                                469         [ +  + ]:          38060 :                             memcmp(str_pos, setchars[i], str_len) == 0)
                                470                 :          18867 :                             break;
                                471                 :                :                     }
                                472         [ +  + ]:          37934 :                     if (i >= setnchars)
                                473                 :          19067 :                         break;  /* no match here */
                                474                 :          18867 :                     string += str_len;
                                475                 :          18867 :                     stringlen -= str_len;
                                476                 :          18867 :                     resultndx++;
                                477                 :          18867 :                     resultnchars--;
                                478                 :                :                 }
                                479                 :                :             }
                                480                 :                : 
                                481         [ +  + ]:          22888 :             if (dortrim)
                                482                 :                :             {
                                483         [ +  - ]:          49171 :                 while (resultnchars > 0)
                                484                 :                :                 {
                                485                 :          49171 :                     str_pos = stringchars[resultndx + resultnchars - 1];
                                486                 :          49171 :                     str_len = stringmblen[resultndx + resultnchars - 1];
                                487         [ +  + ]:          55382 :                     for (i = 0; i < setnchars; i++)
                                488                 :                :                     {
                                489         [ +  - ]:          51158 :                         if (str_len == setmblen[i] &&
                                490         [ +  + ]:          51158 :                             memcmp(str_pos, setchars[i], str_len) == 0)
                                491                 :          44947 :                             break;
                                492                 :                :                     }
                                493         [ +  + ]:          49171 :                     if (i >= setnchars)
                                494                 :           4224 :                         break;  /* no match here */
                                495                 :          44947 :                     stringlen -= str_len;
                                496                 :          44947 :                     resultnchars--;
                                497                 :                :                 }
                                498                 :                :             }
                                499                 :                : 
                                500                 :          22888 :             pfree(stringchars);
                                501                 :          22888 :             pfree(stringmblen);
                                502                 :          22888 :             pfree(setchars);
                                503                 :          22888 :             pfree(setmblen);
                                504                 :                :         }
                                505                 :                :         else
                                506                 :                :         {
                                507                 :                :             /*
                                508                 :                :              * In the single-byte-encoding case, we don't need such overhead.
                                509                 :                :              */
                                510         [ -  + ]:             11 :             if (doltrim)
                                511                 :                :             {
 8497 tgl@sss.pgh.pa.us         512         [ #  # ]:UBC           0 :                 while (stringlen > 0)
                                513                 :                :                 {
 8424 bruce@momjian.us          514                 :              0 :                     char        str_ch = *string;
                                515                 :                : 
 8497 tgl@sss.pgh.pa.us         516         [ #  # ]:              0 :                     for (i = 0; i < setlen; i++)
                                517                 :                :                     {
                                518         [ #  # ]:              0 :                         if (str_ch == set[i])
                                519                 :              0 :                             break;
                                520                 :                :                     }
                                521         [ #  # ]:              0 :                     if (i >= setlen)
                                522                 :              0 :                         break;  /* no match here */
                                523                 :              0 :                     string++;
                                524                 :              0 :                     stringlen--;
                                525                 :                :                 }
                                526                 :                :             }
                                527                 :                : 
 8497 tgl@sss.pgh.pa.us         528         [ +  - ]:CBC          11 :             if (dortrim)
                                529                 :                :             {
                                530         [ +  - ]:             22 :                 while (stringlen > 0)
                                531                 :                :                 {
 8424 bruce@momjian.us          532                 :             22 :                     char        str_ch = string[stringlen - 1];
                                533                 :                : 
 8497 tgl@sss.pgh.pa.us         534         [ +  + ]:             33 :                     for (i = 0; i < setlen; i++)
                                535                 :                :                     {
                                536         [ +  + ]:             22 :                         if (str_ch == set[i])
                                537                 :             11 :                             break;
                                538                 :                :                     }
                                539         [ +  + ]:             22 :                     if (i >= setlen)
                                540                 :             11 :                         break;  /* no match here */
                                541                 :             11 :                     stringlen--;
                                542                 :                :                 }
                                543                 :                :             }
                                544                 :                :         }
                                545                 :                :     }
                                546                 :                : 
                                547                 :                :     /* Return selected portion of string */
 6729                           548                 :          22899 :     return cstring_to_text_with_len(string, stringlen);
                                549                 :                : }
                                550                 :                : 
                                551                 :                : /*
                                552                 :                :  * Common implementation for bytea versions of btrim, ltrim, rtrim
                                553                 :                :  */
                                554                 :                : bytea *
 2047                           555                 :             30 : dobyteatrim(bytea *string, bytea *set, bool doltrim, bool dortrim)
                                556                 :                : {
                                557                 :                :     bytea      *ret;
                                558                 :                :     char       *ptr,
                                559                 :                :                *end,
                                560                 :                :                *ptr2,
                                561                 :                :                *ptr2start,
                                562                 :                :                *end2;
                                563                 :                :     int         m,
                                564                 :                :                 stringlen,
                                565                 :                :                 setlen;
                                566                 :                : 
 6915                           567                 :             30 :     stringlen = VARSIZE_ANY_EXHDR(string);
                                568                 :             30 :     setlen = VARSIZE_ANY_EXHDR(set);
                                569                 :                : 
                                570   [ +  +  +  + ]:             30 :     if (stringlen <= 0 || setlen <= 0)
 2047                           571                 :             10 :         return string;
                                572                 :                : 
 6915                           573                 :             20 :     m = stringlen;
                                574                 :             20 :     ptr = VARDATA_ANY(string);
                                575                 :             20 :     end = ptr + stringlen - 1;
                                576                 :             20 :     ptr2start = VARDATA_ANY(set);
                                577                 :             20 :     end2 = ptr2start + setlen - 1;
                                578                 :                : 
 2047                           579         [ +  + ]:             20 :     if (doltrim)
                                580                 :                :     {
                                581         [ +  - ]:             30 :         while (m > 0)
                                582                 :                :         {
                                583                 :             30 :             ptr2 = ptr2start;
                                584         [ +  + ]:             45 :             while (ptr2 <= end2)
                                585                 :                :             {
                                586         [ +  + ]:             30 :                 if (*ptr == *ptr2)
                                587                 :             15 :                     break;
                                588                 :             15 :                 ++ptr2;
                                589                 :                :             }
                                590         [ +  + ]:             30 :             if (ptr2 > end2)
 9113 bruce@momjian.us          591                 :             15 :                 break;
 2047 tgl@sss.pgh.pa.us         592                 :             15 :             ptr++;
                                593                 :             15 :             m--;
                                594                 :                :         }
                                595                 :                :     }
                                596                 :                : 
                                597         [ +  + ]:             20 :     if (dortrim)
                                598                 :                :     {
                                599         [ +  - ]:             30 :         while (m > 0)
                                600                 :                :         {
                                601                 :             30 :             ptr2 = ptr2start;
                                602         [ +  + ]:             45 :             while (ptr2 <= end2)
                                603                 :                :             {
                                604         [ +  + ]:             30 :                 if (*end == *ptr2)
                                605                 :             15 :                     break;
                                606                 :             15 :                 ++ptr2;
                                607                 :                :             }
                                608         [ +  + ]:             30 :             if (ptr2 > end2)
 9113 bruce@momjian.us          609                 :             15 :                 break;
 2047 tgl@sss.pgh.pa.us         610                 :             15 :             end--;
                                611                 :             15 :             m--;
                                612                 :                :         }
                                613                 :                :     }
                                614                 :                : 
 9113 bruce@momjian.us          615                 :             20 :     ret = (bytea *) palloc(VARHDRSZ + m);
 7121 tgl@sss.pgh.pa.us         616                 :             20 :     SET_VARSIZE(ret, VARHDRSZ + m);
 9113 bruce@momjian.us          617                 :             20 :     memcpy(VARDATA(ret), ptr, m);
 2047 tgl@sss.pgh.pa.us         618                 :             20 :     return ret;
                                619                 :                : }
                                620                 :                : 
                                621                 :                : /********************************************************************
                                622                 :                :  *
                                623                 :                :  * byteatrim
                                624                 :                :  *
                                625                 :                :  * Syntax:
                                626                 :                :  *
                                627                 :                :  *   bytea byteatrim(bytea string, bytea set)
                                628                 :                :  *
                                629                 :                :  * Purpose:
                                630                 :                :  *
                                631                 :                :  *   Returns string with characters removed from the front and back
                                632                 :                :  *   up to the first character not in set.
                                633                 :                :  *
                                634                 :                :  * Cloned from btrim and modified as required.
                                635                 :                :  ********************************************************************/
                                636                 :                : 
                                637                 :                : Datum
                                638                 :             20 : byteatrim(PG_FUNCTION_ARGS)
                                639                 :                : {
                                640                 :             20 :     bytea      *string = PG_GETARG_BYTEA_PP(0);
                                641                 :             20 :     bytea      *set = PG_GETARG_BYTEA_PP(1);
                                642                 :                :     bytea      *ret;
                                643                 :                : 
                                644                 :             20 :     ret = dobyteatrim(string, set, true, true);
                                645                 :                : 
                                646                 :             20 :     PG_RETURN_BYTEA_P(ret);
                                647                 :                : }
                                648                 :                : 
                                649                 :                : /********************************************************************
                                650                 :                :  *
                                651                 :                :  * bytealtrim
                                652                 :                :  *
                                653                 :                :  * Syntax:
                                654                 :                :  *
                                655                 :                :  *   bytea bytealtrim(bytea string, bytea set)
                                656                 :                :  *
                                657                 :                :  * Purpose:
                                658                 :                :  *
                                659                 :                :  *   Returns string with initial characters removed up to the first
                                660                 :                :  *   character not in set.
                                661                 :                :  *
                                662                 :                :  ********************************************************************/
                                663                 :                : 
                                664                 :                : Datum
                                665                 :              5 : bytealtrim(PG_FUNCTION_ARGS)
                                666                 :                : {
                                667                 :              5 :     bytea      *string = PG_GETARG_BYTEA_PP(0);
                                668                 :              5 :     bytea      *set = PG_GETARG_BYTEA_PP(1);
                                669                 :                :     bytea      *ret;
                                670                 :                : 
                                671                 :              5 :     ret = dobyteatrim(string, set, true, false);
                                672                 :                : 
                                673                 :              5 :     PG_RETURN_BYTEA_P(ret);
                                674                 :                : }
                                675                 :                : 
                                676                 :                : /********************************************************************
                                677                 :                :  *
                                678                 :                :  * byteartrim
                                679                 :                :  *
                                680                 :                :  * Syntax:
                                681                 :                :  *
                                682                 :                :  *   bytea byteartrim(bytea string, bytea set)
                                683                 :                :  *
                                684                 :                :  * Purpose:
                                685                 :                :  *
                                686                 :                :  *   Returns string with final characters removed after the last
                                687                 :                :  *   character not in set.
                                688                 :                :  *
                                689                 :                :  ********************************************************************/
                                690                 :                : 
                                691                 :                : Datum
                                692                 :              5 : byteartrim(PG_FUNCTION_ARGS)
                                693                 :                : {
                                694                 :              5 :     bytea      *string = PG_GETARG_BYTEA_PP(0);
                                695                 :              5 :     bytea      *set = PG_GETARG_BYTEA_PP(1);
                                696                 :                :     bytea      *ret;
                                697                 :                : 
                                698                 :              5 :     ret = dobyteatrim(string, set, false, true);
                                699                 :                : 
 9113 bruce@momjian.us          700                 :              5 :     PG_RETURN_BYTEA_P(ret);
                                701                 :                : }
                                702                 :                : 
                                703                 :                : /********************************************************************
                                704                 :                :  *
                                705                 :                :  * ltrim
                                706                 :                :  *
                                707                 :                :  * Syntax:
                                708                 :                :  *
                                709                 :                :  *   text ltrim(text string, text set)
                                710                 :                :  *
                                711                 :                :  * Purpose:
                                712                 :                :  *
                                713                 :                :  *   Returns string with initial characters removed up to the first
                                714                 :                :  *   character not in set.
                                715                 :                :  *
                                716                 :                :  ********************************************************************/
                                717                 :                : 
                                718                 :                : Datum
 9548 tgl@sss.pgh.pa.us         719                 :          18616 : ltrim(PG_FUNCTION_ARGS)
                                720                 :                : {
 6915                           721                 :          18616 :     text       *string = PG_GETARG_TEXT_PP(0);
                                722                 :          18616 :     text       *set = PG_GETARG_TEXT_PP(1);
                                723                 :                :     text       *ret;
                                724                 :                : 
                                725                 :          18616 :     ret = dotrim(VARDATA_ANY(string), VARSIZE_ANY_EXHDR(string),
                                726                 :          18616 :                  VARDATA_ANY(set), VARSIZE_ANY_EXHDR(set),
                                727                 :                :                  true, false);
                                728                 :                : 
 8497                           729                 :          18616 :     PG_RETURN_TEXT_P(ret);
                                730                 :                : }
                                731                 :                : 
                                732                 :                : /********************************************************************
                                733                 :                :  *
                                734                 :                :  * ltrim1 --- ltrim with set fixed as ' '
                                735                 :                :  *
                                736                 :                :  ********************************************************************/
                                737                 :                : 
                                738                 :                : Datum
                                739                 :             48 : ltrim1(PG_FUNCTION_ARGS)
                                740                 :                : {
 6915                           741                 :             48 :     text       *string = PG_GETARG_TEXT_PP(0);
                                742                 :                :     text       *ret;
                                743                 :                : 
                                744                 :             48 :     ret = dotrim(VARDATA_ANY(string), VARSIZE_ANY_EXHDR(string),
                                745                 :                :                  " ", 1,
                                746                 :                :                  true, false);
                                747                 :                : 
 9548                           748                 :             48 :     PG_RETURN_TEXT_P(ret);
                                749                 :                : }
                                750                 :                : 
                                751                 :                : /********************************************************************
                                752                 :                :  *
                                753                 :                :  * rtrim
                                754                 :                :  *
                                755                 :                :  * Syntax:
                                756                 :                :  *
                                757                 :                :  *   text rtrim(text string, text set)
                                758                 :                :  *
                                759                 :                :  * Purpose:
                                760                 :                :  *
                                761                 :                :  *   Returns string with final characters removed after the last
                                762                 :                :  *   character not in set.
                                763                 :                :  *
                                764                 :                :  ********************************************************************/
                                765                 :                : 
                                766                 :                : Datum
                                767                 :            216 : rtrim(PG_FUNCTION_ARGS)
                                768                 :                : {
 6915                           769                 :            216 :     text       *string = PG_GETARG_TEXT_PP(0);
                                770                 :            216 :     text       *set = PG_GETARG_TEXT_PP(1);
                                771                 :                :     text       *ret;
                                772                 :                : 
                                773                 :            216 :     ret = dotrim(VARDATA_ANY(string), VARSIZE_ANY_EXHDR(string),
                                774                 :            216 :                  VARDATA_ANY(set), VARSIZE_ANY_EXHDR(set),
                                775                 :                :                  false, true);
                                776                 :                : 
 8497                           777                 :            216 :     PG_RETURN_TEXT_P(ret);
                                778                 :                : }
                                779                 :                : 
                                780                 :                : /********************************************************************
                                781                 :                :  *
                                782                 :                :  * rtrim1 --- rtrim with set fixed as ' '
                                783                 :                :  *
                                784                 :                :  ********************************************************************/
                                785                 :                : 
                                786                 :                : Datum
                                787                 :           3616 : rtrim1(PG_FUNCTION_ARGS)
                                788                 :                : {
 6915                           789                 :           3616 :     text       *string = PG_GETARG_TEXT_PP(0);
                                790                 :                :     text       *ret;
                                791                 :                : 
                                792                 :           3616 :     ret = dotrim(VARDATA_ANY(string), VARSIZE_ANY_EXHDR(string),
                                793                 :                :                  " ", 1,
                                794                 :                :                  false, true);
                                795                 :                : 
 9548                           796                 :           3616 :     PG_RETURN_TEXT_P(ret);
                                797                 :                : }
                                798                 :                : 
                                799                 :                : 
                                800                 :                : /********************************************************************
                                801                 :                :  *
                                802                 :                :  * translate
                                803                 :                :  *
                                804                 :                :  * Syntax:
                                805                 :                :  *
                                806                 :                :  *   text translate(text string, text from, text to)
                                807                 :                :  *
                                808                 :                :  * Purpose:
                                809                 :                :  *
                                810                 :                :  *   Returns string after replacing all occurrences of characters in from
                                811                 :                :  *   with the corresponding character in to.  If from is longer than to,
                                812                 :                :  *   occurrences of the extra characters in from are deleted.
                                813                 :                :  *   Improved by Edwin Ramirez <ramirez@doc.mssm.edu>.
                                814                 :                :  *
                                815                 :                :  ********************************************************************/
                                816                 :                : 
                                817                 :                : Datum
                                818                 :             23 : translate(PG_FUNCTION_ARGS)
                                819                 :                : {
 6915                           820                 :             23 :     text       *string = PG_GETARG_TEXT_PP(0);
                                821                 :             23 :     text       *from = PG_GETARG_TEXT_PP(1);
                                822                 :             23 :     text       *to = PG_GETARG_TEXT_PP(2);
                                823                 :                :     text       *result;
                                824                 :                :     char       *from_ptr,
                                825                 :                :                *to_ptr,
                                826                 :                :                *to_end;
                                827                 :                :     char       *source,
                                828                 :                :                *target;
                                829                 :                :     const char *source_end;
                                830                 :                :     const char *from_end;
                                831                 :                :     int         m,
                                832                 :                :                 fromlen,
                                833                 :                :                 tolen,
                                834                 :                :                 retlen,
                                835                 :                :                 i;
                                836                 :                :     int         bytelen;
                                837                 :                :     int         len;
                                838                 :                :     int         source_len;
                                839                 :                :     int         from_index;
                                840                 :                : 
                                841                 :             23 :     m = VARSIZE_ANY_EXHDR(string);
                                842         [ +  + ]:             23 :     if (m <= 0)
 9548                           843                 :              5 :         PG_RETURN_TEXT_P(string);
 6914                           844                 :             18 :     source = VARDATA_ANY(string);
  232 tmunro@postgresql.or      845                 :             18 :     source_end = source + m;
                                846                 :                : 
 6915 tgl@sss.pgh.pa.us         847                 :             18 :     fromlen = VARSIZE_ANY_EXHDR(from);
                                848                 :             18 :     from_ptr = VARDATA_ANY(from);
  232 tmunro@postgresql.or      849                 :             18 :     from_end = from_ptr + fromlen;
 6915 tgl@sss.pgh.pa.us         850                 :             18 :     tolen = VARSIZE_ANY_EXHDR(to);
                                851                 :             18 :     to_ptr = VARDATA_ANY(to);
 1275                           852                 :             18 :     to_end = to_ptr + tolen;
                                853                 :                : 
                                854                 :                :     /*
                                855                 :                :      * The worst-case expansion is to substitute a max-length character for a
                                856                 :                :      * single-byte character at each position of the string.
                                857                 :                :      */
 1554                           858         [ +  - ]:             18 :     if (unlikely(pg_mul_s32_overflow(pg_database_encoding_max_length(), m,
                                859                 :             18 :                                      &bytelen)) ||
                                860         [ +  - ]:             18 :         unlikely(pg_add_s32_overflow(bytelen, VARHDRSZ, &bytelen)) ||
                                861         [ -  + ]:             18 :         unlikely(!AllocSizeIsValid(bytelen)))
 6914 tgl@sss.pgh.pa.us         862         [ #  # ]:UBC           0 :         ereport(ERROR,
                                863                 :                :                 (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
                                864                 :                :                  errmsg("requested length too large")));
                                865                 :                : 
 1554 tgl@sss.pgh.pa.us         866                 :CBC          18 :     result = (text *) palloc(bytelen);
                                867                 :                : 
 9661                           868                 :             18 :     target = VARDATA(result);
 9662 lockhart@fourpalms.o      869                 :             18 :     retlen = 0;
                                870                 :                : 
 9104 ishii@postgresql.org      871         [ +  + ]:            164 :     while (m > 0)
                                872                 :                :     {
  232 tmunro@postgresql.or      873                 :            146 :         source_len = pg_mblen_range(source, source_end);
 9104 ishii@postgresql.org      874                 :            146 :         from_index = 0;
                                875                 :                : 
                                876         [ +  + ]:            394 :         for (i = 0; i < fromlen; i += len)
                                877                 :                :         {
  232 tmunro@postgresql.or      878                 :            289 :             len = pg_mblen_range(&from_ptr[i], from_end);
 9072 bruce@momjian.us          879         [ +  - ]:            289 :             if (len == source_len &&
                                880         [ +  + ]:            289 :                 memcmp(source, &from_ptr[i], len) == 0)
                                881                 :             41 :                 break;
                                882                 :                : 
                                883                 :            248 :             from_index++;
                                884                 :                :         }
 9104 ishii@postgresql.org      885         [ +  + ]:            146 :         if (i < fromlen)
                                886                 :                :         {
                                887                 :                :             /* substitute, or delete if no corresponding "to" character */
 9072 bruce@momjian.us          888                 :             41 :             char       *p = to_ptr;
                                889                 :                : 
                                890         [ +  + ]:             64 :             for (i = 0; i < from_index; i++)
                                891                 :                :             {
 1275 tgl@sss.pgh.pa.us         892         [ +  + ]:             28 :                 if (p >= to_end)
 9072 bruce@momjian.us          893                 :              5 :                     break;
  232 tmunro@postgresql.or      894                 :             23 :                 p += pg_mblen_range(p, to_end);
                                895                 :                :             }
 1275 tgl@sss.pgh.pa.us         896         [ +  + ]:             41 :             if (p < to_end)
                                897                 :                :             {
  232 tmunro@postgresql.or      898                 :             31 :                 len = pg_mblen_range(p, to_end);
 9072 bruce@momjian.us          899                 :             31 :                 memcpy(target, p, len);
                                900                 :             31 :                 target += len;
                                901                 :             31 :                 retlen += len;
                                902                 :                :             }
                                903                 :                :         }
                                904                 :                :         else
                                905                 :                :         {
                                906                 :                :             /* no match, so copy */
                                907                 :            105 :             memcpy(target, source, source_len);
                                908                 :            105 :             target += source_len;
                                909                 :            105 :             retlen += source_len;
                                910                 :                :         }
                                911                 :                : 
 9104 ishii@postgresql.org      912                 :            146 :         source += source_len;
                                913                 :            146 :         m -= source_len;
                                914                 :                :     }
                                915                 :                : 
 7121 tgl@sss.pgh.pa.us         916                 :             18 :     SET_VARSIZE(result, retlen + VARHDRSZ);
                                917                 :                : 
                                918                 :                :     /*
                                919                 :                :      * The function result is probably much bigger than needed, if we're using
                                920                 :                :      * a multibyte encoding, but it's not worth reallocating it; the result
                                921                 :                :      * probably won't live long anyway.
                                922                 :                :      */
                                923                 :                : 
 9548                           924                 :             18 :     PG_RETURN_TEXT_P(result);
                                925                 :                : }
                                926                 :                : 
                                927                 :                : /********************************************************************
                                928                 :                :  *
                                929                 :                :  * ascii
                                930                 :                :  *
                                931                 :                :  * Syntax:
                                932                 :                :  *
                                933                 :                :  *   int ascii(text string)
                                934                 :                :  *
                                935                 :                :  * Purpose:
                                936                 :                :  *
                                937                 :                :  *   Returns the decimal representation of the first character from
                                938                 :                :  *   string.
                                939                 :                :  *   If the string is empty we return 0.
                                940                 :                :  *   If the database encoding is UTF8, we return the Unicode codepoint.
                                941                 :                :  *   If the database encoding is any other multi-byte encoding, we
                                942                 :                :  *   return the value of the first byte if it is an ASCII character
                                943                 :                :  *   (range 1 .. 127), or raise an error.
                                944                 :                :  *   For all other encodings we return the value of the first byte,
                                945                 :                :  *   (range 1..255).
                                946                 :                :  *
                                947                 :                :  ********************************************************************/
                                948                 :                : 
                                949                 :                : Datum
                                950                 :            111 : ascii(PG_FUNCTION_ARGS)
                                951                 :                : {
 6915                           952                 :            111 :     text       *string = PG_GETARG_TEXT_PP(0);
 6860 bruce@momjian.us          953                 :            111 :     int         encoding = GetDatabaseEncoding();
                                954                 :                :     unsigned char *data;
                                955                 :                :     int         len;
                                956                 :                : 
   17 michael@paquier.xyz       957                 :            111 :     len = VARSIZE_ANY_EXHDR(string);
                                958         [ +  + ]:            111 :     if (len <= 0)
 9548 tgl@sss.pgh.pa.us         959                 :              5 :         PG_RETURN_INT32(0);
                                960                 :                : 
 6915                           961                 :            106 :     data = (unsigned char *) VARDATA_ANY(string);
                                962                 :                : 
 6918 andrew@dunslane.net       963   [ +  -  +  + ]:            106 :     if (encoding == PG_UTF8 && *data > 127)
                                964                 :                :     {
                                965                 :                :         /* return the code point for Unicode */
                                966                 :                : 
 6860 bruce@momjian.us          967                 :             32 :         int         result = 0,
                                968                 :             32 :                     tbytes = 0,
                                969                 :                :                     i;
                                970                 :                : 
 6918 andrew@dunslane.net       971         [ +  + ]:             32 :         if (*data >= 0xF0)
                                972                 :                :         {
                                973                 :             12 :             result = *data & 0x07;
                                974                 :             12 :             tbytes = 3;
                                975                 :                :         }
                                976         [ +  + ]:             20 :         else if (*data >= 0xE0)
                                977                 :                :         {
                                978                 :              8 :             result = *data & 0x0F;
                                979                 :              8 :             tbytes = 2;
                                980                 :                :         }
   17 michael@paquier.xyz       981         [ +  + ]:             12 :         else if (*data > 0xC0)
                                982                 :                :         {
 6918 andrew@dunslane.net       983                 :              8 :             result = *data & 0x1f;
                                984                 :              8 :             tbytes = 1;
                                985                 :                :         }
                                986                 :                :         else
   17 michael@paquier.xyz       987         [ +  - ]:              4 :             ereport(ERROR,
                                988                 :                :                     (errcode(ERRCODE_CHARACTER_NOT_IN_REPERTOIRE),
                                989                 :                :                      errmsg("invalid byte sequence for encoding \"%s\"",
                                990                 :                :                             GetDatabaseEncodingName())));
                                991                 :                : 
                                992                 :                :         /* All continuation bytes are present in the input */
                                993         [ +  + ]:             28 :         if (tbytes >= len)
                                994         [ +  - ]:             24 :             ereport(ERROR,
                                995                 :                :                     (errcode(ERRCODE_CHARACTER_NOT_IN_REPERTOIRE),
                                996                 :                :                      errmsg("invalid byte sequence for encoding \"%s\"",
                                997                 :                :                             GetDatabaseEncodingName())));
                                998                 :                : 
 6918 andrew@dunslane.net       999         [ +  - ]:              4 :         for (i = 1; i <= tbytes; i++)
                               1000                 :                :         {
   17 michael@paquier.xyz      1001         [ +  - ]:              4 :             if (unlikely((data[i] & 0xC0) != 0x80))
                               1002         [ +  - ]:              4 :                 ereport(ERROR,
                               1003                 :                :                         (errcode(ERRCODE_CHARACTER_NOT_IN_REPERTOIRE),
                               1004                 :                :                          errmsg("invalid byte sequence for encoding \"%s\"",
                               1005                 :                :                                 GetDatabaseEncodingName())));
 6918 andrew@dunslane.net      1006                 :UBC           0 :             result = (result << 6) + (data[i] & 0x3f);
                               1007                 :                :         }
                               1008                 :                : 
                               1009                 :              0 :         PG_RETURN_INT32(result);
                               1010                 :                :     }
                               1011                 :                :     else
                               1012                 :                :     {
 6918 andrew@dunslane.net      1013   [ +  -  -  + ]:CBC          74 :         if (pg_encoding_max_length(encoding) > 1 && *data > 127)
 6918 andrew@dunslane.net      1014         [ #  # ]:UBC           0 :             ereport(ERROR,
                               1015                 :                :                     (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
                               1016                 :                :                      errmsg("requested character too large")));
                               1017                 :                : 
                               1018                 :                : 
 6918 andrew@dunslane.net      1019                 :CBC          74 :         PG_RETURN_INT32((int32) *data);
                               1020                 :                :     }
                               1021                 :                : }
                               1022                 :                : 
                               1023                 :                : /********************************************************************
                               1024                 :                :  *
                               1025                 :                :  * chr
                               1026                 :                :  *
                               1027                 :                :  * Syntax:
                               1028                 :                :  *
                               1029                 :                :  *   text chr(int val)
                               1030                 :                :  *
                               1031                 :                :  * Purpose:
                               1032                 :                :  *
                               1033                 :                :  *  Returns the character having the binary equivalent to val.
                               1034                 :                :  *
                               1035                 :                :  * For UTF8 we treat the argument as a Unicode code point.
                               1036                 :                :  * For other multi-byte encodings we raise an error for arguments
                               1037                 :                :  * outside the strict ASCII range (1..127).
                               1038                 :                :  *
                               1039                 :                :  * It's important that we don't ever return a value that is not valid
                               1040                 :                :  * in the database encoding, so that this doesn't become a way for
                               1041                 :                :  * invalid data to enter the database.
                               1042                 :                :  *
                               1043                 :                :  ********************************************************************/
                               1044                 :                : 
                               1045                 :                : Datum
 6860 bruce@momjian.us         1046                 :         326029 : chr         (PG_FUNCTION_ARGS)
                               1047                 :                : {
 1725 peter@eisentraut.org     1048                 :         326029 :     int32       arg = PG_GETARG_INT32(0);
                               1049                 :                :     uint32      cvalue;
                               1050                 :                :     text       *result;
 6860 bruce@momjian.us         1051                 :         326029 :     int         encoding = GetDatabaseEncoding();
                               1052                 :                : 
                               1053                 :                :     /*
                               1054                 :                :      * Error out on arguments that make no sense or that we can't validly
                               1055                 :                :      * represent in the encoding.
                               1056                 :                :      */
 1725 peter@eisentraut.org     1057         [ -  + ]:         326029 :     if (arg < 0)
 1725 peter@eisentraut.org     1058         [ #  # ]:UBC           0 :         ereport(ERROR,
                               1059                 :                :                 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                               1060                 :                :                  errmsg("character number must be positive")));
 1725 peter@eisentraut.org     1061         [ +  + ]:CBC      326029 :     else if (arg == 0)
                               1062         [ +  - ]:              4 :         ereport(ERROR,
                               1063                 :                :                 (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
                               1064                 :                :                  errmsg("null character not permitted")));
                               1065                 :                : 
                               1066                 :         326025 :     cvalue = arg;
                               1067                 :                : 
 6918 andrew@dunslane.net      1068   [ +  -  -  + ]:         326025 :     if (encoding == PG_UTF8 && cvalue > 127)
 6918 andrew@dunslane.net      1069                 :UBC           0 :     {
                               1070                 :                :         /* for Unicode we treat the argument as a code point */
                               1071                 :                :         int         bytes;
                               1072                 :                :         unsigned char *wch;
                               1073                 :                : 
                               1074                 :                :         /*
                               1075                 :                :          * We only allow valid Unicode code points; per RFC3629 that stops at
                               1076                 :                :          * U+10FFFF, even though 4-byte UTF8 sequences can hold values up to
                               1077                 :                :          * U+1FFFFF.
                               1078                 :                :          */
 4486 tgl@sss.pgh.pa.us        1079         [ #  # ]:              0 :         if (cvalue > 0x0010ffff)
 6918 andrew@dunslane.net      1080         [ #  # ]:              0 :             ereport(ERROR,
                               1081                 :                :                     (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
                               1082                 :                :                      errmsg("requested character too large for encoding: %u",
                               1083                 :                :                             cvalue)));
                               1084                 :                : 
                               1085         [ #  # ]:              0 :         if (cvalue > 0xffff)
                               1086                 :              0 :             bytes = 4;
                               1087         [ #  # ]:              0 :         else if (cvalue > 0x07ff)
                               1088                 :              0 :             bytes = 3;
                               1089                 :                :         else
                               1090                 :              0 :             bytes = 2;
                               1091                 :                : 
                               1092                 :              0 :         result = (text *) palloc(VARHDRSZ + bytes);
                               1093                 :              0 :         SET_VARSIZE(result, VARHDRSZ + bytes);
 4486 tgl@sss.pgh.pa.us        1094                 :              0 :         wch = (unsigned char *) VARDATA(result);
                               1095                 :                : 
 6918 andrew@dunslane.net      1096         [ #  # ]:              0 :         if (bytes == 2)
                               1097                 :                :         {
                               1098                 :              0 :             wch[0] = 0xC0 | ((cvalue >> 6) & 0x1F);
 4167 heikki.linnakangas@i     1099                 :              0 :             wch[1] = 0x80 | (cvalue & 0x3F);
                               1100                 :                :         }
 6918 andrew@dunslane.net      1101         [ #  # ]:              0 :         else if (bytes == 3)
                               1102                 :                :         {
                               1103                 :              0 :             wch[0] = 0xE0 | ((cvalue >> 12) & 0x0F);
                               1104                 :              0 :             wch[1] = 0x80 | ((cvalue >> 6) & 0x3F);
                               1105                 :              0 :             wch[2] = 0x80 | (cvalue & 0x3F);
                               1106                 :                :         }
                               1107                 :                :         else
                               1108                 :                :         {
                               1109                 :              0 :             wch[0] = 0xF0 | ((cvalue >> 18) & 0x07);
                               1110                 :              0 :             wch[1] = 0x80 | ((cvalue >> 12) & 0x3F);
                               1111                 :              0 :             wch[2] = 0x80 | ((cvalue >> 6) & 0x3F);
                               1112                 :              0 :             wch[3] = 0x80 | (cvalue & 0x3F);
                               1113                 :                :         }
                               1114                 :                : 
                               1115                 :                :         /*
                               1116                 :                :          * The preceding range check isn't sufficient, because UTF8 excludes
                               1117                 :                :          * Unicode "surrogate pair" codes.  Make sure what we created is valid
                               1118                 :                :          * UTF8.
                               1119                 :                :          */
 4486 tgl@sss.pgh.pa.us        1120         [ #  # ]:              0 :         if (!pg_utf8_islegal(wch, bytes))
                               1121         [ #  # ]:              0 :             ereport(ERROR,
                               1122                 :                :                     (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
                               1123                 :                :                      errmsg("requested character not valid for encoding: %u",
                               1124                 :                :                             cvalue)));
                               1125                 :                :     }
                               1126                 :                :     else
                               1127                 :                :     {
                               1128                 :                :         bool        is_mb;
                               1129                 :                : 
 6918 andrew@dunslane.net      1130                 :CBC      326025 :         is_mb = pg_encoding_max_length(encoding) > 1;
                               1131                 :                : 
 6827                          1132   [ +  -  +  -  :         326025 :         if ((is_mb && (cvalue > 127)) || (!is_mb && (cvalue > 255)))
                                        -  +  -  - ]
 6918 andrew@dunslane.net      1133         [ #  # ]:UBC           0 :             ereport(ERROR,
                               1134                 :                :                     (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
                               1135                 :                :                      errmsg("requested character too large for encoding: %u",
                               1136                 :                :                             cvalue)));
                               1137                 :                : 
 6918 andrew@dunslane.net      1138                 :CBC      326025 :         result = (text *) palloc(VARHDRSZ + 1);
                               1139                 :         326025 :         SET_VARSIZE(result, VARHDRSZ + 1);
                               1140                 :         326025 :         *VARDATA(result) = (char) cvalue;
                               1141                 :                :     }
                               1142                 :                : 
 9571 tgl@sss.pgh.pa.us        1143                 :         326025 :     PG_RETURN_TEXT_P(result);
                               1144                 :                : }
                               1145                 :                : 
                               1146                 :                : /********************************************************************
                               1147                 :                :  *
                               1148                 :                :  * repeat
                               1149                 :                :  *
                               1150                 :                :  * Syntax:
                               1151                 :                :  *
                               1152                 :                :  *   text repeat(text string, int val)
                               1153                 :                :  *
                               1154                 :                :  * Purpose:
                               1155                 :                :  *
                               1156                 :                :  *  Repeat string by val.
                               1157                 :                :  *
                               1158                 :                :  ********************************************************************/
                               1159                 :                : 
                               1160                 :                : Datum
                               1161                 :          28544 : repeat(PG_FUNCTION_ARGS)
                               1162                 :                : {
 6915                          1163                 :          28544 :     text       *string = PG_GETARG_TEXT_PP(0);
 9289 bruce@momjian.us         1164                 :          28544 :     int32       count = PG_GETARG_INT32(1);
                               1165                 :                :     text       *result;
                               1166                 :                :     int         slen,
                               1167                 :                :                 tlen;
                               1168                 :                :     int         i;
                               1169                 :                :     char       *cp,
                               1170                 :                :                *sp;
                               1171                 :                : 
 9638 lockhart@fourpalms.o     1172         [ +  + ]:          28544 :     if (count < 0)
                               1173                 :              5 :         count = 0;
                               1174                 :                : 
 6915 tgl@sss.pgh.pa.us        1175                 :          28544 :     slen = VARSIZE_ANY_EXHDR(string);
                               1176                 :                : 
 3180 andres@anarazel.de       1177         [ +  - ]:          28544 :     if (unlikely(pg_mul_s32_overflow(count, slen, &tlen)) ||
 1554 tgl@sss.pgh.pa.us        1178         [ +  - ]:          28544 :         unlikely(pg_add_s32_overflow(tlen, VARHDRSZ, &tlen)) ||
                               1179         [ -  + ]:          28544 :         unlikely(!AllocSizeIsValid(tlen)))
 3180 andres@anarazel.de       1180         [ #  # ]:UBC           0 :         ereport(ERROR,
                               1181                 :                :                 (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
                               1182                 :                :                  errmsg("requested length too large")));
                               1183                 :                : 
 9638 lockhart@fourpalms.o     1184                 :CBC       28544 :     result = (text *) palloc(tlen);
                               1185                 :                : 
 7121 tgl@sss.pgh.pa.us        1186                 :          28544 :     SET_VARSIZE(result, tlen);
 9638 lockhart@fourpalms.o     1187                 :          28544 :     cp = VARDATA(result);
 6915 tgl@sss.pgh.pa.us        1188                 :          28544 :     sp = VARDATA_ANY(string);
 9638 lockhart@fourpalms.o     1189         [ +  + ]:       29114681 :     for (i = 0; i < count; i++)
                               1190                 :                :     {
 6915 tgl@sss.pgh.pa.us        1191                 :       29086137 :         memcpy(cp, sp, slen);
 9638 lockhart@fourpalms.o     1192                 :       29086137 :         cp += slen;
 2282 mail@joeconway.com       1193         [ -  + ]:       29086137 :         CHECK_FOR_INTERRUPTS();
                               1194                 :                :     }
                               1195                 :                : 
 9571 tgl@sss.pgh.pa.us        1196                 :          28544 :     PG_RETURN_TEXT_P(result);
                               1197                 :                : }
        

Generated by: LCOV version 2.0-1