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 FcStrPlus (const FcChar8 *s1, const FcChar8 *s2)
47 int s1l = strlen ((char *) s1);
48 int s2l = strlen ((char *) s2);
49 int l = s1l + s2l + 1;
50 FcChar8 *s = malloc (l);
55 memcpy (s + s1l, s2, s2l + 1);
60 FcStrFree (FcChar8 *s)
66 #include "../fc-case/fccase.h"
68 #define FcCaseFoldUpperCount(cf) \
69 ((cf)->method == FC_CASE_FOLD_FULL ? 1 : (cf)->count)
71 typedef struct _FcCaseWalker {
74 FcChar8 utf8[FC_MAX_CASE_FOLD_CHARS + 1];
78 FcStrCaseWalkerInit (const FcChar8 *src, FcCaseWalker *w)
85 FcStrCaseWalkerLong (FcCaseWalker *w, FcChar8 r)
89 int len = strlen((char*)w->src);
91 slen = FcUtf8ToUcs4 (w->src - 1, &ucs4, len + 1);
94 if (FC_MIN_FOLD_CHAR <= ucs4 && ucs4 <= FC_MAX_FOLD_CHAR)
97 int max = FC_NUM_CASE_FOLD;
101 int mid = (min + max) >> 1;
102 FcChar32 low = fcCaseFold[mid].upper;
103 FcChar32 high = low + FcCaseFoldUpperCount (&fcCaseFold[mid]);
111 const FcCaseFold *fold = &fcCaseFold[mid];
114 switch (fold->method) {
115 case FC_CASE_FOLD_EVEN_ODD:
116 if ((ucs4 & 1) != (fold->upper & 1))
118 /* fall through ... */
120 dlen = FcUcs4ToUtf8 (ucs4 + fold->offset, w->utf8);
122 case FC_CASE_FOLD_FULL:
124 memcpy (w->utf8, fcCaseFoldChars + fold->offset, dlen);
128 /* consume rest of src utf-8 bytes */
131 /* read from temp buffer */
132 w->utf8[dlen] = '\0';
142 FcStrCaseWalkerNext (FcCaseWalker *w)
148 if ((r = *w->read++))
154 if ((r & 0xc0) == 0xc0)
155 return FcStrCaseWalkerLong (w, r);
156 if ('A' <= r && r <= 'Z')
162 FcStrCaseWalkerNextIgnoreBlanks (FcCaseWalker *w)
168 if ((r = *w->read++))
177 if ((r & 0xc0) == 0xc0)
178 return FcStrCaseWalkerLong (w, r);
179 if ('A' <= r && r <= 'Z')
185 FcStrDowncase (const FcChar8 *s)
191 FcStrCaseWalkerInit (s, &w);
192 while (FcStrCaseWalkerNext (&w))
194 d = dst = malloc (len + 1);
197 FcStrCaseWalkerInit (s, &w);
198 while ((*d++ = FcStrCaseWalkerNext (&w)));
203 FcStrCmpIgnoreCase (const FcChar8 *s1, const FcChar8 *s2)
208 if (s1 == s2) return 0;
210 FcStrCaseWalkerInit (s1, &w1);
211 FcStrCaseWalkerInit (s2, &w2);
215 c1 = FcStrCaseWalkerNext (&w1);
216 c2 = FcStrCaseWalkerNext (&w2);
217 if (!c1 || (c1 != c2))
220 return (int) c1 - (int) c2;
224 FcStrCmpIgnoreBlanksAndCase (const FcChar8 *s1, const FcChar8 *s2)
229 if (s1 == s2) return 0;
231 FcStrCaseWalkerInit (s1, &w1);
232 FcStrCaseWalkerInit (s2, &w2);
236 c1 = FcStrCaseWalkerNextIgnoreBlanks (&w1);
237 c2 = FcStrCaseWalkerNextIgnoreBlanks (&w2);
238 if (!c1 || (c1 != c2))
241 return (int) c1 - (int) c2;
245 FcStrCmp (const FcChar8 *s1, const FcChar8 *s2)
258 return (int) c1 - (int) c2;
263 _FcStrRegexCmp (const FcChar8 *s, const FcChar8 *regex, int cflags, int eflags)
268 if ((ret = regcomp (®, (const char *)regex, cflags)) != 0)
270 if (FcDebug () & FC_DBG_MATCHV)
274 regerror (ret, ®, buf, 512);
275 printf("Regexp compile error: %s\n", buf);
279 ret = regexec (®, (const char *)s, 0, NULL, eflags);
282 if (FcDebug () & FC_DBG_MATCHV)
286 regerror (ret, ®, buf, 512);
287 printf("Regexp exec error: %s\n", buf);
292 return ret == 0 ? FcTrue : FcFalse;
295 # define _FcStrRegexCmp(_s_, _regex_, _cflags_, _eflags_) (FcFalse)
299 FcStrRegexCmp (const FcChar8 *s, const FcChar8 *regex)
301 return _FcStrRegexCmp (s, regex, REG_EXTENDED | REG_NOSUB, 0);
305 FcStrRegexCmpIgnoreCase (const FcChar8 *s, const FcChar8 *regex)
307 return _FcStrRegexCmp (s, regex, REG_EXTENDED | REG_NOSUB | REG_ICASE, 0);
311 * Return a hash value for a string
315 FcStrHashIgnoreCase (const FcChar8 *s)
321 FcStrCaseWalkerInit (s, &w);
322 while ((c = FcStrCaseWalkerNext (&w)))
323 h = ((h << 3) ^ (h >> 3)) ^ c;
328 * Is the head of s1 equal to s2?
332 FcStrIsAtIgnoreBlanksAndCase (const FcChar8 *s1, const FcChar8 *s2)
337 FcStrCaseWalkerInit (s1, &w1);
338 FcStrCaseWalkerInit (s2, &w2);
342 c1 = FcStrCaseWalkerNextIgnoreBlanks (&w1);
343 c2 = FcStrCaseWalkerNextIgnoreBlanks (&w2);
344 if (!c1 || (c1 != c2))
347 return c1 == c2 || !c2;
351 * Does s1 contain an instance of s2 (ignoring blanks and case)?
355 FcStrContainsIgnoreBlanksAndCase (const FcChar8 *s1, const FcChar8 *s2)
359 if (FcStrIsAtIgnoreBlanksAndCase (s1, s2))
367 FcCharIsPunct (const FcChar8 c)
387 * Is the head of s1 equal to s2?
391 FcStrIsAtIgnoreCase (const FcChar8 *s1, const FcChar8 *s2)
396 FcStrCaseWalkerInit (s1, &w1);
397 FcStrCaseWalkerInit (s2, &w2);
401 c1 = FcStrCaseWalkerNext (&w1);
402 c2 = FcStrCaseWalkerNext (&w2);
403 if (!c1 || (c1 != c2))
406 return c1 == c2 || !c2;
410 * Does s1 contain an instance of s2 (ignoring blanks and case)?
414 FcStrContainsIgnoreCase (const FcChar8 *s1, const FcChar8 *s2)
418 if (FcStrIsAtIgnoreCase (s1, s2))
426 * Does s1 contain an instance of s2 on a word boundary (ignoring case)?
430 FcStrContainsWord (const FcChar8 *s1, const FcChar8 *s2)
432 FcBool wordStart = FcTrue;
433 int s1len = strlen ((char *) s1);
434 int s2len = strlen ((char *) s2);
436 while (s1len >= s2len)
439 FcStrIsAtIgnoreCase (s1, s2) &&
440 (s1len == s2len || FcCharIsPunct (s1[s2len])))
445 if (FcCharIsPunct (*s1))
454 FcStrStrIgnoreCase (const FcChar8 *s1, const FcChar8 *s2)
466 FcStrCaseWalkerInit (s1, &w1);
467 FcStrCaseWalkerInit (s2, &w2);
469 c2 = FcStrCaseWalkerNext (&w2);
474 c1 = FcStrCaseWalkerNext (&w1);
479 FcCaseWalker w1t = w1;
480 FcCaseWalker w2t = w2;
485 c1t = FcStrCaseWalkerNext (&w1t);
486 c2t = FcStrCaseWalkerNext (&w2t);
499 FcStrStr (const FcChar8 *s1, const FcChar8 *s2)
502 const FcChar8 * p = s1;
503 const FcChar8 * b = s2;
532 if (c1 && c2 && c1 != c2)
549 FcUtf8ToUcs4 (const FcChar8 *src_orig,
553 const FcChar8 *src = src_orig;
569 else if (!(s & 0x40))
573 else if (!(s & 0x20))
578 else if (!(s & 0x10))
583 else if (!(s & 0x08))
588 else if (!(s & 0x04))
593 else if ( ! (s & 0x02))
610 if ((s & 0xc0) != 0x80)
616 return src - src_orig;
620 FcUtf8Len (const FcChar8 *string,
634 clen = FcUtf8ToUcs4 (string, &c, len);
635 if (clen <= 0) /* malformed UTF8 string */
646 else if (max > 0x100)
654 FcUcs4ToUtf8 (FcChar32 ucs4,
655 FcChar8 dest[FC_UTF8_MAX_LEN])
660 if (ucs4 < 0x80) { *d++= ucs4; bits= -6; }
661 else if (ucs4 < 0x800) { *d++= ((ucs4 >> 6) & 0x1F) | 0xC0; bits= 0; }
662 else if (ucs4 < 0x10000) { *d++= ((ucs4 >> 12) & 0x0F) | 0xE0; bits= 6; }
663 else if (ucs4 < 0x200000) { *d++= ((ucs4 >> 18) & 0x07) | 0xF0; bits= 12; }
664 else if (ucs4 < 0x4000000) { *d++= ((ucs4 >> 24) & 0x03) | 0xF8; bits= 18; }
665 else if (ucs4 < 0x80000000) { *d++= ((ucs4 >> 30) & 0x01) | 0xFC; bits= 24; }
668 for ( ; bits >= 0; bits-= 6) {
669 *d++= ((ucs4 >> bits) & 0x3F) | 0x80;
674 #define GetUtf16(src,endian) \
675 ((FcChar16) ((src)[endian == FcEndianBig ? 0 : 1] << 8) | \
676 (FcChar16) ((src)[endian == FcEndianBig ? 1 : 0]))
679 FcUtf16ToUcs4 (const FcChar8 *src_orig,
682 int len) /* in bytes */
684 const FcChar8 *src = src_orig;
691 a = GetUtf16 (src, endian); src += 2; len -= 2;
694 * Check for surrogate
696 if ((a & 0xfc00) == 0xd800)
700 b = GetUtf16 (src, endian); src += 2; len -= 2;
702 * Check for invalid surrogate sequence
704 if ((b & 0xfc00) != 0xdc00)
706 result = ((((FcChar32) a & 0x3ff) << 10) |
707 ((FcChar32) b & 0x3ff)) + 0x10000;
712 return src - src_orig;
716 FcUtf16Len (const FcChar8 *string,
718 int len, /* in bytes */
731 clen = FcUtf16ToUcs4 (string, endian, &c, len);
732 if (clen <= 0) /* malformed UTF8 string */
743 else if (max > 0x100)
751 FcStrBufInit (FcStrBuf *buf, FcChar8 *init, int size)
759 buf->buf = buf->buf_static;
760 buf->size = sizeof (buf->buf_static);
762 buf->allocated = FcFalse;
763 buf->failed = FcFalse;
768 FcStrBufDestroy (FcStrBuf *buf)
773 FcStrBufInit (buf, 0, 0);
778 FcStrBufDone (FcStrBuf *buf)
785 ret = malloc (buf->len + 1);
788 memcpy (ret, buf->buf, buf->len);
789 ret[buf->len] = '\0';
791 FcStrBufDestroy (buf);
796 FcStrBufDoneStatic (FcStrBuf *buf)
798 FcStrBufChar (buf, '\0');
807 FcStrBufChar (FcStrBuf *buf, FcChar8 c)
809 if (buf->len == buf->size)
819 size = buf->size * 2;
820 new = realloc (buf->buf, size);
824 size = buf->size + 64;
828 buf->allocated = FcTrue;
829 memcpy (new, buf->buf, buf->len);
834 buf->failed = FcTrue;
840 buf->buf[buf->len++] = c;
845 FcStrBufString (FcStrBuf *buf, const FcChar8 *s)
849 if (!FcStrBufChar (buf, c))
855 FcStrBufData (FcStrBuf *buf, const FcChar8 *s, int len)
858 if (!FcStrBufChar (buf, *s++))
864 FcStrUsesHome (const FcChar8 *s)
870 FcStrCopyFilename (const FcChar8 *s)
876 FcChar8 *home = FcConfigHome ();
881 size = strlen ((char *) home) + strlen ((char *) s);
882 full = (FcChar8 *) malloc (size);
885 strcpy ((char *) full, (char *) home);
886 strcat ((char *) full, (char *) s + 1);
887 new = FcStrCanonFilename (full);
891 new = FcStrCanonFilename (s);
897 FcStrLastSlash (const FcChar8 *path)
901 slash = (FcChar8 *) strrchr ((const char *) path, '/');
906 backslash = (FcChar8 *) strrchr ((const char *) path, '\\');
907 if (!slash || (backslash && backslash > slash))
916 FcStrDirname (const FcChar8 *file)
921 slash = FcStrLastSlash (file);
923 return FcStrCopy ((FcChar8 *) ".");
924 dir = malloc ((slash - file) + 1);
927 strncpy ((char *) dir, (const char *) file, slash - file);
928 dir[slash - file] = '\0';
933 FcStrBasename (const FcChar8 *file)
937 slash = FcStrLastSlash (file);
939 return FcStrCopy (file);
940 return FcStrCopy (slash + 1);
944 FcStrCanonAbsoluteFilename (const FcChar8 *s)
948 const FcChar8 *slash;
951 size = strlen ((char *) s) + 1;
952 file = malloc (size);
958 if (*s == '/' && *(s+1) == '/') /* Network path, do not squash // */
962 if (*s == '/' || *s == '\0')
968 f -= 1; /* squash // and trim final / from file */
971 if (!strncmp ((char *) slash, "/.", 2))
973 f -= 2; /* trim /. from file */
977 if (!strncmp ((char *) slash, "/..", 3))
979 f -= 3; /* trim /.. from file */
998 * Convert '\\' to '/' , remove double '/'
1001 FcConvertDosPath (char *str)
1003 size_t len = strlen (str);
1006 char *end = str + len;
1035 FcStrCanonFilename (const FcChar8 *s)
1038 FcChar8 full[FC_MAX_FILE_LEN + 2];
1039 int size = GetFullPathName ((LPCSTR) s, sizeof (full) -1,
1040 (LPSTR) full, NULL);
1043 perror ("GetFullPathName");
1045 FcConvertDosPath ((char *) full);
1046 return FcStrCanonAbsoluteFilename (full);
1049 return FcStrCanonAbsoluteFilename (s);
1055 FcChar8 cwd[FC_MAX_FILE_LEN + 2];
1056 if (getcwd ((char *) cwd, FC_MAX_FILE_LEN) == NULL)
1058 strcat ((char *) cwd, "/");
1059 full = FcStrPlus (cwd, s);
1060 file = FcStrCanonAbsoluteFilename (full);
1069 FcStrSetCreate (void)
1071 FcStrSet *set = malloc (sizeof (FcStrSet));
1074 FcRefInit (&set->ref, 1);
1082 _FcStrSetAppend (FcStrSet *set, FcChar8 *s)
1084 if (FcStrSetMember (set, s))
1089 if (set->num == set->size)
1091 FcChar8 **strs = malloc ((set->size + 2) * sizeof (FcChar8 *));
1096 memcpy (strs, set->strs, set->num * sizeof (FcChar8 *));
1099 set->size = set->size + 1;
1102 set->strs[set->num++] = s;
1103 set->strs[set->num] = 0;
1108 FcStrSetMember (FcStrSet *set, const FcChar8 *s)
1112 for (i = 0; i < set->num; i++)
1113 if (!FcStrCmp (set->strs[i], s))
1119 FcStrSetEqual (FcStrSet *sa, FcStrSet *sb)
1122 if (sa->num != sb->num)
1124 for (i = 0; i < sa->num; i++)
1125 if (!FcStrSetMember (sb, sa->strs[i]))
1131 FcStrSetAdd (FcStrSet *set, const FcChar8 *s)
1133 FcChar8 *new = FcStrCopy (s);
1136 if (!_FcStrSetAppend (set, new))
1145 FcStrSetAddFilename (FcStrSet *set, const FcChar8 *s)
1147 FcChar8 *new = FcStrCopyFilename (s);
1150 if (!_FcStrSetAppend (set, new))
1159 FcStrSetAddLangs (FcStrSet *strs, const char *languages)
1161 const char *p = languages, *next;
1162 FcChar8 lang[128] = {0}, *normalized_lang;
1164 FcBool ret = FcFalse;
1169 while ((next = strchr (p, ':')))
1172 len = FC_MIN (len, 127);
1173 strncpy ((char *) lang, p, len);
1175 /* ignore an empty item */
1178 normalized_lang = FcLangNormalize ((const FcChar8 *) lang);
1179 if (normalized_lang)
1181 FcStrSetAdd (strs, normalized_lang);
1182 FcStrFree (normalized_lang);
1190 normalized_lang = FcLangNormalize ((const FcChar8 *) p);
1191 if (normalized_lang)
1193 FcStrSetAdd (strs, normalized_lang);
1194 FcStrFree (normalized_lang);
1203 FcStrSetDel (FcStrSet *set, const FcChar8 *s)
1207 for (i = 0; i < set->num; i++)
1208 if (!FcStrCmp (set->strs[i], s))
1210 FcStrFree (set->strs[i]);
1212 * copy remaining string pointers and trailing
1215 memmove (&set->strs[i], &set->strs[i+1],
1216 (set->num - i) * sizeof (FcChar8 *));
1224 FcStrSetDestroy (FcStrSet *set)
1228 /* We rely on this in FcGetDefaultLangs for caching. */
1229 if (FcRefIsConst (&set->ref))
1232 if (FcRefDec (&set->ref) != 1)
1235 for (i = 0; i < set->num; i++)
1236 FcStrFree (set->strs[i]);
1243 FcStrListCreate (FcStrSet *set)
1247 list = malloc (sizeof (FcStrList));
1251 FcRefInc (&set->ref);
1257 FcStrListNext (FcStrList *list)
1259 if (list->n >= list->set->num)
1261 return list->set->strs[list->n++];
1265 FcStrListDone (FcStrList *list)
1267 FcStrSetDestroy (list->set);
1272 #include "fcaliastail.h"