Skip to content
Open
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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## Unreleased

- Fixed a bug where `string.drop_start` would return incorrect results on
JavaScript when the string contained multi-byte characters.

## v1.0.2 - 2026-05-14

- Fixed deprecation warnings when using OTP 29
Expand Down
3 changes: 2 additions & 1 deletion src/gleam_stdlib.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,8 @@ export function length(data) {
}

export function string_byte_slice(string, index, length) {
return string.slice(index, index + length);
const bytes = new TextEncoder().encode(string);
return new TextDecoder().decode(bytes.subarray(index, index + length));
}

export function string_grapheme_slice(string, idx, len) {
Expand Down
8 changes: 8 additions & 0 deletions test/gleam/string_test.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,14 @@ pub fn drop_start_3499_test() {
assert string.drop_start("\r]", 1) == "]"
}

pub fn drop_start_multibyte_test() {
// https://github.com/gleam-lang/stdlib/issues/924
assert string.drop_start("广州abcdefghijklmn", 0) == "广州abcdefghijklmn"
assert string.drop_start("广州abcdefghijklmn", 1) == "州abcdefghijklmn"
assert string.drop_start("广州abcdefghijklmn", 2) == "abcdefghijklmn"
assert string.drop_start("广州abcdefghijklmn", 3) == "bcdefghijklmn"
}

pub fn drop_end_basic_test() {
assert string.drop_end("gleam", up_to: 2) == "gle"
}
Expand Down
Loading