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.
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 * 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)
461 FcUtf8ToUcs4 (const FcChar8 *src_orig,
465 const FcChar8 *src = src_orig;
481 else if (!(s & 0x40))
485 else if (!(s & 0x20))
490 else if (!(s & 0x10))
495 else if (!(s & 0x08))
500 else if (!(s & 0x04))
505 else if ( ! (s & 0x02))
522 if ((s & 0xc0) != 0x80)
528 return src - src_orig;
532 FcUtf8Len (const FcChar8 *string,
546 clen = FcUtf8ToUcs4 (string, &c, len);
547 if (clen <= 0) /* malformed UTF8 string */
558 else if (max > 0x100)
566 FcUcs4ToUtf8 (FcChar32 ucs4,
567 FcChar8 dest[FC_UTF8_MAX_LEN])
572 if (ucs4 < 0x80) { *d++= ucs4; bits= -6; }
573 else if (ucs4 < 0x800) { *d++= ((ucs4 >> 6) & 0x1F) | 0xC0; bits= 0; }
574 else if (ucs4 < 0x10000) { *d++= ((ucs4 >> 12) & 0x0F) | 0xE0; bits= 6; }
575 else if (ucs4 < 0x200000) { *d++= ((ucs4 >> 18) & 0x07) | 0xF0; bits= 12; }
576 else if (ucs4 < 0x4000000) { *d++= ((ucs4 >> 24) & 0x03) | 0xF8; bits= 18; }
577 else if (ucs4 < 0x80000000) { *d++= ((ucs4 >> 30) & 0x01) | 0xFC; bits= 24; }
580 for ( ; bits >= 0; bits-= 6) {
581 *d++= ((ucs4 >> bits) & 0x3F) | 0x80;
586 #define GetUtf16(src,endian) \
587 ((FcChar16) ((src)[endian == FcEndianBig ? 0 : 1] << 8) | \
588 (FcChar16) ((src)[endian == FcEndianBig ? 1 : 0]))
591 FcUtf16ToUcs4 (const FcChar8 *src_orig,
594 int len) /* in bytes */
596 const FcChar8 *src = src_orig;
603 a = GetUtf16 (src, endian); src += 2; len -= 2;
606 * Check for surrogate
608 if ((a & 0xfc00) == 0xd800)
612 b = GetUtf16 (src, endian); src += 2; len -= 2;
614 * Check for invalid surrogate sequence
616 if ((b & 0xfc00) != 0xdc00)
618 result = ((((FcChar32) a & 0x3ff) << 10) |
619 ((FcChar32) b & 0x3ff)) + 0x10000;
624 return src - src_orig;
628 FcUtf16Len (const FcChar8 *string,
630 int len, /* in bytes */
643 clen = FcUtf16ToUcs4 (string, endian, &c, len);
644 if (clen <= 0) /* malformed UTF8 string */
655 else if (max > 0x100)
663 FcStrBufInit (FcStrBuf *buf, FcChar8 *init, int size)
666 buf->allocated = FcFalse;
667 buf->failed = FcFalse;
673 FcStrBufDestroy (FcStrBuf *buf)
677 FcMemFree (FC_MEM_STRBUF, buf->size);
679 FcStrBufInit (buf, 0, 0);
684 FcStrBufDone (FcStrBuf *buf)
688 ret = malloc (buf->len + 1);
691 FcMemAlloc (FC_MEM_STRING, buf->len + 1);
692 memcpy (ret, buf->buf, buf->len);
693 ret[buf->len] = '\0';
695 FcStrBufDestroy (buf);
700 FcStrBufChar (FcStrBuf *buf, FcChar8 c)
702 if (buf->len == buf->size)
709 size = buf->size * 2;
710 new = realloc (buf->buf, size);
714 size = buf->size + 64;
718 buf->allocated = FcTrue;
719 memcpy (new, buf->buf, buf->len);
724 buf->failed = FcTrue;
728 FcMemFree (FC_MEM_STRBUF, buf->size);
729 FcMemAlloc (FC_MEM_STRBUF, size);
733 buf->buf[buf->len++] = c;
738 FcStrBufString (FcStrBuf *buf, const FcChar8 *s)
742 if (!FcStrBufChar (buf, c))
748 FcStrBufData (FcStrBuf *buf, const FcChar8 *s, int len)
751 if (!FcStrBufChar (buf, *s++))
757 FcStrUsesHome (const FcChar8 *s)
763 FcStrCopyFilename (const FcChar8 *s)
769 FcChar8 *home = FcConfigHome ();
774 size = strlen ((char *) home) + strlen ((char *) s);
775 full = (FcChar8 *) malloc (size);
778 strcpy ((char *) full, (char *) home);
779 strcat ((char *) full, (char *) s + 1);
780 new = FcStrCanonFilename (full);
784 new = FcStrCanonFilename (s);
789 FcStrLastSlash (const FcChar8 *path)
793 slash = (FcChar8 *) strrchr ((const char *) path, '/');
798 backslash = (FcChar8 *) strrchr ((const char *) path, '\\');
799 if (!slash || (backslash && backslash > slash))
808 FcStrDirname (const FcChar8 *file)
813 slash = FcStrLastSlash (file);
815 return FcStrCopy ((FcChar8 *) ".");
816 dir = malloc ((slash - file) + 1);
819 FcMemAlloc (FC_MEM_STRING, (slash - file) + 1);
820 strncpy ((char *) dir, (const char *) file, slash - file);
821 dir[slash - file] = '\0';
826 FcStrBasename (const FcChar8 *file)
830 slash = FcStrLastSlash (file);
832 return FcStrCopy (file);
833 return FcStrCopy (slash + 1);
837 FcStrCanonAbsoluteFilename (const FcChar8 *s)
841 const FcChar8 *slash;
844 size = strlen ((char *) s) + 1;
845 file = malloc (size);
848 FcMemAlloc (FC_MEM_STRING, size);
852 if (*s == '/' || *s == '\0')
858 if (!strncmp ((char *) slash, "/.", 2))
860 f -= 2; /* trim /. from file */
864 if (!strncmp ((char *) slash, "/..", 3))
866 f -= 3; /* trim /.. from file */
885 * Convert '\\' to '/' , remove double '/'
888 FcConvertDosPath (char *str)
890 size_t len = strlen (str);
893 char *end = str + len;
916 FcStrCanonFilename (const FcChar8 *s)
919 FcChar8 full[FC_MAX_FILE_LEN + 2];
920 FcChar8 basename[FC_MAX_FILE_LEN + 2];
921 int size = GetFullPathName (s, sizeof (full) -1,
926 perror ("GetFullPathName");
928 FcConvertDosPath (full);
929 return FcStrCanonAbsoluteFilename (full);
932 return FcStrCanonAbsoluteFilename (s);
938 FcChar8 cwd[FC_MAX_FILE_LEN + 2];
939 if (getcwd ((char *) cwd, FC_MAX_FILE_LEN) == NULL)
941 strcat ((char *) cwd, "/");
942 full = FcStrPlus (cwd, s);
943 file = FcStrCanonAbsoluteFilename (full);
952 FcStrSetCreate (void)
954 FcStrSet *set = malloc (sizeof (FcStrSet));
957 FcMemAlloc (FC_MEM_STRSET, sizeof (FcStrSet));
966 _FcStrSetAppend (FcStrSet *set, FcChar8 *s)
968 if (FcStrSetMember (set, s))
973 if (set->num == set->size)
975 FcChar8 **strs = malloc ((set->size + 2) * sizeof (FcChar8 *));
979 FcMemAlloc (FC_MEM_STRSET, (set->size + 2) * sizeof (FcChar8 *));
980 set->size = set->size + 1;
982 memcpy (strs, set->strs, set->num * sizeof (FcChar8 *));
987 set->strs[set->num++] = s;
988 set->strs[set->num] = 0;
993 FcStrSetMember (FcStrSet *set, const FcChar8 *s)
997 for (i = 0; i < set->num; i++)
998 if (!FcStrCmp (set->strs[i], s))
1004 FcStrSetEqual (FcStrSet *sa, FcStrSet *sb)
1007 if (sa->num != sb->num)
1009 for (i = 0; i < sa->num; i++)
1010 if (!FcStrSetMember (sb, sa->strs[i]))
1016 FcStrSetAdd (FcStrSet *set, const FcChar8 *s)
1018 FcChar8 *new = FcStrCopy (s);
1021 if (!_FcStrSetAppend (set, new))
1030 FcStrSetAddFilename (FcStrSet *set, const FcChar8 *s)
1032 FcChar8 *new = FcStrCopyFilename (s);
1035 if (!_FcStrSetAppend (set, new))
1044 FcStrSetDel (FcStrSet *set, const FcChar8 *s)
1048 for (i = 0; i < set->num; i++)
1049 if (!FcStrCmp (set->strs[i], s))
1051 FcStrFree (set->strs[i]);
1053 * copy remaining string pointers and trailing
1056 memmove (&set->strs[i], &set->strs[i+1],
1057 (set->num - i) * sizeof (FcChar8 *));
1065 FcStrSetDestroy (FcStrSet *set)
1067 if (--set->ref == 0)
1071 for (i = 0; i < set->num; i++)
1072 FcStrFree (set->strs[i]);
1073 FcMemFree (FC_MEM_STRSET, (set->size) * sizeof (FcChar8 *));
1076 FcMemFree (FC_MEM_STRSET, sizeof (FcStrSet));
1082 FcStrListCreate (FcStrSet *set)
1086 list = malloc (sizeof (FcStrList));
1089 FcMemAlloc (FC_MEM_STRLIST, sizeof (FcStrList));
1097 FcStrListNext (FcStrList *list)
1099 if (list->n >= list->set->num)
1101 return list->set->strs[list->n++];
1105 FcStrListDone (FcStrList *list)
1107 FcStrSetDestroy (list->set);
1108 FcMemFree (FC_MEM_STRLIST, sizeof (FcStrList));
1113 #include "fcaliastail.h"