Russ Cox | 10c7d19 | 2009-10-12 10:09:35 -0700 | [diff] [blame] | 1 | // Copyright 2009 The Go Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | package strings |
| 6 | |
Kyle Consalus | d7b4851 | 2010-04-20 22:18:26 -0700 | [diff] [blame] | 7 | import ( |
| 8 | "os" |
| 9 | "utf8" |
| 10 | ) |
Russ Cox | 10c7d19 | 2009-10-12 10:09:35 -0700 | [diff] [blame] | 11 | |
Robert Griesemer | 9cd3372 | 2011-05-26 11:02:07 -0700 | [diff] [blame] | 12 | // A Reader implements the io.Reader, io.ByteScanner, and |
| 13 | // io.RuneScanner interfaces by reading from a string. |
| 14 | type Reader struct { |
| 15 | s string |
| 16 | i int // current reading index |
| 17 | prevRune int // index of previous rune; or < 0 |
| 18 | } |
Russ Cox | 10c7d19 | 2009-10-12 10:09:35 -0700 | [diff] [blame] | 19 | |
Brad Fitzpatrick | 2198050 | 2011-05-31 08:47:03 -0700 | [diff] [blame^] | 20 | // Len returns the number of bytes of the unread portion of the |
| 21 | // string. |
| 22 | func (r *Reader) Len() int { |
| 23 | return len(r.s) - r.i |
| 24 | } |
| 25 | |
Russ Cox | 10c7d19 | 2009-10-12 10:09:35 -0700 | [diff] [blame] | 26 | func (r *Reader) Read(b []byte) (n int, err os.Error) { |
Robert Griesemer | 9cd3372 | 2011-05-26 11:02:07 -0700 | [diff] [blame] | 27 | if r.i >= len(r.s) { |
Robert Griesemer | 40621d5 | 2009-11-09 12:07:39 -0800 | [diff] [blame] | 28 | return 0, os.EOF |
Russ Cox | 10c7d19 | 2009-10-12 10:09:35 -0700 | [diff] [blame] | 29 | } |
Robert Griesemer | 9cd3372 | 2011-05-26 11:02:07 -0700 | [diff] [blame] | 30 | n = copy(b, r.s[r.i:]) |
| 31 | r.i += n |
| 32 | r.prevRune = -1 |
Robert Griesemer | d65a5cc | 2009-12-15 15:40:16 -0800 | [diff] [blame] | 33 | return |
Russ Cox | 10c7d19 | 2009-10-12 10:09:35 -0700 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | func (r *Reader) ReadByte() (b byte, err os.Error) { |
Robert Griesemer | 9cd3372 | 2011-05-26 11:02:07 -0700 | [diff] [blame] | 37 | if r.i >= len(r.s) { |
Robert Griesemer | 40621d5 | 2009-11-09 12:07:39 -0800 | [diff] [blame] | 38 | return 0, os.EOF |
Russ Cox | 10c7d19 | 2009-10-12 10:09:35 -0700 | [diff] [blame] | 39 | } |
Robert Griesemer | 9cd3372 | 2011-05-26 11:02:07 -0700 | [diff] [blame] | 40 | b = r.s[r.i] |
| 41 | r.i++ |
| 42 | r.prevRune = -1 |
Robert Griesemer | d65a5cc | 2009-12-15 15:40:16 -0800 | [diff] [blame] | 43 | return |
Russ Cox | 10c7d19 | 2009-10-12 10:09:35 -0700 | [diff] [blame] | 44 | } |
| 45 | |
Robert Griesemer | 9cd3372 | 2011-05-26 11:02:07 -0700 | [diff] [blame] | 46 | |
| 47 | // UnreadByte moves the reading position back by one byte. |
| 48 | // It is an error to call UnreadByte if nothing has been |
| 49 | // read yet. |
| 50 | func (r *Reader) UnreadByte() os.Error { |
| 51 | if r.i <= 0 { |
| 52 | return os.ErrorString("strings.Reader: at beginning of string") |
| 53 | } |
| 54 | r.i-- |
| 55 | r.prevRune = -1 |
| 56 | return nil |
| 57 | } |
| 58 | |
Kyle Consalus | d7b4851 | 2010-04-20 22:18:26 -0700 | [diff] [blame] | 59 | // ReadRune reads and returns the next UTF-8-encoded |
| 60 | // Unicode code point from the buffer. |
| 61 | // If no bytes are available, the error returned is os.EOF. |
| 62 | // If the bytes are an erroneous UTF-8 encoding, it |
| 63 | // consumes one byte and returns U+FFFD, 1. |
| 64 | func (r *Reader) ReadRune() (rune int, size int, err os.Error) { |
Robert Griesemer | 9cd3372 | 2011-05-26 11:02:07 -0700 | [diff] [blame] | 65 | if r.i >= len(r.s) { |
Kyle Consalus | d7b4851 | 2010-04-20 22:18:26 -0700 | [diff] [blame] | 66 | return 0, 0, os.EOF |
| 67 | } |
Robert Griesemer | 9cd3372 | 2011-05-26 11:02:07 -0700 | [diff] [blame] | 68 | r.prevRune = r.i |
| 69 | if c := r.s[r.i]; c < utf8.RuneSelf { |
| 70 | r.i++ |
Kyle Consalus | d7b4851 | 2010-04-20 22:18:26 -0700 | [diff] [blame] | 71 | return int(c), 1, nil |
| 72 | } |
Robert Griesemer | 9cd3372 | 2011-05-26 11:02:07 -0700 | [diff] [blame] | 73 | rune, size = utf8.DecodeRuneInString(r.s[r.i:]) |
| 74 | r.i += size |
Kyle Consalus | d7b4851 | 2010-04-20 22:18:26 -0700 | [diff] [blame] | 75 | return |
| 76 | } |
| 77 | |
Robert Griesemer | 9cd3372 | 2011-05-26 11:02:07 -0700 | [diff] [blame] | 78 | // UnreadRune causes the next call to ReadRune to return the same rune |
| 79 | // as the previous call to ReadRune. |
| 80 | // The last method called on r must have been ReadRune. |
| 81 | func (r *Reader) UnreadRune() os.Error { |
| 82 | if r.prevRune < 0 { |
| 83 | return os.ErrorString("strings.Reader: previous operation was not ReadRune") |
| 84 | } |
| 85 | r.i = r.prevRune |
| 86 | r.prevRune = -1 |
| 87 | return nil |
| 88 | } |
| 89 | |
Russ Cox | 10c7d19 | 2009-10-12 10:09:35 -0700 | [diff] [blame] | 90 | // NewReader returns a new Reader reading from s. |
| 91 | // It is similar to bytes.NewBufferString but more efficient and read-only. |
Robert Griesemer | 9cd3372 | 2011-05-26 11:02:07 -0700 | [diff] [blame] | 92 | func NewReader(s string) *Reader { return &Reader{s, 0, -1} } |