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.
31 /* Objects MT-safe for readonly access. */
34 FcStrCopy (const FcChar8 *s)
40 FcStrPlus (const FcChar8 *s1, const FcChar8 *s2)
42 int s1l = strlen ((char *) s1);
43 int s2l = strlen ((char *) s2);
44 int l = s1l + s2l + 1;
45 FcChar8 *s = malloc (l);
50 memcpy (s + s1l, s2, s2l + 1);
55 FcStrFree (FcChar8 *s)
61 #include "../fc-case/fccase.h"
63 #define FcCaseFoldUpperCount(cf) \
64 ((cf)->method == FC_CASE_FOLD_FULL ? 1 : (cf)->count)
66 typedef struct _FcCaseWalker {
69 FcChar8 utf8[FC_MAX_CASE_FOLD_CHARS + 1];
73 FcStrCaseWalkerInit (const FcChar8 *src, FcCaseWalker *w)
80 FcStrCaseWalkerLong (FcCaseWalker *w, FcChar8 r)
84 int len = strlen((char*)w->src);
86 slen = FcUtf8ToUcs4 (w->src - 1, &ucs4, len + 1);
89 if (FC_MIN_FOLD_CHAR <= ucs4 && ucs4 <= FC_MAX_FOLD_CHAR)
92 int max = FC_NUM_CASE_FOLD;
96 int mid = (min + max) >> 1;
97 FcChar32 low = fcCaseFold[mid].upper;
98 FcChar32 high = low + FcCaseFoldUpperCount (&fcCaseFold[mid]);
106 const FcCaseFold *fold = &fcCaseFold[mid];
109 switch (fold->method) {
110 case FC_CASE_FOLD_EVEN_ODD:
111 if ((ucs4 & 1) != (fold->upper & 1))
113 /* fall through ... */
115 dlen = FcUcs4ToUtf8 (ucs4 + fold->offset, w->utf8);
117 case FC_CASE_FOLD_FULL:
119 memcpy (w->utf8, fcCaseFoldChars + fold->offset, dlen);
123 /* consume rest of src utf-8 bytes */
126 /* read from temp buffer */
127 w->utf8[dlen] = '\0';
137 FcStrCaseWalkerNext (FcCaseWalker *w, const char *delims)
143 if ((r = *w->read++))
150 } while (r != 0 && delims && strchr (delims, r));
152 if ((r & 0xc0) == 0xc0)
153 return FcStrCaseWalkerLong (w, r);
154 if ('A' <= r && r <= 'Z')
160 FcStrDowncase (const FcChar8 *s)
166 FcStrCaseWalkerInit (s, &w);
167 while (FcStrCaseWalkerNext (&w, NULL))
169 d = dst = malloc (len + 1);
172 FcStrCaseWalkerInit (s, &w);
173 while ((*d++ = FcStrCaseWalkerNext (&w, NULL)));
178 FcStrCmpIgnoreCase (const FcChar8 *s1, const FcChar8 *s2)
183 if (s1 == s2) return 0;
185 FcStrCaseWalkerInit (s1, &w1);
186 FcStrCaseWalkerInit (s2, &w2);
190 c1 = FcStrCaseWalkerNext (&w1, NULL);
191 c2 = FcStrCaseWalkerNext (&w2, NULL);
192 if (!c1 || (c1 != c2))
195 return (int) c1 - (int) c2;
199 FcStrCmpIgnoreBlanksAndCase (const FcChar8 *s1, const FcChar8 *s2)
201 return FcStrCmpIgnoreCaseAndDelims (s1, s2, (const FcChar8 *)" ");
205 FcStrCmpIgnoreCaseAndDelims (const FcChar8 *s1, const FcChar8 *s2, const FcChar8 *delims)
210 if (s1 == s2) return 0;
212 FcStrCaseWalkerInit (s1, &w1);
213 FcStrCaseWalkerInit (s2, &w2);
217 c1 = FcStrCaseWalkerNext (&w1, (const char *)delims);
218 c2 = FcStrCaseWalkerNext (&w2, (const char *)delims);
219 if (!c1 || (c1 != c2))
222 return (int) c1 - (int) c2;
226 FcStrCmp (const FcChar8 *s1, const FcChar8 *s2)
239 return (int) c1 - (int) c2;
243 * Return a hash value for a string
247 FcStrHashIgnoreCase (const FcChar8 *s)
253 FcStrCaseWalkerInit (s, &w);
254 while ((c = FcStrCaseWalkerNext (&w, NULL)))
255 h = ((h << 3) ^ (h >> 3)) ^ c;
260 * Is the head of s1 equal to s2?
264 FcStrIsAtIgnoreBlanksAndCase (const FcChar8 *s1, const FcChar8 *s2)
269 FcStrCaseWalkerInit (s1, &w1);
270 FcStrCaseWalkerInit (s2, &w2);
274 c1 = FcStrCaseWalkerNext (&w1, " ");
275 c2 = FcStrCaseWalkerNext (&w2, " ");
276 if (!c1 || (c1 != c2))
279 return c1 == c2 || !c2;
283 * Does s1 contain an instance of s2 (ignoring blanks and case)?
287 FcStrContainsIgnoreBlanksAndCase (const FcChar8 *s1, const FcChar8 *s2)
291 if (FcStrIsAtIgnoreBlanksAndCase (s1, s2))
299 FcCharIsPunct (const FcChar8 c)
319 * Is the head of s1 equal to s2?
323 FcStrIsAtIgnoreCase (const FcChar8 *s1, const FcChar8 *s2)
328 FcStrCaseWalkerInit (s1, &w1);
329 FcStrCaseWalkerInit (s2, &w2);
333 c1 = FcStrCaseWalkerNext (&w1, NULL);
334 c2 = FcStrCaseWalkerNext (&w2, NULL);
335 if (!c1 || (c1 != c2))
338 return c1 == c2 || !c2;
342 * Does s1 contain an instance of s2 (ignoring blanks and case)?
346 FcStrContainsIgnoreCase (const FcChar8 *s1, const FcChar8 *s2)
350 if (FcStrIsAtIgnoreCase (s1, s2))
358 * Does s1 contain an instance of s2 on a word boundary (ignoring case)?
362 FcStrContainsWord (const FcChar8 *s1, const FcChar8 *s2)
364 FcBool wordStart = FcTrue;
365 int s1len = strlen ((char *) s1);
366 int s2len = strlen ((char *) s2);
368 while (s1len >= s2len)
371 FcStrIsAtIgnoreCase (s1, s2) &&
372 (s1len == s2len || FcCharIsPunct (s1[s2len])))
377 if (FcCharIsPunct (*s1))
386 * returns the number of strings (ignoring delimitors and case) being matched
390 FcStrMatchIgnoreCaseAndDelims (const FcChar8 *s1, const FcChar8 *s2, const FcChar8 *delims)
395 if (s1 == s2) return 0;
397 FcStrCaseWalkerInit (s1, &w1);
398 FcStrCaseWalkerInit (s2, &w2);
402 c1 = FcStrCaseWalkerNext (&w1, (const char *)delims);
403 c2 = FcStrCaseWalkerNext (&w2, (const char *)delims);
404 if (!c1 || (c1 != c2))
407 return w1.src - s1 - 1;
411 FcStrGlobMatch (const FcChar8 *glob,
412 const FcChar8 *string)
416 while ((c = *glob++))
420 /* short circuit common case */
423 /* short circuit another common case */
424 if (strchr ((char *) glob, '*') == 0)
428 l1 = strlen ((char *) string);
429 l2 = strlen ((char *) glob);
436 if (FcStrGlobMatch (glob, string))
442 if (*string++ == '\0')
451 return *string == '\0';
455 FcStrStrIgnoreCase (const FcChar8 *s1, const FcChar8 *s2)
467 FcStrCaseWalkerInit (s1, &w1);
468 FcStrCaseWalkerInit (s2, &w2);
470 c2 = FcStrCaseWalkerNext (&w2, NULL);
475 c1 = FcStrCaseWalkerNext (&w1, NULL);
480 FcCaseWalker w1t = w1;
481 FcCaseWalker w2t = w2;
486 c1t = FcStrCaseWalkerNext (&w1t, NULL);
487 c2t = FcStrCaseWalkerNext (&w2t, NULL);
500 FcStrStr (const FcChar8 *s1, const FcChar8 *s2)
503 const FcChar8 * p = s1;
504 const FcChar8 * b = s2;
533 if (c1 && c2 && c1 != c2)
550 FcUtf8ToUcs4 (const FcChar8 *src_orig,
554 const FcChar8 *src = src_orig;
570 else if (!(s & 0x40))
574 else if (!(s & 0x20))
579 else if (!(s & 0x10))
584 else if (!(s & 0x08))
589 else if (!(s & 0x04))
594 else if ( ! (s & 0x02))
611 if ((s & 0xc0) != 0x80)
617 return src - src_orig;
621 FcUtf8Len (const FcChar8 *string,
635 clen = FcUtf8ToUcs4 (string, &c, len);
636 if (clen <= 0) /* malformed UTF8 string */
647 else if (max > 0x100)
655 FcUcs4ToUtf8 (FcChar32 ucs4,
656 FcChar8 dest[FC_UTF8_MAX_LEN])
661 if (ucs4 < 0x80) { *d++= ucs4; bits= -6; }
662 else if (ucs4 < 0x800) { *d++= ((ucs4 >> 6) & 0x1F) | 0xC0; bits= 0; }
663 else if (ucs4 < 0x10000) { *d++= ((ucs4 >> 12) & 0x0F) | 0xE0; bits= 6; }
664 else if (ucs4 < 0x200000) { *d++= ((ucs4 >> 18) & 0x07) | 0xF0; bits= 12; }
665 else if (ucs4 < 0x4000000) { *d++= ((ucs4 >> 24) & 0x03) | 0xF8; bits= 18; }
666 else if (ucs4 < 0x80000000) { *d++= ((ucs4 >> 30) & 0x01) | 0xFC; bits= 24; }
669 for ( ; bits >= 0; bits-= 6) {
670 *d++= ((ucs4 >> bits) & 0x3F) | 0x80;
675 #define GetUtf16(src,endian) \
676 ((FcChar16) ((src)[endian == FcEndianBig ? 0 : 1] << 8) | \
677 (FcChar16) ((src)[endian == FcEndianBig ? 1 : 0]))
680 FcUtf16ToUcs4 (const FcChar8 *src_orig,
683 int len) /* in bytes */
685 const FcChar8 *src = src_orig;
692 a = GetUtf16 (src, endian); src += 2; len -= 2;
695 * Check for surrogate
697 if ((a & 0xfc00) == 0xd800)
701 b = GetUtf16 (src, endian); src += 2; len -= 2;
703 * Check for invalid surrogate sequence
705 if ((b & 0xfc00) != 0xdc00)
707 result = ((((FcChar32) a & 0x3ff) << 10) |
708 ((FcChar32) b & 0x3ff)) + 0x10000;
713 return src - src_orig;
717 FcUtf16Len (const FcChar8 *string,
719 int len, /* in bytes */
732 clen = FcUtf16ToUcs4 (string, endian, &c, len);
733 if (clen <= 0) /* malformed UTF8 string */
744 else if (max > 0x100)
752 FcStrBufInit (FcStrBuf *buf, FcChar8 *init, int size)
760 buf->buf = buf->buf_static;
761 buf->size = sizeof (buf->buf_static);
763 buf->allocated = FcFalse;
764 buf->failed = FcFalse;
769 FcStrBufDestroy (FcStrBuf *buf)
774 FcStrBufInit (buf, 0, 0);
779 FcStrBufDone (FcStrBuf *buf)
786 ret = malloc (buf->len + 1);
789 memcpy (ret, buf->buf, buf->len);
790 ret[buf->len] = '\0';
792 FcStrBufDestroy (buf);
797 FcStrBufDoneStatic (FcStrBuf *buf)
799 FcStrBufChar (buf, '\0');
808 FcStrBufChar (FcStrBuf *buf, FcChar8 c)
810 if (buf->len == buf->size)
820 size = buf->size * 2;
821 new = realloc (buf->buf, size);
825 size = buf->size + 64;
829 buf->allocated = FcTrue;
830 memcpy (new, buf->buf, buf->len);
835 buf->failed = FcTrue;
841 buf->buf[buf->len++] = c;
846 FcStrBufString (FcStrBuf *buf, const FcChar8 *s)
850 if (!FcStrBufChar (buf, c))
856 FcStrBufData (FcStrBuf *buf, const FcChar8 *s, int len)
859 if (!FcStrBufChar (buf, *s++))
865 FcStrUsesHome (const FcChar8 *s)
871 FcStrBuildFilename (const FcChar8 *path,
877 FcChar8 *s, *ret = NULL, *p;
883 sset = FcStrSetCreateEx (FCSS_ALLOW_DUPLICATES | FCSS_GROW_BY_64);
887 if (!FcStrSetAdd (sset, path))
893 s = (FcChar8 *)va_arg (ap, FcChar8 *);
896 if (!FcStrSetAdd (sset, s))
899 list = FcStrListCreate (sset);
900 while ((s = FcStrListNext (list)))
902 len += strlen ((const char *)s) + 1;
905 ret = malloc (sizeof (FcChar8) * (len + 1));
909 while ((s = FcStrListNext (list)))
913 p[0] = FC_DIR_SEPARATOR;
916 len = strlen ((const char *)s);
923 FcStrListDone (list);
927 FcStrSetDestroy (sset);
933 FcStrCopyFilename (const FcChar8 *s)
939 FcChar8 *home = FcConfigHome ();
944 size = strlen ((char *) home) + strlen ((char *) s);
945 full = (FcChar8 *) malloc (size);
948 strcpy ((char *) full, (char *) home);
949 strcat ((char *) full, (char *) s + 1);
950 new = FcStrCanonFilename (full);
954 new = FcStrCanonFilename (s);
960 FcStrLastSlash (const FcChar8 *path)
964 slash = (FcChar8 *) strrchr ((const char *) path, '/');
969 backslash = (FcChar8 *) strrchr ((const char *) path, '\\');
970 if (!slash || (backslash && backslash > slash))
979 FcStrDirname (const FcChar8 *file)
984 slash = FcStrLastSlash (file);
986 return FcStrCopy ((FcChar8 *) ".");
987 dir = malloc ((slash - file) + 1);
990 strncpy ((char *) dir, (const char *) file, slash - file);
991 dir[slash - file] = '\0';
996 FcStrBasename (const FcChar8 *file)
1000 slash = FcStrLastSlash (file);
1002 return FcStrCopy (file);
1003 return FcStrCopy (slash + 1);
1007 FcStrCanonAbsoluteFilename (const FcChar8 *s)
1011 const FcChar8 *slash;
1014 size = strlen ((char *) s) + 1;
1015 file = malloc (size);
1021 if (*s == '/' && *(s+1) == '/') /* Network path, do not squash // */
1025 if (*s == '/' || *s == '\0')
1029 switch (s - slash) {
1031 f -= 1; /* squash // and trim final / from file */
1034 if (!strncmp ((char *) slash, "/.", 2))
1036 f -= 2; /* trim /. from file */
1040 if (!strncmp ((char *) slash, "/..", 3))
1042 f -= 3; /* trim /.. from file */
1061 * Convert '\\' to '/' , remove double '/'
1064 FcConvertDosPath (char *str)
1066 size_t len = strlen (str);
1069 char *end = str + len;
1098 FcStrCanonFilename (const FcChar8 *s)
1101 FcChar8 full[FC_MAX_FILE_LEN + 2];
1102 int size = GetFullPathName ((LPCSTR) s, sizeof (full) -1,
1103 (LPSTR) full, NULL);
1106 perror ("GetFullPathName");
1108 FcConvertDosPath ((char *) full);
1109 return FcStrCanonAbsoluteFilename (full);
1112 return FcStrCanonAbsoluteFilename (s);
1118 FcChar8 cwd[FC_MAX_FILE_LEN + 2];
1119 if (getcwd ((char *) cwd, FC_MAX_FILE_LEN) == NULL)
1121 full = FcStrBuildFilename (cwd, s, NULL);
1122 file = FcStrCanonAbsoluteFilename (full);
1131 FcStrSetCreate (void)
1133 return FcStrSetCreateEx (FCSS_DEFAULT);
1137 FcStrSetCreateEx (unsigned int control)
1139 FcStrSet *set = malloc (sizeof (FcStrSet));
1142 FcRefInit (&set->ref, 1);
1146 set->control = control;
1151 _FcStrSetGrow (FcStrSet *set, int growElements)
1153 /* accommodate an additional NULL entry at the end of the array */
1154 FcChar8 **strs = malloc ((set->size + growElements + 1) * sizeof (FcChar8 *));
1158 memcpy (strs, set->strs, set->num * sizeof (FcChar8 *));
1161 set->size = set->size + growElements;
1167 _FcStrSetAppend (FcStrSet *set, FcChar8 *s)
1169 if (!FcStrSetHasControlBit (set, FCSS_ALLOW_DUPLICATES))
1171 if (FcStrSetMember (set, s))
1177 if (set->num == set->size)
1179 int growElements = FcStrSetHasControlBit (set, FCSS_GROW_BY_64) ? 64 : 1;
1180 if (!_FcStrSetGrow(set, growElements))
1183 set->strs[set->num++] = s;
1184 set->strs[set->num] = 0;
1189 FcStrSetMember (FcStrSet *set, const FcChar8 *s)
1193 for (i = 0; i < set->num; i++)
1194 if (!FcStrCmp (set->strs[i], s))
1200 FcStrSetEqual (FcStrSet *sa, FcStrSet *sb)
1203 if (sa->num != sb->num)
1205 for (i = 0; i < sa->num; i++)
1206 if (!FcStrSetMember (sb, sa->strs[i]))
1212 FcStrSetAdd (FcStrSet *set, const FcChar8 *s)
1214 FcChar8 *new = FcStrCopy (s);
1217 if (!_FcStrSetAppend (set, new))
1226 FcStrSetAddFilename (FcStrSet *set, const FcChar8 *s)
1228 FcChar8 *new = FcStrCopyFilename (s);
1231 if (!_FcStrSetAppend (set, new))
1240 FcStrSetAddLangs (FcStrSet *strs, const char *languages)
1242 const char *p = languages, *next;
1243 FcChar8 lang[128] = {0}, *normalized_lang;
1245 FcBool ret = FcFalse;
1250 while ((next = strchr (p, ':')))
1253 len = FC_MIN (len, 127);
1254 strncpy ((char *) lang, p, len);
1256 /* ignore an empty item */
1259 normalized_lang = FcLangNormalize ((const FcChar8 *) lang);
1260 if (normalized_lang)
1262 FcStrSetAdd (strs, normalized_lang);
1263 FcStrFree (normalized_lang);
1271 normalized_lang = FcLangNormalize ((const FcChar8 *) p);
1272 if (normalized_lang)
1274 FcStrSetAdd (strs, normalized_lang);
1275 FcStrFree (normalized_lang);
1284 FcStrSetDel (FcStrSet *set, const FcChar8 *s)
1288 for (i = 0; i < set->num; i++)
1289 if (!FcStrCmp (set->strs[i], s))
1291 FcStrFree (set->strs[i]);
1293 * copy remaining string pointers and trailing
1296 memmove (&set->strs[i], &set->strs[i+1],
1297 (set->num - i) * sizeof (FcChar8 *));
1304 /* TODO Make public */
1306 FcStrSetReference (FcStrSet *set)
1308 if (FcRefIsConst (&set->ref))
1311 FcRefInc (&set->ref);
1316 FcStrSetDestroy (FcStrSet *set)
1320 /* We rely on this in FcGetDefaultLangs for caching. */
1321 if (FcRefIsConst (&set->ref))
1324 if (FcRefDec (&set->ref) != 1)
1327 for (i = 0; i < set->num; i++)
1328 FcStrFree (set->strs[i]);
1335 FcStrListCreate (FcStrSet *set)
1339 list = malloc (sizeof (FcStrList));
1343 FcStrSetReference (set);
1349 FcStrListFirst (FcStrList *list)
1355 FcStrListNext (FcStrList *list)
1357 if (list->n >= list->set->num)
1359 return list->set->strs[list->n++];
1363 FcStrListDone (FcStrList *list)
1365 FcStrSetDestroy (list->set);
1370 #include "fcaliastail.h"