summaryrefslogtreecommitdiff
path: root/src/library
diff options
context:
space:
mode:
authorAleksandar Pokopec <aleksandar.prokopec@epfl.ch>2010-01-25 18:21:04 +0000
committerAleksandar Pokopec <aleksandar.prokopec@epfl.ch>2010-01-25 18:21:04 +0000
commit0516cd02f1ad19e9dfb1e4a4fbe198932bf8b2e2 (patch)
tree4a6e4dd7be7ba18529d8366629cfacdb0e0afdca /src/library
parent02c6aa766b53523dbb126fd5f8236becfe09036e (diff)
downloadscala-0516cd02f1ad19e9dfb1e4a4fbe198932bf8b2e2.tar.gz
scala-0516cd02f1ad19e9dfb1e4a4fbe198932bf8b2e2.tar.bz2
scala-0516cd02f1ad19e9dfb1e4a4fbe198932bf8b2e2.zip
Replacement in matching can now be done by prov...
Replacement in matching can now be done by providing function arguments for replacement. Fixes #2761. Review by phaller.
Diffstat (limited to 'src/library')
-rw-r--r--src/library/scala/util/matching/Regex.scala52
1 files changed, 51 insertions, 1 deletions
diff --git a/src/library/scala/util/matching/Regex.scala b/src/library/scala/util/matching/Regex.scala
index 2ceef4563c..951dea11c4 100644
--- a/src/library/scala/util/matching/Regex.scala
+++ b/src/library/scala/util/matching/Regex.scala
@@ -107,6 +107,31 @@ class Regex(regex: String, groupNames: String*) {
m.replaceAll(replacement)
}
+ def replaceAllIn(target: java.lang.CharSequence, replacer: String => String): String = {
+ val it = new Regex.MatchIterator(target, this, groupNames) with Replacement
+ while (it.hasNext) {
+ val matchedString = it.next
+ it.replace(replacer(matchedString))
+ }
+ it.replaced
+ }
+
+ /**
+ * Replaces all matches using a replacer function.
+ *
+ * @param target The string to match.
+ * @param replacer The function which maps match data to another string.
+ * @return The resulting string.
+ */
+ def replaceAllMatchDataIn(target: java.lang.CharSequence, replacer: Match => String): String = {
+ val it = new Regex.MatchIterator(target, this, groupNames).replacementData
+ while (it.hasNext) {
+ val matchdata = it.next
+ it.replace(replacer(matchdata))
+ }
+ it.replaced
+ }
+
/** Replaces the first match by a string.
*
* @param target The string to match
@@ -269,7 +294,7 @@ object Regex {
class MatchIterator(val source: java.lang.CharSequence, val regex: Regex, val groupNames: Seq[String])
extends Iterator[String] with MatchData { self =>
- private val matcher = regex.pattern.matcher(source)
+ val matcher = regex.pattern.matcher(source)
private var nextSeen = false
/** Is there another match? */
@@ -307,6 +332,31 @@ object Regex {
def hasNext = self.hasNext
def next = { self.next; new Match(source, matcher, groupNames).force }
}
+
+ /** Convert to an iterator that yields MatchData elements instead of Strings and has replacement support */
+ def replacementData = new Iterator[Match] with Replacement {
+ def matcher = self.matcher
+ def hasNext = self.hasNext
+ def next = { self.next; new Match(source, matcher, groupNames).force }
+ }
+ }
+
+ /**
+ * A trait able to build a string with replacements assuming it has a matcher.
+ * Meant to be mixed in with iterators.
+ */
+ trait Replacement {
+ def matcher: Matcher
+
+ private var sb = new java.lang.StringBuffer
+
+ def replaced = {
+ val newsb = new java.lang.StringBuffer(sb)
+ matcher.appendTail(newsb)
+ newsb.toString
+ }
+
+ def replace(rs: String) = matcher.appendReplacement(sb, rs)
}
}