aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLouis Pilfold <louis@lpil.uk>2020-06-30 13:31:59 +0100
committerLouis Pilfold <louis@lpil.uk>2020-06-30 13:31:59 +0100
commit869aff19338e474f003398aad0a251621df3517c (patch)
treeeeead4d2c1c954b8bba92adb4af454e8b7a0d739
parentcd9f78320acf2942b5e63cd5370f14f3cce12662 (diff)
downloadgleam_stdlib-869aff19338e474f003398aad0a251621df3517c.tar.gz
gleam_stdlib-869aff19338e474f003398aad0a251621df3517c.zip
Fix crash with unmodified bit builder
-rw-r--r--src/gleam/bit_builder.gleam2
-rw-r--r--src/gleam_stdlib.erl5
-rw-r--r--test/gleam/bit_builder_test.gleam (renamed from test/gleam/bit_string_builder_test.gleam)7
3 files changed, 12 insertions, 2 deletions
diff --git a/src/gleam/bit_builder.gleam b/src/gleam/bit_builder.gleam
index c3c88b0..dc066ae 100644
--- a/src/gleam/bit_builder.gleam
+++ b/src/gleam/bit_builder.gleam
@@ -74,7 +74,7 @@ pub external fn concat(List(BitBuilder)) -> BitBuilder =
/// Runs in constant time.
///
pub external fn from_bit_string(BitString) -> BitBuilder =
- "gleam_stdlib" "identity"
+ "gleam_stdlib" "wrap_list"
/// Turns an builder into a bit string.
///
diff --git a/src/gleam_stdlib.erl b/src/gleam_stdlib.erl
index f84e658..fa5f8fd 100644
--- a/src/gleam_stdlib.erl
+++ b/src/gleam_stdlib.erl
@@ -11,7 +11,7 @@
string_pad/4, decode_tuple2/1, decode_map/1, bit_string_int_to_u32/1,
bit_string_int_from_u32/1, bit_string_append/2, bit_string_part_/3,
decode_bit_string/1, compile_regex/2, regex_match/2, regex_split/2,
- regex_scan/2, base_decoded4/1]).
+ regex_scan/2, base_decoded4/1, wrap_list/1]).
should_equal(Actual, Expected) -> ?assertEqual(Expected, Actual).
should_not_equal(Actual, Expected) -> ?assertNotEqual(Expected, Actual).
@@ -205,3 +205,6 @@ base_decoded4(S) ->
try {ok, base64:decode(S)}
catch error:badarith -> {error, nil}
end.
+
+wrap_list(X) when is_list(X) -> X;
+wrap_list(X) -> [X].
diff --git a/test/gleam/bit_string_builder_test.gleam b/test/gleam/bit_builder_test.gleam
index 43e010a..b1635e5 100644
--- a/test/gleam/bit_string_builder_test.gleam
+++ b/test/gleam/bit_builder_test.gleam
@@ -56,3 +56,10 @@ pub fn concat_test() {
|> bit_builder.to_bit_string
|> should.equal(<<1, 2, 3, 4, 5, 6>>)
}
+
+pub fn from_string_test() {
+ // Regression test: no additional modification of the builder
+ bit_builder.from_bit_string(<<>>)
+ |> bit_builder.to_bit_string
+ |> should.equal(<<>>)
+}