omega-mv
Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.
Brought by drakestapleton
omega-mv / tests / compiler / slices / find_byte.osc
| 1 | // slice-entry: find count_eq last_index |
| 2 | // Search a caller buffer: the loop index stays below b.len, so the access never traps; a miss returns the length. |
| 3 | // slice-run: find bytes:0102030405 3 -> 2 |
| 4 | // slice-run: find bytes:0102030405 9 -> 5 |
| 5 | // slice-run: find bytes: 1 -> 0 |
| 6 | // slice-run: find bytes:NULL 1 -> 0 |
| 7 | // slice-run: count_eq bytes:0701070107 7 -> 3 |
| 8 | // slice-run: last_index bytes:0102 2 -> 0 |
| 9 | // slice-run: last_index bytes: 2 -> trap=BOUNDS |
| 10 | fn find(b: bytes, want: u64) -> u64 { |
| 11 | let mut i: u64 = 0; |
| 12 | while i < b.len bound 4096 { |
| 13 | if b[i] == want { |
| 14 | return i; |
| 15 | } |
| 16 | i = i + 1; |
| 17 | } |
| 18 | return b.len; |
| 19 | } |
| 20 | |
| 21 | fn count_eq(b: bytes, want: u64) -> u64 { |
| 22 | let mut n: u64 = 0; |
| 23 | let mut i: u64 = 0; |
| 24 | while i < b.len bound 4096 { |
| 25 | if b[i] == want { |
| 26 | n = n + 1; |
| 27 | } |
| 28 | i = i + 1; |
| 29 | } |
| 30 | return n; |
| 31 | } |
| 32 | |
| 33 | // the element at b.len - 1: an empty slice makes the index wrap and traps BOUNDS |
| 34 | fn last_index(b: bytes, want: u64) -> u64 { |
| 35 | let k: u64 = b.len - 1; |
| 36 | if b[k] == want { |
| 37 | return 0; |
| 38 | } |
| 39 | return 1; |
| 40 | } |