소스 검색

ripple bill-suggestions

Colin Davidson 2 년 전
부모
커밋
38d58e818c
3개의 변경된 파일15개의 추가작업 그리고 24개의 파일을 삭제
  1. 4 7
      core/net/addr.odin
  2. 10 16
      core/net/dns.odin
  3. 1 1
      core/net/url.odin

+ 4 - 7
core/net/addr.odin

@@ -34,11 +34,8 @@ import "core:fmt"
 	If `allow_non_decimal` is true, `aton` is told each component must be decimal and max 255.
 */
 parse_ip4_address :: proc(address_and_maybe_port: string, allow_non_decimal := false) -> (addr: IP4_Address, ok: bool) {
-	res, res_ok := aton(address_and_maybe_port, .IP4, !allow_non_decimal)
-	if ip4, ip4_ok := res.(IP4_Address); ip4_ok {
-		return ip4, res_ok
-	}
-	return {}, false
+	res := aton(address_and_maybe_port, .IP4, !allow_non_decimal) or_return
+	return res.?
 }
 
 /*
@@ -432,7 +429,7 @@ parse_hostname_or_endpoint :: proc(endpoint_str: string) -> (target: Host_Or_End
 // Returns ok=false if port is not a number.
 split_port :: proc(endpoint_str: string) -> (addr_or_host: string, port: int, ok: bool) {
 	// IP6 [addr_or_host]:port
-	if i := strings.last_index(endpoint_str, "]:"); i != -1 {
+	if i := strings.last_index(endpoint_str, "]:"); i >= 0 {
 		addr_or_host = endpoint_str[1:i]
 		port, ok = strconv.parse_int(endpoint_str[i+2:], 10)
 
@@ -741,4 +738,4 @@ parse_ip_component :: proc(input: string, max_value := u64(max(u32)), bases := D
 
 	// If we consumed at least 1 digit byte, `value` *should* continue a valid number in an appropriate base in the allowable range.
 	return value, digit_bytes + prefix_bytes, digit_bytes >= 1
-}
+}

+ 10 - 16
core/net/dns.odin

@@ -58,7 +58,7 @@ dns_configuration := DEFAULT_DNS_CONFIGURATION
 replace_environment_path :: proc(path: string, allocator := context.allocator) -> (res: string, ok: bool) {
 	// Nothing to replace. Return a clone of the original.
 	if strings.count(path, "%") != 2 {
-		return strings.clone(path), true
+		return strings.clone(path, allocator), true
 	}
 
 	left  := strings.index(path, "%") + 1
@@ -68,10 +68,10 @@ replace_environment_path :: proc(path: string, allocator := context.allocator) -
 	assert(right > 0 && right <= len(path)) // should be covered by there being two %
 
 	env_key := path[left: right]
-	env_val := os.get_env(env_key)
+	env_val := os.get_env(env_key, allocator)
 	defer delete(env_val)
 
-	res, _ = strings.replace(path, path[left - 1: right + 1], env_val, 1)
+	res, _ = strings.replace(path, path[left - 1: right + 1], env_val, 1, allocator)
 	return res, true
 }
 
@@ -356,10 +356,7 @@ unpack_dns_header :: proc(id: u16be, bits: u16be) -> (hdr: DNS_Header) {
 load_resolv_conf :: proc(resolv_conf_path: string, allocator := context.allocator) -> (name_servers: []Endpoint, ok: bool) {
 	context.allocator = allocator
 
-	res, success := os.read_entire_file_from_filename(resolv_conf_path)
-	if !success {
-		return
-	}
+	res := os.read_entire_file_from_filename(resolv_conf_path) or_return
 	defer delete(res)
 	resolv_str := string(res)
 
@@ -393,10 +390,7 @@ load_resolv_conf :: proc(resolv_conf_path: string, allocator := context.allocato
 load_hosts :: proc(hosts_file_path: string, allocator := context.allocator) -> (hosts: []DNS_Host_Entry, ok: bool) {
 	context.allocator = allocator
 
-	res, success := os.read_entire_file_from_filename(hosts_file_path, allocator)
-	if !success {
-		return
-	}
+	res := os.read_entire_file_from_filename(hosts_file_path, allocator) or_return
 	defer delete(res)
 
 	_hosts := make([dynamic]DNS_Host_Entry, 0, allocator)
@@ -800,7 +794,7 @@ parse_response :: proc(response: []u8, filter: DNS_Record_Type = nil, allocator
 
 	cur_idx := header_size_bytes
 
-	for i := 0; i < question_count; i += 1 {
+	for _ in 0..<question_count {
 		if cur_idx == len(response) {
 			continue
 		}
@@ -812,7 +806,7 @@ parse_response :: proc(response: []u8, filter: DNS_Record_Type = nil, allocator
 		cur_idx += hn_sz + dq_sz
 	}
 
-	for i := 0; i < answer_count; i += 1 {
+	for _ in 0..<answer_count {
 		if cur_idx == len(response) {
 			continue
 		}
@@ -825,7 +819,7 @@ parse_response :: proc(response: []u8, filter: DNS_Record_Type = nil, allocator
 		append(&_records, rec)
 	}
 
-	for i := 0; i < authority_count; i += 1 {
+	for _ in 0..<authority_count {
 		if cur_idx == len(response) {
 			continue
 		}
@@ -838,7 +832,7 @@ parse_response :: proc(response: []u8, filter: DNS_Record_Type = nil, allocator
 		append(&_records, rec)
 	}
 
-	for i := 0; i < additional_count; i += 1 {
+	for _ in 0..<additional_count {
 		if cur_idx == len(response) {
 			continue
 		}
@@ -852,4 +846,4 @@ parse_response :: proc(response: []u8, filter: DNS_Record_Type = nil, allocator
 	}
 
 	return _records[:], true
-}
+}

+ 1 - 1
core/net/url.odin

@@ -25,7 +25,7 @@ split_url :: proc(url: string, allocator := context.allocator) -> (scheme, host,
 	s := url
 
 	i := strings.last_index(s, "://")
-	if i != -1 {
+	if i >= 0 {
 		scheme = s[:i]
 		s = s[i+3:]
 	}