iter.mx raw

   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 utf8 implements functions and constants to support text encoded in
   6  // UTF-8. It includes functions to translate between runes and UTF-8 byte sequences.
   7  // See https://en.wikipedia.org/wiki/UTF-8
   8  package utf8
   9  
  10  type RuneIter struct {
  11      src  []byte
  12      pos  int32
  13      start int32
  14      r    rune
  15      size int32
  16  }
  17  
  18  func Iter(s []byte) RuneIter { return RuneIter{src: s} }
  19  
  20  func (it *RuneIter) Next() bool {
  21      if it.pos >= int32(len(it.src)) { return false }
  22      it.start = it.pos
  23      r, sz := DecodeRune(it.src[it.pos:])
  24      it.r = r
  25      it.size = int32(sz)
  26      return true
  27  }
  28  
  29  func (it *RuneIter) Rune() rune { return it.r }
  30  
  31  func (it *RuneIter) Advance() { it.pos += it.size }
  32  
  33  func (it *RuneIter) Pos() int32 { return it.start }
  34  
  35  func (it *RuneIter) Size() int32 { return it.size }