1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* string.c (rb_str_enumerate_lines): make String#each_line and

#lines not raise invalid byte sequence error when it is called
  with an argument. The patch also causes performance improvement.
  [ruby-dev:47549] [Bug #8698]

* test/ruby/test_m17n_comb.rb (test_str_each_line): remove
  assertions which check that String#each_line and #lines will
  raise an error if the receiver includes invalid byte sequence.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@42966 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
glass 2013-09-18 14:34:04 +00:00
parent 47beb63fe2
commit 81629f0531
3 changed files with 60 additions and 71 deletions

View file

@ -1,3 +1,14 @@
Wed Sep 18 23:14:58 2013 Masaki Matsushita <glass.saga@gmail.com>
* string.c (rb_str_enumerate_lines): make String#each_line and
#lines not raise invalid byte sequence error when it is called
with an argument. The patch also causes performance improvement.
[ruby-dev:47549] [Bug #8698]
* test/ruby/test_m17n_comb.rb (test_str_each_line): remove
assertions which check that String#each_line and #lines will
raise an error if the receiver includes invalid byte sequence.
Wed Sep 18 16:32:15 2013 Nobuyoshi Nakada <nobu@ruby-lang.org> Wed Sep 18 16:32:15 2013 Nobuyoshi Nakada <nobu@ruby-lang.org>
* proc.c (mnew_from_me): allocate structs after allocated wrapper * proc.c (mnew_from_me): allocate structs after allocated wrapper

115
string.c
View file

@ -6352,21 +6352,17 @@ static VALUE
rb_str_enumerate_lines(int argc, VALUE *argv, VALUE str, int wantarray) rb_str_enumerate_lines(int argc, VALUE *argv, VALUE str, int wantarray)
{ {
rb_encoding *enc; rb_encoding *enc;
VALUE rs; VALUE line, rs, orig = str;
unsigned int newline; const char *ptr, *pend, *subptr, *subend, *rsptr, *hit, *adjusted;
const char *p, *pend, *s, *ptr; long pos, len, rslen;
long len, rslen; int paragraph_mode = 0;
VALUE line;
int n;
VALUE orig = str;
VALUE UNINITIALIZED_VAR(ary); VALUE UNINITIALIZED_VAR(ary);
if (argc == 0) { if (argc == 0)
rs = rb_rs; rs = rb_rs;
} else
else {
rb_scan_args(argc, argv, "01", &rs); rb_scan_args(argc, argv, "01", &rs);
}
if (rb_block_given_p()) { if (rb_block_given_p()) {
if (wantarray) { if (wantarray) {
@ -6396,76 +6392,63 @@ rb_str_enumerate_lines(int argc, VALUE *argv, VALUE str, int wantarray)
return orig; return orig;
} }
} }
str = rb_str_new4(str); str = rb_str_new4(str);
ptr = p = s = RSTRING_PTR(str); ptr = subptr = RSTRING_PTR(str);
pend = p + RSTRING_LEN(str); pend = RSTRING_END(str);
len = RSTRING_LEN(str); len = RSTRING_LEN(str);
StringValue(rs); StringValue(rs);
if (rs == rb_default_rs) {
enc = rb_enc_get(str);
while (p < pend) {
char *p0;
p = memchr(p, '\n', pend - p);
if (!p) break;
p0 = rb_enc_left_char_head(s, p, pend, enc);
if (!rb_enc_is_newline(p0, pend, enc)) {
p++;
continue;
}
p = p0 + rb_enc_mbclen(p0, pend, enc);
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;
}
goto finish;
}
enc = rb_enc_check(str, rs);
rslen = RSTRING_LEN(rs); rslen = RSTRING_LEN(rs);
if (rs == rb_default_rs)
enc = rb_enc_get(str);
else
enc = rb_enc_check(str, rs);
if (rslen == 0) { if (rslen == 0) {
newline = '\n'; rsptr = "\n\n";
rslen = 2;
paragraph_mode = 1;
} }
else { else {
newline = rb_enc_codepoint(RSTRING_PTR(rs), RSTRING_END(rs), enc); rsptr = RSTRING_PTR(rs);
} }
while (p < pend) { if ((rs == rb_default_rs || paragraph_mode) && !rb_enc_asciicompat(enc)) {
unsigned int c = rb_enc_codepoint_len(p, pend, &n, enc); rs = rb_str_new(rsptr, rslen);
rs = rb_str_encode(rs, rb_enc_from_encoding(enc), 0, Qnil);
rsptr = RSTRING_PTR(rs);
rslen = RSTRING_LEN(rs);
}
again: while (subptr < pend) {
if (rslen == 0 && c == newline) { pos = rb_memsearch(rsptr, rslen, subptr, pend - subptr, enc);
p += n; if (pos < 0) break;
if (p < pend && (c = rb_enc_codepoint_len(p, pend, &n, enc)) != newline) { hit = subptr + pos;
goto again; adjusted = rb_enc_right_char_head(subptr, hit, pend, enc);
} if (hit != adjusted) {
while (p < pend && rb_enc_codepoint(p, pend, enc) == newline) { subptr = adjusted;
p += n; continue;
}
p -= n;
} }
if (c == newline && subend = hit + rslen;
(rslen <= 1 || if (paragraph_mode) {
(pend - p >= rslen && memcmp(RSTRING_PTR(rs), p, rslen) == 0))) { while (subend < pend && rb_enc_is_newline(subend, pend, enc)) {
const char *pp = p + (rslen ? rslen : n); subend += rb_enc_mbclen(subend, pend, enc);
line = rb_str_subseq(str, s - ptr, pp - s); }
if (wantarray) }
rb_ary_push(ary, line); line = rb_str_subseq(str, subptr - ptr, subend - subptr);
else if (wantarray) {
rb_yield(line); rb_ary_push(ary, line);
}
else {
rb_yield(line);
str_mod_check(str, ptr, len); str_mod_check(str, ptr, len);
s = pp;
} }
p += n; subptr = subend;
} }
finish: if (subptr != pend) {
if (s != pend) { line = rb_str_subseq(str, subptr - ptr, pend - subptr);
line = rb_str_subseq(str, s - ptr, pend - s);
if (wantarray) if (wantarray)
rb_ary_push(ary, line); rb_ary_push(ary, line);
else else

View file

@ -798,17 +798,12 @@ class TestM17NComb < Test::Unit::TestCase
def test_str_each_line def test_str_each_line
combination(STRINGS, STRINGS) {|s1, s2| combination(STRINGS, STRINGS) {|s1, s2|
if !s1.valid_encoding? || !s2.valid_encoding?
assert_raise(ArgumentError, Encoding::CompatibilityError) { s1.each_line(s2) {} }
next
end
if !s1.ascii_only? && !s2.ascii_only? && s1.encoding != s2.encoding if !s1.ascii_only? && !s2.ascii_only? && s1.encoding != s2.encoding
assert_raise(Encoding::CompatibilityError) { s1.each_line(s2) {} } assert_raise(Encoding::CompatibilityError) { s1.each_line(s2) {} }
next next
end end
lines = [] lines = []
enccall(s1, :each_line, s2) {|line| enccall(s1, :each_line, s2) {|line|
assert(line.valid_encoding?)
assert_equal(s1.encoding, line.encoding) assert_equal(s1.encoding, line.encoding)
lines << line lines << line
} }