Skip to content

Commit ca89c1b

Browse files
committed
Improve drop_start
1 parent 6cea5c7 commit ca89c1b

File tree

3 files changed

+14
-3
lines changed

3 files changed

+14
-3
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Changelog
22

3+
## v0.65.0 - 2025-09-29
4+
5+
- The performance of the `drop_start` function from the `string` module has
6+
been improved.
7+
38
## v0.64.0 - 2025-09-25
49

510
- The `unwrap_both` function of the `result` module has been deprecated.

src/gleam/string.gleam

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,8 @@ pub fn slice(from string: String, at_index idx: Int, length len: Int) -> String
209209
fn do_slice(string: String, idx: Int, len: Int) -> String
210210

211211
/// Drops contents of the first `String` that occur before the second `String`.
212-
/// If the `from` string does not contain the `before` string, `from` is returned unchanged.
212+
/// If the `from` string does not contain the `before` string, `from` is
213+
/// returned unchanged.
213214
///
214215
/// ## Examples
215216
///
@@ -234,12 +235,18 @@ pub fn crop(from string: String, before substring: String) -> String
234235
pub fn drop_start(from string: String, up_to num_graphemes: Int) -> String {
235236
case num_graphemes <= 0 {
236237
True -> string
237-
False -> slice(string, num_graphemes, length(string))
238+
False -> {
239+
let bigger_than_rest_of_string = byte_size(string) * 32
240+
slice(string, num_graphemes, bigger_than_rest_of_string)
241+
}
238242
}
239243
}
240244

241245
/// Drops *n* graphemes from the end of a `String`.
242246
///
247+
/// This function traverses the full string, so it runs in linear time with the
248+
/// size of the string.
249+
///
243250
/// ## Examples
244251
///
245252
/// ```gleam

src/gleam_stdlib.erl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,6 @@ slice(String, Index, Length) ->
468468
X when is_list(X) -> unicode:characters_to_binary(X)
469469
end.
470470

471-
472471
index([X | _], 0) ->
473472
{ok, {some, X}};
474473
index([_, X | _], 1) ->

0 commit comments

Comments
 (0)