-module(string_width@internal@ansi). -compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]). -export([match/1, strip/1]). -export_type([state/0]). -type state() :: text | {escape, integer()} | {csi, integer()} | {osc, integer()} | {osc_esc, integer()}. -file("/home/arkan/Projects/private/string-width/src/string_width/internal/ansi.gleam", 28). -spec match(binary()) -> list({integer(), integer()}). match(Str) -> {_, _, Matches@1} = (string_width_ffi:fold_bytes( Str, {0, text, []}, fun(_use0, Byte) -> {Pos, State, Matches} = _use0, case State of text -> case Byte of 16#1b -> {Pos + 1, {escape, Pos}, Matches}; _ -> {Pos + 1, State, Matches} end; {escape, Start} -> case Byte of 16#5b -> {Pos + 1, {csi, Start}, Matches}; 16#5d -> {Pos + 1, {osc, Start}, Matches}; _ when (Byte >= 16#30) andalso (Byte =< 16#3f) -> {Pos + 1, text, [{Start, (Pos - Start) + 1} | Matches]}; 16#50 -> {Pos + 1, {osc, Start}, Matches}; 16#58 -> {Pos + 1, {osc, Start}, Matches}; 16#5e -> {Pos + 1, {osc, Start}, Matches}; 16#5f -> {Pos + 1, {osc, Start}, Matches}; _ when (Byte >= 16#40) andalso (Byte =< 16#5f) -> {Pos + 1, text, [{Start, (Pos - Start) + 1} | Matches]}; _ when (Byte >= 16#60) andalso (Byte =< 16#7e) -> {Pos + 1, text, [{Start, (Pos - Start) + 1} | Matches]}; _ -> {Pos + 1, text, Matches} end; {csi, Start@1} -> case Byte of _ when (Byte >= 16#20) andalso (Byte =< 16#3f) -> {Pos + 1, State, Matches}; _ when (Byte >= 16#40) andalso (Byte =< 16#7e) -> {Pos + 1, text, [{Start@1, (Pos - Start@1) + 1} | Matches]}; _ -> {Pos + 1, text, Matches} end; {osc, Start@2} -> case Byte of 16#07 -> {Pos + 1, text, [{Start@2, (Pos - Start@2) + 1} | Matches]}; 16#1b -> {Pos + 1, {osc_esc, Start@2}, Matches}; _ -> {Pos + 1, State, Matches} end; {osc_esc, Start@3} -> case Byte of 16#5c -> {Pos + 1, text, [{Start@3, (Pos - Start@3) + 1} | Matches]}; 16#07 -> {Pos + 1, text, [{Start@3, (Pos - Start@3) + 1} | Matches]}; _ -> {Pos + 1, {osc, Start@3}, Matches} end end end )), lists:reverse(Matches@1). -file("/home/arkan/Projects/private/string-width/src/string_width/internal/ansi.gleam", 16). -spec do_strip(binary(), integer(), list({integer(), integer()}), binary()) -> binary(). do_strip(Str, Offset, Matches, Acc) -> case Matches of [] -> <>; [{From, Len} | Matches@1] -> {Before, At_ansi} = erlang:split_binary(Str, From - Offset), {_, Str@1} = erlang:split_binary(At_ansi, Len), do_strip( Str@1, From + Len, Matches@1, <> ) end. -file("/home/arkan/Projects/private/string-width/src/string_width/internal/ansi.gleam", 12). -spec strip(binary()) -> binary(). strip(Str) -> do_strip(Str, 0, match(Str), <<""/utf8>>).