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, 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, bool full)
 
size_t unicode_strtitle (char *dst, size_t dstsize, const char *src, size_t srclen, bool full, WordBoundaryNext wbnext, void *wbstate)
 
size_t unicode_strupper (char *dst, size_t dstsize, const char *src, size_t srclen, bool full)
 
size_t unicode_strfold (char *dst, size_t dstsize, const char *src, size_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 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 393 of file unicode_case.c.

396{
397 uint16 idx;
398
399 /* Fast path for codepoints < 0x80 */
400 if (u1 < 0x80)
401 {
402 /*
403 * The first elements in all tables are reserved as 0 (as NULL). The
404 * data starts at index 1, not 0.
405 */
406 *simple = casekind_map[casekind][u1 + 1];
407
408 return CASEMAP_SIMPLE;
409 }
410
411 idx = case_index(u1);
412
413 if (idx == 0)
414 return CASEMAP_SELF;
415
416 if (full && case_map_special[idx] &&
418 src, srclen, srcoff))
419 {
421 return CASEMAP_SPECIAL;
422 }
423
424 *simple = casekind_map[casekind][idx];
425
426 return CASEMAP_SIMPLE;
427}
Datum idx(PG_FUNCTION_ARGS)
Definition _int_op.c:263
uint16_t uint16
Definition c.h:623
static int fb(int x)
char32_t map[NCaseKind][MAX_CASE_EXPANSION]
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 308 of file unicode_case.c.

309{
310 /* the start of the string is not preceded by a Cased character */
311 if (offset == 0)
312 return false;
313
314 /* iterate backwards, looking for Cased character */
315 for (int i = offset - 1; i >= 0; i--)
316 {
317 if ((str[i] & 0x80) == 0 || (str[i] & 0xC0) == 0xC0)
318 {
319 char32_t curr = utf8_to_unicode(str + i);
320
322 continue;
323 else if (pg_u_prop_cased(curr))
324 break;
325 else
326 return false;
327 }
328 else if ((str[i] & 0xC0) == 0x80)
329 continue;
330
331 Assert(false); /* invalid UTF-8 */
332 }
333
334 /* end of string is not followed by a Cased character */
335 if (offset == len)
336 return true;
337
338 /* iterate forwards, looking for Cased character */
339 for (int i = offset + 1; i < len && str[i] != '\0'; i++)
340 {
341 if ((str[i] & 0x80) == 0 || (str[i] & 0xC0) == 0xC0)
342 {
343 char32_t curr = utf8_to_unicode(str + i);
344
346 continue;
347 else if (pg_u_prop_cased(curr))
348 return false;
349 else
350 break;
351 }
352 else if ((str[i] & 0xC0) == 0x80)
353 continue;
354
355 Assert(false); /* invalid UTF-8 */
356 }
357
358 return true;
359}
#define Assert(condition)
Definition c.h:943
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
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, 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 366 of file unicode_case.c.

368{
369 if (conditions == 0)
370 return true;
371 else if (conditions == PG_U_FINAL_SIGMA)
372 return check_final_sigma((const unsigned char *) str, len, offset);
373
374 /* no other conditions supported */
375 Assert(false);
376 return false;
377}
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,
CaseKind  str_casekind,
bool  full,
WordBoundaryNext  wbnext,
void wbstate 
)
static

Definition at line 209 of file unicode_case.c.

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

References Assert, CaseLower, casemap(), CASEMAP_SELF, CASEMAP_SIMPLE, CASEMAP_SPECIAL, CaseTitle, CaseUpper, fb(), i, MAX_CASE_EXPANSION, memcpy(), unicode_to_utf8(), unicode_utf8len(), 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 434 of file unicode_case.c.

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

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,
bool  full 
)

Definition at line 185 of file unicode_case.c.

187{
188 return convert_case(dst, dstsize, src, srclen, CaseFold, full, NULL,
189 NULL);
190}
static size_t convert_case(char *dst, size_t dstsize, const char *src, size_t srclen, CaseKind str_casekind, bool full, WordBoundaryNext wbnext, void *wbstate)
@ CaseFold

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

Referenced by strfold_builtin(), and tfunc_fold().

◆ unicode_strlower()

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

Definition at line 100 of file unicode_case.c.

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

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,
bool  full,
WordBoundaryNext  wbnext,
void wbstate 
)

Definition at line 136 of file unicode_case.c.

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

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,
bool  full 
)

Definition at line 162 of file unicode_case.c.

164{
165 return convert_case(dst, dstsize, src, srclen, CaseUpper, full, NULL,
166 NULL);
167}

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().

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().