bytes: make search linear and splits consistent

This commit is contained in:
2026-08-09 17:43:31 +09:00
parent 8620e64313
commit 2d947c469e
4 changed files with 178 additions and 37 deletions

View File

@@ -4,6 +4,7 @@ package bytes_test;
import bytes;
import os;
import test;
// ref/hare/bytes/tokenize.ha:258. Hare's @test fn tokenize / rtokenize
// drives the iterator through an expected-token sequence and asserts
@@ -286,6 +287,15 @@ fn expect_tok(toks: [][]u8, i: i32, want: []u8) void = {
expect_tok(t6, 1, d[3:5]);
expect_tok(t6, 2, d[6:8]);
os.free(t6.ptr: *void, (t6.cap: u64) * 24u64);
let t7: [][]u8 = bytes.splitn(b[0:5], zd[0:1], 0);
assert(!(t7.len != 0 || t7.cap != 0));
};
@test fn splitn_negative_aborts() void = {
test.expectabort();
let in: [1]u8; let delim: [1]u8; delim[0] = 1u8;
bytes.splitn(in[0:1], delim[0:1], -1);
};
@test fn rsplitn_cases() void = {
@@ -307,17 +317,15 @@ fn expect_tok(toks: [][]u8, i: i32, want: []u8) void = {
expect_tok(t1, 3, a[18:22]);
os.free(t1.ptr: *void, (t1.cap: u64) * 24u64);
// n > token count — done short-circuit returns the toks in
// reverse-iteration order (last token first). Mirrors Hare's
// behavior at ref/hare/bytes/tokenize.ha:196-199 where the
// reverse-step is gated behind the n-1 loop completion.
// n > token count still returns input order. The result order must not
// change merely because the limit exceeds the number of tokens.
let b: [5]u8; b[0] = 1u8; b[1] = 0u8; b[2] = 2u8; b[3] = 0u8; b[4] = 3u8;
let zd: [1]u8; zd[0] = 0u8;
let t2: [][]u8 = bytes.rsplitn(b[0:5], zd[0:1], 10);
assert(!(t2.len != 3));
expect_tok(t2, 0, b[4:5]);
expect_tok(t2, 0, b[0:1]);
expect_tok(t2, 1, b[2:3]);
expect_tok(t2, 2, b[0:1]);
expect_tok(t2, 2, b[4:5]);
os.free(t2.ptr: *void, (t2.cap: u64) * 24u64);
// n == 1 — single slot holding the whole input as remainder.
@@ -336,6 +344,15 @@ fn expect_tok(toks: [][]u8, i: i32, want: []u8) void = {
assert(!(t4.len != 1));
expect_tok(t4, 0, c[0:3]);
os.free(t4.ptr: *void, (t4.cap: u64) * 24u64);
let t5: [][]u8 = bytes.rsplitn(b[0:5], zd[0:1], 0);
assert(!(t5.len != 0 || t5.cap != 0));
};
@test fn rsplitn_negative_aborts() void = {
test.expectabort();
let in: [1]u8; let delim: [1]u8; delim[0] = 1u8;
bytes.rsplitn(in[0:1], delim[0:1], -1);
};
@test fn split_cases() void = {