raz / commit 0bc2d2a8

commit
0bc2d2a8cb2e80af07a56413e30c43d4349c6cb3
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
1af2d2ec
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.

patch · log

7 files changed, +40 -32

PROPOSALS.md modified +0 -30

@@ -629,36 +629,6 @@
629629 of the reply; a server that says nothing for a minute, as a model does
630630 while it thinks, keeps the turn going for that minute.
631631
632-## `Buf.cut`
633-
634-`Buf` can grow and be emptied, but not cut back to an earlier length.
635-A program that adds to a buffer and must take back what it added, on
636-an error, writes the field itself,
637-
638-```
639-start := b^.len;
640-...
641-b^.len := start;
642-```
643-
644-which works, `len` being exported, but the library doesn't say that a
645-program may lower it; it says only that a program raises it after
646-`spare` (`INC(b^.len, n)`). The proposal, in `Buf`, beside `reset`:
647-
648-```
649-proc cut*(b: ^Buf; n: INT); (* only the first n bytes kept, its memory kept *)
650-```
651-
652-as `Vec.resize` does for an array; `reset` is `cut(b, 0)`. An `n`
653-past `len` is a defect, as an index past the end is. It breaks no
654-program, so it could come with any release, not only a thaw.
655-
656-Met in rrem's `replace`, which adds each match's replacement to the
657-caller's buffer and, when a search stops with `TooCostly` or `NoRoom`,
658-leaves the buffer as it was.
659-Met again in rre's reading window, which drops the lines it has
660-searched by shortening the buffer.
661-
662632 ## The spelling of the option just read
663633
664634 `Opt.Parser` keeps how the option just read was written (`spelt`, no

doc/raz-lib.md (history) modified +4 -1

@@ -596,6 +596,7 @@
596596 proc str*(b: ^Buf; s: []U8);
597597 proc spare*(b: ^Buf; n: INT): []U8; (* room for n more bytes, and those bytes, to fill *)
598598 proc bytes*(b: ^Buf): []U8; (* what it holds, until it next grows *)
599+proc cut*(b: ^Buf; n: INT); (* only the first n bytes kept, its memory kept *)
599600 proc reset*(b: ^Buf); (* emptied, its memory kept *)
600601 proc done*(b: ^Buf); (* its memory given back *)
601602
@@ -678,7 +679,9 @@
678679 failed check does, if the allocator has no room; `room` asks first. A
679680 program reads into a `Buf` through `spare`, then adds what it read to
680681 `len`: `s := Buf.spare(b, 4096); n := Sys.read(fd, s); if n > 0 then
681-INC(b^.len, n); end;`. A `Vec` is `Buf` for any `T`: one block, which
682+INC(b^.len, n); end;`. `cut` takes back what was added, to a length it
683+had: `start := b^.len; ...; Buf.cut(b, start);`; a length past `len`
684+stops the program, as an index past the end does. A `Vec` is `Buf` for any `T`: one block, which
682685 doubles when it is full, so `push` costs the same on average however
683686 long it grows. `all` gives the elements as a slice, indexed and
684687 checked as any is, and `v.len` is their number; the slice, and what

lib/Buf.raz (history) modified +18 -1

@@ -85,10 +85,23 @@
8585 return b^.data[0..b^.len];
8686 end;
8787
88+(* Keeps only the first n bytes, and the memory, for what comes next.
89+ An n past what the buffer holds is the program's mistake, and stops
90+ it, as an index past the end does. *)
91+proc cut*(b: ^Buf; n: INT);
92+begin
93+ if (n < 0) or (n > b^.len) then
94+ _ := Sys.write(Sys.stderr, "Buf.cut past the end\n");
95+ Rt.trace;
96+ Rt.exit(2);
97+ end;
98+ b^.len := n;
99+end;
100+
88101 (* Empties the buffer, keeping its memory for what comes next. *)
89102 proc reset*(b: ^Buf);
90103 begin
91- b^.len := 0;
104+ cut(b, 0);
92105 end;
93106
94107 (* Gives the buffer's memory back, leaving it empty. *)
@@ -116,6 +129,10 @@
116129 reset(ADR(b));
117130 str(ADR(b), "again");
118131 if bytes(ADR(b)) # "again" then Rt.stop("Buf.reset"); end;
132+ cut(ADR(b), 2);
133+ str(ADR(b), "ain");
134+ cut(ADR(b), 5);
135+ if bytes(ADR(b)) # "again" then Rt.stop("Buf.cut"); end;
119136 s := spare(ADR(b), 3);
120137 COPY(s, "abc");
121138 INC(b.len, 2);

test/run/BufCut.expect (history) added +0 -0

An empty file.

test/run/BufCut.raz (history) added +14 -0

@@ -0,0 +1,14 @@
1+module BufCut;
2+(* Buf.cut past what the buffer holds says so, with the calls under
3+ way, and stops the program with status 2 *)
4+import Buf, Heap;
5+
6+proc main*;
7+var b: Buf.Buf;
8+begin
9+ Buf.init(ADR(b), Heap.heap);
10+ Buf.str(ADR(b), "hello");
11+ Buf.cut(ADR(b), 6);
12+end;
13+
14+end BufCut.

test/run/BufCut.status (history) added +1 -0

@@ -0,0 +1 @@
1+2

test/run/BufCut.stderr (history) added +3 -0

@@ -0,0 +1,3 @@
1+Buf.cut past the end
2+ in Buf.cut at lib/Buf.raz:95:9: b = 0x?, n = 6
3+ in BufCut.main at test/run/BufCut.raz:11:5