-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, between/3, count/2, before_at/3, after_at/3, between_at/4, before_all/2, after_all/2, between_all/3]).
-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", 64).
?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) ->
case 'after'(String, Start) of
{ok, After_start} ->
before(After_start, End);
{error, E} ->
{error, E}
end.
-file("src/string_editor.gleam", 87).
?DOC(
" Counts the number of occurrences of a substring in a string.\n"
"\n"
" ## Examples\n"
"\n"
" > count(\"hello hello world\", of: \"hello\")\n"
" 2\n"
"\n"
" > count(\"gleam is fun\", of: \"rust\")\n"
" 0\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", 112).
?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", 146).
?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", 180).
?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 ->
case after_at(String, Start, Index) of
{ok, After_start} ->
before(After_start, End);
{error, E} ->
{error, E}
end
end.
-file("src/string_editor.gleam", 207).
?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", 239).
?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", 271).
?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.