diff options
author | Erik Terpstra <39518+eterps@users.noreply.github.com> | 2020-05-19 10:19:27 +0200 |
---|---|---|
committer | Louis Pilfold <louis@lpil.uk> | 2020-05-19 14:12:11 +0100 |
commit | e5e14239bd6ca6f349b7dd76fb845b60515e3364 (patch) | |
tree | 639b29064a64811c7dec064d5881834aac916b35 /src | |
parent | b3407607c4a58efa50239db278d730ef5b503afe (diff) | |
download | gleam_stdlib-e5e14239bd6ca6f349b7dd76fb845b60515e3364.tar.gz gleam_stdlib-e5e14239bd6ca6f349b7dd76fb845b60515e3364.zip |
string.pad_left & string.pad_right
Diffstat (limited to 'src')
-rw-r--r-- | src/gleam/string.gleam | 68 | ||||
-rw-r--r-- | src/gleam_stdlib.erl | 5 |
2 files changed, 41 insertions, 32 deletions
diff --git a/src/gleam/string.gleam b/src/gleam/string.gleam index dd292b5..d5523ef 100644 --- a/src/gleam/string.gleam +++ b/src/gleam/string.gleam @@ -298,37 +298,6 @@ pub fn join(strings: List(String), with separator: String) -> String { |> iodata.from_strings |> iodata.to_string } -/// -// TODO -// Pad a string on the left until it has at least given number of Graphemes. -// -// ## Examples -// > pad_left("121", to: 5, with: ".") -// "..121" -// -// > pad_left("121", to: 3, with: ".") -// "121" -// -// > pad_left("121", to: 2, with: ".") -// "121" -// -// -// pub fn pad_left(string: String, to size: Int, with: String) {} -// TODO -// Pad a string on the right until it has a given length. -// -// ## Examples -// > pad_right("121", to: 5, with: ".") -// "121.." -// -// > pad_right("121", to: 3, with: ".") -// "121" -// -// > pad_right("121", to: 2, with: ".") -// "121" -// -// -// pub fn pad_right(string: String, to size: Int, with: String) {} type Direction { Leading @@ -336,6 +305,43 @@ type Direction { Both } +external fn erl_pad(String, Int, Direction, String) -> String = + "gleam_stdlib" "string_pad" + +/// Pad a string on the left until it has at least given number of Graphemes. +/// +/// ## Examples +/// > pad_left("121", to: 5, with: ".") +/// "..121" +/// +/// > pad_left("121", to: 3, with: ".") +/// "121" +/// +/// > pad_left("121", to: 2, with: ".") +/// "121" +/// +/// +pub fn pad_left(string: String, to length: Int, with pad_string: String) { + erl_pad(string, length, Leading, pad_string) +} + +/// Pad a string on the right until it has a given length. +/// +/// ## Examples +/// > pad_right("121", to: 5, with: ".") +/// "121.." +/// +/// > pad_right("121", to: 3, with: ".") +/// "121" +/// +/// > pad_right("121", to: 2, with: ".") +/// "121" +/// +/// +pub fn pad_right(string: String, to length: Int, with pad_string: String) { + erl_pad(string, length, Trailing, pad_string) +} + external fn erl_trim(String, Direction) -> String = "string" "trim" diff --git a/src/gleam_stdlib.erl b/src/gleam_stdlib.erl index fbd84da..bbf7630 100644 --- a/src/gleam_stdlib.erl +++ b/src/gleam_stdlib.erl @@ -7,7 +7,7 @@ iodata_append/2, iodata_prepend/2, identity/1, decode_int/1, decode_string/1, decode_bool/1, decode_float/1, decode_thunk/1, decode_atom/1, decode_list/1, decode_field/2, decode_element/2, parse_int/1, parse_float/1, compare_strings/2, - string_contains/2, string_starts_with/2, string_ends_with/2]). + string_contains/2, string_starts_with/2, string_ends_with/2, string_pad/4]). should_equal(Actual, Expected) -> ?assertEqual(Expected, Actual). should_not_equal(Actual, Expected) -> ?assertNotEqual(Expected, Actual). @@ -139,3 +139,6 @@ string_ends_with(String, Suffix) when byte_size(Suffix) > byte_size(String) -> f string_ends_with(String, Suffix) -> SuffixSize = byte_size(Suffix), Suffix == binary_part(String, byte_size(String) - SuffixSize, SuffixSize). + +string_pad(String, Length, Dir, PadString) -> + unicode:characters_to_binary(string:pad(String, Length, Dir, PadString)). |