Skip to content

Commit fdc534b

Browse files
committed
Add specs for String#length with broken strings
1 parent bc73388 commit fdc534b

1 file changed

Lines changed: 16 additions & 0 deletions

File tree

core/string/shared/length.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,20 @@
3636
concat.force_encoding(Encoding::ASCII_8BIT)
3737
concat.size.should == 4
3838
end
39+
40+
it "adds 1 for every invalid byte in UTF-8" do
41+
"\xF4\x90\x80\x80".size.should == 4
42+
"a\xF4\x90\x80\x80b".size.should == 6
43+
\xF4\x90\x80\x80è".size.should == 6
44+
end
45+
46+
it "adds 1 (and not 2) for a incomplete surrogate in UTF-16" do
47+
"\x00\xd8".force_encoding("UTF-16LE").size.should == 1
48+
"\xd8\x00".force_encoding("UTF-16BE").size.should == 1
49+
end
50+
51+
it "adds 1 for a broken sequence in UTF-32" do
52+
"\x04\x03\x02\x01".force_encoding("UTF-32LE").size.should == 1
53+
"\x01\x02\x03\x04".force_encoding("UTF-32BE").size.should == 1
54+
end
3955
end

0 commit comments

Comments
 (0)