commit 6b851eaed490274d17c5cd7eb375a8bd064fad7f
parent f334db3ab5c3e91db8e9dc2aa95c166f372f10a7
Author: Drew DeVault <sir@cmpwn.com>
Date: Sat, 30 Jan 2021 19:16:31 -0500
os: getenv improvements
- accepts const str for name
- only converts input to UTF-8 once
After the next set of tagged union improvements, it should be possible
to set the return value type to (const str | void) as well.
Diffstat:
2 files changed, 5 insertions(+), 4 deletions(-)
diff --git a/os/+linux/environ.ha b/os/+linux/environ.ha
@@ -31,15 +31,16 @@ let args_static: [32]str = [""...];
};
// Looks up an environment variable and returns its value, or void if unset.
-export fn getenv(name: str) (str | void) = {
- // TODO: Type promotion can simplify this null comparison
+export fn getenv(name: const str) (str | void) = {
+ const name_b = strings::to_utf8(name);
+ // TODO: Type promotion can simplify this null comparison:
for (let i = 0z; rt::envp[i] != null: nullable *char; i += 1z) {
const item = rt::envp[i]: *[*]u8;
const eq: size = match (bytes::index(item[..], '=': u32: u8)) {
void => abort("Environment violates System-V invariants"),
i: size => i,
};
- if (bytes::equal(strings::to_utf8(name), item[..eq])) {
+ if (bytes::equal(name_b, item[..eq])) {
const ln = strings::c_strlen(item: *const char);
return strings::from_utf8(item[eq+1z..ln]);
};
diff --git a/os/environ.ha b/os/environ.ha
@@ -1,5 +1,5 @@
// Looks up an environment variable and returns its value. Aborts if unset.
-export fn must_getenv(name: str) str = {
+export fn must_getenv(name: const str) const str = {
// TODO: It would be nice to print the name of the variable in the error
match (getenv(name)) {
s: str => s,