Skip to content

Commit

Permalink
Fix bit_array slices of slices on JavaScript
Browse files Browse the repository at this point in the history
  • Loading branch information
richard-viney committed May 19, 2024
1 parent 6814f28 commit f89c424
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
- Fixed `string.inspect` not formatting the `\f` form feed control character
correctly on Erlang.
- `dynamic.unsafe_coerce` function has been deprecated.
- Fixed `bit_array` slices of slices sometimes being incorrect on JavaScript.

## v0.37.0 - 2024-04-19

Expand Down
3 changes: 2 additions & 1 deletion src/gleam_stdlib.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,8 @@ export function bit_array_slice(bits, position, length) {
const start = Math.min(position, position + length);
const end = Math.max(position, position + length);
if (start < 0 || end > bits.length) return new Error(Nil);
const buffer = new Uint8Array(bits.buffer.buffer, start, Math.abs(length));
const byteOffset = bits.buffer.byteOffset + start;
const buffer = new Uint8Array(bits.buffer.buffer, byteOffset, Math.abs(length));
return new Ok(new BitArray(buffer));
}

Expand Down
6 changes: 6 additions & 0 deletions test/gleam/bit_array_test.gleam
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import gleam/bit_array
import gleam/result
import gleam/should

pub fn byte_size_test() {
Expand Down Expand Up @@ -88,6 +89,11 @@ pub fn slice_test() {
bit_array.from_string("hello")
|> bit_array.slice(1, 6)
|> should.equal(Error(Nil))

bit_array.from_string("ab")
|> bit_array.slice(1, 1)
|> result.try(bit_array.slice(_, 0, 1))
|> should.equal(Ok(<<"b":utf8>>))
}

pub fn to_string_test() {
Expand Down

0 comments on commit f89c424

Please sign in to comment.