Skip to content

Commit af31b35

Browse files
authored
Merge pull request #8 from savi-lang/add/reserve-bytes-ahead
Add `ByteStream.Reader.reserve_bytes_ahead`.
2 parents 3c44f99 + c369153 commit af31b35

2 files changed

Lines changed: 34 additions & 0 deletions

File tree

spec/ByteStream.Reader.Spec.savi

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,27 @@
207207
assert: stream.token_byte_size == 0
208208
assert: stream.token_as_string == ""
209209

210+
:it "reserves at least a specified amount of space in front of the cursor"
211+
stream = ByteStream.Reader.new
212+
write_stream = ByteStream.Writer.to_reader(stream)
213+
214+
assert: stream.space_ahead == 0x4000
215+
assert: stream.reserve_bytes_ahead(0x8000).space_ahead == 0x8000
216+
assert: stream.reserve_bytes_ahead(0x8000).space_ahead == 0x8000
217+
218+
write_stream << b"hello"
219+
assert no_error: write_stream.flush!
220+
221+
assert: stream.space_ahead == 0x8000
222+
assert: stream.reserve_bytes_ahead(0x8000).space_ahead == 0x8000
223+
assert: stream.reserve_bytes_ahead(0x8000).space_ahead == 0x8000
224+
225+
assert no_error: stream.advance!(3)
226+
227+
assert: stream.space_ahead == 0x7FFD
228+
assert: stream.reserve_bytes_ahead(0x8000).space_ahead == 0xFFFD
229+
assert: stream.reserve_bytes_ahead(0x8000).space_ahead == 0xFFFD
230+
210231
:it "extracts an isolated token destructively from the stream"
211232
stream = ByteStream.Reader.new
212233
write_stream = ByteStream.Writer.to_reader(stream)

src/ByteStream.Reader.savi

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,13 +112,26 @@
112112
:: very start of the byte stream, including bytes no longer held in buffer.
113113
:fun bytes_behind_marker: @_mark_offset + @_lost_offset
114114

115+
:: Ensure that the underlying byte buffer is ready to handle the given amount
116+
:: of bytes ahead of the current cursor position, without copying on receive.
117+
:: This includes both bytes already in the buffer and space for future bytes.
118+
::
119+
:: Note that this may trigger a copy of the current content of the buffer
120+
:: if a reallocation is needed to ensure that number of bytes are available,
121+
:: but that copy can be avoided if the full buffer is extracted first.
122+
:fun ref reserve_bytes_ahead(amount USize)
123+
actual_amount = try (amount +! @_offset | USize.max_value)
124+
@_data.reserve(actual_amount)
125+
@
126+
115127
:: Reserve additional space in the underlying byte buffer, which forces a
116128
:: reallocation now, but sets a minimum number of bytes that the buffer
117129
:: can receive beyond the current size without forcing a reallocation.
118130
::
119131
:: See `Bytes.reserve_additional` for more information.
120132
:fun ref reserve_additional(additional_space)
121133
@_data.reserve_additional(additional_space)
134+
@
122135

123136
:: Get the number of bytes in the currently marked token.
124137
:: That is, the number of bytes between the marker and the cursor.

0 commit comments

Comments
 (0)