reader.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. package strings
  5. import (
  6. "errors"
  7. "io"
  8. "unicode/utf8"
  9. )
  10. // A Reader implements the io.Reader, io.ReaderAt, io.Seeker, io.WriterTo,
  11. // io.ByteScanner, and io.RuneScanner interfaces by reading
  12. // from a string.
  13. type Reader struct {
  14. s string
  15. i int64 // current reading index
  16. prevRune int // index of previous rune; or < 0
  17. }
  18. // Len returns the number of bytes of the unread portion of the
  19. // string.
  20. func (r *Reader) Len() int {
  21. if r.i >= int64(len(r.s)) {
  22. return 0
  23. }
  24. return int(int64(len(r.s)) - r.i)
  25. }
  26. func (r *Reader) Read(b []byte) (n int, err error) {
  27. if len(b) == 0 {
  28. return 0, nil
  29. }
  30. if r.i >= int64(len(r.s)) {
  31. return 0, io.EOF
  32. }
  33. r.prevRune = -1
  34. n = copy(b, r.s[r.i:])
  35. r.i += int64(n)
  36. return
  37. }
  38. func (r *Reader) ReadAt(b []byte, off int64) (n int, err error) {
  39. // cannot modify state - see io.ReaderAt
  40. if off < 0 {
  41. return 0, errors.New("strings.Reader.ReadAt: negative offset")
  42. }
  43. if off >= int64(len(r.s)) {
  44. return 0, io.EOF
  45. }
  46. n = copy(b, r.s[off:])
  47. if n < len(b) {
  48. err = io.EOF
  49. }
  50. return
  51. }
  52. func (r *Reader) ReadByte() (b byte, err error) {
  53. r.prevRune = -1
  54. if r.i >= int64(len(r.s)) {
  55. return 0, io.EOF
  56. }
  57. b = r.s[r.i]
  58. r.i++
  59. return
  60. }
  61. func (r *Reader) UnreadByte() error {
  62. r.prevRune = -1
  63. if r.i <= 0 {
  64. return errors.New("strings.Reader.UnreadByte: at beginning of string")
  65. }
  66. r.i--
  67. return nil
  68. }
  69. func (r *Reader) ReadRune() (ch rune, size int, err error) {
  70. if r.i >= int64(len(r.s)) {
  71. r.prevRune = -1
  72. return 0, 0, io.EOF
  73. }
  74. r.prevRune = int(r.i)
  75. if c := r.s[r.i]; c < utf8.RuneSelf {
  76. r.i++
  77. return rune(c), 1, nil
  78. }
  79. ch, size = utf8.DecodeRuneInString(r.s[r.i:])
  80. r.i += int64(size)
  81. return
  82. }
  83. func (r *Reader) UnreadRune() error {
  84. if r.prevRune < 0 {
  85. return errors.New("strings.Reader.UnreadRune: previous operation was not ReadRune")
  86. }
  87. r.i = int64(r.prevRune)
  88. r.prevRune = -1
  89. return nil
  90. }
  91. // Seek implements the io.Seeker interface.
  92. func (r *Reader) Seek(offset int64, whence int) (int64, error) {
  93. r.prevRune = -1
  94. var abs int64
  95. switch whence {
  96. case 0:
  97. abs = offset
  98. case 1:
  99. abs = int64(r.i) + offset
  100. case 2:
  101. abs = int64(len(r.s)) + offset
  102. default:
  103. return 0, errors.New("strings.Reader.Seek: invalid whence")
  104. }
  105. if abs < 0 {
  106. return 0, errors.New("strings.Reader.Seek: negative position")
  107. }
  108. r.i = abs
  109. return abs, nil
  110. }
  111. // WriteTo implements the io.WriterTo interface.
  112. func (r *Reader) WriteTo(w io.Writer) (n int64, err error) {
  113. r.prevRune = -1
  114. if r.i >= int64(len(r.s)) {
  115. return 0, nil
  116. }
  117. s := r.s[r.i:]
  118. m, err := io.WriteString(w, s)
  119. if m > len(s) {
  120. panic("strings.Reader.WriteTo: invalid WriteString count")
  121. }
  122. r.i += int64(m)
  123. n = int64(m)
  124. if m != len(s) && err == nil {
  125. err = io.ErrShortWrite
  126. }
  127. return
  128. }
  129. // NewReader returns a new Reader reading from s.
  130. // It is similar to bytes.NewBufferString but more efficient and read-only.
  131. func NewReader(s string) *Reader { return &Reader{s, 0, -1} }