From d5f145076ce11ed4e47e11af065ee9e36bf5f369 Mon Sep 17 00:00:00 2001 From: Yasuhiro Matsumoto Date: Tue, 29 Sep 2015 21:22:25 +0900 Subject: chop with utf-8. fix #2967 --- src/string.c | 11 +++++++++++ test/t/string.rb | 25 +++++++++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/src/string.c b/src/string.c index 6cad6a4ac..b597c3da9 100644 --- a/src/string.c +++ b/src/string.c @@ -1318,7 +1318,18 @@ mrb_str_chop_bang(mrb_state *mrb, mrb_value str) mrb_str_modify(mrb, s); if (RSTR_LEN(s) > 0) { mrb_int len; +#ifdef MRB_UTF8_STRING + const char* t = RSTR_PTR(s), *p = t; + const char* e = p + RSTR_LEN(s); + while (p=e) break; + p += clen; + } + len = p - t; +#else len = RSTR_LEN(s) - 1; +#endif if (RSTR_PTR(s)[len] == '\n') { if (len > 0 && RSTR_PTR(s)[len-1] == '\r') { diff --git a/test/t/string.rb b/test/t/string.rb index f9a521edd..dfb1b00e0 100644 --- a/test/t/string.rb +++ b/test/t/string.rb @@ -262,6 +262,16 @@ assert('String#chop', '15.2.10.5.11') do assert_equal 'abc', c end +assert('String#chop', '15.2.10.5.11') do + a = ''.chop + b = 'あいう'.chop + c = 'あ\nい'.chop.chop + + assert_equal '', a + assert_equal 'あい', b + assert_equal 'あ', c +end if UTF8STRING + assert('String#chop!', '15.2.10.5.12') do a = '' b = 'abc' @@ -273,6 +283,21 @@ assert('String#chop!', '15.2.10.5.12') do assert_equal b, 'ab' end +assert('String#chop!', '15.2.10.5.12') do + a = '' + b = 'あいうえ\n' + c = 'あいうえ\n' + + a.chop! + b.chop! + c.chop! + c.chop! + + assert_equal a, '' + assert_equal b, 'あいうえ' + assert_equal c, 'あいう' +end if UTF8STRING + assert('String#downcase', '15.2.10.5.13') do a = 'ABC'.downcase b = 'ABC' -- cgit v1.2.3 From 7ad75a4a19f9917bc555b53bffbc6e57b67f1f56 Mon Sep 17 00:00:00 2001 From: Yasuhiro Matsumoto Date: Wed, 30 Sep 2015 13:08:11 +0900 Subject: fix tests --- test/t/string.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/t/string.rb b/test/t/string.rb index dfb1b00e0..da75bdd84 100644 --- a/test/t/string.rb +++ b/test/t/string.rb @@ -265,7 +265,7 @@ end assert('String#chop', '15.2.10.5.11') do a = ''.chop b = 'あいう'.chop - c = 'あ\nい'.chop.chop + c = "あ\nい".chop.chop assert_equal '', a assert_equal 'あい', b @@ -285,8 +285,8 @@ end assert('String#chop!', '15.2.10.5.12') do a = '' - b = 'あいうえ\n' - c = 'あいうえ\n' + b = "あいうえ\n" + c = "あいうえ\n" a.chop! b.chop! -- cgit v1.2.3