diff options
author | Andrew Varner <varnerac@users.noreply.github.com> | 2022-06-21 23:03:42 -0400 |
---|---|---|
committer | Louis Pilfold <louis@lpil.uk> | 2022-06-23 17:28:34 +0100 |
commit | 5abe5464d9a633d75949fa305485a5a876c866fd (patch) | |
tree | a06eb4f2ab11e8f2dbcc8c5c40a8e1c2cb5b0c36 | |
parent | 4607b89d02c8c4091566a9f709db2721f2f26486 (diff) | |
download | gleam_stdlib-5abe5464d9a633d75949fa305485a5a876c866fd.tar.gz gleam_stdlib-5abe5464d9a633d75949fa305485a5a876c866fd.zip |
Add `concat_bit_strings` fun to `bit_builder`
-rw-r--r-- | CHANGELOG.md | 2 | ||||
-rw-r--r-- | src/gleam/bit_builder.gleam | 23 | ||||
-rw-r--r-- | test/gleam/bit_builder_test.gleam | 6 |
3 files changed, 30 insertions, 1 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index 71997e8..2f47119 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,7 @@ # Changelog +- The `bit_builder` module gains the `from_bit_strings` function. + ## v0.22.0 - 2022-06-15 - The `float` module gains the `divide` function. diff --git a/src/gleam/bit_builder.gleam b/src/gleam/bit_builder.gleam index a3791c8..2a7d435 100644 --- a/src/gleam/bit_builder.gleam +++ b/src/gleam/bit_builder.gleam @@ -108,7 +108,7 @@ pub fn append_string(to: BitBuilder, suffix: String) -> BitBuilder { append_builder(to, from_string(suffix)) } -/// Joins a list of builders into a single builders. +/// Joins a list of builders into a single builder. /// /// Runs in constant time. /// @@ -127,6 +127,27 @@ if javascript { } } +/// Joins a list of bit strings into a single builder. +/// +/// Runs in constant time. +/// +pub fn concat_bit_strings(bits: List(BitString)) -> BitBuilder { + do_concat_bit_strings(bits) +} + +if erlang { + external fn do_concat_bit_strings(List(BitString)) -> BitBuilder = + "gleam_stdlib" "identity" +} + +if javascript { + fn do_concat_bit_strings(bits: List(BitString)) -> BitBuilder { + bits + |> list.map(fn(b) { from_bit_string(b) }) + |> concat() + } +} + /// Creates a new builder from a string. /// /// Runs in constant time when running on Erlang. diff --git a/test/gleam/bit_builder_test.gleam b/test/gleam/bit_builder_test.gleam index 7ffa590..99994e5 100644 --- a/test/gleam/bit_builder_test.gleam +++ b/test/gleam/bit_builder_test.gleam @@ -60,6 +60,12 @@ pub fn concat_test() { |> should.equal(<<1, 2, 3, 4, 5, 6>>) } +pub fn concat_bit_strings_test() { + bit_builder.concat_bit_strings([<<"h":utf8>>, <<"e":utf8>>, <<"y":utf8>>]) + |> bit_builder.to_bit_string + |> should.equal(<<"hey":utf8>>) +} + pub fn from_bit_string_test() { // Regression test: no additional modification of the builder bit_builder.from_bit_string(<<>>) |