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

Optimise string trimming on JavaScript #736

Merged
merged 2 commits into from
Nov 26, 2024
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
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
# Changelog

## v0.44.0 - Unreleased
## Unreleased

- The performance of `string.trim`, `string.trim_start`, and `string.trim_end`
has been improved on JavaScript.

## v0.44.0 - 2024-11-25

- The `gleam/queue` module has been deprecated in favour of the `gleam_deque`
package.
Expand Down
2 changes: 1 addition & 1 deletion gleam.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name = "gleam_stdlib"
version = "0.43.0"
version = "0.44.0"
gleam = ">= 0.32.0"
licences = ["Apache-2.0"]
description = "A standard library for the Gleam programming language"
Expand Down
13 changes: 8 additions & 5 deletions src/gleam_stdlib.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -302,19 +302,22 @@ const unicode_whitespaces = [
"\u2029", // Paragraph separator
].join("");

const left_trim_regex = new RegExp(`^([${unicode_whitespaces}]*)`, "g");
const right_trim_regex = new RegExp(`([${unicode_whitespaces}]*)$`, "g");
const trim_start_regex = new RegExp(`^[${unicode_whitespaces}]*`);
const trim_end_regex = new RegExp(`[${unicode_whitespaces}]*$`);
const trim_regex = new RegExp(
`^[${unicode_whitespaces}]*(.*?)[${unicode_whitespaces}]*$`
);

export function trim(string) {
return trim_start(trim_end(string));
return string.match(trim_regex)[1];
}

export function trim_start(string) {
return string.replace(left_trim_regex, "");
return string.replace(trim_start_regex, "");
}

export function trim_end(string) {
return string.replace(right_trim_regex, "");
return string.replace(trim_end_regex, "");
}

export function bit_array_from_string(string) {
Expand Down
17 changes: 17 additions & 0 deletions test/gleam/string_test.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,23 @@ pub fn trim_end_test() {
|> should.equal(" hats")
}

pub fn trim_whole_string_test() {
let s =
"\u{0020}\u{0009}\u{000A}\u{000B}\u{000C}\u{000D}\u{0085}\u{2028}\u{2029}"

s
|> string.trim_start
|> should.equal("")

s
|> string.trim_end
|> should.equal("")

s
|> string.trim
|> should.equal("")
}

// unicode whitespaces
pub fn trim_horizontal_tab_test() {
"hats\u{0009}"
Expand Down