From 8349133498dbd595b82c783d8e11d530689eeb35 Mon Sep 17 00:00:00 2001 From: Louis Pilfold Date: Mon, 25 Nov 2024 19:01:09 +0000 Subject: [PATCH 1/2] v0.44.0 --- CHANGELOG.md | 2 +- gleam.toml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1400019c..63a148bd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,6 @@ # Changelog -## v0.44.0 - Unreleased +## v0.44.0 - 2024-11-25 - The `gleam/queue` module has been deprecated in favour of the `gleam_deque` package. diff --git a/gleam.toml b/gleam.toml index 34d8504e..7a60febd 100644 --- a/gleam.toml +++ b/gleam.toml @@ -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" From 5e9c98b1f833aac1e32727acf6b9e30d540432f4 Mon Sep 17 00:00:00 2001 From: Richard Viney Date: Tue, 12 Nov 2024 11:49:30 +1300 Subject: [PATCH 2/2] Optimise string trimming on JavaScript --- CHANGELOG.md | 5 +++++ src/gleam_stdlib.mjs | 13 ++++++++----- test/gleam/string_test.gleam | 17 +++++++++++++++++ 3 files changed, 30 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 63a148bd..16f2acd9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # Changelog +## 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` diff --git a/src/gleam_stdlib.mjs b/src/gleam_stdlib.mjs index 46bc72a2..06302bfc 100644 --- a/src/gleam_stdlib.mjs +++ b/src/gleam_stdlib.mjs @@ -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) { diff --git a/test/gleam/string_test.gleam b/test/gleam/string_test.gleam index b98a4e14..641d64dd 100644 --- a/test/gleam/string_test.gleam +++ b/test/gleam/string_test.gleam @@ -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}"