aboutsummaryrefslogtreecommitdiff
path: root/std
diff options
context:
space:
mode:
authorAndrew Kelley <superjoe30@gmail.com>2016-09-26 22:33:33 -0400
committerAndrew Kelley <superjoe30@gmail.com>2016-09-26 22:33:33 -0400
commit87b7c28c9aa618eda5589693ee79a5737d907bac (patch)
treee152ba0b645aaac3f541973781b5c5c5baae111c /std
parent7ce7e2c9d1d43886c93f34b67d32ef77cc0d8a6e (diff)
downloadzig-87b7c28c9aa618eda5589693ee79a5737d907bac.tar.gz
zig-87b7c28c9aa618eda5589693ee79a5737d907bac.zip
cstr.len and cstr.cmp can run at compile time
closes #140
Diffstat (limited to 'std')
-rw-r--r--std/cstr.zig26
1 files changed, 18 insertions, 8 deletions
diff --git a/std/cstr.zig b/std/cstr.zig
index d8f797a388..e5035aec4b 100644
--- a/std/cstr.zig
+++ b/std/cstr.zig
@@ -6,22 +6,22 @@ const assert = debug.assert;
const strlen = len;
-// TODO fix https://github.com/andrewrk/zig/issues/140
-// and then make this able to run at compile time
-#static_eval_enable(false)
pub fn len(ptr: &const u8) -> usize {
var count: usize = 0;
while (ptr[count] != 0; count += 1) {}
return count;
}
-// TODO fix https://github.com/andrewrk/zig/issues/140
-// and then make this able to run at compile time
-#static_eval_enable(false)
-pub fn cmp(a: &const u8, b: &const u8) -> i32 {
+pub fn cmp(a: &const u8, b: &const u8) -> i8 {
var index: usize = 0;
while (a[index] == b[index] && a[index] != 0; index += 1) {}
- return a[index] - b[index];
+ return if (a[index] > b[index]) {
+ 1
+ } else if (a[index] < b[index]) {
+ -1
+ } else {
+ 0
+ };
}
pub fn toSliceConst(str: &const u8) -> []const u8 {
@@ -145,3 +145,13 @@ fn testSimpleCBuf() {
%%buf2.resize(4);
assert(buf.startsWithCBuf(&buf2));
}
+
+#attribute("test")
+fn testCompileTimeStrCmp() {
+ assert(@constEval(cmp(c"aoeu", c"aoez") == -1));
+}
+
+#attribute("test")
+fn testCompileTimeStrLen() {
+ assert(@constEval(len(c"123456789") == 9));
+}