]>
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 ((char *) 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
));
856 _FcStrSetAppend (FcStrSet
*set
, FcChar8
*s
)
858 if (FcStrSetMember (set
, s
))
863 if (set
->num
== set
->size
)
865 FcChar8
**strs
= malloc ((set
->size
+ 2) * sizeof (FcChar8
*));
869 FcMemAlloc (FC_MEM_STRSET
, (set
->size
+ 2) * sizeof (FcChar8
*));
870 set
->size
= set
->size
+ 1;
872 memcpy (strs
, set
->strs
, set
->num
* sizeof (FcChar8
*));
877 set
->strs
[set
->num
++] = s
;
878 set
->strs
[set
->num
] = 0;
883 FcStrSetMember (FcStrSet
*set
, const FcChar8
*s
)
887 for (i
= 0; i
< set
->num
; i
++)
888 if (!FcStrCmp (set
->strs
[i
], s
))
894 FcStrSetEqual (FcStrSet
*sa
, FcStrSet
*sb
)
897 if (sa
->num
!= sb
->num
)
899 for (i
= 0; i
< sa
->num
; i
++)
900 if (!FcStrSetMember (sb
, sa
->strs
[i
]))
906 FcStrSetAdd (FcStrSet
*set
, const FcChar8
*s
)
908 FcChar8
*new = FcStrCopy (s
);
911 if (!_FcStrSetAppend (set
, new))
920 FcStrSetAddFilename (FcStrSet
*set
, const FcChar8
*s
)
922 FcChar8
*new = FcStrCopyFilename (s
);
925 if (!_FcStrSetAppend (set
, new))
934 FcStrSetDel (FcStrSet
*set
, const FcChar8
*s
)
938 for (i
= 0; i
< set
->num
; i
++)
939 if (!FcStrCmp (set
->strs
[i
], s
))
941 FcStrFree (set
->strs
[i
]);
943 * copy remaining string pointers and trailing
946 memmove (&set
->strs
[i
], &set
->strs
[i
+1],
947 (set
->num
- i
) * sizeof (FcChar8
*));
955 FcStrSetDestroy (FcStrSet
*set
)
961 for (i
= 0; i
< set
->num
; i
++)
962 FcStrFree (set
->strs
[i
]);
963 FcMemFree (FC_MEM_STRSET
, (set
->size
) * sizeof (FcChar8
*));
966 FcMemFree (FC_MEM_STRSET
, sizeof (FcStrSet
));
972 FcStrListCreate (FcStrSet
*set
)
976 list
= malloc (sizeof (FcStrList
));
979 FcMemAlloc (FC_MEM_STRLIST
, sizeof (FcStrList
));
987 FcStrListNext (FcStrList
*list
)
989 if (list
->n
>= list
->set
->num
)
991 return list
->set
->strs
[list
->n
++];
995 FcStrListDone (FcStrList
*list
)
997 FcStrSetDestroy (list
->set
);
998 FcMemFree (FC_MEM_STRLIST
, sizeof (FcStrList
));