From 0780974482103f4b681f699b54068b9fe9eb5043 Mon Sep 17 00:00:00 2001 From: glass Date: Mon, 22 Jul 2013 09:42:15 +0000 Subject: * string.c (rb_str_enumerate_chars): specify array capa with str_strlen(). * string.c (rb_str_enumerate_codepoints): ditto. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@42113 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 7 +++++++ string.c | 26 ++++++++++++++------------ 2 files changed, 21 insertions(+), 12 deletions(-) diff --git a/ChangeLog b/ChangeLog index baa929f68a..3d0933f6cf 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,10 @@ +Mon Jul 22 18:39:52 2013 Masaki Matsushita + + * string.c (rb_str_enumerate_chars): specify array capa + with str_strlen(). + + * string.c (rb_str_enumerate_codepoints): ditto. + Mon Jul 22 18:01:33 2013 Masaki Matsushita * string.c (rb_str_enumerate_chars): specify array capa. diff --git a/string.c b/string.c index a4c76d4f7b..f101a94c32 100644 --- a/string.c +++ b/string.c @@ -6514,11 +6514,16 @@ rb_str_enumerate_chars(VALUE str, int wantarray) rb_encoding *enc; VALUE UNINITIALIZED_VAR(ary); + str = rb_str_new4(str); + ptr = RSTRING_PTR(str); + len = RSTRING_LEN(str); + enc = rb_enc_get(str); + if (rb_block_given_p()) { if (wantarray) { #if STRING_ENUMERATORS_WANTARRAY rb_warn("given block not used"); - ary = rb_ary_new_capa(rb_str_strlen(str)); + ary = rb_ary_new_capa(str_strlen(str, enc)); #else rb_warning("passing a block to String#chars is deprecated"); wantarray = 0; @@ -6527,15 +6532,11 @@ rb_str_enumerate_chars(VALUE str, int wantarray) } else { if (wantarray) - ary = rb_ary_new_capa(rb_str_strlen(str)); + ary = rb_ary_new_capa(str_strlen(str, enc)); else RETURN_SIZED_ENUMERATOR(str, 0, 0, rb_str_each_char_size); } - str = rb_str_new4(str); - ptr = RSTRING_PTR(str); - len = RSTRING_LEN(str); - enc = rb_enc_get(str); switch (ENC_CODERANGE(str)) { case ENC_CODERANGE_VALID: case ENC_CODERANGE_7BIT: @@ -6617,11 +6618,16 @@ rb_str_enumerate_codepoints(VALUE str, int wantarray) if (single_byte_optimizable(str)) return rb_str_enumerate_bytes(str, wantarray); + str = rb_str_new4(str); + ptr = RSTRING_PTR(str); + end = RSTRING_END(str); + enc = STR_ENC_GET(str); + if (rb_block_given_p()) { if (wantarray) { #if STRING_ENUMERATORS_WANTARRAY rb_warn("given block not used"); - ary = rb_ary_new(); + ary = rb_ary_new_capa(str_strlen(str, enc)); #else rb_warning("passing a block to String#codepoints is deprecated"); wantarray = 0; @@ -6630,15 +6636,11 @@ rb_str_enumerate_codepoints(VALUE str, int wantarray) } else { if (wantarray) - ary = rb_ary_new(); + ary = rb_ary_new_capa(str_strlen(str, enc)); else RETURN_SIZED_ENUMERATOR(str, 0, 0, rb_str_each_char_size); } - str = rb_str_new4(str); - ptr = RSTRING_PTR(str); - end = RSTRING_END(str); - enc = STR_ENC_GET(str); while (ptr < end) { c = rb_enc_codepoint_len(ptr, end, &n, enc); if (wantarray) -- cgit v1.2.3