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.
| @@ -629,36 +629,6 @@ | ||
| 629 | 629 | of the reply; a server that says nothing for a minute, as a model does |
| 630 | 630 | while it thinks, keeps the turn going for that minute. |
| 631 | 631 | |
| 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 | - | |
| 662 | 632 | ## The spelling of the option just read |
| 663 | 633 | |
| 664 | 634 | `Opt.Parser` keeps how the option just read was written (`spelt`, no |
| @@ -596,6 +596,7 @@ | ||
| 596 | 596 | proc str*(b: ^Buf; s: []U8); |
| 597 | 597 | proc spare*(b: ^Buf; n: INT): []U8; (* room for n more bytes, and those bytes, to fill *) |
| 598 | 598 | 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 *) | |
| 599 | 600 | proc reset*(b: ^Buf); (* emptied, its memory kept *) |
| 600 | 601 | proc done*(b: ^Buf); (* its memory given back *) |
| 601 | 602 | |
| @@ -678,7 +679,9 @@ | ||
| 678 | 679 | failed check does, if the allocator has no room; `room` asks first. A |
| 679 | 680 | program reads into a `Buf` through `spare`, then adds what it read to |
| 680 | 681 | `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 | |
| 682 | 685 | doubles when it is full, so `push` costs the same on average however |
| 683 | 686 | long it grows. `all` gives the elements as a slice, indexed and |
| 684 | 687 | checked as any is, and `v.len` is their number; the slice, and what |
| @@ -85,10 +85,23 @@ | ||
| 85 | 85 | return b^.data[0..b^.len]; |
| 86 | 86 | end; |
| 87 | 87 | |
| 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 | + | |
| 88 | 101 | (* Empties the buffer, keeping its memory for what comes next. *) |
| 89 | 102 | proc reset*(b: ^Buf); |
| 90 | 103 | begin |
| 91 | - b^.len := 0; | |
| 104 | + cut(b, 0); | |
| 92 | 105 | end; |
| 93 | 106 | |
| 94 | 107 | (* Gives the buffer's memory back, leaving it empty. *) |
| @@ -116,6 +129,10 @@ | ||
| 116 | 129 | reset(ADR(b)); |
| 117 | 130 | str(ADR(b), "again"); |
| 118 | 131 | 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; | |
| 119 | 136 | s := spare(ADR(b), 3); |
| 120 | 137 | COPY(s, "abc"); |
| 121 | 138 | INC(b.len, 2); |
An empty file.
| @@ -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. |
| @@ -0,0 +1 @@ | ||
| 1 | +2 |
| @@ -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 |