From e5e14239bd6ca6f349b7dd76fb845b60515e3364 Mon Sep 17 00:00:00 2001 From: Erik Terpstra <39518+eterps@users.noreply.github.com> Date: Tue, 19 May 2020 10:19:27 +0200 Subject: string.pad_left & string.pad_right --- src/gleam/string.gleam | 68 +++++++++++++++++++++++++++----------------------- src/gleam_stdlib.erl | 5 +++- 2 files changed, 41 insertions(+), 32 deletions(-) (limited to 'src') 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)). -- cgit v1.2.3