]>
git.wh0rd.org - fontconfig.git/blob - src/fcstr.c
4fbb1a216ea88fd551027b26d25cdea10df0cc17
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 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.
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 l
= strlen ((char *)s1
) + strlen ((char *) s2
) + 1;
54 FcChar8
*s
= malloc (l
);
58 FcMemAlloc (FC_MEM_STRING
, l
);
59 strcpy ((char *) s
, (char *) s1
);
60 strcat ((char *) s
, (char *) s2
);
65 FcStrFree (FcChar8
*s
)
67 FcMemFree (FC_MEM_STRING
, strlen ((char *) s
) + 1);
72 #include "../fc-case/fccase.h"
74 #define FcCaseFoldUpperCount(cf) \
75 ((cf)->method == FC_CASE_FOLD_FULL ? 1 : (cf)->count)
77 #define FC_STR_CANON_BUF_LEN 1024
79 typedef struct _FcCaseWalker
{
82 FcChar8 utf8
[FC_MAX_CASE_FOLD_CHARS
+ 1];
86 FcStrCaseWalkerInit (const FcChar8
*src
, FcCaseWalker
*w
)
93 FcStrCaseWalkerLong (FcCaseWalker
*w
, FcChar8 r
)
97 int len
= strlen((char*)w
->src
);
99 slen
= FcUtf8ToUcs4 (w
->src
- 1, &ucs4
, len
+ 1);
102 if (FC_MIN_FOLD_CHAR
<= ucs4
&& ucs4
<= FC_MAX_FOLD_CHAR
)
105 int max
= FC_NUM_CASE_FOLD
;
109 int mid
= (min
+ max
) >> 1;
110 FcChar32 low
= fcCaseFold
[mid
].upper
;
111 FcChar32 high
= low
+ FcCaseFoldUpperCount (&fcCaseFold
[mid
]);
119 const FcCaseFold
*fold
= &fcCaseFold
[mid
];
122 switch (fold
->method
) {
123 case FC_CASE_FOLD_EVEN_ODD
:
124 if ((ucs4
& 1) != (fold
->upper
& 1))
126 /* fall through ... */
128 dlen
= FcUcs4ToUtf8 (ucs4
+ fold
->offset
, w
->utf8
);
130 case FC_CASE_FOLD_FULL
:
132 memcpy (w
->utf8
, fcCaseFoldChars
+ fold
->offset
, dlen
);
136 /* consume rest of src utf-8 bytes */
139 /* read from temp buffer */
140 w
->utf8
[dlen
] = '\0';
150 FcStrCaseWalkerNext (FcCaseWalker
*w
)
156 if ((r
= *w
->read
++))
162 if ((r
& 0xc0) == 0xc0)
163 return FcStrCaseWalkerLong (w
, r
);
164 if ('A' <= r
&& r
<= 'Z')
170 FcStrCaseWalkerNextIgnoreBlanks (FcCaseWalker
*w
)
176 if ((r
= *w
->read
++))
185 if ((r
& 0xc0) == 0xc0)
186 return FcStrCaseWalkerLong (w
, r
);
187 if ('A' <= r
&& r
<= 'Z')
193 FcStrDowncase (const FcChar8
*s
)
199 FcStrCaseWalkerInit (s
, &w
);
200 while (FcStrCaseWalkerNext (&w
))
202 d
= dst
= malloc (len
+ 1);
205 FcMemAlloc (FC_MEM_STRING
, len
+ 1);
206 FcStrCaseWalkerInit (s
, &w
);
207 while ((*d
++ = FcStrCaseWalkerNext (&w
)));
212 FcStrCmpIgnoreCase (const FcChar8
*s1
, const FcChar8
*s2
)
217 if (s1
== s2
) return 0;
219 FcStrCaseWalkerInit (s1
, &w1
);
220 FcStrCaseWalkerInit (s2
, &w2
);
224 c1
= FcStrCaseWalkerNext (&w1
);
225 c2
= FcStrCaseWalkerNext (&w2
);
226 if (!c1
|| (c1
!= c2
))
229 return (int) c1
- (int) c2
;
233 FcStrCmpIgnoreBlanksAndCase (const FcChar8
*s1
, const FcChar8
*s2
)
238 if (s1
== s2
) return 0;
240 FcStrCaseWalkerInit (s1
, &w1
);
241 FcStrCaseWalkerInit (s2
, &w2
);
245 c1
= FcStrCaseWalkerNextIgnoreBlanks (&w1
);
246 c2
= FcStrCaseWalkerNextIgnoreBlanks (&w2
);
247 if (!c1
|| (c1
!= c2
))
250 return (int) c1
- (int) c2
;
254 FcStrCmp (const FcChar8
*s1
, const FcChar8
*s2
)
267 return (int) c1
- (int) c2
;
271 * Return a hash value for a string
275 FcStrHashIgnoreCase (const FcChar8
*s
)
281 FcStrCaseWalkerInit (s
, &w
);
282 while ((c
= FcStrCaseWalkerNext (&w
)))
283 h
= ((h
<< 3) ^ (h
>> 3)) ^ c
;
288 * Is the head of s1 equal to s2?
292 FcStrIsAtIgnoreBlanksAndCase (const FcChar8
*s1
, const FcChar8
*s2
)
297 FcStrCaseWalkerInit (s1
, &w1
);
298 FcStrCaseWalkerInit (s2
, &w2
);
302 c1
= FcStrCaseWalkerNextIgnoreBlanks (&w1
);
303 c2
= FcStrCaseWalkerNextIgnoreBlanks (&w2
);
304 if (!c1
|| (c1
!= c2
))
307 return c1
== c2
|| !c2
;
311 * Does s1 contain an instance of s2 (ignoring blanks and case)?
315 FcStrContainsIgnoreBlanksAndCase (const FcChar8
*s1
, const FcChar8
*s2
)
319 if (FcStrIsAtIgnoreBlanksAndCase (s1
, s2
))
327 FcCharIsPunct (const FcChar8 c
)
347 * Is the head of s1 equal to s2?
351 FcStrIsAtIgnoreCase (const FcChar8
*s1
, const FcChar8
*s2
)
356 FcStrCaseWalkerInit (s1
, &w1
);
357 FcStrCaseWalkerInit (s2
, &w2
);
361 c1
= FcStrCaseWalkerNext (&w1
);
362 c2
= FcStrCaseWalkerNext (&w2
);
363 if (!c1
|| (c1
!= c2
))
366 return c1
== c2
|| !c2
;
370 * Does s1 contain an instance of s2 (ignoring blanks and case)?
374 FcStrContainsIgnoreCase (const FcChar8
*s1
, const FcChar8
*s2
)
378 if (FcStrIsAtIgnoreCase (s1
, s2
))
386 * Does s1 contain an instance of s2 on a word boundary (ignoring case)?
390 FcStrContainsWord (const FcChar8
*s1
, const FcChar8
*s2
)
392 FcBool wordStart
= FcTrue
;
393 int s1len
= strlen ((char *) s1
);
394 int s2len
= strlen ((char *) s2
);
396 while (s1len
>= s2len
)
399 FcStrIsAtIgnoreCase (s1
, s2
) &&
400 (s1len
== s2len
|| FcCharIsPunct (s1
[s2len
])))
405 if (FcCharIsPunct (*s1
))
414 FcStrStrIgnoreCase (const FcChar8
*s1
, const FcChar8
*s2
)
426 FcStrCaseWalkerInit (s1
, &w1
);
427 FcStrCaseWalkerInit (s2
, &w2
);
429 c2
= FcStrCaseWalkerNext (&w2
);
434 c1
= FcStrCaseWalkerNext (&w1
);
439 FcCaseWalker w1t
= w1
;
440 FcCaseWalker w2t
= w2
;
445 c1t
= FcStrCaseWalkerNext (&w1t
);
446 c2t
= FcStrCaseWalkerNext (&w2t
);
459 FcStrStr (const FcChar8
*s1
, const FcChar8
*s2
)
462 const FcChar8
* p
= s1
;
463 const FcChar8
* b
= s2
;
492 if (c1
&& c2
&& c1
!= c2
)
509 FcUtf8ToUcs4 (const FcChar8
*src_orig
,
513 const FcChar8
*src
= src_orig
;
529 else if (!(s
& 0x40))
533 else if (!(s
& 0x20))
538 else if (!(s
& 0x10))
543 else if (!(s
& 0x08))
548 else if (!(s
& 0x04))
553 else if ( ! (s
& 0x02))
570 if ((s
& 0xc0) != 0x80)
576 return src
- src_orig
;
580 FcUtf8Len (const FcChar8
*string
,
594 clen
= FcUtf8ToUcs4 (string
, &c
, len
);
595 if (clen
<= 0) /* malformed UTF8 string */
606 else if (max
> 0x100)
614 FcUcs4ToUtf8 (FcChar32 ucs4
,
615 FcChar8 dest
[FC_UTF8_MAX_LEN
])
620 if (ucs4
< 0x80) { *d
++= ucs4
; bits
= -6; }
621 else if (ucs4
< 0x800) { *d
++= ((ucs4
>> 6) & 0x1F) | 0xC0; bits
= 0; }
622 else if (ucs4
< 0x10000) { *d
++= ((ucs4
>> 12) & 0x0F) | 0xE0; bits
= 6; }
623 else if (ucs4
< 0x200000) { *d
++= ((ucs4
>> 18) & 0x07) | 0xF0; bits
= 12; }
624 else if (ucs4
< 0x4000000) { *d
++= ((ucs4
>> 24) & 0x03) | 0xF8; bits
= 18; }
625 else if (ucs4
< 0x80000000) { *d
++= ((ucs4
>> 30) & 0x01) | 0xFC; bits
= 24; }
628 for ( ; bits
>= 0; bits
-= 6) {
629 *d
++= ((ucs4
>> bits
) & 0x3F) | 0x80;
634 #define GetUtf16(src,endian) \
635 ((FcChar16) ((src)[endian == FcEndianBig ? 0 : 1] << 8) | \
636 (FcChar16) ((src)[endian == FcEndianBig ? 1 : 0]))
639 FcUtf16ToUcs4 (const FcChar8
*src_orig
,
642 int len
) /* in bytes */
644 const FcChar8
*src
= src_orig
;
651 a
= GetUtf16 (src
, endian
); src
+= 2; len
-= 2;
654 * Check for surrogate
656 if ((a
& 0xfc00) == 0xd800)
660 b
= GetUtf16 (src
, endian
); src
+= 2; len
-= 2;
662 * Check for invalid surrogate sequence
664 if ((b
& 0xfc00) != 0xdc00)
666 result
= ((((FcChar32
) a
& 0x3ff) << 10) |
667 ((FcChar32
) b
& 0x3ff)) + 0x10000;
672 return src
- src_orig
;
676 FcUtf16Len (const FcChar8
*string
,
678 int len
, /* in bytes */
691 clen
= FcUtf16ToUcs4 (string
, endian
, &c
, len
);
692 if (clen
<= 0) /* malformed UTF8 string */
703 else if (max
> 0x100)
711 FcStrBufInit (FcStrBuf
*buf
, FcChar8
*init
, int size
)
719 buf
->buf
= buf
->static_buf
;
720 buf
->size
= sizeof (buf
->static_buf
);
722 buf
->allocated
= FcFalse
;
723 buf
->failed
= FcFalse
;
728 FcStrBufDestroy (FcStrBuf
*buf
)
732 FcMemFree (FC_MEM_STRBUF
, buf
->size
);
734 FcStrBufInit (buf
, 0, 0);
739 FcStrBufDone (FcStrBuf
*buf
)
746 ret
= malloc (buf
->len
+ 1);
749 FcMemAlloc (FC_MEM_STRING
, buf
->len
+ 1);
750 memcpy (ret
, buf
->buf
, buf
->len
);
751 ret
[buf
->len
] = '\0';
753 FcStrBufDestroy (buf
);
758 FcStrBufChar (FcStrBuf
*buf
, FcChar8 c
)
760 if (buf
->len
== buf
->size
)
770 size
= buf
->size
* 2;
771 new = realloc (buf
->buf
, size
);
775 size
= buf
->size
+ 64;
779 buf
->allocated
= FcTrue
;
780 memcpy (new, buf
->buf
, buf
->len
);
785 buf
->failed
= FcTrue
;
789 FcMemFree (FC_MEM_STRBUF
, buf
->size
);
790 FcMemAlloc (FC_MEM_STRBUF
, size
);
794 buf
->buf
[buf
->len
++] = c
;
799 FcStrBufString (FcStrBuf
*buf
, const FcChar8
*s
)
803 if (!FcStrBufChar (buf
, c
))
809 FcStrBufData (FcStrBuf
*buf
, const FcChar8
*s
, int len
)
812 if (!FcStrBufChar (buf
, *s
++))
818 FcStrUsesHome (const FcChar8
*s
)
824 FcStrCopyFilename (const FcChar8
*s
)
830 FcChar8
*home
= FcConfigHome ();
835 size
= strlen ((char *) home
) + strlen ((char *) s
);
836 full
= (FcChar8
*) malloc (size
);
839 strcpy ((char *) full
, (char *) home
);
840 strcat ((char *) full
, (char *) s
+ 1);
841 new = FcStrCanonFilename (full
);
845 new = FcStrCanonFilename (s
);
850 FcStrLastSlash (const FcChar8
*path
)
854 slash
= (FcChar8
*) strrchr ((const char *) path
, '/');
859 backslash
= (FcChar8
*) strrchr ((const char *) path
, '\\');
860 if (!slash
|| (backslash
&& backslash
> slash
))
869 FcStrDirname (const FcChar8
*file
)
874 slash
= FcStrLastSlash (file
);
876 return FcStrCopy ((FcChar8
*) ".");
877 dir
= malloc ((slash
- file
) + 1);
880 FcMemAlloc (FC_MEM_STRING
, (slash
- file
) + 1);
881 strncpy ((char *) dir
, (const char *) file
, slash
- file
);
882 dir
[slash
- file
] = '\0';
887 FcStrBasename (const FcChar8
*file
)
891 slash
= FcStrLastSlash (file
);
893 return FcStrCopy (file
);
894 return FcStrCopy (slash
+ 1);
898 FcStrCanonAbsoluteFilename (const FcChar8
*s
)
902 const FcChar8
*slash
;
905 size
= strlen ((char *) s
) + 1;
906 file
= malloc (size
);
909 FcMemAlloc (FC_MEM_STRING
, size
);
913 if (*s
== '/' || *s
== '\0')
919 f
-= 1; /* squash // and trim final / from file */
922 if (!strncmp ((char *) slash
, "/.", 2))
924 f
-= 2; /* trim /. from file */
928 if (!strncmp ((char *) slash
, "/..", 3))
930 f
-= 3; /* trim /.. from file */
949 * Convert '\\' to '/' , remove double '/'
952 FcConvertDosPath (char *str
)
954 size_t len
= strlen (str
);
957 char *end
= str
+ len
;
986 FcStrCanonFilename (const FcChar8
*s
)
989 FcChar8 full
[FC_MAX_FILE_LEN
+ 2];
990 int size
= GetFullPathName (s
, sizeof (full
) -1,
994 perror ("GetFullPathName");
996 FcConvertDosPath (full
);
997 return FcStrCanonAbsoluteFilename (full
);
1000 return FcStrCanonAbsoluteFilename (s
);
1006 FcChar8 cwd
[FC_MAX_FILE_LEN
+ 2];
1007 if (getcwd ((char *) cwd
, FC_MAX_FILE_LEN
) == NULL
)
1009 strcat ((char *) cwd
, "/");
1010 full
= FcStrPlus (cwd
, s
);
1011 file
= FcStrCanonAbsoluteFilename (full
);
1020 FcStrSetCreate (void)
1022 FcStrSet
*set
= malloc (sizeof (FcStrSet
));
1025 FcMemAlloc (FC_MEM_STRSET
, sizeof (FcStrSet
));
1034 _FcStrSetAppend (FcStrSet
*set
, FcChar8
*s
)
1036 if (FcStrSetMember (set
, s
))
1041 if (set
->num
== set
->size
)
1043 FcChar8
**strs
= malloc ((set
->size
+ 2) * sizeof (FcChar8
*));
1047 FcMemAlloc (FC_MEM_STRSET
, (set
->size
+ 2) * sizeof (FcChar8
*));
1049 memcpy (strs
, set
->strs
, set
->num
* sizeof (FcChar8
*));
1052 FcMemFree (FC_MEM_STRSET
, (set
->size
+ 1) * sizeof (FcChar8
*));
1055 set
->size
= set
->size
+ 1;
1058 set
->strs
[set
->num
++] = s
;
1059 set
->strs
[set
->num
] = 0;
1064 FcStrSetMember (FcStrSet
*set
, const FcChar8
*s
)
1068 for (i
= 0; i
< set
->num
; i
++)
1069 if (!FcStrCmp (set
->strs
[i
], s
))
1075 FcStrSetEqual (FcStrSet
*sa
, FcStrSet
*sb
)
1078 if (sa
->num
!= sb
->num
)
1080 for (i
= 0; i
< sa
->num
; i
++)
1081 if (!FcStrSetMember (sb
, sa
->strs
[i
]))
1087 FcStrSetAdd (FcStrSet
*set
, const FcChar8
*s
)
1089 FcChar8
*new = FcStrCopy (s
);
1092 if (!_FcStrSetAppend (set
, new))
1101 FcStrSetAddFilename (FcStrSet
*set
, const FcChar8
*s
)
1103 FcChar8
*new = FcStrCopyFilename (s
);
1106 if (!_FcStrSetAppend (set
, new))
1115 FcStrSetDel (FcStrSet
*set
, const FcChar8
*s
)
1119 for (i
= 0; i
< set
->num
; i
++)
1120 if (!FcStrCmp (set
->strs
[i
], s
))
1122 FcStrFree (set
->strs
[i
]);
1124 * copy remaining string pointers and trailing
1127 memmove (&set
->strs
[i
], &set
->strs
[i
+1],
1128 (set
->num
- i
) * sizeof (FcChar8
*));
1136 FcStrSetDestroy (FcStrSet
*set
)
1138 if (--set
->ref
== 0)
1142 for (i
= 0; i
< set
->num
; i
++)
1143 FcStrFree (set
->strs
[i
]);
1146 FcMemFree (FC_MEM_STRSET
, (set
->size
+ 1) * sizeof (FcChar8
*));
1149 FcMemFree (FC_MEM_STRSET
, sizeof (FcStrSet
));
1155 FcStrListCreate (FcStrSet
*set
)
1159 list
= malloc (sizeof (FcStrList
));
1162 FcMemAlloc (FC_MEM_STRLIST
, sizeof (FcStrList
));
1170 FcStrListNext (FcStrList
*list
)
1172 if (list
->n
>= list
->set
->num
)
1174 return list
->set
->strs
[list
->n
++];
1178 FcStrListDone (FcStrList
*list
)
1180 FcStrSetDestroy (list
->set
);
1181 FcMemFree (FC_MEM_STRLIST
, sizeof (FcStrList
));
1186 #include "fcaliastail.h"