hex improvements
This commit is contained in:
@@ -273,10 +273,10 @@ func io.printf[..] : void
|
||||
s = s + 1
|
||||
|
||||
if s[0] == 'd'
|
||||
io.print_i64(_var_arg(i))
|
||||
io.print_i64(_var_arg(i) as i64)
|
||||
i = i + 1
|
||||
else if s[0] == 'x'
|
||||
io.print_i64_hex(_var_arg(i))
|
||||
io.print_i64_hex(_var_arg(i) as i64)
|
||||
i = i + 1
|
||||
else if s[0] == 's'
|
||||
io.print(_var_arg(i) as str)
|
||||
@@ -670,41 +670,38 @@ func str.parse_i64[s: str] : i64
|
||||
|
||||
func str.hex_encode[s: str, s_len: i64] : str
|
||||
let hex_chars: str = "0123456789abcdef"
|
||||
let j = 0
|
||||
let out: str = mem.alloc(s_len * 2 + 1) as str
|
||||
|
||||
for i in 0..s_len
|
||||
let high: u8 = (s[i] >> 4) & 15
|
||||
let low: u8 = s[i] & 15
|
||||
out[j] = hex_chars[high]
|
||||
out[j + 1] = hex_chars[low]
|
||||
j = j + 2
|
||||
let b: u8 = s[i]
|
||||
out[i * 2] = hex_chars[(b >> 4) & 15]
|
||||
out[i * 2 + 1] = hex_chars[b & 15]
|
||||
|
||||
out[j] = 0
|
||||
out[s_len * 2] = 0
|
||||
return out
|
||||
|
||||
func str._hex_digit_to_int[d: u8] : u8
|
||||
if d >= 'a' && d <= 'f'
|
||||
return d - 'a' + 10
|
||||
if d >= 'A' && d <= 'F'
|
||||
return d - 'A' + 10
|
||||
return d - '0'
|
||||
if d >= '0' && d <= '9'
|
||||
return d - '0'
|
||||
let lower: u8 = d | 32
|
||||
if lower >= 'a' && lower <= 'f'
|
||||
return lower - 'a' + 10
|
||||
panic("invalid hex digit passed to str._hex_digit_to_int")
|
||||
|
||||
func str.hex_decode[s: str] : str
|
||||
let s_len: i64 = str.len(s)
|
||||
if s_len % 2 != 0
|
||||
panic("invalid hex string passed to str.hex_decode")
|
||||
|
||||
let i = 0
|
||||
let j = 0
|
||||
let out: str = mem.alloc(s_len / 2 + 1) as str
|
||||
let out_len: i64 = s_len / 2
|
||||
let out: str = mem.alloc(out_len + 1) as str
|
||||
|
||||
while i < s_len
|
||||
out[j] = str._hex_digit_to_int(s[i]) * 16 + str._hex_digit_to_int(s[i + 1])
|
||||
i = i + 2
|
||||
j = j + 1
|
||||
for i in 0..out_len
|
||||
let high: u8 = str._hex_digit_to_int(s[i * 2])
|
||||
let low: u8 = str._hex_digit_to_int(s[i * 2 + 1])
|
||||
out[i] = (high << 4) | low
|
||||
|
||||
out[j] = 0
|
||||
out[out_len] = 0
|
||||
return out
|
||||
|
||||
func math.gcd[a: i64, b: i64] : i64
|
||||
|
||||
Reference in New Issue
Block a user