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.
34 /* Objects MT-safe for readonly access. */
37 FcStrCopy (const FcChar8 *s)
43 FcStrPlus (const FcChar8 *s1, const FcChar8 *s2)
45 int s1l = strlen ((char *) s1);
46 int s2l = strlen ((char *) s2);
47 int l = s1l + s2l + 1;
48 FcChar8 *s = malloc (l);
53 memcpy (s + s1l, s2, s2l + 1);
58 FcStrFree (FcChar8 *s)
64 #include "../fc-case/fccase.h"
66 #define FcCaseFoldUpperCount(cf) \
67 ((cf)->method == FC_CASE_FOLD_FULL ? 1 : (cf)->count)
69 typedef struct _FcCaseWalker {
72 FcChar8 utf8[FC_MAX_CASE_FOLD_CHARS + 1];
76 FcStrCaseWalkerInit (const FcChar8 *src, FcCaseWalker *w)
83 FcStrCaseWalkerLong (FcCaseWalker *w, FcChar8 r)
87 int len = strlen((char*)w->src);
89 slen = FcUtf8ToUcs4 (w->src - 1, &ucs4, len + 1);
92 if (FC_MIN_FOLD_CHAR <= ucs4 && ucs4 <= FC_MAX_FOLD_CHAR)
95 int max = FC_NUM_CASE_FOLD;
99 int mid = (min + max) >> 1;
100 FcChar32 low = fcCaseFold[mid].upper;
101 FcChar32 high = low + FcCaseFoldUpperCount (&fcCaseFold[mid]);
109 const FcCaseFold *fold = &fcCaseFold[mid];
112 switch (fold->method) {
113 case FC_CASE_FOLD_EVEN_ODD:
114 if ((ucs4 & 1) != (fold->upper & 1))
116 /* fall through ... */
118 dlen = FcUcs4ToUtf8 (ucs4 + fold->offset, w->utf8);
120 case FC_CASE_FOLD_FULL:
122 memcpy (w->utf8, fcCaseFoldChars + fold->offset, dlen);
126 /* consume rest of src utf-8 bytes */
129 /* read from temp buffer */
130 w->utf8[dlen] = '\0';
140 FcStrCaseWalkerNext (FcCaseWalker *w, const char *delims)
146 if ((r = *w->read++))
153 } while (r != 0 && delims && strchr (delims, r));
155 if ((r & 0xc0) == 0xc0)
156 return FcStrCaseWalkerLong (w, r);
157 if ('A' <= r && r <= 'Z')
163 FcStrDowncase (const FcChar8 *s)
169 FcStrCaseWalkerInit (s, &w);
170 while (FcStrCaseWalkerNext (&w, NULL))
172 d = dst = malloc (len + 1);
175 FcStrCaseWalkerInit (s, &w);
176 while ((*d++ = FcStrCaseWalkerNext (&w, NULL)));
181 FcStrCmpIgnoreCase (const FcChar8 *s1, const FcChar8 *s2)
186 if (s1 == s2) return 0;
188 FcStrCaseWalkerInit (s1, &w1);
189 FcStrCaseWalkerInit (s2, &w2);
193 c1 = FcStrCaseWalkerNext (&w1, NULL);
194 c2 = FcStrCaseWalkerNext (&w2, NULL);
195 if (!c1 || (c1 != c2))
198 return (int) c1 - (int) c2;
202 FcStrCmpIgnoreBlanksAndCase (const FcChar8 *s1, const FcChar8 *s2)
204 return FcStrCmpIgnoreCaseAndDelims (s1, s2, (const FcChar8 *)" ");
208 FcStrCmpIgnoreCaseAndDelims (const FcChar8 *s1, const FcChar8 *s2, const FcChar8 *delims)
213 if (s1 == s2) return 0;
215 FcStrCaseWalkerInit (s1, &w1);
216 FcStrCaseWalkerInit (s2, &w2);
220 c1 = FcStrCaseWalkerNext (&w1, (const char *)delims);
221 c2 = FcStrCaseWalkerNext (&w2, (const char *)delims);
222 if (!c1 || (c1 != c2))
225 return (int) c1 - (int) c2;
229 FcStrCmp (const FcChar8 *s1, const FcChar8 *s2)
242 return (int) c1 - (int) c2;
247 _FcStrRegexCmp (const FcChar8 *s, const FcChar8 *regex, int cflags, int eflags)
252 if ((ret = regcomp (®, (const char *)regex, cflags)) != 0)
254 if (FcDebug () & FC_DBG_MATCHV)
258 regerror (ret, ®, buf, 512);
259 printf("Regexp compile error: %s\n", buf);
263 ret = regexec (®, (const char *)s, 0, NULL, eflags);
266 if (FcDebug () & FC_DBG_MATCHV)
270 regerror (ret, ®, buf, 512);
271 printf("Regexp exec error: %s\n", buf);
276 return ret == 0 ? FcTrue : FcFalse;
279 # define _FcStrRegexCmp(_s_, _regex_, _cflags_, _eflags_) (FcFalse)
283 FcStrRegexCmp (const FcChar8 *s, const FcChar8 *regex)
285 return _FcStrRegexCmp (s, regex, REG_EXTENDED | REG_NOSUB, 0);
289 FcStrRegexCmpIgnoreCase (const FcChar8 *s, const FcChar8 *regex)
291 return _FcStrRegexCmp (s, regex, REG_EXTENDED | REG_NOSUB | REG_ICASE, 0);
295 * Return a hash value for a string
299 FcStrHashIgnoreCase (const FcChar8 *s)
305 FcStrCaseWalkerInit (s, &w);
306 while ((c = FcStrCaseWalkerNext (&w, NULL)))
307 h = ((h << 3) ^ (h >> 3)) ^ c;
312 * Is the head of s1 equal to s2?
316 FcStrIsAtIgnoreBlanksAndCase (const FcChar8 *s1, const FcChar8 *s2)
321 FcStrCaseWalkerInit (s1, &w1);
322 FcStrCaseWalkerInit (s2, &w2);
326 c1 = FcStrCaseWalkerNext (&w1, " ");
327 c2 = FcStrCaseWalkerNext (&w2, " ");
328 if (!c1 || (c1 != c2))
331 return c1 == c2 || !c2;
335 * Does s1 contain an instance of s2 (ignoring blanks and case)?
339 FcStrContainsIgnoreBlanksAndCase (const FcChar8 *s1, const FcChar8 *s2)
343 if (FcStrIsAtIgnoreBlanksAndCase (s1, s2))
351 FcCharIsPunct (const FcChar8 c)
371 * Is the head of s1 equal to s2?
375 FcStrIsAtIgnoreCase (const FcChar8 *s1, const FcChar8 *s2)
380 FcStrCaseWalkerInit (s1, &w1);
381 FcStrCaseWalkerInit (s2, &w2);
385 c1 = FcStrCaseWalkerNext (&w1, NULL);
386 c2 = FcStrCaseWalkerNext (&w2, NULL);
387 if (!c1 || (c1 != c2))
390 return c1 == c2 || !c2;
394 * Does s1 contain an instance of s2 (ignoring blanks and case)?
398 FcStrContainsIgnoreCase (const FcChar8 *s1, const FcChar8 *s2)
402 if (FcStrIsAtIgnoreCase (s1, s2))
410 * Does s1 contain an instance of s2 on a word boundary (ignoring case)?
414 FcStrContainsWord (const FcChar8 *s1, const FcChar8 *s2)
416 FcBool wordStart = FcTrue;
417 int s1len = strlen ((char *) s1);
418 int s2len = strlen ((char *) s2);
420 while (s1len >= s2len)
423 FcStrIsAtIgnoreCase (s1, s2) &&
424 (s1len == s2len || FcCharIsPunct (s1[s2len])))
429 if (FcCharIsPunct (*s1))
438 * returns the number of strings (ignoring delimitors and case) being matched
442 FcStrMatchIgnoreCaseAndDelims (const FcChar8 *s1, const FcChar8 *s2, const FcChar8 *delims)
447 if (s1 == s2) return 0;
449 FcStrCaseWalkerInit (s1, &w1);
450 FcStrCaseWalkerInit (s2, &w2);
454 c1 = FcStrCaseWalkerNext (&w1, (const char *)delims);
455 c2 = FcStrCaseWalkerNext (&w2, (const char *)delims);
456 if (!c1 || (c1 != c2))
459 return w1.src - s1 - 1;
463 FcStrGlobMatch (const FcChar8 *glob,
464 const FcChar8 *string)
468 while ((c = *glob++))
472 /* short circuit common case */
475 /* short circuit another common case */
476 if (strchr ((char *) glob, '*') == 0)
480 l1 = strlen ((char *) string);
481 l2 = strlen ((char *) glob);
488 if (FcStrGlobMatch (glob, string))
494 if (*string++ == '\0')
503 return *string == '\0';
507 FcStrStrIgnoreCase (const FcChar8 *s1, const FcChar8 *s2)
519 FcStrCaseWalkerInit (s1, &w1);
520 FcStrCaseWalkerInit (s2, &w2);
522 c2 = FcStrCaseWalkerNext (&w2, NULL);
527 c1 = FcStrCaseWalkerNext (&w1, NULL);
532 FcCaseWalker w1t = w1;
533 FcCaseWalker w2t = w2;
538 c1t = FcStrCaseWalkerNext (&w1t, NULL);
539 c2t = FcStrCaseWalkerNext (&w2t, NULL);
552 FcStrStr (const FcChar8 *s1, const FcChar8 *s2)
555 const FcChar8 * p = s1;
556 const FcChar8 * b = s2;
585 if (c1 && c2 && c1 != c2)
602 FcUtf8ToUcs4 (const FcChar8 *src_orig,
606 const FcChar8 *src = src_orig;
622 else if (!(s & 0x40))
626 else if (!(s & 0x20))
631 else if (!(s & 0x10))
636 else if (!(s & 0x08))
641 else if (!(s & 0x04))
646 else if ( ! (s & 0x02))
663 if ((s & 0xc0) != 0x80)
669 return src - src_orig;
673 FcUtf8Len (const FcChar8 *string,
687 clen = FcUtf8ToUcs4 (string, &c, len);
688 if (clen <= 0) /* malformed UTF8 string */
699 else if (max > 0x100)
707 FcUcs4ToUtf8 (FcChar32 ucs4,
708 FcChar8 dest[FC_UTF8_MAX_LEN])
713 if (ucs4 < 0x80) { *d++= ucs4; bits= -6; }
714 else if (ucs4 < 0x800) { *d++= ((ucs4 >> 6) & 0x1F) | 0xC0; bits= 0; }
715 else if (ucs4 < 0x10000) { *d++= ((ucs4 >> 12) & 0x0F) | 0xE0; bits= 6; }
716 else if (ucs4 < 0x200000) { *d++= ((ucs4 >> 18) & 0x07) | 0xF0; bits= 12; }
717 else if (ucs4 < 0x4000000) { *d++= ((ucs4 >> 24) & 0x03) | 0xF8; bits= 18; }
718 else if (ucs4 < 0x80000000) { *d++= ((ucs4 >> 30) & 0x01) | 0xFC; bits= 24; }
721 for ( ; bits >= 0; bits-= 6) {
722 *d++= ((ucs4 >> bits) & 0x3F) | 0x80;
727 #define GetUtf16(src,endian) \
728 ((FcChar16) ((src)[endian == FcEndianBig ? 0 : 1] << 8) | \
729 (FcChar16) ((src)[endian == FcEndianBig ? 1 : 0]))
732 FcUtf16ToUcs4 (const FcChar8 *src_orig,
735 int len) /* in bytes */
737 const FcChar8 *src = src_orig;
744 a = GetUtf16 (src, endian); src += 2; len -= 2;
747 * Check for surrogate
749 if ((a & 0xfc00) == 0xd800)
753 b = GetUtf16 (src, endian); src += 2; len -= 2;
755 * Check for invalid surrogate sequence
757 if ((b & 0xfc00) != 0xdc00)
759 result = ((((FcChar32) a & 0x3ff) << 10) |
760 ((FcChar32) b & 0x3ff)) + 0x10000;
765 return src - src_orig;
769 FcUtf16Len (const FcChar8 *string,
771 int len, /* in bytes */
784 clen = FcUtf16ToUcs4 (string, endian, &c, len);
785 if (clen <= 0) /* malformed UTF8 string */
796 else if (max > 0x100)
804 FcStrBufInit (FcStrBuf *buf, FcChar8 *init, int size)
812 buf->buf = buf->buf_static;
813 buf->size = sizeof (buf->buf_static);
815 buf->allocated = FcFalse;
816 buf->failed = FcFalse;
821 FcStrBufDestroy (FcStrBuf *buf)
826 FcStrBufInit (buf, 0, 0);
831 FcStrBufDone (FcStrBuf *buf)
838 ret = malloc (buf->len + 1);
841 memcpy (ret, buf->buf, buf->len);
842 ret[buf->len] = '\0';
844 FcStrBufDestroy (buf);
849 FcStrBufDoneStatic (FcStrBuf *buf)
851 FcStrBufChar (buf, '\0');
860 FcStrBufChar (FcStrBuf *buf, FcChar8 c)
862 if (buf->len == buf->size)
872 size = buf->size * 2;
873 new = realloc (buf->buf, size);
877 size = buf->size + 64;
881 buf->allocated = FcTrue;
882 memcpy (new, buf->buf, buf->len);
887 buf->failed = FcTrue;
893 buf->buf[buf->len++] = c;
898 FcStrBufString (FcStrBuf *buf, const FcChar8 *s)
902 if (!FcStrBufChar (buf, c))
908 FcStrBufData (FcStrBuf *buf, const FcChar8 *s, int len)
911 if (!FcStrBufChar (buf, *s++))
917 FcStrUsesHome (const FcChar8 *s)
923 FcStrBuildFilename (const FcChar8 *path,
927 FcStrSet *sset = FcStrSetCreate ();
929 FcChar8 *s, *ret = NULL, *p;
935 if (!FcStrSetAdd (sset, path))
941 s = (FcChar8 *)va_arg (ap, FcChar8 *);
944 if (!FcStrSetAdd (sset, s))
947 list = FcStrListCreate (sset);
948 while ((s = FcStrListNext (list)))
950 len += strlen ((const char *)s) + 1;
953 ret = malloc (sizeof (FcChar8) * (len + 1));
957 while ((s = FcStrListNext (list)))
961 p[0] = FC_DIR_SEPARATOR;
964 len = strlen ((const char *)s);
971 FcStrListDone (list);
975 FcStrSetDestroy (sset);
981 FcStrCopyFilename (const FcChar8 *s)
987 FcChar8 *home = FcConfigHome ();
992 size = strlen ((char *) home) + strlen ((char *) s);
993 full = (FcChar8 *) malloc (size);
996 strcpy ((char *) full, (char *) home);
997 strcat ((char *) full, (char *) s + 1);
998 new = FcStrCanonFilename (full);
1002 new = FcStrCanonFilename (s);
1008 FcStrLastSlash (const FcChar8 *path)
1012 slash = (FcChar8 *) strrchr ((const char *) path, '/');
1017 backslash = (FcChar8 *) strrchr ((const char *) path, '\\');
1018 if (!slash || (backslash && backslash > slash))
1027 FcStrDirname (const FcChar8 *file)
1032 slash = FcStrLastSlash (file);
1034 return FcStrCopy ((FcChar8 *) ".");
1035 dir = malloc ((slash - file) + 1);
1038 strncpy ((char *) dir, (const char *) file, slash - file);
1039 dir[slash - file] = '\0';
1044 FcStrBasename (const FcChar8 *file)
1048 slash = FcStrLastSlash (file);
1050 return FcStrCopy (file);
1051 return FcStrCopy (slash + 1);
1055 FcStrCanonAbsoluteFilename (const FcChar8 *s)
1059 const FcChar8 *slash;
1062 size = strlen ((char *) s) + 1;
1063 file = malloc (size);
1069 if (*s == '/' && *(s+1) == '/') /* Network path, do not squash // */
1073 if (*s == '/' || *s == '\0')
1077 switch (s - slash) {
1079 f -= 1; /* squash // and trim final / from file */
1082 if (!strncmp ((char *) slash, "/.", 2))
1084 f -= 2; /* trim /. from file */
1088 if (!strncmp ((char *) slash, "/..", 3))
1090 f -= 3; /* trim /.. from file */
1109 * Convert '\\' to '/' , remove double '/'
1112 FcConvertDosPath (char *str)
1114 size_t len = strlen (str);
1117 char *end = str + len;
1146 FcStrCanonFilename (const FcChar8 *s)
1149 FcChar8 full[FC_MAX_FILE_LEN + 2];
1150 int size = GetFullPathName ((LPCSTR) s, sizeof (full) -1,
1151 (LPSTR) full, NULL);
1154 perror ("GetFullPathName");
1156 FcConvertDosPath ((char *) full);
1157 return FcStrCanonAbsoluteFilename (full);
1160 return FcStrCanonAbsoluteFilename (s);
1166 FcChar8 cwd[FC_MAX_FILE_LEN + 2];
1167 if (getcwd ((char *) cwd, FC_MAX_FILE_LEN) == NULL)
1169 full = FcStrBuildFilename (cwd, s, NULL);
1170 file = FcStrCanonAbsoluteFilename (full);
1179 FcStrSetCreate (void)
1181 FcStrSet *set = malloc (sizeof (FcStrSet));
1184 FcRefInit (&set->ref, 1);
1192 _FcStrSetAppend (FcStrSet *set, FcChar8 *s)
1194 if (FcStrSetMember (set, s))
1199 if (set->num == set->size)
1201 FcChar8 **strs = malloc ((set->size + 2) * sizeof (FcChar8 *));
1206 memcpy (strs, set->strs, set->num * sizeof (FcChar8 *));
1209 set->size = set->size + 1;
1212 set->strs[set->num++] = s;
1213 set->strs[set->num] = 0;
1218 FcStrSetMember (FcStrSet *set, const FcChar8 *s)
1222 for (i = 0; i < set->num; i++)
1223 if (!FcStrCmp (set->strs[i], s))
1229 FcStrSetEqual (FcStrSet *sa, FcStrSet *sb)
1232 if (sa->num != sb->num)
1234 for (i = 0; i < sa->num; i++)
1235 if (!FcStrSetMember (sb, sa->strs[i]))
1241 FcStrSetAdd (FcStrSet *set, const FcChar8 *s)
1243 FcChar8 *new = FcStrCopy (s);
1246 if (!_FcStrSetAppend (set, new))
1255 FcStrSetAddFilename (FcStrSet *set, const FcChar8 *s)
1257 FcChar8 *new = FcStrCopyFilename (s);
1260 if (!_FcStrSetAppend (set, new))
1269 FcStrSetAddLangs (FcStrSet *strs, const char *languages)
1271 const char *p = languages, *next;
1272 FcChar8 lang[128] = {0}, *normalized_lang;
1274 FcBool ret = FcFalse;
1279 while ((next = strchr (p, ':')))
1282 len = FC_MIN (len, 127);
1283 strncpy ((char *) lang, p, len);
1285 /* ignore an empty item */
1288 normalized_lang = FcLangNormalize ((const FcChar8 *) lang);
1289 if (normalized_lang)
1291 FcStrSetAdd (strs, normalized_lang);
1292 FcStrFree (normalized_lang);
1300 normalized_lang = FcLangNormalize ((const FcChar8 *) p);
1301 if (normalized_lang)
1303 FcStrSetAdd (strs, normalized_lang);
1304 FcStrFree (normalized_lang);
1313 FcStrSetDel (FcStrSet *set, const FcChar8 *s)
1317 for (i = 0; i < set->num; i++)
1318 if (!FcStrCmp (set->strs[i], s))
1320 FcStrFree (set->strs[i]);
1322 * copy remaining string pointers and trailing
1325 memmove (&set->strs[i], &set->strs[i+1],
1326 (set->num - i) * sizeof (FcChar8 *));
1333 /* TODO Make public */
1335 FcStrSetReference (FcStrSet *set)
1337 if (FcRefIsConst (&set->ref))
1340 FcRefInc (&set->ref);
1345 FcStrSetDestroy (FcStrSet *set)
1349 /* We rely on this in FcGetDefaultLangs for caching. */
1350 if (FcRefIsConst (&set->ref))
1353 if (FcRefDec (&set->ref) != 1)
1356 for (i = 0; i < set->num; i++)
1357 FcStrFree (set->strs[i]);
1364 FcStrListCreate (FcStrSet *set)
1368 list = malloc (sizeof (FcStrList));
1372 FcStrSetReference (set);
1378 FcStrListFirst (FcStrList *list)
1384 FcStrListNext (FcStrList *list)
1386 if (list->n >= list->set->num)
1388 return list->set->strs[list->n++];
1392 FcStrListDone (FcStrList *list)
1394 FcStrSetDestroy (list->set);
1399 #include "fcaliastail.h"