forked from lift/framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRE.scala
158 lines (132 loc) · 4.22 KB
/
RE.scala
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
/*
* Copyright 2006-2011 WorldWide Conferencing, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.liftweb
package util
import java.util.regex.Pattern
import scala.collection.mutable.ListBuffer
import common._
/**
* A regular expressions helper library
* RE("foo") =~ "A string" -or-
* "A string" =~: RE("foo") -or-
* "A String".substring(RE("Str"))
* ("A B cat D" =~: RE("([A-Z])")).capture // List(A,B,D)
*/
object RE {
/**
* Create a regular expression from a String
*/
def apply(in: String) = new REDoer(in, Empty)
def apply[T](in: String, func: PartialFunction[(T, List[String]), T]) = new REDoer(in, Full(func))
implicit def matchResToBoolean(in: REMatcher): Boolean = {
in match {
case null => false
case _ => in.matches
}
}
class SuperString(val str: String) {
def substring(re: REDoer[_]) = re.=~(str).matchStr
}
implicit def strToSuperStr(in: String): SuperString = new SuperString(in)
implicit def strToRe(in: String): REDoer[Nothing] = new REDoer(in, Empty)
}
class REDoer[T](val pattern: String, val func: Box[PartialFunction[(T, List[String]), T]]) extends Function2[T, String, Box[T]] {
val compiled = Pattern.compile(pattern)
def =~(other: String) = {
new REMatcher(other, compiled)
}
def =~:(other: String) = {
new REMatcher(other, compiled)
}
def apply(obj: T, other: String): Box[T] = {
val ma = new REMatcher(other, compiled)
if (!ma.matches) Empty
else func.flatMap(f => if (f.isDefinedAt((obj, ma.capture))) Full(f((obj, ma.capture))) else Empty)
}
}
object REMatcher {
def unapply(in: REMatcher): Option[List[String]] = Some(in.capture)
}
/**
* This class adds higher-order functions and lazy evaluation
* for pattern matching on top of the standard Java regular expressions
* library.
*
* @param str the String in which to perform pattern matching
* @param compiled the java.util.regex.Pattern to use to perform matches
*/
class REMatcher(val str: String, val compiled: Pattern) {
private val matcher = compiled.matcher(str)
/**
* Matches for the pattern in the specified string.
*/
lazy val matches = matcher.find
/**
* A Full Box containing the substring of this REMatcher's string containing the matches
* for the specified pattern, or Empty if no match exists.
*/
lazy val matchStr: Box[String] =
if (matches) Full(str.substring(matcher.start, matcher.end))
else Empty
/**
* Cached version of the matched groups in this matcher's string.
*/
lazy val capture = map(s => s)
/**
* Call the specified function for each match with each match
* and the list of all matched groups, then with any remaining data
* to the end of the string.
*/
def foreach(func: (String, List[String]) => Unit): Unit = {
var pos = 0
matcher.reset
val m = matcher
while (matcher.find) {
func(str.substring(pos, m.start), (0 to m.groupCount).toList.map(i => m.group(i)))
pos = matcher.end
}
func(str.substring(pos), Nil)
}
/**
* Map the specified function over the matches.
*/
def map[T](f : (String) => T): List[T] = synchronized {
val ab = new ListBuffer[T]
matcher.reset
val cnt = matcher.groupCount
def doIt {
def runIt(pos: Int) {
if (pos >= cnt) return
else {ab += f(matcher.group(pos + 1)) ; runIt(pos + 1)}
}
if (!matcher.find) return
else {runIt(0) ; doIt}
}
doIt
ab.toList
}
/**
* Return the list of lists of subgroups of matches.
*/
def eachFound: List[List[String]] = {
val ret = new ListBuffer[List[String]]
matcher.reset
while (matcher.find) {
ret += (0 to matcher.groupCount).toList.map(i => matcher.group(i))
}
ret.toList
}
}