hare

[hare] The Hare programming language
git clone https://git.torresjrjr.com/hare.git
Log | Files | Refs | README | LICENSE

commit 7842e453e4c965437ae9904db5de6db08206b66b
parent 348725230c67e9027e8bf0915930b17ec72aa0ba
Author: Sebastian <sebastian@sebsite.pw>
Date:   Sat,  7 Sep 2024 16:23:24 -0400

ascii: overwrite buf in str(upper|lower)_buf

Rather than appending to the end of it, overwrite its current contents.
This makes the following code work:

	let buf: [N]u8 = [0...];
	assert(len(s) <= N);
	strlower_buf(s, buf); // no need to explicitly slice buf

The documentation is also updated to more clearly communicate this
behavior.

Signed-off-by: Sebastian <sebastian@sebsite.pw>

Diffstat:
Mascii/string.ha | 14++++++++------
1 file changed, 8 insertions(+), 6 deletions(-)

diff --git a/ascii/string.ha b/ascii/string.ha @@ -15,10 +15,11 @@ export fn strlower(s: str) str = { // Converts all ASCII uppercase characters in a string to their lowercase // representation, returning a new string. The new string data is stored in the -// supplied buffer. The buffer is permitted to exactly overlap the string. This -// function will abort if the buffer's capacity is too small to fit the entire -// string. +// supplied buffer (overwriting any existing contents). The buffer is permitted +// to exactly overlap the string. This function will abort if the buffer's +// capacity is too small to fit the entire string. export fn strlower_buf(s: str, buf: []u8) str = { + let buf = buf[..0]; let it = strings::iter(s); for (let r => strings::next(&it)) { static append(buf, utf8::encoderune(tolower(r))...); @@ -36,10 +37,11 @@ export fn strupper(s: str) str = { // Converts all ASCII lowercase characters in a string to their uppercase // representation, returning a new string. The new string data is stored in the -// supplied buffer. The buffer is permitted to exactly overlap the string. This -// function will abort if the buffer's capacity is too small to fit the entire -// string. +// supplied buffer (overwriting any existing contents). The buffer is permitted +// to exactly overlap the string. This function will abort if the buffer's +// capacity is too small to fit the entire string. export fn strupper_buf(s: str, buf: []u8) str = { + let buf = buf[..0]; let it = strings::iter(s); for (let r => strings::next(&it)) { static append(buf, utf8::encoderune(toupper(r))...);