gleam-lang / gleam-lang/stdlib

string.drop_start behaves wrong on the JS target

Open
#924 1 comment 0 reactions 0 assignees View on GitHub
help wanted
Dominant language
Gleam
Stars
710
Forks
225
Avg merge
5h 52m
Merged PRs (30d)
3

Description

```gleam
import gleam/io
import gleam/string

pub fn main() {
io.println(string.drop_start("广州abcdefghijklmn", 0))
io.println(string.drop_start("广州abcdefghijklmn", 1))
io.println(string.drop_start("广州abcdefghijklmn", 2))
io.println(string.drop_start("广州abcdefghijklmn", 3))
}
```

outputs on the JS target:

```
广州abcdefghijklmn
bcdefghijklmn
efghijklmn
fghijklmn
```

So the first two characters are counted as 3 each. `unsafe_byte_slice` is used here with _byte_ offsets: https://github.com/gleam-lang/stdlib/blob/c6f7ceee9c569c71849d7e4c5a94e535feaae724/src/gleam/string.gleam#L236

It calls `string_byte_slice`, which contrary to its name does not operate on bytes but UTF-16 _code units_: https://github.com/gleam-lang/stdlib/blob/c6f7ceee9c569c71849d7e4c5a94e535feaae724/src/gleam_stdlib.mjs#L202

Thus the wrong offsets are sliced from the string.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.