@@ -508,9 +508,10 @@ class Regex private[matching](val pattern: Pattern, groupNames: String*) extends
508
508
* @return The target string after replacements.
509
509
*/
510
510
def replaceAllIn (target : CharSequence , replacer : Match => String ): String = {
511
- val it = new Regex .MatchIterator (target, this , groupNames).replacementData
512
- it foreach (md => it replace replacer(md))
513
- it.replaced
511
+ val rit = new Regex .MatchIterator (target, this , groupNames).replacementData
512
+ for (matchdata <- rit; replacement = replacer(matchdata))
513
+ rit.replace(replacement)
514
+ rit.replaced
514
515
}
515
516
516
517
/**
@@ -535,11 +536,10 @@ class Regex private[matching](val pattern: Pattern, groupNames: String*) extends
535
536
* @return The target string after replacements.
536
537
*/
537
538
def replaceSomeIn (target : CharSequence , replacer : Match => Option [String ]): String = {
538
- val it = new Regex .MatchIterator (target, this , groupNames).replacementData
539
- for (matchdata <- it ; replacement <- replacer(matchdata))
540
- it replace replacement
541
-
542
- it.replaced
539
+ val rit = new Regex .MatchIterator (target, this , groupNames).replacementData
540
+ for (matchdata <- rit; replacement <- replacer(matchdata))
541
+ rit.replace(replacement)
542
+ rit.replaced
543
543
}
544
544
545
545
/** Replaces the first match by a string.
@@ -874,28 +874,27 @@ object Regex {
874
874
875
875
/** Convert to an iterator that yields MatchData elements instead of Strings and has replacement support. */
876
876
private [matching] def replacementData = new AbstractIterator [Match ] with Replacement {
877
- def matcher = self.matcher
877
+ protected def matcher = self.matcher
878
878
def hasNext = self.hasNext
879
- def next () = { self.next(); new Match (source, matcher, _groupNames).force }
879
+ def next (): Match = { self.next(); new Match (source, matcher, _groupNames).force }
880
880
}
881
881
}
882
882
883
- /**
884
- * A trait able to build a string with replacements assuming it has a matcher.
885
- * Meant to be mixed in with iterators.
883
+ /** Internal trait used by `replaceAllIn` and `replaceSomeIn`.
886
884
*/
887
885
private [matching] trait Replacement {
888
886
protected def matcher : Matcher
889
887
890
- private [this ] val sb = new java.lang.StringBuffer
888
+ private [this ] val sb = new java.lang.StringBuffer // StringBuffer for JDK 8 compatibility
891
889
890
+ // Appends the remaining input and returns the result text.
892
891
def replaced = {
893
- val newsb = new java.lang.StringBuffer (sb)
894
- matcher.appendTail(newsb)
895
- newsb.toString
892
+ matcher.appendTail(sb)
893
+ sb.toString
896
894
}
897
895
898
- def replace (rs : String ) = matcher.appendReplacement(sb, rs)
896
+ // Appends the input prefix and the replacement text.
897
+ def replace (replacement : String ) = matcher.appendReplacement(sb, replacement)
899
898
}
900
899
901
900
/** Quotes strings to be used literally in regex patterns.
0 commit comments