1 | #include <string.h>
|
---|
2 | #include <mruby.h>
|
---|
3 | #include <mruby/array.h>
|
---|
4 | #include <mruby/class.h>
|
---|
5 | #include <mruby/string.h>
|
---|
6 | #include <mruby/range.h>
|
---|
7 |
|
---|
8 | static mrb_value
|
---|
9 | mrb_str_getbyte(mrb_state *mrb, mrb_value str)
|
---|
10 | {
|
---|
11 | mrb_int pos;
|
---|
12 | mrb_get_args(mrb, "i", &pos);
|
---|
13 |
|
---|
14 | if (pos < 0)
|
---|
15 | pos += RSTRING_LEN(str);
|
---|
16 | if (pos < 0 || RSTRING_LEN(str) <= pos)
|
---|
17 | return mrb_nil_value();
|
---|
18 |
|
---|
19 | return mrb_fixnum_value((unsigned char)RSTRING_PTR(str)[pos]);
|
---|
20 | }
|
---|
21 |
|
---|
22 | static mrb_value
|
---|
23 | mrb_str_setbyte(mrb_state *mrb, mrb_value str)
|
---|
24 | {
|
---|
25 | mrb_int pos, byte;
|
---|
26 | long len;
|
---|
27 |
|
---|
28 | mrb_get_args(mrb, "ii", &pos, &byte);
|
---|
29 |
|
---|
30 | len = RSTRING_LEN(str);
|
---|
31 | if (pos < -len || len <= pos)
|
---|
32 | mrb_raisef(mrb, E_INDEX_ERROR, "index %S is out of array", mrb_fixnum_value(pos));
|
---|
33 | if (pos < 0)
|
---|
34 | pos += len;
|
---|
35 |
|
---|
36 | mrb_str_modify(mrb, mrb_str_ptr(str));
|
---|
37 | byte &= 0xff;
|
---|
38 | RSTRING_PTR(str)[pos] = byte;
|
---|
39 | return mrb_fixnum_value((unsigned char)byte);
|
---|
40 | }
|
---|
41 |
|
---|
42 | static mrb_value
|
---|
43 | mrb_str_byteslice(mrb_state *mrb, mrb_value str)
|
---|
44 | {
|
---|
45 | mrb_value a1;
|
---|
46 | mrb_int len;
|
---|
47 | int argc;
|
---|
48 |
|
---|
49 | argc = mrb_get_args(mrb, "o|i", &a1, &len);
|
---|
50 | if (argc == 2) {
|
---|
51 | return mrb_str_substr(mrb, str, mrb_fixnum(a1), len);
|
---|
52 | }
|
---|
53 | switch (mrb_type(a1)) {
|
---|
54 | case MRB_TT_RANGE:
|
---|
55 | {
|
---|
56 | mrb_int beg;
|
---|
57 |
|
---|
58 | len = RSTRING_LEN(str);
|
---|
59 | switch (mrb_range_beg_len(mrb, a1, &beg, &len, len, TRUE)) {
|
---|
60 | case 0: /* not range */
|
---|
61 | break;
|
---|
62 | case 1: /* range */
|
---|
63 | return mrb_str_substr(mrb, str, beg, len);
|
---|
64 | case 2: /* out of range */
|
---|
65 | mrb_raisef(mrb, E_RANGE_ERROR, "%S out of range", a1);
|
---|
66 | break;
|
---|
67 | }
|
---|
68 | return mrb_nil_value();
|
---|
69 | }
|
---|
70 | case MRB_TT_FLOAT:
|
---|
71 | a1 = mrb_fixnum_value((mrb_int)mrb_float(a1));
|
---|
72 | /* fall through */
|
---|
73 | case MRB_TT_FIXNUM:
|
---|
74 | return mrb_str_substr(mrb, str, mrb_fixnum(a1), 1);
|
---|
75 | default:
|
---|
76 | mrb_raise(mrb, E_TYPE_ERROR, "wrong type of argument");
|
---|
77 | }
|
---|
78 | /* not reached */
|
---|
79 | return mrb_nil_value();
|
---|
80 | }
|
---|
81 |
|
---|
82 | /*
|
---|
83 | * call-seq:
|
---|
84 | * str.swapcase! -> str or nil
|
---|
85 | *
|
---|
86 | * Equivalent to <code>String#swapcase</code>, but modifies the receiver in
|
---|
87 | * place, returning <i>str</i>, or <code>nil</code> if no changes were made.
|
---|
88 | * Note: case conversion is effective only in ASCII region.
|
---|
89 | */
|
---|
90 | static mrb_value
|
---|
91 | mrb_str_swapcase_bang(mrb_state *mrb, mrb_value str)
|
---|
92 | {
|
---|
93 | char *p, *pend;
|
---|
94 | int modify = 0;
|
---|
95 | struct RString *s = mrb_str_ptr(str);
|
---|
96 |
|
---|
97 | mrb_str_modify(mrb, s);
|
---|
98 | p = RSTRING_PTR(str);
|
---|
99 | pend = p + RSTRING_LEN(str);
|
---|
100 | while (p < pend) {
|
---|
101 | if (ISUPPER(*p)) {
|
---|
102 | *p = TOLOWER(*p);
|
---|
103 | modify = 1;
|
---|
104 | }
|
---|
105 | else if (ISLOWER(*p)) {
|
---|
106 | *p = TOUPPER(*p);
|
---|
107 | modify = 1;
|
---|
108 | }
|
---|
109 | p++;
|
---|
110 | }
|
---|
111 |
|
---|
112 | if (modify) return str;
|
---|
113 | return mrb_nil_value();
|
---|
114 | }
|
---|
115 |
|
---|
116 | /*
|
---|
117 | * call-seq:
|
---|
118 | * str.swapcase -> new_str
|
---|
119 | *
|
---|
120 | * Returns a copy of <i>str</i> with uppercase alphabetic characters converted
|
---|
121 | * to lowercase and lowercase characters converted to uppercase.
|
---|
122 | * Note: case conversion is effective only in ASCII region.
|
---|
123 | *
|
---|
124 | * "Hello".swapcase #=> "hELLO"
|
---|
125 | * "cYbEr_PuNk11".swapcase #=> "CyBeR_pUnK11"
|
---|
126 | */
|
---|
127 | static mrb_value
|
---|
128 | mrb_str_swapcase(mrb_state *mrb, mrb_value self)
|
---|
129 | {
|
---|
130 | mrb_value str;
|
---|
131 |
|
---|
132 | str = mrb_str_dup(mrb, self);
|
---|
133 | mrb_str_swapcase_bang(mrb, str);
|
---|
134 | return str;
|
---|
135 | }
|
---|
136 |
|
---|
137 | static mrb_value mrb_fixnum_chr(mrb_state *mrb, mrb_value num);
|
---|
138 |
|
---|
139 | /*
|
---|
140 | * call-seq:
|
---|
141 | * str << integer -> str
|
---|
142 | * str.concat(integer) -> str
|
---|
143 | * str << obj -> str
|
---|
144 | * str.concat(obj) -> str
|
---|
145 | *
|
---|
146 | * Append---Concatenates the given object to <i>str</i>. If the object is a
|
---|
147 | * <code>Integer</code>, it is considered as a codepoint, and is converted
|
---|
148 | * to a character before concatenation.
|
---|
149 | *
|
---|
150 | * a = "hello "
|
---|
151 | * a << "world" #=> "hello world"
|
---|
152 | * a.concat(33) #=> "hello world!"
|
---|
153 | */
|
---|
154 | static mrb_value
|
---|
155 | mrb_str_concat2(mrb_state *mrb, mrb_value self)
|
---|
156 | {
|
---|
157 | mrb_value str;
|
---|
158 |
|
---|
159 | mrb_get_args(mrb, "o", &str);
|
---|
160 | if (mrb_fixnum_p(str))
|
---|
161 | str = mrb_fixnum_chr(mrb, str);
|
---|
162 | else
|
---|
163 | str = mrb_string_type(mrb, str);
|
---|
164 | mrb_str_concat(mrb, self, str);
|
---|
165 | return self;
|
---|
166 | }
|
---|
167 |
|
---|
168 | /*
|
---|
169 | * call-seq:
|
---|
170 | * str.start_with?([prefixes]+) -> true or false
|
---|
171 | *
|
---|
172 | * Returns true if +str+ starts with one of the +prefixes+ given.
|
---|
173 | *
|
---|
174 | * "hello".start_with?("hell") #=> true
|
---|
175 | *
|
---|
176 | * # returns true if one of the prefixes matches.
|
---|
177 | * "hello".start_with?("heaven", "hell") #=> true
|
---|
178 | * "hello".start_with?("heaven", "paradise") #=> false
|
---|
179 | * "h".start_with?("heaven", "hell") #=> false
|
---|
180 | */
|
---|
181 | static mrb_value
|
---|
182 | mrb_str_start_with(mrb_state *mrb, mrb_value self)
|
---|
183 | {
|
---|
184 | mrb_value *argv, sub;
|
---|
185 | mrb_int argc, i;
|
---|
186 | mrb_get_args(mrb, "*", &argv, &argc);
|
---|
187 |
|
---|
188 | for (i = 0; i < argc; i++) {
|
---|
189 | size_t len_l, len_r;
|
---|
190 | int ai = mrb_gc_arena_save(mrb);
|
---|
191 | sub = mrb_string_type(mrb, argv[i]);
|
---|
192 | mrb_gc_arena_restore(mrb, ai);
|
---|
193 | len_l = RSTRING_LEN(self);
|
---|
194 | len_r = RSTRING_LEN(sub);
|
---|
195 | if (len_l >= len_r) {
|
---|
196 | if (memcmp(RSTRING_PTR(self), RSTRING_PTR(sub), len_r) == 0) {
|
---|
197 | return mrb_true_value();
|
---|
198 | }
|
---|
199 | }
|
---|
200 | }
|
---|
201 | return mrb_false_value();
|
---|
202 | }
|
---|
203 |
|
---|
204 | /*
|
---|
205 | * call-seq:
|
---|
206 | * str.end_with?([suffixes]+) -> true or false
|
---|
207 | *
|
---|
208 | * Returns true if +str+ ends with one of the +suffixes+ given.
|
---|
209 | */
|
---|
210 | static mrb_value
|
---|
211 | mrb_str_end_with(mrb_state *mrb, mrb_value self)
|
---|
212 | {
|
---|
213 | mrb_value *argv, sub;
|
---|
214 | mrb_int argc, i;
|
---|
215 | mrb_get_args(mrb, "*", &argv, &argc);
|
---|
216 |
|
---|
217 | for (i = 0; i < argc; i++) {
|
---|
218 | size_t len_l, len_r;
|
---|
219 | int ai = mrb_gc_arena_save(mrb);
|
---|
220 | sub = mrb_string_type(mrb, argv[i]);
|
---|
221 | mrb_gc_arena_restore(mrb, ai);
|
---|
222 | len_l = RSTRING_LEN(self);
|
---|
223 | len_r = RSTRING_LEN(sub);
|
---|
224 | if (len_l >= len_r) {
|
---|
225 | if (memcmp(RSTRING_PTR(self) + (len_l - len_r),
|
---|
226 | RSTRING_PTR(sub),
|
---|
227 | len_r) == 0) {
|
---|
228 | return mrb_true_value();
|
---|
229 | }
|
---|
230 | }
|
---|
231 | }
|
---|
232 | return mrb_false_value();
|
---|
233 | }
|
---|
234 |
|
---|
235 | static mrb_value
|
---|
236 | mrb_str_hex(mrb_state *mrb, mrb_value self)
|
---|
237 | {
|
---|
238 | return mrb_str_to_inum(mrb, self, 16, FALSE);
|
---|
239 | }
|
---|
240 |
|
---|
241 | static mrb_value
|
---|
242 | mrb_str_oct(mrb_state *mrb, mrb_value self)
|
---|
243 | {
|
---|
244 | return mrb_str_to_inum(mrb, self, 8, FALSE);
|
---|
245 | }
|
---|
246 |
|
---|
247 | /*
|
---|
248 | * call-seq:
|
---|
249 | * string.chr -> string
|
---|
250 | *
|
---|
251 | * Returns a one-character string at the beginning of the string.
|
---|
252 | *
|
---|
253 | * a = "abcde"
|
---|
254 | * a.chr #=> "a"
|
---|
255 | */
|
---|
256 | static mrb_value
|
---|
257 | mrb_str_chr(mrb_state *mrb, mrb_value self)
|
---|
258 | {
|
---|
259 | return mrb_str_substr(mrb, self, 0, 1);
|
---|
260 | }
|
---|
261 |
|
---|
262 | static mrb_value
|
---|
263 | mrb_fixnum_chr(mrb_state *mrb, mrb_value num)
|
---|
264 | {
|
---|
265 | mrb_int cp = mrb_fixnum(num);
|
---|
266 | #ifdef MRB_UTF8_STRING
|
---|
267 | char utf8[4];
|
---|
268 | mrb_int len;
|
---|
269 |
|
---|
270 | if (cp < 0 || 0x10FFFF < cp) {
|
---|
271 | mrb_raisef(mrb, E_RANGE_ERROR, "%S out of char range", num);
|
---|
272 | }
|
---|
273 | if (cp < 0x80) {
|
---|
274 | utf8[0] = (char)cp;
|
---|
275 | len = 1;
|
---|
276 | }
|
---|
277 | else if (cp < 0x800) {
|
---|
278 | utf8[0] = (char)(0xC0 | (cp >> 6));
|
---|
279 | utf8[1] = (char)(0x80 | (cp & 0x3F));
|
---|
280 | len = 2;
|
---|
281 | }
|
---|
282 | else if (cp < 0x10000) {
|
---|
283 | utf8[0] = (char)(0xE0 | (cp >> 12));
|
---|
284 | utf8[1] = (char)(0x80 | ((cp >> 6) & 0x3F));
|
---|
285 | utf8[2] = (char)(0x80 | ( cp & 0x3F));
|
---|
286 | len = 3;
|
---|
287 | }
|
---|
288 | else {
|
---|
289 | utf8[0] = (char)(0xF0 | (cp >> 18));
|
---|
290 | utf8[1] = (char)(0x80 | ((cp >> 12) & 0x3F));
|
---|
291 | utf8[2] = (char)(0x80 | ((cp >> 6) & 0x3F));
|
---|
292 | utf8[3] = (char)(0x80 | ( cp & 0x3F));
|
---|
293 | len = 4;
|
---|
294 | }
|
---|
295 | return mrb_str_new(mrb, utf8, len);
|
---|
296 | #else
|
---|
297 | char c;
|
---|
298 |
|
---|
299 | if (cp < 0 || 0xff < cp) {
|
---|
300 | mrb_raisef(mrb, E_RANGE_ERROR, "%S out of char range", num);
|
---|
301 | }
|
---|
302 | c = (char)cp;
|
---|
303 | return mrb_str_new(mrb, &c, 1);
|
---|
304 | #endif
|
---|
305 | }
|
---|
306 |
|
---|
307 | /*
|
---|
308 | * call-seq:
|
---|
309 | * string.lines -> array of string
|
---|
310 | *
|
---|
311 | * Returns strings per line;
|
---|
312 | *
|
---|
313 | * a = "abc\ndef"
|
---|
314 | * a.lines #=> ["abc\n", "def"]
|
---|
315 | */
|
---|
316 | static mrb_value
|
---|
317 | mrb_str_lines(mrb_state *mrb, mrb_value self)
|
---|
318 | {
|
---|
319 | mrb_value result;
|
---|
320 | mrb_value blk;
|
---|
321 | int ai;
|
---|
322 | mrb_int len;
|
---|
323 | mrb_value arg;
|
---|
324 | char *b = RSTRING_PTR(self);
|
---|
325 | char *p = b, *t;
|
---|
326 | char *e = b + RSTRING_LEN(self);
|
---|
327 |
|
---|
328 | mrb_get_args(mrb, "&", &blk);
|
---|
329 |
|
---|
330 | result = mrb_ary_new(mrb);
|
---|
331 | ai = mrb_gc_arena_save(mrb);
|
---|
332 | if (!mrb_nil_p(blk)) {
|
---|
333 | while (p < e) {
|
---|
334 | t = p;
|
---|
335 | while (p < e && *p != '\n') p++;
|
---|
336 | if (*p == '\n') p++;
|
---|
337 | len = (mrb_int) (p - t);
|
---|
338 | arg = mrb_str_new(mrb, t, len);
|
---|
339 | mrb_yield_argv(mrb, blk, 1, &arg);
|
---|
340 | mrb_gc_arena_restore(mrb, ai);
|
---|
341 | if (b != RSTRING_PTR(self)) {
|
---|
342 | ptrdiff_t diff = p - b;
|
---|
343 | b = RSTRING_PTR(self);
|
---|
344 | p = b + diff;
|
---|
345 | }
|
---|
346 | e = b + RSTRING_LEN(self);
|
---|
347 | }
|
---|
348 | return self;
|
---|
349 | }
|
---|
350 | while (p < e) {
|
---|
351 | t = p;
|
---|
352 | while (p < e && *p != '\n') p++;
|
---|
353 | if (*p == '\n') p++;
|
---|
354 | len = (mrb_int) (p - t);
|
---|
355 | mrb_ary_push(mrb, result, mrb_str_new(mrb, t, len));
|
---|
356 | mrb_gc_arena_restore(mrb, ai);
|
---|
357 | }
|
---|
358 | return result;
|
---|
359 | }
|
---|
360 |
|
---|
361 | /*
|
---|
362 | * call-seq:
|
---|
363 | * string.succ -> string
|
---|
364 | *
|
---|
365 | * Returns next sequence of the string;
|
---|
366 | *
|
---|
367 | * a = "abc"
|
---|
368 | * a.succ #=> "abd"
|
---|
369 | */
|
---|
370 | static mrb_value
|
---|
371 | mrb_str_succ_bang(mrb_state *mrb, mrb_value self)
|
---|
372 | {
|
---|
373 | mrb_value result;
|
---|
374 | unsigned char *p, *e, *b, *t;
|
---|
375 | const char *prepend;
|
---|
376 | struct RString *s = mrb_str_ptr(self);
|
---|
377 | mrb_int l;
|
---|
378 |
|
---|
379 | if (RSTRING_LEN(self) == 0)
|
---|
380 | return self;
|
---|
381 |
|
---|
382 | mrb_str_modify(mrb, s);
|
---|
383 | l = RSTRING_LEN(self);
|
---|
384 | b = p = (unsigned char*) RSTRING_PTR(self);
|
---|
385 | t = e = p + l;
|
---|
386 | *(e--) = 0;
|
---|
387 |
|
---|
388 | // find trailing ascii/number
|
---|
389 | while (e >= b) {
|
---|
390 | if (ISALNUM(*e))
|
---|
391 | break;
|
---|
392 | e--;
|
---|
393 | }
|
---|
394 | if (e < b) {
|
---|
395 | e = p + l - 1;
|
---|
396 | result = mrb_str_new_lit(mrb, "");
|
---|
397 | }
|
---|
398 | else {
|
---|
399 | // find leading letter of the ascii/number
|
---|
400 | b = e;
|
---|
401 | while (b > p) {
|
---|
402 | if (!ISALNUM(*b) || (ISALNUM(*b) && *b != '9' && *b != 'z' && *b != 'Z'))
|
---|
403 | break;
|
---|
404 | b--;
|
---|
405 | }
|
---|
406 | if (!ISALNUM(*b))
|
---|
407 | b++;
|
---|
408 | result = mrb_str_new(mrb, (char*) p, b - p);
|
---|
409 | }
|
---|
410 |
|
---|
411 | while (e >= b) {
|
---|
412 | if (!ISALNUM(*e)) {
|
---|
413 | if (*e == 0xff) {
|
---|
414 | mrb_str_cat_lit(mrb, result, "\x01");
|
---|
415 | (*e) = 0;
|
---|
416 | }
|
---|
417 | else
|
---|
418 | (*e)++;
|
---|
419 | break;
|
---|
420 | }
|
---|
421 | prepend = NULL;
|
---|
422 | if (*e == '9') {
|
---|
423 | if (e == b) prepend = "1";
|
---|
424 | *e = '0';
|
---|
425 | }
|
---|
426 | else if (*e == 'z') {
|
---|
427 | if (e == b) prepend = "a";
|
---|
428 | *e = 'a';
|
---|
429 | }
|
---|
430 | else if (*e == 'Z') {
|
---|
431 | if (e == b) prepend = "A";
|
---|
432 | *e = 'A';
|
---|
433 | }
|
---|
434 | else {
|
---|
435 | (*e)++;
|
---|
436 | break;
|
---|
437 | }
|
---|
438 | if (prepend) mrb_str_cat_cstr(mrb, result, prepend);
|
---|
439 | e--;
|
---|
440 | }
|
---|
441 | result = mrb_str_cat(mrb, result, (char*) b, t - b);
|
---|
442 | l = RSTRING_LEN(result);
|
---|
443 | mrb_str_resize(mrb, self, l);
|
---|
444 | memcpy(RSTRING_PTR(self), RSTRING_PTR(result), l);
|
---|
445 | return self;
|
---|
446 | }
|
---|
447 |
|
---|
448 | static mrb_value
|
---|
449 | mrb_str_succ(mrb_state *mrb, mrb_value self)
|
---|
450 | {
|
---|
451 | mrb_value str;
|
---|
452 |
|
---|
453 | str = mrb_str_dup(mrb, self);
|
---|
454 | mrb_str_succ_bang(mrb, str);
|
---|
455 | return str;
|
---|
456 | }
|
---|
457 |
|
---|
458 | #ifdef MRB_UTF8_STRING
|
---|
459 | static const char utf8len_codepage_zero[256] =
|
---|
460 | {
|
---|
461 | 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
|
---|
462 | 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
|
---|
463 | 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
|
---|
464 | 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
|
---|
465 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
|
---|
466 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
|
---|
467 | 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
|
---|
468 | 3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,4,4,4,4,4,0,0,0,0,0,0,0,0,0,0,0,
|
---|
469 | };
|
---|
470 |
|
---|
471 | static mrb_int
|
---|
472 | utf8code(unsigned char* p)
|
---|
473 | {
|
---|
474 | mrb_int len;
|
---|
475 |
|
---|
476 | if (p[0] < 0x80)
|
---|
477 | return p[0];
|
---|
478 |
|
---|
479 | len = utf8len_codepage_zero[p[0]];
|
---|
480 | if (len > 1 && (p[1] & 0xc0) == 0x80) {
|
---|
481 | if (len == 2)
|
---|
482 | return ((p[0] & 0x1f) << 6) + (p[1] & 0x3f);
|
---|
483 | if ((p[2] & 0xc0) == 0x80) {
|
---|
484 | if (len == 3)
|
---|
485 | return ((p[0] & 0x0f) << 12) + ((p[1] & 0x3f) << 6)
|
---|
486 | + (p[2] & 0x3f);
|
---|
487 | if ((p[3] & 0xc0) == 0x80) {
|
---|
488 | if (len == 4)
|
---|
489 | return ((p[0] & 0x07) << 18) + ((p[1] & 0x3f) << 12)
|
---|
490 | + ((p[2] & 0x3f) << 6) + (p[3] & 0x3f);
|
---|
491 | if ((p[4] & 0xc0) == 0x80) {
|
---|
492 | if (len == 5)
|
---|
493 | return ((p[0] & 0x03) << 24) + ((p[1] & 0x3f) << 18)
|
---|
494 | + ((p[2] & 0x3f) << 12) + ((p[3] & 0x3f) << 6)
|
---|
495 | + (p[4] & 0x3f);
|
---|
496 | if ((p[5] & 0xc0) == 0x80 && len == 6)
|
---|
497 | return ((p[0] & 0x01) << 30) + ((p[1] & 0x3f) << 24)
|
---|
498 | + ((p[2] & 0x3f) << 18) + ((p[3] & 0x3f) << 12)
|
---|
499 | + ((p[4] & 0x3f) << 6) + (p[5] & 0x3f);
|
---|
500 | }
|
---|
501 | }
|
---|
502 | }
|
---|
503 | }
|
---|
504 | return p[0];
|
---|
505 | }
|
---|
506 |
|
---|
507 | static mrb_value
|
---|
508 | mrb_str_ord(mrb_state* mrb, mrb_value str)
|
---|
509 | {
|
---|
510 | if (RSTRING_LEN(str) == 0)
|
---|
511 | mrb_raise(mrb, E_ARGUMENT_ERROR, "empty string");
|
---|
512 | return mrb_fixnum_value(utf8code((unsigned char*) RSTRING_PTR(str)));
|
---|
513 | }
|
---|
514 | #else
|
---|
515 | static mrb_value
|
---|
516 | mrb_str_ord(mrb_state* mrb, mrb_value str)
|
---|
517 | {
|
---|
518 | if (RSTRING_LEN(str) == 0)
|
---|
519 | mrb_raise(mrb, E_ARGUMENT_ERROR, "empty string");
|
---|
520 | return mrb_fixnum_value((unsigned char)RSTRING_PTR(str)[0]);
|
---|
521 | }
|
---|
522 | #endif
|
---|
523 |
|
---|
524 | static mrb_bool
|
---|
525 | all_digits_p(const char *s, mrb_int len)
|
---|
526 | {
|
---|
527 | while (len-- > 0) {
|
---|
528 | if (!ISDIGIT(*s)) return FALSE;
|
---|
529 | s++;
|
---|
530 | }
|
---|
531 | return TRUE;
|
---|
532 | }
|
---|
533 |
|
---|
534 | /*
|
---|
535 | * call-seq:
|
---|
536 | * str.upto(other_str, exclusive=false) {|s| block } -> str
|
---|
537 | * str.upto(other_str, exclusive=false) -> an_enumerator
|
---|
538 | *
|
---|
539 | * Iterates through successive values, starting at <i>str</i> and
|
---|
540 | * ending at <i>other_str</i> inclusive, passing each value in turn to
|
---|
541 | * the block. The <code>String#succ</code> method is used to generate
|
---|
542 | * each value. If optional second argument exclusive is omitted or is false,
|
---|
543 | * the last value will be included; otherwise it will be excluded.
|
---|
544 | *
|
---|
545 | * If no block is given, an enumerator is returned instead.
|
---|
546 | *
|
---|
547 | * "a8".upto("b6") {|s| print s, ' ' }
|
---|
548 | * for s in "a8".."b6"
|
---|
549 | * print s, ' '
|
---|
550 | * end
|
---|
551 | *
|
---|
552 | * <em>produces:</em>
|
---|
553 | *
|
---|
554 | * a8 a9 b0 b1 b2 b3 b4 b5 b6
|
---|
555 | * a8 a9 b0 b1 b2 b3 b4 b5 b6
|
---|
556 | *
|
---|
557 | * If <i>str</i> and <i>other_str</i> contains only ascii numeric characters,
|
---|
558 | * both are recognized as decimal numbers. In addition, the width of
|
---|
559 | * string (e.g. leading zeros) is handled appropriately.
|
---|
560 | *
|
---|
561 | * "9".upto("11").to_a #=> ["9", "10", "11"]
|
---|
562 | * "25".upto("5").to_a #=> []
|
---|
563 | * "07".upto("11").to_a #=> ["07", "08", "09", "10", "11"]
|
---|
564 | */
|
---|
565 | static mrb_value
|
---|
566 | mrb_str_upto(mrb_state *mrb, mrb_value beg)
|
---|
567 | {
|
---|
568 | mrb_value end;
|
---|
569 | mrb_value exclusive = mrb_false_value();
|
---|
570 | mrb_value block = mrb_nil_value();
|
---|
571 | mrb_value current, after_end;
|
---|
572 | mrb_int n;
|
---|
573 | mrb_bool excl;
|
---|
574 |
|
---|
575 | mrb_get_args(mrb, "o|o&", &end, &exclusive, &block);
|
---|
576 |
|
---|
577 | if (mrb_nil_p(block)) {
|
---|
578 | return mrb_funcall(mrb, beg, "to_enum", 3, mrb_symbol_value(mrb_intern_lit(mrb, "upto")), end, exclusive);
|
---|
579 | }
|
---|
580 | end = mrb_string_type(mrb, end);
|
---|
581 | excl = mrb_test(exclusive);
|
---|
582 |
|
---|
583 | /* single character */
|
---|
584 | if (RSTRING_LEN(beg) == 1 && RSTRING_LEN(end) == 1 &&
|
---|
585 | ISASCII(RSTRING_PTR(beg)[0]) && ISASCII(RSTRING_PTR(end)[0])) {
|
---|
586 | char c = RSTRING_PTR(beg)[0];
|
---|
587 | char e = RSTRING_PTR(end)[0];
|
---|
588 | int ai = mrb_gc_arena_save(mrb);
|
---|
589 |
|
---|
590 | if (c > e || (excl && c == e)) return beg;
|
---|
591 | for (;;) {
|
---|
592 | mrb_yield(mrb, block, mrb_str_new(mrb, &c, 1));
|
---|
593 | mrb_gc_arena_restore(mrb, ai);
|
---|
594 | if (!excl && c == e) break;
|
---|
595 | c++;
|
---|
596 | if (excl && c == e) break;
|
---|
597 | }
|
---|
598 | return beg;
|
---|
599 | }
|
---|
600 | /* both edges are all digits */
|
---|
601 | if (ISDIGIT(RSTRING_PTR(beg)[0]) && ISDIGIT(RSTRING_PTR(end)[0]) &&
|
---|
602 | all_digits_p(RSTRING_PTR(beg), RSTRING_LEN(beg)) &&
|
---|
603 | all_digits_p(RSTRING_PTR(end), RSTRING_LEN(end))) {
|
---|
604 | int ai = mrb_gc_arena_save(mrb);
|
---|
605 | mrb_int min_width = RSTRING_LEN(beg);
|
---|
606 | mrb_int max_width = RSTRING_LEN(end);
|
---|
607 | mrb_int bi = mrb_int(mrb, mrb_str_to_inum(mrb, beg, 10, FALSE));
|
---|
608 | mrb_int ei = mrb_int(mrb, mrb_str_to_inum(mrb, end, 10, FALSE));
|
---|
609 | mrb_value str = mrb_str_new(mrb, NULL, max_width);
|
---|
610 | char *buf = RSTRING_PTR(str);
|
---|
611 |
|
---|
612 | while (bi <= ei) {
|
---|
613 | if (excl && bi == ei) break;
|
---|
614 | snprintf(buf, max_width+1, "%.*" MRB_PRId, (int)min_width, bi);
|
---|
615 | mrb_yield(mrb, block, mrb_str_new(mrb, buf, strlen(buf)));
|
---|
616 | mrb_gc_arena_restore(mrb, ai);
|
---|
617 | bi++;
|
---|
618 | }
|
---|
619 |
|
---|
620 | return beg;
|
---|
621 | }
|
---|
622 | /* normal case */
|
---|
623 | n = mrb_int(mrb, mrb_funcall(mrb, beg, "<=>", 1, end));
|
---|
624 | if (n > 0 || (excl && n == 0)) return beg;
|
---|
625 |
|
---|
626 | after_end = mrb_funcall(mrb, end, "succ", 0);
|
---|
627 | current = mrb_str_dup(mrb, beg);
|
---|
628 | while (!mrb_str_equal(mrb, current, after_end)) {
|
---|
629 | int ai = mrb_gc_arena_save(mrb);
|
---|
630 | mrb_value next = mrb_nil_value();
|
---|
631 | if (excl || !mrb_str_equal(mrb, current, end))
|
---|
632 | next = mrb_funcall(mrb, current, "succ", 0);
|
---|
633 | mrb_yield(mrb, block, current);
|
---|
634 | if (mrb_nil_p(next)) break;
|
---|
635 | current = mrb_str_to_str(mrb, next);
|
---|
636 | if (excl && mrb_str_equal(mrb, current, end)) break;
|
---|
637 | if (RSTRING_LEN(current) > RSTRING_LEN(end) || RSTRING_LEN(current) == 0)
|
---|
638 | break;
|
---|
639 | mrb_gc_arena_restore(mrb, ai);
|
---|
640 | }
|
---|
641 |
|
---|
642 | return beg;
|
---|
643 | }
|
---|
644 |
|
---|
645 | void
|
---|
646 | mrb_mruby_string_ext_gem_init(mrb_state* mrb)
|
---|
647 | {
|
---|
648 | struct RClass * s = mrb->string_class;
|
---|
649 |
|
---|
650 | mrb_define_method(mrb, s, "dump", mrb_str_dump, MRB_ARGS_NONE());
|
---|
651 | mrb_define_method(mrb, s, "getbyte", mrb_str_getbyte, MRB_ARGS_REQ(1));
|
---|
652 | mrb_define_method(mrb, s, "setbyte", mrb_str_setbyte, MRB_ARGS_REQ(2));
|
---|
653 | mrb_define_method(mrb, s, "byteslice", mrb_str_byteslice, MRB_ARGS_REQ(1)|MRB_ARGS_OPT(1));
|
---|
654 | mrb_define_method(mrb, s, "swapcase!", mrb_str_swapcase_bang, MRB_ARGS_NONE());
|
---|
655 | mrb_define_method(mrb, s, "swapcase", mrb_str_swapcase, MRB_ARGS_NONE());
|
---|
656 | mrb_define_method(mrb, s, "concat", mrb_str_concat2, MRB_ARGS_REQ(1));
|
---|
657 | mrb_define_method(mrb, s, "<<", mrb_str_concat2, MRB_ARGS_REQ(1));
|
---|
658 | mrb_define_method(mrb, s, "start_with?", mrb_str_start_with, MRB_ARGS_REST());
|
---|
659 | mrb_define_method(mrb, s, "end_with?", mrb_str_end_with, MRB_ARGS_REST());
|
---|
660 | mrb_define_method(mrb, s, "hex", mrb_str_hex, MRB_ARGS_NONE());
|
---|
661 | mrb_define_method(mrb, s, "oct", mrb_str_oct, MRB_ARGS_NONE());
|
---|
662 | mrb_define_method(mrb, s, "chr", mrb_str_chr, MRB_ARGS_NONE());
|
---|
663 | mrb_define_method(mrb, s, "lines", mrb_str_lines, MRB_ARGS_NONE());
|
---|
664 | mrb_define_method(mrb, s, "succ", mrb_str_succ, MRB_ARGS_NONE());
|
---|
665 | mrb_define_method(mrb, s, "succ!", mrb_str_succ_bang, MRB_ARGS_NONE());
|
---|
666 | mrb_alias_method(mrb, s, mrb_intern_lit(mrb, "next"), mrb_intern_lit(mrb, "succ"));
|
---|
667 | mrb_alias_method(mrb, s, mrb_intern_lit(mrb, "next!"), mrb_intern_lit(mrb, "succ!"));
|
---|
668 | mrb_define_method(mrb, s, "ord", mrb_str_ord, MRB_ARGS_NONE());
|
---|
669 | mrb_define_method(mrb, s, "upto", mrb_str_upto, MRB_ARGS_ANY());
|
---|
670 |
|
---|
671 | mrb_define_method(mrb, mrb->fixnum_class, "chr", mrb_fixnum_chr, MRB_ARGS_NONE());
|
---|
672 | }
|
---|
673 |
|
---|
674 | void
|
---|
675 | mrb_mruby_string_ext_gem_final(mrb_state* mrb)
|
---|
676 | {
|
---|
677 | }
|
---|