Skip to content

Commit

Permalink
Remove the octal formatting
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
fredrikekre committed Aug 12, 2024
1 parent fa78ba6 commit 60a8cff
Show file tree
Hide file tree
Showing 3 changed files with 0 additions and 61 deletions.
1 change: 0 additions & 1 deletion src/Runic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
38 changes: 0 additions & 38 deletions src/runestone.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 0 additions & 22 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 60a8cff

Please sign in to comment.