diff options
author | Louis Pilfold <louis@lpil.uk> | 2024-12-17 15:16:17 +0000 |
---|---|---|
committer | Louis Pilfold <louis@lpil.uk> | 2024-12-17 15:16:57 +0000 |
commit | 8dc4112e8ee95d9e07f651e7b85c8d7f76a9dba0 (patch) | |
tree | 2d65c2dda0c0bba8921625b18e8f2649437675ec | |
parent | f13c14377b746f082d6f704a13fe877a363e6884 (diff) | |
download | gleam_stdlib-8dc4112e8ee95d9e07f651e7b85c8d7f76a9dba0.tar.gz gleam_stdlib-8dc4112e8ee95d9e07f651e7b85c8d7f76a9dba0.zip |
Remove deprecated builders
-rw-r--r-- | CHANGELOG.md | 2 | ||||
-rw-r--r-- | src/gleam/bytes_builder.gleam | 174 | ||||
-rw-r--r-- | src/gleam/string_builder.gleam | 212 | ||||
-rw-r--r-- | test/gleam/bytes_builder_test.gleam | 94 | ||||
-rw-r--r-- | test/gleam/string_builder_test.gleam | 149 |
5 files changed, 2 insertions, 629 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index defb515..0c3cb31 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,7 +1,9 @@ # Changelog ## v0.48.0 - 2024-12-17 + - Fixed a bug where `string.utf_codepoint` would erronously accept negative input. +- The deprecated `string_builder` and `bytes_builder` modules have been removed. ## v0.47.0 - 2024-12-10 diff --git a/src/gleam/bytes_builder.gleam b/src/gleam/bytes_builder.gleam deleted file mode 100644 index 330865f..0000000 --- a/src/gleam/bytes_builder.gleam +++ /dev/null @@ -1,174 +0,0 @@ -//// `BytesBuilder` is a type used for efficiently building binary content to be -//// written to a file or a socket. Internally it is represented as tree so to -//// append or prepend to a bytes builder is a constant time operation that -//// allocates a new node in the tree without copying any of the content. When -//// writing to an output stream the tree is traversed and the content is sent -//// directly rather than copying it into a single buffer beforehand. -//// -//// If we append one bit array to another the bit arrays must be copied to a -//// new location in memory so that they can sit together. This behaviour -//// enables efficient reading of the data but copying can be expensive, -//// especially if we want to join many bit arrays together. -//// -//// BytesBuilder is different in that it can be joined together in constant -//// time using minimal memory, and then can be efficiently converted to a -//// bit array using the `to_bit_array` function. -//// -//// Byte builders are always byte aligned, so that a number of bits that is not -//// divisible by 8 will be padded with 0s. -//// -//// On Erlang this type is compatible with Erlang's iolists. - -// TODO: pad bit arrays to byte boundaries when adding to a builder. -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 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() -> 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: 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: BytesTree, suffix second: BitArray) -> BytesTree { - bytes_tree.append_tree(first, bytes_tree.from_bit_array(second)) -} - -/// Prepends a builder onto the start of another. -/// -/// Runs in constant time. -/// -@deprecated("The `bytes_builder` module has been deprecated, use `bytes_tree.prepend_tree` instead.") -pub fn prepend_builder( - to second: BytesTree, - prefix first: BytesTree, -) -> BytesTree { - bytes_tree.append_tree(first, second) -} - -/// Appends a builder onto the end of another. -/// -/// Runs in constant time. -/// -@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: BytesTree, - suffix second: BytesTree, -) -> BytesTree { - bytes_tree.append_tree(first, second) -} - -/// Prepends a string onto the start of a builder. -/// -/// Runs in constant time when running on Erlang. -/// 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: BytesTree, prefix first: String) -> BytesTree { - bytes_tree.append_tree(bytes_tree.from_string(first), second) -} - -/// Appends a string onto the end of a builder. -/// -/// Runs in constant time when running on Erlang. -/// 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: BytesTree, suffix second: String) -> BytesTree { - bytes_tree.append_tree(first, bytes_tree.from_string(second)) -} - -/// Joins a list of builders into a single builder. -/// -/// Runs in constant time. -/// -@deprecated("The `bytes_builder` module has been deprecated, use `bytes_tree.concat` instead.") -@external(erlang, "gleam_stdlib", "identity") -pub fn concat(builders: List(BytesTree)) -> BytesTree { - bytes_tree.concat(builders) -} - -/// Joins a list of bit arrays into a single builder. -/// -/// Runs in constant time. -/// -@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)) -> BytesTree { - bytes_tree.concat_bit_arrays(bits) -} - -/// Creates a new builder from a string. -/// -/// Runs in constant time when running on Erlang. -/// Runs in linear time otherwise. -/// -@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) -> BytesTree { - bytes_tree.from_string(string) -} - -/// Creates a new builder from a string builder. -/// -/// Runs in constant time when running on Erlang. -/// Runs in linear time otherwise. -/// -@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: StringTree) -> BytesTree { - bytes_tree.from_string_tree(builder) -} - -/// Creates a new builder from a bit array. -/// -/// Runs in constant time. -/// -@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) -> BytesTree { - bytes_tree.from_bit_array(bits) -} - -/// Turns an builder into a bit array. -/// -/// Runs in linear time. -/// -/// When running on Erlang this function is implemented natively by the -/// virtual machine and is highly optimised. -/// -@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: BytesTree) -> BitArray { - bytes_tree.to_bit_array(builder) -} - -/// Returns the size of the builder's content in bytes. -/// -/// Runs in linear time. -/// -@deprecated("The `bytes_builder` module has been deprecated, use `bytes_tree.byte_size` instead.") -@external(erlang, "erlang", "iolist_size") -pub fn byte_size(builder: BytesTree) -> Int { - bytes_tree.byte_size(builder) -} diff --git a/src/gleam/string_builder.gleam b/src/gleam/string_builder.gleam deleted file mode 100644 index bf89bd3..0000000 --- a/src/gleam/string_builder.gleam +++ /dev/null @@ -1,212 +0,0 @@ -import gleam/string_tree.{type StringTree} - -/// `StringBuilder` is a type used for efficiently building text content to be -/// written to a file or a socket. Internally it is represented as tree so to -/// append or prepend to a string builder is a constant time operation that -/// allocates a new node in the tree without copying any of the content. When -/// writing to an output stream the tree is traversed and the content is sent -/// directly rather than copying it into a single buffer beforehand. -/// -/// On Erlang this type is compatible with Erlang's iodata. On JavaScript this -/// type is compatible with normal strings. -/// -/// The BEAM virtual machine has an optimisation for appending strings, where it -/// will mutate the string buffer when safe to do so, so if you are looking to -/// build a string through appending many small strings then you may get better -/// performance by not using a string builder. Always benchmark your performance -/// sensitive code. -/// -@deprecated("The `string_builder` module has been deprecated, use the `string_tree.StringTree` type instead.") -pub type StringBuilder = - StringTree - -/// Create an empty `StringBuilder`. Useful as the start of a pipe chaining many -/// builders together. -/// -@deprecated("The `string_builder` module has been deprecated, use `string_tree.new` instead.") -pub fn new() -> StringTree { - string_tree.from_strings([]) -} - -/// Prepends a `String` onto the start of some `StringBuilder`. -/// -/// Runs in constant time. -/// -@deprecated("The `string_builder` module has been deprecated, use `string_tree.prepend` instead.") -pub fn prepend(to builder: StringTree, prefix prefix: String) -> StringTree { - string_tree.append_tree(string_tree.from_string(prefix), builder) -} - -/// Appends a `String` onto the end of some `StringBuilder`. -/// -/// Runs in constant time. -/// -@deprecated("The `string_builder` module has been deprecated, use `string_tree.append` instead.") -pub fn append(to builder: StringTree, suffix second: String) -> StringTree { - string_tree.append_tree(builder, string_tree.from_string(second)) -} - -/// Prepends some `StringBuilder` onto the start of another. -/// -/// Runs in constant time. -/// -@deprecated("The `string_builder` module has been deprecated, use `string_tree.prepend_tree` instead.") -pub fn prepend_builder( - to builder: StringTree, - prefix prefix: StringTree, -) -> StringTree { - string_tree.prepend_tree(builder, prefix) -} - -/// Appends some `StringBuilder` onto the end of another. -/// -/// Runs in constant time. -/// -@deprecated("The `string_builder` module has been deprecated, use `string_tree.append_tree` instead.") -pub fn append_builder( - to builder: StringTree, - suffix suffix: StringTree, -) -> StringTree { - string_tree.append_tree(builder, suffix) -} - -/// Converts a list of strings into a builder. -/// -/// Runs in constant time. -/// -@deprecated("The `string_builder` module has been deprecated, use `string_tree.from_strings` instead.") -pub fn from_strings(strings: List(String)) -> StringTree { - string_tree.from_strings(strings) -} - -/// Joins a list of builders into a single builder. -/// -/// Runs in constant time. -/// -@deprecated("The `string_builder` module has been deprecated, use `string_tree.concat` instead.") -pub fn concat(builders: List(StringTree)) -> StringTree { - string_tree.concat(builders) -} - -/// Converts a string into a builder. -/// -/// Runs in constant time. -/// -@deprecated("The `string_builder` module has been deprecated, use `string_tree.from_string` instead.") -pub fn from_string(string: String) -> StringTree { - string_tree.from_string(string) -} - -/// Turns an `StringBuilder` into a `String` -/// -/// This function is implemented natively by the virtual machine and is highly -/// optimised. -/// -@deprecated("The `string_builder` module has been deprecated, use `string_tree.to_string` instead.") -pub fn to_string(builder: StringTree) -> String { - string_tree.to_string(builder) -} - -/// Returns the size of the `StringBuilder` in bytes. -/// -@deprecated("The `string_builder` module has been deprecated, use `string_tree.byte_size` instead.") -pub fn byte_size(builder: StringTree) -> Int { - string_tree.byte_size(builder) -} - -/// Joins the given builders into a new builder separated with the given string -/// -@deprecated("The `string_builder` module has been deprecated, use `string_tree.join` instead.") -pub fn join(builders: List(StringTree), with sep: String) -> StringTree { - string_tree.join(builders, sep) -} - -/// Converts a builder to a new builder where the contents have been -/// lowercased. -/// -@deprecated("The `string_builder` module has been deprecated, use `string_tree.lowercase` instead.") -pub fn lowercase(builder: StringTree) -> StringTree { - string_tree.lowercase(builder) -} - -/// Converts a builder to a new builder where the contents have been -/// uppercased. -/// -@deprecated("The `string_builder` module has been deprecated, use `string_tree.uppercase` instead.") -pub fn uppercase(builder: StringTree) -> StringTree { - string_tree.uppercase(builder) -} - -/// Converts a builder to a new builder with the contents reversed. -/// -@deprecated("The `string_builder` module has been deprecated, use `string_tree.reverse` instead.") -pub fn reverse(builder: StringTree) -> StringTree { - string_tree.reverse(builder) -} - -/// Splits a builder on a given pattern into a list of builders. -/// -@deprecated("The `string_builder` module has been deprecated, use `string_tree.split` instead.") -pub fn split(iodata: StringTree, on pattern: String) -> List(StringTree) { - string_tree.split(iodata, pattern) -} - -/// Replaces all instances of a pattern with a given string substitute. -/// -@deprecated("The `string_builder` module has been deprecated, use `string_tree.replace` instead.") -@external(erlang, "gleam_stdlib", "string_replace") -@external(javascript, "../gleam_stdlib.mjs", "string_replace") -pub fn replace( - in builder: StringTree, - each pattern: String, - with substitute: String, -) -> StringTree - -/// Compares two builders to determine if they have the same textual content. -/// -/// Comparing two iodata using the `==` operator may return `False` even if they -/// have the same content as they may have been build in different ways, so -/// using this function is often preferred. -/// -/// ## Examples -/// -/// ```gleam -/// from_strings(["a", "b"]) == from_string("ab") -/// // -> False -/// ``` -/// -/// ```gleam -/// is_equal(from_strings(["a", "b"]), from_string("ab")) -/// // -> True -/// ``` -/// -@deprecated("The `string_builder` module has been deprecated, use `string_tree.is_equal` instead.") -@external(erlang, "string", "equal") -pub fn is_equal(a: StringTree, b: StringTree) -> Bool { - a == b -} - -/// Inspects a builder to determine if it is equivalent to an empty string. -/// -/// ## Examples -/// -/// ```gleam -/// from_string("ok") |> is_empty -/// // -> False -/// ``` -/// -/// ```gleam -/// from_string("") |> is_empty -/// // -> True -/// ``` -/// -/// ```gleam -/// from_strings([]) |> is_empty -/// // -> True -/// ``` -/// -@deprecated("The `string_builder` module has been deprecated, use `string_tree.is_empty` instead.") -@external(erlang, "string", "is_empty") -pub fn is_empty(builder: StringTree) -> Bool { - string_tree.from_string("") == builder -} diff --git a/test/gleam/bytes_builder_test.gleam b/test/gleam/bytes_builder_test.gleam deleted file mode 100644 index 0c35dbe..0000000 --- a/test/gleam/bytes_builder_test.gleam +++ /dev/null @@ -1,94 +0,0 @@ -import gleam/bytes_builder -import gleam/should -import gleam/string_builder - -pub fn builder_test() { - let data = - bytes_builder.from_bit_array(<<1>>) - |> bytes_builder.append(<<2>>) - |> bytes_builder.append(<<3>>) - |> bytes_builder.prepend(<<0>>) - - data - |> bytes_builder.to_bit_array - |> should.equal(<<0, 1, 2, 3>>) - - data - |> bytes_builder.byte_size - |> should.equal(4) -} - -pub fn builder_with_strings_test() { - let data = - bytes_builder.from_bit_array(<<1>>) - |> bytes_builder.append_string("2") - |> bytes_builder.append_string("3") - |> bytes_builder.prepend_string("0") - - data - |> bytes_builder.to_bit_array - |> should.equal(<<"0":utf8, 1, "2":utf8, "3":utf8>>) - - data - |> bytes_builder.byte_size - |> should.equal(4) -} - -pub fn builder_with_builders_test() { - let data = - bytes_builder.from_bit_array(<<1>>) - |> bytes_builder.append_builder(bytes_builder.from_bit_array(<<2>>)) - |> bytes_builder.append_builder(bytes_builder.from_bit_array(<<3>>)) - |> bytes_builder.prepend_builder(bytes_builder.from_bit_array(<<0>>)) - - data - |> bytes_builder.to_bit_array - |> should.equal(<<0, 1, 2, 3>>) - - data - |> bytes_builder.byte_size - |> should.equal(4) -} - -pub fn concat_test() { - [ - bytes_builder.from_bit_array(<<1, 2>>), - bytes_builder.from_bit_array(<<3, 4>>), - bytes_builder.from_bit_array(<<5, 6>>), - ] - |> bytes_builder.concat - |> bytes_builder.to_bit_array - |> should.equal(<<1, 2, 3, 4, 5, 6>>) -} - -pub fn concat_bit_arrays_test() { - bytes_builder.concat_bit_arrays([<<"h":utf8>>, <<"e":utf8>>, <<"y":utf8>>]) - |> bytes_builder.to_bit_array - |> should.equal(<<"hey":utf8>>) -} - -pub fn from_bit_array() { - // Regression test: no additional modification of the builder - bytes_builder.from_bit_array(<<>>) - |> bytes_builder.to_bit_array - |> should.equal(<<>>) -} - -pub fn from_string_test() { - // Regression test: no additional modification of the builder - bytes_builder.from_string("") - |> bytes_builder.to_bit_array - |> should.equal(<<>>) -} - -pub fn new_test() { - bytes_builder.new() - |> bytes_builder.to_bit_array - |> should.equal(<<>>) -} - -pub fn from_string_builder_test() { - string_builder.from_string("hello") - |> bytes_builder.from_string_builder - |> should.equal(bytes_builder.from_string("hello")) -} diff --git a/test/gleam/string_builder_test.gleam b/test/gleam/string_builder_test.gleam deleted file mode 100644 index f8a50b7..0000000 --- a/test/gleam/string_builder_test.gleam +++ /dev/null @@ -1,149 +0,0 @@ -import gleam/should -import gleam/string_builder - -pub fn string_builder_test() { - let data = - string_builder.from_string("ello") - |> string_builder.append(",") - |> string_builder.append(" world!") - |> string_builder.prepend("H") - - data - |> string_builder.to_string - |> should.equal("Hello, world!") - - data - |> string_builder.byte_size - |> should.equal(13) - - let data = - string_builder.from_string("ello") - |> string_builder.append_builder(string_builder.from_string(",")) - |> string_builder.append_builder( - string_builder.concat([ - string_builder.from_string(" wo"), - string_builder.from_string("rld!"), - ]), - ) - |> string_builder.prepend_builder(string_builder.from_string("H")) - - data - |> string_builder.to_string - |> should.equal("Hello, world!") - - data - |> string_builder.byte_size - |> should.equal(13) -} - -pub fn reverse_test() { - "Ĺo͂řȩm̅" - |> string_builder.from_string - |> string_builder.reverse - |> string_builder.reverse - |> string_builder.to_string - |> should.equal("Ĺo͂řȩm̅") - - "Ĺo͂řȩm̅" - |> string_builder.from_string - |> string_builder.reverse - |> string_builder.to_string - |> should.equal("m̅ȩřo͂Ĺ") - - "👶🏿" - |> string_builder.from_string - |> string_builder.reverse - |> string_builder.reverse - |> string_builder.to_string - |> should.equal("👶🏿") - - "👶🏿" - |> string_builder.from_string - |> string_builder.reverse - |> string_builder.to_string - |> should.equal("👶🏿") -} - -pub fn lowercase_test() { - ["Gleam", "Gleam"] - |> string_builder.from_strings - |> string_builder.lowercase - |> string_builder.to_string - |> should.equal("gleamgleam") -} - -pub fn uppercase_test() { - ["Gleam", "Gleam"] - |> string_builder.from_strings - |> string_builder.uppercase - |> string_builder.to_string - |> should.equal("GLEAMGLEAM") -} - -pub fn split_test() { - "Gleam,Erlang,Elixir" - |> string_builder.from_string - |> string_builder.split(",") - |> should.equal([ - string_builder.from_string("Gleam"), - string_builder.from_string("Erlang"), - string_builder.from_string("Elixir"), - ]) - - ["Gleam, Erl", "ang,Elixir"] - |> string_builder.from_strings - |> string_builder.split(", ") - |> should.equal([ - string_builder.from_string("Gleam"), - string_builder.from_strings(["Erl", "ang,Elixir"]), - ]) -} - -pub fn is_equal_test() { - string_builder.from_string("12") - |> string_builder.is_equal(string_builder.from_strings(["1", "2"])) - |> should.be_true - - string_builder.from_string("12") - |> string_builder.is_equal(string_builder.from_string("12")) - |> should.be_true - - string_builder.from_string("12") - |> string_builder.is_equal(string_builder.from_string("2")) - |> should.be_false -} - -pub fn is_empty_test() { - string_builder.from_string("") - |> string_builder.is_empty - |> should.be_true - - string_builder.from_string("12") - |> string_builder.is_empty - |> should.be_false - - string_builder.from_strings([]) - |> string_builder.is_empty - |> should.be_true - - string_builder.from_strings(["", ""]) - |> string_builder.is_empty - |> should.be_true -} - -pub fn new_test() { - string_builder.new() - |> string_builder.to_string - |> should.equal("") -} - -pub fn join_test() { - [ - string_builder.from_string("Gleam"), - string_builder.from_string("Elixir"), - string_builder.from_string("Erlang"), - ] - |> string_builder.join(", ") - |> string_builder.to_string - |> should.equal("Gleam, Elixir, Erlang") -} |