Skip to content

Commit 32bd598

Browse files
committed
Reuse regex matcher in replaceAll calls
In: - StdNames.str.sanitize - Text's lengthWithoutAnsi
1 parent 0d418c0 commit 32bd598

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

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

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

44
import scala.collection.mutable
55
import scala.annotation.switch
6+
import scala.annotation.internal.sharable
67
import Names._
78
import Symbols._
89
import Contexts._
@@ -40,7 +41,9 @@ object StdNames {
4041
inline val Tuple = "Tuple"
4142
inline val Product = "Product"
4243

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

4649
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)