]> git.lizzy.rs Git - mt.git/blob - readrune.go
Add WaitGroup to SerializePkt
[mt.git] / readrune.go
1 /*
2 Based on go1.16/src/fmt/scan.go.
3
4 Copyright (c) 2009 The Go Authors. All rights reserved.
5
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions are
8 met:
9
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
15 distribution.
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.
19
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.
31 */
32
33 package mt
34
35 import (
36         "errors"
37         "io"
38         "unicode/utf8"
39 )
40
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 {
45         io.Reader
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)
50 }
51
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) {
55         if r.pending > 0 {
56                 b = r.pendBuf[0]
57                 copy(r.pendBuf[0:], r.pendBuf[1:])
58                 r.pending--
59                 return
60         }
61         n, err := io.ReadFull(r, r.pendBuf[:1])
62         if n != 1 {
63                 return 0, err
64         }
65         return r.pendBuf[0], err
66 }
67
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) {
71         if r.peekRune >= 0 {
72                 rr = r.peekRune
73                 r.peekRune = ^r.peekRune
74                 size = utf8.RuneLen(rr)
75                 return
76         }
77         r.buf[0], err = r.readByte()
78         if err != nil {
79                 return
80         }
81         if r.buf[0] < utf8.RuneSelf { // fast check for common ASCII case
82                 rr = rune(r.buf[0])
83                 size = 1 // Known to be 1.
84                 // Flip the bits of the rune so it's available to UnreadRune.
85                 r.peekRune = ^rr
86                 return
87         }
88         var n int
89         for n = 1; !utf8.FullRune(r.buf[:n]); n++ {
90                 r.buf[n], err = r.readByte()
91                 if err != nil {
92                         if err == io.EOF {
93                                 err = nil
94                                 break
95                         }
96                         return
97                 }
98         }
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
103         }
104         // Flip the bits of the rune so it's available to UnreadRune.
105         r.peekRune = ^rr
106         return
107 }
108
109 func (r *readRune) UnreadRune() error {
110         if r.peekRune >= 0 {
111                 return errors.New("fmt: scanning called UnreadRune with no rune available")
112         }
113         // Reverse bit flip of previously read rune to obtain valid >=0 state.
114         r.peekRune = ^r.peekRune
115         return nil
116 }