-module(string_editor).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/string_editor.gleam").
-export([before/2, 'after'/2, before_last/2, after_last/2, between/3, count/2, before_at/3, after_at/3, between_at/4, before_all/2, after_all/2, between_all/3, replace_before/3, replace_after/3, replace_between/4]).
-if(?OTP_RELEASE >= 27).
-define(MODULEDOC(Str), -moduledoc(Str)).
-define(DOC(Str), -doc(Str)).
-else.
-define(MODULEDOC(Str), -compile([])).
-define(DOC(Str), -compile([])).
-endif.
-file("src/string_editor.gleam", 18).
?DOC(
" Returns the part of a string before the first occurrence of a given substring.\n"
"\n"
" If the substring is not found, or is empty, it returns `Error(Nil)`.\n"
"\n"
" ## Examples\n"
"\n"
" > before(\"hello world\", on: \" \")\n"
" Ok(\"hello\")\n"
"\n"
" > before(\"gleam is fun\", on: \"!\")\n"
" Error(Nil)\n"
).
-spec before(binary(), binary()) -> {ok, binary()} | {error, nil}.
before(String, Pattern) ->
case Pattern of
<<""/utf8>> ->
{error, nil};
_ ->
_pipe = String,
_pipe@1 = gleam@string:split_once(_pipe, Pattern),
gleam@result:map(
_pipe@1,
fun(Parts) -> erlang:element(1, Parts) end
)
end.
-file("src/string_editor.gleam", 40).
?DOC(
" Returns the part of a string after the first occurrence of a given substring.\n"
"\n"
" If the substring is not found, or is empty, it returns `Error(Nil)`.\n"
"\n"
" ## Examples\n"
"\n"
" > after(\"hello world\", on: \" \")\n"
" Ok(\"world\")\n"
"\n"
" > after(\"gleam is fun\", on: \"!\")\n"
" Error(Nil)\n"
).
-spec 'after'(binary(), binary()) -> {ok, binary()} | {error, nil}.
'after'(String, Pattern) ->
case Pattern of
<<""/utf8>> ->
{error, nil};
_ ->
_pipe = String,
_pipe@1 = gleam@string:split_once(_pipe, Pattern),
gleam@result:map(
_pipe@1,
fun(Parts) -> erlang:element(2, Parts) end
)
end.
-file("src/string_editor.gleam", 63).
?DOC(
" Returns the part of a string before the last occurrence of a given\n"
" substring.\n"
"\n"
" If the substring is not found, or is empty, it returns `Error(Nil)`.\n"
"\n"
" ## Examples\n"
"\n"
" > before_last(\"/home/user/document.txt\", on: \"/\")\n"
" Ok(\"/home/user\")\n"
"\n"
" > before_last(\"gleam is fun\", on: \"!\")\n"
" Error(Nil)\n"
).
-spec before_last(binary(), binary()) -> {ok, binary()} | {error, nil}.
before_last(String, Pattern) ->
case Pattern of
<<""/utf8>> ->
{error, nil};
_ ->
case gleam@string:split(String, Pattern) of
[] ->
{error, nil};
[_] ->
{error, nil};
Parts ->
_pipe = Parts,
_pipe@1 = gleam@list:take(_pipe, erlang:length(Parts) - 1),
_pipe@2 = gleam@string:join(_pipe@1, Pattern),
{ok, _pipe@2}
end
end.
-file("src/string_editor.gleam", 91).
?DOC(
" Returns the part of a string after the last occurrence of a given\n"
" substring.\n"
"\n"
" If the substring is not found, or is empty, it returns `Error(Nil)`.\n"
"\n"
" ## Examples\n"
"\n"
" > after_last(\"/home/user/document.txt\", on: \"/\")\n"
" Ok(\"document.txt\")\n"
"\n"
" > after_last(\"gleam is fun\", on: \"!\")\n"
" Error(Nil)\n"
).
-spec after_last(binary(), binary()) -> {ok, binary()} | {error, nil}.
after_last(String, Pattern) ->
case Pattern of
<<""/utf8>> ->
{error, nil};
_ ->
case gleam@string:split(String, Pattern) of
[] ->
{error, nil};
[_] ->
{error, nil};
Parts ->
gleam@list:last(Parts)
end
end.
-file("src/string_editor.gleam", 116).
?DOC(
" Returns the part of a string between two given substrings.\n"
"\n"
" It finds the first occurrence of `start` and then the first\n"
" occurrence of `end` after `start`. If either is not found in the\n"
" correct order, or is empty, it returns `Error(Nil)`.\n"
"\n"
" ## Examples\n"
"\n"
" > between(\"b\", from: \"\", to: \"\")\n"
" Ok(\"b\")\n"
"\n"
" > between(\"
title
\", from: \"\", to: \"\")\n"
" Error(Nil)\n"
).
-spec between(binary(), binary(), binary()) -> {ok, binary()} | {error, nil}.
between(String, Start, End) ->
gleam@result:'try'(
'after'(String, Start),
fun(After_start) -> before(After_start, End) end
).
-file("src/string_editor.gleam", 141).
?DOC(
" Counts the number of non-overlapping occurrences of a substring in a\n"
" string. An empty pattern always counts as `0`.\n"
"\n"
" ## Examples\n"
"\n"
" > count(\"hello hello world\", of: \"hello\")\n"
" 2\n"
"\n"
" > count(\"gleam is fun\", of: \"rust\")\n"
" 0\n"
"\n"
" > count(\"aaaa\", of: \"aa\")\n"
" 2\n"
).
-spec count(binary(), binary()) -> integer().
count(String, Pattern) ->
case Pattern of
<<""/utf8>> ->
0;
_ ->
_pipe = String,
_pipe@1 = gleam@string:split(_pipe, Pattern),
_pipe@2 = erlang:length(_pipe@1),
_pipe@3 = gleam@int:subtract(_pipe@2, 1),
gleam@int:max(_pipe@3, 0)
end.
-file("src/string_editor.gleam", 166).
?DOC(
" Returns the part of a string before the nth occurrence of a given substring.\n"
"\n"
" Index is 0-based. If the pattern doesn't occur enough times, returns `Error(Nil)`.\n"
"\n"
" ## Examples\n"
"\n"
" > before_at(\"a.b.c.d\", on: \".\", at: 1)\n"
" Ok(\"a.b\")\n"
"\n"
" > before_at(\"hello world\", on: \" \", at: 5)\n"
" Error(Nil)\n"
).
-spec before_at(binary(), binary(), integer()) -> {ok, binary()} | {error, nil}.
before_at(String, Pattern, Index) ->
case (Pattern =:= <<""/utf8>>) orelse (Index < 0) of
true ->
{error, nil};
false ->
Parts = gleam@string:split(String, Pattern),
case erlang:length(Parts) > (Index + 1) of
true ->
_pipe = Parts,
_pipe@1 = gleam@list:take(_pipe, Index + 1),
_pipe@2 = gleam@string:join(_pipe@1, Pattern),
{ok, _pipe@2};
false ->
{error, nil}
end
end.
-file("src/string_editor.gleam", 200).
?DOC(
" Returns the part of a string after the nth occurrence of a given substring.\n"
"\n"
" Index is 0-based. If the pattern doesn't occur enough times, returns `Error(Nil)`.\n"
"\n"
" ## Examples\n"
"\n"
" > after_at(\"a.b.c.d\", on: \".\", at: 1)\n"
" Ok(\"c.d\")\n"
"\n"
" > after_at(\"hello world\", on: \" \", at: 5)\n"
" Error(Nil)\n"
).
-spec after_at(binary(), binary(), integer()) -> {ok, binary()} | {error, nil}.
after_at(String, Pattern, Index) ->
case (Pattern =:= <<""/utf8>>) orelse (Index < 0) of
true ->
{error, nil};
false ->
Parts = gleam@string:split(String, Pattern),
case erlang:length(Parts) > (Index + 1) of
true ->
_pipe = Parts,
_pipe@1 = gleam@list:drop(_pipe, Index + 1),
_pipe@2 = gleam@string:join(_pipe@1, Pattern),
{ok, _pipe@2};
false ->
{error, nil}
end
end.
-file("src/string_editor.gleam", 234).
?DOC(
" Returns the part of a string between the nth occurrence of start and the first occurrence of end after that.\n"
"\n"
" Index is 0-based for the start pattern. If patterns don't occur enough times, returns `Error(Nil)`.\n"
"\n"
" ## Examples\n"
"\n"
" > between_at(\"12\", from: \"\", to: \"\", at: 1)\n"
" Ok(\"2\")\n"
"\n"
" > between_at(\"title
\", from: \"\", to: \"\", at: 0)\n"
" Error(Nil)\n"
).
-spec between_at(binary(), binary(), binary(), integer()) -> {ok, binary()} |
{error, nil}.
between_at(String, Start, End, Index) ->
case ((Start =:= <<""/utf8>>) orelse (End =:= <<""/utf8>>)) orelse (Index < 0) of
true ->
{error, nil};
false ->
gleam@result:'try'(
after_at(String, Start, Index),
fun(After_start) -> before(After_start, End) end
)
end.
-file("src/string_editor.gleam", 259).
?DOC(
" Returns all parts of a string before each occurrence of a given substring.\n"
"\n"
" ## Examples\n"
"\n"
" > before_all(\"a.b.c.d\", on: \".\")\n"
" [\"a\", \"a.b\", \"a.b.c\"]\n"
"\n"
" > before_all(\"hello world\", on: \"!\")\n"
" []\n"
).
-spec before_all(binary(), binary()) -> list(binary()).
before_all(String, Pattern) ->
case Pattern =:= <<""/utf8>> of
true ->
[];
false ->
Parts = gleam@string:split(String, Pattern),
case erlang:length(Parts) of
1 ->
[];
_ ->
_pipe@2 = gleam@int:range(
1,
erlang:length(Parts),
[],
fun(Acc, I) ->
Part = begin
_pipe = Parts,
_pipe@1 = gleam@list:take(_pipe, I),
gleam@string:join(_pipe@1, Pattern)
end,
[Part | Acc]
end
),
lists:reverse(_pipe@2)
end
end.
-file("src/string_editor.gleam", 291).
?DOC(
" Returns all parts of a string after each occurrence of a given substring.\n"
"\n"
" ## Examples\n"
"\n"
" > after_all(\"a.b.c.d\", on: \".\")\n"
" [\"b.c.d\", \"c.d\", \"d\"]\n"
"\n"
" > after_all(\"hello world\", on: \"!\")\n"
" []\n"
).
-spec after_all(binary(), binary()) -> list(binary()).
after_all(String, Pattern) ->
case Pattern =:= <<""/utf8>> of
true ->
[];
false ->
Parts = gleam@string:split(String, Pattern),
case erlang:length(Parts) of
1 ->
[];
_ ->
_pipe@2 = gleam@int:range(
1,
erlang:length(Parts),
[],
fun(Acc, I) ->
Part = begin
_pipe = Parts,
_pipe@1 = gleam@list:drop(_pipe, I),
gleam@string:join(_pipe@1, Pattern)
end,
[Part | Acc]
end
),
lists:reverse(_pipe@2)
end
end.
-file("src/string_editor.gleam", 323).
?DOC(
" Returns all parts of a string between each occurrence of start and the next occurrence of end.\n"
"\n"
" ## Examples\n"
"\n"
" > between_all(\"123\", from: \"\", to: \"\")\n"
" [\"1\", \"3\"]\n"
"\n"
" > between_all(\"no matches here\", from: \"
\", to: \"
\")\n"
" []\n"
).
-spec between_all(binary(), binary(), binary()) -> list(binary()).
between_all(String, Start, End) ->
case (Start =:= <<""/utf8>>) orelse (End =:= <<""/utf8>>) of
true ->
[];
false ->
After_parts = after_all(String, Start),
_pipe = After_parts,
gleam@list:filter_map(_pipe, fun(Part) -> before(Part, End) end)
end.
-file("src/string_editor.gleam", 351).
?DOC(
" Replaces the part of a string before the first occurrence of a given\n"
" substring, keeping the pattern itself.\n"
"\n"
" If the substring is not found, or is empty, it returns `Error(Nil)`.\n"
"\n"
" ## Examples\n"
"\n"
" > replace_before(\"hello world\", on: \" \", with: \"goodbye\")\n"
" Ok(\"goodbye world\")\n"
"\n"
" > replace_before(\"gleam is fun\", on: \"!\", with: \"x\")\n"
" Error(Nil)\n"
).
-spec replace_before(binary(), binary(), binary()) -> {ok, binary()} |
{error, nil}.
replace_before(String, Pattern, Replacement) ->
gleam@result:'try'(
'after'(String, Pattern),
fun(Rest) ->
{ok, <<<>/binary, Rest/binary>>}
end
).
-file("src/string_editor.gleam", 373).
?DOC(
" Replaces the part of a string after the first occurrence of a given\n"
" substring, keeping the pattern itself.\n"
"\n"
" If the substring is not found, or is empty, it returns `Error(Nil)`.\n"
"\n"
" ## Examples\n"
"\n"
" > replace_after(\"hello world\", on: \" \", with: \"gleam\")\n"
" Ok(\"hello gleam\")\n"
"\n"
" > replace_after(\"gleam is fun\", on: \"!\", with: \"x\")\n"
" Error(Nil)\n"
).
-spec replace_after(binary(), binary(), binary()) -> {ok, binary()} |
{error, nil}.
replace_after(String, Pattern, Replacement) ->
gleam@result:'try'(
before(String, Pattern),
fun(Prefix) ->
{ok,
<<<>/binary, Replacement/binary>>}
end
).
-file("src/string_editor.gleam", 397).
?DOC(
" Replaces the part of a string between two given substrings, keeping\n"
" both delimiters.\n"
"\n"
" It finds the first occurrence of `start` and then the first\n"
" occurrence of `end` after `start`. If either is not found in the\n"
" correct order, or is empty, it returns `Error(Nil)`.\n"
"\n"
" ## Examples\n"
"\n"
" > replace_between(\"old\", from: \"\", to: \"\", with: \"new\")\n"
" Ok(\"new\")\n"
"\n"
" > replace_between(\"title
\", from: \"\", to: \"\", with: \"x\")\n"
" Error(Nil)\n"
).
-spec replace_between(binary(), binary(), binary(), binary()) -> {ok, binary()} |
{error, nil}.
replace_between(String, Start, End, Replacement) ->
gleam@result:'try'(
before(String, Start),
fun(Prefix) ->
gleam@result:'try'(
'after'(String, Start),
fun(Rest) ->
gleam@result:'try'(
'after'(Rest, End),
fun(Suffix) ->
{ok,
<<<<<<<>/binary,
Replacement/binary>>/binary,
End/binary>>/binary,
Suffix/binary>>}
end
)
end
)
end
).