Skip to content

Commit

Permalink
Merge pull request #423 from savi-lang/add/bytes-set-byte-at
Browse files Browse the repository at this point in the history
Add `Bytes.set_byte_at!` method (and `Bytes.[]=!` synonym).
  • Loading branch information
jemc authored Jan 8, 2023
2 parents 7e2dc88 + bb3233b commit 1678891
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
6 changes: 6 additions & 0 deletions core/Bytes.savi
Original file line number Diff line number Diff line change
Expand Up @@ -270,10 +270,16 @@

:fun "[]!"(index): @byte_at!(index)

:fun ref "[]=!"(index, value): @set_byte_at!(index, value)

:fun byte_at!(index USize)
if (@size <= index) error!
@_ptr._get_at(index)

:fun ref set_byte_at!(index USize, byte U8)
if (@size <= index) error!
@_ptr._assign_at(index, byte)

:fun _offset_to_index(offset ISize) USize
if (offset < 0) (offset.usize + @size | offset.usize)

Expand Down
6 changes: 6 additions & 0 deletions spec/core/Bytes.Spec.savi
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,12 @@
assert: b"example"[3]! == 'm'
assert error: b"example"[7]!

:it "assigns a new byte at the given byte offset"
bytes Bytes'ref = b"example".clone
assert: (bytes[3]! = 'M') == 'M'
assert error: bytes[7]! = '?'
assert: bytes == b"exaMple"

:it "trims bytes off the start and end of the byte string"
assert: b"example".trim(2) == b"ample"
assert: b"example".trim(3, -2) == b"mp"
Expand Down

0 comments on commit 1678891

Please sign in to comment.