aboutsummaryrefslogtreecommitdiff
path: root/std/str.zig
diff options
context:
space:
mode:
Diffstat (limited to 'std/str.zig')
-rw-r--r--std/str.zig34
1 files changed, 34 insertions, 0 deletions
diff --git a/std/str.zig b/std/str.zig
new file mode 100644
index 0000000000..7df0c5658a
--- /dev/null
+++ b/std/str.zig
@@ -0,0 +1,34 @@
+const assert = @import("index.zig").assert;
+
+pub fn len(ptr: &const u8) -> isize {
+ var count: isize = 0;
+ while (ptr[count] != 0) {
+ count += 1;
+ }
+ return count;
+}
+
+pub fn from_c_const(str: &const u8) -> []const u8 {
+ return str[0...len(str)];
+}
+
+pub fn from_c(str: &u8) -> []u8 {
+ return str[0...len(str)];
+}
+
+pub const eql = slice_eql(u8);
+
+pub fn slice_eql(T: type)(a: []const T, b: []const T) -> bool {
+ if (a.len != b.len) return false;
+ for (a) |item, index| {
+ if (b[index] != item) return false;
+ }
+ return true;
+}
+
+#attribute("test")
+fn string_equality() {
+ assert(eql("abcd", "abcd"));
+ assert(!eql("abcdef", "abZdef"));
+ assert(!eql("abcdefg", "abcdef"));
+}