diff options
-rw-r--r-- | CHANGELOG.md | 2 | ||||
-rw-r--r-- | gen/src/gleam@string.erl | 11 | ||||
-rw-r--r-- | gen/test/gleam@string_test.erl | 38 | ||||
-rw-r--r-- | src/gleam/string.gleam | 68 | ||||
-rw-r--r-- | src/gleam_stdlib.erl | 5 | ||||
-rw-r--r-- | test/gleam/string_test.gleam | 36 |
6 files changed, 125 insertions, 35 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index c64c96d..bf95e30 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,7 +3,7 @@ ## Unreleased - The `string` module gains `trim`, `trim_left`, `trim_right`, `starts_with`, - `ends_with` and `slice` functions. + `ends_with`, `slice`, `pad_left` and `pad_right` functions. ## v0.8.0 - 2020-04-28 diff --git a/gen/src/gleam@string.erl b/gen/src/gleam@string.erl index c8b76bc..86b1f79 100644 --- a/gen/src/gleam@string.erl +++ b/gen/src/gleam@string.erl @@ -1,7 +1,7 @@ -module(gleam@string). -compile(no_auto_import). --export([is_empty/1, length/1, reverse/1, replace/3, lowercase/1, uppercase/1, compare/2, slice/3, contains/2, starts_with/2, ends_with/2, split/2, append/2, concat/1, repeat/2, join/2, trim/1, trim_left/1, trim_right/1]). +-export([is_empty/1, length/1, reverse/1, replace/3, lowercase/1, uppercase/1, compare/2, slice/3, contains/2, starts_with/2, ends_with/2, split/2, append/2, concat/1, repeat/2, join/2, pad_left/3, pad_right/3, trim/1, trim_left/1, trim_right/1]). is_empty(Str) -> Str =:= <<""/utf8>>. @@ -88,6 +88,15 @@ join(Strings, Separator) -> gleam@iodata:from_strings(gleam@list:intersperse(Strings, Separator)) ). +erl_pad(A, B, C, D) -> + gleam_stdlib:string_pad(A, B, C, D). + +pad_left(String, Length, PadString) -> + erl_pad(String, Length, leading, PadString). + +pad_right(String, Length, PadString) -> + erl_pad(String, Length, trailing, PadString). + erl_trim(A, B) -> string:trim(A, B). diff --git a/gen/test/gleam@string_test.erl b/gen/test/gleam@string_test.erl index f975af9..54e517f 100644 --- a/gen/test/gleam@string_test.erl +++ b/gen/test/gleam@string_test.erl @@ -1,7 +1,7 @@ -module(gleam@string_test). -compile(no_auto_import). --export([length_test/0, lowercase_test/0, uppercase_test/0, reverse_test/0, split_test/0, replace_test/0, append_test/0, compare_test/0, contains_test/0, concat_test/0, repeat_test/0, join_test/0, trim_test/0, trim_left_test/0, trim_right_test/0, starts_with_test/0, ends_with_test/0, slice_test/0]). +-export([length_test/0, lowercase_test/0, uppercase_test/0, reverse_test/0, split_test/0, replace_test/0, append_test/0, compare_test/0, contains_test/0, concat_test/0, repeat_test/0, join_test/0, trim_test/0, trim_left_test/0, trim_right_test/0, starts_with_test/0, ends_with_test/0, slice_test/0, pad_left_test/0, pad_right_test/0]). length_test() -> gleam@should:equal(gleam@string:length(<<"ß↑e̊"/utf8>>), 3), @@ -174,3 +174,39 @@ slice_test() -> gleam@string:slice(<<"gleam"/utf8>>, -12, 2), <<""/utf8>> ). + +pad_left_test() -> + gleam@should:equal( + gleam@string:pad_left(<<"121"/utf8>>, 5, <<"."/utf8>>), + <<"..121"/utf8>> + ), + gleam@should:equal( + gleam@string:pad_left(<<"121"/utf8>>, 3, <<"."/utf8>>), + <<"121"/utf8>> + ), + gleam@should:equal( + gleam@string:pad_left(<<"121"/utf8>>, 2, <<"."/utf8>>), + <<"121"/utf8>> + ), + gleam@should:equal( + gleam@string:pad_left(<<"121"/utf8>>, 5, <<"XY"/utf8>>), + <<"XYXY121"/utf8>> + ). + +pad_right_test() -> + gleam@should:equal( + gleam@string:pad_right(<<"121"/utf8>>, 5, <<"."/utf8>>), + <<"121.."/utf8>> + ), + gleam@should:equal( + gleam@string:pad_right(<<"121"/utf8>>, 3, <<"."/utf8>>), + <<"121"/utf8>> + ), + gleam@should:equal( + gleam@string:pad_right(<<"121"/utf8>>, 2, <<"."/utf8>>), + <<"121"/utf8>> + ), + gleam@should:equal( + gleam@string:pad_right(<<"121"/utf8>>, 5, <<"XY"/utf8>>), + <<"121XYXY"/utf8>> + ). 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)). diff --git a/test/gleam/string_test.gleam b/test/gleam/string_test.gleam index d08ea78..93de5db 100644 --- a/test/gleam/string_test.gleam +++ b/test/gleam/string_test.gleam @@ -185,3 +185,39 @@ pub fn slice_test() { |> string.slice(at_index: -12, length: 2) |> should.equal("") } + +pub fn pad_left_test() { + "121" + |> string.pad_left(to: 5, with: ".") + |> should.equal("..121") + + "121" + |> string.pad_left(to: 3, with: ".") + |> should.equal("121") + + "121" + |> string.pad_left(to: 2, with: ".") + |> should.equal("121") + + "121" + |> string.pad_left(to: 5, with: "XY") + |> should.equal("XYXY121") +} + +pub fn pad_right_test() { + "121" + |> string.pad_right(to: 5, with: ".") + |> should.equal("121..") + + "121" + |> string.pad_right(to: 3, with: ".") + |> should.equal("121") + + "121" + |> string.pad_right(to: 2, with: ".") + |> should.equal("121") + + "121" + |> string.pad_right(to: 5, with: "XY") + |> should.equal("121XYXY") +} |