2 Based on go1.16/src/fmt/scan.go.
4 Copyright (c) 2009 The Go Authors. All rights reserved.
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions are
10 * Redistributions of source code must retain the above copyright
11 notice, this list of conditions and the following disclaimer.
12 * Redistributions in binary form must reproduce the above
13 copyright notice, this list of conditions and the following disclaimer
14 in the documentation and/or other materials provided with the
16 * Neither the name of Google Inc. nor the names of its
17 contributors may be used to endorse or promote products derived from
18 this software without specific prior written permission.
20 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41 // readRune is a structure to enable reading UTF-8 encoded code points
42 // from an io.Reader. It is used if the Reader given to the scanner does
43 // not already implement io.RuneScanner.
44 type readRune struct {
46 buf [utf8.UTFMax]byte // used only inside ReadRune
47 pending int // number of bytes in pendBuf; only >0 for bad UTF-8
48 pendBuf [utf8.UTFMax]byte // bytes left over
49 peekRune rune // if >=0 next rune; when <0 is ^(previous Rune)
52 // readByte returns the next byte from the input, which may be
53 // left over from a previous read if the UTF-8 was ill-formed.
54 func (r *readRune) readByte() (b byte, err error) {
57 copy(r.pendBuf[0:], r.pendBuf[1:])
61 n, err := io.ReadFull(r, r.pendBuf[:1])
65 return r.pendBuf[0], err
68 // ReadRune returns the next UTF-8 encoded code point from the
69 // io.Reader inside r.
70 func (r *readRune) ReadRune() (rr rune, size int, err error) {
73 r.peekRune = ^r.peekRune
74 size = utf8.RuneLen(rr)
77 r.buf[0], err = r.readByte()
81 if r.buf[0] < utf8.RuneSelf { // fast check for common ASCII case
83 size = 1 // Known to be 1.
84 // Flip the bits of the rune so it's available to UnreadRune.
89 for n = 1; !utf8.FullRune(r.buf[:n]); n++ {
90 r.buf[n], err = r.readByte()
99 rr, size = utf8.DecodeRune(r.buf[:n])
100 if size < n { // an error, save the bytes for the next read
101 copy(r.pendBuf[r.pending:], r.buf[size:n])
102 r.pending += n - size
104 // Flip the bits of the rune so it's available to UnreadRune.
109 func (r *readRune) UnreadRune() error {
111 return errors.New("fmt: scanning called UnreadRune with no rune available")
113 // Reverse bit flip of previously read rune to obtain valid >=0 state.
114 r.peekRune = ^r.peekRune