aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorRichard Viney <richard.viney@gmail.com>2024-01-30 22:51:42 +1300
committerLouis Pilfold <louis@lpil.uk>2024-02-15 11:26:10 +0000
commitbd785507c5238ac1293137decb612a50a725bc75 (patch)
tree0c1c0169616801daa5593adb0097a884ba65e7c7 /src
parent5bc0aa4c7881b98d49804b80789ed279d944d70e (diff)
downloadgleam_stdlib-bd785507c5238ac1293137decb612a50a725bc75.tar.gz
gleam_stdlib-bd785507c5238ac1293137decb612a50a725bc75.zip
Add bit_array.inspect and bit_array.inspect_base16
Diffstat (limited to 'src')
-rw-r--r--src/gleam/bit_array.gleam52
1 files changed, 52 insertions, 0 deletions
diff --git a/src/gleam/bit_array.gleam b/src/gleam/bit_array.gleam
index 1a94679..2f8a07e 100644
--- a/src/gleam/bit_array.gleam
+++ b/src/gleam/bit_array.gleam
@@ -1,5 +1,6 @@
//// BitArrays are a sequence of binary data of any length.
+import gleam/int
import gleam/string
/// Converts a UTF-8 `String` type into a `BitArray`.
@@ -155,3 +156,54 @@ pub fn base16_encode(input: BitArray) -> String
@external(erlang, "gleam_stdlib", "base16_decode")
@external(javascript, "../gleam_stdlib.mjs", "base16_decode")
pub fn base16_decode(input: String) -> Result(BitArray, Nil)
+
+/// Converts a bit array to a string containing the decimal value of each byte.
+///
+/// ## Examples
+///
+/// ```gleam
+/// inspect(<<0, 20, 0x20, 255>>)
+/// // -> "<<0, 20, 32, 255>>"
+/// ```
+///
+pub fn inspect(input: BitArray) -> String {
+ "<<"
+ <> input
+ |> do_inspect(int.to_string)
+ |> string.join(", ")
+ <> ">>"
+}
+
+/// Converts a bit array to a string containing the hexadecimal value of each
+/// byte.
+///
+/// ## Examples
+///
+/// ```gleam
+/// inspect(<<0, 20, 0x20, 255>>)
+/// // -> "<<00 14 20 FF>>"
+/// ```
+///
+pub fn inspect_base16(input: BitArray) -> String {
+ let byte_to_hex_string = fn(b) {
+ b
+ |> int.to_base16
+ |> string.pad_left(2, "0")
+ }
+
+ "<<"
+ <> input
+ |> do_inspect(byte_to_hex_string)
+ |> string.join(" ")
+ <> ">>"
+}
+
+fn do_inspect(
+ input: BitArray,
+ byte_to_string: fn(Int) -> String,
+) -> List(String) {
+ case input {
+ <<b, rest:bytes>> -> [byte_to_string(b), ..do_inspect(rest, byte_to_string)]
+ _ -> []
+ }
+}