postgres.git / summary / log / commit / refs

commit    fe91799d3bc6511247a58643b095b5f9a1ccd660
Author:   Jeff Davis <jdavis@postgresql.org>
Date:     Tue Aug 18 20:16:28 2026 +0000
First released in: 18.7, 19.0

    pg_locale.c: comment improvements.
    
    Add missing comments and fix outdated/incorrect comments in
    pg_locale.c and related files.
    
    Suggested-by: Andres Freund <andres@anarazel.de>
    Discussion: https://postgr.es/m/v3nniwcrxejmcfvz56xbd22hphprqleuornd6hqkmw2bl7kgmz@cnytz2ee5ltk
    Backpatch-through: 18


src/backend/utils/adt/pg_locale.c | 106 ++++++++++++++++++++++++------ src/backend/utils/adt/pg_locale_builtin.c | 4 +- src/backend/utils/adt/pg_locale_icu.c | 19 +++--- src/include/utils/pg_locale.h | 13 +++- 4 files changed, 106 insertions(+), 36 deletions(-) diff --git a/src/backend/utils/adt/pg_locale.c b/src/backend/utils/adt/pg_locale.c index 11d48a3916e..cf77db77682 100644 --- a/src/backend/utils/adt/pg_locale.c +++ b/src/backend/utils/adt/pg_locale.c @@ -1317,6 +1317,20 @@ strupper_c(char *dst, size_t dstsize, const char *src, size_t srclen) return srclen; } +/* + * pg_strlower() + * + * Convert src to lowercase, and return the result length (not including + * terminating NUL). + * + * src must be in the database encoding with no embedded NULs. If dstsize is + * zero, dst may be NULL, which is useful for calculating the required buffer + * size before allocating. + * + * If the result length is less than dstsize, the NUL-terminated result is + * stored in dst. Otherwise, the contents of dst are undefined, and the + * caller should use the return value to resize the buffer and retry. + */ size_t pg_strlower(char *dst, size_t dstsize, const char *src, size_t srclen, pg_locale_t locale) @@ -1327,6 +1341,20 @@ pg_strlower(char *dst, size_t dstsize, const char *src, size_t srclen, return locale->ctype->strlower(dst, dstsize, src, srclen, locale); } +/* + * pg_strtitle() + * + * Convert src to titlecase, and return the result length (not including + * terminating NUL). + * + * src must be in the database encoding with no embedded NULs. If dstsize is + * zero, dst may be NULL, which is useful for calculating the required buffer + * size before allocating. + * + * If the result length is less than dstsize, the NUL-terminated result is + * stored in dst. Otherwise, the contents of dst are undefined, and the + * caller should use the return value to resize the buffer and retry. + */ size_t pg_strtitle(char *dst, size_t dstsize, const char *src, size_t srclen, pg_locale_t locale) @@ -1337,6 +1365,20 @@ pg_strtitle(char *dst, size_t dstsize, const char *src, size_t srclen, return locale->ctype->strtitle(dst, dstsize, src, srclen, locale); } +/* + * pg_strupper() + * + * Convert src to uppercase, and return the result length (not including + * terminating NUL). + * + * src must be in the database encoding with no embedded NULs. If dstsize is + * zero, dst may be NULL, which is useful for calculating the required buffer + * size before allocating. + * + * If the result length is less than dstsize, the NUL-terminated result is + * stored in dst. Otherwise, the contents of dst are undefined, and the + * caller should use the return value to resize the buffer and retry. + */ size_t pg_strupper(char *dst, size_t dstsize, const char *src, size_t srclen, pg_locale_t locale) @@ -1347,6 +1389,19 @@ pg_strupper(char *dst, size_t dstsize, const char *src, size_t srclen, return locale->ctype->strupper(dst, dstsize, src, srclen, locale); } +/* + * pg_strfold() + * + * Casefold src, and return the result length (not including terminating NUL). + * + * src must be in the database encoding with no embedded NULs. If dstsize is + * zero, dst may be NULL, which is useful for calculating the required buffer + * size before allocating. + * + * If the result length is less than dstsize, the NUL-terminated result is + * stored in dst. Otherwise, the contents of dst are undefined, and the + * caller should use the return value to resize the buffer and retry. + */ size_t pg_strfold(char *dst, size_t dstsize, const char *src, size_t srclen, pg_locale_t locale) @@ -1359,11 +1414,15 @@ pg_strfold(char *dst, size_t dstsize, const char *src, size_t srclen, } /* - * Lowercase an identifier using the database default locale. + * pg_downcase_ident() + * + * Lowercase an identifier using historical identifier-folding semantics, and + * return the result length (not including terminating NUL). If the result + * length is less than dstsize, the NUL-terminated result is stored in dst; + * otherwise the contents of dst are undefined. * - * For historical reasons, does not use ordinary locale behavior. Should only - * be used for identifiers. XXX: can we make this equivalent to - * pg_strfold(..., default_locale)? + * XXX: callers currently depend on the result length being equal to srclen, + * but that may change in the future if we change to proper case folding. */ size_t pg_downcase_ident(char *dst, size_t dstsize, const char *src, size_t srclen) @@ -1392,11 +1451,9 @@ pg_strcoll(const char *arg1, const char *arg2, pg_locale_t locale) /* * pg_strncoll * - * Call ucol_strcollUTF8(), ucol_strcoll(), strcoll_l() or wcscoll_l() as - * appropriate for the given locale, platform, and database encoding. If the - * locale is not specified, use the database collation. + * Compare strings according to the given locale. * - * The input strings must be encoded in the database encoding. + * Strings must be encoded in the database encoding with no embedded NULs. * * The caller is responsible for breaking ties if the collation is * deterministic; this maintains consistency with pg_strnxfrm(), which cannot @@ -1410,11 +1467,8 @@ pg_strncoll(const char *arg1, size_t len1, const char *arg2, size_t len2, } /* - * Return true if the collation provider supports pg_strxfrm() and - * pg_strnxfrm(); otherwise false. - * - * - * No similar problem is known for the ICU provider. + * Return true if the locale supports pg_strxfrm() and pg_strnxfrm(); + * otherwise false. */ bool pg_strxfrm_enabled(pg_locale_t locale) @@ -1445,8 +1499,8 @@ pg_strxfrm(char *dest, const char *src, size_t destsize, pg_locale_t locale) * ordinary strcmp() on transformed strings is equivalent to pg_strcoll() on * untransformed strings. * - * The input string must be encoded in the database encoding. If 'destsize' is - * zero, 'dest' may be NULL. + * String must be encoded in the database encoding with no embedded NULs. If + * 'destsize' is zero, 'dest' may be NULL. * * Not all providers support pg_strnxfrm() safely. The caller should check * pg_strxfrm_enabled() first, otherwise this function may return wrong @@ -1464,7 +1518,7 @@ pg_strnxfrm(char *dest, size_t destsize, const char *src, size_t srclen, } /* - * Return true if the collation provider supports pg_strxfrm_prefix() and + * Return true if the locale supports pg_strxfrm_prefix() and * pg_strnxfrm_prefix(); otherwise false. */ bool @@ -1492,11 +1546,11 @@ pg_strxfrm_prefix(char *dest, const char *src, size_t destsize, * memcmp() on the byte sequence is equivalent to pg_strncoll() on * untransformed strings. The result is not nul-terminated. * - * The input string must be encoded in the database encoding. + * String must be encoded in the database encoding with no embedded NULs. If + * destsize is zero, dest may be NULL. * - * Not all providers support pg_strnxfrm_prefix() safely. The caller should - * check pg_strxfrm_prefix_enabled() first, otherwise this function may return - * wrong results or an error. + * Not all providers support pg_strnxfrm_prefix() safely. The caller must + * check pg_strxfrm_prefix_enabled() first. * * If destsize is not large enough to hold the resulting byte sequence, stores * only the first destsize bytes in 'dest'. Returns the number of bytes @@ -1509,6 +1563,18 @@ pg_strnxfrm_prefix(char *dest, size_t destsize, const char *src, return locale->collate->strnxfrm_prefix(dest, destsize, src, srclen, locale); } +/* + * pg_iswdigit(), pg_iswalpha(), etc. + * + * Character semantics for pattern-matching. Uses pg_wchar, which is an + * encoding-dependent value, which may or may not be equivalent to a code + * point. + * + * For case-mapping an entire string, use pg_strlower(), etc., instead. + * String based functions can handle one-to-many and context-sensitive + * mappings. + */ + bool pg_iswdigit(pg_wchar wc, pg_locale_t locale) { diff --git a/src/backend/utils/adt/pg_locale_builtin.c b/src/backend/utils/adt/pg_locale_builtin.c index 7c36fd5091b..8fa0b637b06 100644 --- a/src/backend/utils/adt/pg_locale_builtin.c +++ b/src/backend/utils/adt/pg_locale_builtin.c @@ -288,8 +288,8 @@ char * get_collation_actual_version_builtin(const char *collcollate) { /* - * The only two supported locales (C and C.UTF-8) are both based on memcmp - * and are not expected to change, but track the version anyway. + * The supported locales (C, C.UTF-8, and PG_UNICODE_FAST) are all based + * on memcmp and are not expected to change, but track the version anyway. * * Note that the character semantics may change for some locales, but the * collation version only tracks changes to sort order. diff --git a/src/backend/utils/adt/pg_locale_icu.c b/src/backend/utils/adt/pg_locale_icu.c index cb92ac6ee59..5ba37a54f9c 100644 --- a/src/backend/utils/adt/pg_locale_icu.c +++ b/src/backend/utils/adt/pg_locale_icu.c @@ -705,8 +705,8 @@ strfold_icu_utf8(char *dest, size_t destsize, const char *src, size_t srclen, /* * For historical compatibility, behavior is not multibyte-aware. * - * NB: uses libc tolower() for single-byte encodings (also for historical - * compatibility), and therefore relies on the global LC_CTYPE setting. + * NB: uses libc tolower_l() for single-byte encodings (also for historical + * compatibility), and therefore relies on the LC_CTYPE setting. */ static size_t downcase_ident_icu(char *dst, size_t dstsize, const char *src, @@ -736,10 +736,9 @@ downcase_ident_icu(char *dst, size_t dstsize, const char *src, } /* - * strncoll_icu_utf8 + * strncoll_icu_utf8() * - * Call ucol_strcollUTF8() or ucol_strcoll() as appropriate for the given - * database encoding. + * Wrapper for ucol_strcollUTF8(). */ #ifdef HAVE_UCOL_STRCOLLUTF8 int @@ -927,13 +926,11 @@ icu_to_uchar(UChar **buff_uchar, const char *buff, size_t nbytes) /* * Convert a string of UChars into the database encoding. * - * The source string at buff_uchar is of length len_uchar - * (it needn't be nul-terminated) - * - * *result receives a pointer to the palloc'd result string, and the - * function's result is the number of bytes generated (not counting nul). + * The source string at buff_uchar is of length len_uchar (it needn't be + * nul-terminated) * - * The result string is nul-terminated. + * If the result length is less than destsize, the NUL-terminated result is + * stored in dest. Otherwise the contents of dest are undefined. */ static size_t icu_from_uchar(char *dest, size_t destsize, const UChar *buff_uchar, int32_t len_uchar) diff --git a/src/include/utils/pg_locale.h b/src/include/utils/pg_locale.h index b74821fdfa9..fcd508f5dd6 100644 --- a/src/include/utils/pg_locale.h +++ b/src/include/utils/pg_locale.h @@ -59,7 +59,9 @@ extern void cache_locale_time(void); struct pg_locale_struct; typedef struct pg_locale_struct *pg_locale_t; -/* methods that define collation behavior */ +/* + * Collation behavior: string ordering. + */ struct collate_methods { /* required */ @@ -88,16 +90,19 @@ struct collate_methods /* * If the strnxfrm method is not trusted to return the correct results, - * set strxfrm_is_safe to false. It set to false, the method will not be + * set strxfrm_is_safe to false. If set to false, the method will not be * used in most cases, but the planner still expects it to be there for * estimation purposes (where incorrect results are acceptable). */ bool strxfrm_is_safe; }; +/* + * Character behavior: casing semantics and pattern matching. + */ struct ctype_methods { - /* case mapping: LOWER()/INITCAP()/UPPER() */ + /* required */ size_t (*strlower) (char *dest, size_t destsize, const char *src, size_t srclen, pg_locale_t locale); @@ -110,6 +115,8 @@ struct ctype_methods size_t (*strfold) (char *dest, size_t destsize, const char *src, size_t srclen, pg_locale_t locale); + + /* optional */ size_t (*downcase_ident) (char *dest, size_t destsize, const char *src, size_t srclen, pg_locale_t locale); [parent: e4f16fda6e14]