Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bit_array slices of slices on JavaScript #596

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading