Skip to content

Commit 29652e4

Browse files
richard-vineylpil
authored andcommitted
Switch to string accumulator
1 parent 6ec1de9 commit 29652e4

File tree

1 file changed

+14
-25
lines changed

1 file changed

+14
-25
lines changed

src/gleam/bit_array.gleam

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

33
import gleam/int
44
import gleam/string
5-
import gleam/string_builder.{type StringBuilder}
65

76
/// Converts a UTF-8 `String` type into a `BitArray`.
87
///
@@ -171,42 +170,32 @@ pub fn base16_decode(input: String) -> Result(BitArray, Nil)
171170
/// ```
172171
///
173172
pub fn inspect(input: BitArray) -> String {
174-
string_builder.new()
175-
|> string_builder.append("<<")
176-
|> do_inspect(input)
177-
|> string_builder.append(">>")
178-
|> string_builder.to_string
173+
do_inspect(input, "<<") <> ">>"
179174
}
180175

181-
fn do_inspect(builder: StringBuilder, input: BitArray) -> StringBuilder {
176+
fn do_inspect(input: BitArray, accumulator: String) -> String {
182177
case input {
183-
<<>> -> builder
178+
<<>> -> accumulator
184179

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)")
180+
<<x:size(1)>> -> accumulator <> int.to_string(x) <> ":size(1)"
181+
<<x:size(2)>> -> accumulator <> int.to_string(x) <> ":size(2)"
182+
<<x:size(3)>> -> accumulator <> int.to_string(x) <> ":size(3)"
183+
<<x:size(4)>> -> accumulator <> int.to_string(x) <> ":size(4)"
184+
<<x:size(5)>> -> accumulator <> int.to_string(x) <> ":size(5)"
185+
<<x:size(6)>> -> accumulator <> int.to_string(x) <> ":size(6)"
186+
<<x:size(7)>> -> accumulator <> int.to_string(x) <> ":size(7)"
192187

193188
<<x, rest:bits>> -> {
194189
let suffix = case rest {
195190
<<>> -> ""
196191
_ -> ", "
197192
}
198193

199-
builder
200-
|> do_inspect_int(x, suffix)
201-
|> do_inspect(rest)
194+
let accumulator = accumulator <> int.to_string(x) <> suffix
195+
196+
do_inspect(rest, accumulator)
202197
}
203198

204-
_ -> builder
199+
_ -> accumulator
205200
}
206201
}
207-
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)
212-
}

0 commit comments

Comments
 (0)