summaryrefslogtreecommitdiff
path: root/string.c
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2012-12-25 22:31:21 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2012-12-25 22:31:21 +0000
commit73982fa7217038249bf2e538003cc524146f79c6 (patch)
tree591a13899f41b856d8955bdf59c07237a8024d01 /string.c
parentc8484fb3228eeac72497cd1003c2e601b767f785 (diff)
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
Diffstat (limited to 'string.c')
-rw-r--r--string.c20
1 files changed, 6 insertions, 14 deletions
diff --git a/string.c b/string.c
index e312c883e0..26af8b613f 100644
--- a/string.c
+++ b/string.c
@@ -6161,9 +6161,7 @@ rb_str_enumerate_lines(int argc, VALUE *argv, VALUE str, int wantarray)
continue;
}
p = p0 + rb_enc_mbclen(p0, pend, enc);
- line = rb_str_new5(str, s, p - s);
- OBJ_INFECT(line, str);
- rb_enc_cr_str_copy_for_substr(line, str);
+ line = rb_str_subseq(str, s - ptr, p - s);
if (wantarray)
rb_ary_push(ary, line);
else
@@ -6200,24 +6198,21 @@ rb_str_enumerate_lines(int argc, VALUE *argv, VALUE str, int wantarray)
if (c == newline &&
(rslen <= 1 ||
(pend - p >= rslen && memcmp(RSTRING_PTR(rs), p, rslen) == 0))) {
- line = rb_str_new5(str, s, p - s + (rslen ? rslen : n));
- OBJ_INFECT(line, str);
- rb_enc_cr_str_copy_for_substr(line, str);
+ p += (rslen ? rslen : n);
+ line = rb_str_subseq(str, s - ptr, p - s);
if (wantarray)
rb_ary_push(ary, line);
else
rb_yield(line);
str_mod_check(str, ptr, len);
- s = p + (rslen ? rslen : n);
+ s = p;
}
p += n;
}
finish:
if (s != pend) {
- line = rb_str_new5(str, s, pend - s);
- OBJ_INFECT(line, str);
- rb_enc_cr_str_copy_for_substr(line, str);
+ line = rb_str_subseq(str, s - ptr, pend - s);
if (wantarray)
rb_ary_push(ary, line);
else
@@ -6643,10 +6638,7 @@ rb_str_chop_bang(VALUE str)
static VALUE
rb_str_chop(VALUE str)
{
- VALUE str2 = rb_str_new5(str, RSTRING_PTR(str), chopped_length(str));
- rb_enc_cr_str_copy_for_substr(str2, str);
- OBJ_INFECT(str2, str);
- return str2;
+ return rb_str_subseq(str, 0, chopped_length(str));
}