LCOV - differential code coverage report
Current view: top level - src/backend/utils/adt - pg_locale_builtin.c (source / functions) Coverage Total Hit UNC UBC GNC CBC DCB
Current: 603a3335f2b60b2c798da5c627b3bb288b92a7bd vs e395fbd32a07557de4ac98088928c1749d4845d8 Lines: 80.7 % 114 92 8 14 16 76 8
Current Date: 2026-07-25 17:13:00 -0400 Functions: 81.8 % 22 18 4 4 14
Baseline: lcov-20260726-baseline Branches: 60.0 % 40 24 4 12 4 20
Baseline Date: 2026-07-25 19:16:42 +0200 Line coverage date bins:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
(7,30] days: 69.4 % 36 25 8 3 16 9
(30,360] days: 82.8 % 29 24 5 24
(360..) days: 87.8 % 49 43 6 43
Function coverage date bins:
(30,360] days: 87.5 % 8 7 1 4 3
(360..) days: 78.6 % 14 11 3 11
Branch coverage date bins:
(7,30] days: 64.3 % 14 9 4 1 4 5
(30,360] days: 66.7 % 6 4 2 4
(360..) days: 55.0 % 20 11 9 11

 Age         Owner                    Branch data    TLA  Line data    Source code
                                  1                 :                : /*-----------------------------------------------------------------------
                                  2                 :                :  *
                                  3                 :                :  * PostgreSQL locale utilities for builtin provider
                                  4                 :                :  *
                                  5                 :                :  * Portions Copyright (c) 2002-2026, PostgreSQL Global Development Group
                                  6                 :                :  *
                                  7                 :                :  * src/backend/utils/adt/pg_locale_builtin.c
                                  8                 :                :  *
                                  9                 :                :  *-----------------------------------------------------------------------
                                 10                 :                :  */
                                 11                 :                : 
                                 12                 :                : #include "postgres.h"
                                 13                 :                : 
                                 14                 :                : #include "catalog/pg_database.h"
                                 15                 :                : #include "catalog/pg_collation.h"
                                 16                 :                : #include "common/unicode_case.h"
                                 17                 :                : #include "common/unicode_category.h"
                                 18                 :                : #include "common/unicode_limits.h"
                                 19                 :                : #include "miscadmin.h"
                                 20                 :                : #include "utils/builtins.h"
                                 21                 :                : #include "utils/memutils.h"
                                 22                 :                : #include "utils/pg_locale.h"
                                 23                 :                : #include "utils/syscache.h"
                                 24                 :                : 
                                 25                 :                : /*
                                 26                 :                :  * The largest text value must fit in MaxAllocSize, but then may grow during
                                 27                 :                :  * case mapping. While the resulting string will not be representable as a new
                                 28                 :                :  * text value, we must at least be sure not to overflow a size_t while
                                 29                 :                :  * processing it.
                                 30                 :                :  */
                                 31                 :                : StaticAssertDecl(SIZE_MAX / UTF8_MAX_CASEMAP_EXPANSION > MaxAllocSize,
                                 32                 :                :                  "case mapping may overflow size_t");
                                 33                 :                : 
                                 34                 :                : extern pg_locale_t create_pg_locale_builtin(Oid collid,
                                 35                 :                :                                             MemoryContext context);
                                 36                 :                : extern char *get_collation_actual_version_builtin(const char *collcollate);
                                 37                 :                : 
                                 38                 :                : struct WordBoundaryState
                                 39                 :                : {
                                 40                 :                :     const char *str;
                                 41                 :                :     size_t      len;
                                 42                 :                :     size_t      offset;
                                 43                 :                :     bool        posix;
                                 44                 :                :     bool        init;
                                 45                 :                :     bool        prev_alnum;
                                 46                 :                : };
                                 47                 :                : 
                                 48                 :                : /*
                                 49                 :                :  * In UTF-8, pg_wchar is guaranteed to be the code point value.
                                 50                 :                :  */
                                 51                 :                : static inline char32_t
  270 jdavis@postgresql.or       52                 :CBC      129459 : to_char32(pg_wchar wc)
                                 53                 :                : {
                                 54         [ -  + ]:         129459 :     Assert(GetDatabaseEncoding() == PG_UTF8);
                                 55                 :         129459 :     return (char32_t) wc;
                                 56                 :                : }
                                 57                 :                : 
                                 58                 :                : static inline pg_wchar
                                 59                 :            650 : to_pg_wchar(char32_t c32)
                                 60                 :                : {
                                 61         [ -  + ]:            650 :     Assert(GetDatabaseEncoding() == PG_UTF8);
                                 62                 :            650 :     return (pg_wchar) c32;
                                 63                 :                : }
                                 64                 :                : 
                                 65                 :                : /*
                                 66                 :                :  * Simple word boundary iterator that draws boundaries each time the result of
                                 67                 :                :  * pg_u_isalnum() changes.
                                 68                 :                :  */
                                 69                 :                : static size_t
  587                            70                 :            564 : initcap_wbnext(void *state)
                                 71                 :                : {
                                 72                 :            564 :     struct WordBoundaryState *wbstate = (struct WordBoundaryState *) state;
                                 73                 :                : 
   72                            74         [ +  + ]:           1165 :     while (wbstate->offset < wbstate->len)
                                 75                 :                :     {
   19                            76                 :           1032 :         int         ulen = pg_utf_mblen((const unsigned char *) wbstate->str +
  587                            77                 :           1032 :                                         wbstate->offset);
                                 78                 :                :         char32_t    u;
                                 79                 :                :         bool        curr_alnum;
   19                            80                 :           1032 :         size_t      prev_offset = wbstate->offset;
                                 81                 :                : 
                                 82                 :                :         /* invalid UTF8 */
                                 83         [ -  + ]:           1032 :         if (wbstate->offset + ulen > wbstate->len)
                                 84                 :                :         {
   19 jdavis@postgresql.or       85                 :UBC           0 :             wbstate->init = true;
                                 86                 :              0 :             wbstate->offset = wbstate->len;
                                 87                 :              0 :             return prev_offset;
                                 88                 :                :         }
                                 89                 :                : 
   19 jdavis@postgresql.or       90                 :CBC        1032 :         u = utf8_to_unicode((const unsigned char *) wbstate->str +
                                 91                 :           1032 :                             wbstate->offset);
                                 92                 :           1032 :         curr_alnum = pg_u_isalnum(u, wbstate->posix);
                                 93                 :                : 
                                 94   [ +  +  +  + ]:           1032 :         if (!wbstate->init || curr_alnum != wbstate->prev_alnum)
                                 95                 :                :         {
  587                            96                 :            431 :             wbstate->init = true;
   19                            97                 :            431 :             wbstate->offset += ulen;
  587                            98                 :            431 :             wbstate->prev_alnum = curr_alnum;
                                 99                 :            431 :             return prev_offset;
                                100                 :                :         }
                                101                 :                : 
   19                           102                 :            601 :         wbstate->offset += ulen;
                                103                 :                :     }
                                104                 :                : 
  587                           105                 :            133 :     return wbstate->len;
                                106                 :                : }
                                107                 :                : 
                                108                 :                : static size_t
   72                           109                 :           6321 : strlower_builtin(char *dest, size_t destsize, const char *src, size_t srclen,
                                110                 :                :                  pg_locale_t locale)
                                111                 :                : {
                                112                 :                :     size_t      consumed;
                                113                 :                :     size_t      result;
                                114                 :                : 
   19 jdavis@postgresql.or      115                 :GNC        6321 :     result = unicode_strlower(dest, destsize, src, srclen, &consumed,
                                116                 :           6321 :                               locale->builtin.casemap_full);
                                117         [ -  + ]:           6321 :     if (consumed < srclen)
   19 jdavis@postgresql.or      118                 :UNC           0 :         report_invalid_encoding(GetDatabaseEncoding(), src + consumed,
                                119                 :              0 :                                 srclen - consumed);
                                120                 :                : 
   19 jdavis@postgresql.or      121                 :GNC        6321 :     return result;
                                122                 :                : }
                                123                 :                : 
                                124                 :                : static size_t
   72 jdavis@postgresql.or      125                 :CBC         133 : strtitle_builtin(char *dest, size_t destsize, const char *src, size_t srclen,
                                126                 :                :                  pg_locale_t locale)
                                127                 :                : {
  587                           128                 :            133 :     struct WordBoundaryState wbstate = {
                                129                 :                :         .str = src,
                                130                 :                :         .len = srclen,
                                131                 :                :         .offset = 0,
  299 peter@eisentraut.org      132                 :            133 :         .posix = !locale->builtin.casemap_full,
                                133                 :                :         .init = false,
                                134                 :                :         .prev_alnum = false,
                                135                 :                :     };
                                136                 :                :     size_t      consumed;
                                137                 :                :     size_t      result;
                                138                 :                : 
   19 jdavis@postgresql.or      139                 :GNC         133 :     result = unicode_strtitle(dest, destsize, src, srclen, &consumed,
                                140                 :            133 :                               locale->builtin.casemap_full,
                                141                 :                :                               initcap_wbnext, &wbstate);
                                142                 :                : 
                                143         [ -  + ]:            133 :     if (consumed < srclen)
   19 jdavis@postgresql.or      144                 :UNC           0 :         report_invalid_encoding(GetDatabaseEncoding(), src + consumed,
                                145                 :              0 :                                 srclen - consumed);
                                146                 :                : 
   19 jdavis@postgresql.or      147                 :GNC         133 :     return result;
                                148                 :                : }
                                149                 :                : 
                                150                 :                : static size_t
   72 jdavis@postgresql.or      151                 :CBC      158561 : strupper_builtin(char *dest, size_t destsize, const char *src, size_t srclen,
                                152                 :                :                  pg_locale_t locale)
                                153                 :                : {
                                154                 :                :     size_t      consumed;
                                155                 :                :     size_t      result;
                                156                 :                : 
   19 jdavis@postgresql.or      157                 :GNC      158561 :     result = unicode_strupper(dest, destsize, src, srclen, &consumed,
                                158                 :         158561 :                               locale->builtin.casemap_full);
                                159         [ -  + ]:         158561 :     if (consumed < srclen)
   19 jdavis@postgresql.or      160                 :UNC           0 :         report_invalid_encoding(GetDatabaseEncoding(), src + consumed,
                                161                 :              0 :                                 srclen - consumed);
                                162                 :                : 
   19 jdavis@postgresql.or      163                 :GNC      158561 :     return result;
                                164                 :                : }
                                165                 :                : 
                                166                 :                : static size_t
   72 jdavis@postgresql.or      167                 :CBC          10 : strfold_builtin(char *dest, size_t destsize, const char *src, size_t srclen,
                                168                 :                :                 pg_locale_t locale)
                                169                 :                : {
                                170                 :                :     size_t      consumed;
                                171                 :                :     size_t      result;
                                172                 :                : 
   19 jdavis@postgresql.or      173                 :GNC          10 :     result = unicode_strfold(dest, destsize, src, srclen, &consumed,
                                174                 :             10 :                              locale->builtin.casemap_full);
                                175         [ -  + ]:             10 :     if (consumed < srclen)
   19 jdavis@postgresql.or      176                 :UNC           0 :         report_invalid_encoding(GetDatabaseEncoding(), src + consumed,
                                177                 :              0 :                                 srclen - consumed);
                                178                 :                : 
   19 jdavis@postgresql.or      179                 :GNC          10 :     return result;
                                180                 :                : }
                                181                 :                : 
                                182                 :                : static bool
  390 jdavis@postgresql.or      183                 :CBC       43117 : wc_isdigit_builtin(pg_wchar wc, pg_locale_t locale)
                                184                 :                : {
  270                           185                 :          43117 :     return pg_u_isdigit(to_char32(wc), !locale->builtin.casemap_full);
                                186                 :                : }
                                187                 :                : 
                                188                 :                : static bool
  390                           189                 :          19901 : wc_isalpha_builtin(pg_wchar wc, pg_locale_t locale)
                                190                 :                : {
  270                           191                 :          19901 :     return pg_u_isalpha(to_char32(wc));
                                192                 :                : }
                                193                 :                : 
                                194                 :                : static bool
  390                           195                 :          24708 : wc_isalnum_builtin(pg_wchar wc, pg_locale_t locale)
                                196                 :                : {
  270                           197                 :          24708 :     return pg_u_isalnum(to_char32(wc), !locale->builtin.casemap_full);
                                198                 :                : }
                                199                 :                : 
                                200                 :                : static bool
  390                           201                 :          16384 : wc_isupper_builtin(pg_wchar wc, pg_locale_t locale)
                                202                 :                : {
  270                           203                 :          16384 :     return pg_u_isupper(to_char32(wc));
                                204                 :                : }
                                205                 :                : 
                                206                 :                : static bool
  390 jdavis@postgresql.or      207                 :UBC           0 : wc_islower_builtin(pg_wchar wc, pg_locale_t locale)
                                208                 :                : {
  270                           209                 :              0 :     return pg_u_islower(to_char32(wc));
                                210                 :                : }
                                211                 :                : 
                                212                 :                : static bool
  390                           213                 :              0 : wc_isgraph_builtin(pg_wchar wc, pg_locale_t locale)
                                214                 :                : {
  270                           215                 :              0 :     return pg_u_isgraph(to_char32(wc));
                                216                 :                : }
                                217                 :                : 
                                218                 :                : static bool
  390                           219                 :              0 : wc_isprint_builtin(pg_wchar wc, pg_locale_t locale)
                                220                 :                : {
  270                           221                 :              0 :     return pg_u_isprint(to_char32(wc));
                                222                 :                : }
                                223                 :                : 
                                224                 :                : static bool
  390 jdavis@postgresql.or      225                 :CBC       16384 : wc_ispunct_builtin(pg_wchar wc, pg_locale_t locale)
                                226                 :                : {
  270                           227                 :          16384 :     return pg_u_ispunct(to_char32(wc), !locale->builtin.casemap_full);
                                228                 :                : }
                                229                 :                : 
                                230                 :                : static bool
  390                           231                 :           8312 : wc_isspace_builtin(pg_wchar wc, pg_locale_t locale)
                                232                 :                : {
  270                           233                 :           8312 :     return pg_u_isspace(to_char32(wc));
                                234                 :                : }
                                235                 :                : 
                                236                 :                : static bool
  281                           237                 :              3 : wc_isxdigit_builtin(pg_wchar wc, pg_locale_t locale)
                                238                 :                : {
  270                           239                 :              3 :     return pg_u_isxdigit(to_char32(wc), !locale->builtin.casemap_full);
                                240                 :                : }
                                241                 :                : 
                                242                 :                : static bool
  228 jdavis@postgresql.or      243                 :UBC           0 : wc_iscased_builtin(pg_wchar wc, pg_locale_t locale)
                                244                 :                : {
                                245                 :              0 :     return pg_u_prop_cased(to_char32(wc));
                                246                 :                : }
                                247                 :                : 
                                248                 :                : static pg_wchar
  390 jdavis@postgresql.or      249                 :CBC         325 : wc_toupper_builtin(pg_wchar wc, pg_locale_t locale)
                                250                 :                : {
  270                           251                 :            325 :     return to_pg_wchar(unicode_uppercase_simple(to_char32(wc)));
                                252                 :                : }
                                253                 :                : 
                                254                 :                : static pg_wchar
  390                           255                 :            325 : wc_tolower_builtin(pg_wchar wc, pg_locale_t locale)
                                256                 :                : {
  270                           257                 :            325 :     return to_pg_wchar(unicode_lowercase_simple(to_char32(wc)));
                                258                 :                : }
                                259                 :                : 
                                260                 :                : static const struct ctype_methods ctype_methods_builtin = {
                                261                 :                :     .strlower = strlower_builtin,
                                262                 :                :     .strtitle = strtitle_builtin,
                                263                 :                :     .strupper = strupper_builtin,
                                264                 :                :     .strfold = strfold_builtin,
                                265                 :                :     /* uses plain ASCII semantics for historical reasons */
                                266                 :                :     .downcase_ident = NULL,
                                267                 :                :     .wc_isdigit = wc_isdigit_builtin,
                                268                 :                :     .wc_isalpha = wc_isalpha_builtin,
                                269                 :                :     .wc_isalnum = wc_isalnum_builtin,
                                270                 :                :     .wc_isupper = wc_isupper_builtin,
                                271                 :                :     .wc_islower = wc_islower_builtin,
                                272                 :                :     .wc_isgraph = wc_isgraph_builtin,
                                273                 :                :     .wc_isprint = wc_isprint_builtin,
                                274                 :                :     .wc_ispunct = wc_ispunct_builtin,
                                275                 :                :     .wc_isspace = wc_isspace_builtin,
                                276                 :                :     .wc_isxdigit = wc_isxdigit_builtin,
                                277                 :                :     .wc_iscased = wc_iscased_builtin,
                                278                 :                :     .wc_tolower = wc_tolower_builtin,
                                279                 :                :     .wc_toupper = wc_toupper_builtin,
                                280                 :                : };
                                281                 :                : 
                                282                 :                : pg_locale_t
  601                           283                 :            989 : create_pg_locale_builtin(Oid collid, MemoryContext context)
                                284                 :                : {
                                285                 :                :     const char *locstr;
                                286                 :                :     pg_locale_t result;
                                287                 :                : 
                                288         [ +  + ]:            989 :     if (collid == DEFAULT_COLLATION_OID)
                                289                 :                :     {
                                290                 :                :         HeapTuple   tp;
                                291                 :                :         Datum       datum;
                                292                 :                : 
                                293                 :            951 :         tp = SearchSysCache1(DATABASEOID, ObjectIdGetDatum(MyDatabaseId));
                                294         [ -  + ]:            951 :         if (!HeapTupleIsValid(tp))
  601 jdavis@postgresql.or      295         [ #  # ]:UBC           0 :             elog(ERROR, "cache lookup failed for database %u", MyDatabaseId);
  601 jdavis@postgresql.or      296                 :CBC         951 :         datum = SysCacheGetAttrNotNull(DATABASEOID, tp,
                                297                 :                :                                        Anum_pg_database_datlocale);
                                298                 :            951 :         locstr = TextDatumGetCString(datum);
                                299                 :            951 :         ReleaseSysCache(tp);
                                300                 :                :     }
                                301                 :                :     else
                                302                 :                :     {
                                303                 :                :         HeapTuple   tp;
                                304                 :                :         Datum       datum;
                                305                 :                : 
                                306                 :             38 :         tp = SearchSysCache1(COLLOID, ObjectIdGetDatum(collid));
                                307         [ -  + ]:             38 :         if (!HeapTupleIsValid(tp))
  601 jdavis@postgresql.or      308         [ #  # ]:UBC           0 :             elog(ERROR, "cache lookup failed for collation %u", collid);
  601 jdavis@postgresql.or      309                 :CBC          38 :         datum = SysCacheGetAttrNotNull(COLLOID, tp,
                                310                 :                :                                        Anum_pg_collation_colllocale);
                                311                 :             38 :         locstr = TextDatumGetCString(datum);
                                312                 :             38 :         ReleaseSysCache(tp);
                                313                 :                :     }
                                314                 :                : 
                                315                 :            989 :     builtin_validate_locale(GetDatabaseEncoding(), locstr);
                                316                 :                : 
                                317                 :            989 :     result = MemoryContextAllocZero(context, sizeof(struct pg_locale_struct));
                                318                 :                : 
  299 peter@eisentraut.org      319                 :            989 :     result->builtin.locale = MemoryContextStrdup(context, locstr);
                                320                 :            989 :     result->builtin.casemap_full = (strcmp(locstr, "PG_UNICODE_FAST") == 0);
  601 jdavis@postgresql.or      321                 :            989 :     result->deterministic = true;
                                322                 :            989 :     result->collate_is_c = true;
                                323                 :            989 :     result->ctype_is_c = (strcmp(locstr, "C") == 0);
  390                           324         [ +  + ]:            989 :     if (!result->ctype_is_c)
                                325                 :            966 :         result->ctype = &ctype_methods_builtin;
                                326                 :                : 
  601                           327                 :            989 :     return result;
                                328                 :                : }
                                329                 :                : 
                                330                 :                : char *
  564                           331                 :           1028 : get_collation_actual_version_builtin(const char *collcollate)
                                332                 :                : {
                                333                 :                :     /*
                                334                 :                :      * The only two supported locales (C and C.UTF-8) are both based on memcmp
                                335                 :                :      * and are not expected to change, but track the version anyway.
                                336                 :                :      *
                                337                 :                :      * Note that the character semantics may change for some locales, but the
                                338                 :                :      * collation version only tracks changes to sort order.
                                339                 :                :      */
                                340         [ +  + ]:           1028 :     if (strcmp(collcollate, "C") == 0)
                                341                 :             44 :         return "1";
                                342         [ +  + ]:            984 :     else if (strcmp(collcollate, "C.UTF-8") == 0)
                                343                 :            971 :         return "1";
  555                           344         [ +  - ]:             13 :     else if (strcmp(collcollate, "PG_UNICODE_FAST") == 0)
                                345                 :             13 :         return "1";
                                346                 :                :     else
  564 jdavis@postgresql.or      347         [ #  # ]:UBC           0 :         ereport(ERROR,
                                348                 :                :                 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
                                349                 :                :                  errmsg("invalid locale name \"%s\" for builtin provider",
                                350                 :                :                         collcollate)));
                                351                 :                : 
                                352                 :                :     return NULL;                /* keep compiler quiet */
                                353                 :                : }
        

Generated by: LCOV version 2.0-1