Skip to content

Commit

Permalink
Format charlists
Browse files Browse the repository at this point in the history
  • Loading branch information
sabiwara authored and michalmuskala committed May 26, 2024
1 parent 1fe6178 commit e9b96b6
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 77 deletions.
4 changes: 2 additions & 2 deletions lib/codegen.ex
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ defmodule Jason.Codegen do
|> Enum.map(&encode_pair(&1, encode_args))
|> Enum.intersperse(",")

collapse_static(List.flatten(["{", elements] ++ '}'))
collapse_static(List.flatten(["{", elements] ++ ~c'}'))
end

defp clauses_to_ranges([{:->, _, [[{:in, _, [byte, range]}, rest], action]} | tail], acc, env) do
Expand Down Expand Up @@ -116,7 +116,7 @@ defmodule Jason.Codegen do

defp check_safe_key!(binary) do
for <<(<<byte>> <- binary)>> do
if byte > 0x7F or byte < 0x1F or byte in '"\\/' do
if byte > 0x7F or byte < 0x1F or byte in ~c'"\\/' do
raise EncodeError,
"invalid byte #{inspect(byte, base: :hex)} in literal key: #{inspect(binary)}"
end
Expand Down
120 changes: 60 additions & 60 deletions lib/decoder.ex
Original file line number Diff line number Diff line change
Expand Up @@ -101,37 +101,37 @@ defmodule Jason.Decoder do

defp value(data, original, skip, stack, decode) do
bytecase data do
_ in '\s\n\t\r', rest ->
_ in ~c'\s\n\t\r', rest ->
value(rest, original, skip + 1, stack, decode)
_ in '0', rest ->
_ in ~c'0', rest ->
number_zero(rest, original, skip, stack, decode, 1)
_ in '123456789', rest ->
_ in ~c'123456789', rest ->
number(rest, original, skip, stack, decode, 1)
_ in '-', rest ->
_ in ~c'-', rest ->
number_minus(rest, original, skip, stack, decode)
_ in '"', rest ->
_ in ~c'"', rest ->
string(rest, original, skip + 1, stack, decode, 0)
_ in '[', rest ->
_ in ~c'[', rest ->
array(rest, original, skip + 1, stack, decode)
_ in '{', rest ->
_ in ~c'{', rest ->
object(rest, original, skip + 1, stack, decode)
_ in ']', rest ->
_ in ~c']', rest ->
empty_array(rest, original, skip + 1, stack, decode)
_ in 't', rest ->
_ in ~c't', rest ->
case rest do
<<"rue", rest::bits>> ->
continue(rest, original, skip + 4, stack, decode, true)
<<_::bits>> ->
error(original, skip)
end
_ in 'f', rest ->
_ in ~c'f', rest ->
case rest do
<<"alse", rest::bits>> ->
continue(rest, original, skip + 5, stack, decode, false)
<<_::bits>> ->
error(original, skip)
end
_ in 'n', rest ->
_ in ~c'n', rest ->
case rest do
<<"ull", rest::bits>> ->
continue(rest, original, skip + 4, stack, decode, nil)
Expand All @@ -149,7 +149,7 @@ defmodule Jason.Decoder do
number_zero(rest, original, skip, stack, decode, 2)
end
defp number_minus(<<byte, rest::bits>>, original, skip, stack, decode)
when byte in '123456789' do
when byte in ~c'123456789' do
number(rest, original, skip, stack, decode, 2)
end
defp number_minus(<<_rest::bits>>, original, skip, _stack, _decode) do
Expand All @@ -164,13 +164,13 @@ defmodule Jason.Decoder do
end

defp number(<<byte, rest::bits>>, original, skip, stack, decode, len)
when byte in '0123456789' do
when byte in ~c'0123456789' do
number(rest, original, skip, stack, decode, len + 1)
end
defp number(<<?., rest::bits>>, original, skip, stack, decode, len) do
number_frac(rest, original, skip, stack, decode, len + 1)
end
defp number(<<e, rest::bits>>, original, skip, stack, decode, len) when e in 'eE' do
defp number(<<e, rest::bits>>, original, skip, stack, decode, len) when e in ~c'eE' do
prefix = binary_part(original, skip, len)
number_exp_copy(rest, original, skip + len + 1, stack, decode, prefix)
end
Expand All @@ -184,19 +184,19 @@ defmodule Jason.Decoder do
end

defp number_frac(<<byte, rest::bits>>, original, skip, stack, decode, len)
when byte in '0123456789' do
when byte in ~c'0123456789' do
number_frac_cont(rest, original, skip, stack, decode, len + 1)
end
defp number_frac(<<_rest::bits>>, original, skip, _stack, _decode, len) do
error(original, skip + len)
end

defp number_frac_cont(<<byte, rest::bits>>, original, skip, stack, decode, len)
when byte in '0123456789' do
when byte in ~c'0123456789' do
number_frac_cont(rest, original, skip, stack, decode, len + 1)
end
defp number_frac_cont(<<e, rest::bits>>, original, skip, stack, decode, len)
when e in 'eE' do
when e in ~c'eE' do
number_exp(rest, original, skip, stack, decode, len + 1)
end
defp number_frac_cont(<<rest::bits>>, original, skip, stack, decode, len) do
Expand All @@ -207,27 +207,27 @@ defmodule Jason.Decoder do
end

defp number_exp(<<byte, rest::bits>>, original, skip, stack, decode, len)
when byte in '0123456789' do
when byte in ~c'0123456789' do
number_exp_cont(rest, original, skip, stack, decode, len + 1)
end
defp number_exp(<<byte, rest::bits>>, original, skip, stack, decode, len)
when byte in '+-' do
when byte in ~c'+-' do
number_exp_sign(rest, original, skip, stack, decode, len + 1)
end
defp number_exp(<<_rest::bits>>, original, skip, _stack, _decode, len) do
error(original, skip + len)
end

defp number_exp_sign(<<byte, rest::bits>>, original, skip, stack, decode, len)
when byte in '0123456789' do
when byte in ~c'0123456789' do
number_exp_cont(rest, original, skip, stack, decode, len + 1)
end
defp number_exp_sign(<<_rest::bits>>, original, skip, _stack, _decode, len) do
error(original, skip + len)
end

defp number_exp_cont(<<byte, rest::bits>>, original, skip, stack, decode, len)
when byte in '0123456789' do
when byte in ~c'0123456789' do
number_exp_cont(rest, original, skip, stack, decode, len + 1)
end
defp number_exp_cont(<<rest::bits>>, original, skip, stack, decode, len) do
Expand All @@ -238,27 +238,27 @@ defmodule Jason.Decoder do
end

defp number_exp_copy(<<byte, rest::bits>>, original, skip, stack, decode, prefix)
when byte in '0123456789' do
when byte in ~c'0123456789' do
number_exp_cont(rest, original, skip, stack, decode, prefix, 1)
end
defp number_exp_copy(<<byte, rest::bits>>, original, skip, stack, decode, prefix)
when byte in '+-' do
when byte in ~c'+-' do
number_exp_sign(rest, original, skip, stack, decode, prefix, 1)
end
defp number_exp_copy(<<_rest::bits>>, original, skip, _stack, _decode, _prefix) do
error(original, skip)
end

defp number_exp_sign(<<byte, rest::bits>>, original, skip, stack, decode, prefix, len)
when byte in '0123456789' do
when byte in ~c'0123456789' do
number_exp_cont(rest, original, skip, stack, decode, prefix, len + 1)
end
defp number_exp_sign(<<_rest::bits>>, original, skip, _stack, _decode, _prefix, len) do
error(original, skip + len)
end

defp number_exp_cont(<<byte, rest::bits>>, original, skip, stack, decode, prefix, len)
when byte in '0123456789' do
when byte in ~c'0123456789' do
number_exp_cont(rest, original, skip, stack, decode, prefix, len + 1)
end
defp number_exp_cont(<<rest::bits>>, original, skip, stack, decode, prefix, len) do
Expand All @@ -276,7 +276,7 @@ defmodule Jason.Decoder do
defp number_zero(<<?., rest::bits>>, original, skip, stack, decode, len) do
number_frac(rest, original, skip, stack, decode, len + 1)
end
defp number_zero(<<e, rest::bits>>, original, skip, stack, decode, len) when e in 'eE' do
defp number_zero(<<e, rest::bits>>, original, skip, stack, decode, len) when e in ~c'eE' do
number_exp_copy(rest, original, skip + len + 1, stack, decode, "0")
end
defp number_zero(<<rest::bits>>, original, skip, stack, decode, len) do
Expand All @@ -300,13 +300,13 @@ defmodule Jason.Decoder do

defp array(data, original, skip, stack, decode, value) do
bytecase data do
_ in '\s\n\t\r', rest ->
_ in ~c'\s\n\t\r', rest ->
array(rest, original, skip + 1, stack, decode, value)
_ in ']', rest ->
_ in ~c']', rest ->
[acc | stack] = stack
value = :lists.reverse(acc, [value])
continue(rest, original, skip + 1, stack, decode, value)
_ in ',', rest ->
_ in ~c',', rest ->
[acc | stack] = stack
value(rest, original, skip + 1, [@array, [value | acc] | stack], decode)
_, _rest ->
Expand All @@ -324,16 +324,16 @@ defmodule Jason.Decoder do

defp object(data, original, skip, stack, decode, value) do
bytecase data do
_ in '\s\n\t\r', rest ->
_ in ~c'\s\n\t\r', rest ->
object(rest, original, skip + 1, stack, decode, value)
_ in '}', rest ->
_ in ~c'}', rest ->
skip = skip + 1
[key, acc | stack] = stack
decode(keys: key_decode) = decode
final = [{key_decode.(key), value} | acc]
decode(objects: object_decode) = decode
continue(rest, original, skip, stack, decode, object_decode.(final))
_ in ',', rest ->
_ in ~c',', rest ->
skip = skip + 1
[key, acc | stack] = stack
decode(keys: key_decode) = decode
Expand All @@ -348,17 +348,17 @@ defmodule Jason.Decoder do

defp key(data, original, skip, stack, decode) do
bytecase data do
_ in '\s\n\t\r', rest ->
_ in ~c'\s\n\t\r', rest ->
key(rest, original, skip + 1, stack, decode)
_ in '}', rest ->
_ in ~c'}', rest ->
case stack do
[[] | stack] ->
decode(objects: object_decode) = decode
continue(rest, original, skip + 1, stack, decode, object_decode.([]))
_ ->
error(original, skip)
end
_ in '"', rest ->
_ in ~c'"', rest ->
string(rest, original, skip + 1, [@key | stack], decode, 0)
_, _rest ->
error(original, skip)
Expand All @@ -369,9 +369,9 @@ defmodule Jason.Decoder do

defp key(data, original, skip, stack, decode, value) do
bytecase data do
_ in '\s\n\t\r', rest ->
_ in ~c'\s\n\t\r', rest ->
key(rest, original, skip + 1, stack, decode, value)
_ in ':', rest ->
_ in ~c':', rest ->
value(rest, original, skip + 1, [@object, value | stack], decode)
_, _rest ->
error(original, skip)
Expand All @@ -385,11 +385,11 @@ defmodule Jason.Decoder do
# http://bjoern.hoehrmann.de/utf-8/decoder/dfa/
defp string(data, original, skip, stack, decode, len) do
bytecase data, 128 do
_ in '"', rest ->
_ in ~c'"', rest ->
decode(strings: string_decode) = decode
string = string_decode.(binary_part(original, skip, len))
continue(rest, original, skip + len + 1, stack, decode, string)
_ in '\\', rest ->
_ in ~c'\\', rest ->
part = binary_part(original, skip, len)
escape(rest, original, skip + len, stack, decode, part)
_ in unquote(0x00..0x1F), _rest ->
Expand All @@ -409,11 +409,11 @@ defmodule Jason.Decoder do

defp string(data, original, skip, stack, decode, acc, len) do
bytecase data, 128 do
_ in '"', rest ->
_ in ~c'"', rest ->
last = binary_part(original, skip, len)
string = IO.iodata_to_binary([acc | last])
continue(rest, original, skip + len + 1, stack, decode, string)
_ in '\\', rest ->
_ in ~c'\\', rest ->
part = binary_part(original, skip, len)
escape(rest, original, skip + len, stack, decode, [acc | part])
_ in unquote(0x00..0x1F), _rest ->
Expand All @@ -433,23 +433,23 @@ defmodule Jason.Decoder do

defp escape(data, original, skip, stack, decode, acc) do
bytecase data do
_ in 'b', rest ->
string(rest, original, skip + 2, stack, decode, [acc | '\b'], 0)
_ in 't', rest ->
string(rest, original, skip + 2, stack, decode, [acc | '\t'], 0)
_ in 'n', rest ->
string(rest, original, skip + 2, stack, decode, [acc | '\n'], 0)
_ in 'f', rest ->
string(rest, original, skip + 2, stack, decode, [acc | '\f'], 0)
_ in 'r', rest ->
string(rest, original, skip + 2, stack, decode, [acc | '\r'], 0)
_ in '"', rest ->
string(rest, original, skip + 2, stack, decode, [acc | '\"'], 0)
_ in '/', rest ->
string(rest, original, skip + 2, stack, decode, [acc | '/'], 0)
_ in '\\', rest ->
string(rest, original, skip + 2, stack, decode, [acc | '\\'], 0)
_ in 'u', rest ->
_ in ~c'b', rest ->
string(rest, original, skip + 2, stack, decode, [acc | ~c'\b'], 0)
_ in ~c't', rest ->
string(rest, original, skip + 2, stack, decode, [acc | ~c'\t'], 0)
_ in ~c'n', rest ->
string(rest, original, skip + 2, stack, decode, [acc | ~c'\n'], 0)
_ in ~c'f', rest ->
string(rest, original, skip + 2, stack, decode, [acc | ~c'\f'], 0)
_ in ~c'r', rest ->
string(rest, original, skip + 2, stack, decode, [acc | ~c'\r'], 0)
_ in ~c'"', rest ->
string(rest, original, skip + 2, stack, decode, [acc | ~c'\"'], 0)
_ in ~c'/', rest ->
string(rest, original, skip + 2, stack, decode, [acc | ~c'/'], 0)
_ in ~c'\\', rest ->
string(rest, original, skip + 2, stack, decode, [acc | ~c'\\'], 0)
_ in ~c'u', rest ->
escapeu(rest, original, skip, stack, decode, acc)
_, _rest ->
error(original, skip + 1)
Expand Down Expand Up @@ -606,7 +606,7 @@ defmodule Jason.Decoder do
end

defp escapeu_surrogate_clauses(last, rest, original, skip, stack, decode, acc, hi) do
digits1 = 'Dd'
digits1 = ~c'Dd'
digits2 = Stream.concat([?C..?F, ?c..?f])
for {int, first} <- unicode_escapes(digits1, digits2) do
escapeu_surrogate_clause(int, first, last, rest, original, skip, stack, decode, acc, hi)
Expand Down Expand Up @@ -692,7 +692,7 @@ defmodule Jason.Decoder do
end

defp terminate(<<byte, rest::bits>>, original, skip, stack, decode, value)
when byte in '\s\n\r\t' do
when byte in ~c'\s\n\r\t' do
terminate(rest, original, skip + 1, stack, decode, value)
end
defp terminate(<<>>, _original, _skip, _stack, _decode, value) do
Expand Down
8 changes: 4 additions & 4 deletions lib/encode.ex
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ defmodule Jason.Encode do
end

defp list_loop([], _escape, _encode_map) do
']'
~c']'
end

defp list_loop([head | tail], escape, encode_map) do
Expand Down Expand Up @@ -210,7 +210,7 @@ defmodule Jason.Encode do
end

defp map_naive_loop([], _escape, _encode_map) do
'}'
~c'}'
end

defp map_naive_loop([{key, value} | tail], escape, encode_map) do
Expand All @@ -228,7 +228,7 @@ defmodule Jason.Encode do
end

defp map_strict_loop([], _encode_map, _escape, _visited) do
'}'
~c'}'
end

defp map_strict_loop([{key, value} | tail], escape, encode_map, visited) do
Expand Down Expand Up @@ -301,7 +301,7 @@ defmodule Jason.Encode do
[?", escape.(string), ?"]
end

slash_escapes = Enum.zip('\b\t\n\f\r\"\\', 'btnfr"\\')
slash_escapes = Enum.zip(~c'\b\t\n\f\r\"\\', ~c'btnfr"\\')
surogate_escapes = Enum.zip([0x2028, 0x2029], ["\\u2028", "\\u2029"])
ranges = [{0x00..0x1F, :unicode} | slash_escapes]
html_ranges = [{0x00..0x1F, :unicode}, {?<, :unicode}, {?/, ?/} | slash_escapes]
Expand Down
Loading

0 comments on commit e9b96b6

Please sign in to comment.