]>
git.wh0rd.org - fontconfig.git/blob - src/fcstr.c
2 * fontconfig/src/fcstr.c
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 the author(s) not be used in
11 * advertising or publicity pertaining to distribution of the software without
12 * specific, written prior permission. The authors make no
13 * representations about the suitability of this software for any purpose. It
14 * is provided "as is" without express or implied warranty.
16 * THE AUTHOR(S) DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
17 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
18 * EVENT SHALL THE AUTHOR(S) 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 s1l
= strlen ((char *) s1
);
54 int s2l
= strlen ((char *) s2
);
55 int l
= s1l
+ s2l
+ 1;
56 FcChar8
*s
= malloc (l
);
60 FcMemAlloc (FC_MEM_STRING
, l
);
62 memcpy (s
+ s1l
, s2
, s2l
+ 1);
67 FcStrFree (FcChar8
*s
)
69 FcMemFree (FC_MEM_STRING
, strlen ((char *) s
) + 1);
74 #include "../fc-case/fccase.h"
76 #define FcCaseFoldUpperCount(cf) \
77 ((cf)->method == FC_CASE_FOLD_FULL ? 1 : (cf)->count)
79 #define FC_STR_CANON_BUF_LEN 1024
81 typedef struct _FcCaseWalker
{
84 FcChar8 utf8
[FC_MAX_CASE_FOLD_CHARS
+ 1];
88 FcStrCaseWalkerInit (const FcChar8
*src
, FcCaseWalker
*w
)
95 FcStrCaseWalkerLong (FcCaseWalker
*w
, FcChar8 r
)
99 int len
= strlen((char*)w
->src
);
101 slen
= FcUtf8ToUcs4 (w
->src
- 1, &ucs4
, len
+ 1);
104 if (FC_MIN_FOLD_CHAR
<= ucs4
&& ucs4
<= FC_MAX_FOLD_CHAR
)
107 int max
= FC_NUM_CASE_FOLD
;
111 int mid
= (min
+ max
) >> 1;
112 FcChar32 low
= fcCaseFold
[mid
].upper
;
113 FcChar32 high
= low
+ FcCaseFoldUpperCount (&fcCaseFold
[mid
]);
121 const FcCaseFold
*fold
= &fcCaseFold
[mid
];
124 switch (fold
->method
) {
125 case FC_CASE_FOLD_EVEN_ODD
:
126 if ((ucs4
& 1) != (fold
->upper
& 1))
128 /* fall through ... */
130 dlen
= FcUcs4ToUtf8 (ucs4
+ fold
->offset
, w
->utf8
);
132 case FC_CASE_FOLD_FULL
:
134 memcpy (w
->utf8
, fcCaseFoldChars
+ fold
->offset
, dlen
);
138 /* consume rest of src utf-8 bytes */
141 /* read from temp buffer */
142 w
->utf8
[dlen
] = '\0';
152 FcStrCaseWalkerNext (FcCaseWalker
*w
)
158 if ((r
= *w
->read
++))
164 if ((r
& 0xc0) == 0xc0)
165 return FcStrCaseWalkerLong (w
, r
);
166 if ('A' <= r
&& r
<= 'Z')
172 FcStrCaseWalkerNextIgnoreBlanks (FcCaseWalker
*w
)
178 if ((r
= *w
->read
++))
187 if ((r
& 0xc0) == 0xc0)
188 return FcStrCaseWalkerLong (w
, r
);
189 if ('A' <= r
&& r
<= 'Z')
195 FcStrDowncase (const FcChar8
*s
)
201 FcStrCaseWalkerInit (s
, &w
);
202 while (FcStrCaseWalkerNext (&w
))
204 d
= dst
= malloc (len
+ 1);
207 FcMemAlloc (FC_MEM_STRING
, len
+ 1);
208 FcStrCaseWalkerInit (s
, &w
);
209 while ((*d
++ = FcStrCaseWalkerNext (&w
)));
214 FcStrCmpIgnoreCase (const FcChar8
*s1
, const FcChar8
*s2
)
219 if (s1
== s2
) return 0;
221 FcStrCaseWalkerInit (s1
, &w1
);
222 FcStrCaseWalkerInit (s2
, &w2
);
226 c1
= FcStrCaseWalkerNext (&w1
);
227 c2
= FcStrCaseWalkerNext (&w2
);
228 if (!c1
|| (c1
!= c2
))
231 return (int) c1
- (int) c2
;
235 FcStrCmpIgnoreBlanksAndCase (const FcChar8
*s1
, const FcChar8
*s2
)
240 if (s1
== s2
) return 0;
242 FcStrCaseWalkerInit (s1
, &w1
);
243 FcStrCaseWalkerInit (s2
, &w2
);
247 c1
= FcStrCaseWalkerNextIgnoreBlanks (&w1
);
248 c2
= FcStrCaseWalkerNextIgnoreBlanks (&w2
);
249 if (!c1
|| (c1
!= c2
))
252 return (int) c1
- (int) c2
;
256 FcStrCmp (const FcChar8
*s1
, const FcChar8
*s2
)
269 return (int) c1
- (int) c2
;
273 * Return a hash value for a string
277 FcStrHashIgnoreCase (const FcChar8
*s
)
283 FcStrCaseWalkerInit (s
, &w
);
284 while ((c
= FcStrCaseWalkerNext (&w
)))
285 h
= ((h
<< 3) ^ (h
>> 3)) ^ c
;
290 * Is the head of s1 equal to s2?
294 FcStrIsAtIgnoreBlanksAndCase (const FcChar8
*s1
, const FcChar8
*s2
)
299 FcStrCaseWalkerInit (s1
, &w1
);
300 FcStrCaseWalkerInit (s2
, &w2
);
304 c1
= FcStrCaseWalkerNextIgnoreBlanks (&w1
);
305 c2
= FcStrCaseWalkerNextIgnoreBlanks (&w2
);
306 if (!c1
|| (c1
!= c2
))
309 return c1
== c2
|| !c2
;
313 * Does s1 contain an instance of s2 (ignoring blanks and case)?
317 FcStrContainsIgnoreBlanksAndCase (const FcChar8
*s1
, const FcChar8
*s2
)
321 if (FcStrIsAtIgnoreBlanksAndCase (s1
, s2
))
329 FcCharIsPunct (const FcChar8 c
)
349 * Is the head of s1 equal to s2?
353 FcStrIsAtIgnoreCase (const FcChar8
*s1
, const FcChar8
*s2
)
358 FcStrCaseWalkerInit (s1
, &w1
);
359 FcStrCaseWalkerInit (s2
, &w2
);
363 c1
= FcStrCaseWalkerNext (&w1
);
364 c2
= FcStrCaseWalkerNext (&w2
);
365 if (!c1
|| (c1
!= c2
))
368 return c1
== c2
|| !c2
;
372 * Does s1 contain an instance of s2 (ignoring blanks and case)?
376 FcStrContainsIgnoreCase (const FcChar8
*s1
, const FcChar8
*s2
)
380 if (FcStrIsAtIgnoreCase (s1
, s2
))
388 * Does s1 contain an instance of s2 on a word boundary (ignoring case)?
392 FcStrContainsWord (const FcChar8
*s1
, const FcChar8
*s2
)
394 FcBool wordStart
= FcTrue
;
395 int s1len
= strlen ((char *) s1
);
396 int s2len
= strlen ((char *) s2
);
398 while (s1len
>= s2len
)
401 FcStrIsAtIgnoreCase (s1
, s2
) &&
402 (s1len
== s2len
|| FcCharIsPunct (s1
[s2len
])))
407 if (FcCharIsPunct (*s1
))
416 FcStrStrIgnoreCase (const FcChar8
*s1
, const FcChar8
*s2
)
428 FcStrCaseWalkerInit (s1
, &w1
);
429 FcStrCaseWalkerInit (s2
, &w2
);
431 c2
= FcStrCaseWalkerNext (&w2
);
436 c1
= FcStrCaseWalkerNext (&w1
);
441 FcCaseWalker w1t
= w1
;
442 FcCaseWalker w2t
= w2
;
447 c1t
= FcStrCaseWalkerNext (&w1t
);
448 c2t
= FcStrCaseWalkerNext (&w2t
);
461 FcStrStr (const FcChar8
*s1
, const FcChar8
*s2
)
464 const FcChar8
* p
= s1
;
465 const FcChar8
* b
= s2
;
494 if (c1
&& c2
&& c1
!= c2
)
511 FcUtf8ToUcs4 (const FcChar8
*src_orig
,
515 const FcChar8
*src
= src_orig
;
531 else if (!(s
& 0x40))
535 else if (!(s
& 0x20))
540 else if (!(s
& 0x10))
545 else if (!(s
& 0x08))
550 else if (!(s
& 0x04))
555 else if ( ! (s
& 0x02))
572 if ((s
& 0xc0) != 0x80)
578 return src
- src_orig
;
582 FcUtf8Len (const FcChar8
*string
,
596 clen
= FcUtf8ToUcs4 (string
, &c
, len
);
597 if (clen
<= 0) /* malformed UTF8 string */
608 else if (max
> 0x100)
616 FcUcs4ToUtf8 (FcChar32 ucs4
,
617 FcChar8 dest
[FC_UTF8_MAX_LEN
])
622 if (ucs4
< 0x80) { *d
++= ucs4
; bits
= -6; }
623 else if (ucs4
< 0x800) { *d
++= ((ucs4
>> 6) & 0x1F) | 0xC0; bits
= 0; }
624 else if (ucs4
< 0x10000) { *d
++= ((ucs4
>> 12) & 0x0F) | 0xE0; bits
= 6; }
625 else if (ucs4
< 0x200000) { *d
++= ((ucs4
>> 18) & 0x07) | 0xF0; bits
= 12; }
626 else if (ucs4
< 0x4000000) { *d
++= ((ucs4
>> 24) & 0x03) | 0xF8; bits
= 18; }
627 else if (ucs4
< 0x80000000) { *d
++= ((ucs4
>> 30) & 0x01) | 0xFC; bits
= 24; }
630 for ( ; bits
>= 0; bits
-= 6) {
631 *d
++= ((ucs4
>> bits
) & 0x3F) | 0x80;
636 #define GetUtf16(src,endian) \
637 ((FcChar16) ((src)[endian == FcEndianBig ? 0 : 1] << 8) | \
638 (FcChar16) ((src)[endian == FcEndianBig ? 1 : 0]))
641 FcUtf16ToUcs4 (const FcChar8
*src_orig
,
644 int len
) /* in bytes */
646 const FcChar8
*src
= src_orig
;
653 a
= GetUtf16 (src
, endian
); src
+= 2; len
-= 2;
656 * Check for surrogate
658 if ((a
& 0xfc00) == 0xd800)
662 b
= GetUtf16 (src
, endian
); src
+= 2; len
-= 2;
664 * Check for invalid surrogate sequence
666 if ((b
& 0xfc00) != 0xdc00)
668 result
= ((((FcChar32
) a
& 0x3ff) << 10) |
669 ((FcChar32
) b
& 0x3ff)) + 0x10000;
674 return src
- src_orig
;
678 FcUtf16Len (const FcChar8
*string
,
680 int len
, /* in bytes */
693 clen
= FcUtf16ToUcs4 (string
, endian
, &c
, len
);
694 if (clen
<= 0) /* malformed UTF8 string */
705 else if (max
> 0x100)
713 FcStrBufInit (FcStrBuf
*buf
, FcChar8
*init
, int size
)
721 buf
->buf
= buf
->buf_static
;
722 buf
->size
= sizeof (buf
->buf_static
);
724 buf
->allocated
= FcFalse
;
725 buf
->failed
= FcFalse
;
730 FcStrBufDestroy (FcStrBuf
*buf
)
734 FcMemFree (FC_MEM_STRBUF
, buf
->size
);
736 FcStrBufInit (buf
, 0, 0);
741 FcStrBufDone (FcStrBuf
*buf
)
748 ret
= malloc (buf
->len
+ 1);
751 FcMemAlloc (FC_MEM_STRING
, buf
->len
+ 1);
752 memcpy (ret
, buf
->buf
, buf
->len
);
753 ret
[buf
->len
] = '\0';
755 FcStrBufDestroy (buf
);
760 FcStrBufDoneStatic (FcStrBuf
*buf
)
762 FcStrBufChar (buf
, '\0');
771 FcStrBufChar (FcStrBuf
*buf
, FcChar8 c
)
773 if (buf
->len
== buf
->size
)
783 size
= buf
->size
* 2;
784 new = realloc (buf
->buf
, size
);
788 size
= buf
->size
+ 64;
792 buf
->allocated
= FcTrue
;
793 memcpy (new, buf
->buf
, buf
->len
);
798 buf
->failed
= FcTrue
;
802 FcMemFree (FC_MEM_STRBUF
, buf
->size
);
803 FcMemAlloc (FC_MEM_STRBUF
, size
);
807 buf
->buf
[buf
->len
++] = c
;
812 FcStrBufString (FcStrBuf
*buf
, const FcChar8
*s
)
816 if (!FcStrBufChar (buf
, c
))
822 FcStrBufData (FcStrBuf
*buf
, const FcChar8
*s
, int len
)
825 if (!FcStrBufChar (buf
, *s
++))
831 FcStrUsesHome (const FcChar8
*s
)
837 FcStrCopyFilename (const FcChar8
*s
)
843 FcChar8
*home
= FcConfigHome ();
848 size
= strlen ((char *) home
) + strlen ((char *) s
);
849 full
= (FcChar8
*) malloc (size
);
852 strcpy ((char *) full
, (char *) home
);
853 strcat ((char *) full
, (char *) s
+ 1);
854 new = FcStrCanonFilename (full
);
858 new = FcStrCanonFilename (s
);
863 FcStrLastSlash (const FcChar8
*path
)
867 slash
= (FcChar8
*) strrchr ((const char *) path
, '/');
872 backslash
= (FcChar8
*) strrchr ((const char *) path
, '\\');
873 if (!slash
|| (backslash
&& backslash
> slash
))
882 FcStrDirname (const FcChar8
*file
)
887 slash
= FcStrLastSlash (file
);
889 return FcStrCopy ((FcChar8
*) ".");
890 dir
= malloc ((slash
- file
) + 1);
893 FcMemAlloc (FC_MEM_STRING
, (slash
- file
) + 1);
894 strncpy ((char *) dir
, (const char *) file
, slash
- file
);
895 dir
[slash
- file
] = '\0';
900 FcStrBasename (const FcChar8
*file
)
904 slash
= FcStrLastSlash (file
);
906 return FcStrCopy (file
);
907 return FcStrCopy (slash
+ 1);
911 FcStrCanonAbsoluteFilename (const FcChar8
*s
)
915 const FcChar8
*slash
;
918 size
= strlen ((char *) s
) + 1;
919 file
= malloc (size
);
922 FcMemAlloc (FC_MEM_STRING
, size
);
926 if (*s
== '/' || *s
== '\0')
932 f
-= 1; /* squash // and trim final / from file */
935 if (!strncmp ((char *) slash
, "/.", 2))
937 f
-= 2; /* trim /. from file */
941 if (!strncmp ((char *) slash
, "/..", 3))
943 f
-= 3; /* trim /.. from file */
962 * Convert '\\' to '/' , remove double '/'
965 FcConvertDosPath (char *str
)
967 size_t len
= strlen (str
);
970 char *end
= str
+ len
;
999 FcStrCanonFilename (const FcChar8
*s
)
1002 FcChar8 full
[FC_MAX_FILE_LEN
+ 2];
1003 int size
= GetFullPathName (s
, sizeof (full
) -1,
1007 perror ("GetFullPathName");
1009 FcConvertDosPath (full
);
1010 return FcStrCanonAbsoluteFilename (full
);
1013 return FcStrCanonAbsoluteFilename (s
);
1019 FcChar8 cwd
[FC_MAX_FILE_LEN
+ 2];
1020 if (getcwd ((char *) cwd
, FC_MAX_FILE_LEN
) == NULL
)
1022 strcat ((char *) cwd
, "/");
1023 full
= FcStrPlus (cwd
, s
);
1024 file
= FcStrCanonAbsoluteFilename (full
);
1033 FcStrSetCreate (void)
1035 FcStrSet
*set
= malloc (sizeof (FcStrSet
));
1038 FcMemAlloc (FC_MEM_STRSET
, sizeof (FcStrSet
));
1047 _FcStrSetAppend (FcStrSet
*set
, FcChar8
*s
)
1049 if (FcStrSetMember (set
, s
))
1054 if (set
->num
== set
->size
)
1056 FcChar8
**strs
= malloc ((set
->size
+ 2) * sizeof (FcChar8
*));
1060 FcMemAlloc (FC_MEM_STRSET
, (set
->size
+ 2) * sizeof (FcChar8
*));
1062 memcpy (strs
, set
->strs
, set
->num
* sizeof (FcChar8
*));
1065 FcMemFree (FC_MEM_STRSET
, (set
->size
+ 1) * sizeof (FcChar8
*));
1068 set
->size
= set
->size
+ 1;
1071 set
->strs
[set
->num
++] = s
;
1072 set
->strs
[set
->num
] = 0;
1077 FcStrSetMember (FcStrSet
*set
, const FcChar8
*s
)
1081 for (i
= 0; i
< set
->num
; i
++)
1082 if (!FcStrCmp (set
->strs
[i
], s
))
1088 FcStrSetEqual (FcStrSet
*sa
, FcStrSet
*sb
)
1091 if (sa
->num
!= sb
->num
)
1093 for (i
= 0; i
< sa
->num
; i
++)
1094 if (!FcStrSetMember (sb
, sa
->strs
[i
]))
1100 FcStrSetAdd (FcStrSet
*set
, const FcChar8
*s
)
1102 FcChar8
*new = FcStrCopy (s
);
1105 if (!_FcStrSetAppend (set
, new))
1114 FcStrSetAddFilename (FcStrSet
*set
, const FcChar8
*s
)
1116 FcChar8
*new = FcStrCopyFilename (s
);
1119 if (!_FcStrSetAppend (set
, new))
1128 FcStrSetDel (FcStrSet
*set
, const FcChar8
*s
)
1132 for (i
= 0; i
< set
->num
; i
++)
1133 if (!FcStrCmp (set
->strs
[i
], s
))
1135 FcStrFree (set
->strs
[i
]);
1137 * copy remaining string pointers and trailing
1140 memmove (&set
->strs
[i
], &set
->strs
[i
+1],
1141 (set
->num
- i
) * sizeof (FcChar8
*));
1149 FcStrSetDestroy (FcStrSet
*set
)
1151 if (--set
->ref
== 0)
1155 for (i
= 0; i
< set
->num
; i
++)
1156 FcStrFree (set
->strs
[i
]);
1159 FcMemFree (FC_MEM_STRSET
, (set
->size
+ 1) * sizeof (FcChar8
*));
1162 FcMemFree (FC_MEM_STRSET
, sizeof (FcStrSet
));
1168 FcStrListCreate (FcStrSet
*set
)
1172 list
= malloc (sizeof (FcStrList
));
1175 FcMemAlloc (FC_MEM_STRLIST
, sizeof (FcStrList
));
1183 FcStrListNext (FcStrList
*list
)
1185 if (list
->n
>= list
->set
->num
)
1187 return list
->set
->strs
[list
->n
++];
1191 FcStrListDone (FcStrList
*list
)
1193 FcStrSetDestroy (list
->set
);
1194 FcMemFree (FC_MEM_STRLIST
, sizeof (FcStrList
));
1199 #include "fcaliastail.h"