OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libgo/] [go/] [strings/] [reader.go] - Blame information for rev 760

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 747 jeremybenn
// 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
 
7
import (
8
        "errors"
9
        "io"
10
        "unicode/utf8"
11
)
12
 
13
// A Reader implements the io.Reader, io.ByteScanner, and
14
// io.RuneScanner interfaces by reading from a string.
15
type Reader struct {
16
        s        string
17
        i        int // current reading index
18
        prevRune int // index of previous rune; or < 0
19
}
20
 
21
// Len returns the number of bytes of the unread portion of the
22
// string.
23
func (r *Reader) Len() int {
24
        return len(r.s) - r.i
25
}
26
 
27
func (r *Reader) Read(b []byte) (n int, err error) {
28
        if r.i >= len(r.s) {
29
                return 0, io.EOF
30
        }
31
        n = copy(b, r.s[r.i:])
32
        r.i += n
33
        r.prevRune = -1
34
        return
35
}
36
 
37
func (r *Reader) ReadByte() (b byte, err error) {
38
        if r.i >= len(r.s) {
39
                return 0, io.EOF
40
        }
41
        b = r.s[r.i]
42
        r.i++
43
        r.prevRune = -1
44
        return
45
}
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() error {
51
        if r.i <= 0 {
52
                return errors.New("strings.Reader: at beginning of string")
53
        }
54
        r.i--
55
        r.prevRune = -1
56
        return nil
57
}
58
 
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 io.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() (ch rune, size int, err error) {
65
        if r.i >= len(r.s) {
66
                return 0, 0, io.EOF
67
        }
68
        r.prevRune = r.i
69
        if c := r.s[r.i]; c < utf8.RuneSelf {
70
                r.i++
71
                return rune(c), 1, nil
72
        }
73
        ch, size = utf8.DecodeRuneInString(r.s[r.i:])
74
        r.i += size
75
        return
76
}
77
 
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() error {
82
        if r.prevRune < 0 {
83
                return errors.New("strings.Reader: previous operation was not ReadRune")
84
        }
85
        r.i = r.prevRune
86
        r.prevRune = -1
87
        return nil
88
}
89
 
90
// NewReader returns a new Reader reading from s.
91
// It is similar to bytes.NewBufferString but more efficient and read-only.
92
func NewReader(s string) *Reader { return &Reader{s, 0, -1} }

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.