diff --git a/README.md b/README.md index 34ea29d..435a8f4 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ A very cool language * Growing [standard library](https://github.com/antpiasecki/zern/tree/main/src/std) * Produces tiny static executables (11KB for `hello.zr`) * No libc required! -* Has the pipe operator, variadics, dynamic arrays, hashmaps, etc. +* Has the pipe operator, variadics, dynamic arrays, hashmaps, DNS resolver, etc. ## Syntax ```rust diff --git a/src/std/net.zr b/src/std/net.zr index e4c4250..41685ae 100644 --- a/src/std/net.zr +++ b/src/std/net.zr @@ -201,7 +201,7 @@ func net.resolve?[domain: str] : i64 let query: io.Buffer = net.build_dns_query(domain, DNS_TYPE_A) // 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() return -1 diff --git a/src/std/std.zr b/src/std/std.zr index 3ce9cdf..bbbd894 100644 --- a/src/std/std.zr +++ b/src/std/std.zr @@ -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