-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.n
More file actions
205 lines (189 loc) · 5.01 KB
/
utils.n
File metadata and controls
205 lines (189 loc) · 5.01 KB
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import fs
import libc
import syscall
import "parser.n" as *
// filenames_or_default returns the provided filenames or [".env"] if empty
#local
fn filenames_or_default([string] filenames):[string] {
if filenames.len() == 0 {
return ['.env']
}
return filenames
}
// load_file reads a single .env file and sets env vars.
// If overload is false, existing env vars are not overwritten.
#local
fn load_file(string filename, bool overload_):void! {
var env_map = read_file(filename)
// get current environment variable keys
var raw_env = libc.get_envs()
{string:bool} current_env = {}
for raw_line in raw_env {
var eq_idx = find_char_index(raw_line, '='[0])
if eq_idx > 0 {
current_env[raw_line[..eq_idx]] = true
}
}
for key, value in env_map {
if !current_env.contains(key) || overload_ {
libc.setenv(key.to_cstr(), value.to_cstr(), 1)
}
}
}
// read_file reads a single .env file and returns its key-value pairs
#local
fn read_file(string filename):{string:string}! {
var f = fs.open(filename, syscall.O_RDONLY, 0)
var content = f.content()
f.close()
{string:string} out = {}
parse_bytes(content, out)
return out
}
// double_quote_escape escapes special characters for double-quoted .env values
#local
fn double_quote_escape(string line):string {
var result = line
// order matters: backslash first
result = replace_all(result, '\\', '\\\\')
result = replace_all(result, '\n', '\\n')
result = replace_all(result, '\r', '\\r')
result = replace_all(result, '"', '\\"')
result = replace_all(result, '!', '\\!')
result = replace_all(result, '$', '\\$')
result = replace_all(result, '`', '\\`')
return result
}
// is_integer checks if a string represents a pure integer (optional leading minus)
#local
fn is_integer(string s):bool {
if s.len() == 0 {
return false
}
var start = 0
if s[0] == '-'[0] || s[0] == '+'[0] {
start = 1
}
if start >= s.len() {
return false
}
for int i = start; i < s.len(); i += 1 {
if !is_digit(s[i]) {
return false
}
}
return true
}
// sort_strings sorts a string array in-place using bubble sort
#local
fn sort_strings([string] arr) {
var n = arr.len()
for int i = 0; i < n - 1; i += 1 {
for int j = 0; j < n - i - 1; j += 1 {
if arr[j] > arr[j + 1] {
(arr[j], arr[j + 1]) = (arr[j + 1], arr[j])
}
}
}
}
// join_strings joins a string array with a separator
#local
fn join_strings([string] arr, string sep):string {
if arr.len() == 0 {
return ''
}
var result = arr[0]
for int i = 1; i < arr.len(); i += 1 {
result += sep + arr[i]
}
return result
}
#local
fn get_raw(string key):string! {
var val = libc.getenv(key.to_cstr())
if val as anyptr == 0 as anyptr {
throw errorf('environment variable "%s" is not set', key)
}
return val.to_string()
}
#local
fn parse_bool(string s):bool! {
var lower = to_lower(s)
if lower == 'true' || lower == '1' || lower == 'yes' {
return true
}
if lower == 'false' || lower == '0' || lower == 'no' {
return false
}
throw errorf('cannot parse "%s" as bool (expected true/false/1/0/yes/no)', s)
}
#local
fn parse_dict(string s):{string:string}! {
{string:string} result = {}
var pairs = split_and_trim(s, ','[0])
for int i = 0; i < pairs.len(); i += 1 {
var pair = pairs[i]
if pair.len() == 0 {
continue
}
var eq_pos = -1
for int j = 0; j < pair.len(); j += 1 {
if pair[j] == '='[0] {
eq_pos = j
break
}
}
if eq_pos < 0 {
throw errorf('invalid key=value pair: "%s"', pair)
}
var k = trim_spaces(pair[0..eq_pos])
var v = trim_spaces(pair[eq_pos + 1..])
result[k] = v
}
return result
}
#local
fn to_lower(string s):string {
[u8] result = []
for int i = 0; i < s.len(); i += 1 {
var c = s[i]
if c >= 65 && c <= 90 {
result.push(c + 32)
} else {
result.push(c)
}
}
return result as string
}
#local
fn split_and_trim(string s, u8 delim):[string] {
[string] parts = []
var start = 0
for int i = 0; i < s.len(); i += 1 {
if s[i] == delim {
parts.push(trim_spaces(s[start..i]))
start = i + 1
}
}
parts.push(trim_spaces(s[start..]))
return parts
}
#local
fn is_space(u8 c):bool {
return c == ' '[0] || c == '\t'[0] || c == '\r'[0] || c == 11 || c == 12
}
#local
fn trim_spaces(string s):string {
var left = 0
for left < s.len() && is_space(s[left]) {
left += 1
}
var right = s.len()
for right > left && (is_space(s[right - 1]) || s[right - 1] == '\n'[0]) {
right -= 1
}
if left == 0 && right == s.len() {
return s
}
return s[left..right]
}