File: string16.c

package info (click to toggle)
rxp 1.2.3-1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 436 kB
  • ctags: 966
  • sloc: ansic: 9,679; makefile: 127; sh: 3
file content (321 lines) | stat: -rw-r--r-- 4,843 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
#ifdef FOR_LT

#include "lt-memory.h"

#define Malloc salloc
#define Realloc srealloc
#define Free sfree

#else

#include "system.h"

#endif

#include "charset.h"
#include "ctype16.h"
#include "string16.h"

int strcasecmp8(const char8 *s1, const char8 *s2)
{
    char8 c1, c2;

    while(1)
    {
	c1 = Toupper(*s1++);
	c2 = Toupper(*s2++);
	if(c1 == 0 && c2 == 0)
	    return 0;
	if(c1 == 0)
	    return -1;
	if(c2 == 0)
	    return 1;
	if(c1 < c2)
	    return -1;
	if(c1 > c2)
	    return 1;
    }
}

int strncasecmp8(const char8 *s1, const char8 *s2, size_t n)
{
    char8 c1, c2;

    while(n-- > 0)
    {
	c1 = Toupper(*s1++);
	c2 = Toupper(*s2++);
	if(c1 == 0 && c2 == 0)
	    return 0;
	if(c1 == 0)
	    return -1;
	if(c2 == 0)
	    return 1;
	if(c1 < c2)
	    return -1;
	if(c1 > c2)
	    return 1;
    }

    return 0;
}

char8 *strdup8(const char8 *s)
{
    char8 *buf;
    int len;

    len = strlen8(s);
    buf = Malloc(len + 1);
    if(!buf)
	return 0;

    strcpy8(buf, s);

    return buf;
}

/* Convert a Latin-1 string to UTF-16 (easy!) */

void translate_latin1_utf16(const char8 *from, char16 *to)
{
    while(*from)
	*to++ = (unsigned char)*from++; /* be sure not to sign extend */
    *to = 0;
}

/* Convert a UTF-16 string to Latin-1, replacing missing characters with Xs */

void translate_utf16_latin1(const char16 *from, char8 *to)
{
    while(*from)
    {
	char16 c = *from++;
	*to++ = c > 255 ? 'X' : c;
    }
    *to = 0;
}

/* Conversion functions that realloc the destination buffer; so if
   it's null the space will be malloced. */

char16 *translate_latin1_utf16_m(const char8 *from, char16 *to)
{
    to = Realloc(to, (strlen8(from) + 1) * sizeof(char16));
    if(to)
	translate_latin1_utf16(from, to);
    return to;
}

char8 *translate_utf16_latin1_m(const char16 *from, char8 *to)
{
    to = Realloc(to, strlen16(from) + 1);
    if(to)
	translate_utf16_latin1(from, to);
    return to;
}

char16 *strcpy16(char16 *s1, const char16 *s2)
{
    char16 *t = s1;

    while(*s2)
	*s1++ = *s2++;
    *s1 = 0;

    return t;
}

char16 *strncpy16(char16 *s1, const char16 *s2, size_t n)
{
    char16 *t = s1;

    while(n > 0 && *s2)
    {
	n--;
	*s1++ = *s2++;
    }

    /* Apparently strncpy is supposed to fill the destination with nulls,
       not just null terminate */

    while(n-- > 0)
	*s1++ = 0;

    return t;
}

char16 *strdup16(const char16 *s)
{
    char16 *buf;
    int len;

    len = strlen16(s);
    buf = Malloc((len + 1) * sizeof(char16));
    if(!buf)
	return 0;

    strcpy16(buf, s);

    return buf;
}

size_t strlen16(const char16 *s)
{
    int len = 0;

    while(*s++)
	len++;

    return len;
}

char16 *strchr16(const char16 *s, int c)
{
    for( ; *s; s++)
	if(*s == c)
	    return (char16 *)s;	/* Is const bogus or what? */

    return 0;
}

int strcmp16(const char16 *s1, const char16 *s2)
{
    char16 c1, c2;

    while(1)
    {
	c1 = *s1++;
	c2 = *s2++;
	if(c1 == 0 && c2 == 0)
	    return 0;
	if(c1 == 0)
	    return -1;
	if(c2 == 0)
	    return 1;
	if(c1 < c2)
	    return -1;
	if(c1 > c2)
	    return 1;
    }
}

int strncmp16(const char16 *s1, const char16 *s2, size_t n)
{
    char16 c1, c2;

    while(n-- > 0)
    {
	c1 = *s1++;
	c2 = *s2++;
	if(c1 == 0 && c2 == 0)
	    return 0;
	if(c1 == 0)
	    return -1;
	if(c2 == 0)
	    return 1;
	if(c1 < c2)
	    return -1;
	if(c1 > c2)
	    return 1;
    }

    return 0;
}

/* XXX only works for characters < 256 because Toupper does */

int strcasecmp16(const char16 *s1, const char16 *s2)
{
    char16 c1, c2;

    while(1)
    {
	c1 = Toupper(*s1++);
	c2 = Toupper(*s2++);
	if(c1 == 0 && c2 == 0)
	    return 0;
	if(c1 == 0)
	    return -1;
	if(c2 == 0)
	    return 1;
	if(c1 < c2)
	    return -1;
	if(c1 > c2)
	    return 1;
    }
}

int strncasecmp16(const char16 *s1, const char16 *s2, size_t n)
{
    char16 c1, c2;

    while(n-- > 0)
    {
	c1 = Toupper(*s1++);
	c2 = Toupper(*s2++);
	if(c1 == 0 && c2 == 0)
	    return 0;
	if(c1 == 0)
	    return -1;
	if(c2 == 0)
	    return 1;
	if(c1 < c2)
	    return -1;
	if(c1 > c2)
	    return 1;
    }

    return 0;
}

char16 *strcat16(char16 *s1, const char16 *s2)
{
    char16 *t = s1;

    s1 += strlen16(s1);
    strcpy16(s1, s2);

    return t;
}

char16 *strncat16(char16 *s1, const char16 *s2, size_t n)
{
    char16 *t = s1;

    s1 += strlen16(s1);

    /* Unlike strncpy, strncat *always* null terminates, and does not
       fill with nulls. */

    while(n-- > 0 && *s2)
	*s1++ = *s2++;

    *s1 = 0;

    return t;
}

/* A very naive implementation */

char16 *strstr16(const char16 *s1, const char16 *s2)
{
    int len, first;

    first = s2[0];
    if(first == 0)
	return (char16 *)s1;

    len = strlen16(s2);

    while((s1 = strchr16(s1, first)))
    {
	if(strncmp16(s1, s2, len) == 0)
	    return (char16 *)s1;
	else
	    s1++;
    }

    return 0;
}