File: string.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 (423 lines) | stat: -rw-r--r-- 10,045 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
/*
 * 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 <neko_vm.h>
#include <string.h>

/**
	<doc>
	<h1>String Functions</h1>
	<p>
	Some useful functions dealing with string manipulation.
	</p>
	</doc>
**/

/**
	string_split : s:string -> sep:string -> string list
	<doc>split the string [s] using separator [sep]</doc>
**/
static value string_split( value o, value s ) {
	value l, first;
	int ilen;
	int slen;
	int start = 0;
	int pos;
	val_check(s,string);
	val_check(o,string);
	ilen = val_strlen(o);
	slen = val_strlen(s);
	l = NULL;
	first = NULL;
	for(pos=slen?0:1;pos<=ilen-slen;pos++)
		if( memcmp(val_string(o)+pos,val_string(s),slen) == 0 ) {
			value ss = copy_string(val_string(o)+start,pos-start);
			value l2 = alloc_array(2);
			val_array_ptr(l2)[0] = ss;
			val_array_ptr(l2)[1] = val_null;
			if( first == NULL )
				first = l2;
			else
				val_array_ptr(l)[1] = l2;
			l = l2;
			start = pos + slen;
			if( slen )
				pos = start - 1;
		}
	if( ilen > 0 && slen ) {
		value ss = start ? copy_string(val_string(o)+start,ilen-start) : o;
		value l2 = alloc_array(2);
		val_array_ptr(l2)[0] = ss;
		val_array_ptr(l2)[1] = val_null;
		if( first == NULL )
			first = l2;
		else
			val_array_ptr(l)[1] = l2;
	}
	return (first == NULL)?val_null:first;
}

#define HEX			1
#define HEX_SMALL	2

/**
	sprintf : fmt:string -> params:(any | array) -> string
	<doc>
	Format a string. If only one parameter is needed then it can be
	directly passed, either the parameters need to be stored in an array.
	The following formats are accepted (with corresponding types) :
	<ul>
		<li>[%s] : string</li>
		<li>[%d] [%x] [%X] : int</li>
		<li>[%c] : int in the 0..255 range</li>
		<li>[%b] : bool</li>
		<li>[%f] : float</li>
	</ul>
	</doc>
**/
static value neko_sprintf( value fmt, value params ) {
	char *last, *cur, *end;
	int count = 0;
	buffer b;
	val_check(fmt,string);
	b = alloc_buffer(NULL);
	last = val_string(fmt);
	cur = last;
	end = cur + val_strlen(fmt);
	while( cur != end ) {
		if( *cur == '%' ) {
			int width = 0, prec = 0, flags = 0;
			buffer_append_sub(b,last,cur - last);
			cur++;
			while( *cur >= '0' && *cur <= '9' ) {
				width = width * 10 + (*cur - '0');
				cur++;
			}
			if( *cur == '.' ) {
				cur++;
				while( *cur >= '0' && *cur <= '9' ) {
					prec = prec * 10 + (*cur - '0');
					cur++;
				}
			}
			if( *cur == '%' ) {
				buffer_append_sub(b,"%",1);
				cur++;
			} else {
				value param;
				if( count == 0 && !val_is_array(params) ) { // first ?
					param = params;
					count++;
				} else if( !val_is_array(params) || val_array_size(params) <= count )
					neko_error();
				else
					param = val_array_ptr(params)[count++];
				switch( *cur ) {
				case 'c':
					{
						int c;
						char cc;
						val_check(param,int);
						c = val_int(param);
						if( c < 0 || c > 255 )
							neko_error();
						cc = (char)c;
						buffer_append_sub(b,&cc,1);
					}
					break;
				case 'x':
					flags |= HEX_SMALL;
				case 'X':
					flags |= HEX;
				case 'd':
					{
						char tmp[10];
						int sign = 0;
						int size = 0;
						int tsize;
						int n;
						val_check(param,int);
						n = val_int(param);
						if( !(flags & HEX) && n < 0 ) {
							sign++;
							prec--;
							n = -n;
						} else if( n == 0 )
							tmp[9-size++] = '0';
						if( flags & HEX ) {
							unsigned int nn = (unsigned int)n;
							while( nn > 0 ) {
								int k = nn&15;
								if( k < 10 )
									tmp[9-size++] = k + '0';
								else
									tmp[9-size++] = (k - 10) + ((flags & HEX_SMALL)?'a':'A');
								nn = nn >> 4;
							}
						} else {
							while( n > 0 ) {
								tmp[9-size++] = (n % 10) + '0';
								n = n / 10;
							}
						}
						tsize = (size > prec)?size:prec + sign;
						while( width > tsize ) {
							width--;
							buffer_append_sub(b," ",1);
						}
						if( sign )
							buffer_append_sub(b,"-",1);
						while( prec > size ) {
							prec--;
							buffer_append_sub(b,"0",1);
						}
						buffer_append_sub(b,tmp+10-size,size);
					}
					break;
				case 'f':
					{
						val_check(param,float);
						val_buffer(b,param);
					}
					break;
				case 's':
					{
						int size;
						int tsize;
						val_check(param,string);
						size = val_strlen(param);
						tsize = (size > prec)?size:prec;
						while( width > tsize ) {
							width--;
							buffer_append_sub(b," ",1);
						}
						while( prec > size ) {
							prec--;
							buffer_append_sub(b," ",1);
						}
						buffer_append_sub(b,val_string(param),size);
					}
					break;
				case 'b':
					{
						val_check(param,bool);
						buffer_append_sub(b,val_bool(param)?"true":"false",val_bool(param)?4:5);
					}
					break;
				default:
					neko_error();
					break;
				}
			}
			cur++;
			last = cur;
		} else
			cur++;
	}
	buffer_append_sub(b,last,cur - last);
	return buffer_to_string(b);
}

/**
	url_decode : string -> string
	<doc>Decode an url using escaped format</doc>
**/
static value url_decode( value v ) {
	val_check(v,string);
	{
		int pin = 0;
		int pout = 0;
		const char *in = val_string(v);
		int len = val_strlen(v);
		value v2 = alloc_empty_string(len);
		char *out = (char*)val_string(v2);
		while( len-- > 0 ) {
			char c = in[pin++];
			if( c == '+' )
				c = ' ';
			else if( c == '%' ) {
				int p1, p2;
				if( len < 2 )
					break;
				p1 = in[pin++];
				p2 = in[pin++];
				len -= 2;
				if( p1 >= '0' && p1 <= '9' )
					p1 -= '0';
				else if( p1 >= 'a' && p1 <= 'f' )
					p1 -= 'a' - 10;
				else if( p1 >= 'A' && p1 <= 'F' )
					p1 -= 'A' - 10;
				else
					continue;
				if( p2 >= '0' && p2 <= '9' )
					p2 -= '0';
				else if( p2 >= 'a' && p2 <= 'f' )
					p2 -= 'a' - 10;
				else if( p2 >= 'A' && p2 <= 'F' )
					p2 -= 'A' - 10;
				else
					continue;
				c = (char)((unsigned char)((p1 << 4) + p2));
			}
			out[pout++] = c;
		}
		out[pout] = 0;
		val_set_size(v2,pout);
		return v2;
	}
}

/**
	url_encode : string -> string
	<doc>Encode an url using escaped format</doc>
**/
static value url_encode( value v ) {
	val_check(v,string);
	{
		int pin = 0;
		int pout = 0;
		const unsigned char *in = (const unsigned char*)val_string(v);
		static const char *hex = "0123456789ABCDEF";
		int len = val_strlen(v);
		value v2 = alloc_empty_string(len * 3);
		unsigned char *out = (unsigned char*)val_string(v2);
		while( len-- > 0 ) {
			unsigned char c = in[pin++];
			if( (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || c == '_' || c == '-' || c == '.' )
				out[pout++] = c;
			else {
				out[pout++] = '%';
				out[pout++] = hex[c >> 4];
				out[pout++] = hex[c & 0xF];
			}
		}
		out[pout] = 0;
		val_set_size(v2,pout);
		return v2;
	}
}

/**
	base_encode : s:string -> base:string -> string
	<doc>
	Encode a string using the specified base.
	The base length must be a power of two.
	</doc>
**/
static value base_encode( value s, value base ) {
	int nbits;
	int len;
	int size;
	int mask;
	unsigned int buf;
	int curbits;
	value out;
	unsigned char *cin, *cout, *chars;
	val_check(s,string);
	val_check(base,string);
	len = val_strlen(base);
	cin = (unsigned char *)val_string(s);
	chars = (unsigned char *)val_string(base);
	nbits = 1;
	while( len > 1 << nbits )
		nbits++;
	if( nbits > 8 || len != 1 << nbits )
		neko_error();
	size = (val_strlen(s) * 8 + nbits - 1) / nbits;
	out = alloc_empty_string(size);
	cout = (unsigned char *)val_string(out);
	buf = 0;
	curbits = 0;
	mask = ((1 << nbits) - 1);
	while( size-- > 0 ) {
		while( curbits < nbits ) {
			curbits += 8;
			buf <<= 8;
			buf |= *cin++;
		}
		curbits -= nbits;
		*cout++ = chars[(buf >> curbits) & mask];
	}
	return out;
}

/**
	base_decode : s:string -> base:string -> string
	<doc>
	Decode a string encode in the specified base.
	The base length must be a power of two.
	</doc>
**/
static value base_decode( value s, value base ) {
	int nbits;
	int len;
	int size;
	unsigned int buf;
	int curbits;
	value out;
	int i;
	int tbl[256];
	unsigned char *cin, *cout, *chars;
	val_check(s,string);
	val_check(base,string);
	len = val_strlen(base);
	cin = (unsigned char *)val_string(s);
	chars = (unsigned char *)val_string(base);
	nbits = 1;
	while( len > 1 << nbits )
		nbits++;
	if( nbits > 8 || len != 1 << nbits )
		neko_error();
	for(i=0;i<256;i++)
		tbl[i] = -1;
	for(i=0;i<len;i++)
		tbl[chars[i]] = i;
	size = (val_strlen(s) * nbits) / 8;
	out = alloc_empty_string(size);
	cout = (unsigned char *)val_string(out);
	buf = 0;
	curbits = 0;
	while( size-- > 0 ) {
		while( curbits < 8 ) {
			curbits += nbits;
			buf <<= nbits;
			i = tbl[*cin++];
			if( i == -1 )
				neko_error();
			buf |= i;
		}
		curbits -= 8;
		*cout++ = (buf >> curbits) & 0xFF;
	}
	return out;
}

#define neko_sprintf__2 sprintf__2
DEFINE_PRIM(neko_sprintf,2);
DEFINE_PRIM(string_split,2);
DEFINE_PRIM(url_decode,1);
DEFINE_PRIM(url_encode,1);
DEFINE_PRIM(base_encode,2);
DEFINE_PRIM(base_decode,2);

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