]>
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 FcStrPathPlus (const FcChar8
*s1
, ...)
83 al
= strlen ((char *) arg
);
85 /* make sure there's a single separator */
88 if ((!arg
[0] || (arg
[al
- 1] != '/' && arg
[al
- 1] != '\\')) &&
91 (isalpha (file
[0]) && file
[1] == ':' && (file
[2] == '/' || file
[2] == '\\'))))
94 if (s
&& (s
[l
] != FC_DIR_SEPARATOR
&& arg
[0] != FC_DIR_SEPARATOR
))
100 s
= realloc (s
, l
+ al
+ 1);
104 s
[l
- 1] = FC_DIR_SEPARATOR
;
105 memcpy (s
+ l
, arg
, al
+ 1);
108 arg
= va_arg (ap
, const FcChar8
*);
113 FcMemAlloc (FC_MEM_STRING
, l
+ 1);
118 FcStrFree (FcChar8
*s
)
120 FcMemFree (FC_MEM_STRING
, strlen ((char *) s
) + 1);
125 #include "../fc-case/fccase.h"
127 #define FcCaseFoldUpperCount(cf) \
128 ((cf)->method == FC_CASE_FOLD_FULL ? 1 : (cf)->count)
130 #define FC_STR_CANON_BUF_LEN 1024
132 typedef struct _FcCaseWalker
{
135 FcChar8 utf8
[FC_MAX_CASE_FOLD_CHARS
+ 1];
139 FcStrCaseWalkerInit (const FcChar8
*src
, FcCaseWalker
*w
)
146 FcStrCaseWalkerLong (FcCaseWalker
*w
, FcChar8 r
)
150 int len
= strlen((char*)w
->src
);
152 slen
= FcUtf8ToUcs4 (w
->src
- 1, &ucs4
, len
+ 1);
155 if (FC_MIN_FOLD_CHAR
<= ucs4
&& ucs4
<= FC_MAX_FOLD_CHAR
)
158 int max
= FC_NUM_CASE_FOLD
;
162 int mid
= (min
+ max
) >> 1;
163 FcChar32 low
= fcCaseFold
[mid
].upper
;
164 FcChar32 high
= low
+ FcCaseFoldUpperCount (&fcCaseFold
[mid
]);
172 const FcCaseFold
*fold
= &fcCaseFold
[mid
];
175 switch (fold
->method
) {
176 case FC_CASE_FOLD_EVEN_ODD
:
177 if ((ucs4
& 1) != (fold
->upper
& 1))
179 /* fall through ... */
181 dlen
= FcUcs4ToUtf8 (ucs4
+ fold
->offset
, w
->utf8
);
183 case FC_CASE_FOLD_FULL
:
185 memcpy (w
->utf8
, fcCaseFoldChars
+ fold
->offset
, dlen
);
189 /* consume rest of src utf-8 bytes */
192 /* read from temp buffer */
193 w
->utf8
[dlen
] = '\0';
203 FcStrCaseWalkerNext (FcCaseWalker
*w
)
209 if ((r
= *w
->read
++))
215 if ((r
& 0xc0) == 0xc0)
216 return FcStrCaseWalkerLong (w
, r
);
217 if ('A' <= r
&& r
<= 'Z')
223 FcStrCaseWalkerNextIgnoreBlanks (FcCaseWalker
*w
)
229 if ((r
= *w
->read
++))
238 if ((r
& 0xc0) == 0xc0)
239 return FcStrCaseWalkerLong (w
, r
);
240 if ('A' <= r
&& r
<= 'Z')
246 FcStrDowncase (const FcChar8
*s
)
252 FcStrCaseWalkerInit (s
, &w
);
253 while (FcStrCaseWalkerNext (&w
))
255 d
= dst
= malloc (len
+ 1);
258 FcMemAlloc (FC_MEM_STRING
, len
+ 1);
259 FcStrCaseWalkerInit (s
, &w
);
260 while ((*d
++ = FcStrCaseWalkerNext (&w
)));
265 FcStrCmpIgnoreCase (const FcChar8
*s1
, const FcChar8
*s2
)
270 if (s1
== s2
) return 0;
272 FcStrCaseWalkerInit (s1
, &w1
);
273 FcStrCaseWalkerInit (s2
, &w2
);
277 c1
= FcStrCaseWalkerNext (&w1
);
278 c2
= FcStrCaseWalkerNext (&w2
);
279 if (!c1
|| (c1
!= c2
))
282 return (int) c1
- (int) c2
;
286 FcStrCmpIgnoreBlanksAndCase (const FcChar8
*s1
, const FcChar8
*s2
)
291 if (s1
== s2
) return 0;
293 FcStrCaseWalkerInit (s1
, &w1
);
294 FcStrCaseWalkerInit (s2
, &w2
);
298 c1
= FcStrCaseWalkerNextIgnoreBlanks (&w1
);
299 c2
= FcStrCaseWalkerNextIgnoreBlanks (&w2
);
300 if (!c1
|| (c1
!= c2
))
303 return (int) c1
- (int) c2
;
307 FcStrCmp (const FcChar8
*s1
, const FcChar8
*s2
)
320 return (int) c1
- (int) c2
;
324 * Return a hash value for a string
328 FcStrHashIgnoreCase (const FcChar8
*s
)
334 FcStrCaseWalkerInit (s
, &w
);
335 while ((c
= FcStrCaseWalkerNext (&w
)))
336 h
= ((h
<< 3) ^ (h
>> 3)) ^ c
;
341 * Is the head of s1 equal to s2?
345 FcStrIsAtIgnoreBlanksAndCase (const FcChar8
*s1
, const FcChar8
*s2
)
350 FcStrCaseWalkerInit (s1
, &w1
);
351 FcStrCaseWalkerInit (s2
, &w2
);
355 c1
= FcStrCaseWalkerNextIgnoreBlanks (&w1
);
356 c2
= FcStrCaseWalkerNextIgnoreBlanks (&w2
);
357 if (!c1
|| (c1
!= c2
))
360 return c1
== c2
|| !c2
;
364 * Does s1 contain an instance of s2 (ignoring blanks and case)?
368 FcStrContainsIgnoreBlanksAndCase (const FcChar8
*s1
, const FcChar8
*s2
)
372 if (FcStrIsAtIgnoreBlanksAndCase (s1
, s2
))
380 FcCharIsPunct (const FcChar8 c
)
400 * Is the head of s1 equal to s2?
404 FcStrIsAtIgnoreCase (const FcChar8
*s1
, const FcChar8
*s2
)
409 FcStrCaseWalkerInit (s1
, &w1
);
410 FcStrCaseWalkerInit (s2
, &w2
);
414 c1
= FcStrCaseWalkerNext (&w1
);
415 c2
= FcStrCaseWalkerNext (&w2
);
416 if (!c1
|| (c1
!= c2
))
419 return c1
== c2
|| !c2
;
423 * Does s1 contain an instance of s2 (ignoring blanks and case)?
427 FcStrContainsIgnoreCase (const FcChar8
*s1
, const FcChar8
*s2
)
431 if (FcStrIsAtIgnoreCase (s1
, s2
))
439 * Does s1 contain an instance of s2 on a word boundary (ignoring case)?
443 FcStrContainsWord (const FcChar8
*s1
, const FcChar8
*s2
)
445 FcBool wordStart
= FcTrue
;
446 int s1len
= strlen ((char *) s1
);
447 int s2len
= strlen ((char *) s2
);
449 while (s1len
>= s2len
)
452 FcStrIsAtIgnoreCase (s1
, s2
) &&
453 (s1len
== s2len
|| FcCharIsPunct (s1
[s2len
])))
458 if (FcCharIsPunct (*s1
))
467 FcStrStrIgnoreCase (const FcChar8
*s1
, const FcChar8
*s2
)
479 FcStrCaseWalkerInit (s1
, &w1
);
480 FcStrCaseWalkerInit (s2
, &w2
);
482 c2
= FcStrCaseWalkerNext (&w2
);
487 c1
= FcStrCaseWalkerNext (&w1
);
492 FcCaseWalker w1t
= w1
;
493 FcCaseWalker w2t
= w2
;
498 c1t
= FcStrCaseWalkerNext (&w1t
);
499 c2t
= FcStrCaseWalkerNext (&w2t
);
512 FcStrStr (const FcChar8
*s1
, const FcChar8
*s2
)
515 const FcChar8
* p
= s1
;
516 const FcChar8
* b
= s2
;
545 if (c1
&& c2
&& c1
!= c2
)
562 FcUtf8ToUcs4 (const FcChar8
*src_orig
,
566 const FcChar8
*src
= src_orig
;
582 else if (!(s
& 0x40))
586 else if (!(s
& 0x20))
591 else if (!(s
& 0x10))
596 else if (!(s
& 0x08))
601 else if (!(s
& 0x04))
606 else if ( ! (s
& 0x02))
623 if ((s
& 0xc0) != 0x80)
629 return src
- src_orig
;
633 FcUtf8Len (const FcChar8
*string
,
647 clen
= FcUtf8ToUcs4 (string
, &c
, len
);
648 if (clen
<= 0) /* malformed UTF8 string */
659 else if (max
> 0x100)
667 FcUcs4ToUtf8 (FcChar32 ucs4
,
668 FcChar8 dest
[FC_UTF8_MAX_LEN
])
673 if (ucs4
< 0x80) { *d
++= ucs4
; bits
= -6; }
674 else if (ucs4
< 0x800) { *d
++= ((ucs4
>> 6) & 0x1F) | 0xC0; bits
= 0; }
675 else if (ucs4
< 0x10000) { *d
++= ((ucs4
>> 12) & 0x0F) | 0xE0; bits
= 6; }
676 else if (ucs4
< 0x200000) { *d
++= ((ucs4
>> 18) & 0x07) | 0xF0; bits
= 12; }
677 else if (ucs4
< 0x4000000) { *d
++= ((ucs4
>> 24) & 0x03) | 0xF8; bits
= 18; }
678 else if (ucs4
< 0x80000000) { *d
++= ((ucs4
>> 30) & 0x01) | 0xFC; bits
= 24; }
681 for ( ; bits
>= 0; bits
-= 6) {
682 *d
++= ((ucs4
>> bits
) & 0x3F) | 0x80;
687 #define GetUtf16(src,endian) \
688 ((FcChar16) ((src)[endian == FcEndianBig ? 0 : 1] << 8) | \
689 (FcChar16) ((src)[endian == FcEndianBig ? 1 : 0]))
692 FcUtf16ToUcs4 (const FcChar8
*src_orig
,
695 int len
) /* in bytes */
697 const FcChar8
*src
= src_orig
;
704 a
= GetUtf16 (src
, endian
); src
+= 2; len
-= 2;
707 * Check for surrogate
709 if ((a
& 0xfc00) == 0xd800)
713 b
= GetUtf16 (src
, endian
); src
+= 2; len
-= 2;
715 * Check for invalid surrogate sequence
717 if ((b
& 0xfc00) != 0xdc00)
719 result
= ((((FcChar32
) a
& 0x3ff) << 10) |
720 ((FcChar32
) b
& 0x3ff)) + 0x10000;
725 return src
- src_orig
;
729 FcUtf16Len (const FcChar8
*string
,
731 int len
, /* in bytes */
744 clen
= FcUtf16ToUcs4 (string
, endian
, &c
, len
);
745 if (clen
<= 0) /* malformed UTF8 string */
756 else if (max
> 0x100)
764 FcStrBufInit (FcStrBuf
*buf
, FcChar8
*init
, int size
)
772 buf
->buf
= buf
->buf_static
;
773 buf
->size
= sizeof (buf
->buf_static
);
775 buf
->allocated
= FcFalse
;
776 buf
->failed
= FcFalse
;
781 FcStrBufDestroy (FcStrBuf
*buf
)
785 FcMemFree (FC_MEM_STRBUF
, buf
->size
);
787 FcStrBufInit (buf
, 0, 0);
792 FcStrBufDone (FcStrBuf
*buf
)
799 ret
= malloc (buf
->len
+ 1);
802 FcMemAlloc (FC_MEM_STRING
, buf
->len
+ 1);
803 memcpy (ret
, buf
->buf
, buf
->len
);
804 ret
[buf
->len
] = '\0';
806 FcStrBufDestroy (buf
);
811 FcStrBufDoneStatic (FcStrBuf
*buf
)
813 FcStrBufChar (buf
, '\0');
822 FcStrBufChar (FcStrBuf
*buf
, FcChar8 c
)
824 if (buf
->len
== buf
->size
)
834 size
= buf
->size
* 2;
835 new = realloc (buf
->buf
, size
);
839 size
= buf
->size
+ 64;
843 buf
->allocated
= FcTrue
;
844 memcpy (new, buf
->buf
, buf
->len
);
849 buf
->failed
= FcTrue
;
853 FcMemFree (FC_MEM_STRBUF
, buf
->size
);
854 FcMemAlloc (FC_MEM_STRBUF
, size
);
858 buf
->buf
[buf
->len
++] = c
;
863 FcStrBufString (FcStrBuf
*buf
, const FcChar8
*s
)
867 if (!FcStrBufChar (buf
, c
))
873 FcStrBufData (FcStrBuf
*buf
, const FcChar8
*s
, int len
)
876 if (!FcStrBufChar (buf
, *s
++))
882 FcStrUsesHome (const FcChar8
*s
)
888 FcStrCopyFilename (const FcChar8
*s
)
894 FcChar8
*home
= FcConfigHome ();
899 size
= strlen ((char *) home
) + strlen ((char *) s
);
900 full
= (FcChar8
*) malloc (size
);
903 strcpy ((char *) full
, (char *) home
);
904 strcat ((char *) full
, (char *) s
+ 1);
905 new = FcStrCanonFilename (full
);
909 new = FcStrCanonFilename (s
);
914 FcStrLastSlash (const FcChar8
*path
)
918 slash
= (FcChar8
*) strrchr ((const char *) path
, '/');
923 backslash
= (FcChar8
*) strrchr ((const char *) path
, '\\');
924 if (!slash
|| (backslash
&& backslash
> slash
))
933 FcStrDirname (const FcChar8
*file
)
938 slash
= FcStrLastSlash (file
);
940 return FcStrCopy ((FcChar8
*) ".");
941 dir
= malloc ((slash
- file
) + 1);
944 FcMemAlloc (FC_MEM_STRING
, (slash
- file
) + 1);
945 strncpy ((char *) dir
, (const char *) file
, slash
- file
);
946 dir
[slash
- file
] = '\0';
951 FcStrBasename (const FcChar8
*file
)
955 slash
= FcStrLastSlash (file
);
957 return FcStrCopy (file
);
958 return FcStrCopy (slash
+ 1);
962 FcStrCanonAbsoluteFilename (const FcChar8
*s
)
966 const FcChar8
*slash
;
969 size
= strlen ((char *) s
) + 1;
970 file
= malloc (size
);
973 FcMemAlloc (FC_MEM_STRING
, size
);
977 if (*s
== '/' || *s
== '\0')
983 f
-= 1; /* squash // and trim final / from file */
986 if (!strncmp ((char *) slash
, "/.", 2))
988 f
-= 2; /* trim /. from file */
992 if (!strncmp ((char *) slash
, "/..", 3))
994 f
-= 3; /* trim /.. from file */
1013 * Convert '\\' to '/' , remove double '/'
1016 FcConvertDosPath (char *str
)
1018 size_t len
= strlen (str
);
1021 char *end
= str
+ len
;
1050 FcStrCanonFilename (const FcChar8
*s
)
1053 FcChar8 full
[FC_MAX_FILE_LEN
+ 2];
1054 int size
= GetFullPathName (s
, sizeof (full
) -1,
1058 perror ("GetFullPathName");
1060 FcConvertDosPath (full
);
1061 return FcStrCanonAbsoluteFilename (full
);
1064 return FcStrCanonAbsoluteFilename (s
);
1070 FcChar8 cwd
[FC_MAX_FILE_LEN
+ 2];
1071 if (getcwd ((char *) cwd
, FC_MAX_FILE_LEN
) == NULL
)
1073 strcat ((char *) cwd
, "/");
1074 full
= FcStrPlus (cwd
, s
);
1075 file
= FcStrCanonAbsoluteFilename (full
);
1084 FcStrSetCreate (void)
1086 FcStrSet
*set
= malloc (sizeof (FcStrSet
));
1089 FcMemAlloc (FC_MEM_STRSET
, sizeof (FcStrSet
));
1098 _FcStrSetAppend (FcStrSet
*set
, FcChar8
*s
)
1100 if (FcStrSetMember (set
, s
))
1105 if (set
->num
== set
->size
)
1107 FcChar8
**strs
= malloc ((set
->size
+ 2) * sizeof (FcChar8
*));
1111 FcMemAlloc (FC_MEM_STRSET
, (set
->size
+ 2) * sizeof (FcChar8
*));
1113 memcpy (strs
, set
->strs
, set
->num
* sizeof (FcChar8
*));
1116 FcMemFree (FC_MEM_STRSET
, (set
->size
+ 1) * sizeof (FcChar8
*));
1119 set
->size
= set
->size
+ 1;
1122 set
->strs
[set
->num
++] = s
;
1123 set
->strs
[set
->num
] = 0;
1128 FcStrSetMember (FcStrSet
*set
, const FcChar8
*s
)
1132 for (i
= 0; i
< set
->num
; i
++)
1133 if (!FcStrCmp (set
->strs
[i
], s
))
1139 FcStrSetEqual (FcStrSet
*sa
, FcStrSet
*sb
)
1142 if (sa
->num
!= sb
->num
)
1144 for (i
= 0; i
< sa
->num
; i
++)
1145 if (!FcStrSetMember (sb
, sa
->strs
[i
]))
1151 FcStrSetAdd (FcStrSet
*set
, const FcChar8
*s
)
1153 FcChar8
*new = FcStrCopy (s
);
1156 if (!_FcStrSetAppend (set
, new))
1165 FcStrSetAddFilename (FcStrSet
*set
, const FcChar8
*s
)
1167 FcChar8
*new = FcStrCopyFilename (s
);
1170 if (!_FcStrSetAppend (set
, new))
1179 FcStrSetDel (FcStrSet
*set
, const FcChar8
*s
)
1183 for (i
= 0; i
< set
->num
; i
++)
1184 if (!FcStrCmp (set
->strs
[i
], s
))
1186 FcStrFree (set
->strs
[i
]);
1188 * copy remaining string pointers and trailing
1191 memmove (&set
->strs
[i
], &set
->strs
[i
+1],
1192 (set
->num
- i
) * sizeof (FcChar8
*));
1200 FcStrSetDestroy (FcStrSet
*set
)
1202 if (--set
->ref
== 0)
1206 for (i
= 0; i
< set
->num
; i
++)
1207 FcStrFree (set
->strs
[i
]);
1210 FcMemFree (FC_MEM_STRSET
, (set
->size
+ 1) * sizeof (FcChar8
*));
1213 FcMemFree (FC_MEM_STRSET
, sizeof (FcStrSet
));
1219 FcStrListCreate (FcStrSet
*set
)
1223 list
= malloc (sizeof (FcStrList
));
1226 FcMemAlloc (FC_MEM_STRLIST
, sizeof (FcStrList
));
1234 FcStrListNext (FcStrList
*list
)
1236 if (list
->n
>= list
->set
->num
)
1238 return list
->set
->strs
[list
->n
++];
1242 FcStrListDone (FcStrList
*list
)
1244 FcStrSetDestroy (list
->set
);
1245 FcMemFree (FC_MEM_STRLIST
, sizeof (FcStrList
));
1250 #include "fcaliastail.h"