@@ -355,14 +355,10 @@ defmodule Base do
355
355
356
356
def valid16? ( string , opts ) when is_binary ( string ) and rem ( byte_size ( string ) , 2 ) == 0 do
357
357
case Keyword . get ( opts , :case , :upper ) do
358
- :upper -> validate16upper! ( string )
359
- :lower -> validate16lower! ( string )
360
- :mixed -> validate16mixed! ( string )
358
+ :upper -> validate16upper? ( string )
359
+ :lower -> validate16lower? ( string )
360
+ :mixed -> validate16mixed? ( string )
361
361
end
362
-
363
- true
364
- rescue
365
- ArgumentError -> false
366
362
end
367
363
368
364
def valid16? ( string , _opts ) when is_binary ( string ) do
@@ -373,21 +369,26 @@ defmodule Base do
373
369
374
370
for { base , alphabet } <- [ upper: upper , lower: to_lower_dec . ( upper ) , mixed: to_mixed_dec . ( upper ) ] do
375
371
decode_name = :"decode16#{ base } !"
376
- validate_name = :"validate16#{ base } !"
372
+ validate_name = :"validate16#{ base } ?"
373
+ valid_char_name = :"valid_char16#{ base } ?"
377
374
378
375
{ min , decoded } = to_decode_list . ( alphabet )
379
376
380
- defp unquote ( validate_name ) ( << >> ) , do: :ok
377
+ defp unquote ( validate_name ) ( << >> ) , do: true
381
378
382
379
defp unquote ( validate_name ) ( << c1 , c2 , rest :: binary >> ) do
383
- unquote ( decode_name ) ( c1 )
384
- unquote ( decode_name ) ( c2 )
385
- unquote ( validate_name ) ( rest )
380
+ unquote ( valid_char_name ) ( c1 ) and
381
+ unquote ( valid_char_name ) ( c2 ) and
382
+ unquote ( validate_name ) ( rest )
386
383
end
387
384
388
- defp unquote ( validate_name ) ( << char , _rest :: binary >> ) do
389
- bad_character! ( char )
390
- end
385
+ defp unquote ( validate_name ) ( << _char , _rest :: binary >> ) , do: false
386
+
387
+ defp unquote ( valid_char_name ) ( char )
388
+ when elem ( { unquote_splicing ( decoded ) } , char - unquote ( min ) ) != nil ,
389
+ do: true
390
+
391
+ defp unquote ( valid_char_name ) ( _char ) , do: false
391
392
392
393
defp unquote ( decode_name ) ( char ) do
393
394
index = char - unquote ( min )
0 commit comments