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 Keith Packard not be used in
11 * advertising or publicity pertaining to distribution of the software without
12 * specific, written prior permission. Keith Packard makes no
13 * representations about the suitability of this software for any purpose. It
14 * is provided "as is" without express or implied warranty.
16 * KEITH PACKARD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
17 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
18 * EVENT SHALL KEITH PACKARD 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 FcStrCopy (const FcChar8 *s)
41 len = strlen ((char *) s) + 1;
42 r = (FcChar8 *) malloc (len);
45 FcMemAlloc (FC_MEM_STRING, len);
51 FcStrPlus (const FcChar8 *s1, const FcChar8 *s2)
53 int l = strlen ((char *)s1) + strlen ((char *) s2) + 1;
54 FcChar8 *s = malloc (l);
58 FcMemAlloc (FC_MEM_STRING, l);
59 strcpy ((char *) s, (char *) s1);
60 strcat ((char *) s, (char *) s2);
65 FcStrFree (FcChar8 *s)
67 FcMemFree (FC_MEM_STRING, strlen ((char *) s) + 1);
72 #include "../fc-case/fccase.h"
74 #define FcCaseFoldUpperCount(cf) \
75 ((cf)->method == FC_CASE_FOLD_FULL ? 1 : (cf)->count)
77 #define FC_STR_CANON_BUF_LEN 1024
79 typedef struct _FcCaseWalker {
82 FcChar8 utf8[FC_MAX_CASE_FOLD_CHARS + 1];
86 FcStrCaseWalkerInit (const FcChar8 *src, FcCaseWalker *w)
93 FcStrCaseWalkerLong (FcCaseWalker *w, FcChar8 r)
97 int len = strlen((char*)w->src);
99 slen = FcUtf8ToUcs4 (w->src - 1, &ucs4, len + 1);
102 if (FC_MIN_FOLD_CHAR <= ucs4 && ucs4 <= FC_MAX_FOLD_CHAR)
105 int max = FC_NUM_CASE_FOLD;
109 int mid = (min + max) >> 1;
110 FcChar32 low = fcCaseFold[mid].upper;
111 FcChar32 high = low + FcCaseFoldUpperCount (&fcCaseFold[mid]);
119 const FcCaseFold *fold = &fcCaseFold[mid];
122 switch (fold->method) {
123 case FC_CASE_FOLD_EVEN_ODD:
124 if ((ucs4 & 1) != (fold->upper & 1))
126 /* fall through ... */
128 dlen = FcUcs4ToUtf8 (ucs4 + fold->offset, w->utf8);
130 case FC_CASE_FOLD_FULL:
132 memcpy (w->utf8, fcCaseFoldChars + fold->offset, dlen);
136 /* consume rest of src utf-8 bytes */
139 /* read from temp buffer */
140 w->utf8[dlen] = '\0';
150 FcStrCaseWalkerNext (FcCaseWalker *w)
156 if ((r = *w->read++))
162 if ((r & 0xc0) == 0xc0)
163 return FcStrCaseWalkerLong (w, r);
164 if ('A' <= r && r <= 'Z')
170 FcStrCaseWalkerNextIgnoreBlanks (FcCaseWalker *w)
176 if ((r = *w->read++))
185 if ((r & 0xc0) == 0xc0)
186 return FcStrCaseWalkerLong (w, r);
187 if ('A' <= r && r <= 'Z')
193 FcStrDowncase (const FcChar8 *s)
199 FcStrCaseWalkerInit (s, &w);
200 while (FcStrCaseWalkerNext (&w))
202 d = dst = malloc (len + 1);
205 FcMemAlloc (FC_MEM_STRING, 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;
271 * Return a hash value for a string
275 FcStrHashIgnoreCase (const FcChar8 *s)
281 FcStrCaseWalkerInit (s, &w);
282 while ((c = FcStrCaseWalkerNext (&w)))
283 h = ((h << 3) ^ (h >> 3)) ^ c;
288 * Is the head of s1 equal to s2?
292 FcStrIsAtIgnoreBlanksAndCase (const FcChar8 *s1, const FcChar8 *s2)
297 FcStrCaseWalkerInit (s1, &w1);
298 FcStrCaseWalkerInit (s2, &w2);
302 c1 = FcStrCaseWalkerNextIgnoreBlanks (&w1);
303 c2 = FcStrCaseWalkerNextIgnoreBlanks (&w2);
304 if (!c1 || (c1 != c2))
307 return c1 == c2 || !c2;
311 * Does s1 contain an instance of s2 (ignoring blanks and case)?
315 FcStrContainsIgnoreBlanksAndCase (const FcChar8 *s1, const FcChar8 *s2)
319 if (FcStrIsAtIgnoreBlanksAndCase (s1, s2))
327 FcCharIsPunct (const FcChar8 c)
347 * Is the head of s1 equal to s2?
351 FcStrIsAtIgnoreCase (const FcChar8 *s1, const FcChar8 *s2)
356 FcStrCaseWalkerInit (s1, &w1);
357 FcStrCaseWalkerInit (s2, &w2);
361 c1 = FcStrCaseWalkerNext (&w1);
362 c2 = FcStrCaseWalkerNext (&w2);
363 if (!c1 || (c1 != c2))
366 return c1 == c2 || !c2;
370 * Does s1 contain an instance of s2 (ignoring blanks and case)?
374 FcStrContainsIgnoreCase (const FcChar8 *s1, const FcChar8 *s2)
378 if (FcStrIsAtIgnoreCase (s1, s2))
386 * Does s1 contain an instance of s2 on a word boundary (ignoring case)?
390 FcStrContainsWord (const FcChar8 *s1, const FcChar8 *s2)
392 FcBool wordStart = FcTrue;
393 int s1len = strlen ((char *) s1);
394 int s2len = strlen ((char *) s2);
396 while (s1len >= s2len)
399 FcStrIsAtIgnoreCase (s1, s2) &&
400 (s1len == s2len || FcCharIsPunct (s1[s2len])))
405 if (FcCharIsPunct (*s1))
414 FcStrStrIgnoreCase (const FcChar8 *s1, const FcChar8 *s2)
426 FcStrCaseWalkerInit (s1, &w1);
427 FcStrCaseWalkerInit (s2, &w2);
429 c2 = FcStrCaseWalkerNext (&w2);
434 c1 = FcStrCaseWalkerNext (&w1);
439 FcCaseWalker w1t = w1;
440 FcCaseWalker w2t = w2;
445 c1t = FcStrCaseWalkerNext (&w1t);
446 c2t = FcStrCaseWalkerNext (&w2t);
459 FcStrStr (const FcChar8 *s1, const FcChar8 *s2)
462 const FcChar8 * p = s1;
463 const FcChar8 * b = s2;
492 if (c1 && c2 && c1 != c2)
509 FcUtf8ToUcs4 (const FcChar8 *src_orig,
513 const FcChar8 *src = src_orig;
529 else if (!(s & 0x40))
533 else if (!(s & 0x20))
538 else if (!(s & 0x10))
543 else if (!(s & 0x08))
548 else if (!(s & 0x04))
553 else if ( ! (s & 0x02))
570 if ((s & 0xc0) != 0x80)
576 return src - src_orig;
580 FcUtf8Len (const FcChar8 *string,
594 clen = FcUtf8ToUcs4 (string, &c, len);
595 if (clen <= 0) /* malformed UTF8 string */
606 else if (max > 0x100)
614 FcUcs4ToUtf8 (FcChar32 ucs4,
615 FcChar8 dest[FC_UTF8_MAX_LEN])
620 if (ucs4 < 0x80) { *d++= ucs4; bits= -6; }
621 else if (ucs4 < 0x800) { *d++= ((ucs4 >> 6) & 0x1F) | 0xC0; bits= 0; }
622 else if (ucs4 < 0x10000) { *d++= ((ucs4 >> 12) & 0x0F) | 0xE0; bits= 6; }
623 else if (ucs4 < 0x200000) { *d++= ((ucs4 >> 18) & 0x07) | 0xF0; bits= 12; }
624 else if (ucs4 < 0x4000000) { *d++= ((ucs4 >> 24) & 0x03) | 0xF8; bits= 18; }
625 else if (ucs4 < 0x80000000) { *d++= ((ucs4 >> 30) & 0x01) | 0xFC; bits= 24; }
628 for ( ; bits >= 0; bits-= 6) {
629 *d++= ((ucs4 >> bits) & 0x3F) | 0x80;
634 #define GetUtf16(src,endian) \
635 ((FcChar16) ((src)[endian == FcEndianBig ? 0 : 1] << 8) | \
636 (FcChar16) ((src)[endian == FcEndianBig ? 1 : 0]))
639 FcUtf16ToUcs4 (const FcChar8 *src_orig,
642 int len) /* in bytes */
644 const FcChar8 *src = src_orig;
651 a = GetUtf16 (src, endian); src += 2; len -= 2;
654 * Check for surrogate
656 if ((a & 0xfc00) == 0xd800)
660 b = GetUtf16 (src, endian); src += 2; len -= 2;
662 * Check for invalid surrogate sequence
664 if ((b & 0xfc00) != 0xdc00)
666 result = ((((FcChar32) a & 0x3ff) << 10) |
667 ((FcChar32) b & 0x3ff)) + 0x10000;
672 return src - src_orig;
676 FcUtf16Len (const FcChar8 *string,
678 int len, /* in bytes */
691 clen = FcUtf16ToUcs4 (string, endian, &c, len);
692 if (clen <= 0) /* malformed UTF8 string */
703 else if (max > 0x100)
711 FcStrBufInit (FcStrBuf *buf, FcChar8 *init, int size)
714 buf->allocated = FcFalse;
715 buf->failed = FcFalse;
721 FcStrBufDestroy (FcStrBuf *buf)
725 FcMemFree (FC_MEM_STRBUF, buf->size);
727 FcStrBufInit (buf, 0, 0);
732 FcStrBufDone (FcStrBuf *buf)
736 ret = malloc (buf->len + 1);
739 FcMemAlloc (FC_MEM_STRING, buf->len + 1);
740 memcpy (ret, buf->buf, buf->len);
741 ret[buf->len] = '\0';
743 FcStrBufDestroy (buf);
748 FcStrBufChar (FcStrBuf *buf, FcChar8 c)
750 if (buf->len == buf->size)
757 size = buf->size * 2;
758 new = realloc (buf->buf, size);
762 size = buf->size + 64;
766 buf->allocated = FcTrue;
767 memcpy (new, buf->buf, buf->len);
772 buf->failed = FcTrue;
776 FcMemFree (FC_MEM_STRBUF, buf->size);
777 FcMemAlloc (FC_MEM_STRBUF, size);
781 buf->buf[buf->len++] = c;
786 FcStrBufString (FcStrBuf *buf, const FcChar8 *s)
790 if (!FcStrBufChar (buf, c))
796 FcStrBufData (FcStrBuf *buf, const FcChar8 *s, int len)
799 if (!FcStrBufChar (buf, *s++))
805 FcStrUsesHome (const FcChar8 *s)
811 FcStrCopyFilename (const FcChar8 *s)
817 FcChar8 *home = FcConfigHome ();
822 size = strlen ((char *) home) + strlen ((char *) s);
823 full = (FcChar8 *) malloc (size);
826 strcpy ((char *) full, (char *) home);
827 strcat ((char *) full, (char *) s + 1);
828 new = FcStrCanonFilename (full);
832 new = FcStrCanonFilename (s);
837 FcStrLastSlash (const FcChar8 *path)
841 slash = (FcChar8 *) strrchr ((const char *) path, '/');
846 backslash = (FcChar8 *) strrchr ((const char *) path, '\\');
847 if (!slash || (backslash && backslash > slash))
856 FcStrDirname (const FcChar8 *file)
861 slash = FcStrLastSlash (file);
863 return FcStrCopy ((FcChar8 *) ".");
864 dir = malloc ((slash - file) + 1);
867 FcMemAlloc (FC_MEM_STRING, (slash - file) + 1);
868 strncpy ((char *) dir, (const char *) file, slash - file);
869 dir[slash - file] = '\0';
874 FcStrBasename (const FcChar8 *file)
878 slash = FcStrLastSlash (file);
880 return FcStrCopy (file);
881 return FcStrCopy (slash + 1);
885 FcStrCanonAbsoluteFilename (const FcChar8 *s)
889 const FcChar8 *slash;
892 size = strlen ((char *) s) + 1;
893 file = malloc (size);
896 FcMemAlloc (FC_MEM_STRING, size);
900 if (*s == '/' || *s == '\0')
906 f -= 1; /* squash // and trim final / from file */
909 if (!strncmp ((char *) slash, "/.", 2))
911 f -= 2; /* trim /. from file */
915 if (!strncmp ((char *) slash, "/..", 3))
917 f -= 3; /* trim /.. from file */
936 * Convert '\\' to '/' , remove double '/'
939 FcConvertDosPath (char *str)
941 size_t len = strlen (str);
944 char *end = str + len;
967 FcStrCanonFilename (const FcChar8 *s)
970 FcChar8 full[FC_MAX_FILE_LEN + 2];
971 FcChar8 basename[FC_MAX_FILE_LEN + 2];
972 int size = GetFullPathName (s, sizeof (full) -1,
977 perror ("GetFullPathName");
979 FcConvertDosPath (full);
980 return FcStrCanonAbsoluteFilename (full);
983 return FcStrCanonAbsoluteFilename (s);
989 FcChar8 cwd[FC_MAX_FILE_LEN + 2];
990 if (getcwd ((char *) cwd, FC_MAX_FILE_LEN) == NULL)
992 strcat ((char *) cwd, "/");
993 full = FcStrPlus (cwd, s);
994 file = FcStrCanonAbsoluteFilename (full);
1003 FcStrSetCreate (void)
1005 FcStrSet *set = malloc (sizeof (FcStrSet));
1008 FcMemAlloc (FC_MEM_STRSET, sizeof (FcStrSet));
1017 _FcStrSetAppend (FcStrSet *set, FcChar8 *s)
1019 if (FcStrSetMember (set, s))
1024 if (set->num == set->size)
1026 FcChar8 **strs = malloc ((set->size + 2) * sizeof (FcChar8 *));
1030 FcMemAlloc (FC_MEM_STRSET, (set->size + 2) * sizeof (FcChar8 *));
1032 memcpy (strs, set->strs, set->num * sizeof (FcChar8 *));
1035 FcMemFree (FC_MEM_STRSET, (set->size + 1) * sizeof (FcChar8 *));
1038 set->size = set->size + 1;
1041 set->strs[set->num++] = s;
1042 set->strs[set->num] = 0;
1047 FcStrSetMember (FcStrSet *set, const FcChar8 *s)
1051 for (i = 0; i < set->num; i++)
1052 if (!FcStrCmp (set->strs[i], s))
1058 FcStrSetEqual (FcStrSet *sa, FcStrSet *sb)
1061 if (sa->num != sb->num)
1063 for (i = 0; i < sa->num; i++)
1064 if (!FcStrSetMember (sb, sa->strs[i]))
1070 FcStrSetAdd (FcStrSet *set, const FcChar8 *s)
1072 FcChar8 *new = FcStrCopy (s);
1075 if (!_FcStrSetAppend (set, new))
1084 FcStrSetAddFilename (FcStrSet *set, const FcChar8 *s)
1086 FcChar8 *new = FcStrCopyFilename (s);
1089 if (!_FcStrSetAppend (set, new))
1098 FcStrSetDel (FcStrSet *set, const FcChar8 *s)
1102 for (i = 0; i < set->num; i++)
1103 if (!FcStrCmp (set->strs[i], s))
1105 FcStrFree (set->strs[i]);
1107 * copy remaining string pointers and trailing
1110 memmove (&set->strs[i], &set->strs[i+1],
1111 (set->num - i) * sizeof (FcChar8 *));
1119 FcStrSetDestroy (FcStrSet *set)
1121 if (--set->ref == 0)
1125 for (i = 0; i < set->num; i++)
1126 FcStrFree (set->strs[i]);
1129 FcMemFree (FC_MEM_STRSET, (set->size + 1) * sizeof (FcChar8 *));
1132 FcMemFree (FC_MEM_STRSET, sizeof (FcStrSet));
1138 FcStrListCreate (FcStrSet *set)
1142 list = malloc (sizeof (FcStrList));
1145 FcMemAlloc (FC_MEM_STRLIST, sizeof (FcStrList));
1153 FcStrListNext (FcStrList *list)
1155 if (list->n >= list->set->num)
1157 return list->set->strs[list->n++];
1161 FcStrListDone (FcStrList *list)
1163 FcStrSetDestroy (list->set);
1164 FcMemFree (FC_MEM_STRLIST, sizeof (FcStrList));
1169 #include "fcaliastail.h"