|
1
|
1 #include "shared.h"
|
|
|
2
|
|
|
3 #include <locale.h>
|
|
|
4
|
|
|
5 using namespace pfc;
|
|
|
6
|
|
|
7 extern "C" {
|
|
|
8
|
|
|
9 #if 0
|
|
|
10 inline static unsigned q_tolower(unsigned c)
|
|
|
11 {
|
|
|
12 if (c>='A' && c<='Z') c += 'a' - 'A';
|
|
|
13 return c;
|
|
|
14 }
|
|
|
15 #else
|
|
|
16 static const t_uint8 ascii_tolower_table[128] = {0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F,0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,0x1A,0x1B,0x1C,0x1D,0x1E,0x1F,0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,0x29,0x2A,0x2B,0x2C,0x2D,0x2E,0x2F,0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x3A,0x3B,0x3C,0x3D,0x3E,0x3F,0x40,0x61,0x62,0x63,0x64,0x65,0x66,0x67,0x68,0x69,0x6A,0x6B,0x6C,0x6D,0x6E,0x6F,0x70,0x71,0x72,0x73,0x74,0x75,0x76,0x77,0x78,0x79,0x7A,0x5B,0x5C,0x5D,0x5E,0x5F,0x60,0x61,0x62,0x63,0x64,0x65,0x66,0x67,0x68,0x69,0x6A,0x6B,0x6C,0x6D,0x6E,0x6F,0x70,0x71,0x72,0x73,0x74,0x75,0x76,0x77,0x78,0x79,0x7A,0x7B,0x7C,0x7D,0x7E,0x7F};
|
|
|
17 #define q_tolower(c) ascii_tolower_table[(unsigned)c]
|
|
|
18 #endif
|
|
|
19
|
|
|
20
|
|
|
21 unsigned SHARED_EXPORT uCharLower(unsigned param)
|
|
|
22 {
|
|
|
23 return pfc::charLower(param);
|
|
|
24 }
|
|
|
25
|
|
|
26 unsigned SHARED_EXPORT uCharUpper(unsigned param)
|
|
|
27 {
|
|
|
28 return pfc::charUpper(param);
|
|
|
29 }
|
|
|
30
|
|
|
31 static inline int compare_wchar(unsigned c1,unsigned c2) throw()
|
|
|
32 {
|
|
|
33 if (c1==c2) return 0;
|
|
|
34 c1 = pfc::charLower(c1);
|
|
|
35 c2 = pfc::charLower(c2);
|
|
|
36 if (c1<c2) return -1;
|
|
|
37 else if (c1>c2) return 1;
|
|
|
38 else return 0;
|
|
|
39 }
|
|
|
40
|
|
|
41
|
|
|
42 int SHARED_EXPORT stricmp_utf8(const char * p1,const char * p2) throw()
|
|
|
43 {
|
|
|
44 for(;;)
|
|
|
45 {
|
|
|
46 if (*p1>=0 && *p2>=0)//signed char
|
|
|
47 {
|
|
|
48 unsigned c1 = q_tolower(*p1), c2 = q_tolower(*p2);
|
|
|
49 if (c1<c2) return -1;
|
|
|
50 else if (c1>c2) return 1;
|
|
|
51 else if (c1 == 0) return 0;
|
|
|
52 else
|
|
|
53 {
|
|
|
54 p1++;
|
|
|
55 p2++;
|
|
|
56 }
|
|
|
57 }
|
|
|
58 else
|
|
|
59 {
|
|
|
60 unsigned w1,w2; t_size d1,d2;
|
|
|
61 d1 = utf8_decode_char(p1,w1);
|
|
|
62 d2 = utf8_decode_char(p2,w2);
|
|
|
63 if (d1 == 0 && d2 == 0) return 0;
|
|
|
64 else if (d1==0) return -1;
|
|
|
65 else if (d2==0) return 1;
|
|
|
66 int rv = compare_wchar(w1,w2);
|
|
|
67 if (rv) return rv;
|
|
|
68 p1 += d1;
|
|
|
69 p2 += d2;
|
|
|
70 }
|
|
|
71 }
|
|
|
72 }
|
|
|
73
|
|
|
74 int SHARED_EXPORT stricmp_utf8_stringtoblock(const char * p1,const char * p2,t_size p2_bytes) throw()
|
|
|
75 {
|
|
|
76 return stricmp_utf8_ex(p1,-1,p2,p2_bytes);
|
|
|
77 }
|
|
|
78
|
|
|
79 int SHARED_EXPORT stricmp_utf8_partial(const char * p1,const char * p2,t_size num) throw()
|
|
|
80 {
|
|
|
81 for(;num;)
|
|
|
82 {
|
|
|
83 unsigned w1,w2; t_size d1,d2;
|
|
|
84 d1 = utf8_decode_char(p1,w1);
|
|
|
85 d2 = utf8_decode_char(p2,w2);
|
|
|
86 if (w2==0 || d2==0) return 0;
|
|
|
87 int rv = compare_wchar(w1,w2);
|
|
|
88 if (rv) return rv;
|
|
|
89 p1 += d1;
|
|
|
90 p2 += d2;
|
|
|
91 num--;
|
|
|
92 }
|
|
|
93 return 0;
|
|
|
94 }
|
|
|
95
|
|
|
96 int SHARED_EXPORT stricmp_utf8_max(const char * p1,const char * p2,t_size p1_bytes) throw()
|
|
|
97 {
|
|
|
98 return stricmp_utf8_ex(p1,p1_bytes,p2,-1);
|
|
|
99 }
|
|
|
100
|
|
|
101 namespace {
|
|
|
102 typedef bool (*t_replace_test)(const char * src,const char * test,t_size len);
|
|
|
103
|
|
|
104 static bool replace_test_i(const char * src,const char * test,t_size len)
|
|
|
105 {
|
|
|
106 return stricmp_utf8_max(src,test,len)==0;
|
|
|
107 }
|
|
|
108
|
|
|
109 static bool replace_test(const char * src,const char * test,t_size len)
|
|
|
110 {
|
|
|
111 t_size ptr;
|
|
|
112 bool rv = true;
|
|
|
113 for(ptr=0;ptr<len;ptr++)
|
|
|
114 {
|
|
|
115 if (src[ptr]!=test[ptr]) {rv = false; break;}
|
|
|
116 }
|
|
|
117 return rv;
|
|
|
118 }
|
|
|
119 }
|
|
|
120
|
|
|
121 t_size SHARED_EXPORT uReplaceStringAdd(string_base & out,const char * src,t_size src_len,const char * s1,t_size len1,const char * s2,t_size len2,bool casesens)
|
|
|
122 {
|
|
|
123 t_replace_test testfunc = casesens ? replace_test : replace_test_i;
|
|
|
124
|
|
|
125 len1 = strlen_max(s1,len1); len2 = strlen_max(s2,len2);
|
|
|
126
|
|
|
127 t_size len = strlen_max(src,src_len);
|
|
|
128
|
|
|
129 t_size count = 0;
|
|
|
130
|
|
|
131 if (len1>0)
|
|
|
132 {
|
|
|
133 t_size ptr = 0;
|
|
|
134 while(ptr+len1<=len)
|
|
|
135 {
|
|
|
136 if (testfunc(src+ptr,s1,len1))
|
|
|
137 {
|
|
|
138 count++;
|
|
|
139 out.add_string(s2,len2);
|
|
|
140 ptr += len1;
|
|
|
141 }
|
|
|
142 else out.add_byte(src[ptr++]);
|
|
|
143 }
|
|
|
144 if (ptr<len) out.add_string(src+ptr,len-ptr);
|
|
|
145 }
|
|
|
146 return count;
|
|
|
147 }
|
|
|
148
|
|
|
149 t_size SHARED_EXPORT uReplaceCharAdd(string_base & out,const char * src,t_size src_len,unsigned c1,unsigned c2,bool casesens)
|
|
|
150 {
|
|
|
151 assert(c1>0);
|
|
|
152 assert(c2>0);
|
|
|
153 char s1[8],s2[8];
|
|
|
154 t_size len1,len2;
|
|
|
155 len1 = utf8_encode_char(c1,s1);
|
|
|
156 len2 = utf8_encode_char(c2,s2);
|
|
|
157 return uReplaceString(out,src,src_len,s1,len1,s2,len2,casesens);
|
|
|
158 }
|
|
|
159
|
|
|
160
|
|
|
161 void SHARED_EXPORT uAddStringLower(string_base & out,const char * src,t_size len)
|
|
|
162 {
|
|
|
163 while(len && *src)
|
|
|
164 {
|
|
|
165 unsigned c; t_size d;
|
|
|
166 d = utf8_decode_char(src,c,len);
|
|
|
167 if (d==0 || d>len) break;
|
|
|
168 out.add_char(uCharLower(c));
|
|
|
169 src+=d;
|
|
|
170 len-=d;
|
|
|
171 }
|
|
|
172 }
|
|
|
173
|
|
|
174 void SHARED_EXPORT uAddStringUpper(string_base & out,const char * src,t_size len)
|
|
|
175 {
|
|
|
176 while(len && *src)
|
|
|
177 {
|
|
|
178 unsigned c; t_size d;
|
|
|
179 d = utf8_decode_char(src,c,len);
|
|
|
180 if (d==0 || d>len) break;
|
|
|
181 out.add_char(uCharUpper(c));
|
|
|
182 src+=d;
|
|
|
183 len-=d;
|
|
|
184 }
|
|
|
185 }
|
|
|
186
|
|
|
187 int SHARED_EXPORT stricmp_utf8_ex(const char * p1,t_size p1_bytes,const char * p2,t_size p2_bytes) throw()
|
|
|
188 {
|
|
|
189 p1_bytes = strlen_max(p1,p1_bytes);
|
|
|
190 p2_bytes = strlen_max(p2,p2_bytes);
|
|
|
191 for(;;)
|
|
|
192 {
|
|
|
193 if (p1_bytes == 0 && p2_bytes == 0) return 0;
|
|
|
194 else if (p1_bytes == 0) return -1;
|
|
|
195 else if (p2_bytes == 0) return 1;
|
|
|
196 else if (*p1>0 && *p2>0)//signed char
|
|
|
197 {
|
|
|
198 unsigned c1 = q_tolower(*p1), c2 = q_tolower(*p2);
|
|
|
199 if (c1<c2) return -1;
|
|
|
200 else if (c1>c2) return 1;
|
|
|
201 else
|
|
|
202 {
|
|
|
203 p1++;
|
|
|
204 p2++;
|
|
|
205 p1_bytes--;
|
|
|
206 p2_bytes--;
|
|
|
207 }
|
|
|
208 }
|
|
|
209 else
|
|
|
210 {
|
|
|
211 unsigned w1,w2;
|
|
|
212 auto d1 = utf8_decode_char(p1,w1,p1_bytes);
|
|
|
213 auto d2 = utf8_decode_char(p2,w2,p2_bytes);
|
|
|
214 if (d1==0) return -1;
|
|
|
215 if (d2==0) return 1;
|
|
|
216 int rv = compare_wchar(w1,w2);
|
|
|
217 if (rv) return rv;
|
|
|
218 p1 += d1;
|
|
|
219 p2 += d2;
|
|
|
220 p1_bytes -= d1;
|
|
|
221 p2_bytes -= d2;
|
|
|
222 }
|
|
|
223 }
|
|
|
224 }
|
|
|
225
|
|
|
226 }
|