]>
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
)
38 len
= strlen ((char *) s
) + 1;
39 r
= (FcChar8
*) malloc (len
);
42 FcMemAlloc (FC_MEM_STRING
, len
);
48 FcStrPlus (const FcChar8
*s1
, const FcChar8
*s2
)
50 int l
= strlen ((char *)s1
) + strlen ((char *) s2
) + 1;
51 FcChar8
*s
= malloc (l
);
55 FcMemAlloc (FC_MEM_STRING
, l
);
56 strcpy ((char *) s
, (char *) s1
);
57 strcat ((char *) s
, (char *) s2
);
62 FcStrFree (FcChar8
*s
)
64 FcMemFree (FC_MEM_STRING
, strlen ((char *) s
) + 1);
69 #include "../fc-case/fccase.h"
71 #define FcCaseFoldUpperCount(cf) \
72 ((cf)->method == FC_CASE_FOLD_FULL ? 1 : (cf)->count)
74 #define FC_STR_CANON_BUF_LEN 1024
76 typedef struct _FcCaseWalker
{
79 FcChar8 utf8
[FC_MAX_CASE_FOLD_CHARS
+ 1];
83 FcStrCaseWalkerInit (const FcChar8
*src
, FcCaseWalker
*w
)
90 FcStrCaseWalkerLong (FcCaseWalker
*w
, FcChar8 r
)
94 int len
= strlen((char*)w
->src
);
96 slen
= FcUtf8ToUcs4 (w
->src
- 1, &ucs4
, len
+ 1);
99 if (FC_MIN_FOLD_CHAR
<= ucs4
&& ucs4
<= FC_MAX_FOLD_CHAR
)
102 int max
= FC_NUM_CASE_FOLD
;
106 int mid
= (min
+ max
) >> 1;
107 FcChar32 low
= fcCaseFold
[mid
].upper
;
108 FcChar32 high
= low
+ FcCaseFoldUpperCount (&fcCaseFold
[mid
]);
116 const FcCaseFold
*fold
= &fcCaseFold
[mid
];
119 switch (fold
->method
) {
120 case FC_CASE_FOLD_EVEN_ODD
:
121 if ((ucs4
& 1) != (fold
->upper
& 1))
123 /* fall through ... */
125 dlen
= FcUcs4ToUtf8 (ucs4
+ fold
->offset
, w
->utf8
);
127 case FC_CASE_FOLD_FULL
:
129 memcpy (w
->utf8
, fcCaseFoldChars
+ fold
->offset
, dlen
);
133 /* 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
++))
159 if ((r
& 0xc0) == 0xc0)
160 return FcStrCaseWalkerLong (w
, r
);
161 if ('A' <= r
&& r
<= 'Z')
167 FcStrCaseWalkerNextIgnoreBlanks (FcCaseWalker
*w
)
173 if ((r
= *w
->read
++))
182 if ((r
& 0xc0) == 0xc0)
183 return FcStrCaseWalkerLong (w
, r
);
184 if ('A' <= r
&& r
<= 'Z')
190 FcStrDowncase (const FcChar8
*s
)
196 FcStrCaseWalkerInit (s
, &w
);
197 while (FcStrCaseWalkerNext (&w
))
199 d
= dst
= malloc (len
+ 1);
202 FcMemAlloc (FC_MEM_STRING
, len
+ 1);
203 FcStrCaseWalkerInit (s
, &w
);
204 while ((*d
++ = FcStrCaseWalkerNext (&w
)));
209 FcStrCmpIgnoreCase (const FcChar8
*s1
, const FcChar8
*s2
)
214 if (s1
== s2
) return 0;
216 FcStrCaseWalkerInit (s1
, &w1
);
217 FcStrCaseWalkerInit (s2
, &w2
);
221 c1
= FcStrCaseWalkerNext (&w1
);
222 c2
= FcStrCaseWalkerNext (&w2
);
223 if (!c1
|| (c1
!= c2
))
226 return (int) c1
- (int) c2
;
230 FcStrCmpIgnoreBlanksAndCase (const FcChar8
*s1
, const FcChar8
*s2
)
235 if (s1
== s2
) return 0;
237 FcStrCaseWalkerInit (s1
, &w1
);
238 FcStrCaseWalkerInit (s2
, &w2
);
242 c1
= FcStrCaseWalkerNextIgnoreBlanks (&w1
);
243 c2
= FcStrCaseWalkerNextIgnoreBlanks (&w2
);
244 if (!c1
|| (c1
!= c2
))
247 return (int) c1
- (int) c2
;
251 FcStrCmp (const FcChar8
*s1
, const FcChar8
*s2
)
264 return (int) c1
- (int) c2
;
268 * Return a hash value for a string
272 FcStrHashIgnoreCase (const FcChar8
*s
)
278 FcStrCaseWalkerInit (s
, &w
);
279 while ((c
= FcStrCaseWalkerNext (&w
)))
280 h
= ((h
<< 3) ^ (h
>> 3)) ^ c
;
285 * Is the head of s1 equal to s2?
289 FcStrIsAtIgnoreBlanksAndCase (const FcChar8
*s1
, const FcChar8
*s2
)
294 FcStrCaseWalkerInit (s1
, &w1
);
295 FcStrCaseWalkerInit (s2
, &w2
);
299 c1
= FcStrCaseWalkerNextIgnoreBlanks (&w1
);
300 c2
= FcStrCaseWalkerNextIgnoreBlanks (&w2
);
301 if (!c1
|| (c1
!= c2
))
304 return c1
== c2
|| !c2
;
308 * Does s1 contain an instance of s2 (ignoring blanks and case)?
312 FcStrContainsIgnoreBlanksAndCase (const FcChar8
*s1
, const FcChar8
*s2
)
316 if (FcStrIsAtIgnoreBlanksAndCase (s1
, s2
))
324 * Is the head of s1 equal to s2?
328 FcStrIsAtIgnoreCase (const FcChar8
*s1
, const FcChar8
*s2
)
333 FcStrCaseWalkerInit (s1
, &w1
);
334 FcStrCaseWalkerInit (s2
, &w2
);
338 c1
= FcStrCaseWalkerNext (&w1
);
339 c2
= FcStrCaseWalkerNext (&w2
);
340 if (!c1
|| (c1
!= c2
))
343 return c1
== c2
|| !c2
;
347 * Does s1 contain an instance of s2 (ignoring blanks and case)?
351 FcStrContainsIgnoreCase (const FcChar8
*s1
, const FcChar8
*s2
)
355 if (FcStrIsAtIgnoreCase (s1
, s2
))
363 FcStrStrIgnoreCase (const FcChar8
*s1
, const FcChar8
*s2
)
375 FcStrCaseWalkerInit (s1
, &w1
);
376 FcStrCaseWalkerInit (s2
, &w2
);
378 c2
= FcStrCaseWalkerNext (&w2
);
383 c1
= FcStrCaseWalkerNext (&w1
);
388 FcCaseWalker w1t
= w1
;
389 FcCaseWalker w2t
= w2
;
394 c1t
= FcStrCaseWalkerNext (&w1t
);
395 c2t
= FcStrCaseWalkerNext (&w2t
);
408 FcStrStr (const FcChar8
*s1
, const FcChar8
*s2
)
411 const FcChar8
* p
= s1
;
412 const FcChar8
* b
= s2
;
441 if (c1
&& c2
&& c1
!= c2
)
458 FcUtf8ToUcs4 (const FcChar8
*src_orig
,
462 const FcChar8
*src
= src_orig
;
478 else if (!(s
& 0x40))
482 else if (!(s
& 0x20))
487 else if (!(s
& 0x10))
492 else if (!(s
& 0x08))
497 else if (!(s
& 0x04))
502 else if ( ! (s
& 0x02))
519 if ((s
& 0xc0) != 0x80)
525 return src
- src_orig
;
529 FcUtf8Len (const FcChar8
*string
,
543 clen
= FcUtf8ToUcs4 (string
, &c
, len
);
544 if (clen
<= 0) /* malformed UTF8 string */
555 else if (max
> 0x100)
563 FcUcs4ToUtf8 (FcChar32 ucs4
,
564 FcChar8 dest
[FC_UTF8_MAX_LEN
])
569 if (ucs4
< 0x80) { *d
++= ucs4
; bits
= -6; }
570 else if (ucs4
< 0x800) { *d
++= ((ucs4
>> 6) & 0x1F) | 0xC0; bits
= 0; }
571 else if (ucs4
< 0x10000) { *d
++= ((ucs4
>> 12) & 0x0F) | 0xE0; bits
= 6; }
572 else if (ucs4
< 0x200000) { *d
++= ((ucs4
>> 18) & 0x07) | 0xF0; bits
= 12; }
573 else if (ucs4
< 0x4000000) { *d
++= ((ucs4
>> 24) & 0x03) | 0xF8; bits
= 18; }
574 else if (ucs4
< 0x80000000) { *d
++= ((ucs4
>> 30) & 0x01) | 0xFC; bits
= 24; }
577 for ( ; bits
>= 0; bits
-= 6) {
578 *d
++= ((ucs4
>> bits
) & 0x3F) | 0x80;
583 #define GetUtf16(src,endian) \
584 ((FcChar16) ((src)[endian == FcEndianBig ? 0 : 1] << 8) | \
585 (FcChar16) ((src)[endian == FcEndianBig ? 1 : 0]))
588 FcUtf16ToUcs4 (const FcChar8
*src_orig
,
591 int len
) /* in bytes */
593 const FcChar8
*src
= src_orig
;
600 a
= GetUtf16 (src
, endian
); src
+= 2; len
-= 2;
603 * Check for surrogate
605 if ((a
& 0xfc00) == 0xd800)
609 b
= GetUtf16 (src
, endian
); src
+= 2; len
-= 2;
611 * Check for invalid surrogate sequence
613 if ((b
& 0xfc00) != 0xdc00)
615 result
= ((((FcChar32
) a
& 0x3ff) << 10) |
616 ((FcChar32
) b
& 0x3ff)) + 0x10000;
621 return src
- src_orig
;
625 FcUtf16Len (const FcChar8
*string
,
627 int len
, /* in bytes */
640 clen
= FcUtf16ToUcs4 (string
, endian
, &c
, len
);
641 if (clen
<= 0) /* malformed UTF8 string */
652 else if (max
> 0x100)
660 FcStrBufInit (FcStrBuf
*buf
, FcChar8
*init
, int size
)
663 buf
->allocated
= FcFalse
;
664 buf
->failed
= FcFalse
;
670 FcStrBufDestroy (FcStrBuf
*buf
)
674 FcMemFree (FC_MEM_STRBUF
, buf
->size
);
676 FcStrBufInit (buf
, 0, 0);
681 FcStrBufDone (FcStrBuf
*buf
)
685 ret
= malloc (buf
->len
+ 1);
688 FcMemAlloc (FC_MEM_STRING
, buf
->len
+ 1);
689 memcpy (ret
, buf
->buf
, buf
->len
);
690 ret
[buf
->len
] = '\0';
692 FcStrBufDestroy (buf
);
697 FcStrBufChar (FcStrBuf
*buf
, FcChar8 c
)
699 if (buf
->len
== buf
->size
)
706 size
= buf
->size
* 2;
707 new = realloc (buf
->buf
, size
);
711 size
= buf
->size
+ 64;
715 buf
->allocated
= FcTrue
;
716 memcpy (new, buf
->buf
, buf
->len
);
721 buf
->failed
= FcTrue
;
725 FcMemFree (FC_MEM_STRBUF
, buf
->size
);
726 FcMemAlloc (FC_MEM_STRBUF
, size
);
730 buf
->buf
[buf
->len
++] = c
;
735 FcStrBufString (FcStrBuf
*buf
, const FcChar8
*s
)
739 if (!FcStrBufChar (buf
, c
))
745 FcStrBufData (FcStrBuf
*buf
, const FcChar8
*s
, int len
)
748 if (!FcStrBufChar (buf
, *s
++))
754 FcStrUsesHome (const FcChar8
*s
)
760 FcStrCopyFilename (const FcChar8
*s
)
766 FcChar8
*home
= FcConfigHome ();
771 size
= strlen ((char *) home
) + strlen ((char *) s
);
772 full
= (FcChar8
*) malloc (size
);
775 strcpy ((char *) full
, (char *) home
);
776 strcat ((char *) full
, (char *) s
+ 1);
777 new = FcStrCanonFilename (full
);
781 new = FcStrCanonFilename (s
);
786 FcStrLastSlash (const FcChar8
*path
)
790 slash
= (FcChar8
*) strrchr ((const char *) path
, '/');
795 backslash
= (FcChar8
*) strrchr ((const char *) path
, '\\');
796 if (!slash
|| (backslash
&& backslash
> slash
))
805 FcStrDirname (const FcChar8
*file
)
810 slash
= FcStrLastSlash (file
);
812 return FcStrCopy ((FcChar8
*) ".");
813 dir
= malloc ((slash
- file
) + 1);
816 FcMemAlloc (FC_MEM_STRING
, (slash
- file
) + 1);
817 strncpy ((char *) dir
, (const char *) file
, slash
- file
);
818 dir
[slash
- file
] = '\0';
823 FcStrBasename (const FcChar8
*file
)
827 slash
= FcStrLastSlash (file
);
829 return FcStrCopy (file
);
830 return FcStrCopy (slash
+ 1);
834 FcStrCanonFilename (const FcChar8
*s
)
838 const FcChar8
*slash
;
845 FcChar8 cwd
[FC_MAX_FILE_LEN
+ 2];
846 if (getcwd ((char *) cwd
, FC_MAX_FILE_LEN
) == NULL
)
848 strcat ((char *) cwd
, "/");
849 full
= FcStrPlus (cwd
, s
);
850 file
= FcStrCanonFilename (full
);
854 size
= strlen ((char *) s
) + 1;
855 file
= malloc (size
);
858 FcMemAlloc (FC_MEM_STRING
, size
);
862 if (*s
== '/' || *s
== '\0')
868 if (!strncmp ((char *) slash
, "/.", 2))
870 f
-= 2; /* trim /. from file */
874 if (!strncmp ((char *) slash
, "/..", 3))
876 f
-= 3; /* trim /.. from file */
894 FcStrSetCreate (void)
896 FcStrSet
*set
= malloc (sizeof (FcStrSet
));
899 FcMemAlloc (FC_MEM_STRSET
, sizeof (FcStrSet
));
908 _FcStrSetAppend (FcStrSet
*set
, FcChar8
*s
)
910 if (FcStrSetMember (set
, s
))
915 if (set
->num
== set
->size
)
917 FcChar8
**strs
= malloc ((set
->size
+ 2) * sizeof (FcChar8
*));
921 FcMemAlloc (FC_MEM_STRSET
, (set
->size
+ 2) * sizeof (FcChar8
*));
922 set
->size
= set
->size
+ 1;
924 memcpy (strs
, set
->strs
, set
->num
* sizeof (FcChar8
*));
929 set
->strs
[set
->num
++] = s
;
930 set
->strs
[set
->num
] = 0;
935 FcStrSetMember (FcStrSet
*set
, const FcChar8
*s
)
939 for (i
= 0; i
< set
->num
; i
++)
940 if (!FcStrCmp (set
->strs
[i
], s
))
946 FcStrSetEqual (FcStrSet
*sa
, FcStrSet
*sb
)
949 if (sa
->num
!= sb
->num
)
951 for (i
= 0; i
< sa
->num
; i
++)
952 if (!FcStrSetMember (sb
, sa
->strs
[i
]))
958 FcStrSetAdd (FcStrSet
*set
, const FcChar8
*s
)
960 FcChar8
*new = FcStrCopy (s
);
963 if (!_FcStrSetAppend (set
, new))
972 FcStrSetAddFilename (FcStrSet
*set
, const FcChar8
*s
)
974 FcChar8
*new = FcStrCopyFilename (s
);
977 if (!_FcStrSetAppend (set
, new))
986 FcStrSetDel (FcStrSet
*set
, const FcChar8
*s
)
990 for (i
= 0; i
< set
->num
; i
++)
991 if (!FcStrCmp (set
->strs
[i
], s
))
993 FcStrFree (set
->strs
[i
]);
995 * copy remaining string pointers and trailing
998 memmove (&set
->strs
[i
], &set
->strs
[i
+1],
999 (set
->num
- i
) * sizeof (FcChar8
*));
1007 FcStrSetDestroy (FcStrSet
*set
)
1009 if (--set
->ref
== 0)
1013 for (i
= 0; i
< set
->num
; i
++)
1014 FcStrFree (set
->strs
[i
]);
1015 FcMemFree (FC_MEM_STRSET
, (set
->size
) * sizeof (FcChar8
*));
1018 FcMemFree (FC_MEM_STRSET
, sizeof (FcStrSet
));
1024 FcStrListCreate (FcStrSet
*set
)
1028 list
= malloc (sizeof (FcStrList
));
1031 FcMemAlloc (FC_MEM_STRLIST
, sizeof (FcStrList
));
1039 FcStrListNext (FcStrList
*list
)
1041 if (list
->n
>= list
->set
->num
)
1043 return list
->set
->strs
[list
->n
++];
1047 FcStrListDone (FcStrList
*list
)
1049 FcStrSetDestroy (list
->set
);
1050 FcMemFree (FC_MEM_STRLIST
, sizeof (FcStrList
));