diff options
author | Jechol Lee <mr.jechol@gmail.com> | 2020-08-14 20:40:30 +0900 |
---|---|---|
committer | Louis Pilfold <louis@lpil.uk> | 2020-08-16 11:19:51 +0100 |
commit | e2587fe4c54b03ac57ff90456c9a8350561ad378 (patch) | |
tree | f52cf9152bd4dfcca1370a236b67db2829c7bbe8 | |
parent | d313a037cc839d34aeeb6dcd87b35199ee8b3c67 (diff) | |
download | gleam_stdlib-e2587fe4c54b03ac57ff90456c9a8350561ad378.tar.gz gleam_stdlib-e2587fe4c54b03ac57ff90456c9a8350561ad378.zip |
Refactor string.gleam
-rw-r--r-- | src/gleam/string.gleam | 20 |
1 files changed, 6 insertions, 14 deletions
diff --git a/src/gleam/string.gleam b/src/gleam/string.gleam index 3b8c5e0..26bf01d 100644 --- a/src/gleam/string.gleam +++ b/src/gleam/string.gleam @@ -3,6 +3,7 @@ import gleam/string_builder import gleam/dynamic.{Dynamic} +import gleam/iterator import gleam/list import gleam/order import gleam/result @@ -312,13 +313,6 @@ pub fn concat(strings: List(String)) -> String { |> string_builder.to_string } -fn repeat_help(chunk: String, result: List(String), repeats: Int) -> String { - case repeats <= 0 { - True -> concat(result) - False -> repeat_help(chunk, [chunk, ..result], repeats - 1) - } -} - /// Create a new string by repeating a string a given number of times. /// /// This function runs in linear time. @@ -329,7 +323,9 @@ fn repeat_help(chunk: String, result: List(String), repeats: Int) -> String { /// "hahaha" /// pub fn repeat(string: String, times times: Int) -> String { - repeat_help(string, [], times) + iterator.repeat(string) + |> iterator.take(times) + |> concat } /// Join many strings together with a given separator. @@ -344,14 +340,11 @@ pub fn repeat(string: String, times times: Int) -> String { pub fn join(strings: List(String), with separator: String) -> String { strings |> list.intersperse(with: separator) - |> string_builder.from_strings - |> string_builder.to_string + |> concat } type Direction { - Leading - Trailing Both } @@ -466,8 +459,7 @@ external fn int_to_utf_codepoint(Int) -> UtfCodepoint = pub fn utf_codepoint(value: Int) -> Result(UtfCodepoint, Nil) { case value { i if i > 1114111 -> Error(Nil) - i if i == 65534 -> Error(Nil) - i if i == 65535 -> Error(Nil) + 65534 | 65535 -> Error(Nil) i if i >= 55296 && i <= 57343 -> Error(Nil) i -> Ok(int_to_utf_codepoint(i)) } |