Debian Codegraph @

vendor/mruby-source/mrbgems/mruby-string-ext/src/string.c

raw
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 #include <mruby/internal.h>
8
9 #define ENC_ASCII_8BIT "ASCII-8BIT"
10 #define ENC_BINARY "BINARY"
11 #define ENC_UTF8 "UTF-8"
12
13 #define ENC_COMP_P(enc, enc_lit) \
14 casecmp_p(RSTRING_PTR(enc), RSTRING_LEN(enc), enc_lit, sizeof(enc_lit"")-1)
15
16 static mrb_bool
17 casecmp_p(const char *s1, mrb_int len1, const char *s2, mrb_int len2)
18 {
19 const char *e1, *e2;
20
21 if (len1 != len2) return FALSE;
22 e1 = s1 + len1;
23 e2 = s2 + len2;
24 while (s1 < e1 && s2 < e2) {
25 if (*s1 != *s2 && TOUPPER(*s1) != TOUPPER(*s2)) return FALSE;
26 s1++;
27 s2++;
28 }
29 return TRUE;
30 }
31
32 static mrb_value
33 int_chr_binary(mrb_state *mrb, mrb_value num)
34 {
35 mrb_int cp = mrb_as_int(mrb, num);
36 char c;
37 mrb_value str;
38
39 if (cp < 0 || 0xff < cp) {
40 mrb_raisef(mrb, E_RANGE_ERROR, "%v out of char range", num);
41 }
42 c = (char)cp;
43 str = mrb_str_new(mrb, &c, 1);
44 RSTR_SET_ASCII_FLAG(mrb_str_ptr(str));
45 return str;
46 }
47
48 #ifdef MRB_UTF8_STRING
49 static mrb_value
50 int_chr_utf8(mrb_state *mrb, mrb_value num)
51 {
52 mrb_int cp = mrb_int(mrb, num);
53 char utf8[4];
54 mrb_int len;
55 mrb_value str;
56 uint32_t ascii_flag = 0;
57
58 if (cp < 0 || 0x10FFFF < cp) {
59 mrb_raisef(mrb, E_RANGE_ERROR, "%v out of char range", num);
60 }
61 if (cp < 0x80) {
62 utf8[0] = (char)cp;
63 len = 1;
64 ascii_flag = MRB_STR_ASCII;
65 }
66 else if (cp < 0x800) {
67 utf8[0] = (char)(0xC0 | (cp >> 6));
68 utf8[1] = (char)(0x80 | (cp & 0x3F));
69 len = 2;
70 }
71 else if (cp < 0x10000) {
72 utf8[0] = (char)(0xE0 | (cp >> 12));
73 utf8[1] = (char)(0x80 | ((cp >> 6) & 0x3F));
74 utf8[2] = (char)(0x80 | ( cp & 0x3F));
75 len = 3;
76 }
77 else {
78 utf8[0] = (char)(0xF0 | (cp >> 18));
79 utf8[1] = (char)(0x80 | ((cp >> 12) & 0x3F));
80 utf8[2] = (char)(0x80 | ((cp >> 6) & 0x3F));
81 utf8[3] = (char)(0x80 | ( cp & 0x3F));
82 len = 4;
83 }
84 str = mrb_str_new(mrb, utf8, len);
85 mrb_str_ptr(str)->flags |= ascii_flag;
86 return str;
87 }
88 #endif
89
90 /*
91 * call-seq:
92 * str.swapcase! -> str or nil
93 *
94 * Equivalent to <code>String#swapcase</code>, but modifies the receiver in
95 * place, returning <i>str</i>, or <code>nil</code> if no changes were made.
96 * Note: case conversion is effective only in ASCII region.
97 */
98 static mrb_value
99 str_swapcase_bang(mrb_state *mrb, mrb_value str)
100 {
101 char *p, *pend;
102 int modify = 0;
103 struct RString *s = mrb_str_ptr(str);
104
105 mrb_str_modify(mrb, s);
106 p = RSTRING_PTR(str);
107 pend = p + RSTRING_LEN(str);
108 while (p < pend) {
109 if (ISUPPER(*p)) {
110 *p = TOLOWER(*p);
111 modify = 1;
112 }
113 else if (ISLOWER(*p)) {
114 *p = TOUPPER(*p);
115 modify = 1;
116 }
117 p++;
118 }
119
120 if (modify) return str;
121 return mrb_nil_value();
122 }
123
124 /*
125 * call-seq:
126 * str.swapcase -> new_str
127 *
128 * Returns a copy of <i>str</i> with uppercase alphabetic characters converted
129 * to lowercase and lowercase characters converted to uppercase.
130 * Note: case conversion is effective only in ASCII region.
131 *
132 * "Hello".swapcase #=> "hELLO"
133 * "cYbEr_PuNk11".swapcase #=> "CyBeR_pUnK11"
134 */
135 static mrb_value
136 str_swapcase(mrb_state *mrb, mrb_value self)
137 {
138 mrb_value str;
139
140 str = mrb_str_dup(mrb, self);
141 str_swapcase_bang(mrb, str);
142 return str;
143 }
144
145 static void
146 str_concat(mrb_state *mrb, mrb_value self, mrb_value str)
147 {
148 if (mrb_integer_p(str) || mrb_float_p(str))
149 #ifdef MRB_UTF8_STRING
150 str = int_chr_utf8(mrb, str);
151 #else
152 str = int_chr_binary(mrb, str);
153 #endif
154 else
155 mrb_ensure_string_type(mrb, str);
156 mrb_str_cat_str(mrb, self, str);
157 }
158
159 /*
160 * call-seq:
161 * str << obj -> str
162 * str.concat(*obj) -> str
163 *
164 * s = 'foo'
165 * s.concat('bar', 'baz') # => "foobarbaz"
166 * s # => "foobarbaz"
167 *
168 * For each given object +object+ that is an \Integer,
169 * the value is considered a codepoint and converted to a character before concatenation:
170 *
171 * s = 'foo'
172 * s.concat(32, 'bar', 32, 'baz') # => "foo bar baz"
173 *
174 */
175 static mrb_value
176 str_concat_m(mrb_state *mrb, mrb_value self)
177 {
178 if (mrb_get_argc(mrb) == 1) {
179 str_concat(mrb, self, mrb_get_arg1(mrb));
180 return self;
181 }
182
183 mrb_value *args;
184 mrb_int alen;
185
186 mrb_get_args(mrb, "*", &args, &alen);
187 for (mrb_int i=0; i<alen; i++) {
188 str_concat(mrb, self, args[i]);
189 }
190 return self;
191 }
192
193 /*
194 * call-seq:
195 * str.start_with?([prefixes]+) -> true or false
196 *
197 * Returns true if +str+ starts with one of the +prefixes+ given.
198 *
199 * "hello".start_with?("hell") #=> true
200 *
201 * # returns true if one of the prefixes matches.
202 * "hello".start_with?("heaven", "hell") #=> true
203 * "hello".start_with?("heaven", "paradise") #=> false
204 * "h".start_with?("heaven", "hell") #=> false
205 */
206 static mrb_value
207 str_start_with(mrb_state *mrb, mrb_value self)
208 {
209 const mrb_value *argv;
210 mrb_int argc, i;
211 mrb_get_args(mrb, "*", &argv, &argc);
212
213 for (i = 0; i < argc; i++) {
214 size_t len_l, len_r;
215 int ai = mrb_gc_arena_save(mrb);
216 mrb_value sub = argv[i];
217 mrb_ensure_string_type(mrb, sub);
218 mrb_gc_arena_restore(mrb, ai);
219 len_l = RSTRING_LEN(self);
220 len_r = RSTRING_LEN(sub);
221 if (len_l >= len_r) {
222 if (memcmp(RSTRING_PTR(self), RSTRING_PTR(sub), len_r) == 0) {
223 return mrb_true_value();
224 }
225 }
226 }
227 return mrb_false_value();
228 }
229
230 /*
231 * call-seq:
232 * str.end_with?([suffixes]+) -> true or false
233 *
234 * Returns true if +str+ ends with one of the +suffixes+ given.
235 */
236 static mrb_value
237 str_end_with(mrb_state *mrb, mrb_value self)
238 {
239 const mrb_value *argv;
240 mrb_int argc, i;
241 mrb_get_args(mrb, "*", &argv, &argc);
242
243 for (i = 0; i < argc; i++) {
244 size_t len_l, len_r;
245 int ai = mrb_gc_arena_save(mrb);
246 mrb_value sub = argv[i];
247 mrb_ensure_string_type(mrb, sub);
248 mrb_gc_arena_restore(mrb, ai);
249 len_l = RSTRING_LEN(self);
250 len_r = RSTRING_LEN(sub);
251 if (len_l >= len_r) {
252 if (memcmp(RSTRING_PTR(self) + (len_l - len_r),
253 RSTRING_PTR(sub),
254 len_r) == 0) {
255 return mrb_true_value();
256 }
257 }
258 }
259 return mrb_false_value();
260 }
261
262 enum tr_pattern_type {
263 TR_UNINITIALIZED = 0,
264 TR_IN_ORDER = 1,
265 TR_RANGE = 2,
266 };
267
268 /*
269 #tr Pattern syntax
270
271 <syntax> ::= (<pattern>)* | '^' (<pattern>)*
272 <pattern> ::= <in order> | <range>
273 <in order> ::= (<ch>)+
274 <range> ::= <ch> '-' <ch>
275 */
276 struct tr_pattern {
277 uint8_t type; // 1:in-order, 2:range
278 mrb_bool flag_reverse : 1;
279 mrb_bool flag_on_heap : 1;
280 uint16_t n;
281 union {
282 uint16_t start_pos;
283 char ch[2];
284 } val;
285 struct tr_pattern *next;
286 };
287
288 #define STATIC_TR_PATTERN { 0 }
289
290 static inline void
291 tr_free_pattern(mrb_state *mrb, struct tr_pattern *pat)
292 {
293 while (pat) {
294 struct tr_pattern *p = pat->next;
295 if (pat->flag_on_heap) {
296 mrb_free(mrb, pat);
297 }
298 pat = p;
299 }
300 }
301
302 static struct tr_pattern*
303 tr_parse_pattern(mrb_state *mrb, struct tr_pattern *ret, const mrb_value v_pattern, mrb_bool flag_reverse_enable, struct tr_pattern *pat0)
304 {
305 const char *pattern = RSTRING_PTR(v_pattern);
306 mrb_int pattern_length = RSTRING_LEN(v_pattern);
307 mrb_bool flag_reverse = FALSE;
308 struct tr_pattern *pat1;
309 mrb_int i = 0;
310
311 if (flag_reverse_enable && pattern_length >= 2 && pattern[0] == '^') {
312 flag_reverse = TRUE;
313 i++;
314 }
315
316 while (i < pattern_length) {
317 /* is range pattern ? */
318 mrb_bool const ret_uninit = (ret->type == TR_UNINITIALIZED);
319 pat1 = ret_uninit
320 ? ret
321 : (struct tr_pattern*)mrb_malloc_simple(mrb, sizeof(struct tr_pattern));
322 if (pat1 == NULL) {
323 if (pat0) tr_free_pattern(mrb, pat0);
324 tr_free_pattern(mrb, ret);
325 mrb_exc_raise(mrb, mrb_obj_value(mrb->nomem_err));
326 return NULL; /* not reached */
327 }
328 if ((i+2) < pattern_length && pattern[i] != '\\' && pattern[i+1] == '-') {
329 pat1->type = TR_RANGE;
330 pat1->flag_reverse = flag_reverse;
331 pat1->flag_on_heap = !ret_uninit;
332 pat1->n = pattern[i+2] - pattern[i] + 1;
333 pat1->next = NULL;
334 pat1->val.ch[0] = pattern[i];
335 pat1->val.ch[1] = pattern[i+2];
336 i += 3;
337 }
338 else {
339 /* in order pattern. */
340 mrb_int start_pos = i++;
341 mrb_int len;
342
343 while (i < pattern_length) {
344 if ((i+2) < pattern_length && pattern[i] != '\\' && pattern[i+1] == '-')
345 break;
346 i++;
347 }
348
349 len = i - start_pos;
350 if (len > UINT16_MAX) {
351 if (pat0) tr_free_pattern(mrb, pat0);
352 tr_free_pattern(mrb, ret);
353 if (ret != pat1) mrb_free(mrb, pat1);
354 mrb_raise(mrb, E_ARGUMENT_ERROR, "tr pattern too long (max 65535)");
355 }
356 pat1->type = TR_IN_ORDER;
357 pat1->flag_reverse = flag_reverse;
358 pat1->flag_on_heap = !ret_uninit;
359 pat1->n = (uint16_t)len;
360 pat1->next = NULL;
361 pat1->val.start_pos = (uint16_t)start_pos;
362 }
363
364 if (!ret_uninit) {
365 struct tr_pattern *p = ret;
366 while (p->next != NULL) {
367 p = p->next;
368 }
369 p->next = pat1;
370 }
371 }
372
373 return ret;
374 }
375
376 static inline mrb_int
377 tr_find_character(const struct tr_pattern *pat, const char *pat_str, int ch)
378 {
379 mrb_int ret = -1;
380 mrb_int n_sum = 0;
381 mrb_int flag_reverse = pat ? pat->flag_reverse : 0;
382
383 while (pat != NULL) {
384 if (pat->type == TR_IN_ORDER) {
385 int i;
386 for (i = 0; i < pat->n; i++) {
387 if (pat_str[pat->val.start_pos + i] == ch) ret = n_sum + i;
388 }
389 }
390 else if (pat->type == TR_RANGE) {
391 if (pat->val.ch[0] <= ch && ch <= pat->val.ch[1])
392 ret = n_sum + ch - pat->val.ch[0];
393 }
394 else {
395 mrb_assert(pat->type == TR_UNINITIALIZED);
396 }
397 n_sum += pat->n;
398 pat = pat->next;
399 }
400
401 if (flag_reverse) {
402 return (ret < 0) ? MRB_INT_MAX : -1;
403 }
404 return ret;
405 }
406
407 static inline mrb_int
408 tr_get_character(const struct tr_pattern *pat, const char *pat_str, mrb_int n_th)
409 {
410 mrb_int n_sum = 0;
411
412 while (pat != NULL) {
413 if (n_th < (n_sum + pat->n)) {
414 mrb_int i = (n_th - n_sum);
415
416 switch (pat->type) {
417 case TR_IN_ORDER:
418 return pat_str[pat->val.start_pos + i];
419 case TR_RANGE:
420 return pat->val.ch[0]+i;
421 case TR_UNINITIALIZED:
422 return -1;
423 }
424 }
425 if (pat->next == NULL) {
426 switch (pat->type) {
427 case TR_IN_ORDER:
428 return pat_str[pat->val.start_pos + pat->n - 1];
429 case TR_RANGE:
430 return pat->val.ch[1];
431 case TR_UNINITIALIZED:
432 return -1;
433 }
434 }
435 n_sum += pat->n;
436 pat = pat->next;
437 }
438
439 return -1;
440 }
441
442 static inline void
443 tr_bitmap_set(uint8_t bitmap[32], uint8_t ch)
444 {
445 uint8_t idx1 = ch / 8;
446 uint8_t idx2 = ch % 8;
447 bitmap[idx1] |= (1<<idx2);
448 }
449
450 static inline mrb_bool
451 tr_bitmap_detect(uint8_t bitmap[32], uint8_t ch)
452 {
453 uint8_t idx1 = ch / 8;
454 uint8_t idx2 = ch % 8;
455 if (bitmap[idx1] & (1<<idx2))
456 return TRUE;
457 return FALSE;
458 }
459
460 /* compile pattern to bitmap */
461 static void
462 tr_compile_pattern(const struct tr_pattern *pat, mrb_value pstr, uint8_t bitmap[32])
463 {
464 const char *pattern = RSTRING_PTR(pstr);
465 mrb_int flag_reverse = pat ? pat->flag_reverse : 0;
466 int i;
467
468 for (i=0; i<32; i++) {
469 bitmap[i] = 0;
470 }
471 while (pat != NULL) {
472 if (pat->type == TR_IN_ORDER) {
473 for (i = 0; i < pat->n; i++) {
474 tr_bitmap_set(bitmap, pattern[pat->val.start_pos + i]);
475 }
476 }
477 else if (pat->type == TR_RANGE) {
478 for (i = pat->val.ch[0]; i < pat->val.ch[1]; i++) {
479 tr_bitmap_set(bitmap, i);
480 }
481 }
482 else {
483 mrb_assert(pat->type == TR_UNINITIALIZED);
484 }
485 pat = pat->next;
486 }
487
488 if (flag_reverse) {
489 for (i=0; i<32; i++) {
490 bitmap[i] ^= 0xff;
491 }
492 }
493 }
494
495 static mrb_bool
496 str_tr(mrb_state *mrb, mrb_value str, mrb_value p1, mrb_value p2, mrb_bool squeeze)
497 {
498 struct tr_pattern pat = STATIC_TR_PATTERN;
499 struct tr_pattern rep = STATIC_TR_PATTERN;
500 char *s;
501 mrb_int len;
502 mrb_int i;
503 mrb_int j;
504 mrb_bool flag_changed = FALSE;
505 mrb_int lastch = -1;
506
507 mrb_str_modify(mrb, mrb_str_ptr(str));
508 tr_parse_pattern(mrb, &pat, p1, TRUE, NULL);
509 tr_parse_pattern(mrb, &rep, p2, FALSE, &pat);
510 s = RSTRING_PTR(str);
511 len = RSTRING_LEN(str);
512
513 for (i=j=0; i<len; i++,j++) {
514 mrb_int n = tr_find_character(&pat, RSTRING_PTR(p1), s[i]);
515
516 if (i>j) s[j] = s[i];
517 if (n >= 0) {
518 flag_changed = TRUE;
519 mrb_int c = tr_get_character(&rep, RSTRING_PTR(p2), n);
520
521 if (c < 0 || (squeeze && c == lastch)) {
522 j--;
523 continue;
524 }
525 if (c > 0x80) {
526 tr_free_pattern(mrb, &pat);
527 tr_free_pattern(mrb, &rep);
528 mrb_raisef(mrb, E_ARGUMENT_ERROR, "character (%i) out of range", c);
529 }
530 lastch = c;
531 s[i] = (char)c;
532 }
533 }
534
535 tr_free_pattern(mrb, &pat);
536 tr_free_pattern(mrb, &rep);
537
538 if (flag_changed) {
539 RSTR_SET_LEN(RSTRING(str), j);
540 RSTRING_PTR(str)[j] = 0;
541 }
542 return flag_changed;
543 }
544
545 /*
546 * call-seq:
547 * str.tr(from_str, to_str) => new_str
548 *
549 * Returns a copy of str with the characters in from_str replaced by the
550 * corresponding characters in to_str. If to_str is shorter than from_str,
551 * it is padded with its last character in order to maintain the
552 * correspondence.
553 *
554 * "hello".tr('el', 'ip') #=> "hippo"
555 * "hello".tr('aeiou', '*') #=> "h*ll*"
556 * "hello".tr('aeiou', 'AA*') #=> "hAll*"
557 *
558 * Both strings may use the c1-c2 notation to denote ranges of characters,
559 * and from_str may start with a ^, which denotes all characters except
560 * those listed.
561 *
562 * "hello".tr('a-y', 'b-z') #=> "ifmmp"
563 * "hello".tr('^aeiou', '*') #=> "*e**o"
564 *
565 * The backslash character \ can be used to escape ^ or - and is otherwise
566 * ignored unless it appears at the end of a range or the end of the
567 * from_str or to_str:
568 *
569 *
570 * "hello^world".tr("\\^aeiou", "*") #=> "h*ll**w*rld"
571 * "hello-world".tr("a\\-eo", "*") #=> "h*ll**w*rld"
572 *
573 * "hello\r\nworld".tr("\r", "") #=> "hello\nworld"
574 * "hello\r\nworld".tr("\\r", "") #=> "hello\r\nwold"
575 * "hello\r\nworld".tr("\\\r", "") #=> "hello\nworld"
576 *
577 * "X['\\b']".tr("X\\", "") #=> "['b']"
578 * "X['\\b']".tr("X-\\]", "") #=> "'b'"
579 *
580 * Note: conversion is effective only in ASCII region.
581 */
582 static mrb_value
583 str_tr_m(mrb_state *mrb, mrb_value str)
584 {
585 mrb_value dup;
586 mrb_value p1, p2;
587
588 mrb_get_args(mrb, "SS", &p1, &p2);
589 dup = mrb_str_dup(mrb, str);
590 str_tr(mrb, dup, p1, p2, FALSE);
591 return dup;
592 }
593
594 /*
595 * call-seq:
596 * str.tr!(from_str, to_str) -> str or nil
597 *
598 * Translates str in place, using the same rules as String#tr.
599 * Returns str, or nil if no changes were made.
600 */
601 static mrb_value
602 str_tr_bang(mrb_state *mrb, mrb_value str)
603 {
604 mrb_value p1, p2;
605
606 mrb_get_args(mrb, "SS", &p1, &p2);
607 if (str_tr(mrb, str, p1, p2, FALSE)) {
608 return str;
609 }
610 return mrb_nil_value();
611 }
612
613 /*
614 * call-seq:
615 * str.tr_s(from_str, to_str) -> new_str
616 *
617 * Processes a copy of str as described under String#tr, then removes
618 * duplicate characters in regions that were affected by the translation.
619 *
620 * "hello".tr_s('l', 'r') #=> "hero"
621 * "hello".tr_s('el', '*') #=> "h*o"
622 * "hello".tr_s('el', 'hx') #=> "hhxo"
623 */
624 static mrb_value
625 str_tr_s(mrb_state *mrb, mrb_value str)
626 {
627 mrb_value dup;
628 mrb_value p1, p2;
629
630 mrb_get_args(mrb, "SS", &p1, &p2);
631 dup = mrb_str_dup(mrb, str);
632 str_tr(mrb, dup, p1, p2, TRUE);
633 return dup;
634 }
635
636 /*
637 * call-seq:
638 * str.tr_s!(from_str, to_str) -> str or nil
639 *
640 * Performs String#tr_s processing on str in place, returning
641 * str, or nil if no changes were made.
642 */
643 static mrb_value
644 str_tr_s_bang(mrb_state *mrb, mrb_value str)
645 {
646 mrb_value p1, p2;
647
648 mrb_get_args(mrb, "SS", &p1, &p2);
649 if (str_tr(mrb, str, p1, p2, TRUE)) {
650 return str;
651 }
652 return mrb_nil_value();
653 }
654
655 static mrb_bool
656 str_squeeze(mrb_state *mrb, mrb_value str, mrb_value v_pat)
657 {
658 struct tr_pattern pat_storage = STATIC_TR_PATTERN;
659 struct tr_pattern *pat = NULL;
660 mrb_int i, j;
661 char *s;
662 mrb_int len;
663 mrb_bool flag_changed = FALSE;
664 mrb_int lastch = -1;
665 uint8_t bitmap[32];
666
667 mrb_str_modify(mrb, mrb_str_ptr(str));
668 if (!mrb_nil_p(v_pat)) {
669 pat = tr_parse_pattern(mrb, &pat_storage, v_pat, TRUE, NULL);
670 tr_compile_pattern(pat, v_pat, bitmap);
671 tr_free_pattern(mrb, pat);
672 }
673 s = RSTRING_PTR(str);
674 len = RSTRING_LEN(str);
675
676 if (pat) {
677 for (i=j=0; i<len; i++,j++) {
678 if (i>j) s[j] = s[i];
679 if (tr_bitmap_detect(bitmap, s[i]) && s[i] == lastch) {
680 flag_changed = TRUE;
681 j--;
682 }
683 lastch = s[i];
684 }
685 }
686 else {
687 for (i=j=0; i<len; i++,j++) {
688 if (i>j) s[j] = s[i];
689 if (s[i] >= 0 && s[i] == lastch) {
690 flag_changed = TRUE;
691 j--;
692 }
693 lastch = s[i];
694 }
695 }
696
697 if (flag_changed) {
698 RSTR_SET_LEN(RSTRING(str), j);
699 RSTRING_PTR(str)[j] = 0;
700 }
701 return flag_changed;
702 }
703
704 /*
705 * call-seq:
706 * str.squeeze([other_str]) -> new_str
707 *
708 * Builds a set of characters from the other_str
709 * parameter(s) using the procedure described for String#count. Returns a
710 * new string where runs of the same character that occur in this set are
711 * replaced by a single character. If no arguments are given, all runs of
712 * identical characters are replaced by a single character.
713 *
714 * "yellow moon".squeeze #=> "yelow mon"
715 * " now is the".squeeze(" ") #=> " now is the"
716 * "putters shoot balls".squeeze("m-z") #=> "puters shot balls"
717 */
718 static mrb_value
719 str_squeeze_m(mrb_state *mrb, mrb_value str)
720 {
721 mrb_value pat = mrb_nil_value();
722 mrb_value dup;
723
724 mrb_get_args(mrb, "|S", &pat);
725 dup = mrb_str_dup(mrb, str);
726 str_squeeze(mrb, dup, pat);
727 return dup;
728 }
729
730 /*
731 * call-seq:
732 * str.squeeze!([other_str]) -> str or nil
733 *
734 * Squeezes str in place, returning either str, or nil if no
735 * changes were made.
736 */
737 static mrb_value
738 str_squeeze_bang(mrb_state *mrb, mrb_value str)
739 {
740 mrb_value pat = mrb_nil_value();
741
742 mrb_get_args(mrb, "|S", &pat);
743 if (str_squeeze(mrb, str, pat)) {
744 return str;
745 }
746 return mrb_nil_value();
747 }
748
749 static mrb_bool
750 str_delete(mrb_state *mrb, mrb_value str, mrb_value v_pat)
751 {
752 struct tr_pattern pat = STATIC_TR_PATTERN;
753 mrb_int i, j;
754 char *s;
755 mrb_int len;
756 mrb_bool flag_changed = FALSE;
757 uint8_t bitmap[32];
758
759 mrb_str_modify(mrb, mrb_str_ptr(str));
760 tr_parse_pattern(mrb, &pat, v_pat, TRUE, NULL);
761 tr_compile_pattern(&pat, v_pat, bitmap);
762 tr_free_pattern(mrb, &pat);
763
764 s = RSTRING_PTR(str);
765 len = RSTRING_LEN(str);
766
767 for (i=j=0; i<len; i++,j++) {
768 if (i>j) s[j] = s[i];
769 if (tr_bitmap_detect(bitmap, s[i])) {
770 flag_changed = TRUE;
771 j--;
772 }
773 }
774 if (flag_changed) {
775 RSTR_SET_LEN(RSTRING(str), j);
776 RSTRING_PTR(str)[j] = 0;
777 }
778 return flag_changed;
779 }
780
781 static mrb_value
782 str_delete_m(mrb_state *mrb, mrb_value str)
783 {
784 mrb_value pat;
785 mrb_value dup;
786
787 mrb_get_args(mrb, "S", &pat);
788 dup = mrb_str_dup(mrb, str);
789 str_delete(mrb, dup, pat);
790 return dup;
791 }
792
793 static mrb_value
794 str_delete_bang(mrb_state *mrb, mrb_value str)
795 {
796 mrb_value pat;
797
798 mrb_get_args(mrb, "S", &pat);
799 if (str_delete(mrb, str, pat)) {
800 return str;
801 }
802 return mrb_nil_value();
803 }
804
805 /*
806 * call_seq:
807 * str.count([other_str]) -> integer
808 *
809 * Each other_str parameter defines a set of characters to count. The
810 * intersection of these sets defines the characters to count in str. Any
811 * other_str that starts with a caret ^ is negated. The sequence c1-c2
812 * means all characters between c1 and c2. The backslash character \ can
813 * be used to escape ^ or - and is otherwise ignored unless it appears at
814 * the end of a sequence or the end of a other_str.
815 */
816 static mrb_value
817 str_count(mrb_state *mrb, mrb_value str)
818 {
819 mrb_value v_pat = mrb_nil_value();
820 mrb_int i;
821 char *s;
822 mrb_int len;
823 mrb_int count = 0;
824 struct tr_pattern pat = STATIC_TR_PATTERN;
825 uint8_t bitmap[32];
826
827 mrb_get_args(mrb, "S", &v_pat);
828 tr_parse_pattern(mrb, &pat, v_pat, TRUE, NULL);
829 tr_compile_pattern(&pat, v_pat, bitmap);
830 tr_free_pattern(mrb, &pat);
831
832 s = RSTRING_PTR(str);
833 len = RSTRING_LEN(str);
834 for (i = 0; i < len; i++) {
835 if (tr_bitmap_detect(bitmap, s[i])) count++;
836 }
837 return mrb_fixnum_value(count);
838 }
839
840 static mrb_value
841 str_hex(mrb_state *mrb, mrb_value self)
842 {
843 return mrb_str_to_integer(mrb, self, 16, FALSE);
844 }
845
846 static mrb_value
847 str_oct(mrb_state *mrb, mrb_value self)
848 {
849 return mrb_str_to_integer(mrb, self, 8, FALSE);
850 }
851
852 /*
853 * call-seq:
854 * string.chr -> string
855 *
856 * Returns a one-character string at the beginning of the string.
857 *
858 * a = "abcde"
859 * a.chr #=> "a"
860 */
861 static mrb_value
862 str_chr(mrb_state *mrb, mrb_value self)
863 {
864 return mrb_str_substr(mrb, self, 0, 1);
865 }
866
867 /*
868 * call-seq:
869 * int.chr([encoding]) -> string
870 *
871 * Returns a string containing the character represented by the +int+'s value
872 * according to +encoding+. +"ASCII-8BIT"+ (+"BINARY"+) and +"UTF-8"+ (only
873 * with +MRB_UTF8_STRING+) can be specified as +encoding+ (default is
874 * +"ASCII-8BIT"+).
875 *
876 * 65.chr #=> "A"
877 * 230.chr #=> "\xE6"
878 * 230.chr("ASCII-8BIT") #=> "\xE6"
879 * 230.chr("UTF-8") #=> "\u00E6"
880 */
881 static mrb_value
882 int_chr(mrb_state *mrb, mrb_value num)
883 {
884 mrb_value enc;
885 mrb_bool enc_given;
886
887 mrb_get_args(mrb, "|S?", &enc, &enc_given);
888 if (!enc_given ||
889 ENC_COMP_P(enc, ENC_ASCII_8BIT) ||
890 ENC_COMP_P(enc, ENC_BINARY)) {
891 return int_chr_binary(mrb, num);
892 }
893 #ifdef MRB_UTF8_STRING
894 else if (ENC_COMP_P(enc, ENC_UTF8)) {
895 return int_chr_utf8(mrb, num);
896 }
897 #endif
898 else {
899 mrb_raisef(mrb, E_ARGUMENT_ERROR, "unknown encoding name - %v", enc);
900 }
901 /* not reached */
902 return mrb_nil_value();
903 }
904
905 /*
906 * call-seq:
907 * string.succ -> string
908 *
909 * Returns next sequence of the string;
910 *
911 * a = "abc"
912 * a.succ #=> "abd"
913 */
914 static mrb_value
915 str_succ_bang(mrb_state *mrb, mrb_value self)
916 {
917 mrb_value result;
918 unsigned char *p, *e, *b, *t;
919 const char *prepend;
920 struct RString *s = mrb_str_ptr(self);
921 mrb_int l;
922
923 if (RSTRING_LEN(self) == 0)
924 return self;
925
926 mrb_str_modify(mrb, s);
927 l = RSTRING_LEN(self);
928 b = p = (unsigned char*) RSTRING_PTR(self);
929 t = e = p + l;
930 *(e--) = 0;
931
932 // find trailing ascii/number
933 while (e >= b) {
934 if (ISALNUM(*e))
935 break;
936 e--;
937 }
938 if (e < b) {
939 e = p + l - 1;
940 result = mrb_str_new_lit(mrb, "");
941 }
942 else {
943 // find leading letter of the ascii/number
944 b = e;
945 while (b > p) {
946 if (!ISALNUM(*b) || (ISALNUM(*b) && *b != '9' && *b != 'z' && *b != 'Z'))
947 break;
948 b--;
949 }
950 if (!ISALNUM(*b))
951 b++;
952 result = mrb_str_new(mrb, (char*) p, b - p);
953 }
954
955 while (e >= b) {
956 if (!ISALNUM(*e)) {
957 if (*e == 0xff) {
958 mrb_str_cat_lit(mrb, result, "\x01");
959 (*e) = 0;
960 }
961 else
962 (*e)++;
963 break;
964 }
965 prepend = NULL;
966 if (*e == '9') {
967 if (e == b) prepend = "1";
968 *e = '0';
969 }
970 else if (*e == 'z') {
971 if (e == b) prepend = "a";
972 *e = 'a';
973 }
974 else if (*e == 'Z') {
975 if (e == b) prepend = "A";
976 *e = 'A';
977 }
978 else {
979 (*e)++;
980 break;
981 }
982 if (prepend) mrb_str_cat_cstr(mrb, result, prepend);
983 e--;
984 }
985 result = mrb_str_cat(mrb, result, (char*) b, t - b);
986 l = RSTRING_LEN(result);
987 mrb_str_resize(mrb, self, l);
988 memcpy(RSTRING_PTR(self), RSTRING_PTR(result), l);
989 return self;
990 }
991
992 static mrb_value
993 str_succ(mrb_state *mrb, mrb_value self)
994 {
995 mrb_value str;
996
997 str = mrb_str_dup(mrb, self);
998 str_succ_bang(mrb, str);
999 return str;
1000 }
1001
1002 #ifdef MRB_UTF8_STRING
1003 extern const char mrb_utf8len_table[];
1004
1005 MRB_INLINE mrb_int
1006 utf8code(mrb_state* mrb, const unsigned char* p, const unsigned char *e)
1007 {
1008 mrb_int len;
1009
1010 if (p[0] < 0x80)
1011 return p[0];
1012
1013 len = mrb_utf8len_table[p[0]>>3];
1014 if (p+len <= e && len > 1 && (p[1] & 0xc0) == 0x80) {
1015 if (len == 2)
1016 return ((p[0] & 0x1f) << 6) + (p[1] & 0x3f);
1017 if ((p[2] & 0xc0) == 0x80) {
1018 if (len == 3)
1019 return ((p[0] & 0x0f) << 12) + ((p[1] & 0x3f) << 6)
1020 + (p[2] & 0x3f);
1021 if ((p[3] & 0xc0) == 0x80) {
1022 if (len == 4)
1023 return ((p[0] & 0x07) << 18) + ((p[1] & 0x3f) << 12)
1024 + ((p[2] & 0x3f) << 6) + (p[3] & 0x3f);
1025 }
1026 }
1027 }
1028 mrb_raise(mrb, E_ARGUMENT_ERROR, "invalid UTF-8 byte sequence");
1029 /* not reached */
1030 return -1;
1031 }
1032
1033 static mrb_value
1034 str_ord(mrb_state* mrb, mrb_value str)
1035 {
1036 if (RSTRING_LEN(str) == 0)
1037 mrb_raise(mrb, E_ARGUMENT_ERROR, "empty string");
1038 const unsigned char *p = (unsigned char*)RSTRING_PTR(str);
1039 const unsigned char *e = p + RSTRING_LEN(str);
1040
1041 mrb_int c = utf8code(mrb, p, e);
1042 return mrb_fixnum_value(c);
1043 }
1044
1045 static mrb_value
1046 str_codepoints(mrb_state *mrb, mrb_value str)
1047 {
1048 mrb_value result;
1049 const unsigned char *p = (unsigned char*)RSTRING_PTR(str);
1050 const unsigned char *e = p + RSTRING_LEN(str);
1051
1052 mrb->c->ci->mid = 0;
1053 result = mrb_ary_new(mrb);
1054 while (p < e) {
1055 mrb_int c = utf8code(mrb, p, e);
1056 mrb_ary_push(mrb, result, mrb_int_value(mrb, c));
1057 p += mrb_utf8len_table[p[0]>>3];
1058 }
1059 return result;
1060 }
1061 #else
1062 static mrb_value
1063 str_ord(mrb_state* mrb, mrb_value str)
1064 {
1065 if (RSTRING_LEN(str) == 0)
1066 mrb_raise(mrb, E_ARGUMENT_ERROR, "empty string");
1067 return mrb_fixnum_value((unsigned char)RSTRING_PTR(str)[0]);
1068 }
1069
1070 static mrb_value
1071 str_codepoints(mrb_state *mrb, mrb_value self)
1072 {
1073 mrb_value result;
1074 char *p = RSTRING_PTR(self);
1075 char *e = p + RSTRING_LEN(self);
1076
1077 mrb->c->ci->mid = 0;
1078 result = mrb_ary_new(mrb);
1079 while (p < e) {
1080 mrb_ary_push(mrb, result, mrb_int_value(mrb, (mrb_int)*p));
1081 p++;
1082 }
1083 return result;
1084 }
1085 #endif
1086
1087 /*
1088 * call-seq:
1089 * str.delete_prefix!(prefix) -> self or nil
1090 *
1091 * Deletes leading <code>prefix</code> from <i>str</i>, returning
1092 * <code>nil</code> if no change was made.
1093 *
1094 * "hello".delete_prefix!("hel") #=> "lo"
1095 * "hello".delete_prefix!("llo") #=> nil
1096 */
1097 static mrb_value
1098 str_del_prefix_bang(mrb_state *mrb, mrb_value self)
1099 {
1100 mrb_int plen, slen;
1101 const char *ptr;
1102 char *s;
1103 struct RString *str = RSTRING(self);
1104
1105 mrb_get_args(mrb, "s", &ptr, &plen);
1106 slen = RSTR_LEN(str);
1107 if (plen > slen) return mrb_nil_value();
1108 s = RSTR_PTR(str);
1109 if (memcmp(s, ptr, plen) != 0) return mrb_nil_value();
1110 if (!mrb_frozen_p(str) && (RSTR_SHARED_P(str) || RSTR_FSHARED_P(str))) {
1111 str->as.heap.ptr += plen;
1112 }
1113 else {
1114 mrb_str_modify(mrb, str);
1115 s = RSTR_PTR(str);
1116 memmove(s, s+plen, slen-plen);
1117 }
1118 RSTR_SET_LEN(str, slen-plen);
1119 return self;
1120 }
1121
1122 /*
1123 * call-seq:
1124 * str.delete_prefix(prefix) -> new_str
1125 *
1126 * Returns a copy of <i>str</i> with leading <code>prefix</code> deleted.
1127 *
1128 * "hello".delete_prefix("hel") #=> "lo"
1129 * "hello".delete_prefix("llo") #=> "hello"
1130 */
1131 static mrb_value
1132 str_del_prefix(mrb_state *mrb, mrb_value self)
1133 {
1134 mrb_int plen, slen;
1135 const char *ptr;
1136
1137 mrb_get_args(mrb, "s", &ptr, &plen);
1138 slen = RSTRING_LEN(self);
1139 if (plen > slen) return mrb_str_dup(mrb, self);
1140 if (memcmp(RSTRING_PTR(self), ptr, plen) != 0)
1141 return mrb_str_dup(mrb, self);
1142 return mrb_str_substr(mrb, self, plen, slen-plen);
1143 }
1144
1145 /*
1146 * call-seq:
1147 * str.delete_suffix!(suffix) -> self or nil
1148 *
1149 * Deletes trailing <code>suffix</code> from <i>str</i>, returning
1150 * <code>nil</code> if no change was made.
1151 *
1152 * "hello".delete_suffix!("llo") #=> "he"
1153 * "hello".delete_suffix!("hel") #=> nil
1154 */
1155 static mrb_value
1156 str_del_suffix_bang(mrb_state *mrb, mrb_value self)
1157 {
1158 mrb_int plen, slen;
1159 const char *ptr;
1160 char *s;
1161 struct RString *str = RSTRING(self);
1162
1163 mrb_get_args(mrb, "s", &ptr, &plen);
1164 slen = RSTR_LEN(str);
1165 if (plen > slen) return mrb_nil_value();
1166 s = RSTR_PTR(str);
1167 if (memcmp(s+slen-plen, ptr, plen) != 0) return mrb_nil_value();
1168 if (!mrb_frozen_p(str) && (RSTR_SHARED_P(str) || RSTR_FSHARED_P(str))) {
1169 /* no need to modify string */
1170 }
1171 else {
1172 mrb_str_modify(mrb, str);
1173 }
1174 RSTR_SET_LEN(str, slen-plen);
1175 return self;
1176 }
1177
1178 /*
1179 * call-seq:
1180 * str.delete_suffix(suffix) -> new_str
1181 *
1182 * Returns a copy of <i>str</i> with leading <code>suffix</code> deleted.
1183 *
1184 * "hello".delete_suffix("hel") #=> "lo"
1185 * "hello".delete_suffix("llo") #=> "hello"
1186 */
1187 static mrb_value
1188 str_del_suffix(mrb_state *mrb, mrb_value self)
1189 {
1190 mrb_int plen, slen;
1191 const char *ptr;
1192
1193 mrb_get_args(mrb, "s", &ptr, &plen);
1194 slen = RSTRING_LEN(self);
1195 if (plen > slen) return mrb_str_dup(mrb, self);
1196 if (memcmp(RSTRING_PTR(self)+slen-plen, ptr, plen) != 0)
1197 return mrb_str_dup(mrb, self);
1198 return mrb_str_substr(mrb, self, 0, slen-plen);
1199 }
1200
1201 #define lesser(a,b) (((a)>(b))?(b):(a))
1202
1203 /*
1204 * call-seq:
1205 * str.casecmp(other_str) -> -1, 0, +1 or nil
1206 *
1207 * Case-insensitive version of <code>String#<=></code>.
1208 *
1209 * "abcdef".casecmp("abcde") #=> 1
1210 * "aBcDeF".casecmp("abcdef") #=> 0
1211 * "abcdef".casecmp("abcdefg") #=> -1
1212 * "abcdef".casecmp("ABCDEF") #=> 0
1213 */
1214 static mrb_value
1215 str_casecmp(mrb_state *mrb, mrb_value self)
1216 {
1217 mrb_value str = mrb_get_arg1(mrb);
1218
1219 if (!mrb_string_p(str)) return mrb_nil_value();
1220
1221 struct RString *s1 = mrb_str_ptr(self);
1222 struct RString *s2 = mrb_str_ptr(str);
1223
1224 mrb_int len1 = RSTR_LEN(s1);
1225 mrb_int len2 = RSTR_LEN(s2);
1226 mrb_int len = lesser(len1, len2);
1227 char *p1 = RSTR_PTR(s1);
1228 char *p2 = RSTR_PTR(s2);
1229 if (p1 == p2) return mrb_fixnum_value(0);
1230
1231 for (mrb_int i=0; i<len; i++) {
1232 int c1 = p1[i], c2 = p2[i];
1233 if (ISASCII(c1) && ISUPPER(c1)) c1 = TOLOWER(c1);
1234 if (ISASCII(c2) && ISUPPER(c2)) c2 = TOLOWER(c2);
1235 if (c1 > c2) return mrb_fixnum_value(1);
1236 if (c1 < c2) return mrb_fixnum_value(-1);
1237 }
1238 if (len1 == len2) return mrb_fixnum_value(0);
1239 if (len1 > len2) return mrb_fixnum_value(1);
1240 return mrb_fixnum_value(-1);
1241 }
1242
1243 /*
1244 * call-seq:
1245 * str.casecmp?(other) -> true, false, or nil
1246 *
1247 * Returns true if str and other_str are equal after case folding,
1248 * false if they are not equal, and nil if other is not a string.
1249 */
1250 static mrb_value
1251 str_casecmp_p(mrb_state *mrb, mrb_value self)
1252 {
1253 mrb_value c = str_casecmp(mrb, self);
1254 if (mrb_nil_p(c)) return c;
1255 return mrb_bool_value(mrb_fixnum(c) == 0);
1256 }
1257
1258 static mrb_value
1259 str_lines(mrb_state *mrb, mrb_value self)
1260 {
1261 mrb_value result;
1262 int ai;
1263 mrb_int len;
1264 char *b = RSTRING_PTR(self);
1265 char *p = b, *t;
1266 char *e = b + RSTRING_LEN(self);
1267
1268 mrb->c->ci->mid = 0;
1269 result = mrb_ary_new(mrb);
1270 ai = mrb_gc_arena_save(mrb);
1271 while (p < e) {
1272 t = p;
1273 while (p < e && *p != '\n') p++;
1274 if (*p == '\n') p++;
1275 len = (mrb_int) (p - t);
1276 mrb_ary_push(mrb, result, mrb_str_new(mrb, t, len));
1277 mrb_gc_arena_restore(mrb, ai);
1278 }
1279 return result;
1280 }
1281
1282 /*
1283 * call-seq:
1284 * +string -> new_string or self
1285 *
1286 * Returns +self+ if +self+ is not frozen.
1287 *
1288 * Otherwise returns <tt>self.dup</tt>, which is not frozen.
1289 */
1290 static mrb_value
1291 str_uplus(mrb_state *mrb, mrb_value str)
1292 {
1293 if (mrb_frozen_p(mrb_obj_ptr(str))) {
1294 return mrb_str_dup(mrb, str);
1295 }
1296 else {
1297 return str;
1298 }
1299 }
1300
1301 /*
1302 * call-seq:
1303 * -string -> frozen_string
1304 *
1305 * Returns a frozen, possibly pre-existing copy of the string.
1306 *
1307 * The returned \String will be deduplicated as long as it does not have
1308 * any instance variables set on it and is not a String subclass.
1309 *
1310 * String#dedup is an alias for String#-@.
1311 */
1312 static mrb_value
1313 str_uminus(mrb_state *mrb, mrb_value str)
1314 {
1315 if (mrb_frozen_p(mrb_obj_ptr(str))) {
1316 return str;
1317 }
1318 return mrb_obj_freeze(mrb, mrb_str_dup(mrb, str));
1319 }
1320
1321 void
1322 mrb_mruby_string_ext_gem_init(mrb_state* mrb)
1323 {
1324 struct RClass * s = mrb->string_class;
1325
1326 mrb_define_method(mrb, s, "dump", mrb_str_dump, MRB_ARGS_NONE());
1327 mrb_define_method(mrb, s, "swapcase!", str_swapcase_bang, MRB_ARGS_NONE());
1328 mrb_define_method(mrb, s, "swapcase", str_swapcase, MRB_ARGS_NONE());
1329 mrb_define_method(mrb, s, "concat", str_concat_m, MRB_ARGS_REQ(1));
1330 mrb_define_method(mrb, s, "<<", str_concat_m, MRB_ARGS_REQ(1));
1331 mrb_define_method(mrb, s, "count", str_count, MRB_ARGS_REQ(1));
1332 mrb_define_method(mrb, s, "tr", str_tr_m, MRB_ARGS_REQ(2));
1333 mrb_define_method(mrb, s, "tr!", str_tr_bang, MRB_ARGS_REQ(2));
1334 mrb_define_method(mrb, s, "tr_s", str_tr_s, MRB_ARGS_REQ(2));
1335 mrb_define_method(mrb, s, "tr_s!", str_tr_s_bang, MRB_ARGS_REQ(2));
1336 mrb_define_method(mrb, s, "squeeze", str_squeeze_m, MRB_ARGS_OPT(1));
1337 mrb_define_method(mrb, s, "squeeze!", str_squeeze_bang, MRB_ARGS_OPT(1));
1338 mrb_define_method(mrb, s, "delete", str_delete_m, MRB_ARGS_REQ(1));
1339 mrb_define_method(mrb, s, "delete!", str_delete_bang, MRB_ARGS_REQ(1));
1340 mrb_define_method(mrb, s, "start_with?", str_start_with, MRB_ARGS_REST());
1341 mrb_define_method(mrb, s, "end_with?", str_end_with, MRB_ARGS_REST());
1342 mrb_define_method(mrb, s, "hex", str_hex, MRB_ARGS_NONE());
1343 mrb_define_method(mrb, s, "oct", str_oct, MRB_ARGS_NONE());
1344 mrb_define_method(mrb, s, "chr", str_chr, MRB_ARGS_NONE());
1345 mrb_define_method(mrb, s, "succ", str_succ, MRB_ARGS_NONE());
1346 mrb_define_method(mrb, s, "succ!", str_succ_bang, MRB_ARGS_NONE());
1347 mrb_define_method(mrb, s, "next", str_succ, MRB_ARGS_NONE());
1348 mrb_define_method(mrb, s, "next!", str_succ_bang, MRB_ARGS_NONE());
1349 mrb_define_method(mrb, s, "ord", str_ord, MRB_ARGS_NONE());
1350 mrb_define_method(mrb, s, "delete_prefix!", str_del_prefix_bang, MRB_ARGS_REQ(1));
1351 mrb_define_method(mrb, s, "delete_prefix", str_del_prefix, MRB_ARGS_REQ(1));
1352 mrb_define_method(mrb, s, "delete_suffix!", str_del_suffix_bang, MRB_ARGS_REQ(1));
1353 mrb_define_method(mrb, s, "delete_suffix", str_del_suffix, MRB_ARGS_REQ(1));
1354 mrb_define_method(mrb, s, "casecmp", str_casecmp, MRB_ARGS_REQ(1));
1355 mrb_define_method(mrb, s, "casecmp?", str_casecmp_p, MRB_ARGS_REQ(1));
1356 mrb_define_method(mrb, s, "+@", str_uplus, MRB_ARGS_REQ(1));
1357 mrb_define_method(mrb, s, "-@", str_uminus, MRB_ARGS_REQ(1));
1358
1359 mrb_define_method(mrb, s, "__lines", str_lines, MRB_ARGS_NONE());
1360 mrb_define_method(mrb, s, "__codepoints", str_codepoints, MRB_ARGS_NONE());
1361
1362 mrb_define_method(mrb, mrb->integer_class, "chr", int_chr, MRB_ARGS_OPT(1));
1363 }
1364
1365 void
1366 mrb_mruby_string_ext_gem_final(mrb_state* mrb)
1367 {
1368 }