File: utf8.c

package info (click to toggle)
neko 2.4.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,412 kB
  • sloc: ansic: 19,583; ml: 4,924; sh: 54; makefile: 23
file content (456 lines) | stat: -rw-r--r-- 10,541 bytes parent folder | download | duplicates (2)
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
/*
 * Copyright (C)2005-2022 Haxe Foundation
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 */
#include <neko.h>
#include <string.h>


/**
	<doc>
	<h1>UTF8</h1>
	<p>
	Operations on UTF8 strings.
	Most of the operations are optimized for speed so they might still
	succeed on some malformed UTF8 string. The only function that completely
	check the UTF8 format is [utf8_validate]. Other functions might raise
	some exception or not depending on the malformed data.
	</p>
	</doc>
**/


typedef struct {
	value buf;
	int pos;
	int nesc;
} ubuf;

DEFINE_KIND(k_ubuf);

/**
	utf8_buf_alloc : size:int -> 'ubuf
	<doc>Create a new buffer with an initial size in bytes</doc>
**/
static value utf8_buf_alloc( value size ) {
	ubuf *b;
	val_check(size,int);
	if( val_int(size) < 0 )
		neko_error();
	b = (ubuf*)alloc(sizeof(ubuf));
	b->buf = alloc_empty_string(val_int(size));
	b->nesc = 0;
	b->pos = 0;
	return alloc_abstract(k_ubuf,b);
}

static void utf8_buf_resize( ubuf *b ) {
	value s;
	int len = val_strlen(b->buf);
	// allocate a number of bytes depending on previously
	// escaped caracters, with a minimum of 10
	int nbytes = (b->nesc + b->pos * 2 - 1) / (b->pos?b->pos:1);
	if( nbytes < 10 )
		nbytes = 10;
	s = alloc_empty_string(len+nbytes);
	memcpy(val_string(s),val_string(b->buf),len);
	b->buf = s;
}


/**
	utf8_buf_add : 'buf -> int -> void
	<doc>Add a valid UTF8 char (0 - 0x10FFFF) to the buffer</doc>
**/
static value utf8_buf_add( value buf, value uchar ) {
	ubuf *b;
	unsigned char *s;
	unsigned int c;
	val_check_kind(buf,k_ubuf);
	val_check(uchar,int);
	b = (ubuf*)val_data(buf);
	c = (unsigned)val_int(uchar);
	if( c <= 0x7F ) {
		if( b->pos >= val_strlen(b->buf) )
			utf8_buf_resize(b);
		val_string(b->buf)[b->pos++] = (char)c;
		return val_true;
	}
	if( b->pos + 4 > val_strlen(b->buf) )
		utf8_buf_resize(b);
	s = (unsigned char*)val_string(b->buf);
	if( c <= 0x7FF ) {
		b->nesc += 1;
		s[b->pos++] = 0xC0 | (c >> 6);
		s[b->pos++] = 0x80 | (c & 63);
	} else if( c <= 0xFFFF ) {
		b->nesc += 2;
		s[b->pos++] = 0xE0 | (c >> 12);
		s[b->pos++] = 0x80 | ((c >> 6) & 63);
		s[b->pos++] = 0x80 | (c & 63);
	} else if( c <= 0x10FFFF ) {
		b->nesc += 3;
		s[b->pos++] = 0xF0 | (c >> 18);
		s[b->pos++] = 0x80 | ((c >> 12) & 63);
		s[b->pos++] = 0x80 | ((c >> 6) & 63);
		s[b->pos++] = 0x80 | (c & 63);
	} else
		neko_error();
	return val_true;
}

/**
	utf8_buf_content : 'buf -> string
	<doc>
	Return the current content of the buffer.
	This is not a copy of the buffer but the shared content.
	Retreiving content and then continuing to add chars is
	possible but not very efficient.
	</doc>
**/
static value utf8_buf_content( value buf ) {
	ubuf *b;
	val_check_kind(buf,k_ubuf);
	b = (ubuf*)val_data(buf);
	val_set_length(b->buf,b->pos);
	val_string(b->buf)[b->pos] = 0;
	return b->buf;
}

/**
	utf8_buf_length : 'buf -> int
	<doc>Return the number of UTF8 chars stored in the buffer</doc>
**/
static value utf8_buf_length( value buf ) {
	ubuf *b;
	val_check_kind(buf,k_ubuf);
	b = (ubuf*)val_data(buf);
	return alloc_int(b->pos - b->nesc);
}

/**
	utf8_buf_size : 'buf -> int
	<doc>Return the current size in bytes of the buffer</doc>
**/
static value utf8_buf_size( value buf ) {
	ubuf *b;
	val_check_kind(buf,k_ubuf);
	b = (ubuf*)val_data(buf);
	return alloc_int(b->pos);
}

/**
	utf8_validate : string -> bool
	<doc>Validate if a string is encoded using the UTF8 format</doc>
**/
static value utf8_validate( value str ) {
	int l;
	unsigned char *s;
	val_check(str,string);
	l = val_strlen(str);
	s = (unsigned char*)val_string(str);
	while( l-- ) {
		unsigned char c = *s++;
		if( c < 0x80 )
			continue;
		else if( c < 0xC0 )
			return val_false;
		else if( c < 0xE0 ) {
			if( (*s++ & 0x80) != 0x80 )
				return val_false;
			l--;
		} else if( c < 0xF0 ) {
			if( (*s++ & 0x80) != 0x80 )
				return val_false;
			if( (*s++ & 0x80) != 0x80 )
				return val_false;
			l-=2;
		} else {
			if( (*s++ & 0x80) != 0x80 )
				return val_false;
			if( (*s++ & 0x80) != 0x80 )
				return val_false;
			if( (*s++ & 0x80) != 0x80 )
				return val_false;
			l-=3;
		}
	}
	return val_true;
}

/**
	utf8_length : string -> int
	<doc>Returns the number of UTF8 chars in the string.</doc>
**/
static value utf8_length( value str ) {
	int l;
	int count = 0;
	unsigned char *s;
	val_check(str,string);
	l = val_strlen(str);
	s = (unsigned char*)val_string(str);
	while( l > 0 ) {
		unsigned char c = *s;
		count++;
		if( c < 0x80 ) {
			l--;
			s++;
		} else if( c < 0xC0 )
			neko_error();
		else if( c < 0xE0 ) {
			l-=2;
			s+=2;
		} else if( c < 0xF0 ) {
			l-=3;
			s+=3;
		} else {
			l-=4;
			s+=4;
		}
	}
	if( l < 0 )
		neko_error();
	return alloc_int(count);
}

/**
	utf8_sub : string -> pos:int -> len:int -> string
	<doc>Returns a part of an UTF8 string.</doc>
**/
static value utf8_sub( value str, value pos, value len ) {
	int l;
	int count;
	unsigned char *s, *save;
	val_check(str,string);
	val_check(pos,int);
	val_check(len,int);
	l = val_strlen(str);
	count = val_int(pos);
	if( count < 0 )
		neko_error();
	s = (unsigned char*)val_string(str);
	while( count-- && l > 0 ) {
		unsigned char c = *s;
		if( c < 0x80 ) {
			l--;
			s++;
		} else if( c < 0xC0 )
			neko_error();
		else if( c < 0xE0 ) {
			l-=2;
			s+=2;
		} else if( c < 0xF0 ) {
			l-=3;
			s+=3;
		} else {
			l-=4;
			s+=4;
		}
	}
	if( l < 0 )
		neko_error();
	save = s;
	count = val_int(len);
	if( count < 0 )
		neko_error();
	while( count-- && l > 0 ) {
		unsigned char c = *s;
		if( c < 0x80 ) {
			l--;
			s++;
		} else if( c < 0xC0 )
			neko_error();
		else if( c < 0xE0 ) {
			l-=2;
			s+=2;
		} else if( c < 0xF0 ) {
			l-=3;
			s+=3;
		} else {
			l-=4;
			s+=4;
		}
	}
	if( l < 0 )
		neko_error();
	l = (int)(s - save);
	str = alloc_empty_string(l);
	memcpy(val_string(str),save,l);
	return str;
}

/**
	utf8_get : string -> n:int -> int
	<doc>Returns the [n]th char in an UTF8 string.
	This might be inefficient if [n] is big.</doc>
**/
static value utf8_get( value str, value pos ) {
	int l;
	int p;
	unsigned char *s;
	val_check(pos,int);
	val_check(str,string);
	l = val_strlen(str);
	p = val_int(pos);
	if( p < 0 )
		neko_error();
	s = (unsigned char*)val_string(str);
	while( l-- ) {
		unsigned char c = *s++;
		if( c < 0x80 ) {
			if( p-- == 0 )
				return alloc_int(c);
		} else if( c < 0xC0 )
			neko_error();
		else if( c < 0xE0 ) {
			l--;
			if( l < 0 )
				neko_error();
			if( p-- == 0 )
				return alloc_int(((c & 0x3F) << 6) | ((*s) & 0x7F));
			s++;
		} else if( c < 0xF0 ) {
			l -= 2;
			if( l < 0 )
				neko_error();
			if( p-- == 0 )
				return alloc_int(((c & 0x1F) << 12) | (((*s) & 0x7F) << 6) | (s[1] & 0x7F));
			s += 2;
		} else {
			l -= 3;
			if( l < 0 )
				neko_error();
			if( p-- == 0 )
				return alloc_int(((c & 0x0F) << 18) | (((*s) & 0x7F) << 12) | ((s[1] & 0x7F) << 6) | (s[2] & 0x7F));
			s += 3;
		}
	}
	neko_error();
	return val_true;
}

/**
	utf8_iter : string -> f:(int -> void) -> void
	<doc>Call [f] with each of UTF8 char of the string.</doc>
**/
static value utf8_iter( value str, value f ) {
	int l;
	unsigned char *s;
	val_check(str,string);
	val_check_function(f,1);
	l = val_strlen(str);
	s = (unsigned char*)val_string(str);
	while( l-- ) {
		unsigned char c = *s++;
		if( c < 0x80 )
			val_call1(f,alloc_int(c));
		else if( c < 0xC0 )
			neko_error();
		else if( c < 0xE0 ) {
			l--;
			if( l < 0 )
				neko_error();
			val_call1(f,alloc_int(((c & 0x3F) << 6) | ((*s) & 0x7F)));
			s++;
		} else if( c < 0xF0 ) {
			l -= 2;
			if( l < 0 )
				neko_error();
			val_call1(f,alloc_int(((c & 0x1F) << 12) | (((*s) & 0x7F) << 6) | (s[1] & 0x7F)));
			s += 2;
		} else {
			l -= 3;
			if( l < 0 )
				neko_error();
			val_call1(f,alloc_int(((c & 0x0F) << 18) | (((*s) & 0x7F) << 12) | ((s[1] & 0x7F) << 6) | (s[2] & 0x7F)));
			s += 3;
		}
	}
	return val_true;
}

/**
	utf8_compare : s1:string -> s2:string -> int
	<doc>Compare two UTF8 strings according to UTF8 char codes.</doc>
**/
static value utf8_compare( value str1, value str2 ) {
	int l1, l2, l;
	unsigned char *s1, *s2;
	val_check(str1,string);
	val_check(str2,string);
	l1 = val_strlen(str1);
	l2 = val_strlen(str2);
	s1 = (unsigned char*)val_string(str1);
	s2 = (unsigned char*)val_string(str2);
	l = (l1 < l2)?l1:l2;
	while( l-- ) {
		unsigned char c1 = *s1++;
		unsigned char c2 = *s2++;
		if( c1 != c2 )
			return alloc_int((c1 > c2)?1:-1);
		if( c1 < 0x7F )
			continue;
		else if( c1 < 0xC0 )
			neko_error();
		else if( c1 < 0xE0 ) {
			l--;
			if( l < 0 )
				neko_error();
			if( *s1++ != *s2++ )
				return alloc_int((s1[-1] > s2[-1])?1:-1);
		} else if( c1 < 0xF0 ) {
			l-=2;
			if( l < 0 )
				neko_error();
			if( *s1++ != *s2++ )
				return alloc_int((s1[-1] > s2[-1])?1:-1);
			if( *s1++ != *s2++ )
				return alloc_int((s1[-1] > s2[-1])?1:-1);
		} else {
			l -= 3;
			if( l < 0 )
				neko_error();
			if( *s1++ != *s2++ )
				return alloc_int((s1[-1] > s2[-1])?1:-1);
			if( *s1++ != *s2++ )
				return alloc_int((s1[-1] > s2[-1])?1:-1);
			if( *s1++ != *s2++ )
				return alloc_int((s1[-1] > s2[-1])?1:-1);
		}
	}
	if( l1 != l2 )
		return alloc_int((l1 > l2)?1:-1);
	return alloc_int(0);
}

DEFINE_PRIM(utf8_buf_alloc,1);
DEFINE_PRIM(utf8_buf_add,2);
DEFINE_PRIM(utf8_buf_content,1);
DEFINE_PRIM(utf8_buf_length,1);
DEFINE_PRIM(utf8_buf_size,1);

DEFINE_PRIM(utf8_get,2);
DEFINE_PRIM(utf8_validate,1);
DEFINE_PRIM(utf8_iter,2);
DEFINE_PRIM(utf8_length,1);
DEFINE_PRIM(utf8_compare,2);
DEFINE_PRIM(utf8_sub,3);

/* ************************************************************************ */