diff options
author | Adriaan Moors <adriaan.moors@typesafe.com> | 2014-01-15 14:08:12 -0800 |
---|---|---|
committer | Adriaan Moors <adriaan.moors@typesafe.com> | 2014-01-15 14:08:12 -0800 |
commit | a7de84d24aafb554019ebebff36a18c966568a30 (patch) | |
tree | 0d74c614ed4d8ff5905becb52e7e3e483715b063 | |
parent | 0a2e5905bef4c2f02eaca6bda115266833ff0057 (diff) | |
parent | 2fe767806b44aea3f6f53735301b11fd2ffae08b (diff) | |
download | scala-a7de84d24aafb554019ebebff36a18c966568a30.tar.gz scala-a7de84d24aafb554019ebebff36a18c966568a30.tar.bz2 scala-a7de84d24aafb554019ebebff36a18c966568a30.zip |
Merge pull request #3317 from qerub/si-8107-add-regex-quote
SI-8107: Add Regex.quote
-rw-r--r-- | src/library/scala/Enumeration.scala | 4 | ||||
-rw-r--r-- | src/library/scala/collection/immutable/StringLike.scala | 4 | ||||
-rw-r--r-- | src/library/scala/util/matching/Regex.scala | 8 | ||||
-rw-r--r-- | src/repl/scala/tools/nsc/interpreter/JavapClass.scala | 6 | ||||
-rw-r--r-- | src/repl/scala/tools/nsc/interpreter/Naming.scala | 7 |
5 files changed, 18 insertions, 11 deletions
diff --git a/src/library/scala/Enumeration.scala b/src/library/scala/Enumeration.scala index 59be0cdfa3..d4b9c17eab 100644 --- a/src/library/scala/Enumeration.scala +++ b/src/library/scala/Enumeration.scala @@ -11,7 +11,7 @@ package scala import scala.collection.{ mutable, immutable, generic, SortedSetLike, AbstractSet } import java.lang.reflect.{ Modifier, Method => JMethod, Field => JField } import scala.reflect.NameTransformer._ -import java.util.regex.Pattern +import scala.util.matching.Regex /** Defines a finite set of values specific to the enumeration. Typically * these values enumerate all possible forms something can take and provide @@ -64,7 +64,7 @@ abstract class Enumeration (initial: Int) extends Serializable { */ override def toString = ((getClass.getName stripSuffix MODULE_SUFFIX_STRING split '.').last split - Pattern.quote(NAME_JOIN_STRING)).last + Regex.quote(NAME_JOIN_STRING)).last /** The mapping from the integer used to identify values to the actual * values. */ diff --git a/src/library/scala/collection/immutable/StringLike.scala b/src/library/scala/collection/immutable/StringLike.scala index 5a0d24ddd2..43d46cf4d0 100644 --- a/src/library/scala/collection/immutable/StringLike.scala +++ b/src/library/scala/collection/immutable/StringLike.scala @@ -164,8 +164,8 @@ self => * @return the resulting string */ def replaceAllLiterally(literal: String, replacement: String): String = { - val arg1 = java.util.regex.Pattern.quote(literal) - val arg2 = java.util.regex.Matcher.quoteReplacement(replacement) + val arg1 = Regex.quote(literal) + val arg2 = Regex.quoteReplacement(replacement) toString.replaceAll(arg1, arg2) } diff --git a/src/library/scala/util/matching/Regex.scala b/src/library/scala/util/matching/Regex.scala index 22dbb37789..86132bb876 100644 --- a/src/library/scala/util/matching/Regex.scala +++ b/src/library/scala/util/matching/Regex.scala @@ -704,6 +704,14 @@ object Regex { def replace(rs: String) = matcher.appendReplacement(sb, rs) } + /** Quotes strings to be used literally in regex patterns. + * + * All regex metacharacters in the input match themselves literally in the output. + * + * @example {{{List("US$", "CAN$").map(Regex.quote).mkString("|").r}}} + */ + def quote(text: String): String = Pattern quote text + /** Quotes replacement strings to be used in replacement methods. * * Replacement methods give special meaning to backslashes (`\`) and diff --git a/src/repl/scala/tools/nsc/interpreter/JavapClass.scala b/src/repl/scala/tools/nsc/interpreter/JavapClass.scala index 496d5face1..915fd57bf8 100644 --- a/src/repl/scala/tools/nsc/interpreter/JavapClass.scala +++ b/src/repl/scala/tools/nsc/interpreter/JavapClass.scala @@ -11,7 +11,6 @@ import java.lang.{ ClassLoader => JavaClassLoader, Iterable => JIterable } import scala.tools.nsc.util.ScalaClassLoader import java.io.{ ByteArrayInputStream, CharArrayWriter, FileNotFoundException, PrintWriter, Writer } import java.util.{ Locale } -import java.util.regex.Pattern import java.util.concurrent.ConcurrentLinkedQueue import javax.tools.{ Diagnostic, DiagnosticCollector, DiagnosticListener, ForwardingJavaFileManager, JavaFileManager, JavaFileObject, @@ -20,6 +19,7 @@ import scala.reflect.io.{ AbstractFile, Directory, File, Path } import scala.io.Source import scala.util.{ Try, Success, Failure } import scala.util.Properties.lineSeparator +import scala.util.matching.Regex import scala.collection.JavaConverters import scala.collection.generic.Clearable import java.net.URL @@ -608,12 +608,12 @@ object JavapClass { // class k, candidate f without prefix def isFunOfClass(k: String, f: String) = { - val p = (s"${Pattern quote k}\\$$+anonfun").r + val p = (s"${Regex quote k}\\$$+anonfun").r (p findPrefixOf f).nonEmpty } // class k, candidate f without prefix, method m def isFunOfMethod(k: String, m: String, f: String) = { - val p = (s"${Pattern quote k}\\$$+anonfun\\$$${Pattern quote m}\\$$").r + val p = (s"${Regex quote k}\\$$+anonfun\\$$${Regex quote m}\\$$").r (p findPrefixOf f).nonEmpty } def isFunOfTarget(k: String, m: Option[String], f: String) = diff --git a/src/repl/scala/tools/nsc/interpreter/Naming.scala b/src/repl/scala/tools/nsc/interpreter/Naming.scala index cf38a2ae3a..e09c6f315e 100644 --- a/src/repl/scala/tools/nsc/interpreter/Naming.scala +++ b/src/repl/scala/tools/nsc/interpreter/Naming.scala @@ -8,6 +8,7 @@ package tools.nsc package interpreter import scala.util.Properties.lineSeparator +import scala.util.matching.Regex /** This is for name logic which is independent of the compiler (notice there's no Global.) * That includes at least generating, metaquoting, mangling, and unmangling. @@ -38,12 +39,10 @@ trait Naming { // // $line3.$read.$iw.$iw.Bippy = // $line3.$read$$iw$$iw$Bippy@4a6a00ca - - private def noMeta(s: String) = "\\Q" + s + "\\E" lazy val lineRegex = { val sn = sessionNames - val members = List(sn.read, sn.eval, sn.print) map noMeta mkString ("(?:", "|", ")") - debugging("lineRegex")(noMeta(sn.line) + """\d+[./]""" + members + """[$.]""") + val members = List(sn.read, sn.eval, sn.print) map Regex.quote mkString ("(?:", "|", ")") + debugging("lineRegex")(Regex.quote(sn.line) + """\d+[./]""" + members + """[$.]""") } private def removeLineWrapper(s: String) = s.replaceAll(lineRegex, "") |