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.
31 FcStrCopy (const FcChar8 *s)
37 r = (FcChar8 *) malloc (strlen ((char *) s) + 1);
40 FcMemAlloc (FC_MEM_STRING, strlen ((char *) s) + 1);
41 strcpy ((char *) r, (char *) s);
46 FcStrPlus (const FcChar8 *s1, const FcChar8 *s2)
48 int l = strlen ((char *)s1) + strlen ((char *) s2) + 1;
49 FcChar8 *s = malloc (l);
53 FcMemAlloc (FC_MEM_STRING, l);
54 strcpy ((char *) s, (char *) s1);
55 strcat ((char *) s, (char *) s2);
60 FcStrFree (FcChar8 *s)
62 FcMemFree (FC_MEM_STRING, strlen ((char *) s) + 1);
67 #include "../fc-case/fccase.h"
69 #define FcCaseFoldUpperCount(cf) \
70 ((cf)->method == FC_CASE_FOLD_FULL ? 1 : (cf)->count)
72 #define FC_STR_CANON_BUF_LEN 1024
74 typedef struct _FcCaseWalker {
77 FcChar8 utf8[FC_MAX_CASE_FOLD_CHARS + 1];
81 FcStrCaseWalkerInit (const FcChar8 *src, FcCaseWalker *w)
88 FcStrCaseWalkerLong (FcCaseWalker *w, FcChar8 r)
92 int len = strlen((char*)w->src);
94 slen = FcUtf8ToUcs4 (w->src - 1, &ucs4, len + 1);
97 if (FC_MIN_FOLD_CHAR <= ucs4 && ucs4 <= FC_MAX_FOLD_CHAR)
100 int max = FC_NUM_CASE_FOLD;
104 int mid = (min + max) >> 1;
105 FcChar32 low = fcCaseFold[mid].upper;
106 FcChar32 high = low + FcCaseFoldUpperCount (&fcCaseFold[mid]);
114 const FcCaseFold *fold = &fcCaseFold[mid];
117 switch (fold->method) {
118 case FC_CASE_FOLD_EVEN_ODD:
119 if ((ucs4 & 1) != (fold->upper & 1))
121 /* fall through ... */
123 dlen = FcUcs4ToUtf8 (ucs4 + fold->offset, w->utf8);
125 case FC_CASE_FOLD_FULL:
127 memcpy (w->utf8, fcCaseFoldChars + fold->offset, dlen);
131 /* consume rest of src utf-8 bytes */
134 /* read from temp buffer */
135 w->utf8[dlen] = '\0';
145 FcStrCaseWalkerNext (FcCaseWalker *w)
151 if ((r = *w->read++))
157 if ((r & 0xc0) == 0xc0)
158 return FcStrCaseWalkerLong (w, r);
159 if ('A' <= r && r <= 'Z')
165 FcStrCaseWalkerNextIgnoreBlanks (FcCaseWalker *w)
171 if ((r = *w->read++))
180 if ((r & 0xc0) == 0xc0)
181 return FcStrCaseWalkerLong (w, r);
182 if ('A' <= r && r <= 'Z')
188 FcStrDowncase (const FcChar8 *s)
194 FcStrCaseWalkerInit (s, &w);
195 while (FcStrCaseWalkerNext (&w))
197 d = dst = malloc (len + 1);
200 FcMemAlloc (FC_MEM_STRING, len + 1);
201 FcStrCaseWalkerInit (s, &w);
202 while ((*d++ = FcStrCaseWalkerNext (&w)));
207 FcStrCmpIgnoreCase (const FcChar8 *s1, const FcChar8 *s2)
212 if (s1 == s2) return 0;
214 FcStrCaseWalkerInit (s1, &w1);
215 FcStrCaseWalkerInit (s2, &w2);
219 c1 = FcStrCaseWalkerNext (&w1);
220 c2 = FcStrCaseWalkerNext (&w2);
221 if (!c1 || (c1 != c2))
224 return (int) c1 - (int) c2;
228 FcStrCmpIgnoreBlanksAndCase (const FcChar8 *s1, const FcChar8 *s2)
233 if (s1 == s2) return 0;
235 FcStrCaseWalkerInit (s1, &w1);
236 FcStrCaseWalkerInit (s2, &w2);
240 c1 = FcStrCaseWalkerNextIgnoreBlanks (&w1);
241 c2 = FcStrCaseWalkerNextIgnoreBlanks (&w2);
242 if (!c1 || (c1 != c2))
245 return (int) c1 - (int) c2;
249 FcStrCmp (const FcChar8 *s1, const FcChar8 *s2)
262 return (int) c1 - (int) c2;
266 * Return a hash value for a string
270 FcStrHashIgnoreCase (const FcChar8 *s)
276 FcStrCaseWalkerInit (s, &w);
277 while ((c = FcStrCaseWalkerNext (&w)))
278 h = ((h << 3) ^ (h >> 3)) ^ c;
283 * Is the head of s1 equal to s2?
287 FcStrIsAtIgnoreBlanksAndCase (const FcChar8 *s1, const FcChar8 *s2)
292 FcStrCaseWalkerInit (s1, &w1);
293 FcStrCaseWalkerInit (s2, &w2);
297 c1 = FcStrCaseWalkerNextIgnoreBlanks (&w1);
298 c2 = FcStrCaseWalkerNextIgnoreBlanks (&w2);
299 if (!c1 || (c1 != c2))
302 return c1 == c2 || !c2;
306 * Does s1 contain an instance of s2 (ignoring blanks and case)?
310 FcStrContainsIgnoreBlanksAndCase (const FcChar8 *s1, const FcChar8 *s2)
314 if (FcStrIsAtIgnoreBlanksAndCase (s1, s2))
322 * Is the head of s1 equal to s2?
326 FcStrIsAtIgnoreCase (const FcChar8 *s1, const FcChar8 *s2)
331 FcStrCaseWalkerInit (s1, &w1);
332 FcStrCaseWalkerInit (s2, &w2);
336 c1 = FcStrCaseWalkerNext (&w1);
337 c2 = FcStrCaseWalkerNext (&w2);
338 if (!c1 || (c1 != c2))
341 return c1 == c2 || !c2;
345 * Does s1 contain an instance of s2 (ignoring blanks and case)?
349 FcStrContainsIgnoreCase (const FcChar8 *s1, const FcChar8 *s2)
353 if (FcStrIsAtIgnoreCase (s1, s2))
361 FcStrStrIgnoreCase (const FcChar8 *s1, const FcChar8 *s2)
373 FcStrCaseWalkerInit (s1, &w1);
374 FcStrCaseWalkerInit (s2, &w2);
376 c2 = FcStrCaseWalkerNext (&w2);
381 c1 = FcStrCaseWalkerNext (&w1);
386 FcCaseWalker w1t = w1;
387 FcCaseWalker w2t = w2;
392 c1t = FcStrCaseWalkerNext (&w1t);
393 c2t = FcStrCaseWalkerNext (&w2t);
406 FcStrStr (const FcChar8 *s1, const FcChar8 *s2)
409 const FcChar8 * p = s1;
410 const FcChar8 * b = s2;
439 if (c1 && c2 && c1 != c2)
457 FcUtf8ToUcs4 (const FcChar8 *src_orig,
461 const FcChar8 *src = src_orig;
477 else if (!(s & 0x40))
481 else if (!(s & 0x20))
486 else if (!(s & 0x10))
491 else if (!(s & 0x08))
496 else if (!(s & 0x04))
501 else if ( ! (s & 0x02))
518 if ((s & 0xc0) != 0x80)
524 return src - src_orig;
528 FcUtf8Len (const FcChar8 *string,
542 clen = FcUtf8ToUcs4 (string, &c, len);
543 if (clen <= 0) /* malformed UTF8 string */
554 else if (max > 0x100)
562 FcUcs4ToUtf8 (FcChar32 ucs4,
563 FcChar8 dest[FC_UTF8_MAX_LEN])
568 if (ucs4 < 0x80) { *d++= ucs4; bits= -6; }
569 else if (ucs4 < 0x800) { *d++= ((ucs4 >> 6) & 0x1F) | 0xC0; bits= 0; }
570 else if (ucs4 < 0x10000) { *d++= ((ucs4 >> 12) & 0x0F) | 0xE0; bits= 6; }
571 else if (ucs4 < 0x200000) { *d++= ((ucs4 >> 18) & 0x07) | 0xF0; bits= 12; }
572 else if (ucs4 < 0x4000000) { *d++= ((ucs4 >> 24) & 0x03) | 0xF8; bits= 18; }
573 else if (ucs4 < 0x80000000) { *d++= ((ucs4 >> 30) & 0x01) | 0xFC; bits= 24; }
576 for ( ; bits >= 0; bits-= 6) {
577 *d++= ((ucs4 >> bits) & 0x3F) | 0x80;
582 #define GetUtf16(src,endian) \
583 ((FcChar16) ((src)[endian == FcEndianBig ? 0 : 1] << 8) | \
584 (FcChar16) ((src)[endian == FcEndianBig ? 1 : 0]))
587 FcUtf16ToUcs4 (const FcChar8 *src_orig,
590 int len) /* in bytes */
592 const FcChar8 *src = src_orig;
599 a = GetUtf16 (src, endian); src += 2; len -= 2;
602 * Check for surrogate
604 if ((a & 0xfc00) == 0xd800)
608 b = GetUtf16 (src, endian); src += 2; len -= 2;
610 * Check for invalid surrogate sequence
612 if ((b & 0xfc00) != 0xdc00)
614 result = ((((FcChar32) a & 0x3ff) << 10) |
615 ((FcChar32) b & 0x3ff)) + 0x10000;
620 return src - src_orig;
624 FcUtf16Len (const FcChar8 *string,
626 int len, /* in bytes */
639 clen = FcUtf16ToUcs4 (string, endian, &c, len);
640 if (clen <= 0) /* malformed UTF8 string */
651 else if (max > 0x100)
659 FcStrBufInit (FcStrBuf *buf, FcChar8 *init, int size)
662 buf->allocated = FcFalse;
663 buf->failed = FcFalse;
669 FcStrBufDestroy (FcStrBuf *buf)
673 FcMemFree (FC_MEM_STRBUF, buf->size);
675 FcStrBufInit (buf, 0, 0);
680 FcStrBufDone (FcStrBuf *buf)
684 ret = malloc (buf->len + 1);
687 FcMemAlloc (FC_MEM_STRING, buf->len + 1);
688 memcpy (ret, buf->buf, buf->len);
689 ret[buf->len] = '\0';
691 FcStrBufDestroy (buf);
696 FcStrBufChar (FcStrBuf *buf, FcChar8 c)
698 if (buf->len == buf->size)
705 size = buf->size * 2;
706 new = realloc (buf->buf, size);
710 size = buf->size + 1024;
714 buf->allocated = FcTrue;
715 memcpy (new, buf->buf, buf->len);
720 buf->failed = FcTrue;
724 FcMemFree (FC_MEM_STRBUF, buf->size);
725 FcMemAlloc (FC_MEM_STRBUF, size);
729 buf->buf[buf->len++] = c;
734 FcStrBufString (FcStrBuf *buf, const FcChar8 *s)
738 if (!FcStrBufChar (buf, c))
744 FcStrBufData (FcStrBuf *buf, const FcChar8 *s, int len)
747 if (!FcStrBufChar (buf, *s++))
753 FcStrUsesHome (const FcChar8 *s)
759 FcStrCopyFilename (const FcChar8 *s)
765 FcChar8 *home = FcConfigHome ();
769 size = strlen ((char *) home) + strlen ((char *) s);
770 new = (FcChar8 *) malloc (size);
773 FcMemAlloc (FC_MEM_STRING, size);
774 strcpy ((char *) new, (char *) home);
775 strcat ((char *) new, (char *) s + 1);
779 int size = strlen ((char *) s) + 1;
780 new = (FcChar8 *) malloc (size);
783 FcMemAlloc (FC_MEM_STRING, size);
784 strcpy ((char *) new, (const char *) s);
790 FcStrLastSlash (const FcChar8 *path)
794 slash = (FcChar8 *) strrchr ((const char *) path, '/');
799 backslash = (FcChar8 *) strrchr ((const char *) path, '\\');
800 if (!slash || (backslash && backslash > slash))
809 FcStrDirname (const FcChar8 *file)
814 slash = FcStrLastSlash (file);
816 return FcStrCopy ((FcChar8 *) ".");
817 dir = malloc ((slash - file) + 1);
820 FcMemAlloc (FC_MEM_STRING, (slash - file) + 1);
821 strncpy ((char *) dir, (const char *) file, slash - file);
822 dir[slash - file] = '\0';
827 FcStrBasename (const FcChar8 *file)
831 slash = FcStrLastSlash (file);
833 return FcStrCopy (file);
834 return FcStrCopy (slash + 1);
838 FcStrSetCreate (void)
840 FcStrSet *set = malloc (sizeof (FcStrSet));
843 FcMemAlloc (FC_MEM_STRSET, sizeof (FcStrSet));
852 _FcStrSetAppend (FcStrSet *set, FcChar8 *s)
854 if (FcStrSetMember (set, s))
859 if (set->num == set->size)
861 FcChar8 **strs = malloc ((set->size + 2) * sizeof (FcChar8 *));
865 FcMemAlloc (FC_MEM_STRSET, (set->size + 2) * sizeof (FcChar8 *));
866 set->size = set->size + 1;
868 memcpy (strs, set->strs, set->num * sizeof (FcChar8 *));
873 set->strs[set->num++] = s;
874 set->strs[set->num] = 0;
879 FcStrSetMember (FcStrSet *set, const FcChar8 *s)
883 for (i = 0; i < set->num; i++)
884 if (!FcStrCmp (set->strs[i], s))
890 FcStrSetEqual (FcStrSet *sa, FcStrSet *sb)
893 if (sa->num != sb->num)
895 for (i = 0; i < sa->num; i++)
896 if (!FcStrSetMember (sb, sa->strs[i]))
902 FcStrSetAdd (FcStrSet *set, const FcChar8 *s)
904 FcChar8 *new = FcStrCopy (s);
907 if (!_FcStrSetAppend (set, new))
916 FcStrSetAddFilename (FcStrSet *set, const FcChar8 *s)
918 FcChar8 *new = FcStrCopyFilename (s);
921 if (!_FcStrSetAppend (set, new))
930 FcStrSetDel (FcStrSet *set, const FcChar8 *s)
934 for (i = 0; i < set->num; i++)
935 if (!FcStrCmp (set->strs[i], s))
937 FcStrFree (set->strs[i]);
939 * copy remaining string pointers and trailing
942 memmove (&set->strs[i], &set->strs[i+1],
943 (set->num - i) * sizeof (FcChar8 *));
951 FcStrSetDestroy (FcStrSet *set)
957 for (i = 0; i < set->num; i++)
958 FcStrFree (set->strs[i]);
959 FcMemFree (FC_MEM_STRSET, (set->size) * sizeof (FcChar8 *));
962 FcMemFree (FC_MEM_STRSET, sizeof (FcStrSet));
968 FcStrListCreate (FcStrSet *set)
972 list = malloc (sizeof (FcStrList));
975 FcMemAlloc (FC_MEM_STRLIST, sizeof (FcStrList));
983 FcStrListNext (FcStrList *list)
985 if (list->n >= list->set->num)
987 return list->set->strs[list->n++];
991 FcStrListDone (FcStrList *list)
993 FcStrSetDestroy (list->set);
994 FcMemFree (FC_MEM_STRLIST, sizeof (FcStrList));