summaryrefslogtreecommitdiff
path: root/examples/scala-js/javalib/src/main/scala/java/util/regex/Matcher.scala
blob: 331f56b3042fb5bdf35c28b9b109cd82c921474c (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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
package java.util.regex

import scala.language.implicitConversions

import scala.annotation.switch

import scala.scalajs.js

final class Matcher private[regex] (
    private var pattern0: Pattern, private var input0: CharSequence,
    private var regionStart0: Int, private var regionEnd0: Int)
    extends AnyRef with MatchResult {

  import Matcher._

  def pattern(): Pattern = pattern0

  // Configuration (updated manually)
  private var regexp = new js.RegExp(pattern0.jspattern, pattern0.jsflags)
  private var inputstr = input0.subSequence(regionStart0, regionEnd0).toString

  // Match result (updated by successful matches)
  private var lastMatch: js.RegExp.ExecResult = null
  private var lastMatchIsValid = false
  private var canStillFind = true

  // Append state (updated by replacement methods)
  private var appendPos: Int = 0

  // Lookup methods

  def matches(): Boolean = {
    reset()
    find()
    // TODO this check is wrong with non-greedy patterns
    // Further, it might be wrong to just use ^$ delimiters for two reasons:
    // - They might already be there
    // - They might not behave as expected when newline characters are present
    if ((lastMatch ne null) && (start != 0 || end != inputstr.length))
      reset()
    lastMatch ne null
  }

  def lookingAt(): Boolean = {
    reset()
    find()
    if ((lastMatch ne null) && (start != 0))
      reset()
    lastMatch ne null
  }

  def find(): Boolean = if (canStillFind) {
    lastMatchIsValid = true
    lastMatch = regexp.exec(inputstr)
    if (lastMatch ne null) {
      if (lastMatch(0).get.isEmpty)
        regexp.lastIndex += 1
    } else {
      canStillFind = false
    }
    lastMatch ne null
  } else false

  def find(start: Int): Boolean = {
    reset()
    regexp.lastIndex = start
    find()
  }

  // Replace methods

  def appendReplacement(sb: StringBuffer, replacement: String): Matcher = {
    sb.append(inputstr.substring(appendPos, start))

    @inline def isDigit(c: Char) = c >= '0' && c <= '9'

    val len = replacement.length
    var i = 0
    while (i < len) {
      replacement.charAt(i) match {
        case '$' =>
          i += 1
          val j = i
          while (i < len && isDigit(replacement.charAt(i)))
            i += 1
          val group = Integer.parseInt(replacement.substring(j, i))
          sb.append(this.group(group))

        case '\\' =>
          i += 1
          if (i < len)
            sb.append(replacement.charAt(i))
          i += 1

        case c =>
          sb.append(c)
          i += 1
      }
    }

    appendPos = end
    this
  }

  def appendTail(sb: StringBuffer): StringBuffer = {
    sb.append(inputstr.substring(appendPos))
    appendPos = inputstr.length
    sb
  }

  def replaceFirst(replacement: String): String = {
    reset()

    if (find()) {
      val sb = new StringBuffer
      appendReplacement(sb, replacement)
      appendTail(sb)
      sb.toString
    } else {
      inputstr
    }
  }

  def replaceAll(replacement: String): String = {
    reset()

    val sb = new StringBuffer
    while (find()) {
      appendReplacement(sb, replacement)
    }
    appendTail(sb)

    sb.toString
  }

  // Reset methods

  def reset(): Matcher = {
    regexp.lastIndex = 0
    lastMatch = null
    lastMatchIsValid = false
    canStillFind = true
    appendPos = 0
    this
  }

  def reset(input: CharSequence): Matcher = {
    regionStart0 = 0
    regionEnd0 = input.length()
    input0 = input
    inputstr = input0.toString
    reset()
  }

  def usePattern(pattern: Pattern): Matcher = {
    val prevLastIndex = regexp.lastIndex
    pattern0 = pattern
    regexp = new js.RegExp(pattern.jspattern, pattern.jsflags)
    regexp.lastIndex = prevLastIndex
    lastMatch = null
    this
  }

  // Query state methods - implementation of MatchResult

  private def ensureLastMatch: js.RegExp.ExecResult = {
    if (lastMatch == null)
      throw new IllegalStateException("No match available")
    lastMatch
  }

  def groupCount(): Int = ensureLastMatch.length-1

  def start(): Int = ensureLastMatch.index
  def end(): Int = start() + group().length
  def group(): String = ensureLastMatch(0).get

  def start(group: Int): Int = {
    if (group == 0) start()
    else {
      val last = ensureLastMatch
      // not provided by JS RegExp, so we make up something that at least
      // will have some sound behavior from scala.util.matching.Regex
      last(group).fold(-1) {
        groupStr => inputstr.indexOf(groupStr, last.index)
      }
    }
  }

  def end(group: Int): Int = {
    val s = start(group)
    if (s == -1) -1
    else s + this.group(group).length
  }

  def group(group: Int): String = ensureLastMatch(group).orNull

  // Seal the state

  def toMatchResult(): MatchResult = new SealedResult(inputstr, lastMatch)

  // Other query state methods

  def hitEnd(): Boolean =
    lastMatchIsValid && (lastMatch == null || end() == inputstr.length)

  //def requireEnd(): Boolean // I don't understand the spec

  // Stub methods for region management

  def regionStart(): Int = regionStart0
  def regionEnd(): Int = regionEnd0
  def region(start: Int, end: Int): Matcher =
    new Matcher(pattern0, input0, start, end)

  def hasTransparentBounds(): Boolean = false
  //def useTransparentBounds(b: Boolean): Matcher

  def hasAnchoringBounds(): Boolean = true
  //def useAnchoringBounds(b: Boolean): Matcher
}

object Matcher {
  def quoteReplacement(s: String): String = {
    var result = ""
    var i = 0
    while (i < s.length) {
      val c = s.charAt(i)
      result += ((c: @switch) match {
        case '\\' | '$' => "\\"+c
        case _ => c
      })
      i += 1
    }
    result
  }

  private final class SealedResult(inputstr: String,
      lastMatch: js.RegExp.ExecResult) extends MatchResult {

    def groupCount(): Int = ensureLastMatch.length-1

    def start(): Int = ensureLastMatch.index
    def end(): Int = start() + group().length
    def group(): String = ensureLastMatch(0).get

    def start(group: Int): Int = {
      if (group == 0) start()
      else {
        val last = ensureLastMatch

        // not provided by JS RegExp, so we make up something that at least
        // will have some sound behavior from scala.util.matching.Regex
        last(group).fold(-1) {
          groupStr => inputstr.indexOf(groupStr, last.index)
        }
      }
    }

    def end(group: Int): Int = {
      val s = start(group)
      if (s == -1) -1
      else s + this.group(group).length
    }

    def group(group: Int): String = ensureLastMatch(group).orNull

    private def ensureLastMatch: js.RegExp.ExecResult = {
      if (lastMatch == null)
        throw new IllegalStateException("No match available")
      lastMatch
    }
  }
}