8000 Merge pull request #11061 from som-snytt/tweak/dubious-string-buffer · scala/scala@f313bf2 · GitHub
[go: up one dir, main page]

Skip to content

Commit f313bf2

Browse files
authored
Merge pull request #11061 from som-snytt/tweak/dubious-string-buffer
Dubious string buffer [ci: last-only]
2 parents 7e411f1 + 76d23c1 commit f313bf2

File tree

2 files changed

+20
-21
lines changed

2 files changed

+20
-21
lines changed

src/library/scala/util/matching/Regex.scala

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -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.

src/reflect/scala/reflect/internal/Symbols.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1316,21 +1316,21 @@ trait Symbols extends api.Symbols { self: SymbolTable =>
13161316
final def fullName(separator: Char): String = fullName(separator, "")
13171317

13181318
private def fullName(separator: Char, suffix: CharSequence): String = {
1319-
var b: java.lang.StringBuffer = null
1319+
var b: StringBuilder = null
13201320
def loop(size: Int, sym: Symbol): Unit = {
13211321
val symName = sym.name
13221322
val nSize = symName.length - (if (symName.endsWith(nme.LOCAL_SUFFIX_STRING)) 1 else 0)
13231323
if (sym.isRoot || sym.isRootPackage || sym == NoSymbol || sym.owner.isEffectiveRoot) {
13241324
val capacity = size + nSize
1325-
b = new java.lang.StringBuffer(capacity)
1325+
b = new StringBuilder(capacity)
13261326
symName.appendTo(b, 0, nSize)
13271327
} else {
13281328
loop(size + nSize + 1, sym.effectiveOwner.enclClass)
13291329
b.append(separator)
13301330
symName.appendTo(b, 0, nSize)
13311331
}
13321332
}
1333-
loop(suffix.length(), this)
1333+
loop(suffix.length, this)
13341334
b.append(suffix)
13351335
b.toString
13361336
}

0 commit comments

Comments
 (0)
0