]>
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
{
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
)
456 FcUtf8ToUcs4 (const FcChar8
*src_orig
,
460 const FcChar8
*src
= src_orig
;
476 else if (!(s
& 0x40))
480 else if (!(s
& 0x20))
485 else if (!(s
& 0x10))
490 else if (!(s
& 0x08))
495 else if (!(s
& 0x04))
500 else if ( ! (s
& 0x02))
517 if ((s
& 0xc0) != 0x80)
523 return src
- src_orig
;
527 FcUtf8Len (const FcChar8
*string
,
541 clen
= FcUtf8ToUcs4 (string
, &c
, len
);
542 if (clen
<= 0) /* malformed UTF8 string */
553 else if (max
> 0x100)
561 FcUcs4ToUtf8 (FcChar32 ucs4
,
562 FcChar8 dest
[FC_UTF8_MAX_LEN
])
567 if (ucs4
< 0x80) { *d
++= ucs4
; bits
= -6; }
568 else if (ucs4
< 0x800) { *d
++= ((ucs4
>> 6) & 0x1F) | 0xC0; bits
= 0; }
569 else if (ucs4
< 0x10000) { *d
++= ((ucs4
>> 12) & 0x0F) | 0xE0; bits
= 6; }
570 else if (ucs4
< 0x200000) { *d
++= ((ucs4
>> 18) & 0x07) | 0xF0; bits
= 12; }
571 else if (ucs4
< 0x4000000) { *d
++= ((ucs4
>> 24) & 0x03) | 0xF8; bits
= 18; }
572 else if (ucs4
< 0x80000000) { *d
++= ((ucs4
>> 30) & 0x01) | 0xFC; bits
= 24; }
575 for ( ; bits
>= 0; bits
-= 6) {
576 *d
++= ((ucs4
>> bits
) & 0x3F) | 0x80;
581 #define GetUtf16(src,endian) \
582 ((FcChar16) ((src)[endian == FcEndianBig ? 0 : 1] << 8) | \
583 (FcChar16) ((src)[endian == FcEndianBig ? 1 : 0]))
586 FcUtf16ToUcs4 (const FcChar8
*src_orig
,
589 int len
) /* in bytes */
591 const FcChar8
*src
= src_orig
;
598 a
= GetUtf16 (src
, endian
); src
+= 2; len
-= 2;
601 * Check for surrogate
603 if ((a
& 0xfc00) == 0xd800)
607 b
= GetUtf16 (src
, endian
); src
+= 2; len
-= 2;
609 * Check for invalid surrogate sequence
611 if ((b
& 0xfc00) != 0xdc00)
613 result
= ((((FcChar32
) a
& 0x3ff) << 10) |
614 ((FcChar32
) b
& 0x3ff)) + 0x10000;
619 return src
- src_orig
;
623 FcUtf16Len (const FcChar8
*string
,
625 int len
, /* in bytes */
638 clen
= FcUtf16ToUcs4 (string
, endian
, &c
, len
);
639 if (clen
<= 0) /* malformed UTF8 string */
650 else if (max
> 0x100)
658 FcStrBufInit (FcStrBuf
*buf
, FcChar8
*init
, int size
)
661 buf
->allocated
= FcFalse
;
662 buf
->failed
= FcFalse
;
668 FcStrBufDestroy (FcStrBuf
*buf
)
672 FcMemFree (FC_MEM_STRBUF
, buf
->size
);
674 FcStrBufInit (buf
, 0, 0);
679 FcStrBufDone (FcStrBuf
*buf
)
683 ret
= malloc (buf
->len
+ 1);
686 FcMemAlloc (FC_MEM_STRING
, buf
->len
+ 1);
687 memcpy (ret
, buf
->buf
, buf
->len
);
688 ret
[buf
->len
] = '\0';
690 FcStrBufDestroy (buf
);
695 FcStrBufChar (FcStrBuf
*buf
, FcChar8 c
)
697 if (buf
->len
== buf
->size
)
704 size
= buf
->size
* 2;
705 new = realloc (buf
->buf
, size
);
709 size
= buf
->size
+ 1024;
713 buf
->allocated
= FcTrue
;
714 memcpy (new, buf
->buf
, buf
->len
);
719 buf
->failed
= FcTrue
;
723 FcMemFree (FC_MEM_STRBUF
, buf
->size
);
724 FcMemAlloc (FC_MEM_STRBUF
, size
);
728 buf
->buf
[buf
->len
++] = c
;
733 FcStrBufString (FcStrBuf
*buf
, const FcChar8
*s
)
737 if (!FcStrBufChar (buf
, c
))
743 FcStrBufData (FcStrBuf
*buf
, const FcChar8
*s
, int len
)
746 if (!FcStrBufChar (buf
, *s
++))
752 FcStrUsesHome (const FcChar8
*s
)
758 FcStrCopyFilename (const FcChar8
*s
)
764 FcChar8
*home
= FcConfigHome ();
768 size
= strlen ((char *) home
) + strlen ((char *) s
);
769 new = (FcChar8
*) malloc (size
);
772 FcMemAlloc (FC_MEM_STRING
, size
);
773 strcpy ((char *) new, (char *) home
);
774 strcat ((char *) new, (char *) s
+ 1);
778 int size
= strlen ((char *) s
) + 1;
779 new = (FcChar8
*) malloc (size
);
782 FcMemAlloc (FC_MEM_STRING
, size
);
783 strcpy ((char *) new, (const char *) 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 FcStrSetCreate (void)
839 FcStrSet
*set
= malloc (sizeof (FcStrSet
));
842 FcMemAlloc (FC_MEM_STRSET
, sizeof (FcStrSet
));
851 _FcStrSetAppend (FcStrSet
*set
, FcChar8
*s
)
853 if (FcStrSetMember (set
, s
))
858 if (set
->num
== set
->size
)
860 FcChar8
**strs
= malloc ((set
->size
+ 2) * sizeof (FcChar8
*));
864 FcMemAlloc (FC_MEM_STRSET
, (set
->size
+ 2) * sizeof (FcChar8
*));
865 set
->size
= set
->size
+ 1;
867 memcpy (strs
, set
->strs
, set
->num
* sizeof (FcChar8
*));
872 set
->strs
[set
->num
++] = s
;
873 set
->strs
[set
->num
] = 0;
878 FcStrSetMember (FcStrSet
*set
, const FcChar8
*s
)
882 for (i
= 0; i
< set
->num
; i
++)
883 if (!FcStrCmp (set
->strs
[i
], s
))
889 FcStrSetEqual (FcStrSet
*sa
, FcStrSet
*sb
)
892 if (sa
->num
!= sb
->num
)
894 for (i
= 0; i
< sa
->num
; i
++)
895 if (!FcStrSetMember (sb
, sa
->strs
[i
]))
901 FcStrSetAdd (FcStrSet
*set
, const FcChar8
*s
)
903 FcChar8
*new = FcStrCopy (s
);
906 if (!_FcStrSetAppend (set
, new))
915 FcStrSetAddFilename (FcStrSet
*set
, const FcChar8
*s
)
917 FcChar8
*new = FcStrCopyFilename (s
);
920 if (!_FcStrSetAppend (set
, new))
929 FcStrSetDel (FcStrSet
*set
, const FcChar8
*s
)
933 for (i
= 0; i
< set
->num
; i
++)
934 if (!FcStrCmp (set
->strs
[i
], s
))
936 FcStrFree (set
->strs
[i
]);
938 * copy remaining string pointers and trailing
941 memmove (&set
->strs
[i
], &set
->strs
[i
+1],
942 (set
->num
- i
) * sizeof (FcChar8
*));
950 FcStrSetDestroy (FcStrSet
*set
)
956 for (i
= 0; i
< set
->num
; i
++)
957 FcStrFree (set
->strs
[i
]);
958 FcMemFree (FC_MEM_STRSET
, (set
->size
) * sizeof (FcChar8
*));
961 FcMemFree (FC_MEM_STRSET
, sizeof (FcStrSet
));
967 FcStrListCreate (FcStrSet
*set
)
971 list
= malloc (sizeof (FcStrList
));
974 FcMemAlloc (FC_MEM_STRLIST
, sizeof (FcStrList
));
982 FcStrListNext (FcStrList
*list
)
984 if (list
->n
>= list
->set
->num
)
986 return list
->set
->strs
[list
->n
++];
990 FcStrListDone (FcStrList
*list
)
992 FcStrSetDestroy (list
->set
);
993 FcMemFree (FC_MEM_STRLIST
, sizeof (FcStrList
));