]>
git.wh0rd.org - fontconfig.git/blob - src/fcstr.c
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
{
78 FcChar8 utf8
[FC_MAX_CASE_FOLD_CHARS
+ 1];
82 FcStrCaseWalkerInit (const FcChar8
*src
, FcCaseWalker
*w
)
86 w
->len
= strlen (src
);
90 FcStrCaseWalkerLong (FcCaseWalker
*w
, FcChar8 r
)
95 slen
= FcUtf8ToUcs4 (w
->src
- 1, &ucs4
, w
->len
+ 1);
98 if (FC_MIN_FOLD_CHAR
<= ucs4
&& ucs4
<= FC_MAX_FOLD_CHAR
)
101 int max
= FC_NUM_CASE_FOLD
;
105 int mid
= (min
+ max
) >> 1;
106 FcChar32 low
= fcCaseFold
[mid
].upper
;
107 FcChar32 high
= low
+ FcCaseFoldUpperCount (&fcCaseFold
[mid
]);
115 const FcCaseFold
*fold
= &fcCaseFold
[mid
];
118 switch (fold
->method
) {
119 case FC_CASE_FOLD_EVEN_ODD
:
120 if ((ucs4
& 1) != (fold
->upper
& 1))
122 /* fall through ... */
124 dlen
= FcUcs4ToUtf8 (ucs4
+ fold
->offset
, w
->utf8
);
126 case FC_CASE_FOLD_FULL
:
128 memcpy (w
->utf8
, fcCaseFoldChars
+ fold
->offset
, dlen
);
132 /* consume rest of src utf-8 bytes */
136 /* read from temp buffer */
137 w
->utf8
[dlen
] = '\0';
147 FcStrCaseWalkerNext (FcCaseWalker
*w
)
153 if ((r
= *w
->read
++))
160 if ((r
& 0xc0) == 0xc0)
161 return FcStrCaseWalkerLong (w
, r
);
162 if ('A' <= r
&& r
<= 'Z')
168 FcStrCaseWalkerNextIgnoreBlanks (FcCaseWalker
*w
)
174 if ((r
= *w
->read
++))
184 if ((r
& 0xc0) == 0xc0)
185 return FcStrCaseWalkerLong (w
, r
);
186 if ('A' <= r
&& r
<= 'Z')
192 FcStrDowncase (const FcChar8
*s
)
198 FcStrCaseWalkerInit (s
, &w
);
199 while (FcStrCaseWalkerNext (&w
))
201 d
= dst
= malloc (len
+ 1);
204 FcMemAlloc (FC_MEM_STRING
, len
+ 1);
205 FcStrCaseWalkerInit (s
, &w
);
206 while ((*d
++ = FcStrCaseWalkerNext (&w
)));
211 FcStrCmpIgnoreCase (const FcChar8
*s1
, const FcChar8
*s2
)
216 if (s1
== s2
) return 0;
218 FcStrCaseWalkerInit (s1
, &w1
);
219 FcStrCaseWalkerInit (s2
, &w2
);
223 c1
= FcStrCaseWalkerNext (&w1
);
224 c2
= FcStrCaseWalkerNext (&w2
);
225 if (!c1
|| (c1
!= c2
))
228 return (int) c1
- (int) c2
;
232 FcStrCmpIgnoreBlanksAndCase (const FcChar8
*s1
, const FcChar8
*s2
)
237 if (s1
== s2
) return 0;
239 FcStrCaseWalkerInit (s1
, &w1
);
240 FcStrCaseWalkerInit (s2
, &w2
);
244 c1
= FcStrCaseWalkerNextIgnoreBlanks (&w1
);
245 c2
= FcStrCaseWalkerNextIgnoreBlanks (&w2
);
246 if (!c1
|| (c1
!= c2
))
249 return (int) c1
- (int) c2
;
253 FcStrCmp (const FcChar8
*s1
, const FcChar8
*s2
)
266 return (int) c1
- (int) c2
;
270 * Return a hash value for a string
274 FcStrHashIgnoreCase (const FcChar8
*s
)
280 FcStrCaseWalkerInit (s
, &w
);
281 while ((c
= FcStrCaseWalkerNext (&w
)))
282 h
= ((h
<< 3) ^ (h
>> 3)) ^ c
;
287 * Is the head of s1 equal to s2?
291 FcStrIsAtIgnoreBlanksAndCase (const FcChar8
*s1
, const FcChar8
*s2
)
296 FcStrCaseWalkerInit (s1
, &w1
);
297 FcStrCaseWalkerInit (s2
, &w2
);
301 c1
= FcStrCaseWalkerNextIgnoreBlanks (&w1
);
302 c2
= FcStrCaseWalkerNextIgnoreBlanks (&w2
);
303 if (!c1
|| (c1
!= c2
))
306 return c1
== c2
|| !c2
;
310 * Does s1 contain an instance of s2 (ignoring blanks and case)?
314 FcStrContainsIgnoreBlanksAndCase (const FcChar8
*s1
, const FcChar8
*s2
)
318 if (FcStrIsAtIgnoreBlanksAndCase (s1
, s2
))
326 * Is the head of s1 equal to s2?
330 FcStrIsAtIgnoreCase (const FcChar8
*s1
, const FcChar8
*s2
)
335 FcStrCaseWalkerInit (s1
, &w1
);
336 FcStrCaseWalkerInit (s2
, &w2
);
340 c1
= FcStrCaseWalkerNext (&w1
);
341 c2
= FcStrCaseWalkerNext (&w2
);
342 if (!c1
|| (c1
!= c2
))
345 return c1
== c2
|| !c2
;
349 * Does s1 contain an instance of s2 (ignoring blanks and case)?
353 FcStrContainsIgnoreCase (const FcChar8
*s1
, const FcChar8
*s2
)
357 if (FcStrIsAtIgnoreCase (s1
, s2
))
365 FcStrStrIgnoreCase (const FcChar8
*s1
, const FcChar8
*s2
)
377 FcStrCaseWalkerInit (s1
, &w1
);
378 FcStrCaseWalkerInit (s2
, &w2
);
380 c2
= FcStrCaseWalkerNext (&w2
);
385 c1
= FcStrCaseWalkerNext (&w1
);
390 FcCaseWalker w1t
= w1
;
391 FcCaseWalker w2t
= w2
;
396 c1t
= FcStrCaseWalkerNext (&w1t
);
397 c2t
= FcStrCaseWalkerNext (&w2t
);
410 FcStrStr (const FcChar8
*s1
, const FcChar8
*s2
)
413 const FcChar8
* p
= s1
;
414 const FcChar8
* b
= s2
;
443 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
+ 1024;
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 ();
773 size
= strlen ((char *) home
) + strlen ((char *) s
);
774 new = (FcChar8
*) malloc (size
);
777 FcMemAlloc (FC_MEM_STRING
, size
);
778 strcpy ((char *) new, (char *) home
);
779 strcat ((char *) new, (char *) s
+ 1);
783 int size
= strlen ((char *) s
) + 1;
784 new = (FcChar8
*) malloc (size
);
787 FcMemAlloc (FC_MEM_STRING
, size
);
788 strcpy ((char *) new, (const char *) s
);
794 FcStrLastSlash (const FcChar8
*path
)
798 slash
= (FcChar8
*) strrchr ((const char *) path
, '/');
803 backslash
= (FcChar8
*) strrchr ((const char *) path
, '\\');
804 if (!slash
|| (backslash
&& backslash
> slash
))
813 FcStrDirname (const FcChar8
*file
)
818 slash
= FcStrLastSlash (file
);
820 return FcStrCopy ((FcChar8
*) ".");
821 dir
= malloc ((slash
- file
) + 1);
824 FcMemAlloc (FC_MEM_STRING
, (slash
- file
) + 1);
825 strncpy ((char *) dir
, (const char *) file
, slash
- file
);
826 dir
[slash
- file
] = '\0';
831 FcStrBasename (const FcChar8
*file
)
835 slash
= FcStrLastSlash (file
);
837 return FcStrCopy (file
);
838 return FcStrCopy (slash
+ 1);
842 FcStrSetCreate (void)
844 FcStrSet
*set
= malloc (sizeof (FcStrSet
));
847 FcMemAlloc (FC_MEM_STRSET
, sizeof (FcStrSet
));
851 set
->storage
= FcStorageDynamic
;
856 static FcChar8
* strset_buf
= 0;
857 static int strset_buf_ptr
= 0, strset_buf_count
= 0;
858 static int * strset_idx
= 0;
859 static int strset_idx_ptr
= 0, strset_idx_count
= 0;
860 static FcStrSet
* strsets
= 0;
861 static int strset_ptr
= 0, strset_count
= 0;
863 void FcStrSetClearStatic()
865 strset_buf
= 0; strset_buf_ptr
= 0; strset_buf_count
= 0;
866 strset_idx
= 0; strset_idx_ptr
= 0; strset_idx_count
= 0;
867 strsets
= 0; strset_ptr
= 0; strset_count
= 0;
871 FcStrSetGet (const FcStrSet
*set
, int i
)
874 switch (set
->storage
)
876 case FcStorageStatic
:
877 index
= strset_idx
[set
->u
.stridx_offset
];
880 return &strset_buf
[index
];
881 case FcStorageDynamic
:
882 return set
->u
.strs
[i
];
889 FcStrSetPtrU (const FcStrSetPtr set
)
893 case FcStorageStatic
:
894 return &strsets
[set
.u
.stat
];
895 case FcStorageDynamic
:
896 return (FcStrSet
*)set
.u
.dyn
;
903 FcStrSetPtrCreateDynamic (const FcStrSet
* set
)
907 new.storage
= FcStorageDynamic
;
908 new.u
.dyn
= (FcStrSet
*)set
;
913 _FcStrSetAppend (FcStrSet
*set
, FcChar8
*s
)
915 if (FcStrSetMember (set
, s
))
920 if (set
->num
== set
->size
|| set
->storage
== FcStorageStatic
)
922 FcChar8
**strs
= malloc ((set
->size
+ 2) * sizeof (FcChar8
*));
926 FcMemAlloc (FC_MEM_STRSET
, (set
->size
+ 2) * sizeof (FcChar8
*));
927 set
->size
= set
->size
+ 1;
928 if (set
->storage
== FcStorageDynamic
)
931 memcpy (strs
, set
->u
.strs
, set
->num
* sizeof (FcChar8
*));
938 memcpy (strs
, strset_idx
+set
->u
.stridx_offset
,
939 set
->num
* sizeof (FcChar8
*));
940 set
->storage
= FcStorageDynamic
;
944 set
->u
.strs
[set
->num
++] = s
;
945 set
->u
.strs
[set
->num
] = 0;
950 FcStrSetMember (FcStrSet
*set
, const FcChar8
*s
)
954 for (i
= 0; i
< set
->num
; i
++)
955 if (!FcStrCmp (FcStrSetGet(set
, i
), s
))
961 FcStrSetEqual (FcStrSet
*sa
, FcStrSet
*sb
)
964 if (sa
->num
!= sb
->num
)
966 for (i
= 0; i
< sa
->num
; i
++)
967 if (!FcStrSetMember (sb
, FcStrSetGet(sa
, i
)))
973 FcStrSetAdd (FcStrSet
*set
, const FcChar8
*s
)
975 FcChar8
*new = FcStrCopy (s
);
978 if (!_FcStrSetAppend (set
, new))
987 FcStrSetAddFilename (FcStrSet
*set
, const FcChar8
*s
)
989 FcChar8
*new = FcStrCopyFilename (s
);
992 if (!_FcStrSetAppend (set
, new))
1001 FcStrSetDel (FcStrSet
*set
, const FcChar8
*s
)
1005 for (i
= 0; i
< set
->num
; i
++)
1006 if (!FcStrCmp (FcStrSetGet(set
, i
), s
))
1008 if (set
->storage
== FcStorageDynamic
)
1009 FcStrFree (set
->u
.strs
[i
]);
1011 * copy remaining string pointers and trailing
1014 memmove (FcStrSetGet(set
, i
), FcStrSetGet(set
, i
+1),
1015 (set
->num
- i
) * sizeof (FcChar8
*));
1023 FcStrSetDestroy (FcStrSet
*set
)
1025 if (--set
->ref
== 0)
1029 if (set
->storage
== FcStorageDynamic
)
1031 for (i
= 0; i
< set
->num
; i
++)
1032 FcStrFree (set
->u
.strs
[i
]);
1033 FcMemFree (FC_MEM_STRSET
, (set
->size
) * sizeof (FcChar8
*));
1036 FcMemFree (FC_MEM_STRSET
, sizeof (FcStrSet
));
1042 static int _FcStrSetSort_helper (const void * a
, const void * b
)
1044 return FcStrCmp (&strset_buf
[(int)a
],
1045 &strset_buf
[(int)b
]);
1049 FcStrSetSort (FcStrSet
* set
)
1051 switch (set
->storage
)
1053 case FcStorageDynamic
:
1054 qsort (set
->u
.strs
, set
->num
, sizeof (FcChar8
*),
1055 (int (*)(const void *, const void *)) FcStrCmp
);
1057 case FcStorageStatic
:
1058 qsort (strset_idx
+set
->u
.stridx_offset
, set
->num
, sizeof (int),
1059 _FcStrSetSort_helper
);
1067 FcStrSetPrepareSerialize (const FcStrSet
*set
)
1075 strset_idx_count
+= set
->num
;
1076 for (i
= 0; i
< set
->num
; i
++)
1078 if (FcStrSetGet(set
, i
))
1079 strset_buf_count
+= strlen(FcStrSetGet(set
, i
));
1086 FcStrSetSerialize (FcStrSet
*set
)
1094 strsets
= malloc (strset_count
* sizeof(FcStrSet
));
1095 if (!strsets
) goto bail1
;
1096 strset_idx
= malloc (strset_idx_count
* sizeof(int));
1097 if (!strset_idx
) goto bail2
;
1098 strset_buf
= malloc (strset_buf_count
* sizeof (FcChar8
));
1099 if (!strset_buf
) goto bail3
;
1103 return FcStrSetPtrCreateDynamic(0);
1105 newp
.storage
= FcStorageStatic
;
1106 newp
.u
.stat
= strset_ptr
;
1108 new = &strsets
[strset_ptr
++];
1109 new->ref
= set
->ref
;
1110 new->num
= set
->num
;
1111 new->size
= set
->num
;
1112 new->storage
= FcStorageStatic
;
1113 new->u
.stridx_offset
= strset_idx_ptr
;
1114 for (i
= 0; i
< set
->num
; i
++)
1116 FcChar8
* s
= FcStrSetGet(set
, i
);
1120 memcpy(strset_buf
+strset_buf_ptr
, s
,
1122 strset_idx
[strset_idx_ptr
++] = strset_buf_ptr
;
1123 strset_buf_ptr
+= strlen((char *)s
)+1;
1126 strset_idx
[strset_idx_ptr
++] = -1;
1129 if (strset_ptr
> strset_count
|| strset_idx_ptr
> strset_idx_count
)
1130 return FcStrSetPtrCreateDynamic(0);
1132 // problem with multiple ptrs to the same LangSet.
1133 // should hash LangSets or something.
1134 // FcStrSetDestroy (set);
1143 return FcStrSetPtrCreateDynamic(0);
1147 FcStrListCreate (FcStrSet
*set
)
1151 list
= malloc (sizeof (FcStrList
));
1154 FcMemAlloc (FC_MEM_STRLIST
, sizeof (FcStrList
));
1162 FcStrListNext (FcStrList
*list
)
1164 if (list
->n
>= list
->set
->num
)
1166 return FcStrSetGet(list
->set
, list
->n
++);
1170 FcStrListDone (FcStrList
*list
)
1172 FcStrSetDestroy (list
->set
);
1173 FcMemFree (FC_MEM_STRLIST
, sizeof (FcStrList
));