Browse Source

Remove the octal formatting

Octal formatting isn't as useful as hex formatting and it is also value
dependent (not just string-length dependent) which makes it a bit odd.
Octals are also very rare. One of the more common ones are filemodes and
for that case e.g. 0o755 looks better than 0o000755. Closes #40.
pull/44/head
Fredrik Ekre 1 year ago
parent
commit
4bee1e0f94
No known key found for this signature in database
GPG Key ID: DE82E6D5E364C0A2
  1. 8
      README.md
  2. 1
      src/Runic.jl
  3. 38
      src/runestone.jl
  4. 22
      test/runtests.jl

8
README.md

@ -560,14 +560,6 @@ Hex literals are padded with zeros to better highlight the resulting type of the @@ -560,14 +560,6 @@ Hex literals are padded with zeros to better highlight the resulting type of the
+0x00012345
```
Similarly, oct literals are also padded:
```diff
-0o644
-0o644644
+0o000644
+0o00000644644
```
### Parentheses around operator calls in colon
Add parentheses around operator calls in colon expressions to better highlight the low

1
src/Runic.jl

@ -305,7 +305,6 @@ function format_node!(ctx::Context, node::Node)::Union{Node, Nothing, NullNode} @@ -305,7 +305,6 @@ function format_node!(ctx::Context, node::Node)::Union{Node, Nothing, NullNode}
@return_something insert_delete_mark_newlines(ctx, node)
@return_something trim_trailing_whitespace(ctx, node)
@return_something format_hex_literals(ctx, node)
@return_something format_oct_literals(ctx, node)
@return_something format_float_literals(ctx, node)
@return_something spaces_around_operators(ctx, node)
@return_something spaces_around_assignments(ctx, node)

38
src/runestone.jl

@ -67,44 +67,6 @@ function format_hex_literals(ctx::Context, node::Node) @@ -67,44 +67,6 @@ function format_hex_literals(ctx::Context, node::Node)
return node′
end
function format_oct_literals(ctx::Context, node::Node)
kind(node) === K"OctInt" || return nothing
@assert flags(node) == 0
@assert is_leaf(node)
spn = span(node)
@assert spn > 2 # 0o prefix + something more
# Padding depends on the value of the literal...
str = String(read_bytes(ctx, node))
n = tryparse(UInt128, str)
if n === nothing || spn > 45
# Do nothing: BigInt oct literal
return nothing
end
# Compute the target span
target_span_from_value =
n <= typemax(UInt8) ? 5 : n <= typemax(UInt16) ? 8 :
n <= typemax(UInt32) ? 13 : n <= typemax(UInt64) ? 24 :
n <= typemax(UInt128) ? 45 : error("unreachable")
target_spans = (5, 8, 13, 24, 45)
i = findfirst(x -> x >= spn, target_spans)::Int
target_span_from_source = target_spans[i]
target_span = max(target_span_from_value, target_span_from_source)
if spn == target_span
# Do nothing: correctly formatted oct literal
return nothing
end
# Insert leading zeros
bytes = read_bytes(ctx, node)
while length(bytes) < target_span
insert!(bytes, 3, '0')
end
nb = replace_bytes!(ctx, bytes, spn)
@assert nb == length(bytes) == target_span
# Create new node and return it
node′ = Node(head(node), nb)
return node′
end
function format_float_literals(ctx::Context, node::Node)
kind(node) in KSet"Float Float32" || return nothing
@assert flags(node) == 0

22
test/runtests.jl

@ -78,28 +78,6 @@ end @@ -78,28 +78,6 @@ end
("0x" * z(n) * "1" => "0x" * z(31) * "1" for n in 16:31)...,
# Hex BigInt
("0x" * z(n) * "1" => "0x" * z(n) * "1" for n in 32:35)...,
# Octal UInt8
("0o" * z(n) * "1" => "0o001" for n in 0:2)...,
"0o377" => "0o377", # typemax(UInt8)
# Octal UInt16
"0o400" => "0o000400", # typemax(UInt8) + 1
("0o" * z(n) * "1" => "0o000001" for n in 3:5)...,
"0o177777" => "0o177777", # typemax(UInt16)
# Octal UInt32
"0o200000" => "0o00000200000", # typemax(UInt16) + 1
("0o" * z(n) * "1" => "0o00000000001" for n in 6:10)...,
"0o37777777777" => "0o37777777777", # typemax(UInt32)
# Octal UInt64
"0o40000000000" => "0o0000000000040000000000", # typemax(UInt32) + 1
("0o" * z(n) * "1" => "0o" * z(21) * "1" for n in 11:21)...,
"0o1777777777777777777777" => "0o1777777777777777777777", # typemax(UInt64)
# Octal UInt128
"0o2" * z(21) => "0o" * z(21) * "2" * z(21), # typemax(UInt64) + 1
("0o" * z(n) * "1" => "0o" * z(42) * "1" for n in 22:42)...,
"0o3" * "7"^42 => "0o3" * "7"^42, # typemax(UInt128)
# Octal BigInt
"0o4" * z(42) => "0o4" * z(42), # typemax(UInt128) + 1
"0o7" * z(42) => "0o7" * z(42),
]
mod = Module()
for (a, b) in test_cases

Loading…
Cancel
Save