aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--CHANGELOG.md2
-rw-r--r--src/gleam/bit_builder.gleam23
-rw-r--r--test/gleam/bit_builder_test.gleam6
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(<<>>)