Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
basic regex support. Just a wrapper for scala (java) regex classes.
- Loading branch information
Showing
13 changed files
with
410 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
src/main/scala/org/moe/runtime/nativeobjects/MoeRegexObject.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package org.moe.runtime.nativeobjects | ||
|
||
import org.moe.runtime._ | ||
import org.moe.runtime.nativeobjects._ | ||
|
||
import scala.util.matching.Regex | ||
|
||
class MoeRegexObject( | ||
rx: String, | ||
flags: Option[String] = None, | ||
t: Option[MoeType] = None | ||
) extends MoeNativeObject[(String, Option[String])]((rx, flags), t) { | ||
|
||
private val regex = new Regex(rx).unanchored | ||
|
||
def getRegex = regex | ||
def getFlags = flags.getOrElse("") | ||
|
||
def matches (r: MoeRuntime, text: MoeStrObject): MoeBoolObject = r.NativeObjects.getBool( | ||
regex.findFirstIn(text.unboxToString.get).nonEmpty | ||
) | ||
|
||
private def regexFlagsToMap(flags: String): Map[String, Boolean] = { | ||
val validFlags = Map( | ||
'i' -> "ignore_case", | ||
'g' -> "global", | ||
'm' -> "match_multiple_lines", | ||
's' -> "match_single_line", | ||
'x' -> "extended" | ||
) | ||
(for (f <- flags) yield (validFlags(f) -> true)).toMap | ||
} | ||
|
||
def find (r: MoeRuntime, | ||
target: MoeStrObject, | ||
flags: Option[MoeStrObject] = None | ||
) = { | ||
val f: String = flags match { | ||
case Some(f) => f.unboxToString.get | ||
case None => getFlags | ||
} | ||
|
||
// TODO: other flags | ||
val find_all = regexFlagsToMap(f).getOrElse("global", false) | ||
val matches = if (find_all) | ||
regex.findAllIn(target.unboxToString.get) | ||
else | ||
regex.findFirstIn(target.unboxToString.get) | ||
} | ||
|
||
def replace ( | ||
r: MoeRuntime, | ||
target: MoeStrObject, | ||
replacement: MoeStrObject, | ||
flags: Option[MoeStrObject] | ||
): MoeStrObject = { | ||
val f = flags match { | ||
case Some(f) => f.unboxToString.get | ||
case None => getFlags | ||
} | ||
val replace_all = regexFlagsToMap(f).getOrElse("global", false) | ||
// TODO: other flags | ||
r.NativeObjects.getStr( | ||
if (replace_all) | ||
regex.replaceAllIn(target.unboxToString.get, replacement.unboxToString.get) | ||
else | ||
regex.replaceFirstIn(target.unboxToString.get, replacement.unboxToString.get) | ||
) | ||
} | ||
|
||
// MoeNativeObject overrides | ||
|
||
override def copy = new MoeRegexObject(getNativeValue._1, getNativeValue._2, getAssociatedType) | ||
|
||
} |
Oops, something went wrong.