| | @@ -628,3 +628,31 @@ |
| 628 | 628 | thread, which can only set a flag that the turn reads after each read |
| 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 | + |
| 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. |