2 * $RCSId: xc/lib/fontconfig/src/fcstr.c,v 1.10 2002/08/31 22:17:32 keithp Exp $
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.
32 FcStrCopy (const FcChar8 *s)
38 r = (FcChar8 *) malloc (strlen ((char *) s) + 1);
41 FcMemAlloc (FC_MEM_STRING, strlen ((char *) s) + 1);
42 strcpy ((char *) r, (char *) s);
47 FcStrPlus (const FcChar8 *s1, const FcChar8 *s2)
49 int l = strlen ((char *)s1) + strlen ((char *) s2) + 1;
50 FcChar8 *s = malloc (l);
54 FcMemAlloc (FC_MEM_STRING, l);
55 strcpy ((char *) s, (char *) s1);
56 strcat ((char *) s, (char *) s2);
61 FcStrFree (FcChar8 *s)
63 FcMemFree (FC_MEM_STRING, strlen ((char *) s) + 1);
68 #include "../fc-case/fccase.h"
70 #define FcCaseFoldUpperCount(cf) \
71 ((cf)->method == FC_CASE_FOLD_FULL ? 1 : (cf)->count)
73 #define FC_STR_CANON_BUF_LEN 1024
75 typedef struct _FcCaseWalker {
79 FcChar8 utf8[FC_MAX_CASE_FOLD_CHARS + 1];
83 FcStrCaseWalkerInit (const FcChar8 *src, FcCaseWalker *w)
87 w->len = strlen (src);
91 FcStrCaseWalkerLong (FcCaseWalker *w, FcChar8 r)
96 slen = FcUtf8ToUcs4 (w->src - 1, &ucs4, w->len + 1);
99 if (FC_MIN_FOLD_CHAR <= ucs4 && ucs4 <= FC_MAX_FOLD_CHAR)
102 int max = FC_NUM_CASE_FOLD;
106 int mid = (min + max) >> 1;
107 FcChar32 low = fcCaseFold[mid].upper;
108 FcChar32 high = low + FcCaseFoldUpperCount (&fcCaseFold[mid]);
116 const FcCaseFold *fold = &fcCaseFold[mid];
119 switch (fold->method) {
120 case FC_CASE_FOLD_EVEN_ODD:
121 if ((ucs4 & 1) != (fold->upper & 1))
123 /* fall through ... */
125 dlen = FcUcs4ToUtf8 (ucs4 + fold->offset, w->utf8);
127 case FC_CASE_FOLD_FULL:
129 memcpy (w->utf8, fcCaseFoldChars + fold->offset, dlen);
133 /* consume rest of src utf-8 bytes */
137 /* read from temp buffer */
138 w->utf8[dlen] = '\0';
148 FcStrCaseWalkerNext (FcCaseWalker *w)
154 if ((r = *w->read++))
161 if ((r & 0xc0) == 0xc0)
162 return FcStrCaseWalkerLong (w, r);
163 if ('A' <= r && r <= 'Z')
169 FcStrCaseWalkerNextIgnoreBlanks (FcCaseWalker *w)
175 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 * Is the head of s1 equal to s2?
331 FcStrIsAtIgnoreCase (const FcChar8 *s1, const FcChar8 *s2)
336 FcStrCaseWalkerInit (s1, &w1);
337 FcStrCaseWalkerInit (s2, &w2);
341 c1 = FcStrCaseWalkerNext (&w1);
342 c2 = FcStrCaseWalkerNext (&w2);
343 if (!c1 || (c1 != c2))
346 return c1 == c2 || !c2;
350 * Does s1 contain an instance of s2 (ignoring blanks and case)?
354 FcStrContainsIgnoreCase (const FcChar8 *s1, const FcChar8 *s2)
358 if (FcStrIsAtIgnoreCase (s1, s2))
366 FcStrStrIgnoreCase (const FcChar8 *s1, const FcChar8 *s2)
378 FcStrCaseWalkerInit (s1, &w1);
379 FcStrCaseWalkerInit (s2, &w2);
381 c2 = FcStrCaseWalkerNext (&w2);
386 c1 = FcStrCaseWalkerNext (&w1);
391 FcCaseWalker w1t = w1;
392 FcCaseWalker w2t = w2;
397 c1t = FcStrCaseWalkerNext (&w1t);
398 c2t = FcStrCaseWalkerNext (&w2t);
411 FcStrStr (const FcChar8 *s1, const FcChar8 *s2)
414 const FcChar8 * p = s1;
415 const FcChar8 * b = s2;
444 if (c1 && c2 && c1 != c2)
462 FcUtf8ToUcs4 (const FcChar8 *src_orig,
466 const FcChar8 *src = src_orig;
482 else if (!(s & 0x40))
486 else if (!(s & 0x20))
491 else if (!(s & 0x10))
496 else if (!(s & 0x08))
501 else if (!(s & 0x04))
506 else if ( ! (s & 0x02))
523 if ((s & 0xc0) != 0x80)
529 return src - src_orig;
533 FcUtf8Len (const FcChar8 *string,
547 clen = FcUtf8ToUcs4 (string, &c, len);
548 if (clen <= 0) /* malformed UTF8 string */
559 else if (max > 0x100)
567 FcUcs4ToUtf8 (FcChar32 ucs4,
568 FcChar8 dest[FC_UTF8_MAX_LEN])
573 if (ucs4 < 0x80) { *d++= ucs4; bits= -6; }
574 else if (ucs4 < 0x800) { *d++= ((ucs4 >> 6) & 0x1F) | 0xC0; bits= 0; }
575 else if (ucs4 < 0x10000) { *d++= ((ucs4 >> 12) & 0x0F) | 0xE0; bits= 6; }
576 else if (ucs4 < 0x200000) { *d++= ((ucs4 >> 18) & 0x07) | 0xF0; bits= 12; }
577 else if (ucs4 < 0x4000000) { *d++= ((ucs4 >> 24) & 0x03) | 0xF8; bits= 18; }
578 else if (ucs4 < 0x80000000) { *d++= ((ucs4 >> 30) & 0x01) | 0xFC; bits= 24; }
581 for ( ; bits >= 0; bits-= 6) {
582 *d++= ((ucs4 >> bits) & 0x3F) | 0x80;
587 #define GetUtf16(src,endian) \
588 ((FcChar16) ((src)[endian == FcEndianBig ? 0 : 1] << 8) | \
589 (FcChar16) ((src)[endian == FcEndianBig ? 1 : 0]))
592 FcUtf16ToUcs4 (const FcChar8 *src_orig,
595 int len) /* in bytes */
597 const FcChar8 *src = src_orig;
604 a = GetUtf16 (src, endian); src += 2; len -= 2;
607 * Check for surrogate
609 if ((a & 0xfc00) == 0xd800)
613 b = GetUtf16 (src, endian); src += 2; len -= 2;
615 * Check for invalid surrogate sequence
617 if ((b & 0xfc00) != 0xdc00)
619 result = ((((FcChar32) a & 0x3ff) << 10) |
620 ((FcChar32) b & 0x3ff)) + 0x10000;
625 return src - src_orig;
629 FcUtf16Len (const FcChar8 *string,
631 int len, /* in bytes */
644 clen = FcUtf16ToUcs4 (string, endian, &c, len);
645 if (clen <= 0) /* malformed UTF8 string */
656 else if (max > 0x100)
664 FcStrBufInit (FcStrBuf *buf, FcChar8 *init, int size)
667 buf->allocated = FcFalse;
668 buf->failed = FcFalse;
674 FcStrBufDestroy (FcStrBuf *buf)
678 FcMemFree (FC_MEM_STRBUF, buf->size);
680 FcStrBufInit (buf, 0, 0);
685 FcStrBufDone (FcStrBuf *buf)
689 ret = malloc (buf->len + 1);
692 FcMemAlloc (FC_MEM_STRING, buf->len + 1);
693 memcpy (ret, buf->buf, buf->len);
694 ret[buf->len] = '\0';
696 FcStrBufDestroy (buf);
701 FcStrBufChar (FcStrBuf *buf, FcChar8 c)
703 if (buf->len == buf->size)
710 size = buf->size * 2;
711 new = realloc (buf->buf, size);
715 size = buf->size + 1024;
719 buf->allocated = FcTrue;
720 memcpy (new, buf->buf, buf->len);
725 buf->failed = FcTrue;
729 FcMemFree (FC_MEM_STRBUF, buf->size);
730 FcMemAlloc (FC_MEM_STRBUF, size);
734 buf->buf[buf->len++] = c;
739 FcStrBufString (FcStrBuf *buf, const FcChar8 *s)
743 if (!FcStrBufChar (buf, c))
749 FcStrBufData (FcStrBuf *buf, const FcChar8 *s, int len)
752 if (!FcStrBufChar (buf, *s++))
758 FcStrUsesHome (const FcChar8 *s)
764 FcStrCopyFilename (const FcChar8 *s)
770 FcChar8 *home = FcConfigHome ();
774 size = strlen ((char *) home) + strlen ((char *) s);
775 new = (FcChar8 *) malloc (size);
778 FcMemAlloc (FC_MEM_STRING, size);
779 strcpy ((char *) new, (char *) home);
780 strcat ((char *) new, (char *) s + 1);
784 int size = strlen ((char *) s) + 1;
785 new = (FcChar8 *) malloc (size);
788 FcMemAlloc (FC_MEM_STRING, size);
789 strcpy ((char *) new, (const char *) s);
795 FcStrLastSlash (const FcChar8 *path)
799 slash = (FcChar8 *) strrchr ((const char *) path, '/');
804 backslash = (FcChar8 *) strrchr ((const char *) path, '\\');
805 if (!slash || (backslash && backslash > slash))
814 FcStrDirname (const FcChar8 *file)
819 slash = FcStrLastSlash (file);
821 return FcStrCopy ((FcChar8 *) ".");
822 dir = malloc ((slash - file) + 1);
825 FcMemAlloc (FC_MEM_STRING, (slash - file) + 1);
826 strncpy ((char *) dir, (const char *) file, slash - file);
827 dir[slash - file] = '\0';
832 FcStrBasename (const FcChar8 *file)
836 slash = FcStrLastSlash (file);
838 return FcStrCopy (file);
839 return FcStrCopy (slash + 1);
843 FcStrSetCreate (void)
845 FcStrSet *set = malloc (sizeof (FcStrSet));
848 FcMemAlloc (FC_MEM_STRSET, sizeof (FcStrSet));
852 set->storage = FcStorageDynamic;
857 static FcChar8 * strset_buf = 0;
858 static int strset_buf_ptr = 0, strset_buf_count = 0;
859 static int * strset_idx = 0;
860 static int strset_idx_ptr = 0, strset_idx_count = 0;
861 static FcStrSet * strsets = 0;
862 static int strset_ptr = 0, strset_count = 0;
864 void FcStrSetClearStatic()
866 strset_buf = 0; strset_buf_ptr = 0; strset_buf_count = 0;
867 strset_idx = 0; strset_idx_ptr = 0; strset_idx_count = 0;
868 strsets = 0; strset_ptr = 0; strset_count = 0;
872 FcStrSetGet (const FcStrSet *set, int i)
875 switch (set->storage)
877 case FcStorageStatic:
878 index = strset_idx[set->u.stridx_offset];
881 return &strset_buf[index];
882 case FcStorageDynamic:
883 return set->u.strs[i];
890 FcStrSetPtrU (const FcStrSetPtr set)
894 case FcStorageStatic:
895 return &strsets[set.u.stat];
896 case FcStorageDynamic:
897 return (FcStrSet *)set.u.dyn;
904 FcStrSetPtrCreateDynamic (const FcStrSet * set)
908 new.storage = FcStorageDynamic;
909 new.u.dyn = (FcStrSet *)set;
914 _FcStrSetAppend (FcStrSet *set, FcChar8 *s)
916 if (FcStrSetMember (set, s))
921 if (set->num == set->size || set->storage == FcStorageStatic)
923 FcChar8 **strs = malloc ((set->size + 2) * sizeof (FcChar8 *));
927 FcMemAlloc (FC_MEM_STRSET, (set->size + 2) * sizeof (FcChar8 *));
928 set->size = set->size + 1;
929 if (set->storage == FcStorageDynamic)
932 memcpy (strs, set->u.strs, set->num * sizeof (FcChar8 *));
939 memcpy (strs, strset_idx+set->u.stridx_offset,
940 set->num * sizeof (FcChar8 *));
941 set->storage = FcStorageDynamic;
945 set->u.strs[set->num++] = s;
946 set->u.strs[set->num] = 0;
951 FcStrSetMember (FcStrSet *set, const FcChar8 *s)
955 for (i = 0; i < set->num; i++)
956 if (!FcStrCmp (FcStrSetGet(set, i), s))
962 FcStrSetEqual (FcStrSet *sa, FcStrSet *sb)
965 if (sa->num != sb->num)
967 for (i = 0; i < sa->num; i++)
968 if (!FcStrSetMember (sb, FcStrSetGet(sa, i)))
974 FcStrSetAdd (FcStrSet *set, const FcChar8 *s)
976 FcChar8 *new = FcStrCopy (s);
979 if (!_FcStrSetAppend (set, new))
988 FcStrSetAddFilename (FcStrSet *set, const FcChar8 *s)
990 FcChar8 *new = FcStrCopyFilename (s);
993 if (!_FcStrSetAppend (set, new))
1002 FcStrSetDel (FcStrSet *set, const FcChar8 *s)
1006 for (i = 0; i < set->num; i++)
1007 if (!FcStrCmp (FcStrSetGet(set, i), s))
1009 if (set->storage == FcStorageDynamic)
1010 FcStrFree (set->u.strs[i]);
1012 * copy remaining string pointers and trailing
1015 memmove (FcStrSetGet(set, i), FcStrSetGet(set, i+1),
1016 (set->num - i) * sizeof (FcChar8 *));
1024 FcStrSetDestroy (FcStrSet *set)
1026 if (--set->ref == 0)
1030 if (set->storage == FcStorageDynamic)
1032 for (i = 0; i < set->num; i++)
1033 FcStrFree (set->u.strs[i]);
1034 FcMemFree (FC_MEM_STRSET, (set->size) * sizeof (FcChar8 *));
1037 FcMemFree (FC_MEM_STRSET, sizeof (FcStrSet));
1043 static int _FcStrSetSort_helper (const void * a, const void * b)
1045 return FcStrCmp (&strset_buf[(int)a],
1046 &strset_buf[(int)b]);
1050 FcStrSetSort (FcStrSet * set)
1052 switch (set->storage)
1054 case FcStorageDynamic:
1055 qsort (set->u.strs, set->num, sizeof (FcChar8 *),
1056 (int (*)(const void *, const void *)) FcStrCmp);
1058 case FcStorageStatic:
1059 qsort (strset_idx+set->u.stridx_offset, set->num, sizeof (int),
1060 _FcStrSetSort_helper);
1068 FcStrSetPrepareSerialize (const FcStrSet *set)
1076 strset_idx_count += set->num;
1077 for (i = 0; i < set->num; i++)
1079 if (FcStrSetGet(set, i))
1080 strset_buf_count += strlen(FcStrSetGet(set, i));
1087 FcStrSetSerialize (FcStrSet *set)
1095 strsets = malloc (strset_count * sizeof(FcStrSet));
1096 if (!strsets) goto bail1;
1097 strset_idx = malloc (strset_idx_count * sizeof(int));
1098 if (!strset_idx) goto bail2;
1099 strset_buf = malloc (strset_buf_count * sizeof (FcChar8));
1100 if (!strset_buf) goto bail3;
1104 return FcStrSetPtrCreateDynamic(0);
1106 newp.storage = FcStorageStatic;
1107 newp.u.stat = strset_ptr;
1109 new = &strsets[strset_ptr++];
1110 new->ref = set->ref;
1111 new->num = set->num;
1112 new->size = set->num;
1113 new->storage = FcStorageStatic;
1114 new->u.stridx_offset = strset_idx_ptr;
1115 for (i = 0; i < set->num; i++)
1117 FcChar8 * s = FcStrSetGet(set, i);
1121 memcpy(strset_buf+strset_buf_ptr, s,
1123 strset_idx[strset_idx_ptr++] = strset_buf_ptr;
1124 strset_buf_ptr += strlen((char *)s)+1;
1127 strset_idx[strset_idx_ptr++] = -1;
1130 if (strset_ptr > strset_count || strset_idx_ptr > strset_idx_count)
1131 return FcStrSetPtrCreateDynamic(0);
1133 // problem with multiple ptrs to the same StrSet.
1134 // should hash StrSets or something.
1135 // FcStrSetDestroy (set);
1144 return FcStrSetPtrCreateDynamic(0);
1148 FcStrSetRead (int fd, FcCache metadata)
1150 strsets = mmap(NULL,
1151 metadata.strsets_length * sizeof (FcStrSet),
1153 MAP_SHARED, fd, metadata.strsets_offset);
1154 if (strsets == MAP_FAILED)
1156 strset_count = strset_ptr = metadata.strsets_length;
1158 strset_idx = mmap(NULL,
1159 metadata.strsets_idx_length * sizeof (int),
1161 MAP_SHARED, fd, metadata.strsets_idx_offset);
1162 if (strset_idx == MAP_FAILED)
1164 strset_idx_count = strset_idx_ptr = metadata.strsets_length;
1166 strset_buf = mmap(NULL,
1167 metadata.strset_buf_length * sizeof (char),
1169 MAP_SHARED, fd, metadata.strset_buf_offset);
1170 if (strset_buf == MAP_FAILED)
1172 strset_buf_count = strset_buf_ptr = metadata.strset_buf_length;
1177 munmap (strset_idx, metadata.strsets_idx_length * sizeof (int));
1179 munmap (strsets, metadata.strsets_length * sizeof (FcStrSet));
1185 FcStrSetWrite (int fd, FcCache *metadata)
1187 metadata->strsets_length = strset_ptr;
1188 metadata->strsets_offset = FcCacheNextOffset(fd);
1191 lseek (fd, metadata->strsets_offset, SEEK_SET);
1192 if (write (fd, strsets, strset_ptr * sizeof(FcStrSet)) == -1)
1196 metadata->strsets_idx_length = strset_idx_ptr;
1197 metadata->strsets_idx_offset = FcCacheNextOffset(fd);
1198 if (strset_idx_ptr > 0)
1200 lseek (fd, metadata->strsets_idx_offset, SEEK_SET);
1201 if (write (fd, strset_idx, strset_idx_ptr * sizeof (int)) == -1)
1205 metadata->strset_buf_offset = FcCacheNextOffset(fd);
1206 metadata->strset_buf_length = strset_buf_ptr;
1207 if (strset_buf_ptr > 0)
1209 lseek (fd, metadata->strset_buf_offset, SEEK_SET);
1210 if (write (fd, strset_buf,
1211 metadata->strset_buf_length * sizeof (char)) == -1)
1219 FcStrListCreate (FcStrSet *set)
1223 list = malloc (sizeof (FcStrList));
1226 FcMemAlloc (FC_MEM_STRLIST, sizeof (FcStrList));
1234 FcStrListNext (FcStrList *list)
1236 if (list->n >= list->set->num)
1238 return FcStrSetGet(list->set, list->n++);
1242 FcStrListDone (FcStrList *list)
1244 FcStrSetDestroy (list->set);
1245 FcMemFree (FC_MEM_STRLIST, sizeof (FcStrList));