hex improvements
This commit is contained in:
@@ -8,7 +8,7 @@ A very cool language
|
|||||||
* Growing [standard library](https://github.com/antpiasecki/zern/tree/main/src/std)
|
* Growing [standard library](https://github.com/antpiasecki/zern/tree/main/src/std)
|
||||||
* Produces tiny static executables (11KB for `hello.zr`)
|
* Produces tiny static executables (11KB for `hello.zr`)
|
||||||
* No libc required!
|
* No libc required!
|
||||||
* Has the pipe operator, variadics, dynamic arrays, hashmaps, etc.
|
* Has the pipe operator, variadics, dynamic arrays, hashmaps, DNS resolver, etc.
|
||||||
|
|
||||||
## Syntax
|
## Syntax
|
||||||
```rust
|
```rust
|
||||||
|
|||||||
@@ -201,7 +201,7 @@ func net.resolve?[domain: str] : i64
|
|||||||
let query: io.Buffer = net.build_dns_query(domain, DNS_TYPE_A)
|
let query: io.Buffer = net.build_dns_query(domain, DNS_TYPE_A)
|
||||||
|
|
||||||
// TODO: this probably shouldnt be hardcoded
|
// TODO: this probably shouldnt be hardcoded
|
||||||
let s: net.UDPSocket = net.create_udp_client?(net.pack_addr(8, 8, 8, 8), 53)
|
let s: net.UDPSocket = net.create_udp_client?(net.pack_addr(1, 1, 1, 1), 53)
|
||||||
if err.check()
|
if err.check()
|
||||||
return -1
|
return -1
|
||||||
|
|
||||||
|
|||||||
@@ -273,10 +273,10 @@ func io.printf[..] : void
|
|||||||
s = s + 1
|
s = s + 1
|
||||||
|
|
||||||
if s[0] == 'd'
|
if s[0] == 'd'
|
||||||
io.print_i64(_var_arg(i))
|
io.print_i64(_var_arg(i) as i64)
|
||||||
i = i + 1
|
i = i + 1
|
||||||
else if s[0] == 'x'
|
else if s[0] == 'x'
|
||||||
io.print_i64_hex(_var_arg(i))
|
io.print_i64_hex(_var_arg(i) as i64)
|
||||||
i = i + 1
|
i = i + 1
|
||||||
else if s[0] == 's'
|
else if s[0] == 's'
|
||||||
io.print(_var_arg(i) as str)
|
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
|
func str.hex_encode[s: str, s_len: i64] : str
|
||||||
let hex_chars: str = "0123456789abcdef"
|
let hex_chars: str = "0123456789abcdef"
|
||||||
let j = 0
|
|
||||||
let out: str = mem.alloc(s_len * 2 + 1) as str
|
let out: str = mem.alloc(s_len * 2 + 1) as str
|
||||||
|
|
||||||
for i in 0..s_len
|
for i in 0..s_len
|
||||||
let high: u8 = (s[i] >> 4) & 15
|
let b: u8 = s[i]
|
||||||
let low: u8 = s[i] & 15
|
out[i * 2] = hex_chars[(b >> 4) & 15]
|
||||||
out[j] = hex_chars[high]
|
out[i * 2 + 1] = hex_chars[b & 15]
|
||||||
out[j + 1] = hex_chars[low]
|
|
||||||
j = j + 2
|
|
||||||
|
|
||||||
out[j] = 0
|
out[s_len * 2] = 0
|
||||||
return out
|
return out
|
||||||
|
|
||||||
func str._hex_digit_to_int[d: u8] : u8
|
func str._hex_digit_to_int[d: u8] : u8
|
||||||
if d >= 'a' && d <= 'f'
|
if d >= '0' && d <= '9'
|
||||||
return d - 'a' + 10
|
return d - '0'
|
||||||
if d >= 'A' && d <= 'F'
|
let lower: u8 = d | 32
|
||||||
return d - 'A' + 10
|
if lower >= 'a' && lower <= 'f'
|
||||||
return d - '0'
|
return lower - 'a' + 10
|
||||||
|
panic("invalid hex digit passed to str._hex_digit_to_int")
|
||||||
|
|
||||||
func str.hex_decode[s: str] : str
|
func str.hex_decode[s: str] : str
|
||||||
let s_len: i64 = str.len(s)
|
let s_len: i64 = str.len(s)
|
||||||
if s_len % 2 != 0
|
if s_len % 2 != 0
|
||||||
panic("invalid hex string passed to str.hex_decode")
|
panic("invalid hex string passed to str.hex_decode")
|
||||||
|
|
||||||
let i = 0
|
let out_len: i64 = s_len / 2
|
||||||
let j = 0
|
let out: str = mem.alloc(out_len + 1) as str
|
||||||
let out: str = mem.alloc(s_len / 2 + 1) as str
|
|
||||||
|
|
||||||
while i < s_len
|
for i in 0..out_len
|
||||||
out[j] = str._hex_digit_to_int(s[i]) * 16 + str._hex_digit_to_int(s[i + 1])
|
let high: u8 = str._hex_digit_to_int(s[i * 2])
|
||||||
i = i + 2
|
let low: u8 = str._hex_digit_to_int(s[i * 2 + 1])
|
||||||
j = j + 1
|
out[i] = (high << 4) | low
|
||||||
|
|
||||||
out[j] = 0
|
out[out_len] = 0
|
||||||
return out
|
return out
|
||||||
|
|
||||||
func math.gcd[a: i64, b: i64] : i64
|
func math.gcd[a: i64, b: i64] : i64
|
||||||
|
|||||||
Reference in New Issue
Block a user