summaryrefslogtreecommitdiff
path: root/test/ruby/test_string.rb
diff options
context:
space:
mode:
authornagachika <nagachika@ruby-lang.org>2021-05-23 16:09:17 +0900
committernagachika <nagachika@ruby-lang.org>2021-05-23 16:09:17 +0900
commit5af5ea7f860ed64062796e54e73274e7a56c7280 (patch)
tree0a3bb24a41eb093858593de234c6169833500cac /test/ruby/test_string.rb
parent44b87adc07621b6a8eddfcf4aaff34ce634179d4 (diff)
merge revision(s) cfd162d535c7a4f8b1f95255cc6be696a8b75557: [Backport #17467]
Make String#{strip,lstrip}{,!} strip leading NUL bytes The documentation already specifies that they strip whitespace and defines whitespace to include null. This wraps the new behavior in the appropriate guards in the specs, but does not specify behavior for previous versions, because this is a bug that could be backported. Fixes [Bug #17467] --- spec/ruby/core/string/lstrip_spec.rb | 18 ++++++++++++------ spec/ruby/core/string/strip_spec.rb | 22 ++++++++++------------ string.c | 4 ++-- test/ruby/test_string.rb | 16 ++++++++++++++++ 4 files changed, 40 insertions(+), 20 deletions(-)
Diffstat (limited to 'test/ruby/test_string.rb')
-rw-r--r--test/ruby/test_string.rb16
1 files changed, 16 insertions, 0 deletions
diff --git a/test/ruby/test_string.rb b/test/ruby/test_string.rb
index 4814872789..29ad98beaf 100644
--- a/test/ruby/test_string.rb
+++ b/test/ruby/test_string.rb
@@ -1852,6 +1852,7 @@ CODE
def test_strip
assert_equal(S("x"), S(" x ").strip)
assert_equal(S("x"), S(" \n\r\t x \t\r\n\n ").strip)
+ assert_equal(S("x"), S("\x00x\x00").strip)
assert_equal("0b0 ".force_encoding("UTF-16BE"),
"\x00 0b0 ".force_encoding("UTF-16BE").strip)
@@ -1870,6 +1871,10 @@ CODE
assert_equal(S("x"), a.strip!)
assert_equal(S("x"), a)
+ a = S("\x00x\x00")
+ assert_equal(S("x"), a.strip!)
+ assert_equal(S("x"), a)
+
a = S("x")
assert_nil(a.strip!)
assert_equal(S("x") ,a)
@@ -2703,6 +2708,7 @@ CODE
def test_rstrip
assert_equal(" hello", " hello ".rstrip)
assert_equal("\u3042", "\u3042 ".rstrip)
+ assert_equal("\u3042", "\u3042\u0000".rstrip)
assert_raise(Encoding::CompatibilityError) { "\u3042".encode("ISO-2022-JP").rstrip }
end
@@ -2723,12 +2729,17 @@ CODE
assert_equal(nil, s4.rstrip!)
assert_equal("\u3042", s4)
+ s5 = S("\u3042\u0000")
+ assert_equal("\u3042", s5.rstrip!)
+ assert_equal("\u3042", s5)
+
assert_raise(Encoding::CompatibilityError) { "\u3042".encode("ISO-2022-JP").rstrip! }
end
def test_lstrip
assert_equal("hello ", " hello ".lstrip)
assert_equal("\u3042", " \u3042".lstrip)
+ assert_equal("hello ", "\x00hello ".lstrip)
end
def test_lstrip_bang
@@ -2747,6 +2758,11 @@ CODE
s4 = S("\u3042")
assert_equal(nil, s4.lstrip!)
assert_equal("\u3042", s4)
+
+ s5 = S("\u0000\u3042")
+ assert_equal("\u3042", s5.lstrip!)
+ assert_equal("\u3042", s5)
+
end
def test_delete_prefix