From 0bc2d2a8cb2e80af07a56413e30c43d4349c6cb3 Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Fri, 16 Oct 2026 20:13:37 +0000 Subject: [PATCH] Buf.cut: only the first n bytes kept As Vec.resize cuts an array short, keeping its memory; reset is cut(b, 0). A length past len stops the program, with the calls under way, as Buf does when it has no room. Met in rrem's replace and rre's reading window; its proposal is removed. --- diff --git a/PROPOSALS.md b/PROPOSALS.md index 8541b66..7b21f21 100644 --- a/PROPOSALS.md +++ b/PROPOSALS.md @@ -629,36 +629,6 @@ of the reply; a server that says nothing for a minute, as a model does while it thinks, keeps the turn going for that minute. -## `Buf.cut` - -`Buf` can grow and be emptied, but not cut back to an earlier length. -A program that adds to a buffer and must take back what it added, on -an error, writes the field itself, - -``` -start := b^.len; -... -b^.len := start; -``` - -which works, `len` being exported, but the library doesn't say that a -program may lower it; it says only that a program raises it after -`spare` (`INC(b^.len, n)`). The proposal, in `Buf`, beside `reset`: - -``` -proc cut*(b: ^Buf; n: INT); (* only the first n bytes kept, its memory kept *) -``` - -as `Vec.resize` does for an array; `reset` is `cut(b, 0)`. An `n` -past `len` is a defect, as an index past the end is. It breaks no -program, so it could come with any release, not only a thaw. - -Met in rrem's `replace`, which adds each match's replacement to the -caller's buffer and, when a search stops with `TooCostly` or `NoRoom`, -leaves the buffer as it was. -Met again in rre's reading window, which drops the lines it has -searched by shortening the buffer. - ## The spelling of the option just read `Opt.Parser` keeps how the option just read was written (`spelt`, no diff --git a/doc/raz-lib.md b/doc/raz-lib.md index 0fac695..f00b066 100644 --- a/doc/raz-lib.md +++ b/doc/raz-lib.md @@ -596,6 +596,7 @@ proc str*(b: ^Buf; s: []U8); proc spare*(b: ^Buf; n: INT): []U8; (* room for n more bytes, and those bytes, to fill *) proc bytes*(b: ^Buf): []U8; (* what it holds, until it next grows *) +proc cut*(b: ^Buf; n: INT); (* only the first n bytes kept, its memory kept *) proc reset*(b: ^Buf); (* emptied, its memory kept *) proc done*(b: ^Buf); (* its memory given back *) @@ -678,7 +679,9 @@ failed check does, if the allocator has no room; `room` asks first. A program reads into a `Buf` through `spare`, then adds what it read to `len`: `s := Buf.spare(b, 4096); n := Sys.read(fd, s); if n > 0 then -INC(b^.len, n); end;`. A `Vec` is `Buf` for any `T`: one block, which +INC(b^.len, n); end;`. `cut` takes back what was added, to a length it +had: `start := b^.len; ...; Buf.cut(b, start);`; a length past `len` +stops the program, as an index past the end does. A `Vec` is `Buf` for any `T`: one block, which doubles when it is full, so `push` costs the same on average however long it grows. `all` gives the elements as a slice, indexed and checked as any is, and `v.len` is their number; the slice, and what diff --git a/lib/Buf.raz b/lib/Buf.raz index bef7023..e226ad8 100644 --- a/lib/Buf.raz +++ b/lib/Buf.raz @@ -85,10 +85,23 @@ return b^.data[0..b^.len]; end; +(* Keeps only the first n bytes, and the memory, for what comes next. + An n past what the buffer holds is the program's mistake, and stops + it, as an index past the end does. *) +proc cut*(b: ^Buf; n: INT); +begin + if (n < 0) or (n > b^.len) then + _ := Sys.write(Sys.stderr, "Buf.cut past the end\n"); + Rt.trace; + Rt.exit(2); + end; + b^.len := n; +end; + (* Empties the buffer, keeping its memory for what comes next. *) proc reset*(b: ^Buf); begin - b^.len := 0; + cut(b, 0); end; (* Gives the buffer's memory back, leaving it empty. *) @@ -116,6 +129,10 @@ reset(ADR(b)); str(ADR(b), "again"); if bytes(ADR(b)) # "again" then Rt.stop("Buf.reset"); end; + cut(ADR(b), 2); + str(ADR(b), "ain"); + cut(ADR(b), 5); + if bytes(ADR(b)) # "again" then Rt.stop("Buf.cut"); end; s := spare(ADR(b), 3); COPY(s, "abc"); INC(b.len, 2); diff --git a/test/run/BufCut.expect b/test/run/BufCut.expect new file mode 100644 index 0000000..e69de29 diff --git a/test/run/BufCut.raz b/test/run/BufCut.raz new file mode 100644 index 0000000..f42c7e3 --- /dev/null +++ b/test/run/BufCut.raz @@ -0,0 +1,14 @@ +module BufCut; +(* Buf.cut past what the buffer holds says so, with the calls under + way, and stops the program with status 2 *) +import Buf, Heap; + +proc main*; +var b: Buf.Buf; +begin + Buf.init(ADR(b), Heap.heap); + Buf.str(ADR(b), "hello"); + Buf.cut(ADR(b), 6); +end; + +end BufCut. diff --git a/test/run/BufCut.status b/test/run/BufCut.status new file mode 100644 index 0000000..0cfbf08 --- /dev/null +++ b/test/run/BufCut.status @@ -0,0 +1 @@ +2 diff --git a/test/run/BufCut.stderr b/test/run/BufCut.stderr new file mode 100644 index 0000000..756983b --- /dev/null +++ b/test/run/BufCut.stderr @@ -0,0 +1,3 @@ +Buf.cut past the end + in Buf.cut at lib/Buf.raz:95:9: b = 0x?, n = 6 + in BufCut.main at test/run/BufCut.raz:11:5