summaryrefslogtreecommitdiff
path: root/src/compiler/scala/tools/nsc/util/NewCharArrayReader.scala
blob: 3dfb84d581948e4927495718f4022ae692482676 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/* NSC -- new Scala compiler
 * Copyright 2005-2009 LAMP/EPFL
 * @author  Martin Odersky
 */
// $Id$

package scala.tools.nsc.util

import scala.tools.nsc.util.SourceFile.{LF, FF, CR, SU}

class NewCharArrayReader(val buf: RandomAccessSeq[Char], // should not change
                       decodeUni: Boolean, error: (Int,String) => Unit) extends Iterator[Char] {
  private var idx : Int = 0
  private var isUnicode0 = false
  def isUnicode = isUnicode0

  private val bufLength = buf.length
  def seek(offset : Int) = {
    assert(offset <= bufLength)
    idx = offset
  }
  def offset = idx
  def withOffset = new Iterator[(Int,Char)] {
    def hasNext = NewCharArrayReader.this.hasNext
    def next = (offset, NewCharArrayReader.this.next)
  }
  override def hasNext = { // could be padded
    if (idx == bufLength - 1) buf(idx) != SU
    else idx < bufLength
  }
  override def next : Char = {
    isUnicode0 = false
    if (!hasNext) return SU
    var ch = buf(idx)
    idx = idx + 1
    ch match {
    case CR if buf.safeIs(idx + 1, LF) =>
      idx += 1; ch = LF
    case LF | FF =>
    case '\\' =>
      def evenSlashPrefix: Boolean = {
        var p = idx - 2
        while (p >= 0 && buf(p) == '\\') p = p - 1;
        (idx - p) % 2 == 0
      }
      def udigit: Int = {
        val d = digit2int(buf(idx), 16)
        if (d >= 0) { idx = idx + 1 }
        else if (error != null) error(idx, "error in unicode escape");
        d
      }
      if (idx < bufLength && buf(idx) == 'u' && decodeUni && evenSlashPrefix) {
        do {
          idx = idx + 1; // nextcol = nextcol + 1;
        } while (idx < bufLength && buf(idx) == 'u');
        val code = udigit << 12 | udigit << 8 | udigit << 4 | udigit
        isUnicode0 = true
        ch = code.asInstanceOf[Char]
      }
    case _ =>
    }
    ch
  }
  def digit2int(ch: Char, base: Int): Int = {
    if ('0' <= ch && ch <= '9' && ch < '0' + base)
      ch - '0'
    else if ('A' <= ch && ch < 'A' + base - 10)
      ch - 'A' + 10
    else if ('a' <= ch && ch < 'a' + base - 10)
      ch - 'a' + 10
    else
      -1
  }
}