forked from mnbtkd/MameScheme
-
Notifications
You must be signed in to change notification settings - Fork 0
/
string.c
287 lines (244 loc) · 6.99 KB
/
string.c
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
/*--------------------------------------------------------------------
string.c - MameScheme ( Scheme Interpreter based on R5RS )
--------------------------------------------------------------------*/
/*
* Copyright (c) 2007-2011 mnbtkd
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of authors nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include "mame.h"
int mbslen(char* str)
{
int size,len;
unsigned char c=0;
len=0;
while ( *str ) {
c = (unsigned char)*str;
size = get_char_size(c);
str += size;
len++;
}
return len;
}
#ifdef ENCODING_SJIS
#define MAXCHARSIZE 2
#endif
#ifdef ENCODING_UTF8
#define MAXCHARSIZE 4
#endif
#if defined ENCODING_SJIS
#define STR_LEN(s) (mbslen(s))
#elif defined ENCODING_UTF8
#define STR_LEN(s) (mbslen(s))
#else
#define STR_LEN(s) (strlen(s))
#endif
#define STR_SIZE(s) (strlen(s))
SchObj make_char( unsigned int c )
{
SchChar* ptr;
ptr = SCH_MALLOC( sizeof(SchChar) );
ptr->hd.flags = T_CHAR;
ptr->ch = c;
return (SchObj)ptr;
}
int get_char_byte_size(unsigned int ch)
{
if ((ch & 0xff000000U) != 0) return 4;
if ((ch & 0xff0000U) != 0) return 3;
if ((ch & 0xff00U) != 0) return 2;
if ((ch & 0xffU) != 0) return 1;
return 0;
}
SchObj subr_is_eq_char( SchObj args )
{
return CHAR_EQUALP(SCH_CAR(args),
SCH_CADR(args));
}
SchObj make_string( char* s )
{
SchString* ptr;
ptr = SCH_MALLOC( sizeof(SchString) );
ptr->hd.flags = T_STRING;
ptr->size = STR_SIZE( s );
ptr->len = STR_LEN( s );
ptr->buf = SCH_MALLOC( ptr->size + 1 );
strcpy( ptr->buf, s );
return (SchObj)ptr;
}
SchObj make_string_k( int k )
{
SchString* ptr;
ptr = SCH_MALLOC( sizeof(SchString) );
ptr->hd.flags = T_STRING;
ptr->len = k;
ptr->size = (k+1)*MAXCHARSIZE;
ptr->buf = SCH_MALLOC(ptr->size);
ptr->buf[0] = '\0';
return (SchObj)ptr;
}
SchObj string_ref( SchString* str, int i )
{
return SCH_CHAR( str->buf[i] );
}
void string_set( SchString* str, int i, char c)
{
str->buf[i] = c;
}
SchObj make_string_k_ch(int k, char c)
{
SchString* ptr;
int i;
ptr = SCH_STRING_OBJ( make_string_k(k) );
for ( i=0 ; i<k ; ++i ) {
string_set( ptr, i, c );
}
return (SchObj)ptr;
}
char* set_forward(char* str, int size)
{
while ( size > 0 ) {
str += get_char_size(*str);
--size;
}
return str;
}
SchObj substring( SchObj str,
int beg,
int end )
{
SchObj ret;
size_t len;
int diff;
int size;
char* s;
char *buf;
if ( ! STRINGP(str) ) {
EXCEPTION("string required");
}
len = SCH_STRING_OBJ(str)->len;
if ( len < beg || len < end || beg > end) {
EXCEPTION("out of range");
}
ret = make_string_k( end - beg );
diff = end - beg;
s = SCH_STRING_OBJ(str)->buf;
buf = SCH_STRING_OBJ(ret)->buf;
s = set_forward(s,beg);
while ( diff > 0 ) {
size = get_char_size(*s);
while ( size > 0 ) {
*buf = *s;
buf++;
s++;
--size;
}
--diff;
}
return ret;
}
SchObj string2list( SchObj str )
{
char * buf = SCH_STRING_OBJ(str)->buf;
unsigned int len = SCH_STRING_OBJ(str)->len;
SchPort* p = make_input_port_string(buf);
unsigned int ch;
SchObj ret;
SchObj last_cons;
if ( len > 0 ) {
ch = SCH_GETC(p);
last_cons = ret = SCH_LIST1(SCH_CHAR(ch));
--len;
while ( len > 0 ) {
ch = SCH_GETC(p);
SCH_SET_CDR(last_cons,SCH_LIST1(SCH_CHAR(ch)));
last_cons = SCH_CDR(last_cons);
--len;
}
return ret;
} else {
return SCH_NIL;
}
}
SchObj list2string( SchObj lst )
{
SchObj kar,ret;
unsigned int ch;
SchObj SCH_MANGLE(tmp);
ret = make_string_k(10);
char* buf = SCH_STRING_OBJ(ret)->buf;
FOR_EACH(kar,lst){
ch = SCH_CHAR_OBJ(kar)->ch;
if ( ch ) {
if ( ((ch >> 24) & 0xffU) != 0 ){
*buf++ = (unsigned char)((ch >> 24) & 0xffU);
SCH_STRING_OBJ(ret)->size++;
}
if ( ((ch >> 16) & 0xffU) != 0 ){
*buf++ = (unsigned char)((ch >> 16) & 0xffU);
SCH_STRING_OBJ(ret)->size++;
}
if ( ((ch >> 8) & 0xffU) != 0 ){
*buf++ = (unsigned char)((ch >> 8) & 0xffU);
SCH_STRING_OBJ(ret)->size++;
}
*buf++ = (unsigned char)(ch & 0xffU);
SCH_STRING_OBJ(ret)->size++;
}
SCH_STRING_OBJ(ret)->len++;
}
*buf = '\0';
return ret;
}
SchObj string_fill( SchObj str, SchObj chobj )
{
char* buf = SCH_STRING_OBJ(str)->buf;
int org_len = SCH_STRING_OBJ(str)->len;
unsigned int ch = SCH_CHAR_OBJ(chobj)->ch;
unsigned int ch_size,len,shift_bit,i,j;
if ( 0 != (ch >> 24) ) {
ch_size = 4;
} else if ( 0 != (ch >> 16) ) {
ch_size = 3;
} else if ( 0 != (ch >> 8) ) {
ch_size = 2;
} else {
ch_size = 1;
}
len = (SCH_STRING_OBJ(str)->size) / ch_size;
if ( len > org_len ) {
len = org_len;
}
for ( i=0 ; i < len ; ++i ) {
for ( j=0 ; j < ch_size ; ++j ) {
shift_bit = ((ch_size-1)*8) - (j*8);
*buf++ = (unsigned char)((ch >> shift_bit) & 0xffU);
}
}
*buf = '\0';
SCH_STRING_OBJ(str)->size = len*ch_size;
SCH_STRING_OBJ(str)->len = len;
return str;
}