hare

[hare] The Hare programming language
git clone https://git.torresjrjr.com/hare.git
Log | Files | Refs | README | LICENSE

commit 0d0e4994dc06b85aea8c461624df8a912541e74c
parent a986adec473af9dd117676d15acf351d386017ee
Author: Armin Preiml <apreiml@strohwolke.at>
Date:   Thu, 28 Sep 2023 07:58:10 +0200

bufio::scanner: fix unread for scan_byte and scan_rune

Remove the defered shifts which took away the space required for unread.

Diffstat:
Mbufio/scanner.ha | 11+++--------
Mbufio/scanner_test+test.ha | 7+++++++
2 files changed, 10 insertions(+), 8 deletions(-)

diff --git a/bufio/scanner.ha b/bufio/scanner.ha @@ -145,9 +145,6 @@ export fn scan_byte(scan: *scanner) (u8 | io::EOF | io::error) = { // Consume previous read, if any scan_shift(scan); - // Consume this read right away - defer scan_shift(scan); - return scan_consume(scan, 1)[0]; }; @@ -196,6 +193,9 @@ export fn scan_bytes( export fn scan_rune( scan: *scanner, ) (rune | io::EOF | io::error | utf8::invalid) = { + // Consume previous read, if any + scan_shift(scan); + if (scan.pending == 0) { match (scan_readahead(scan)?) { case io::EOF => @@ -222,11 +222,6 @@ export fn scan_rune( }; }; - // Consume previous read, if any - scan_shift(scan); - // Consume this read right away - defer scan_shift(scan); - const buf = scan_consume(scan, sz); const dec = utf8::decode(buf[..sz]); match (utf8::next(&dec)?) { diff --git a/bufio/scanner_test+test.ha b/bufio/scanner_test+test.ha @@ -131,6 +131,13 @@ use strings; let scanner = newscanner(&in, 32); defer finish(&scanner); + + let b = scan_byte(&scanner) as u8; + unread(&scanner, [b]); + + let b = scan_rune(&scanner) as rune; + unread(&scanner, utf8::encoderune(b)); + let l = scan_line(&scanner)! as const str; assert(l == " I will not repeat ");