hare

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

commit 7ec19f2d2fa87255654d0239ec8d3e446c48797d
parent 737cd29e7b354d35db3a5a02cfd32521e7dcef2f
Author: Sebastian <sebastian@sebsite.pw>
Date:   Mon, 27 Nov 2023 01:18:15 -0500

bytes: simplify reverse

Also updates the docstring to clarify that the slice is reversed in
place.

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

Diffstat:
Mbytes/reverse.ha | 15+++++----------
1 file changed, 5 insertions(+), 10 deletions(-)

diff --git a/bytes/reverse.ha b/bytes/reverse.ha @@ -1,17 +1,12 @@ // SPDX-License-Identifier: MPL-2.0 // (c) Hare authors <https://harelang.org> -// Reverses a slice of bytes. +// Reverses a slice of bytes in place. export fn reverse(b: []u8) void = { - if (len(b) == 0) { - return; - }; - for (let s = 0z, e = len(b) - 1; s < e) { - let x = b[s]; - b[s] = b[e]; - b[e] = x; - s += 1; - e -= 1; + for (let i = 0z; i < len(b) / 2; i += 1) { + let x = b[i]; + b[i] = b[len(b) - i - 1]; + b[len(b) - i - 1] = x; }; };