-module(glugify@internal@optimized_processors). -compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]). -define(FILEPATH, "src/glugify/internal/optimized_processors.gleam"). -export([optimized_normalize_whitespace/1, optimized_apply_separators/2, optimized_remove_invalid_chars/2, optimized_apply_custom_replacements/2, batch_process_with_tree/2]). -if(?OTP_RELEASE >= 27). -define(MODULEDOC(Str), -moduledoc(Str)). -define(DOC(Str), -doc(Str)). -else. -define(MODULEDOC(Str), -compile([])). -define(DOC(Str), -compile([])). -endif. ?MODULEDOC(false). -file("src/glugify/internal/optimized_processors.gleam", 47). ?DOC(false). -spec is_whitespace(binary()) -> boolean(). is_whitespace(Char) -> (((Char =:= <<" "/utf8>>) orelse (Char =:= <<"\t"/utf8>>)) orelse (Char =:= <<"\n"/utf8>>)) orelse (Char =:= <<"\r"/utf8>>). -file("src/glugify/internal/optimized_processors.gleam", 16). ?DOC(false). -spec normalize_whitespace_with_tree( list(binary()), gleam@string_tree:string_tree(), boolean() ) -> gleam@string_tree:string_tree(). normalize_whitespace_with_tree(Graphemes, Builder, Last_was_space) -> case Graphemes of [] -> Builder; [Char | Rest] -> case is_whitespace(Char) of true -> case Last_was_space of true -> normalize_whitespace_with_tree(Rest, Builder, true); false -> normalize_whitespace_with_tree( Rest, gleam@string_tree:append(Builder, <<" "/utf8>>), true ) end; false -> normalize_whitespace_with_tree( Rest, gleam@string_tree:append(Builder, Char), false ) end end. -file("src/glugify/internal/optimized_processors.gleam", 6). ?DOC(false). -spec optimized_normalize_whitespace(binary()) -> {ok, binary()} | {error, glugify@errors:slugify_error()}. optimized_normalize_whitespace(Text) -> _pipe = Text, _pipe@1 = gleam@string:to_graphemes(_pipe), _pipe@2 = normalize_whitespace_with_tree( _pipe@1, gleam@string_tree:new(), false ), _pipe@3 = unicode:characters_to_binary(_pipe@2), {ok, _pipe@3}. -file("src/glugify/internal/optimized_processors.gleam", 129). ?DOC(false). -spec is_alphanumeric(binary()) -> boolean(). is_alphanumeric(Char) -> case gleam@string:to_utf_codepoints(Char) of [Codepoint] -> Code = gleam_stdlib:identity(Codepoint), (((Code >= 48) andalso (Code =< 57)) orelse ((Code >= 65) andalso (Code =< 90))) orelse ((Code >= 97) andalso (Code =< 122)); _ -> false end. -file("src/glugify/internal/optimized_processors.gleam", 89). ?DOC(false). -spec apply_separators_with_tree( list(binary()), binary(), gleam@string_tree:string_tree(), boolean() ) -> gleam@string_tree:string_tree(). apply_separators_with_tree(Graphemes, Separator, Builder, Last_was_separator) -> case Graphemes of [] -> Builder; [Char | Rest] -> case is_alphanumeric(Char) of true -> apply_separators_with_tree( Rest, Separator, gleam@string_tree:append(Builder, Char), false ); false -> case Last_was_separator orelse string:is_empty(Builder) of true -> apply_separators_with_tree( Rest, Separator, Builder, true ); false -> case Rest of [] -> Builder; _ -> apply_separators_with_tree( Rest, Separator, gleam@string_tree:append( Builder, Separator ), true ) end end end end. -file("src/glugify/internal/optimized_processors.gleam", 73). ?DOC(false). -spec filter_alphanumeric_with_tree( list(binary()), gleam@string_tree:string_tree() ) -> gleam@string_tree:string_tree(). filter_alphanumeric_with_tree(Graphemes, Builder) -> case Graphemes of [] -> Builder; [Char | Rest] -> case is_alphanumeric(Char) of true -> filter_alphanumeric_with_tree( Rest, gleam@string_tree:append(Builder, Char) ); false -> filter_alphanumeric_with_tree(Rest, Builder) end end. -file("src/glugify/internal/optimized_processors.gleam", 51). ?DOC(false). -spec optimized_apply_separators(binary(), glugify@config:config()) -> {ok, binary()} | {error, glugify@errors:slugify_error()}. optimized_apply_separators(Text, Config) -> case erlang:element(2, Config) of <<""/utf8>> -> _pipe = Text, _pipe@1 = gleam@string:to_graphemes(_pipe), _pipe@2 = filter_alphanumeric_with_tree( _pipe@1, gleam@string_tree:new() ), _pipe@3 = unicode:characters_to_binary(_pipe@2), {ok, _pipe@3}; _ -> _pipe@4 = Text, _pipe@5 = gleam@string:to_graphemes(_pipe@4), _pipe@6 = apply_separators_with_tree( _pipe@5, erlang:element(2, Config), gleam@string_tree:new(), false ), _pipe@7 = unicode:characters_to_binary(_pipe@6), {ok, _pipe@7} end. -file("src/glugify/internal/optimized_processors.gleam", 189). ?DOC(false). -spec is_unicode_char(binary()) -> boolean(). is_unicode_char(Char) -> case gleam@string:to_utf_codepoints(Char) of [Codepoint] -> Code = gleam_stdlib:identity(Codepoint), Code > 127; _ -> false end. -file("src/glugify/internal/optimized_processors.gleam", 182). ?DOC(false). -spec is_alphanumeric_or_unicode(binary(), boolean()) -> boolean(). is_alphanumeric_or_unicode(Char, Allow_unicode) -> case Allow_unicode of true -> is_alphanumeric(Char) orelse is_unicode_char(Char); false -> is_alphanumeric(Char) end. -file("src/glugify/internal/optimized_processors.gleam", 156). ?DOC(false). -spec filter_valid_chars_with_tree( list(binary()), binary(), boolean(), gleam@string_tree:string_tree() ) -> gleam@string_tree:string_tree(). filter_valid_chars_with_tree(Graphemes, Separator, Allow_unicode, Builder) -> case Graphemes of [] -> Builder; [Char | Rest] -> case is_alphanumeric_or_unicode(Char, Allow_unicode) orelse (Char =:= Separator) of true -> filter_valid_chars_with_tree( Rest, Separator, Allow_unicode, gleam@string_tree:append(Builder, Char) ); false -> filter_valid_chars_with_tree( Rest, Separator, Allow_unicode, Builder ) end end. -file("src/glugify/internal/optimized_processors.gleam", 141). ?DOC(false). -spec optimized_remove_invalid_chars(binary(), glugify@config:config()) -> {ok, binary()} | {error, glugify@errors:slugify_error()}. optimized_remove_invalid_chars(Text, Config) -> _pipe = Text, _pipe@1 = gleam@string:to_graphemes(_pipe), _pipe@2 = filter_valid_chars_with_tree( _pipe@1, erlang:element(2, Config), erlang:element(7, Config), gleam@string_tree:new() ), _pipe@3 = unicode:characters_to_binary(_pipe@2), {ok, _pipe@3}. -file("src/glugify/internal/optimized_processors.gleam", 229). ?DOC(false). -spec normalize_spaces_helper( list(binary()), gleam@string_tree:string_tree(), boolean() ) -> gleam@string_tree:string_tree(). normalize_spaces_helper(Graphemes, Builder, Last_was_space) -> case Graphemes of [] -> Builder; [Char | Rest] -> case Char =:= <<" "/utf8>> of true -> case Last_was_space of true -> normalize_spaces_helper(Rest, Builder, true); false -> normalize_spaces_helper( Rest, gleam@string_tree:append(Builder, <<" "/utf8>>), true ) end; false -> normalize_spaces_helper( Rest, gleam@string_tree:append(Builder, Char), false ) end end. -file("src/glugify/internal/optimized_processors.gleam", 222). ?DOC(false). -spec normalize_spaces_after_replacement(binary()) -> binary(). normalize_spaces_after_replacement(Text) -> _pipe = Text, _pipe@1 = gleam@string:to_graphemes(_pipe), _pipe@2 = normalize_spaces_helper(_pipe@1, gleam@string_tree:new(), false), unicode:characters_to_binary(_pipe@2). -file("src/glugify/internal/optimized_processors.gleam", 209). ?DOC(false). -spec apply_replacements_with_tree(binary(), list({binary(), binary()})) -> binary(). apply_replacements_with_tree(Text, Replacements) -> case Replacements of [] -> Text; [{Find, Replace} | Rest] -> Updated = gleam@string:replace(Text, Find, Replace), apply_replacements_with_tree(Updated, Rest) end. -file("src/glugify/internal/optimized_processors.gleam", 199). ?DOC(false). -spec optimized_apply_custom_replacements(binary(), list({binary(), binary()})) -> {ok, binary()} | {error, glugify@errors:slugify_error()}. optimized_apply_custom_replacements(Text, Replacements) -> _pipe = Text, _pipe@1 = apply_replacements_with_tree(_pipe, Replacements), _pipe@2 = normalize_spaces_after_replacement(_pipe@1), {ok, _pipe@2}. -file("src/glugify/internal/optimized_processors.gleam", 310). ?DOC(false). -spec is_valid_char(binary(), glugify@config:config()) -> boolean(). is_valid_char(Char, Config) -> is_alphanumeric_or_unicode(Char, erlang:element(7, Config)) orelse (Char =:= erlang:element( 2, Config )). -file("src/glugify/internal/optimized_processors.gleam", 271). ?DOC(false). -spec batch_process_graphemes( list(binary()), glugify@config:config(), gleam@string_tree:string_tree(), boolean() ) -> gleam@string_tree:string_tree(). batch_process_graphemes(Graphemes, Config, Builder, Last_was_separator) -> case Graphemes of [] -> Builder; [Char | Rest] -> Processed_char = case is_whitespace(Char) of true -> erlang:element(2, Config); false -> Char end, case is_valid_char(Processed_char, Config) of true -> case (Processed_char =:= erlang:element(2, Config)) andalso Last_was_separator of true -> batch_process_graphemes(Rest, Config, Builder, true); false -> Final_char = case erlang:element(3, Config) of true -> string:lowercase(Processed_char); false -> Processed_char end, batch_process_graphemes( Rest, Config, gleam@string_tree:append(Builder, Final_char), Processed_char =:= erlang:element(2, Config) ) end; false -> batch_process_graphemes( Rest, Config, Builder, Last_was_separator ) end end. -file("src/glugify/internal/optimized_processors.gleam", 260). ?DOC(false). -spec batch_process_with_tree(binary(), glugify@config:config()) -> {ok, binary()} | {error, glugify@errors:slugify_error()}. batch_process_with_tree(Text, Config) -> _pipe = Text, _pipe@1 = gleam@string:to_graphemes(_pipe), _pipe@2 = batch_process_graphemes( _pipe@1, Config, gleam@string_tree:new(), false ), _pipe@3 = unicode:characters_to_binary(_pipe@2), {ok, _pipe@3}.