aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--CHANGELOG.md1
-rw-r--r--src/gleam/string.gleam2
-rw-r--r--src/gleam_stdlib.erl8
-rw-r--r--test/gleam/string_test.gleam7
4 files changed, 16 insertions, 2 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 000848d..1ff62ba 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -19,6 +19,7 @@
where the `Intl` API is not supported.
- Fixed a bug where the behaviour of `uri.percent_decode` would decode `+` as a
space on JavaScript.
+- Fixed a bug where `string.slice` could return invalid values on Erlang.
## v0.39.0 - 2024-07-09
diff --git a/src/gleam/string.gleam b/src/gleam/string.gleam
index 07affd5..4eb8b85 100644
--- a/src/gleam/string.gleam
+++ b/src/gleam/string.gleam
@@ -230,7 +230,7 @@ pub fn slice(from string: String, at_index idx: Int, length len: Int) -> String
}
}
-@external(erlang, "string", "slice")
+@external(erlang, "gleam_stdlib", "slice")
fn do_slice(string: String, idx: Int, len: Int) -> String {
string
|> to_graphemes
diff --git a/src/gleam_stdlib.erl b/src/gleam_stdlib.erl
index 0b4aae5..0941a33 100644
--- a/src/gleam_stdlib.erl
+++ b/src/gleam_stdlib.erl
@@ -14,7 +14,7 @@
decode_tuple5/1, decode_tuple6/1, tuple_get/2, classify_dynamic/1, print/1,
println/1, print_error/1, println_error/1, inspect/1, float_to_string/1,
int_from_base_string/2, utf_codepoint_list_to_string/1, contains_string/2,
- crop_string/2, base16_decode/1, string_replace/3, regex_replace/3
+ crop_string/2, base16_decode/1, string_replace/3, regex_replace/3, slice/3
]).
%% Taken from OTP's uri_string module
@@ -547,3 +547,9 @@ base16_decode(String) ->
string_replace(String, Pattern, Replacement) ->
string:replace(String, Pattern, Replacement, all).
+
+slice(String, Index, Length) ->
+ case string:slice(String, Index, Length) of
+ X when is_binary(X) -> X;
+ X when is_list(X) -> unicode:characters_to_binary(X)
+ end.
diff --git a/test/gleam/string_test.gleam b/test/gleam/string_test.gleam
index e5a4904..6d3031e 100644
--- a/test/gleam/string_test.gleam
+++ b/test/gleam/string_test.gleam
@@ -465,6 +465,13 @@ pub fn drop_left_test() {
|> should.equal("gleam")
}
+pub fn drop_left_3499_test() {
+ // https://github.com/gleam-lang/gleam/issues/3499
+ "\r]"
+ |> string.drop_left(1)
+ |> should.equal("]")
+}
+
pub fn drop_right_test() {
"gleam"
|> string.drop_right(up_to: 2)