| 1 |
// SPDX-License-Identifier: MPL-2.0 |
| 2 |
// (c) Hare authors <https://harelang.org> |
| 3 |
|
| 4 |
use encoding::utf8; |
| 5 |
use sort::cmp; |
| 6 |
use types; |
| 7 |
|
| 8 |
let empty: [_]u8 = [0]; |
| 9 |
|
| 10 |
// An empty NUL-terminated C string. |
| 11 |
export let empty_string: *const char = &empty[0]: *const char; |
| 12 |
|
| 13 |
// Computes the length of a NUL-terminated C string, in octets, in O(n). The |
| 14 |
// computed length does not include the NUL terminator. |
| 15 |
export fn strlen(cstr: *const char) size = { |
| 16 |
const ptr = cstr: *[*]u8; |
| 17 |
let ln = 0z; |
| 18 |
for (ptr[ln] != 0; ln += 1) void; |
| 19 |
return ln; |
| 20 |
}; |
| 21 |
|
| 22 |
// Computes the length of a NUL-terminated C string, only looking at the first |
| 23 |
// maxlen bytes. The computed length does not include the NUL terminator. |
| 24 |
// |
| 25 |
// Note: although cstr's type is non-nullable, passing in a null pointer has |
| 26 |
// well-defined behavior if maxlen == 0. |
| 27 |
export fn strnlen(cstr: *const char, maxlen: size) size = { |
| 28 |
const ptr = cstr: *[*]u8; |
| 29 |
let ln = 0z; |
| 30 |
for (ln < maxlen && ptr[ln] != 0; ln += 1) void; |
| 31 |
return ln; |
| 32 |
}; |
| 33 |
|
| 34 |
// Compares two NUL-terminated C strings. Zero is returned if the strings are |
| 35 |
// equal, a negative value if a is less than b, or a positive value if a is |
| 36 |
// greater than b. |
| 37 |
export fn strcmp(a: *const char, b: *const char) int = { |
| 38 |
return cmp::cstrs(&a, &b); |
| 39 |
}; |
| 40 |
|
| 41 |
// Compares two NUL-terminated C strings, only looking at the first n bytes. |
| 42 |
// Zero is returned if the strings are equal, a negative value if a is less than |
| 43 |
// b, or a positive value if a is greater than b. |
| 44 |
// |
| 45 |
// Note: although the string parameters have non-nullable types, passing in a |
| 46 |
// null pointer for either of them has well-defined behavior if n == 0. |
| 47 |
export fn strncmp(a: *const char, b: *const char, n: size) int = { |
| 48 |
if (n == 0) { |
| 49 |
return 0; |
| 50 |
}; |
| 51 |
const a = a: *const [*]u8, b = b: *const [*]u8; |
| 52 |
let i = 0z; |
| 53 |
for (i < n - 1 && a[i] == b[i] && a[i] != '\0'; i += 1) void; |
| 54 |
return a[i]: int - b[i]: int; |
| 55 |
}; |
| 56 |
|
| 57 |
// Converts a C string to a Hare string in O(n), and does not check if it's |
| 58 |
// valid UTF-8. |
| 59 |
export fn tostr_unsafe(cstr: *const char) const str = { |
| 60 |
return tostrn_unsafe(cstr, strlen(cstr)); |
| 61 |
}; |
| 62 |
|
| 63 |
// Converts a C string with a given length to a Hare string, and does not check |
| 64 |
// if it's valid UTF-8. |
| 65 |
export fn tostrn_unsafe(cstr: *const char, length: size) const str = { |
| 66 |
const s = types::string { |
| 67 |
data = cstr: *[*]u8, |
| 68 |
length = length, |
| 69 |
capacity = length + 1, |
| 70 |
}; |
| 71 |
return *(&s: *const str); |
| 72 |
}; |
| 73 |
|
| 74 |
// Converts a C string to a Hare string in O(n). If the string is not valid |
| 75 |
// UTF-8, return [[encoding::utf8::invalid]]. |
| 76 |
export fn tostr(cstr: *const char) (const str | utf8::invalid) = { |
| 77 |
return tostrn(cstr, strlen(cstr)); |
| 78 |
}; |
| 79 |
|
| 80 |
// Converts a C string with a given length to a Hare string. If the string is |
| 81 |
// not valid UTF-8, return [[encoding::utf8::invalid]]. |
| 82 |
export fn tostrn(cstr: *const char, length: size) (const str | utf8::invalid) = { |
| 83 |
utf8::validate((cstr: *[*]u8)[..length])?; |
| 84 |
return tostrn_unsafe(cstr, length); |
| 85 |
}; |
| 86 |
|
| 87 |
// Converts a Hare string to a C string. The result is allocated; the caller |
| 88 |
// must free it when they're done. |
| 89 |
export fn fromstr(s: const str) (*char | nomem) = { |
| 90 |
let slice: []char = alloc([0...], len(s) + 1)?; |
| 91 |
return fromstr_buf(s, slice); |
| 92 |
}; |
| 93 |
|
| 94 |
// Converts a Hare string to a C string. The result is stored into a |
| 95 |
// user-supplied buffer. Returns nomem if the buffer does not provide enough |
| 96 |
// space. |
| 97 |
export fn fromstr_buf(s: const str, sl: []char) (*char | nomem) = { |
| 98 |
if (len(sl) < len(s) + 1) { |
| 99 |
return nomem; |
| 100 |
}; |
| 101 |
|
| 102 |
const s = &s: *[]char; |
| 103 |
sl[..len(s)] = s[..]; |
| 104 |
sl[len(s)] = 0; |
| 105 |
|
| 106 |
return (*(&sl: *types::slice)).data: *char; |
| 107 |
}; |
| 108 |
|
| 109 |
// Converts a NUL-terminated Hare string to a C string. Aborts if the input |
| 110 |
// string isn't NUL-terminated. The result is borrowed from the input. |
| 111 |
export fn nulstr(s: const str) *const char = { |
| 112 |
let s = &s: *types::string; |
| 113 |
let data = s.data as *[*]u8; |
| 114 |
assert(data[s.length - 1] == '\0', "types::c::nulstr input must be NUL-terminated"); |
| 115 |
return s.data: *const char; |
| 116 |
}; |
| 117 |
|
| 118 |
// Converts a non-NUL-terminated Hare string to a *const [[char]]. The return |
| 119 |
// value is borrowed from the input, except in the case of an empty string, in |
| 120 |
// which case it is statically allocated. |
| 121 |
// |
| 122 |
// Use with caution! |
| 123 |
export fn unterminatedstr(s: const str) *const char = { |
| 124 |
let s = &s: *types::string; |
| 125 |
if (s.data == null) { |
| 126 |
return empty_string; |
| 127 |
}; |
| 128 |
let data = s.data as *[*]u8; |
| 129 |
return data: *const char; |
| 130 |
}; |