diff options
author | inoas <mail@inoas.com> | 2022-09-19 12:14:53 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-09-19 13:14:53 +0100 |
commit | de55a928a712df78faad08a9e9aafea7717d3190 (patch) | |
tree | 9a6539c99fb9502956ea2ac72c5d677f04fcf75b | |
parent | 0076b6f2bef09c2b1cf7f14c3da12faa8b2a4c11 (diff) | |
download | gleam_stdlib-de55a928a712df78faad08a9e9aafea7717d3190.tar.gz gleam_stdlib-de55a928a712df78faad08a9e9aafea7717d3190.zip |
String slice tco (#338)
-rw-r--r-- | src/gleam/string.gleam | 9 | ||||
-rw-r--r-- | test/gleam/list_test.gleam | 18 | ||||
-rw-r--r-- | test/gleam/string_test.gleam | 19 |
3 files changed, 39 insertions, 7 deletions
diff --git a/src/gleam/string.gleam b/src/gleam/string.gleam index 1645894..9c1bc9e 100644 --- a/src/gleam/string.gleam +++ b/src/gleam/string.gleam @@ -710,9 +710,14 @@ if javascript { /// ``` /// pub fn to_graphemes(string: String) -> List(String) { + do_to_graphemes(string, []) + |> list.reverse +} + +pub fn do_to_graphemes(string: String, acc: List(String)) -> List(String) { case pop_grapheme(string) { - Ok(#(grapheme, rest)) -> [grapheme, ..to_graphemes(rest)] - _ -> [] + Ok(#(grapheme, rest)) -> do_to_graphemes(rest, [grapheme, ..acc]) + _ -> acc } } diff --git a/test/gleam/list_test.gleam b/test/gleam/list_test.gleam index 1ea4f42..f9a7f44 100644 --- a/test/gleam/list_test.gleam +++ b/test/gleam/list_test.gleam @@ -3,6 +3,14 @@ import gleam/int import gleam/list import gleam/should +if erlang { + const recursion_test_cycles = 999999 +} + +if javascript { + const recursion_test_cycles = 16999 +} + pub fn length_test() { list.length([]) |> should.equal(0) @@ -49,7 +57,7 @@ pub fn contains_test() { list.contains([], 1) |> should.be_false - list.repeat(0, 16999) + list.repeat(0, recursion_test_cycles) |> list.contains(1) |> should.equal(False) } @@ -309,11 +317,11 @@ pub fn all_test() { list.all([], fn(_) { False }) |> should.equal(True) - list.repeat(False, 16999) + list.repeat(False, recursion_test_cycles) |> list.all(fn(item) { item }) |> should.equal(False) - list.repeat(True, 16999) + list.repeat(True, recursion_test_cycles) |> list.all(fn(item) { item }) |> should.equal(True) @@ -340,11 +348,11 @@ pub fn any_test() { list.any([], fn(_) { False }) |> should.equal(False) - list.repeat(True, 16999) + list.repeat(True, recursion_test_cycles) |> list.any(fn(item) { item }) |> should.equal(True) - list.repeat(False, 16999) + list.repeat(False, recursion_test_cycles) |> list.any(fn(item) { item }) |> should.equal(False) diff --git a/test/gleam/string_test.gleam b/test/gleam/string_test.gleam index 2e595b1..7155ea6 100644 --- a/test/gleam/string_test.gleam +++ b/test/gleam/string_test.gleam @@ -3,6 +3,14 @@ import gleam/order import gleam/should import gleam/string +if erlang { + const recursion_test_cycles = 999999 +} + +if javascript { + const recursion_test_cycles = 16999 +} + pub fn length_test() { string.length("ĆāeĢ") |> should.equal(3) @@ -53,6 +61,12 @@ pub fn reverse_test() { |> string.reverse |> string.reverse |> should.equal("š¶šæ") + + "abc" + |> string.repeat(recursion_test_cycles) + |> string.reverse + |> string.starts_with("cba") + |> should.be_true } pub fn split_test() { @@ -233,6 +247,11 @@ pub fn slice_test() { "š¶šæ" |> string.slice(at_index: 0, length: 3) |> should.equal("š¶šæ") + + "aaa" + |> string.repeat(recursion_test_cycles) + |> string.slice(at_index: recursion_test_cycles / 2, length: 3) + |> should.equal("aaa") } pub fn crop_test() { |