Skip to content

Commit 4965cb4

Browse files
committed
Reuse regex matcher in replaceAll calls
In: - StdNames.str.sanitize - Text's lengthWithoutAnsi
1 parent 4f6eb18 commit 4965cb4

File tree

2 files changed

+5
-2
lines changed

2 files changed

+5
-2
lines changed

compiler/src/dotty/tools/dotc/core/StdNames.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ object StdNames {
4040
inline val Tuple = "Tuple"
4141
inline val Product = "Product"
4242

43-
def sanitize(str: String): String = str.replaceAll("""[<>]""", """\$""").nn
43+
private val disallowed = java.util.regex.Pattern.compile("""[<>]""").nn
44+
def sanitize(str: String): String = disallowed.matcher(str).nn.replaceAll("""\$""").nn
4445
}
4546

4647
abstract class DefinedNames[N <: Name] {

compiler/src/dotty/tools/dotc/printing/Texts.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package printing
33

44
object Texts {
55

6+
private val ansi = java.util.regex.Pattern.compile("\u001b\\[\\d+m").nn
7+
68
sealed abstract class Text {
79

810
protected def indentMargin: Int = 2
@@ -70,7 +72,7 @@ object Texts {
7072
else appendIndented(that)(width)
7173

7274
private def lengthWithoutAnsi(str: String): Int =
73-
str.replaceAll("\u001b\\[\\d+m", "").nn.length
75+
ansi.matcher(str).nn.replaceAll("").nn.length
7476

7577
def layout(width: Int): Text = this match {
7678
case Str(s, _) =>

0 commit comments

Comments
 (0)