Fix stupid TCO fail

This commit is contained in:
Mikko Ahlroth 2023-06-16 20:16:20 +03:00
parent 1330319e80
commit 7c1f67a46f
4 changed files with 52 additions and 16 deletions

View file

@ -1,3 +1,8 @@
4.0.1
-----
* Fixed a call stack error on the JavaScript target
4.0.0 4.0.0
----- -----

View file

@ -1,5 +1,5 @@
name = "glentities" name = "glentities"
version = "4.0.0" version = "4.0.1"
description = "HTML entity encoder/decoder for Gleam" description = "HTML entity encoder/decoder for Gleam"
# Fill out these fields if you intend to generate HTML documentation or publish # Fill out these fields if you intend to generate HTML documentation or publish

View file

@ -2332,7 +2332,14 @@ pub fn encode_html_body(text: String, acc: StringBuilder) -> String {
">" <> rest -> encode_html_body(rest, string_builder.append(acc, "&gt;")) ">" <> rest -> encode_html_body(rest, string_builder.append(acc, "&gt;"))
"\"" <> rest -> encode_html_body(rest, string_builder.append(acc, "&quot;")) "\"" <> rest -> encode_html_body(rest, string_builder.append(acc, "&quot;"))
"'" <> rest -> encode_html_body(rest, string_builder.append(acc, "&#39;")) "'" <> rest -> encode_html_body(rest, string_builder.append(acc, "&#39;"))
other -> encode_other_case(other, acc, encode_html_body) other -> {
let maybe_grapheme = string.pop_grapheme(other)
case maybe_grapheme {
Ok(#(grapheme, rest)) ->
encode_html_body(rest, string_builder.append(acc, grapheme))
Error(Nil) -> string_builder.to_string(acc)
}
}
} }
} }
@ -4074,19 +4081,13 @@ pub fn encode_named(text: String, acc: StringBuilder) -> String {
"𝓏" <> rest -> encode_named(rest, string_builder.append(acc, "&zscr;")) "𝓏" <> rest -> encode_named(rest, string_builder.append(acc, "&zscr;"))
"" <> rest -> encode_named(rest, string_builder.append(acc, "&zwj;")) "" <> rest -> encode_named(rest, string_builder.append(acc, "&zwj;"))
"" <> rest -> encode_named(rest, string_builder.append(acc, "&zwnj;")) "" <> rest -> encode_named(rest, string_builder.append(acc, "&zwnj;"))
other -> encode_other_case(other, acc, encode_named) other -> {
} let maybe_grapheme = string.pop_grapheme(other)
} case maybe_grapheme {
Ok(#(grapheme, rest)) ->
fn encode_other_case( encode_named(rest, string_builder.append(acc, grapheme))
text: String, Error(Nil) -> string_builder.to_string(acc)
acc: StringBuilder, }
continue_callback: fn(String, StringBuilder) -> String, }
) -> String {
let maybe_grapheme = string.pop_grapheme(text)
case maybe_grapheme {
Ok(#(grapheme, rest)) ->
continue_callback(rest, string_builder.append(acc, grapheme))
Error(Nil) -> string_builder.to_string(acc)
} }
} }

View file

@ -1,3 +1,4 @@
import gleam/string
import gleeunit import gleeunit
import gleeunit/should import gleeunit/should
import glentities import glentities
@ -74,3 +75,32 @@ pub fn roundtrip_hex_test() {
input, input,
) )
} }
pub fn tco_test() {
let input =
string.repeat(
"I will never try to be clever in tail call recursive languages again",
100,
)
should.equal(
input
|> glentities.encode(glentities.Hex)
|> glentities.decode(),
input,
)
should.equal(
input
|> glentities.encode(glentities.Named)
|> glentities.decode(),
input,
)
should.equal(
input
|> glentities.encode(glentities.HTMLBody)
|> glentities.decode(),
input,
)
}