Edinburgh Speech Tools  2.1-release
string16.c
Go to the documentation of this file.
1 /*************************************************************************/
2 /* */
3 /* Copyright (c) 1997-98 Richard Tobin, Language Technology Group, HCRC, */
4 /* University of Edinburgh. */
5 /* */
6 /* THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND, */
7 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
8 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
9 /* IN NO EVENT SHALL THE AUTHOR OR THE UNIVERSITY OF EDINBURGH BE LIABLE */
10 /* FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF */
11 /* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION */
12 /* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
13 /* */
14 /*************************************************************************/
15 #ifdef FOR_LT
16 
17 #include "lt-memory.h"
18 
19 #define Malloc salloc
20 #define Realloc srealloc
21 #define Free sfree
22 
23 #else
24 
25 #include "system.h"
26 
27 #endif
28 
29 #include "charset.h"
30 #include "ctype16.h"
31 #include "string16.h"
32 
33 int strcasecmp8(const char8 *s1, const char8 *s2)
34 {
35  char8 c1, c2;
36 
37  while(1)
38  {
39  c1 = Toupper(*s1++);
40  c2 = Toupper(*s2++);
41  if(c1 == 0 && c2 == 0)
42  return 0;
43  if(c1 == 0)
44  return -1;
45  if(c2 == 0)
46  return 1;
47  if(c1 < c2)
48  return -1;
49  if(c1 > c2)
50  return 1;
51  }
52 }
53 
54 int strncasecmp8(const char8 *s1, const char8 *s2, size_t n)
55 {
56  char8 c1, c2;
57 
58  while(n-- > 0)
59  {
60  c1 = Toupper(*s1++);
61  c2 = Toupper(*s2++);
62  if(c1 == 0 && c2 == 0)
63  return 0;
64  if(c1 == 0)
65  return -1;
66  if(c2 == 0)
67  return 1;
68  if(c1 < c2)
69  return -1;
70  if(c1 > c2)
71  return 1;
72  }
73 
74  return 0;
75 }
76 
77 char8 *strdup8(const char8 *s)
78 {
79  char8 *buf;
80  int len;
81 
82  len = strlen8(s);
83  buf = Malloc(len + 1);
84  if(!buf)
85  return 0;
86 
87  strcpy8(buf, s);
88 
89  return buf;
90 }
91 
92 /*
93  * NB these two functions return static storage, use Strdup or strdup8
94  * if you want to keep the result.
95  */
96 
97 /* Convert a Latin-1 string to UTF-16 (easy!) */
98 
100 {
101  static char16 *buf = 0;
102  int i, len;
103 
104  len = strlen8(s);
105  buf = Realloc(buf, (len + 1) * sizeof(char16));
106  if(!buf)
107  return 0;
108 
109  for(i=0; i<len; i++)
110  buf[i] = s[i];
111  buf[i] = 0;
112 
113  return buf;
114 }
115 
116 /* Convert a UTF-16 string to Latin-1, replacing missing characters with Xs */
117 
119 {
120  static char8 *buf = 0;
121  int i, len;
122 
123  len = strlen16(s);
124  buf = Realloc(buf, len + 1);
125  if(!buf)
126  return 0;
127 
128  for(i=0; i<len; i++)
129  buf[i] = s[i] > 255 ? 'X' : s[i];
130  buf[i] = 0;
131 
132  return buf;
133 }
134 
135 char16 *strcpy16(char16 *s1, const char16 *s2)
136 {
137  char16 *t = s1;
138 
139  while(*s2)
140  *s1++ = *s2++;
141  *s1 = 0;
142 
143  return t;
144 }
145 
146 char16 *strncpy16(char16 *s1, const char16 *s2, size_t n)
147 {
148  char16 *t = s1;
149 
150  while(n-- > 0 && *s2)
151  *s1++ = *s2++;
152  if(n > 0)
153  *s1 = 0;
154 
155  return t;
156 }
157 
159 {
160  char16 *buf;
161  int len;
162 
163  len = strlen16(s);
164  buf = Malloc((len + 1) * sizeof(char16));
165  if(!buf)
166  return 0;
167 
168  strcpy16(buf, s);
169 
170  return buf;
171 }
172 
173 size_t strlen16(const char16 *s)
174 {
175  int len = 0;
176 
177  while(*s++)
178  len++;
179 
180  return len;
181 }
182 
183 char16 *strchr16(const char16 *s, int c)
184 {
185  for( ; *s; s++)
186  if(*s == c)
187  return (char16 *)s; /* Is const bogus or what? */
188 
189  return 0;
190 }
191 
192 int strcmp16(const char16 *s1, const char16 *s2)
193 {
194  char16 c1, c2;
195 
196  while(1)
197  {
198  c1 = *s1++;
199  c2 = *s2++;
200  if(c1 == 0 && c2 == 0)
201  return 0;
202  if(c1 == 0)
203  return -1;
204  if(c2 == 0)
205  return 1;
206  if(c1 < c2)
207  return -1;
208  if(c1 > c2)
209  return 1;
210  }
211 }
212 
213 int strncmp16(const char16 *s1, const char16 *s2, size_t n)
214 {
215  char16 c1, c2;
216 
217  while(n-- > 0)
218  {
219  c1 = *s1++;
220  c2 = *s2++;
221  if(c1 == 0 && c2 == 0)
222  return 0;
223  if(c1 == 0)
224  return -1;
225  if(c2 == 0)
226  return 1;
227  if(c1 < c2)
228  return -1;
229  if(c1 > c2)
230  return 1;
231  }
232 
233  return 0;
234 }
235 
236 /* XXX only works for characters < 256 because Toupper does */
237 
238 int strcasecmp16(const char16 *s1, const char16 *s2)
239 {
240  char16 c1, c2;
241 
242  while(1)
243  {
244  c1 = Toupper(*s1++);
245  c2 = Toupper(*s2++);
246  if(c1 == 0 && c2 == 0)
247  return 0;
248  if(c1 == 0)
249  return -1;
250  if(c2 == 0)
251  return 1;
252  if(c1 < c2)
253  return -1;
254  if(c1 > c2)
255  return 1;
256  }
257 }
258 
259 int strncasecmp16(const char16 *s1, const char16 *s2, size_t n)
260 {
261  char16 c1, c2;
262 
263  while(n-- > 0)
264  {
265  c1 = Toupper(*s1++);
266  c2 = Toupper(*s2++);
267  if(c1 == 0 && c2 == 0)
268  return 0;
269  if(c1 == 0)
270  return -1;
271  if(c2 == 0)
272  return 1;
273  if(c1 < c2)
274  return -1;
275  if(c1 > c2)
276  return 1;
277  }
278 
279  return 0;
280 }
281 
282 /* A very naive implementation */
283 
284 char16 *strstr16(const char16 *s1, const char16 *s2)
285 {
286  int len, first;
287 
288  first = s2[0];
289  if(first == 0)
290  return (char16 *)s1;
291 
292  len = strlen16(s2);
293 
294  while((s1 = strchr16(s1, first)))
295  {
296  if(strncmp16(s1, s2, len) == 0)
297  return (char16 *)s1;
298  else
299  s1++;
300  }
301 
302  return 0;
303 }
304 
#define strlen8(s)
Definition: string16.h:51
#define strcpy8(s1, s2)
Definition: string16.h:54
char8 * strdup8(const char8 *s)
Definition: string16.c:77
char8 * char16tochar8(const char16 *s)
Definition: string16.c:118
int strncasecmp8(const char8 *s1, const char8 *s2, size_t n)
Definition: string16.c:54
unsigned short char16
Definition: charset.h:32
STD_API int Toupper(int c)
Definition: ctype16.c:52
char16 * strncpy16(char16 *s1, const char16 *s2, size_t n)
Definition: string16.c:146
char16 * strcpy16(char16 *s1, const char16 *s2)
Definition: string16.c:135
int strcasecmp8(const char8 *s1, const char8 *s2)
Definition: string16.c:33
char16 * strstr16(const char16 *s1, const char16 *s2)
Definition: string16.c:284
char16 * char8tochar16(const char8 *s)
Definition: string16.c:99
int strcmp16(const char16 *s1, const char16 *s2)
Definition: string16.c:192
char16 * strchr16(const char16 *s, int c)
Definition: string16.c:183
char16 * strdup16(const char16 *s)
Definition: string16.c:158
int strncmp16(const char16 *s1, const char16 *s2, size_t n)
Definition: string16.c:213
size_t strlen16(const char16 *s)
Definition: string16.c:173
char char8
Definition: charset.h:31
int strcasecmp16(const char16 *s1, const char16 *s2)
Definition: string16.c:238
void * Malloc(int bytes)
Definition: system.c:19
void * Realloc(void *mem, int bytes)
Definition: system.c:27
int strncasecmp16(const char16 *s1, const char16 *s2, size_t n)
Definition: string16.c:259