Skip to content

Commit

Permalink
make string/bytes_builder aliases of tree counterparts
Browse files Browse the repository at this point in the history
  • Loading branch information
giacomocavalieri authored and lpil committed Nov 17, 2024
1 parent ec9cc3f commit b19f32b
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 180 deletions.
114 changes: 36 additions & 78 deletions src/gleam/bytes_builder.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -20,41 +20,37 @@
//// On Erlang this type is compatible with Erlang's iolists.

// TODO: pad bit arrays to byte boundaries when adding to a builder.
import gleam/bit_array
import gleam/list
import gleam/string_builder.{type StringBuilder}
import gleam/bytes_tree.{type BytesTree}
import gleam/string_tree.{type StringTree}

@deprecated("The `bytes_builder` module has been deprecated, use the `bytes_tree.BytesTree` type instead.")
pub opaque type BytesBuilder {
Bytes(BitArray)
Text(StringBuilder)
Many(List(BytesBuilder))
}
pub type BytesBuilder =
BytesTree

/// Create an empty `BytesBuilder`. Useful as the start of a pipe chaining many
/// builders together.
///
@deprecated("The `bytes_builder` module has been deprecated, use `bytes_tree.new` instead.")
pub fn new() -> BytesBuilder {
concat([])
pub fn new() -> BytesTree {
bytes_tree.concat([])
}

/// Prepends a bit array to the start of a builder.
///
/// Runs in constant time.
///
@deprecated("The `bytes_builder` module has been deprecated, use `bytes_tree.prepend` instead.")
pub fn prepend(to second: BytesBuilder, prefix first: BitArray) -> BytesBuilder {
append_builder(from_bit_array(first), second)
pub fn prepend(to second: BytesTree, prefix first: BitArray) -> BytesTree {
bytes_tree.append_tree(bytes_tree.from_bit_array(first), second)
}

/// Appends a bit array to the end of a builder.
///
/// Runs in constant time.
///
@deprecated("The `bytes_builder` module has been deprecated, use `bytes_tree.append` instead.")
pub fn append(to first: BytesBuilder, suffix second: BitArray) -> BytesBuilder {
append_builder(first, from_bit_array(second))
pub fn append(to first: BytesTree, suffix second: BitArray) -> BytesTree {
bytes_tree.append_tree(first, bytes_tree.from_bit_array(second))
}

/// Prepends a builder onto the start of another.
Expand All @@ -63,10 +59,10 @@ pub fn append(to first: BytesBuilder, suffix second: BitArray) -> BytesBuilder {
///
@deprecated("The `bytes_builder` module has been deprecated, use `bytes_tree.prepend_tree` instead.")
pub fn prepend_builder(
to second: BytesBuilder,
prefix first: BytesBuilder,
) -> BytesBuilder {
append_builder(first, second)
to second: BytesTree,
prefix first: BytesTree,
) -> BytesTree {
bytes_tree.append_tree(first, second)
}

/// Appends a builder onto the end of another.
Expand All @@ -76,13 +72,10 @@ pub fn prepend_builder(
@deprecated("The `bytes_builder` module has been deprecated, use `bytes_tree.append_tree` instead.")
@external(erlang, "gleam_stdlib", "iodata_append")
pub fn append_builder(
to first: BytesBuilder,
suffix second: BytesBuilder,
) -> BytesBuilder {
case second {
Many(builders) -> Many([first, ..builders])
_ -> Many([first, second])
}
to first: BytesTree,
suffix second: BytesTree,
) -> BytesTree {
bytes_tree.append_tree(first, second)
}

/// Prepends a string onto the start of a builder.
Expand All @@ -91,11 +84,8 @@ pub fn append_builder(
/// Runs in linear time with the length of the string otherwise.
///
@deprecated("The `bytes_builder` module has been deprecated, use `bytes_tree.prepend_string` instead.")
pub fn prepend_string(
to second: BytesBuilder,
prefix first: String,
) -> BytesBuilder {
append_builder(from_string(first), second)
pub fn prepend_string(to second: BytesTree, prefix first: String) -> BytesTree {
bytes_tree.append_tree(bytes_tree.from_string(first), second)
}

/// Appends a string onto the end of a builder.
Expand All @@ -104,11 +94,8 @@ pub fn prepend_string(
/// Runs in linear time with the length of the string otherwise.
///
@deprecated("The `bytes_builder` module has been deprecated, use `bytes_tree.append_string` instead.")
pub fn append_string(
to first: BytesBuilder,
suffix second: String,
) -> BytesBuilder {
append_builder(first, from_string(second))
pub fn append_string(to first: BytesTree, suffix second: String) -> BytesTree {
bytes_tree.append_tree(first, bytes_tree.from_string(second))
}

/// Joins a list of builders into a single builder.
Expand All @@ -117,8 +104,8 @@ pub fn append_string(
///
@deprecated("The `bytes_builder` module has been deprecated, use `bytes_tree.concat` instead.")
@external(erlang, "gleam_stdlib", "identity")
pub fn concat(builders: List(BytesBuilder)) -> BytesBuilder {
Many(builders)
pub fn concat(builders: List(BytesTree)) -> BytesTree {
bytes_tree.concat(builders)
}

/// Joins a list of bit arrays into a single builder.
Expand All @@ -127,10 +114,8 @@ pub fn concat(builders: List(BytesBuilder)) -> BytesBuilder {
///
@deprecated("The `bytes_builder` module has been deprecated, use `bytes_tree.concat_bit_arrays` instead.")
@external(erlang, "gleam_stdlib", "identity")
pub fn concat_bit_arrays(bits: List(BitArray)) -> BytesBuilder {
bits
|> list.map(fn(b) { from_bit_array(b) })
|> concat()
pub fn concat_bit_arrays(bits: List(BitArray)) -> BytesTree {
bytes_tree.concat_bit_arrays(bits)
}

/// Creates a new builder from a string.
Expand All @@ -140,8 +125,8 @@ pub fn concat_bit_arrays(bits: List(BitArray)) -> BytesBuilder {
///
@deprecated("The `bytes_builder` module has been deprecated, use `bytes_tree.from_string` instead.")
@external(erlang, "gleam_stdlib", "wrap_list")
pub fn from_string(string: String) -> BytesBuilder {
Text(string_builder.from_string(string))
pub fn from_string(string: String) -> BytesTree {
bytes_tree.from_string(string)
}

/// Creates a new builder from a string builder.
Expand All @@ -151,8 +136,8 @@ pub fn from_string(string: String) -> BytesBuilder {
///
@deprecated("The `bytes_builder` module has been deprecated, use `bytes_tree.from_string_tree` instead.")
@external(erlang, "gleam_stdlib", "wrap_list")
pub fn from_string_builder(builder: StringBuilder) -> BytesBuilder {
Text(builder)
pub fn from_string_builder(builder: StringTree) -> BytesTree {
bytes_tree.from_string_tree(builder)
}

/// Creates a new builder from a bit array.
Expand All @@ -161,8 +146,8 @@ pub fn from_string_builder(builder: StringBuilder) -> BytesBuilder {
///
@deprecated("The `bytes_builder` module has been deprecated, use `bytes_tree.from_bit_array` instead.")
@external(erlang, "gleam_stdlib", "wrap_list")
pub fn from_bit_array(bits: BitArray) -> BytesBuilder {
Bytes(bits)
pub fn from_bit_array(bits: BitArray) -> BytesTree {
bytes_tree.from_bit_array(bits)
}

/// Turns an builder into a bit array.
Expand All @@ -174,33 +159,8 @@ pub fn from_bit_array(bits: BitArray) -> BytesBuilder {
///
@deprecated("The `bytes_builder` module has been deprecated, use `bytes_tree.to_bit_array` instead.")
@external(erlang, "erlang", "list_to_bitstring")
pub fn to_bit_array(builder: BytesBuilder) -> BitArray {
[[builder]]
|> to_list([])
|> list.reverse
|> bit_array.concat
}

fn to_list(
stack: List(List(BytesBuilder)),
acc: List(BitArray),
) -> List(BitArray) {
case stack {
[] -> acc

[[], ..remaining_stack] -> to_list(remaining_stack, acc)

[[Bytes(bits), ..rest], ..remaining_stack] ->
to_list([rest, ..remaining_stack], [bits, ..acc])

[[Text(builder), ..rest], ..remaining_stack] -> {
let bits = bit_array.from_string(string_builder.to_string(builder))
to_list([rest, ..remaining_stack], [bits, ..acc])
}

[[Many(builders), ..rest], ..remaining_stack] ->
to_list([builders, rest, ..remaining_stack], acc)
}
pub fn to_bit_array(builder: BytesTree) -> BitArray {
bytes_tree.to_bit_array(builder)
}

/// Returns the size of the builder's content in bytes.
Expand All @@ -209,8 +169,6 @@ fn to_list(
///
@deprecated("The `bytes_builder` module has been deprecated, use `bytes_tree.byte_size` instead.")
@external(erlang, "erlang", "iolist_size")
pub fn byte_size(builder: BytesBuilder) -> Int {
[[builder]]
|> to_list([])
|> list.fold(0, fn(acc, builder) { bit_array.byte_size(builder) + acc })
pub fn byte_size(builder: BytesTree) -> Int {
bytes_tree.byte_size(builder)
}
Loading

0 comments on commit b19f32b

Please sign in to comment.