Ver código fonte

update core:filepath's clean, join and split_list to return optional Allocator_Errors

jason 1 ano atrás
pai
commit
07a9c69714

+ 8 - 8
core/os/os2/process_linux.odin

@@ -66,9 +66,9 @@ _process_list :: proc(allocator: runtime.Allocator) -> (list: []int, err: Error)
 	}
 	defer linux.close(dir_fd)
 
-	dynamic_list := make([dynamic]int, temp_allocator())
+	dynamic_list := make([dynamic]int, temp_allocator()) or_return
 
-	buf := make([dynamic]u8, 128, 128, temp_allocator())
+	buf := make([dynamic]u8, 128, 128, temp_allocator()) or_return
 	loop: for {
 		buflen: int
 		buflen, errno = linux.getdents(dir_fd, buf[:])
@@ -204,7 +204,7 @@ _process_info_by_pid :: proc(pid: int, selection: Process_Info_Fields, allocator
 				info.executable_path = strings.clone(cmdline[:terminator], allocator) or_return
 				info.fields += {.Executable_Path}
 			} else if cwd_err == nil {
-				info.executable_path = filepath.join({ cwd, cmdline[:terminator] }, allocator)
+				info.executable_path = filepath.join({ cwd, cmdline[:terminator] }, allocator) or_return
 				info.fields += {.Executable_Path}
 			} else {
 				break cmdline_if
@@ -414,9 +414,9 @@ _process_start :: proc(desc: Process_Desc) -> (process: Process, err: Error) {
 	executable_name := desc.command[0]
 	if strings.index_byte(executable_name, '/') == -1 {
 		path_env := get_env("PATH", temp_allocator())
-		path_dirs := filepath.split_list(path_env, temp_allocator())
+		path_dirs := filepath.split_list(path_env, temp_allocator()) or_return
 
-		exe_builder := strings.builder_make(temp_allocator())
+		exe_builder := strings.builder_make(temp_allocator()) or_return
 
 		found: bool
 		for dir in path_dirs {
@@ -467,7 +467,7 @@ _process_start :: proc(desc: Process_Desc) -> (process: Process, err: Error) {
 
 	// args and environment need to be a list of cstrings
 	// that are terminated by a nil pointer.
-	cargs := make([]cstring, len(desc.command) + 1, temp_allocator())
+	cargs := make([]cstring, len(desc.command) + 1, temp_allocator()) or_return
 	for command, i in desc.command {
 		cargs[i] = temp_cstring(command) or_return
 	}
@@ -478,7 +478,7 @@ _process_start :: proc(desc: Process_Desc) -> (process: Process, err: Error) {
 		// take this process's current environment
 		env = raw_data(export_cstring_environment(temp_allocator()))
 	} else {
-		cenv := make([]cstring, len(desc.env) + 1, temp_allocator())
+		cenv := make([]cstring, len(desc.env) + 1, temp_allocator()) or_return
 		for env, i in desc.env {
 			cenv[i] = temp_cstring(env) or_return
 		}
@@ -609,7 +609,7 @@ _process_start :: proc(desc: Process_Desc) -> (process: Process, err: Error) {
 _process_state_update_times :: proc(state: ^Process_State) -> (err: Error) {
 	TEMP_ALLOCATOR_GUARD()
 
-	stat_path_buf: [32]u8
+	stat_path_buf: [48]u8
 	path_builder := strings.builder_from_bytes(stat_path_buf[:])
 	strings.write_string(&path_builder, "/proc/")
 	strings.write_int(&path_builder, int(state.pid))

+ 30 - 25
core/path/filepath/path.odin

@@ -2,6 +2,7 @@
 // To process paths such as URLs that depend on forward slashes regardless of the OS, use the path package
 package filepath
 
+import "base:runtime"
 import "core:strings"
 
 SEPARATOR_CHARS :: `/\`
@@ -244,7 +245,7 @@ long_ext :: proc(path: string) -> string {
 	If the result of the path is an empty string, the returned path with be `"."`.
 
 */
-clean :: proc(path: string, allocator := context.allocator) -> string {
+clean :: proc(path: string, allocator := context.allocator) -> (cleaned: string, err: runtime.Allocator_Error) #optional_allocator_error {
 	context.allocator = allocator
 
 	path := path
@@ -256,9 +257,9 @@ clean :: proc(path: string, allocator := context.allocator) -> string {
 		if vol_len > 1 && original_path[1] != ':' {
 			s, ok := from_slash(original_path)
 			if !ok {
-				s = strings.clone(s)
+				s = strings.clone(s) or_return
 			}
-			return s
+			return s, nil
 		}
 		return strings.concatenate({original_path, "."})
 	}
@@ -275,7 +276,7 @@ clean :: proc(path: string, allocator := context.allocator) -> string {
 
 	r, dot_dot := 0, 0
 	if rooted {
-		lazy_buffer_append(out, SEPARATOR)
+		lazy_buffer_append(out, SEPARATOR) or_return
 		r, dot_dot = 1, 1
 	}
 
@@ -295,33 +296,35 @@ clean :: proc(path: string, allocator := context.allocator) -> string {
 				}
 			case !rooted:
 				if out.w > 0 {
-					lazy_buffer_append(out, SEPARATOR)
+					lazy_buffer_append(out, SEPARATOR) or_return
 				}
-				lazy_buffer_append(out, '.')
-				lazy_buffer_append(out, '.')
+				lazy_buffer_append(out, '.') or_return
+				lazy_buffer_append(out, '.') or_return
 				dot_dot = out.w
 			}
 		case:
 			if rooted && out.w != 1 || !rooted && out.w != 0 {
-				lazy_buffer_append(out, SEPARATOR)
+				lazy_buffer_append(out, SEPARATOR) or_return
 			}
 			for ; r < n && !is_separator(path[r]); r += 1 {
-				lazy_buffer_append(out, path[r])
+				lazy_buffer_append(out, path[r]) or_return
 			}
 
 		}
 	}
 
 	if out.w == 0 {
-		lazy_buffer_append(out, '.')
+		lazy_buffer_append(out, '.') or_return
 	}
 
-	s := lazy_buffer_string(out)
-	cleaned, new_allocation := from_slash(s)
+	s := lazy_buffer_string(out) or_return
+
+	new_allocation: bool
+	cleaned, new_allocation = from_slash(s)
 	if new_allocation {
 		delete(s)
 	}
-	return cleaned
+	return
 }
 
 // Returns the result of replacing each forward slash `/` character in the path with the separate OS specific character.
@@ -453,9 +456,9 @@ dir :: proc(path: string, allocator := context.allocator) -> string {
 // An empty string returns nil. A non-empty string with no separators returns a 1-element array.
 // Any empty components will be included, e.g. `a::b` will return a 3-element array, as will `::`.
 // Separators within pairs of double-quotes will be ignored and stripped, e.g. `"a:b"c:d` will return []{`a:bc`, `d`}.
-split_list :: proc(path: string, allocator := context.allocator) -> []string {
+split_list :: proc(path: string, allocator := context.allocator) -> (list: []string, err: runtime.Allocator_Error) #optional_allocator_error {
 	if path == "" {
-		return nil
+		return nil, nil
 	}
 
 	start: int
@@ -475,7 +478,7 @@ split_list :: proc(path: string, allocator := context.allocator) -> []string {
 	}
 
 	start, quote = 0, false
-	list := make([]string, count + 1, allocator)
+	list = make([]string, count + 1, allocator) or_return
 	index := 0
 	for i := 0; i < len(path); i += 1 {
 		c := path[i]
@@ -494,12 +497,12 @@ split_list :: proc(path: string, allocator := context.allocator) -> []string {
 	for s0, i in list {
 		s, new := strings.replace_all(s0, `"`, ``, allocator)
 		if !new {
-			s = strings.clone(s, allocator)
+			s = strings.clone(s, allocator) or_return
 		}
 		list[i] = s
 	}
 
-	return list
+	return list, nil
 }
 
 
@@ -526,33 +529,35 @@ lazy_buffer_index :: proc(lb: ^Lazy_Buffer, i: int) -> byte {
 	return lb.s[i]
 }
 @(private)
-lazy_buffer_append :: proc(lb: ^Lazy_Buffer, c: byte) {
+lazy_buffer_append :: proc(lb: ^Lazy_Buffer, c: byte) -> (err: runtime.Allocator_Error) {
 	if lb.b == nil {
 		if lb.w < len(lb.s) && lb.s[lb.w] == c {
 			lb.w += 1
 			return
 		}
-		lb.b = make([]byte, len(lb.s))
+		lb.b = make([]byte, len(lb.s)) or_return
 		copy(lb.b, lb.s[:lb.w])
 	}
 	lb.b[lb.w] = c
 	lb.w += 1
+	return
 }
 @(private)
-lazy_buffer_string :: proc(lb: ^Lazy_Buffer) -> string {
+lazy_buffer_string :: proc(lb: ^Lazy_Buffer) -> (s: string, err: runtime.Allocator_Error) {
 	if lb.b == nil {
 		return strings.clone(lb.vol_and_path[:lb.vol_len+lb.w])
 	}
 
 	x := lb.vol_and_path[:lb.vol_len]
 	y := string(lb.b[:lb.w])
-	z := make([]byte, len(x)+len(y))
+	z := make([]byte, len(x)+len(y)) or_return
 	copy(z, x)
 	copy(z[len(x):], y)
-	return string(z)
+	return string(z), nil
 }
 @(private)
-lazy_buffer_destroy :: proc(lb: ^Lazy_Buffer) {
-	delete(lb.b)
+lazy_buffer_destroy :: proc(lb: ^Lazy_Buffer) -> runtime.Allocator_Error {
+	err := delete(lb.b)
 	lb^ = {}
+	return err
 }

+ 3 - 3
core/path/filepath/path_unix.odin

@@ -39,15 +39,15 @@ abs :: proc(path: string, allocator := context.allocator) -> (string, bool) {
 	return path_str, true
 }
 
-join :: proc(elems: []string, allocator := context.allocator) -> string {
+join :: proc(elems: []string, allocator := context.allocator) -> (joined: string, err: runtime.Allocator_Error) #optional_allocator_error {
 	for e, i in elems {
 		if e != "" {
 			runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD(ignore = context.temp_allocator == allocator)
-			p := strings.join(elems[i:], SEPARATOR_STRING, context.temp_allocator)
+			p := strings.join(elems[i:], SEPARATOR_STRING, context.temp_allocator) or_return
 			return clean(p, allocator)
 		}
 	}
-	return ""
+	return "", nil
 }
 
 @(private)

+ 4 - 1
core/strings/strings.odin

@@ -2071,7 +2071,10 @@ replace :: proc(s, old, new: string, n: int, allocator := context.allocator, loc
 	}
 
 
-	t := make([]byte, len(s) + byte_count*(len(new) - len(old)), allocator, loc)
+	t, err := make([]byte, len(s) + byte_count*(len(new) - len(old)), allocator, loc)
+	if err != nil {
+		return
+	}
 	was_allocation = true
 
 	w := 0

+ 0 - 0
core/sys/windows/kernel32.odin