Skip to content

Add Base.valid{n}?/2 functions #14417

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Apr 11, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 95 additions & 21 deletions lib/elixir/lib/base.ex
Original file line number Diff line number Diff line change
Expand Up @@ -319,13 +319,84 @@ defmodule Base do
"Double check your string for unwanted characters or pad it accordingly"
end

@doc """
Checks if a string is a valid base 16 encoded string.

Use this function when you just need to *validate* that a string is valid base64 data,
without actually producing a decoded output string.

## Options

The accepted options are:

* `:case` - specifies the character case to accept when decoding

The values for `:case` can be:

* `:upper` - only allows upper case characters (default)
* `:lower` - only allows lower case characters
* `:mixed` - allows mixed case characters

An `ArgumentError` exception is raised if the padding is incorrect or
a non-alphabet character is present in the string.

## Examples

iex> Base.valid16?("666F6F626172")
true

iex> Base.valid16?("666f6f626172", case: :lower)
true

iex> Base.valid16?("666f6F626172", case: :mixed)
true

iex> Base.valid16?("ff", case: :upper)
false

"""
@doc since: "1.19.0"
@spec valid16?(binary, case: decode_case) :: boolean
def valid16?(string, opts \\ [])

def valid16?(string, opts) when is_binary(string) and rem(byte_size(string), 2) == 0 do
case Keyword.get(opts, :case, :upper) do
:upper -> validate16upper!(string)
:lower -> validate16lower!(string)
:mixed -> validate16mixed!(string)
end

true
rescue
ArgumentError -> false
end

def valid16?(string, _opts) when is_binary(string) do
false
end

upper = Enum.with_index(b16_alphabet)

for {base, alphabet} <- [upper: upper, lower: to_lower_dec.(upper), mixed: to_mixed_dec.(upper)] do
name = :"decode16#{base}!"
decode_name = :"decode16#{base}!"
validate_name = :"validate16#{base}!"

{min, decoded} = to_decode_list.(alphabet)

defp unquote(name)(char) do
valid_chars = Enum.map(alphabet, fn {char, _val} -> char end)

defp unquote(validate_name)(<<>>), do: :ok

defp unquote(validate_name)(<<c1, c2, rest::binary>>)
when c1 in unquote(valid_chars) and c2 in unquote(valid_chars) do
unquote(validate_name)(rest)
end

defp unquote(validate_name)(<<char, _rest::binary>>) do
bad_character!(char)
end

defp unquote(decode_name)(char) do
try do
elem({unquote_splicing(decoded)}, char - unquote(min))
rescue
Expand All @@ -336,41 +407,44 @@ defmodule Base do
end
end

defp unquote(name)(<<c1, c2, c3, c4, c5, c6, c7, c8, rest::binary>>, acc) do
unquote(name)(
defp unquote(decode_name)(<<c1, c2, c3, c4, c5, c6, c7, c8, rest::binary>>, acc) do
unquote(decode_name)(
rest,
<<
acc::binary,
unquote(name)(c1)::4,
unquote(name)(c2)::4,
unquote(name)(c3)::4,
unquote(name)(c4)::4,
unquote(name)(c5)::4,
unquote(name)(c6)::4,
unquote(name)(c7)::4,
unquote(name)(c8)::4
unquote(decode_name)(c1)::4,
unquote(decode_name)(c2)::4,
unquote(decode_name)(c3)::4,
unquote(decode_name)(c4)::4,
unquote(decode_name)(c5)::4,
unquote(decode_name)(c6)::4,
unquote(decode_name)(c7)::4,
unquote(decode_name)(c8)::4
>>
)
end

defp unquote(name)(<<c1, c2, c3, c4, rest::binary>>, acc) do
unquote(name)(
defp unquote(decode_name)(<<c1, c2, c3, c4, rest::binary>>, acc) do
unquote(decode_name)(
rest,
<<
acc::binary,
unquote(name)(c1)::4,
unquote(name)(c2)::4,
unquote(name)(c3)::4,
unquote(name)(c4)::4
unquote(decode_name)(c1)::4,
unquote(decode_name)(c2)::4,
unquote(decode_name)(c3)::4,
unquote(decode_name)(c4)::4
>>
)
end

defp unquote(name)(<<c1::8, c2::8, rest::binary>>, acc) do
unquote(name)(rest, <<acc::binary, unquote(name)(c1)::4, unquote(name)(c2)::4>>)
defp unquote(decode_name)(<<c1::8, c2::8, rest::binary>>, acc) do
unquote(decode_name)(
rest,
<<acc::binary, unquote(decode_name)(c1)::4, unquote(decode_name)(c2)::4>>
)
end

defp unquote(name)(<<>>, acc) do
defp unquote(decode_name)(<<>>, acc) do
acc
end
end
Expand Down
27 changes: 27 additions & 0 deletions lib/elixir/test/elixir/base_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,33 @@ defmodule BaseTest do
end
end

test "valid16?/1" do
assert valid16?("")
assert valid16?("66")
assert valid16?("666F")
assert valid16?("666F6F")
assert valid16?("666F6F62")
assert valid16?("666F6F6261")
assert valid16?("666F6F626172")
assert valid16?("A1B2C3D4E5F67891")
assert valid16?("a1b2c3d4e5f67891", case: :lower)
assert valid16?("a1B2c3D4e5F67891", case: :mixed)
end

test "valid16?/1 returns false on non-alphabet character" do
refute valid16?("66KF")
refute valid16?("66ff")
refute valid16?("66FF", case: :lower)
end

test "valid16?/1 errors on odd-length string" do
refute valid16?("666")
end

test "valid16?/1 errors odd-length string" do
refute valid16?("666")
end

test "encode64/1 can deal with empty strings" do
assert "" == encode64("")
end
Expand Down
Loading