2 * fontconfig/src/fcstr.c
4 * Copyright © 2000 Keith Packard
6 * Permission to use, copy, modify, distribute, and sell this software and its
7 * documentation for any purpose is hereby granted without fee, provided that
8 * the above copyright notice appear in all copies and that both that
9 * copyright notice and this permission notice appear in supporting
10 * documentation, and that the name of the author(s) not be used in
11 * advertising or publicity pertaining to distribution of the software without
12 * specific, written prior permission. The authors make no
13 * representations about the suitability of this software for any purpose. It
14 * is provided "as is" without express or implied warranty.
16 * THE AUTHOR(S) DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
17 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
18 * EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY SPECIAL, INDIRECT OR
19 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
20 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
21 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
22 * PERFORMANCE OF THIS SOFTWARE.
36 /* Objects MT-safe for readonly access. */
39 FcStrCopy (const FcChar8 *s)
45 len = strlen ((char *) s) + 1;
46 r = (FcChar8 *) malloc (len);
54 FcStrPlus (const FcChar8 *s1, const FcChar8 *s2)
56 int s1l = strlen ((char *) s1);
57 int s2l = strlen ((char *) s2);
58 int l = s1l + s2l + 1;
59 FcChar8 *s = malloc (l);
64 memcpy (s + s1l, s2, s2l + 1);
69 FcStrFree (FcChar8 *s)
75 #include "../fc-case/fccase.h"
77 #define FcCaseFoldUpperCount(cf) \
78 ((cf)->method == FC_CASE_FOLD_FULL ? 1 : (cf)->count)
80 typedef struct _FcCaseWalker {
83 FcChar8 utf8[FC_MAX_CASE_FOLD_CHARS + 1];
87 FcStrCaseWalkerInit (const FcChar8 *src, FcCaseWalker *w)
94 FcStrCaseWalkerLong (FcCaseWalker *w, FcChar8 r)
98 int len = strlen((char*)w->src);
100 slen = FcUtf8ToUcs4 (w->src - 1, &ucs4, len + 1);
103 if (FC_MIN_FOLD_CHAR <= ucs4 && ucs4 <= FC_MAX_FOLD_CHAR)
106 int max = FC_NUM_CASE_FOLD;
110 int mid = (min + max) >> 1;
111 FcChar32 low = fcCaseFold[mid].upper;
112 FcChar32 high = low + FcCaseFoldUpperCount (&fcCaseFold[mid]);
120 const FcCaseFold *fold = &fcCaseFold[mid];
123 switch (fold->method) {
124 case FC_CASE_FOLD_EVEN_ODD:
125 if ((ucs4 & 1) != (fold->upper & 1))
127 /* fall through ... */
129 dlen = FcUcs4ToUtf8 (ucs4 + fold->offset, w->utf8);
131 case FC_CASE_FOLD_FULL:
133 memcpy (w->utf8, fcCaseFoldChars + fold->offset, dlen);
137 /* consume rest of src utf-8 bytes */
140 /* read from temp buffer */
141 w->utf8[dlen] = '\0';
151 FcStrCaseWalkerNext (FcCaseWalker *w)
157 if ((r = *w->read++))
163 if ((r & 0xc0) == 0xc0)
164 return FcStrCaseWalkerLong (w, r);
165 if ('A' <= r && r <= 'Z')
171 FcStrCaseWalkerNextIgnoreBlanks (FcCaseWalker *w)
177 if ((r = *w->read++))
186 if ((r & 0xc0) == 0xc0)
187 return FcStrCaseWalkerLong (w, r);
188 if ('A' <= r && r <= 'Z')
194 FcStrDowncase (const FcChar8 *s)
200 FcStrCaseWalkerInit (s, &w);
201 while (FcStrCaseWalkerNext (&w))
203 d = dst = malloc (len + 1);
206 FcStrCaseWalkerInit (s, &w);
207 while ((*d++ = FcStrCaseWalkerNext (&w)));
212 FcStrCmpIgnoreCase (const FcChar8 *s1, const FcChar8 *s2)
217 if (s1 == s2) return 0;
219 FcStrCaseWalkerInit (s1, &w1);
220 FcStrCaseWalkerInit (s2, &w2);
224 c1 = FcStrCaseWalkerNext (&w1);
225 c2 = FcStrCaseWalkerNext (&w2);
226 if (!c1 || (c1 != c2))
229 return (int) c1 - (int) c2;
233 FcStrCmpIgnoreBlanksAndCase (const FcChar8 *s1, const FcChar8 *s2)
238 if (s1 == s2) return 0;
240 FcStrCaseWalkerInit (s1, &w1);
241 FcStrCaseWalkerInit (s2, &w2);
245 c1 = FcStrCaseWalkerNextIgnoreBlanks (&w1);
246 c2 = FcStrCaseWalkerNextIgnoreBlanks (&w2);
247 if (!c1 || (c1 != c2))
250 return (int) c1 - (int) c2;
254 FcStrCmp (const FcChar8 *s1, const FcChar8 *s2)
267 return (int) c1 - (int) c2;
272 _FcStrRegexCmp (const FcChar8 *s, const FcChar8 *regex, int cflags, int eflags)
277 if ((ret = regcomp (®, (const char *)regex, cflags)) != 0)
279 if (FcDebug () & FC_DBG_MATCHV)
283 regerror (ret, ®, buf, 512);
284 printf("Regexp compile error: %s\n", buf);
288 ret = regexec (®, (const char *)s, 0, NULL, eflags);
291 if (FcDebug () & FC_DBG_MATCHV)
295 regerror (ret, ®, buf, 512);
296 printf("Regexp exec error: %s\n", buf);
301 return ret == 0 ? FcTrue : FcFalse;
304 # define _FcStrRegexCmp(_s_, _regex_, _cflags_, _eflags_) (FcFalse)
308 FcStrRegexCmp (const FcChar8 *s, const FcChar8 *regex)
310 return _FcStrRegexCmp (s, regex, REG_EXTENDED | REG_NOSUB, 0);
314 FcStrRegexCmpIgnoreCase (const FcChar8 *s, const FcChar8 *regex)
316 return _FcStrRegexCmp (s, regex, REG_EXTENDED | REG_NOSUB | REG_ICASE, 0);
320 * Return a hash value for a string
324 FcStrHashIgnoreCase (const FcChar8 *s)
330 FcStrCaseWalkerInit (s, &w);
331 while ((c = FcStrCaseWalkerNext (&w)))
332 h = ((h << 3) ^ (h >> 3)) ^ c;
337 * Is the head of s1 equal to s2?
341 FcStrIsAtIgnoreBlanksAndCase (const FcChar8 *s1, const FcChar8 *s2)
346 FcStrCaseWalkerInit (s1, &w1);
347 FcStrCaseWalkerInit (s2, &w2);
351 c1 = FcStrCaseWalkerNextIgnoreBlanks (&w1);
352 c2 = FcStrCaseWalkerNextIgnoreBlanks (&w2);
353 if (!c1 || (c1 != c2))
356 return c1 == c2 || !c2;
360 * Does s1 contain an instance of s2 (ignoring blanks and case)?
364 FcStrContainsIgnoreBlanksAndCase (const FcChar8 *s1, const FcChar8 *s2)
368 if (FcStrIsAtIgnoreBlanksAndCase (s1, s2))
376 FcCharIsPunct (const FcChar8 c)
396 * Is the head of s1 equal to s2?
400 FcStrIsAtIgnoreCase (const FcChar8 *s1, const FcChar8 *s2)
405 FcStrCaseWalkerInit (s1, &w1);
406 FcStrCaseWalkerInit (s2, &w2);
410 c1 = FcStrCaseWalkerNext (&w1);
411 c2 = FcStrCaseWalkerNext (&w2);
412 if (!c1 || (c1 != c2))
415 return c1 == c2 || !c2;
419 * Does s1 contain an instance of s2 (ignoring blanks and case)?
423 FcStrContainsIgnoreCase (const FcChar8 *s1, const FcChar8 *s2)
427 if (FcStrIsAtIgnoreCase (s1, s2))
435 * Does s1 contain an instance of s2 on a word boundary (ignoring case)?
439 FcStrContainsWord (const FcChar8 *s1, const FcChar8 *s2)
441 FcBool wordStart = FcTrue;
442 int s1len = strlen ((char *) s1);
443 int s2len = strlen ((char *) s2);
445 while (s1len >= s2len)
448 FcStrIsAtIgnoreCase (s1, s2) &&
449 (s1len == s2len || FcCharIsPunct (s1[s2len])))
454 if (FcCharIsPunct (*s1))
463 FcStrStrIgnoreCase (const FcChar8 *s1, const FcChar8 *s2)
475 FcStrCaseWalkerInit (s1, &w1);
476 FcStrCaseWalkerInit (s2, &w2);
478 c2 = FcStrCaseWalkerNext (&w2);
483 c1 = FcStrCaseWalkerNext (&w1);
488 FcCaseWalker w1t = w1;
489 FcCaseWalker w2t = w2;
494 c1t = FcStrCaseWalkerNext (&w1t);
495 c2t = FcStrCaseWalkerNext (&w2t);
508 FcStrStr (const FcChar8 *s1, const FcChar8 *s2)
511 const FcChar8 * p = s1;
512 const FcChar8 * b = s2;
541 if (c1 && c2 && c1 != c2)
558 FcUtf8ToUcs4 (const FcChar8 *src_orig,
562 const FcChar8 *src = src_orig;
578 else if (!(s & 0x40))
582 else if (!(s & 0x20))
587 else if (!(s & 0x10))
592 else if (!(s & 0x08))
597 else if (!(s & 0x04))
602 else if ( ! (s & 0x02))
619 if ((s & 0xc0) != 0x80)
625 return src - src_orig;
629 FcUtf8Len (const FcChar8 *string,
643 clen = FcUtf8ToUcs4 (string, &c, len);
644 if (clen <= 0) /* malformed UTF8 string */
655 else if (max > 0x100)
663 FcUcs4ToUtf8 (FcChar32 ucs4,
664 FcChar8 dest[FC_UTF8_MAX_LEN])
669 if (ucs4 < 0x80) { *d++= ucs4; bits= -6; }
670 else if (ucs4 < 0x800) { *d++= ((ucs4 >> 6) & 0x1F) | 0xC0; bits= 0; }
671 else if (ucs4 < 0x10000) { *d++= ((ucs4 >> 12) & 0x0F) | 0xE0; bits= 6; }
672 else if (ucs4 < 0x200000) { *d++= ((ucs4 >> 18) & 0x07) | 0xF0; bits= 12; }
673 else if (ucs4 < 0x4000000) { *d++= ((ucs4 >> 24) & 0x03) | 0xF8; bits= 18; }
674 else if (ucs4 < 0x80000000) { *d++= ((ucs4 >> 30) & 0x01) | 0xFC; bits= 24; }
677 for ( ; bits >= 0; bits-= 6) {
678 *d++= ((ucs4 >> bits) & 0x3F) | 0x80;
683 #define GetUtf16(src,endian) \
684 ((FcChar16) ((src)[endian == FcEndianBig ? 0 : 1] << 8) | \
685 (FcChar16) ((src)[endian == FcEndianBig ? 1 : 0]))
688 FcUtf16ToUcs4 (const FcChar8 *src_orig,
691 int len) /* in bytes */
693 const FcChar8 *src = src_orig;
700 a = GetUtf16 (src, endian); src += 2; len -= 2;
703 * Check for surrogate
705 if ((a & 0xfc00) == 0xd800)
709 b = GetUtf16 (src, endian); src += 2; len -= 2;
711 * Check for invalid surrogate sequence
713 if ((b & 0xfc00) != 0xdc00)
715 result = ((((FcChar32) a & 0x3ff) << 10) |
716 ((FcChar32) b & 0x3ff)) + 0x10000;
721 return src - src_orig;
725 FcUtf16Len (const FcChar8 *string,
727 int len, /* in bytes */
740 clen = FcUtf16ToUcs4 (string, endian, &c, len);
741 if (clen <= 0) /* malformed UTF8 string */
752 else if (max > 0x100)
760 FcStrBufInit (FcStrBuf *buf, FcChar8 *init, int size)
768 buf->buf = buf->buf_static;
769 buf->size = sizeof (buf->buf_static);
771 buf->allocated = FcFalse;
772 buf->failed = FcFalse;
777 FcStrBufDestroy (FcStrBuf *buf)
782 FcStrBufInit (buf, 0, 0);
787 FcStrBufDone (FcStrBuf *buf)
794 ret = malloc (buf->len + 1);
797 memcpy (ret, buf->buf, buf->len);
798 ret[buf->len] = '\0';
800 FcStrBufDestroy (buf);
805 FcStrBufDoneStatic (FcStrBuf *buf)
807 FcStrBufChar (buf, '\0');
816 FcStrBufChar (FcStrBuf *buf, FcChar8 c)
818 if (buf->len == buf->size)
828 size = buf->size * 2;
829 new = realloc (buf->buf, size);
833 size = buf->size + 64;
837 buf->allocated = FcTrue;
838 memcpy (new, buf->buf, buf->len);
843 buf->failed = FcTrue;
849 buf->buf[buf->len++] = c;
854 FcStrBufString (FcStrBuf *buf, const FcChar8 *s)
858 if (!FcStrBufChar (buf, c))
864 FcStrBufData (FcStrBuf *buf, const FcChar8 *s, int len)
867 if (!FcStrBufChar (buf, *s++))
873 FcStrUsesHome (const FcChar8 *s)
879 FcStrCopyFilename (const FcChar8 *s)
885 FcChar8 *home = FcConfigHome ();
890 size = strlen ((char *) home) + strlen ((char *) s);
891 full = (FcChar8 *) malloc (size);
894 strcpy ((char *) full, (char *) home);
895 strcat ((char *) full, (char *) s + 1);
896 new = FcStrCanonFilename (full);
900 new = FcStrCanonFilename (s);
906 FcStrLastSlash (const FcChar8 *path)
910 slash = (FcChar8 *) strrchr ((const char *) path, '/');
915 backslash = (FcChar8 *) strrchr ((const char *) path, '\\');
916 if (!slash || (backslash && backslash > slash))
925 FcStrDirname (const FcChar8 *file)
930 slash = FcStrLastSlash (file);
932 return FcStrCopy ((FcChar8 *) ".");
933 dir = malloc ((slash - file) + 1);
936 strncpy ((char *) dir, (const char *) file, slash - file);
937 dir[slash - file] = '\0';
942 FcStrBasename (const FcChar8 *file)
946 slash = FcStrLastSlash (file);
948 return FcStrCopy (file);
949 return FcStrCopy (slash + 1);
953 FcStrCanonAbsoluteFilename (const FcChar8 *s)
957 const FcChar8 *slash;
960 size = strlen ((char *) s) + 1;
961 file = malloc (size);
967 if (*s == '/' && *(s+1) == '/') /* Network path, do not squash // */
971 if (*s == '/' || *s == '\0')
977 f -= 1; /* squash // and trim final / from file */
980 if (!strncmp ((char *) slash, "/.", 2))
982 f -= 2; /* trim /. from file */
986 if (!strncmp ((char *) slash, "/..", 3))
988 f -= 3; /* trim /.. from file */
1007 * Convert '\\' to '/' , remove double '/'
1010 FcConvertDosPath (char *str)
1012 size_t len = strlen (str);
1015 char *end = str + len;
1044 FcStrCanonFilename (const FcChar8 *s)
1047 FcChar8 full[FC_MAX_FILE_LEN + 2];
1048 int size = GetFullPathName ((LPCSTR) s, sizeof (full) -1,
1049 (LPSTR) full, NULL);
1052 perror ("GetFullPathName");
1054 FcConvertDosPath ((char *) full);
1055 return FcStrCanonAbsoluteFilename (full);
1058 return FcStrCanonAbsoluteFilename (s);
1064 FcChar8 cwd[FC_MAX_FILE_LEN + 2];
1065 if (getcwd ((char *) cwd, FC_MAX_FILE_LEN) == NULL)
1067 strcat ((char *) cwd, "/");
1068 full = FcStrPlus (cwd, s);
1069 file = FcStrCanonAbsoluteFilename (full);
1078 FcStrSetCreate (void)
1080 FcStrSet *set = malloc (sizeof (FcStrSet));
1083 FcRefInit (&set->ref, 1);
1091 _FcStrSetAppend (FcStrSet *set, FcChar8 *s)
1093 if (FcStrSetMember (set, s))
1098 if (set->num == set->size)
1100 FcChar8 **strs = malloc ((set->size + 2) * sizeof (FcChar8 *));
1105 memcpy (strs, set->strs, set->num * sizeof (FcChar8 *));
1108 set->size = set->size + 1;
1111 set->strs[set->num++] = s;
1112 set->strs[set->num] = 0;
1117 FcStrSetMember (FcStrSet *set, const FcChar8 *s)
1121 for (i = 0; i < set->num; i++)
1122 if (!FcStrCmp (set->strs[i], s))
1128 FcStrSetEqual (FcStrSet *sa, FcStrSet *sb)
1131 if (sa->num != sb->num)
1133 for (i = 0; i < sa->num; i++)
1134 if (!FcStrSetMember (sb, sa->strs[i]))
1140 FcStrSetAdd (FcStrSet *set, const FcChar8 *s)
1142 FcChar8 *new = FcStrCopy (s);
1145 if (!_FcStrSetAppend (set, new))
1154 FcStrSetAddFilename (FcStrSet *set, const FcChar8 *s)
1156 FcChar8 *new = FcStrCopyFilename (s);
1159 if (!_FcStrSetAppend (set, new))
1168 FcStrSetAddLangs (FcStrSet *strs, const char *languages)
1170 const char *p = languages, *next;
1171 FcChar8 lang[128] = {0}, *normalized_lang;
1173 FcBool ret = FcFalse;
1178 while ((next = strchr (p, ':')))
1181 len = FC_MIN (len, 127);
1182 strncpy ((char *) lang, p, len);
1184 /* ignore an empty item */
1187 normalized_lang = FcLangNormalize ((const FcChar8 *) lang);
1188 if (normalized_lang)
1190 FcStrSetAdd (strs, normalized_lang);
1191 FcStrFree (normalized_lang);
1199 normalized_lang = FcLangNormalize ((const FcChar8 *) p);
1200 if (normalized_lang)
1202 FcStrSetAdd (strs, normalized_lang);
1203 FcStrFree (normalized_lang);
1212 FcStrSetDel (FcStrSet *set, const FcChar8 *s)
1216 for (i = 0; i < set->num; i++)
1217 if (!FcStrCmp (set->strs[i], s))
1219 FcStrFree (set->strs[i]);
1221 * copy remaining string pointers and trailing
1224 memmove (&set->strs[i], &set->strs[i+1],
1225 (set->num - i) * sizeof (FcChar8 *));
1233 FcStrSetDestroy (FcStrSet *set)
1237 /* We rely on this in FcGetDefaultLangs for caching. */
1238 if (FcRefIsConst (&set->ref))
1241 if (FcRefDec (&set->ref) != 1)
1244 for (i = 0; i < set->num; i++)
1245 FcStrFree (set->strs[i]);
1252 FcStrListCreate (FcStrSet *set)
1256 list = malloc (sizeof (FcStrList));
1260 FcRefInc (&set->ref);
1266 FcStrListNext (FcStrList *list)
1268 if (list->n >= list->set->num)
1270 return list->set->strs[list->n++];
1274 FcStrListDone (FcStrList *list)
1276 FcStrSetDestroy (list->set);
1281 #include "fcaliastail.h"