1 /*-------------------------------------------------------------------------
4 * Portable SQL-like case-independent comparisons and conversions.
6 * SQL99 specifies Unicode-aware case normalization, which we don't yet
7 * have the infrastructure for. Instead we use tolower() to provide a
8 * locale-aware translation. However, there are some locales where this
9 * is not right either (eg, Turkish may do strange things with 'i' and
10 * 'I'). Our current compromise is to use tolower() for characters with
11 * the high bit set, and use an ASCII-only downcasing for 7-bit
14 * NB: this code should match downcase_truncate_identifier() in scansup.c.
17 * Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group
21 *-------------------------------------------------------------------------
29 * Case-independent comparison of two null-terminated strings.
32 pg_strcasecmp(const char *s1
, const char *s2
)
36 unsigned char ch1
= (unsigned char) *s1
++;
37 unsigned char ch2
= (unsigned char) *s2
++;
41 if (ch1
>= 'A' && ch1
<= 'Z')
43 else if (IS_HIGHBIT_SET(ch1
) && isupper(ch1
))
46 if (ch2
>= 'A' && ch2
<= 'Z')
48 else if (IS_HIGHBIT_SET(ch2
) && isupper(ch2
))
52 return (int) ch1
- (int) ch2
;
61 * Case-independent comparison of two not-necessarily-null-terminated strings.
62 * At most n bytes will be examined from each string.
65 pg_strncasecmp(const char *s1
, const char *s2
, size_t n
)
69 unsigned char ch1
= (unsigned char) *s1
++;
70 unsigned char ch2
= (unsigned char) *s2
++;
74 if (ch1
>= 'A' && ch1
<= 'Z')
76 else if (IS_HIGHBIT_SET(ch1
) && isupper(ch1
))
79 if (ch2
>= 'A' && ch2
<= 'Z')
81 else if (IS_HIGHBIT_SET(ch2
) && isupper(ch2
))
85 return (int) ch1
- (int) ch2
;
94 * Fold a character to upper case.
96 * Unlike some versions of toupper(), this is safe to apply to characters
97 * that aren't lower case letters. Note however that the whole thing is
98 * a bit bogus for multibyte character sets.
101 pg_toupper(unsigned char ch
)
103 if (ch
>= 'a' && ch
<= 'z')
105 else if (IS_HIGHBIT_SET(ch
) && islower(ch
))
111 * Fold a character to lower case.
113 * Unlike some versions of tolower(), this is safe to apply to characters
114 * that aren't upper case letters. Note however that the whole thing is
115 * a bit bogus for multibyte character sets.
118 pg_tolower(unsigned char ch
)
120 if (ch
>= 'A' && ch
<= 'Z')
122 else if (IS_HIGHBIT_SET(ch
) && isupper(ch
))