Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(3)

Delta Between Two Patch Sets: src/pkg/bytes/reader.go

Issue 77580046: code review 77580046: strings: fix Reader.UnreadRune (Closed)
Left Patch Set: diff -r 1ddce2d9ee32 https://code.google.com/p/go Created 11 years ago
Right Patch Set: diff -r 1ddce2d9ee32 https://code.google.com/p/go Created 11 years ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Right: Side by side diff | Download
« no previous file with change/comment | « no previous file | src/pkg/bytes/reader_test.go » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
(no file at all)
1 // Copyright 2012 The Go Authors. All rights reserved. 1 // Copyright 2012 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style 2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file. 3 // license that can be found in the LICENSE file.
4 4
5 package bytes 5 package bytes
6 6
7 import ( 7 import (
8 "errors" 8 "errors"
9 "io" 9 "io"
10 "unicode/utf8" 10 "unicode/utf8"
(...skipping 12 matching lines...) Expand all
23 // Len returns the number of bytes of the unread portion of the 23 // Len returns the number of bytes of the unread portion of the
24 // slice. 24 // slice.
25 func (r *Reader) Len() int { 25 func (r *Reader) Len() int {
26 if r.i >= len(r.s) { 26 if r.i >= len(r.s) {
27 return 0 27 return 0
28 } 28 }
29 return len(r.s) - r.i 29 return len(r.s) - r.i
30 } 30 }
31 31
32 func (r *Reader) Read(b []byte) (n int, err error) { 32 func (r *Reader) Read(b []byte) (n int, err error) {
33 r.prevRune = -1
33 if len(b) == 0 { 34 if len(b) == 0 {
34 return 0, nil 35 return 0, nil
35 } 36 }
36 if r.i >= len(r.s) { 37 if r.i >= len(r.s) {
37 return 0, io.EOF 38 return 0, io.EOF
38 } 39 }
39 n = copy(b, r.s[r.i:]) 40 n = copy(b, r.s[r.i:])
40 r.i += n 41 r.i += n
41 r.prevRune = -1
42 return 42 return
43 } 43 }
44 44
45 func (r *Reader) ReadAt(b []byte, off int64) (n int, err error) { 45 func (r *Reader) ReadAt(b []byte, off int64) (n int, err error) {
46 r.prevRune = -1
46 if off < 0 { 47 if off < 0 {
47 return 0, errors.New("bytes: invalid offset") 48 return 0, errors.New("bytes: invalid offset")
48 } 49 }
49 if off >= int64(len(r.s)) { 50 if off >= int64(len(r.s)) {
50 return 0, io.EOF 51 return 0, io.EOF
51 } 52 }
52 n = copy(b, r.s[int(off):]) 53 n = copy(b, r.s[int(off):])
53 if n < len(b) { 54 if n < len(b) {
54 err = io.EOF 55 err = io.EOF
55 } 56 }
56 return 57 return
57 } 58 }
58 59
59 func (r *Reader) ReadByte() (b byte, err error) { 60 func (r *Reader) ReadByte() (b byte, err error) {
61 r.prevRune = -1
60 if r.i >= len(r.s) { 62 if r.i >= len(r.s) {
61 return 0, io.EOF 63 return 0, io.EOF
62 } 64 }
63 b = r.s[r.i] 65 b = r.s[r.i]
64 r.i++ 66 r.i++
65 r.prevRune = -1
66 return 67 return
67 } 68 }
68 69
69 func (r *Reader) UnreadByte() error { 70 func (r *Reader) UnreadByte() error {
70 if r.i <= 0 { 71 if r.i <= 0 {
71 return errors.New("bytes.Reader: at beginning of slice") 72 return errors.New("bytes.Reader: at beginning of slice")
72 } 73 }
73 r.i-- 74 r.i--
74 r.prevRune = -1 75 r.prevRune = -1
75 return nil 76 return nil
76 } 77 }
77 78
78 func (r *Reader) ReadRune() (ch rune, size int, err error) { 79 func (r *Reader) ReadRune() (ch rune, size int, err error) {
79 if r.i >= len(r.s) { 80 if r.i >= len(r.s) {
81 r.prevRune = -1
80 return 0, 0, io.EOF 82 return 0, 0, io.EOF
81 } 83 }
82 r.prevRune = r.i 84 r.prevRune = r.i
83 if c := r.s[r.i]; c < utf8.RuneSelf { 85 if c := r.s[r.i]; c < utf8.RuneSelf {
84 r.i++ 86 r.i++
85 return rune(c), 1, nil 87 return rune(c), 1, nil
86 } 88 }
87 ch, size = utf8.DecodeRune(r.s[r.i:]) 89 ch, size = utf8.DecodeRune(r.s[r.i:])
88 r.i += size 90 r.i += size
89 return 91 return
90 } 92 }
91 93
92 func (r *Reader) UnreadRune() error { 94 func (r *Reader) UnreadRune() error {
93 if r.prevRune < 0 { 95 if r.prevRune < 0 {
94 return errors.New("bytes.Reader: previous operation was not Read Rune") 96 return errors.New("bytes.Reader: previous operation was not Read Rune")
95 } 97 }
96 r.i = r.prevRune 98 r.i = r.prevRune
97 r.prevRune = -1 99 r.prevRune = -1
98 return nil 100 return nil
99 } 101 }
100 102
101 // Seek implements the io.Seeker interface. 103 // Seek implements the io.Seeker interface.
102 func (r *Reader) Seek(offset int64, whence int) (int64, error) { 104 func (r *Reader) Seek(offset int64, whence int) (int64, error) {
105 r.prevRune = -1
103 var abs int64 106 var abs int64
104 switch whence { 107 switch whence {
105 case 0: 108 case 0:
106 abs = offset 109 abs = offset
107 case 1: 110 case 1:
108 abs = int64(r.i) + offset 111 abs = int64(r.i) + offset
109 case 2: 112 case 2:
110 abs = int64(len(r.s)) + offset 113 abs = int64(len(r.s)) + offset
111 default: 114 default:
112 return 0, errors.New("bytes: invalid whence") 115 return 0, errors.New("bytes: invalid whence")
(...skipping 22 matching lines...) Expand all
135 r.i += m 138 r.i += m
136 n = int64(m) 139 n = int64(m)
137 if m != len(b) && err == nil { 140 if m != len(b) && err == nil {
138 err = io.ErrShortWrite 141 err = io.ErrShortWrite
139 } 142 }
140 return 143 return
141 } 144 }
142 145
143 // NewReader returns a new Reader reading from b. 146 // NewReader returns a new Reader reading from b.
144 func NewReader(b []byte) *Reader { return &Reader{b, 0, -1} } 147 func NewReader(b []byte) *Reader { return &Reader{b, 0, -1} }
LEFTRIGHT

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b