From acc2b198692687394c9f8f84b16a0bec9ae12ee3 Mon Sep 17 00:00:00 2001 From: Martin Odersky Date: Fri, 24 May 2013 16:37:47 +0200 Subject: NameTransformer.encode now goes from names to names. Also, special treatment of , which is not encoded. --- src/dotty/tools/dotc/core/Decorators.scala | 2 -- src/dotty/tools/dotc/core/Names.scala | 12 +++++------- src/dotty/tools/dotc/core/StdNames.scala | 3 ++- src/dotty/tools/dotc/util/NameTransformer.scala | 15 +++++++++------ 4 files changed, 16 insertions(+), 16 deletions(-) (limited to 'src') diff --git a/src/dotty/tools/dotc/core/Decorators.scala b/src/dotty/tools/dotc/core/Decorators.scala index 5e2da43e5..031974dfd 100644 --- a/src/dotty/tools/dotc/core/Decorators.scala +++ b/src/dotty/tools/dotc/core/Decorators.scala @@ -12,8 +12,6 @@ object Decorators { implicit class StringDecorator(val s: String) extends AnyVal with PreName { def toTypeName: TypeName = typeName(s) def toTermName: TermName = termName(s) - def toEncodedTypeName = encodedTypeName(s) - def toEncodedTermName = encodedTermName(s) def toText(printer: Printer): Text = Str(s) } diff --git a/src/dotty/tools/dotc/core/Names.scala b/src/dotty/tools/dotc/core/Names.scala index e5535cff3..8cc07d5b5 100644 --- a/src/dotty/tools/dotc/core/Names.scala +++ b/src/dotty/tools/dotc/core/Names.scala @@ -111,7 +111,8 @@ object Names { else this /** Replace operator symbols by corresponding \$op_name's. */ - def encode: Name = fromString(NameTransformer.encode(toString)) + def encode: Name = + if (this eq CONSTRUCTOR) this else NameTransformer.encode(this) /** A more efficient version of concatenation */ def ++ (other: Name): ThisName = ++ (other.toString) @@ -371,15 +372,9 @@ object Names { /** Create a term name from a string, without encoding operators */ def termName(s: String): TermName = termName(s.toCharArray, 0, s.length) - /** Create a term name from a string, encode if necessary*/ - def encodedTermName(s: String): TermName = termName(NameTransformer.encode(s)) - /** Create a type name from a string, wihtout encoding operators */ def typeName(s: String): TypeName = typeName(s.toCharArray, 0, s.length) - /** Create a type name from a string, encode if necessary*/ - def encodedTypeName(s: String): TypeName = typeName(NameTransformer.encode(s)) - /** The term name represented by the empoty string */ val EmptyTermName = new TermName(-1, 0, null) @@ -388,6 +383,9 @@ object Names { /** The type name represented by the empoty string */ val EmptyTypeName = EmptyTermName.toTypeName + // can't use nme.CONSTRUCTOR in encode because of bootstrap failures. + private val CONSTRUCTOR = termName("") + def termNameBuilder: Builder[Char, TermName] = StringBuilder.newBuilder.mapResult(termName) diff --git a/src/dotty/tools/dotc/core/StdNames.scala b/src/dotty/tools/dotc/core/StdNames.scala index ea671ed8a..ada74c69d 100644 --- a/src/dotty/tools/dotc/core/StdNames.scala +++ b/src/dotty/tools/dotc/core/StdNames.scala @@ -17,6 +17,7 @@ object StdNames { abstract class DefinedNames[N <: Name] { protected implicit def fromString(s: String): N + protected def fromName(name: Name): N = fromString(name.toString) private val kws = mutable.Set[N]() protected def kw(name: N) = { kws += name; name } @@ -25,7 +26,7 @@ object StdNames { } abstract class ScalaNames[N <: Name] extends DefinedNames[N] { - private def encode(s: String): N = fromString(NameTransformer.encode(s)) + private def encode(s: String): N = fromName(fromString(s).encode) // Keywords, need to come first ----------------------- diff --git a/src/dotty/tools/dotc/util/NameTransformer.scala b/src/dotty/tools/dotc/util/NameTransformer.scala index 287c7660c..be77dd9a6 100644 --- a/src/dotty/tools/dotc/util/NameTransformer.scala +++ b/src/dotty/tools/dotc/util/NameTransformer.scala @@ -9,6 +9,9 @@ package dotty.tools.dotc package util +import core.Names._ +import core.Decorators._ + /** Provides functions to encode and decode Scala symbolic names. * Also provides some constants. */ @@ -57,16 +60,16 @@ object NameTransformer { * @param name the string to encode * @return the string with all recognized opchars replaced with their encoding */ - def encode(name: String): String = { + def encode(name: Name): TermName = { var buf: StringBuilder = null - val len = name.length() + val len = name.length var i = 0 while (i < len) { - val c = name charAt i + val c = name(i) if (c < nops && (op2code(c) ne null)) { if (buf eq null) { buf = new StringBuilder() - buf.append(name.substring(0, i)) + buf.append(name.slice(0, i)) } buf.append(op2code(c)) /* Handle glyphs that are not valid Java/JVM identifiers */ @@ -74,7 +77,7 @@ object NameTransformer { else if (!Character.isJavaIdentifierPart(c)) { if (buf eq null) { buf = new StringBuilder() - buf.append(name.substring(0, i)) + buf.append(name.slice(0, i)) } buf.append("$u%04X".format(c.toInt)) } @@ -83,7 +86,7 @@ object NameTransformer { } i += 1 } - if (buf eq null) name else buf.toString() + if (buf eq null) name.toTermName else buf.toString.toTermName } /** Replace `\$opname` by corresponding operator symbol. -- cgit v1.2.3