summaryrefslogtreecommitdiffstats
path: root/string.c
diff options
context:
space:
mode:
Diffstat (limited to 'string.c')
-rw-r--r--string.c17
1 files changed, 14 insertions, 3 deletions
diff --git a/string.c b/string.c
index ba46a7b9c..9242877a3 100644
--- a/string.c
+++ b/string.c
@@ -4590,11 +4590,22 @@ rb_str_each_byte(VALUE str)
static VALUE
rb_str_each_char(VALUE str)
{
- int i, len = str_strlen(str, 0);
+ int i, len, n;
+ const char *ptr, *s;
+ rb_encoding *enc;
RETURN_ENUMERATOR(str, 0, 0);
- for (i=0; i<len; i++) {
- rb_yield(rb_str_substr(str, i, 1));
+ ptr = RSTRING_PTR(str);
+ len = RSTRING_LEN(str);
+ enc = rb_enc_get(str);
+ n = rb_enc_mbclen(ptr, ptr + len, enc);
+ for (i = 0; i < len; i += n) {
+ rb_yield(rb_str_subseq(str, i, n));
+ ptr = RSTRING_PTR(str);
+ len = RSTRING_LEN(str);
+ enc = rb_enc_get(str);
+ s = rb_enc_left_char_head(ptr, ptr + i, enc);
+ n = rb_enc_mbclen(s, ptr + len, enc);
}
return str;
}