diff options
author | Adriaan Moors <adriaan.moors@typesafe.com> | 2015-02-13 11:47:15 -0800 |
---|---|---|
committer | Adriaan Moors <adriaan.moors@typesafe.com> | 2015-02-13 11:47:15 -0800 |
commit | 3b93e1fecaa8aa76d36abd327e146c6b16cd453b (patch) | |
tree | 936d7a707ecc93ca62d21759b29df0d5fa7a6385 /src | |
parent | 4554a10bcafce6fb14eb8b123bf76887bb6a8114 (diff) | |
parent | 51800ce0e83daeadf68a90bef4d64734e4721f3a (diff) | |
download | scala-3b93e1fecaa8aa76d36abd327e146c6b16cd453b.tar.gz scala-3b93e1fecaa8aa76d36abd327e146c6b16cd453b.tar.bz2 scala-3b93e1fecaa8aa76d36abd327e146c6b16cd453b.zip |
Merge pull request #4304 from adriaanm/rebase-4219
SI-8818 FreshName extractor forgives suffix
Diffstat (limited to 'src')
-rw-r--r-- | src/reflect/scala/reflect/internal/FreshNames.scala | 24 |
1 files changed, 14 insertions, 10 deletions
diff --git a/src/reflect/scala/reflect/internal/FreshNames.scala b/src/reflect/scala/reflect/internal/FreshNames.scala index 7e9a568266..17883d12ad 100644 --- a/src/reflect/scala/reflect/internal/FreshNames.scala +++ b/src/reflect/scala/reflect/internal/FreshNames.scala @@ -7,6 +7,7 @@ package reflect package internal import scala.reflect.internal.util.FreshNameCreator +import scala.util.matching.Regex trait FreshNames { self: Names with StdNames => // SI-6879 Keeps track of counters that are supposed to be globally unique @@ -23,17 +24,20 @@ trait FreshNames { self: Names with StdNames => // Extractor that matches names which were generated by some // FreshNameCreator with known prefix. Extracts user-specified // prefix that was used as a parameter to newName by stripping - // global creator prefix and unique number in the end of the name. + // global creator prefix and unique numerical suffix. + // The creator prefix and numerical suffix may both be empty. class FreshNameExtractor(creatorPrefix: String = "") { - // quote prefix so that it can be used with replaceFirst - // which expects regExp rather than simple string - val quotedCreatorPrefix = java.util.regex.Pattern.quote(creatorPrefix) - - def unapply(name: Name): Option[String] = { - val sname = name.toString - // name should start with creatorPrefix and end with number - if (!sname.startsWith(creatorPrefix) || !sname.matches("^.*\\d*$")) None - else Some(NameTransformer.decode(sname.replaceFirst(quotedCreatorPrefix, "").replaceAll("\\d*$", ""))) + + // name should start with creatorPrefix and end with number + val freshlyNamed = { + val pre = if (!creatorPrefix.isEmpty) Regex quote creatorPrefix else "" + s"""$pre(.*?)\\d*""".r } + + def unapply(name: Name): Option[String] = + name.toString match { + case freshlyNamed(prefix) => Some(prefix) + case _ => None + } } } |