Skip to content
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

run JuliaFormatter on Base64 stdlib #47151

Closed
wants to merge 4 commits into from
Closed
Changes from all commits
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
8 changes: 4 additions & 4 deletions stdlib/Base64/src/decode.jl
Original file line number Diff line number Diff line change
@@ -9,7 +9,7 @@ for (i, c) in enumerate(BASE64_ENCODE)
BASE64_DECODE[Int(c)+1] = UInt8(i - 1)
end
BASE64_DECODE[Int(encodepadding())+1] = BASE64_CODE_PAD
decode(x::UInt8) = @inbounds return BASE64_DECODE[x + 1]
decode(x::UInt8) = @inbounds return BASE64_DECODE[x+1]

"""
Base64DecodePipe(istream)
@@ -69,9 +69,9 @@ function read_until_end(pipe::Base64DecodePipe, ptr::Ptr{UInt8}, n::UInt)
while true
if b1 < 0x40 && b2 < 0x40 && b3 < 0x40 && b4 < 0x40 && p + 2 < p_end
# fast path to decode
unsafe_store!(p , b1 << 2 | b2 >> 4)
unsafe_store!(p, b1 << 2 | b2 >> 4)
unsafe_store!(p + 1, b2 << 4 | b3 >> 2)
unsafe_store!(p + 2, b3 << 6 | b4 )
unsafe_store!(p + 2, b3 << 6 | b4)
p += 3
else
i, p, ended = decode_slow(b1, b2, b3, b4, buffer, i, pipe.io, p, p_end - p, pipe.rest)
@@ -182,7 +182,7 @@ function decode_slow(b1, b2, b3, b4, buffer, i, input, ptr, n, rest)
end
k ≥ 1 && output(b1 << 2 | b2 >> 4)
k ≥ 2 && output(b2 << 4 | b3 >> 2)
k ≥ 3 && output(b3 << 6 | b4 )
k ≥ 3 && output(b3 << 6 | b4)

return i, p, k == 0
end
32 changes: 16 additions & 16 deletions stdlib/Base64/src/encode.jl
Original file line number Diff line number Diff line change
@@ -2,8 +2,8 @@

# Generate encode table.
const BASE64_ENCODE = [UInt8(x) for x in append!(['A':'Z'; 'a':'z'; '0':'9'], ['+', '/'])]
encode(x::UInt8) = @inbounds return BASE64_ENCODE[(x & 0x3f) + 1]
encodepadding() = UInt8('=')
encode(x::UInt8) = @inbounds return BASE64_ENCODE[(x&0x3f)+1]
encodepadding() = UInt8('=')

"""
Base64EncodePipe(ostream)
@@ -69,10 +69,10 @@ function Base.unsafe_write(pipe::Base64EncodePipe, ptr::Ptr{UInt8}, n::UInt)::In
i = 0
p_end = ptr + n
while true
buffer[i+1] = encode(b1 >> 2 )
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these are examples of unwanted alignment change

this can also happen at location of = instead of ()

buffer[i+1] = encode(b1 >> 2)
buffer[i+2] = encode(b1 << 4 | b2 >> 4)
buffer[i+3] = encode(b2 << 2 | b3 >> 6)
buffer[i+4] = encode( b3 )
buffer[i+4] = encode(b3)
i += 4
if p + 2 < p_end
b1 = unsafe_load(p, 1)
@@ -113,23 +113,23 @@ function Base.close(pipe::Base64EncodePipe)
# no leftover and padding
elseif k == 1
write(pipe.io,
encode(b1 >> 2),
encode(b1 << 4),
encodepadding(),
encodepadding())
encode(b1 >> 2),
encode(b1 << 4),
encodepadding(),
encodepadding())
elseif k == 2
write(pipe.io,
encode( b1 >> 2),
encode(b1 << 4 | b2 >> 4),
encode(b2 << 2 ),
encodepadding())
encode(b1 >> 2),
encode(b1 << 4 | b2 >> 4),
encode(b2 << 2),
encodepadding())
else
@assert k == 3
write(pipe.io,
encode(b1 >> 2 ),
encode(b1 << 4 | b2 >> 4),
encode(b2 << 2 | b3 >> 6),
encode( b3 ))
encode(b1 >> 2),
encode(b1 << 4 | b2 >> 4),
encode(b2 << 2 | b3 >> 6),
encode(b3))
end
return nothing
end
18 changes: 9 additions & 9 deletions stdlib/Base64/test/runtests.jl
Original file line number Diff line number Diff line change
@@ -75,18 +75,18 @@ const longDecodedText = "name = \"Genie\"\nuuid = \"c43c736e-a2d1-11e8-161f-af95
#@test read(ipipe, String) == inputText

# Decode with two padding characters ("==")
ipipe = Base64DecodePipe(IOBuffer(string(encodedMaxLine76[1:end - 2], "==")))
@test read(ipipe, String) == inputText[1:end - 1]
ipipe = Base64DecodePipe(IOBuffer(string(encodedMaxLine76[1:end-2], "==")))
@test read(ipipe, String) == inputText[1:end-1]

# Test incorrect format
ipipe = Base64DecodePipe(IOBuffer(encodedMaxLine76[1:end - 3]))
ipipe = Base64DecodePipe(IOBuffer(encodedMaxLine76[1:end-3]))
@test_throws ArgumentError read(ipipe, String)

# issue #21314
@test base64decode(chomp("test")) == base64decode("test")

# issue #32397
@test String(base64decode(longEncodedText)) == longDecodedText;
@test String(base64decode(longEncodedText)) == longDecodedText

# Optional padding
@test base64decode("AQ==") == base64decode("AQ")
@@ -116,16 +116,16 @@ struct PNG end
Base.show(io::IO, ::MIME"image/png", ::PNG) = print(io, "PNG")

@testset "stringmime" begin
@test stringmime("text/plain", [1 2;3 4]) == repr("text/plain", [1 2;3 4])
@test stringmime("text/plain", [1 2; 3 4]) == repr("text/plain", [1 2; 3 4])
@test stringmime("text/html", "raw html data") == "raw html data"
@test stringmime("text/plain", "string") == "\"string\""
@test stringmime("image/png", UInt8[2,3,4,7]) == "AgMEBw=="
@test stringmime("text/plain", 3.141592653589793, context = :compact => true) == "3.14159"
@test stringmime("image/png", UInt8[2, 3, 4, 7]) == "AgMEBw=="
@test stringmime("text/plain", 3.141592653589793, context=:compact => true) == "3.14159"
@test stringmime("image/png", PNG()) == stringmime(MIME("image/png"), PNG()) == "UE5H"
end

function splace(in::String, p = 0.3)
spaces = ["\n"," "]
function splace(in::String, p=0.3)
spaces = ["\n", " "]
len = length(in)
len == 0 && return in
rc::String = ""