| 1 |
// SPDX-License-Identifier: MPL-2.0 |
| 2 |
// (c) Hare authors <https://harelang.org> |
| 3 |
|
| 4 |
// [[sort::cmpfunc]] for use with int. |
| 5 |
export fn ints(a: const *opaque, b: const *opaque) int = { |
| 6 |
const a = *(a: const *int), b = *(b: const *int); |
| 7 |
return if (a < b) -1 |
| 8 |
else if (a > b) 1 |
| 9 |
else 0; |
| 10 |
}; |
| 11 |
|
| 12 |
// [[sort::cmpfunc]] for use with uint. |
| 13 |
export fn uints(a: const *opaque, b: const *opaque) int = { |
| 14 |
const a = *(a: const *uint), b = *(b: const *uint); |
| 15 |
return if (a < b) -1 |
| 16 |
else if (a > b) 1 |
| 17 |
else 0; |
| 18 |
}; |
| 19 |
|
| 20 |
// [[sort::cmpfunc]] for use with i8. |
| 21 |
export fn i8s(a: const *opaque, b: const *opaque) int = { |
| 22 |
const a = *(a: const *i8): int, b = *(b: const *i8): int; |
| 23 |
return a - b; |
| 24 |
}; |
| 25 |
|
| 26 |
// [[sort::cmpfunc]] for use with u8. |
| 27 |
export fn u8s(a: const *opaque, b: const *opaque) int = { |
| 28 |
const a = *(a: const *u8): int, b = *(b: const *u8): int; |
| 29 |
return a - b; |
| 30 |
}; |
| 31 |
|
| 32 |
// [[sort::cmpfunc]] for use with i16. |
| 33 |
export fn i16s(a: const *opaque, b: const *opaque) int = { |
| 34 |
const a = *(a: const *i16): int, b = *(b: const *i16): int; |
| 35 |
return a - b; |
| 36 |
}; |
| 37 |
|
| 38 |
// [[sort::cmpfunc]] for use with u16. |
| 39 |
export fn u16s(a: const *opaque, b: const *opaque) int = { |
| 40 |
const a = *(a: const *u16): int, b = *(b: const *u16): int; |
| 41 |
return a - b; |
| 42 |
}; |
| 43 |
|
| 44 |
// [[sort::cmpfunc]] for use with i32. |
| 45 |
export fn i32s(a: const *opaque, b: const *opaque) int = { |
| 46 |
const a = *(a: const *i32), b = *(b: const *i32); |
| 47 |
return if (a < b) -1 |
| 48 |
else if (a > b) 1 |
| 49 |
else 0; |
| 50 |
}; |
| 51 |
|
| 52 |
// [[sort::cmpfunc]] for use with u32. |
| 53 |
export fn u32s(a: const *opaque, b: const *opaque) int = { |
| 54 |
const a = *(a: const *u32), b = *(b: const *u32); |
| 55 |
return if (a < b) -1 |
| 56 |
else if (a > b) 1 |
| 57 |
else 0; |
| 58 |
}; |
| 59 |
|
| 60 |
// [[sort::cmpfunc]] for use with i64. |
| 61 |
export fn i64s(a: const *opaque, b: const *opaque) int = { |
| 62 |
const a = *(a: const *i64), b = *(b: const *i64); |
| 63 |
return if (a < b) -1 |
| 64 |
else if (a > b) 1 |
| 65 |
else 0; |
| 66 |
}; |
| 67 |
|
| 68 |
// [[sort::cmpfunc]] for use with u64. |
| 69 |
export fn u64s(a: const *opaque, b: const *opaque) int = { |
| 70 |
const a = *(a: const *u64), b = *(b: const *u64); |
| 71 |
return if (a < b) -1 |
| 72 |
else if (a > b) 1 |
| 73 |
else 0; |
| 74 |
}; |
| 75 |
|
| 76 |
// [[sort::cmpfunc]] for use with size. |
| 77 |
export fn sizes(a: const *opaque, b: const *opaque) int = { |
| 78 |
const a = *(a: const *size), b = *(b: const *size); |
| 79 |
return if (a < b) -1 |
| 80 |
else if (a > b) 1 |
| 81 |
else 0; |
| 82 |
}; |
| 83 |
|
| 84 |
// [[sort::cmpfunc]] for use with str. Sorting is done with respect to Unicode |
| 85 |
// codepoints; see [[strings::compare]]. |
| 86 |
export fn strs(a: const *opaque, b: const *opaque) int = { |
| 87 |
// Manual strings::toutf8() to avoid dependency on strings |
| 88 |
const a = *(a: *[]u8), b = *(b: *[]u8); |
| 89 |
|
| 90 |
let ln = if (len(a) < len(b)) (len(a), -1) else (len(b), 1); |
| 91 |
for (let i = 0z; i < ln.0; i += 1) { |
| 92 |
if (a[i] != b[i]) { |
| 93 |
return a[i]: int - b[i]: int; |
| 94 |
}; |
| 95 |
}; |
| 96 |
return if (len(a) == len(b)) 0 else ln.1; |
| 97 |
}; |
| 98 |
|
| 99 |
// [[sort::cmpfunc]] for use with NUL-terminated C strings |
| 100 |
// (*const [[types::c::char]]). Sorting is done in the same manner as in |
| 101 |
// [[types::c::strcmp]]. |
| 102 |
export fn cstrs(a: const *opaque, b: const *opaque) int = { |
| 103 |
const a = *(a: const **[*]u8), b = *(b: const **[*]u8); |
| 104 |
let i = 0z; |
| 105 |
for (a[i] == b[i] && a[i] != '\0'; i += 1) void; |
| 106 |
return a[i]: int - b[i]: int; |
| 107 |
}; |