string.c: reduce duplicated code
* string.c (rb_str_enumerate_lines, rb_str_chop): reduce duplicated code. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@38616 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
c8484fb322
commit
73982fa721
@ -1,4 +1,7 @@
|
|||||||
Wed Dec 26 07:31:14 2012 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
Wed Dec 26 07:31:19 2012 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
||||||
|
|
||||||
|
* string.c (rb_str_enumerate_lines, rb_str_chop): reduce duplicated
|
||||||
|
code.
|
||||||
|
|
||||||
* string.c (rb_str_enumerate_chars): prevent shared copy from GC.
|
* string.c (rb_str_enumerate_chars): prevent shared copy from GC.
|
||||||
|
|
||||||
|
20
string.c
20
string.c
@ -6161,9 +6161,7 @@ rb_str_enumerate_lines(int argc, VALUE *argv, VALUE str, int wantarray)
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
p = p0 + rb_enc_mbclen(p0, pend, enc);
|
p = p0 + rb_enc_mbclen(p0, pend, enc);
|
||||||
line = rb_str_new5(str, s, p - s);
|
line = rb_str_subseq(str, s - ptr, p - s);
|
||||||
OBJ_INFECT(line, str);
|
|
||||||
rb_enc_cr_str_copy_for_substr(line, str);
|
|
||||||
if (wantarray)
|
if (wantarray)
|
||||||
rb_ary_push(ary, line);
|
rb_ary_push(ary, line);
|
||||||
else
|
else
|
||||||
@ -6200,24 +6198,21 @@ rb_str_enumerate_lines(int argc, VALUE *argv, VALUE str, int wantarray)
|
|||||||
if (c == newline &&
|
if (c == newline &&
|
||||||
(rslen <= 1 ||
|
(rslen <= 1 ||
|
||||||
(pend - p >= rslen && memcmp(RSTRING_PTR(rs), p, rslen) == 0))) {
|
(pend - p >= rslen && memcmp(RSTRING_PTR(rs), p, rslen) == 0))) {
|
||||||
line = rb_str_new5(str, s, p - s + (rslen ? rslen : n));
|
p += (rslen ? rslen : n);
|
||||||
OBJ_INFECT(line, str);
|
line = rb_str_subseq(str, s - ptr, p - s);
|
||||||
rb_enc_cr_str_copy_for_substr(line, str);
|
|
||||||
if (wantarray)
|
if (wantarray)
|
||||||
rb_ary_push(ary, line);
|
rb_ary_push(ary, line);
|
||||||
else
|
else
|
||||||
rb_yield(line);
|
rb_yield(line);
|
||||||
str_mod_check(str, ptr, len);
|
str_mod_check(str, ptr, len);
|
||||||
s = p + (rslen ? rslen : n);
|
s = p;
|
||||||
}
|
}
|
||||||
p += n;
|
p += n;
|
||||||
}
|
}
|
||||||
|
|
||||||
finish:
|
finish:
|
||||||
if (s != pend) {
|
if (s != pend) {
|
||||||
line = rb_str_new5(str, s, pend - s);
|
line = rb_str_subseq(str, s - ptr, pend - s);
|
||||||
OBJ_INFECT(line, str);
|
|
||||||
rb_enc_cr_str_copy_for_substr(line, str);
|
|
||||||
if (wantarray)
|
if (wantarray)
|
||||||
rb_ary_push(ary, line);
|
rb_ary_push(ary, line);
|
||||||
else
|
else
|
||||||
@ -6643,10 +6638,7 @@ rb_str_chop_bang(VALUE str)
|
|||||||
static VALUE
|
static VALUE
|
||||||
rb_str_chop(VALUE str)
|
rb_str_chop(VALUE str)
|
||||||
{
|
{
|
||||||
VALUE str2 = rb_str_new5(str, RSTRING_PTR(str), chopped_length(str));
|
return rb_str_subseq(str, 0, chopped_length(str));
|
||||||
rb_enc_cr_str_copy_for_substr(str2, str);
|
|
||||||
OBJ_INFECT(str2, str);
|
|
||||||
return str2;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user