aboutsummaryrefslogtreecommitdiff
path: root/core/os/internal_util.odin
blob: a279e9beeb36557df6fc9114d9b92c692812d7aa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#+private
package os

import "base:intrinsics"
import "base:runtime"
import "core:math/rand"


// Splits pattern by the last wildcard "*", if it exists, and returns the prefix and suffix
// parts which are split by the last "*"
@(require_results)
_prefix_and_suffix :: proc(pattern: string) -> (prefix, suffix: string, err: Error) {
	for i in 0..<len(pattern) {
		if is_path_separator(pattern[i]) {
			err = .Pattern_Has_Separator
			return
		}
	}
	prefix = pattern
	for i := len(pattern)-1; i >= 0; i -= 1 {
		if pattern[i] == '*' {
			prefix, suffix = pattern[:i], pattern[i+1:]
			break
		}
	}
	return
}

@(require_results)
clone_string :: proc(s: string, allocator: runtime.Allocator) -> (res: string, err: runtime.Allocator_Error) {
	buf := make([]byte, len(s), allocator) or_return
	copy(buf, s)
	return string(buf), nil
}


@(require_results)
clone_to_cstring :: proc(s: string, allocator: runtime.Allocator) -> (res: cstring, err: runtime.Allocator_Error) {
	res = "" // do not use a `nil` cstring
	buf := make([]byte, len(s)+1, allocator) or_return
	copy(buf, s)
	buf[len(s)] = 0
	return cstring(&buf[0]), nil
}

@(require_results)
string_from_null_terminated_bytes :: proc(b: []byte) -> (res: string) {
	s := string(b)
	i := 0
	for ; i < len(s); i += 1 {
		if s[i] == 0 {
			break
		}
	}
	return s[:i]
}

@(require_results)
concatenate_strings_from_buffer :: proc(buf: []byte, strings: ..string) -> string {
	n := 0
	for s in strings {
		(n < len(buf)) or_break
		n += copy(buf[n:], s)
	}
	n = min(len(buf), n)
	return string(buf[:n])
}

@(require_results)
concatenate :: proc(strings: []string, allocator: runtime.Allocator) -> (res: string, err: runtime.Allocator_Error) {
	n := 0
	for s in strings {
		n += len(s)
	}
	buf := make([]byte, n, allocator) or_return
	n = 0
	for s in strings {
		n += copy(buf[n:], s)
	}
	return string(buf), nil
}

@(require_results)
random_string :: proc(buf: []byte) -> string {
	for i := 0; i < len(buf); i += 16 {
		n := rand.uint64()
		end := min(i + 16, len(buf))
		for j := i; j < end; j += 1 {
			buf[j] = '0' + u8(n) % 10
			n >>= 4
		}
	}
	return string(buf)
}