Correct erroneous comment regarding realpath availability
[ciopfs.git] / unicode-icu.c
blob898ce778d0239cfc2dde1d92420c34f62bc604e9
1 #include <unicode/ustring.h>
2 #include <unicode/uchar.h>
4 static inline UChar *utf8_to_utf16(const char *str, int32_t *length)
6 UChar *ustr;
7 UErrorCode status = U_ZERO_ERROR;
9 u_strFromUTF8(NULL, 0, length, str, -1, &status);
10 status = U_ZERO_ERROR;
11 (*length)++; /* for the NUL char */
12 ustr = malloc(sizeof(UChar) * (*length));
13 if (!ustr)
14 return NULL;
15 u_strFromUTF8(ustr, *length, NULL, str, -1, &status);
16 if (U_FAILURE(status)) {
17 free(ustr);
18 return NULL;
20 return ustr;
23 static inline char *utf16_to_utf8(UChar *ustr, int32_t *length)
25 char *str;
26 UErrorCode status = U_ZERO_ERROR;
28 u_strToUTF8(NULL, 0, length, ustr, -1, &status);
29 status = U_ZERO_ERROR;
30 (*length)++; /* for the NUL char */
31 str = malloc(*length);
32 if (!str)
33 return NULL;
34 u_strToUTF8(str, *length, NULL, ustr, -1, &status);
35 if (U_FAILURE(status)) {
36 free(str);
37 return NULL;
39 return str;
42 static inline bool str_contains_upper(const char *s)
44 bool ret = false;
45 int32_t length, i;
46 UChar32 c;
47 UChar *ustr = utf8_to_utf16(s, &length);
48 if (!ustr)
49 return true;
50 for (i = 0; i < length; /* U16_NEXT post-increments */) {
51 U16_NEXT(ustr, i, length, c);
52 if (u_isupper(c)) {
53 ret = true;
54 goto out;
57 out:
58 free(ustr);
59 return ret;
62 static inline char *str_fold(const char *s)
64 int32_t length;
65 char *str;
66 UChar *ustr;
67 UErrorCode status = U_ZERO_ERROR;
69 ustr = utf8_to_utf16(s, &length);
70 if (!ustr)
71 return NULL;
72 u_strFoldCase(ustr, length, ustr, length, U_FOLD_CASE_EXCLUDE_SPECIAL_I, &status);
73 if (U_FAILURE(status))
74 return NULL;
75 str = utf16_to_utf8(ustr, &length);
76 free(ustr);
77 return str;