diff options
-rw-r--r-- | ChangeLog | 5 | ||||
-rw-r--r-- | string.c | 17 |
2 files changed, 19 insertions, 3 deletions
@@ -1,3 +1,8 @@ +Sun Jan 20 13:06:01 2008 Nobuyoshi Nakada <nobu@ruby-lang.org> + + * string.c (rb_str_each_char): performance improvement, and stop if + shortened in the block. [ruby-dev:33189] + Sun Jan 20 09:12:26 2008 NARUSE, Yui <naruse@ruby-lang.org> * common.mk: use -Ks when read insns.def. [ruby-dev#33185] @@ -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; } |