File tree 2 files changed +24
-0
lines changed
2 files changed +24
-0
lines changed Original file line number Diff line number Diff line change @@ -2163,6 +2163,17 @@ describe "String" do
2163
2163
" fo\u {0000}" .compare(" FO" , case_insensitive: true ).should eq(1 )
2164
2164
end
2165
2165
2166
+ it " builds with write_byte" do
2167
+ string = String .build do |io |
2168
+ 255 _u8 .times do |byte |
2169
+ io.write_byte(byte)
2170
+ end
2171
+ end
2172
+ 255 .times do |i |
2173
+ string.byte_at(i).should eq(i)
2174
+ end
2175
+ end
2176
+
2166
2177
it " raises if String.build negative capacity" do
2167
2178
expect_raises(ArgumentError , " Negative capacity" ) do
2168
2179
String .build(-1 ) { }
Original file line number Diff line number Diff line change @@ -53,6 +53,19 @@ class String::Builder
53
53
nil
54
54
end
55
55
56
+ def write_byte (byte : UInt8 )
57
+ new_bytesize = real_bytesize + 1
58
+ if new_bytesize > @capacity
59
+ resize_to_capacity(Math .pw2ceil(new_bytesize))
60
+ end
61
+
62
+ @buffer [real_bytesize] = byte
63
+
64
+ @bytesize += 1
65
+
66
+ nil
67
+ end
68
+
56
69
def buffer
57
70
@buffer + String ::HEADER_SIZE
58
71
end
You can’t perform that action at this time.
0 commit comments