@@ -508,9 +508,10 @@ class Regex private[matching](val pattern: Pattern, groupNames: String*) extends
508508 * @return The target string after replacements.
509509 */
510510 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
514515 }
515516
516517 /**
@@ -535,11 +536,10 @@ class Regex private[matching](val pattern: Pattern, groupNames: String*) extends
535536 * @return The target string after replacements.
536537 */
537538 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
543543 }
544544
545545 /** Replaces the first match by a string.
@@ -874,28 +874,27 @@ object Regex {
874874
875875 /** Convert to an iterator that yields MatchData elements instead of Strings and has replacement support. */
876876 private [matching] def replacementData = new AbstractIterator [Match ] with Replacement {
877- def matcher = self.matcher
877+ protected def matcher = self.matcher
878878 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 }
880880 }
881881 }
882882
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`.
886884 */
887885 private [matching] trait Replacement {
888886 protected def matcher : Matcher
889887
890- private [this ] val sb = new java.lang.StringBuffer
888+ private [this ] val sb = new java.lang.StringBuffer // StringBuffer for JDK 8 compatibility
891889
890+ // Appends the remaining input and returns the result text.
892891 def replaced = {
893- val newsb = new java.lang.StringBuffer (sb)
894- matcher.appendTail(newsb)
895- newsb.toString
892+ matcher.appendTail(sb)
893+ sb.toString
896894 }
897895
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)
899898 }
900899
901900 /** Quotes strings to be used literally in regex patterns.
0 commit comments