string comparisons

This commit is contained in:
gingerBill
2016-08-15 13:46:01 +01:00
parent 0f48a7d299
commit 3ed75b22a3
14 changed files with 1176 additions and 195 deletions
+52 -4
View File
@@ -1,7 +1,10 @@
// C Runtime procedures
putchar :: proc(c: i32) -> i32 #foreign
malloc :: proc(sz: int) -> rawptr #foreign;
free :: proc(ptr: rawptr) #foreign;
// CRT
putchar :: proc(c: i32) -> i32 #foreign
heap_alloc :: proc(sz: int) -> rawptr #foreign "malloc"
heap_free :: proc(ptr: rawptr) #foreign "free"
mem_compare :: proc(dst, src : rawptr, len: int) -> i32 #foreign "memcmp"
mem_copy :: proc(dst, src : rawptr, len: int) -> i32 #foreign "memcpy"
mem_move :: proc(dst, src : rawptr, len: int) -> i32 #foreign "memmove"
print_string :: proc(s: string) {
@@ -129,3 +132,48 @@ print_bool :: proc(b : bool) {
print_string("false");
}
}
// Runtime procedures
__string_eq :: proc(a, b : string) -> bool {
if len(a) != len(b) {
return false;
}
if ^a[0] == ^b[0] {
return true;
}
return mem_compare(^a[0], ^b[0], len(a)) == 0;
}
__string_ne :: proc(a, b : string) -> bool {
return !__string_eq(a, b);
}
__string_cmp :: proc(a, b : string) -> int {
min_len := len(a);
if len(b) < min_len {
min_len = len(b);
}
for i := 0; i < min_len; i++ {
x := a[i];
y := b[i];
if x < y {
return -1;
} else if x > y {
return +1;
}
}
if len(a) < len(b) {
return -1;
} else if len(a) > len(b) {
return +1;
}
return 0;
}
__string_lt :: proc(a, b : string) -> bool { return __string_cmp(a, b) < 0; }
__string_gt :: proc(a, b : string) -> bool { return __string_cmp(a, b) > 0; }
__string_le :: proc(a, b : string) -> bool { return __string_cmp(a, b) <= 0; }
__string_ge :: proc(a, b : string) -> bool { return __string_cmp(a, b) >= 0; }