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: f76c13edadc2b319036e0d238703ed56bb23f934 vs 9e17d25e79d4756be08b4a5521b4b58450217137 Lines: 84.2 % 114 96 8 10 16 80 8
Current Date: 2026-09-20 14:13:17 +0900 Functions: 90.9 % 22 20 2 4 16
Baseline: lcov-20260920-baseline Branches: 60.0 % 40 24 4 12 4 20
Baseline Date: 2026-09-20 14:13:13 +0900 Line coverage date bins:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
(30,360] days: 78.5 % 65 51 8 6 16 35
(360..) days: 91.8 % 49 45 4 45
Function coverage date bins:
(30,360] days: 87.5 % 8 7 1 4 3
(360..) days: 92.9 % 14 13 1 13
Branch coverage date bins:
(30,360] days: 65.0 % 20 13 4 3 4 9
(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
  326 jdavis@postgresql.or       52                 :CBC      133561 : to_char32(pg_wchar wc)
                                 53                 :                : {
                                 54         [ -  + ]:         133561 :     Assert(GetDatabaseEncoding() == PG_UTF8);
                                 55                 :         133561 :     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
  643                            70                 :            580 : initcap_wbnext(void *state)
                                 71                 :                : {
                                 72                 :            580 :     struct WordBoundaryState *wbstate = (struct WordBoundaryState *) state;
                                 73                 :                : 
  128                            74         [ +  + ]:           1213 :     while (wbstate->offset < wbstate->len)
                                 75                 :                :     {
   75                            76                 :           1076 :         int         ulen = pg_utf_mblen((const unsigned char *) wbstate->str +
  643                            77                 :           1076 :                                         wbstate->offset);
                                 78                 :                :         char32_t    u;
                                 79                 :                :         bool        curr_alnum;
   75                            80                 :           1076 :         size_t      prev_offset = wbstate->offset;
                                 81                 :                : 
                                 82                 :                :         /* invalid UTF8 */
                                 83         [ -  + ]:           1076 :         if (wbstate->offset + ulen > wbstate->len)
                                 84                 :                :         {
   75 jdavis@postgresql.or       85                 :UBC           0 :             wbstate->init = true;
                                 86                 :              0 :             wbstate->offset = wbstate->len;
                                 87                 :              0 :             return prev_offset;
                                 88                 :                :         }
                                 89                 :                : 
   75 jdavis@postgresql.or       90                 :CBC        1076 :         u = utf8_to_unicode((const unsigned char *) wbstate->str +
                                 91                 :           1076 :                             wbstate->offset);
                                 92                 :           1076 :         curr_alnum = pg_u_isalnum(u, wbstate->posix);
                                 93                 :                : 
                                 94   [ +  +  +  + ]:           1076 :         if (!wbstate->init || curr_alnum != wbstate->prev_alnum)
                                 95                 :                :         {
  643                            96                 :            443 :             wbstate->init = true;
   75                            97                 :            443 :             wbstate->offset += ulen;
  643                            98                 :            443 :             wbstate->prev_alnum = curr_alnum;
                                 99                 :            443 :             return prev_offset;
                                100                 :                :         }
                                101                 :                : 
   75                           102                 :            633 :         wbstate->offset += ulen;
                                103                 :                :     }
                                104                 :                : 
  643                           105                 :            137 :     return wbstate->len;
                                106                 :                : }
                                107                 :                : 
                                108                 :                : static size_t
  128                           109                 :           6331 : 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                 :                : 
   75 jdavis@postgresql.or      115                 :GNC        6331 :     result = unicode_strlower(dest, destsize, src, srclen, &consumed,
                                116                 :           6331 :                               locale->builtin.casemap_full);
                                117         [ -  + ]:           6331 :     if (consumed < srclen)
   75 jdavis@postgresql.or      118                 :UNC           0 :         report_invalid_encoding(GetDatabaseEncoding(), src + consumed,
                                119                 :              0 :                                 srclen - consumed);
                                120                 :                : 
   75 jdavis@postgresql.or      121                 :GNC        6331 :     return result;
                                122                 :                : }
                                123                 :                : 
                                124                 :                : static size_t
  128 jdavis@postgresql.or      125                 :CBC         137 : strtitle_builtin(char *dest, size_t destsize, const char *src, size_t srclen,
                                126                 :                :                  pg_locale_t locale)
                                127                 :                : {
  643                           128                 :            137 :     struct WordBoundaryState wbstate = {
                                129                 :                :         .str = src,
                                130                 :                :         .len = srclen,
                                131                 :                :         .offset = 0,
  355 peter@eisentraut.org      132                 :            137 :         .posix = !locale->builtin.casemap_full,
                                133                 :                :         .init = false,
                                134                 :                :         .prev_alnum = false,
                                135                 :                :     };
                                136                 :                :     size_t      consumed;
                                137                 :                :     size_t      result;
                                138                 :                : 
   75 jdavis@postgresql.or      139                 :GNC         137 :     result = unicode_strtitle(dest, destsize, src, srclen, &consumed,
                                140                 :            137 :                               locale->builtin.casemap_full,
                                141                 :                :                               initcap_wbnext, &wbstate);
                                142                 :                : 
                                143         [ -  + ]:            137 :     if (consumed < srclen)
   75 jdavis@postgresql.or      144                 :UNC           0 :         report_invalid_encoding(GetDatabaseEncoding(), src + consumed,
                                145                 :              0 :                                 srclen - consumed);
                                146                 :                : 
   75 jdavis@postgresql.or      147                 :GNC         137 :     return result;
                                148                 :                : }
                                149                 :                : 
                                150                 :                : static size_t
  128 jdavis@postgresql.or      151                 :CBC      158567 : 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                 :                : 
   75 jdavis@postgresql.or      157                 :GNC      158567 :     result = unicode_strupper(dest, destsize, src, srclen, &consumed,
                                158                 :         158567 :                               locale->builtin.casemap_full);
                                159         [ -  + ]:         158567 :     if (consumed < srclen)
   75 jdavis@postgresql.or      160                 :UNC           0 :         report_invalid_encoding(GetDatabaseEncoding(), src + consumed,
                                161                 :              0 :                                 srclen - consumed);
                                162                 :                : 
   75 jdavis@postgresql.or      163                 :GNC      158567 :     return result;
                                164                 :                : }
                                165                 :                : 
                                166                 :                : static size_t
  128 jdavis@postgresql.or      167                 :CBC          14 : 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                 :                : 
   75 jdavis@postgresql.or      173                 :GNC          14 :     result = unicode_strfold(dest, destsize, src, srclen, &consumed,
                                174                 :             14 :                              locale->builtin.casemap_full);
                                175         [ -  + ]:             14 :     if (consumed < srclen)
   75 jdavis@postgresql.or      176                 :UNC           0 :         report_invalid_encoding(GetDatabaseEncoding(), src + consumed,
                                177                 :              0 :                                 srclen - consumed);
                                178                 :                : 
   75 jdavis@postgresql.or      179                 :GNC          14 :     return result;
                                180                 :                : }
                                181                 :                : 
                                182                 :                : static bool
  446 jdavis@postgresql.or      183                 :CBC       43117 : wc_isdigit_builtin(pg_wchar wc, pg_locale_t locale)
                                184                 :                : {
  326                           185                 :          43117 :     return pg_u_isdigit(to_char32(wc), !locale->builtin.casemap_full);
                                186                 :                : }
                                187                 :                : 
                                188                 :                : static bool
  446                           189                 :          19901 : wc_isalpha_builtin(pg_wchar wc, pg_locale_t locale)
                                190                 :                : {
  326                           191                 :          19901 :     return pg_u_isalpha(to_char32(wc));
                                192                 :                : }
                                193                 :                : 
                                194                 :                : static bool
  446                           195                 :          24708 : wc_isalnum_builtin(pg_wchar wc, pg_locale_t locale)
                                196                 :                : {
  326                           197                 :          24708 :     return pg_u_isalnum(to_char32(wc), !locale->builtin.casemap_full);
                                198                 :                : }
                                199                 :                : 
                                200                 :                : static bool
  446                           201                 :          16384 : wc_isupper_builtin(pg_wchar wc, pg_locale_t locale)
                                202                 :                : {
  326                           203                 :          16384 :     return pg_u_isupper(to_char32(wc));
                                204                 :                : }
                                205                 :                : 
                                206                 :                : static bool
  446 jdavis@postgresql.or      207                 :UBC           0 : wc_islower_builtin(pg_wchar wc, pg_locale_t locale)
                                208                 :                : {
  326                           209                 :              0 :     return pg_u_islower(to_char32(wc));
                                210                 :                : }
                                211                 :                : 
                                212                 :                : static bool
  446 jdavis@postgresql.or      213                 :CBC        2051 : wc_isgraph_builtin(pg_wchar wc, pg_locale_t locale)
                                214                 :                : {
  326                           215                 :           2051 :     return pg_u_isgraph(to_char32(wc));
                                216                 :                : }
                                217                 :                : 
                                218                 :                : static bool
  446                           219                 :           2051 : wc_isprint_builtin(pg_wchar wc, pg_locale_t locale)
                                220                 :                : {
  326                           221                 :           2051 :     return pg_u_isprint(to_char32(wc));
                                222                 :                : }
                                223                 :                : 
                                224                 :                : static bool
  446                           225                 :          16384 : wc_ispunct_builtin(pg_wchar wc, pg_locale_t locale)
                                226                 :                : {
  326                           227                 :          16384 :     return pg_u_ispunct(to_char32(wc), !locale->builtin.casemap_full);
                                228                 :                : }
                                229                 :                : 
                                230                 :                : static bool
  446                           231                 :           8312 : wc_isspace_builtin(pg_wchar wc, pg_locale_t locale)
                                232                 :                : {
  326                           233                 :           8312 :     return pg_u_isspace(to_char32(wc));
                                234                 :                : }
                                235                 :                : 
                                236                 :                : static bool
  337                           237                 :              3 : wc_isxdigit_builtin(pg_wchar wc, pg_locale_t locale)
                                238                 :                : {
  326                           239                 :              3 :     return pg_u_isxdigit(to_char32(wc), !locale->builtin.casemap_full);
                                240                 :                : }
                                241                 :                : 
                                242                 :                : static bool
  284 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
  446 jdavis@postgresql.or      249                 :CBC         325 : wc_toupper_builtin(pg_wchar wc, pg_locale_t locale)
                                250                 :                : {
  326                           251                 :            325 :     return to_pg_wchar(unicode_uppercase_simple(to_char32(wc)));
                                252                 :                : }
                                253                 :                : 
                                254                 :                : static pg_wchar
  446                           255                 :            325 : wc_tolower_builtin(pg_wchar wc, pg_locale_t locale)
                                256                 :                : {
  326                           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                 :                :     .wc_isdigit = wc_isdigit_builtin,
                                266                 :                :     .wc_isalpha = wc_isalpha_builtin,
                                267                 :                :     .wc_isalnum = wc_isalnum_builtin,
                                268                 :                :     .wc_isupper = wc_isupper_builtin,
                                269                 :                :     .wc_islower = wc_islower_builtin,
                                270                 :                :     .wc_isgraph = wc_isgraph_builtin,
                                271                 :                :     .wc_isprint = wc_isprint_builtin,
                                272                 :                :     .wc_ispunct = wc_ispunct_builtin,
                                273                 :                :     .wc_isspace = wc_isspace_builtin,
                                274                 :                :     .wc_isxdigit = wc_isxdigit_builtin,
                                275                 :                :     .wc_iscased = wc_iscased_builtin,
                                276                 :                :     .wc_tolower = wc_tolower_builtin,
                                277                 :                :     .wc_toupper = wc_toupper_builtin,
                                278                 :                : };
                                279                 :                : 
                                280                 :                : pg_locale_t
  657                           281                 :           1018 : create_pg_locale_builtin(Oid collid, MemoryContext context)
                                282                 :                : {
                                283                 :                :     const char *locstr;
                                284                 :                :     pg_locale_t result;
                                285                 :                : 
                                286         [ +  + ]:           1018 :     if (collid == DEFAULT_COLLATION_OID)
                                287                 :                :     {
                                288                 :                :         HeapTuple   tp;
                                289                 :                :         Datum       datum;
                                290                 :                : 
                                291                 :            975 :         tp = SearchSysCache1(DATABASEOID, ObjectIdGetDatum(MyDatabaseId));
                                292         [ -  + ]:            975 :         if (!HeapTupleIsValid(tp))
  657 jdavis@postgresql.or      293         [ #  # ]:UBC           0 :             elog(ERROR, "cache lookup failed for database %u", MyDatabaseId);
  657 jdavis@postgresql.or      294                 :CBC         975 :         datum = SysCacheGetAttrNotNull(DATABASEOID, tp,
                                295                 :                :                                        Anum_pg_database_datlocale);
                                296                 :            975 :         locstr = TextDatumGetCString(datum);
                                297                 :            975 :         ReleaseSysCache(tp);
                                298                 :                :     }
                                299                 :                :     else
                                300                 :                :     {
                                301                 :                :         HeapTuple   tp;
                                302                 :                :         Datum       datum;
                                303                 :                : 
                                304                 :             43 :         tp = SearchSysCache1(COLLOID, ObjectIdGetDatum(collid));
                                305         [ -  + ]:             43 :         if (!HeapTupleIsValid(tp))
  657 jdavis@postgresql.or      306         [ #  # ]:UBC           0 :             elog(ERROR, "cache lookup failed for collation %u", collid);
  657 jdavis@postgresql.or      307                 :CBC          43 :         datum = SysCacheGetAttrNotNull(COLLOID, tp,
                                308                 :                :                                        Anum_pg_collation_colllocale);
                                309                 :             43 :         locstr = TextDatumGetCString(datum);
                                310                 :             43 :         ReleaseSysCache(tp);
                                311                 :                :     }
                                312                 :                : 
                                313                 :           1018 :     builtin_validate_locale(GetDatabaseEncoding(), locstr);
                                314                 :                : 
                                315                 :           1018 :     result = MemoryContextAllocZero(context, sizeof(struct pg_locale_struct));
                                316                 :                : 
  355 peter@eisentraut.org      317                 :           1018 :     result->builtin.locale = MemoryContextStrdup(context, locstr);
                                318                 :           1018 :     result->builtin.casemap_full = (strcmp(locstr, "PG_UNICODE_FAST") == 0);
  657 jdavis@postgresql.or      319                 :           1018 :     result->deterministic = true;
                                320                 :           1018 :     result->collate_is_c = true;
                                321                 :           1018 :     result->ctype_is_c = (strcmp(locstr, "C") == 0);
  446                           322         [ +  + ]:           1018 :     if (!result->ctype_is_c)
                                323                 :            995 :         result->ctype = &ctype_methods_builtin;
                                324                 :                : 
  657                           325                 :           1018 :     return result;
                                326                 :                : }
                                327                 :                : 
                                328                 :                : char *
  620                           329                 :           1053 : get_collation_actual_version_builtin(const char *collcollate)
                                330                 :                : {
                                331                 :                :     /*
                                332                 :                :      * The supported locales (C, C.UTF-8, and PG_UNICODE_FAST) are all based
                                333                 :                :      * on memcmp and are not expected to change, but track the version anyway.
                                334                 :                :      *
                                335                 :                :      * Note that the character semantics may change for some locales, but the
                                336                 :                :      * collation version only tracks changes to sort order.
                                337                 :                :      */
                                338         [ +  + ]:           1053 :     if (strcmp(collcollate, "C") == 0)
                                339                 :             40 :         return "1";
                                340         [ +  + ]:           1013 :     else if (strcmp(collcollate, "C.UTF-8") == 0)
                                341                 :           1000 :         return "1";
  611                           342         [ +  - ]:             13 :     else if (strcmp(collcollate, "PG_UNICODE_FAST") == 0)
                                343                 :             13 :         return "1";
                                344                 :                :     else
  620 jdavis@postgresql.or      345         [ #  # ]:UBC           0 :         ereport(ERROR,
                                346                 :                :                 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
                                347                 :                :                  errmsg("invalid locale name \"%s\" for builtin provider",
                                348                 :                :                         collcollate)));
                                349                 :                : 
                                350                 :                :     return NULL;                /* keep compiler quiet */
                                351                 :                : }
        

Generated by: LCOV version 2.0-1