PostgreSQL Source Code git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
unicode_case.c File Reference
Include dependency graph for unicode_case.c:

Go to the source code of this file.

Enumerations

enum  CaseMapResult { CASEMAP_SELF , CASEMAP_SIMPLE , CASEMAP_SPECIAL }
 

Functions

static pg_wchar find_case_map (pg_wchar ucs, const pg_wchar *map)
 
static size_t convert_case (char *dst, size_t dstsize, const char *src, ssize_t srclen, CaseKind str_casekind, bool full, WordBoundaryNext wbnext, void *wbstate)
 
static enum CaseMapResult casemap (pg_wchar u1, CaseKind casekind, bool full, const char *src, size_t srclen, size_t srcoff, pg_wchar *simple, const pg_wchar **special)
 
pg_wchar unicode_lowercase_simple (pg_wchar code)
 
pg_wchar unicode_titlecase_simple (pg_wchar code)
 
pg_wchar unicode_uppercase_simple (pg_wchar code)
 
pg_wchar unicode_casefold_simple (pg_wchar code)
 
size_t unicode_strlower (char *dst, size_t dstsize, const char *src, ssize_t srclen, bool full)
 
size_t unicode_strtitle (char *dst, size_t dstsize, const char *src, ssize_t srclen, bool full, WordBoundaryNext wbnext, void *wbstate)
 
size_t unicode_strupper (char *dst, size_t dstsize, const char *src, ssize_t srclen, bool full)
 
size_t unicode_strfold (char *dst, size_t dstsize, const char *src, ssize_t srclen, bool full)
 
static bool check_final_sigma (const unsigned char *str, size_t len, size_t offset)
 
static bool check_special_conditions (int conditions, const char *str, size_t len, size_t offset)
 

Variables

static const pg_wchar *const casekind_map [NCaseKind]
 

Enumeration Type Documentation

◆ CaseMapResult

Enumerator
CASEMAP_SELF 
CASEMAP_SIMPLE 
CASEMAP_SPECIAL 

Definition at line 23 of file unicode_case.c.

24{
28};
@ CASEMAP_SPECIAL
Definition: unicode_case.c:27
@ CASEMAP_SIMPLE
Definition: unicode_case.c:26
@ CASEMAP_SELF
Definition: unicode_case.c:25

Function Documentation

◆ casemap()

static enum CaseMapResult casemap ( pg_wchar  u1,
CaseKind  casekind,
bool  full,
const char *  src,
size_t  srclen,
size_t  srcoff,
pg_wchar simple,
const pg_wchar **  special 
)
static

Definition at line 397 of file unicode_case.c.

400{
401 uint16 idx;
402
403 /* Fast path for codepoints < 0x80 */
404 if (u1 < 0x80)
405 {
406 /*
407 * The first elements in all tables are reserved as 0 (as NULL). The
408 * data starts at index 1, not 0.
409 */
410 *simple = casekind_map[casekind][u1 + 1];
411
412 return CASEMAP_SIMPLE;
413 }
414
415 idx = case_index(u1);
416
417 if (idx == 0)
418 return CASEMAP_SELF;
419
420 if (full && case_map_special[idx] &&
422 src, srclen, srcoff))
423 {
424 *special = special_case[case_map_special[idx]].map[casekind];
425 return CASEMAP_SPECIAL;
426 }
427
428 *simple = casekind_map[casekind][idx];
429
430 return CASEMAP_SIMPLE;
431}
Datum idx(PG_FUNCTION_ARGS)
Definition: _int_op.c:262
uint16_t uint16
Definition: c.h:501
pg_wchar map[NCaseKind][MAX_CASE_EXPANSION]
static bool check_special_conditions(int conditions, const char *str, size_t len, size_t offset)
Definition: unicode_case.c:370
static const pg_wchar *const casekind_map[NCaseKind]
Definition: unicode_case.c:33
static const uint8 case_map_special[1704]
static const pg_special_case special_case[106]
static uint16 case_index(pg_wchar cp)

References case_index(), case_map_special, casekind_map, CASEMAP_SELF, CASEMAP_SIMPLE, CASEMAP_SPECIAL, check_special_conditions(), idx(), pg_special_case::map, and special_case.

Referenced by convert_case(), and main().

◆ check_final_sigma()

static bool check_final_sigma ( const unsigned char *  str,
size_t  len,
size_t  offset 
)
static

Definition at line 312 of file unicode_case.c.

313{
314 /* the start of the string is not preceded by a Cased character */
315 if (offset == 0)
316 return false;
317
318 /* iterate backwards, looking for Cased character */
319 for (int i = offset - 1; i >= 0; i--)
320 {
321 if ((str[i] & 0x80) == 0 || (str[i] & 0xC0) == 0xC0)
322 {
323 pg_wchar curr = utf8_to_unicode(str + i);
324
325 if (pg_u_prop_case_ignorable(curr))
326 continue;
327 else if (pg_u_prop_cased(curr))
328 break;
329 else
330 return false;
331 }
332 else if ((str[i] & 0xC0) == 0x80)
333 continue;
334
335 Assert(false); /* invalid UTF-8 */
336 }
337
338 /* end of string is not followed by a Cased character */
339 if (offset == len)
340 return true;
341
342 /* iterate forwards, looking for Cased character */
343 for (int i = offset + 1; i < len && str[i] != '\0'; i++)
344 {
345 if ((str[i] & 0x80) == 0 || (str[i] & 0xC0) == 0xC0)
346 {
347 pg_wchar curr = utf8_to_unicode(str + i);
348
349 if (pg_u_prop_case_ignorable(curr))
350 continue;
351 else if (pg_u_prop_cased(curr))
352 return false;
353 else
354 break;
355 }
356 else if ((str[i] & 0xC0) == 0x80)
357 continue;
358
359 Assert(false); /* invalid UTF-8 */
360 }
361
362 return true;
363}
Assert(PointerIsAligned(start, uint64))
const char * str
int i
Definition: isn.c:77
static pg_wchar utf8_to_unicode(const unsigned char *c)
Definition: mbprint.c:53
unsigned int pg_wchar
Definition: mbprint.c:31
const void size_t len
bool pg_u_prop_cased(pg_wchar code)
bool pg_u_prop_case_ignorable(pg_wchar code)

References Assert(), i, len, pg_u_prop_case_ignorable(), pg_u_prop_cased(), str, and utf8_to_unicode().

Referenced by check_special_conditions().

◆ check_special_conditions()

static bool check_special_conditions ( int  conditions,
const char *  str,
size_t  len,
size_t  offset 
)
static

Definition at line 370 of file unicode_case.c.

372{
373 if (conditions == 0)
374 return true;
375 else if (conditions == PG_U_FINAL_SIGMA)
376 return check_final_sigma((unsigned char *) str, len, offset);
377
378 /* no other conditions supported */
379 Assert(false);
380 return false;
381}
static bool check_final_sigma(const unsigned char *str, size_t len, size_t offset)
Definition: unicode_case.c:312
#define PG_U_FINAL_SIGMA

References Assert(), check_final_sigma(), len, PG_U_FINAL_SIGMA, and str.

Referenced by casemap().

◆ convert_case()

static size_t convert_case ( char *  dst,
size_t  dstsize,
const char *  src,
ssize_t  srclen,
CaseKind  str_casekind,
bool  full,
WordBoundaryNext  wbnext,
void *  wbstate 
)
static

Definition at line 213 of file unicode_case.c.

216{
217 /* character CaseKind varies while titlecasing */
218 CaseKind chr_casekind = str_casekind;
219 size_t srcoff = 0;
220 size_t result_len = 0;
221 size_t boundary = 0;
222
223 Assert((str_casekind == CaseTitle && wbnext && wbstate) ||
224 (str_casekind != CaseTitle && !wbnext && !wbstate));
225
226 if (str_casekind == CaseTitle)
227 {
228 boundary = wbnext(wbstate);
229 Assert(boundary == 0); /* start of text is always a boundary */
230 }
231
232 while ((srclen < 0 || srcoff < srclen) && src[srcoff] != '\0')
233 {
234 pg_wchar u1 = utf8_to_unicode((unsigned char *) src + srcoff);
235 int u1len = unicode_utf8len(u1);
236 pg_wchar simple = 0;
237 const pg_wchar *special = NULL;
238 enum CaseMapResult casemap_result;
239
240 if (str_casekind == CaseTitle)
241 {
242 if (srcoff == boundary)
243 {
244 chr_casekind = full ? CaseTitle : CaseUpper;
245 boundary = wbnext(wbstate);
246 }
247 else
248 chr_casekind = CaseLower;
249 }
250
251 casemap_result = casemap(u1, chr_casekind, full, src, srclen, srcoff,
252 &simple, &special);
253
254 switch (casemap_result)
255 {
256 case CASEMAP_SELF:
257 /* no mapping; copy bytes from src */
258 Assert(simple == 0);
259 Assert(special == NULL);
260 if (result_len + u1len <= dstsize)
261 memcpy(dst + result_len, src + srcoff, u1len);
262
263 result_len += u1len;
264 break;
265 case CASEMAP_SIMPLE:
266 {
267 /* replace with single character */
268 pg_wchar u2 = simple;
269 pg_wchar u2len = unicode_utf8len(u2);
270
271 Assert(special == NULL);
272 if (result_len + u2len <= dstsize)
273 unicode_to_utf8(u2, (unsigned char *) dst + result_len);
274
275 result_len += u2len;
276 }
277 break;
278 case CASEMAP_SPECIAL:
279 /* replace with up to MAX_CASE_EXPANSION characters */
280 Assert(simple == 0);
281 for (int i = 0; i < MAX_CASE_EXPANSION && special[i]; i++)
282 {
283 pg_wchar u2 = special[i];
284 size_t u2len = unicode_utf8len(u2);
285
286 if (result_len + u2len <= dstsize)
287 unicode_to_utf8(u2, (unsigned char *) dst + result_len);
288
289 result_len += u2len;
290 }
291 break;
292 }
293
294 srcoff += u1len;
295 }
296
297 if (result_len < dstsize)
298 dst[result_len] = '\0';
299
300 return result_len;
301}
static unsigned char * unicode_to_utf8(pg_wchar c, unsigned char *utf8string)
Definition: pg_wchar.h:575
static int unicode_utf8len(pg_wchar c)
Definition: pg_wchar.h:607
static enum CaseMapResult casemap(pg_wchar u1, CaseKind casekind, bool full, const char *src, size_t srclen, size_t srcoff, pg_wchar *simple, const pg_wchar **special)
Definition: unicode_case.c:397
CaseMapResult
Definition: unicode_case.c:24
#define MAX_CASE_EXPANSION
@ CaseTitle
@ CaseLower
@ CaseUpper

References Assert(), CaseLower, casemap(), CASEMAP_SELF, CASEMAP_SIMPLE, CASEMAP_SPECIAL, CaseTitle, CaseUpper, i, MAX_CASE_EXPANSION, unicode_to_utf8(), unicode_utf8len(), and utf8_to_unicode().

Referenced by unicode_strfold(), unicode_strlower(), unicode_strtitle(), and unicode_strupper().

◆ find_case_map()

static pg_wchar find_case_map ( pg_wchar  ucs,
const pg_wchar map 
)
static

Definition at line 438 of file unicode_case.c.

439{
440 /* Fast path for codepoints < 0x80 */
441 if (ucs < 0x80)
442 /* The first elements in all tables are reserved as 0 (as NULL). */
443 return map[ucs + 1];
444 return map[case_index(ucs)];
445}

References case_index().

Referenced by unicode_casefold_simple(), unicode_lowercase_simple(), unicode_titlecase_simple(), and unicode_uppercase_simple().

◆ unicode_casefold_simple()

pg_wchar unicode_casefold_simple ( pg_wchar  code)

Definition at line 74 of file unicode_case.c.

75{
77
78 return cp != 0 ? cp : code;
79}
static pg_wchar find_case_map(pg_wchar ucs, const pg_wchar *map)
Definition: unicode_case.c:438
static const pg_wchar case_map_fold[1704]

References case_map_fold, and find_case_map().

◆ unicode_lowercase_simple()

pg_wchar unicode_lowercase_simple ( pg_wchar  code)

Definition at line 50 of file unicode_case.c.

51{
53
54 return cp != 0 ? cp : code;
55}
static const pg_wchar case_map_lower[1704]

References case_map_lower, and find_case_map().

Referenced by pg_wc_tolower().

◆ unicode_strfold()

size_t unicode_strfold ( char *  dst,
size_t  dstsize,
const char *  src,
ssize_t  srclen,
bool  full 
)

Definition at line 189 of file unicode_case.c.

191{
192 return convert_case(dst, dstsize, src, srclen, CaseFold, full, NULL,
193 NULL);
194}
static size_t convert_case(char *dst, size_t dstsize, const char *src, ssize_t srclen, CaseKind str_casekind, bool full, WordBoundaryNext wbnext, void *wbstate)
Definition: unicode_case.c:213
@ CaseFold

References CaseFold, and convert_case().

Referenced by strfold_builtin(), and tfunc_fold().

◆ unicode_strlower()

size_t unicode_strlower ( char *  dst,
size_t  dstsize,
const char *  src,
ssize_t  srclen,
bool  full 
)

Definition at line 101 of file unicode_case.c.

103{
104 return convert_case(dst, dstsize, src, srclen, CaseLower, full, NULL,
105 NULL);
106}

References CaseLower, and convert_case().

Referenced by strlower_builtin(), and tfunc_lower().

◆ unicode_strtitle()

size_t unicode_strtitle ( char *  dst,
size_t  dstsize,
const char *  src,
ssize_t  srclen,
bool  full,
WordBoundaryNext  wbnext,
void *  wbstate 
)

Definition at line 138 of file unicode_case.c.

140{
141 return convert_case(dst, dstsize, src, srclen, CaseTitle, full, wbnext,
142 wbstate);
143}

References CaseTitle, and convert_case().

Referenced by strtitle_builtin(), and tfunc_title().

◆ unicode_strupper()

size_t unicode_strupper ( char *  dst,
size_t  dstsize,
const char *  src,
ssize_t  srclen,
bool  full 
)

Definition at line 165 of file unicode_case.c.

167{
168 return convert_case(dst, dstsize, src, srclen, CaseUpper, full, NULL,
169 NULL);
170}

References CaseUpper, and convert_case().

Referenced by strupper_builtin(), and tfunc_upper().

◆ unicode_titlecase_simple()

pg_wchar unicode_titlecase_simple ( pg_wchar  code)

Definition at line 58 of file unicode_case.c.

59{
61
62 return cp != 0 ? cp : code;
63}
static const pg_wchar case_map_title[1704]

References case_map_title, and find_case_map().

◆ unicode_uppercase_simple()

pg_wchar unicode_uppercase_simple ( pg_wchar  code)

Definition at line 66 of file unicode_case.c.

67{
69
70 return cp != 0 ? cp : code;
71}
static const pg_wchar case_map_upper[1704]

References case_map_upper, and find_case_map().

Referenced by pg_wc_toupper().

Variable Documentation

◆ casekind_map

const pg_wchar* const casekind_map[NCaseKind]
static
Initial value:

Definition at line 33 of file unicode_case.c.

Referenced by casemap().