From 7ec19f2d2fa87255654d0239ec8d3e446c48797d Mon Sep 17 00:00:00 2001 From: Sebastian Date: Mon, 27 Nov 2023 01:18:15 -0500 Subject: [PATCH] bytes: simplify reverse Also updates the docstring to clarify that the slice is reversed in place. Signed-off-by: Sebastian --- bytes/reverse.ha | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/bytes/reverse.ha b/bytes/reverse.ha index bf5771f5..c4952cf0 100644 --- a/bytes/reverse.ha +++ b/bytes/reverse.ha @@ -1,17 +1,12 @@ // SPDX-License-Identifier: MPL-2.0 // (c) Hare authors -// 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; }; }; -- 2.45.2