First of all, if you don't care about performance at all, you can relay all the work to the regex:
~r/\A(.{0,30})(?:-|\Z)/
I assume it will be the shortest solution, but not efficient:
iex(28)> string
"another-long-string-to-be-truncated-and-much-text-here"
iex(29)> string2
"another-long-string-to-be-cool-about-that"
iex(30)> Regex.run(~r/\A(.{0,30})(?:-|\Z)/, string) |> List.last()
"another-long-string-to-be"
iex(31)> Regex.run(~r/\A(.{0,30})(?:-|\Z)/, string2) |> List.last()
"another-long-string-to-be-cool"
Efficient solution
But if you do care about performance and memory, then I suggest this:
defmodule CoolSlugHelper do
def slug(input, length \\ 30) do
length_minus_1 = length - 1
case input do
# if the substring ends with "-"
# i. e. "abc-def-ghi", 8 or "abc-def-", 8 -> "abc-def"
<<result::binary-size(length_minus_1), "-", _::binary>> -> result
# if the next char after the substring is "-"
# i. e. "abc-def-ghi", 7 or "abc-def-", 7 -> "abc-def"
<<result::binary-size(length), "-", _::binary>> -> result
# if it is the exact string. i. e. "abc-def", 7 -> "abc-def"
<<_::binary-size(length)>> -> input
# return an empty string if we reached the beginnig of the string
_ when length <= 1 -> ""
# otherwise look into shorter substring
_ -> slug(input, length_minus_1)
end
end
end
It does not collect the resulting string char-by-char. Instead, it looks for the correct substring starting from the desired length down to 1. That's how it becomes efficient in terms of memory and speed.
We need this length_minus_1 variable because we cannot use expressions in the binary-size binary pattern matching.
Here is the benchmark of all the proposed solutions as of Dec 22nd, 2018:
(Simple Regex is the ~r/\A(.{0,30})(?:-|\Z)/ regex above)
Name ips average deviation median 99th %
CoolSlugHelper 352.14 K 2.84 μs ±1184.93% 2 μs 8 μs
SlugHelper 70.98 K 14.09 μs ±170.20% 10 μs 87 μs
Simple Regex 33.14 K 30.17 μs ±942.90% 21 μs 126 μs
Truncation 11.56 K 86.51 μs ±84.81% 62 μs 299 μs
Comparison:
CoolSlugHelper 352.14 K
SlugHelper 70.98 K - 4.96x slower
Simple Regex 33.14 K - 10.63x slower
Truncation 11.56 K - 30.46x slower
Memory usage statistics:
Name Memory usage
CoolSlugHelper 2.30 KB
SlugHelper 12.94 KB - 5.61x memory usage
Simple Regex 20.16 KB - 8.75x memory usage
Truncation 35.36 KB - 15.34x memory usage