PostgreSQL Source Code git master
Loading...
Searching...
No Matches
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 char32_t find_case_map (char32_t ucs, const char32_t *map)
 
static size_t convert_case (char *dst, size_t dstsize, const char *src, size_t srclen, size_t *pconsumed, CaseKind str_casekind, bool full, WordBoundaryNext wbnext, void *wbstate)
 
static enum CaseMapResult casemap (char32_t u1, CaseKind casekind, bool full, const char *src, size_t srclen, size_t srcoff, char32_t *simple, const char32_t **special)
 
char32_t unicode_lowercase_simple (char32_t code)
 
char32_t unicode_titlecase_simple (char32_t code)
 
char32_t unicode_uppercase_simple (char32_t code)
 
char32_t unicode_casefold_simple (char32_t code)
 
size_t unicode_strlower (char *dst, size_t dstsize, const char *src, size_t srclen, size_t *pconsumed, bool full)
 
size_t unicode_strtitle (char *dst, size_t dstsize, const char *src, size_t srclen, size_t *pconsumed, bool full, WordBoundaryNext wbnext, void *wbstate)
 
size_t unicode_strupper (char *dst, size_t dstsize, const char *src, size_t srclen, size_t *pconsumed, bool full)
 
size_t unicode_strfold (char *dst, size_t dstsize, const char *src, size_t srclen, size_t *pconsumed, bool full)
 
static int utf8_mblen (const unsigned char *s)
 
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 char32_t *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
@ CASEMAP_SIMPLE
@ CASEMAP_SELF

Function Documentation

◆ casemap()

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

Definition at line 433 of file unicode_case.c.

436{
437 uint16 idx;
438
439 /* Fast path for codepoints < 0x80 */
440 if (u1 < 0x80)
441 {
442 /*
443 * The first elements in all tables are reserved as 0 (as NULL). The
444 * data starts at index 1, not 0.
445 */
446 *simple = casekind_map[casekind][u1 + 1];
447
448 return CASEMAP_SIMPLE;
449 }
450
451 idx = case_index(u1);
452
453 if (idx == 0)
454 return CASEMAP_SELF;
455
456 if (full && case_map_special[idx] &&
458 src, srclen, srcoff))
459 {
461 return CASEMAP_SPECIAL;
462 }
463
464 *simple = casekind_map[casekind][idx];
465
466 return CASEMAP_SIMPLE;
467}
Datum idx(PG_FUNCTION_ARGS)
Definition _int_op.c:263
uint16_t uint16
Definition c.h:682
static int fb(int x)
char32_t map[NCaseKind][UNICODE_MAX_CASEMAP_CODEPOINTS]
static const char32_t *const casekind_map[NCaseKind]
static bool check_special_conditions(int conditions, const char *str, size_t len, size_t offset)
static const pg_special_case special_case[106]
static uint16 case_index(char32_t cp)
static const uint8 case_map_special[1732]

References case_index(), case_map_special, casekind_map, CASEMAP_SELF, CASEMAP_SIMPLE, CASEMAP_SPECIAL, check_special_conditions(), fb(), 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 343 of file unicode_case.c.

344{
345 bool preceded_by_cased = false;
346 bool followed_by_cased = false;
347 char32_t curr;
348 int ulen;
349
350 /* iterate backwards looking for preceding character */
351 for (size_t i = offset; i > 0;)
352 {
353 /* skip backwards through continuation bytes */
354 i--;
355 if ((str[i] & 0xC0) == 0x80)
356 continue;
357
358 /* now at leading byte of previous sequence */
359 Assert((str[i] & 0x80) == 0 || (str[i] & 0xC0) == 0xC0);
360
361 ulen = utf8_mblen((const unsigned char *) str + i);
362
363 /* invalid UTF8 */
365 return false;
366
367 curr = utf8_to_unicode((const unsigned char *) str + i);
368
370 {
372 break;
373 }
374 }
375
376 ulen = utf8_mblen((const unsigned char *) str + offset);
377
378 /* iterate forward looking for following character */
379 for (size_t i = offset + ulen; i < len;)
380 {
381 ulen = utf8_mblen((const unsigned char *) str + i);
382
383 /* invalid UTF8 */
385 return false;
386
387 curr = utf8_to_unicode((const unsigned char *) str + i);
388
390 {
392 break;
393 }
394
395 i += ulen;
396 }
397
399}
#define Assert(condition)
Definition c.h:1002
const char * str
int i
Definition isn.c:77
static char32_t utf8_to_unicode(const unsigned char *c)
Definition mbprint.c:53
const void size_t len
static int utf8_mblen(const unsigned char *s)
bool pg_u_prop_cased(char32_t code)
bool pg_u_prop_case_ignorable(char32_t code)

References Assert, fb(), i, len, pg_u_prop_case_ignorable(), pg_u_prop_cased(), str, utf8_mblen(), 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 406 of file unicode_case.c.

408{
409 if (conditions == 0)
410 return true;
411 else if (conditions == PG_U_FINAL_SIGMA)
412 return check_final_sigma((const unsigned char *) str, len, offset);
413
414 /* no other conditions supported */
415 Assert(false);
416 return false;
417}
static bool check_final_sigma(const unsigned char *str, size_t len, size_t offset)
#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,
size_t  srclen,
size_t pconsumed,
CaseKind  str_casekind,
bool  full,
WordBoundaryNext  wbnext,
void wbstate 
)
static

Definition at line 230 of file unicode_case.c.

233{
234 /* character CaseKind varies while titlecasing */
236 size_t srcoff = 0;
237 size_t result_len = 0;
238 size_t boundary = 0;
239
240 /*
241 * Must be guaranteed by caller to avoid overflow (text values limited to
242 * MaxAllocSize anyway).
243 */
245
247 (str_casekind != CaseTitle && !wbnext && !wbstate));
248
249 if (str_casekind == CaseTitle)
250 {
251 boundary = wbnext(wbstate);
252 Assert(boundary == 0); /* start of text is always a boundary */
253 }
254
255 while (srcoff < srclen)
256 {
257 int u1len = utf8_mblen((const unsigned char *) src + srcoff);
258 char32_t u1;
259 char32_t simple = 0;
260 const char32_t *special = NULL;
262
263 /* invalid UTF8 */
265 break;
266
267 u1 = utf8_to_unicode((const unsigned char *) src + srcoff);
268
269 if (str_casekind == CaseTitle)
270 {
271 if (srcoff == boundary)
272 {
274 boundary = wbnext(wbstate);
275 }
276 else
278 }
279
281 &simple, &special);
282
283 switch (casemap_result)
284 {
285 case CASEMAP_SELF:
286 /* no mapping; copy bytes from src */
287 Assert(simple == 0);
288 Assert(special == NULL);
289 if (result_len + u1len <= dstsize)
290 memcpy(dst + result_len, src + srcoff, u1len);
291
292 result_len += u1len;
293 break;
294 case CASEMAP_SIMPLE:
295 {
296 /* replace with single character */
297 char32_t u2 = simple;
298 char32_t u2len = unicode_utf8len(u2);
299
300 Assert(special == NULL);
301 if (result_len + u2len <= dstsize)
302 unicode_to_utf8(u2, (unsigned char *) dst + result_len);
303
304 result_len += u2len;
305 }
306 break;
307 case CASEMAP_SPECIAL:
308 /* replace with up to UNICODE_MAX_CASEMAP_CODEPOINTS */
309 Assert(simple == 0);
310 for (int i = 0; i < UNICODE_MAX_CASEMAP_CODEPOINTS && special[i]; i++)
311 {
312 char32_t u2 = special[i];
313 size_t u2len = unicode_utf8len(u2);
314
315 if (result_len + u2len <= dstsize)
316 unicode_to_utf8(u2, (unsigned char *) dst + result_len);
317
318 result_len += u2len;
319 }
320 break;
321 }
322
323 srcoff += u1len;
324 }
325
326 if (result_len < dstsize)
327 dst[result_len] = '\0';
328
329 *pconsumed = srcoff;
330 return result_len;
331}
memcpy(sums, checksumBaseOffsets, sizeof(checksumBaseOffsets))
static unsigned char * unicode_to_utf8(char32_t c, unsigned char *utf8string)
Definition pg_wchar.h:428
static int unicode_utf8len(char32_t c)
Definition pg_wchar.h:460
static enum CaseMapResult casemap(char32_t u1, CaseKind casekind, bool full, const char *src, size_t srclen, size_t srcoff, char32_t *simple, const char32_t **special)
CaseMapResult
@ CaseTitle
@ CaseLower
@ CaseUpper
#define UNICODE_MAX_CASEMAP_CODEPOINTS
#define UTF8_MAX_CASEMAP_EXPANSION

References Assert, CaseLower, casemap(), CASEMAP_SELF, CASEMAP_SIMPLE, CASEMAP_SPECIAL, CaseTitle, CaseUpper, fb(), i, memcpy(), UNICODE_MAX_CASEMAP_CODEPOINTS, unicode_to_utf8(), unicode_utf8len(), UTF8_MAX_CASEMAP_EXPANSION, utf8_mblen(), and utf8_to_unicode().

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

◆ find_case_map()

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

Definition at line 474 of file unicode_case.c.

475{
476 /* Fast path for codepoints < 0x80 */
477 if (ucs < 0x80)
478 /* The first elements in all tables are reserved as 0 (as NULL). */
479 return map[ucs + 1];
480 return map[case_index(ucs)];
481}

References case_index(), and fb().

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

◆ unicode_casefold_simple()

char32_t unicode_casefold_simple ( char32_t  code)

Definition at line 74 of file unicode_case.c.

75{
76 char32_t cp = find_case_map(code, case_map_fold);
77
78 return cp != 0 ? cp : code;
79}
static char32_t find_case_map(char32_t ucs, const char32_t *map)
static const char32_t case_map_fold[1732]

References case_map_fold, fb(), and find_case_map().

◆ unicode_lowercase_simple()

char32_t unicode_lowercase_simple ( char32_t  code)

Definition at line 50 of file unicode_case.c.

51{
52 char32_t cp = find_case_map(code, case_map_lower);
53
54 return cp != 0 ? cp : code;
55}
static const char32_t case_map_lower[1732]

References case_map_lower, fb(), and find_case_map().

Referenced by wc_tolower_builtin().

◆ unicode_strfold()

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

Definition at line 190 of file unicode_case.c.

192{
193 return convert_case(dst, dstsize, src, srclen, pconsumed, CaseFold, full,
194 NULL, NULL);
195}
static size_t convert_case(char *dst, size_t dstsize, const char *src, size_t srclen, size_t *pconsumed, CaseKind str_casekind, bool full, WordBoundaryNext wbnext, void *wbstate)
@ CaseFold

References CaseFold, convert_case(), and fb().

Referenced by strfold_builtin(), test_convert_case(), and tfunc_fold().

◆ unicode_strlower()

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

Definition at line 101 of file unicode_case.c.

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

References CaseLower, convert_case(), and fb().

Referenced by strlower_builtin(), and tfunc_lower().

◆ unicode_strtitle()

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

Definition at line 138 of file unicode_case.c.

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

References CaseTitle, convert_case(), and fb().

Referenced by strtitle_builtin(), and tfunc_title().

◆ unicode_strupper()

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

Definition at line 166 of file unicode_case.c.

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

References CaseUpper, convert_case(), and fb().

Referenced by strupper_builtin(), and tfunc_upper().

◆ unicode_titlecase_simple()

char32_t unicode_titlecase_simple ( char32_t  code)

Definition at line 58 of file unicode_case.c.

59{
60 char32_t cp = find_case_map(code, case_map_title);
61
62 return cp != 0 ? cp : code;
63}
static const char32_t case_map_title[1732]

References case_map_title, fb(), and find_case_map().

◆ unicode_uppercase_simple()

char32_t unicode_uppercase_simple ( char32_t  code)

Definition at line 66 of file unicode_case.c.

67{
68 char32_t cp = find_case_map(code, case_map_upper);
69
70 return cp != 0 ? cp : code;
71}
static const char32_t case_map_upper[1732]

References case_map_upper, fb(), and find_case_map().

Referenced by wc_toupper_builtin().

◆ utf8_mblen()

static int utf8_mblen ( const unsigned char s)
static

Definition at line 199 of file unicode_case.c.

200{
201 if ((*s & 0x80) == 0)
202 return 1;
203 else if ((*s & 0xe0) == 0xc0)
204 return 2;
205 else if ((*s & 0xf0) == 0xe0)
206 return 3;
207 else if ((*s & 0xf8) == 0xf0)
208 return 4;
209 else
210 return -1;
211}

Referenced by check_final_sigma(), and convert_case().

Variable Documentation

◆ casekind_map

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

Definition at line 33 of file unicode_case.c.

Referenced by casemap().