diff options
Diffstat (limited to 'system/libraries/string')
| -rw-r--r-- | system/libraries/string/builder.zig | 48 | ||||
| -rw-r--r-- | system/libraries/string/cstring.zig | 17 | ||||
| -rw-r--r-- | system/libraries/string/location.zig | 59 | ||||
| -rw-r--r-- | system/libraries/string/string.zig | 16 | ||||
| -rw-r--r-- | system/libraries/string/string.zon | 6 |
5 files changed, 146 insertions, 0 deletions
diff --git a/system/libraries/string/builder.zig b/system/libraries/string/builder.zig new file mode 100644 index 0000000..16bd35c --- /dev/null +++ b/system/libraries/string/builder.zig @@ -0,0 +1,48 @@ +//! String building + +pub fn build(buf: []u8, parts: anytype) []const u8 { + var pos: usize = 0; + inline for (parts) |part| { + for (part) |c| { + if (pos >= buf.len) break; + buf[pos] = c; + pos += 1; + } + } + return buf[0..pos]; +} + +pub fn concat(buf: []u8, a: []const u8, b: []const u8) []const u8 { + var pos: usize = 0; + for (a) |c| { + if (pos >= buf.len) break; + buf[pos] = c; + pos += 1; + } + for (b) |c| { + if (pos >= buf.len) break; + buf[pos] = c; + pos += 1; + } + return buf[0..pos]; +} + +pub fn concat3(buf: []u8, a: []const u8, b: []const u8, c: []const u8) []const u8 { + var pos: usize = 0; + for (a) |ch| { + if (pos >= buf.len) break; + buf[pos] = ch; + pos += 1; + } + for (b) |ch| { + if (pos >= buf.len) break; + buf[pos] = ch; + pos += 1; + } + for (c) |ch| { + if (pos >= buf.len) break; + buf[pos] = ch; + pos += 1; + } + return buf[0..pos]; +} diff --git a/system/libraries/string/cstring.zig b/system/libraries/string/cstring.zig new file mode 100644 index 0000000..32ad60f --- /dev/null +++ b/system/libraries/string/cstring.zig @@ -0,0 +1,17 @@ +//! C-string utilities + +pub fn len(str: [*:0]const u8) usize { + var i: usize = 0; + while (str[i] != 0) : (i += 1) {} + return i; +} + +pub fn toSlice(str: [*:0]const u8) []const u8 { + return str[0..len(str)]; +} + +pub fn findNull(buf: []const u8) usize { + var i: usize = 0; + while (i < buf.len and buf[i] != 0) : (i += 1) {} + return i; +} diff --git a/system/libraries/string/location.zig b/system/libraries/string/location.zig new file mode 100644 index 0000000..ff1c915 --- /dev/null +++ b/system/libraries/string/location.zig @@ -0,0 +1,59 @@ +//! Location utilities + +pub fn getStackName(location: []const u8) []const u8 { + if (location.len == 0 or (location.len == 1 and location[0] == '/')) { + return "/"; + } + + var last_slash: usize = 0; + for (location, 0..) |c, i| { + if (c == '/') { + last_slash = i; + } + } + + if (last_slash == location.len - 1 and location.len > 1) { + var i: usize = location.len - 2; + while (i > 0) : (i -= 1) { + if (location[i] == '/') { + return location[i + 1 .. location.len - 1]; + } + } + return location[1 .. location.len - 1]; + } + + if (last_slash + 1 < location.len) { + return location[last_slash + 1 ..]; + } + + return location; +} + +pub fn parent(location: []const u8, buf: []u8) []const u8 { + if (location.len <= 1) { + buf[0] = '/'; + return buf[0..1]; + } + + var end = location.len; + if (location[end - 1] == '/') { + end -= 1; + } + + var i: usize = end; + while (i > 0) : (i -= 1) { + if (location[i - 1] == '/') { + if (i == 1) { + buf[0] = '/'; + return buf[0..1]; + } + for (location[0 .. i - 1], 0..) |c, j| { + buf[j] = c; + } + return buf[0 .. i - 1]; + } + } + + buf[0] = '/'; + return buf[0..1]; +} diff --git a/system/libraries/string/string.zig b/system/libraries/string/string.zig new file mode 100644 index 0000000..1d7c798 --- /dev/null +++ b/system/libraries/string/string.zig @@ -0,0 +1,16 @@ +//! String utilities + +pub const cstring = @import("cstring.zig"); +pub const location = @import("location.zig"); +pub const builder = @import("builder.zig"); + +pub const len = cstring.len; +pub const toSlice = cstring.toSlice; +pub const findNull = cstring.findNull; + +pub const getStackName = location.getStackName; +pub const parent = location.parent; + +pub const build = builder.build; +pub const concat = builder.concat; +pub const concat3 = builder.concat3; diff --git a/system/libraries/string/string.zon b/system/libraries/string/string.zon new file mode 100644 index 0000000..e722bf8 --- /dev/null +++ b/system/libraries/string/string.zon @@ -0,0 +1,6 @@ +.{ + .name = "string", + .version = "1.0.0", + .type = .library, + .entry = "string.zig", +} |
