reverse.ha (578B)
1 // SPDX-License-Identifier: MPL-2.0 2 // (c) Hare authors <https://harelang.org> 3 4 // Reverses a slice of bytes in place. 5 export fn reverse(b: []u8) void = { 6 for (let i = 0z; i < len(b) / 2; i += 1) { 7 let x = b[i]; 8 b[i] = b[len(b) - i - 1]; 9 b[len(b) - i - 1] = x; 10 }; 11 }; 12 13 @test fn reverse() void = { 14 let b: [4]u8 = [1, 3, 3, 7]; 15 reverse(b); 16 assert(equal(b, [7, 3, 3, 1])); 17 let b: [5]u8 = [1, 2, 3, 4, 5]; 18 reverse(b); 19 assert(equal(b, [5, 4, 3, 2, 1])); 20 let b: [1]u8 = [1]; 21 reverse(b); 22 assert(equal(b, [1])); 23 let b: []u8 = []; 24 reverse(b); 25 assert(equal(b, [])); 26 };