diff options
author | Erik Terpstra <39518+eterps@users.noreply.github.com> | 2020-05-18 09:21:06 +0200 |
---|---|---|
committer | Louis Pilfold <louis@lpil.uk> | 2020-05-22 11:51:50 +0100 |
commit | 2109886ed8a7fc9aae1a2f5f9fb4e85cc29f49e5 (patch) | |
tree | a5f62e7d4cd23a468cab78cfd86c00f8345de1e2 /src | |
parent | 44c2a9a9bab0d0deca11a255d5d7d8d8dca72562 (diff) | |
download | gleam_stdlib-2109886ed8a7fc9aae1a2f5f9fb4e85cc29f49e5.tar.gz gleam_stdlib-2109886ed8a7fc9aae1a2f5f9fb4e85cc29f49e5.zip |
string.drop_left & string.drop_right
Diffstat (limited to 'src')
-rw-r--r-- | src/gleam/string.gleam | 61 |
1 files changed, 36 insertions, 25 deletions
diff --git a/src/gleam/string.gleam b/src/gleam/string.gleam index fc58329..25122b9 100644 --- a/src/gleam/string.gleam +++ b/src/gleam/string.gleam @@ -150,36 +150,47 @@ pub fn slice( at_index idx: Int, length len: Int, ) -> String { - case idx < 0 { - True -> { - let translated_idx = length(string) + idx - case translated_idx < 0 { - True -> "" - False -> erl_slice(string, translated_idx, len) + case len < 0 { + True -> "" + False -> case idx < 0 { + True -> { + let translated_idx = length(string) + idx + case translated_idx < 0 { + True -> "" + False -> erl_slice(string, translated_idx, len) + } } + False -> erl_slice(string, idx, len) } - False -> erl_slice(string, idx, len) } } -// TODO -// Drop *n* Graphemes from the left side of a -// -// ## Examples -// > drop_left(from: "The Lone Gunmen", up_to: 2) -// "e Lone Gunmen" -// -// -// pub fn drop_left(from string: String, up_to num_graphemes: Int) -> String {} -// TODO -// Drop *n* Graphemes from the right side of a -// -// ## Examples -// > drop_right(from: "Cigarette Smoking Man", up_to: 2) -// "Cigarette Smoking M" -// -// -// pub fn drop_right(from string: String, up_to num_graphemes: Int) -> String {} +/// Drop *n* Graphemes from the left side of a string. +/// +/// ## Examples +/// > drop_left(from: "The Lone Gunmen", up_to: 2) +/// "e Lone Gunmen" +/// +pub fn drop_left(from string: String, up_to num_graphemes: Int) -> String { + case num_graphemes < 0 { + True -> string + False -> slice(string, num_graphemes, length(string) - num_graphemes) + } +} + +/// Drop *n* Graphemes from the right side of a string. +/// +/// ## Examples +/// > drop_right(from: "Cigarette Smoking Man", up_to: 2) +/// "Cigarette Smoking M" +/// +pub fn drop_right(from string: String, up_to num_graphemes: Int) -> String { + case num_graphemes < 0 { + True -> string + False -> slice(string, 0, length(string) - num_graphemes) + } +} + external fn erl_contains(String, String) -> Dynamic = "string" "find" |