# Is `self` a valid Base64 character ?
fun is_base64_char: Bool do
if code_point >= 127 then return false
- return ascii.is_base64_char
+ return code_point.is_base64_char
end
end
-redef class Byte
+redef class Int
# Is `self` a valid Base64 character ?
fun is_base64_char: Bool do
- if self == b'+' then return true
- if self == b'/' then return true
- if self > b'Z' then
- if self < b'a' then return false
- if self <= b'z' then return true
+ if self == u'+' then return true
+ if self == u'/' then return true
+ if self > u'Z' then
+ if self < u'a' then return false
+ if self <= u'z' then return true
return false
end
- if self >= b'A' then return true
- if self <= b'9' and self >= b'0' then return true
+ if self >= u'A' then return true
+ if self <= u'9' and self >= u'0' then return true
return false
end
# Returns the `base64` equivalent of `self`
#
# REQUIRE `self`.`is_base64_char`
- fun to_base64_char: Byte do
- if self == b'+' then return 62u8
- if self == b'/' then return 63u8
- if self > b'Z' then
- if self < b'a' then abort
- if self <= b'z' then return self - 71u8
+ fun to_base64_char: Int do
+ if self == u'+' then return 62
+ if self == u'/' then return 63
+ if self > u'Z' then
+ if self < u'a' then abort
+ if self <= u'z' then return self - 71
abort
end
- if self >= b'A' then return self - 0x41u8
- if self <= b'9' and self >= b'0' then return self + 4u8
+ if self >= u'A' then return self - 0x41
+ if self <= u'9' and self >= u'0' then return self + 4
abort
end
end
var in_off = 0
for s in [0 .. steps[ do
- var ind = ((self[in_off] & 0b1111_1100u8) >> 2).to_i
+ var ind = (self[in_off] & 0b1111_1100) >> 2
result.add base64_bytes[ind]
- ind = ((self[in_off] & 0b0000_0011u8) << 4).to_i | ((self[in_off + 1] & 0b1111_0000u8) >> 4).to_i
+ ind = ((self[in_off] & 0b0000_0011) << 4) | ((self[in_off + 1] & 0b1111_0000) >> 4)
result.add base64_bytes[ind]
- ind = ((self[in_off + 1] & 0b0000_1111u8) << 2).to_i | ((self[in_off + 2] & 0b1100_0000u8) >> 6).to_i
+ ind = ((self[in_off + 1] & 0b0000_1111) << 2) | ((self[in_off + 2] & 0b1100_0000) >> 6)
result.add base64_bytes[ind]
- ind = (self[in_off + 2] & 0b0011_1111u8).to_i
+ ind = (self[in_off + 2] & 0b0011_1111)
result.add base64_bytes[ind]
in_off += 3
end
if bytes_in_last_step == 1 then
- result.add base64_bytes[((self[in_off] & 0b1111_1100u8) >> 2).to_i]
- result.add base64_bytes[((self[in_off] & 0b0000_0011u8) << 4).to_i]
+ result.add base64_bytes[(self[in_off] & 0b1111_1100) >> 2]
+ result.add base64_bytes[(self[in_off] & 0b0000_0011) << 4]
else if bytes_in_last_step == 2 then
- result.add base64_bytes[((self[in_off] & 0b1111_1100u8) >> 2).to_i]
- result.add base64_bytes[(((self[in_off] & 0b0000_0011u8) << 4) | ((self[in_off + 1] & 0b1111_0000u8) >> 4)).to_i]
- result.add base64_bytes[((self[in_off + 1] & 0b0000_1111u8) << 2).to_i]
+ result.add base64_bytes[(self[in_off] & 0b1111_1100) >> 2]
+ result.add base64_bytes[((self[in_off] & 0b0000_0011) << 4) | ((self[in_off + 1] & 0b1111_0000) >> 4)]
+ result.add base64_bytes[(self[in_off + 1] & 0b0000_1111) << 2]
end
var rempad = if bytes_in_last_step > 0 then 3 - bytes_in_last_step else 0
- for i in [0 .. rempad[ do result.add b'='
+ for i in [0 .. rempad[ do result.add u'='
return result
end
if length == 0 then return new Bytes.empty
# Avoids constant unboxing
- var pad = b'='
+ var pad = '='
var result = new Bytes.with_capacity((length / 4 + 1) * 3)
break
end
# Ignore whitespaces
- if b <= 0x20u8 then continue
+ if b <= 0x20 then continue
if not b.is_base64_char then continue
curr <<= 6
curr += b.to_base64_char.to_i
cnt += 1
if cnt == 4 then
- result.add(((curr & 0xFF0000) >> 16).to_b)
- result.add(((curr & 0xFF00) >> 8).to_b)
- result.add((curr & 0xFF).to_b)
+ result.add ((curr & 0xFF0000) >> 16)
+ result.add ((curr & 0xFF00) >> 8)
+ result.add (curr & 0xFF)
curr = 0
cnt = 0
end
var pads = 0
for i in [endpos .. length[ do
var b = self[i]
- if b <= 0x20u8 then continue
+ if b <= 0x20 then continue
pads += 1
end
if cnt == 2 then
curr >>= 4
- result.add(curr.to_b)
+ result.add(curr)
else if cnt == 3 then
curr >>= 2
- result.add(((curr & 0xFF00) >> 8).to_b)
- result.add((curr & 0xFF).to_b)
+ result.add ((curr & 0xFF00) >> 8)
+ result.add (curr & 0xFF)
end
end
return result
var rlen = 0
var opos = length
for i in [0 .. length[ do
- if self[i] == b'=' then
+ if self[i] == u'=' then
opos = i
break
end
if self[i].is_whitespace then continue
- if not self[i].is_base64_char then return new Error("Invalid Base64 character at position {i}: {self[i].ascii}")
+ if not self[i].is_base64_char then return new Error("Invalid Base64 character at position {i}: {self[i].code_point}")
rlen += 1
if rlen > 4 then rlen -= 4
end
var pad = 0
for i in [opos .. length[ do
if self[i].is_whitespace then continue
- if self[i] != b'=' then return new Error("Invalid padding character {self[i].ascii} at position {i}")
+ if self[i] != u'=' then return new Error("Invalid padding character {self[i].code_point} at position {i}")
pad += 1
end
if rlen + pad != 4 then return new Error("Invalid padding length")