Skip to content

Commit 6a93763

Browse files
richard-vineylpil
authored andcommitted
Remove base16 variant. Add support for partial bytes.
1 parent bd78550 commit 6a93763

File tree

2 files changed

+43
-42
lines changed

2 files changed

+43
-42
lines changed

src/gleam/bit_array.gleam

Lines changed: 37 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import gleam/int
44
import gleam/string
5+
import gleam/string_builder.{type StringBuilder}
56

67
/// Converts a UTF-8 `String` type into a `BitArray`.
78
///
@@ -164,46 +165,48 @@ pub fn base16_decode(input: String) -> Result(BitArray, Nil)
164165
/// ```gleam
165166
/// inspect(<<0, 20, 0x20, 255>>)
166167
/// // -> "<<0, 20, 32, 255>>"
168+
///
169+
/// inspect(<<100, 5:3>>)
170+
/// // -> "<<100, 5:size(3)>>"
167171
/// ```
168172
///
169173
pub fn inspect(input: BitArray) -> String {
170-
"<<"
171-
<> input
172-
|> do_inspect(int.to_string)
173-
|> string.join(", ")
174-
<> ">>"
174+
string_builder.new()
175+
|> string_builder.append("<<")
176+
|> do_inspect(input)
177+
|> string_builder.append(">>")
178+
|> string_builder.to_string
175179
}
176180

177-
/// Converts a bit array to a string containing the hexadecimal value of each
178-
/// byte.
179-
///
180-
/// ## Examples
181-
///
182-
/// ```gleam
183-
/// inspect(<<0, 20, 0x20, 255>>)
184-
/// // -> "<<00 14 20 FF>>"
185-
/// ```
186-
///
187-
pub fn inspect_base16(input: BitArray) -> String {
188-
let byte_to_hex_string = fn(b) {
189-
b
190-
|> int.to_base16
191-
|> string.pad_left(2, "0")
181+
fn do_inspect(builder: StringBuilder, input: BitArray) -> StringBuilder {
182+
case input {
183+
<<>> -> builder
184+
185+
<<x:size(1)>> -> do_inspect_int(builder, x, ":size(1)")
186+
<<x:size(2)>> -> do_inspect_int(builder, x, ":size(2)")
187+
<<x:size(3)>> -> do_inspect_int(builder, x, ":size(3)")
188+
<<x:size(4)>> -> do_inspect_int(builder, x, ":size(4)")
189+
<<x:size(5)>> -> do_inspect_int(builder, x, ":size(5)")
190+
<<x:size(6)>> -> do_inspect_int(builder, x, ":size(6)")
191+
<<x:size(7)>> -> do_inspect_int(builder, x, ":size(7)")
192+
193+
<<x, rest:bits>> -> {
194+
let suffix = case rest {
195+
<<>> -> ""
196+
_ -> ", "
197+
}
198+
199+
builder
200+
|> do_inspect_int(x, suffix)
201+
|> do_inspect(rest)
202+
}
203+
204+
_ -> builder
192205
}
193-
194-
"<<"
195-
<> input
196-
|> do_inspect(byte_to_hex_string)
197-
|> string.join(" ")
198-
<> ">>"
199206
}
200207

201-
fn do_inspect(
202-
input: BitArray,
203-
byte_to_string: fn(Int) -> String,
204-
) -> List(String) {
205-
case input {
206-
<<b, rest:bytes>> -> [byte_to_string(b), ..do_inspect(rest, byte_to_string)]
207-
_ -> []
208-
}
208+
fn do_inspect_int(builder: StringBuilder, value: Int, suffix: String) {
209+
builder
210+
|> string_builder.append(int.to_string(value))
211+
|> string_builder.append(suffix)
209212
}

test/gleam/bit_array_test.gleam

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -289,15 +289,13 @@ pub fn inspect_test() {
289289

290290
bit_array.inspect(<<0, 20, 0x20, 255>>)
291291
|> should.equal("<<0, 20, 32, 255>>")
292-
}
293292

294-
pub fn inspect_base16_test() {
295-
bit_array.inspect_base16(<<>>)
296-
|> should.equal("<<>>")
293+
bit_array.inspect(<<4:5>>)
294+
|> should.equal("<<4:size(5)>>")
297295

298-
bit_array.inspect_base16(<<0x80>>)
299-
|> should.equal("<<80>>")
296+
bit_array.inspect(<<100, 5:3>>)
297+
|> should.equal("<<100, 5:size(3)>>")
300298

301-
bit_array.inspect_base16(<<0, 20, 0x20, 255>>)
302-
|> should.equal("<<00 14 20 FF>>")
299+
bit_array.inspect(<<5:3, 11:4, 1:2>>)
300+
|> should.equal("<<182, 1:size(1)>>")
303301
}

0 commit comments

Comments
 (0)