diff options
| author | gingerBill <ginger.bill.22@gmail.com> | 2016-08-15 13:46:01 +0100 |
|---|---|---|
| committer | gingerBill <ginger.bill.22@gmail.com> | 2016-08-15 13:46:01 +0100 |
| commit | 3ed75b22a357292393618fc684b18a1d167f4eb7 (patch) | |
| tree | 9233d60f2a870416f09a833ecd31956f375120da /examples/basic.odin | |
| parent | 0f48a7d299a80c2e461bdcf5b37b5f624a48d7e8 (diff) | |
string comparisons
Diffstat (limited to 'examples/basic.odin')
| -rw-r--r-- | examples/basic.odin | 56 |
1 files changed, 52 insertions, 4 deletions
diff --git a/examples/basic.odin b/examples/basic.odin index 60a13cce2..8177ca4da 100644 --- a/examples/basic.odin +++ b/examples/basic.odin @@ -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; } |