From 75fbef7e1c6ecf2d79dd36a62863875fc89127f2 Mon Sep 17 00:00:00 2001 From: Martin Odersky Date: Fri, 15 Feb 2013 16:04:29 +0100 Subject: PreNames as a way to unify Names and Strings. --- src/dotty/tools/dotc/core/Decorators.scala | 2 +- src/dotty/tools/dotc/core/Names.scala | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/dotty/tools/dotc/core/Decorators.scala b/src/dotty/tools/dotc/core/Decorators.scala index e3679266a..b45c1ee76 100644 --- a/src/dotty/tools/dotc/core/Decorators.scala +++ b/src/dotty/tools/dotc/core/Decorators.scala @@ -8,7 +8,7 @@ import Contexts._, Names._ object Decorators { - implicit class StringDecorator(val s: String) extends AnyVal { + implicit class StringDecorator(val s: String) extends AnyVal with PreName { def toTypeName: TypeName = typeName(s) def toTermName: TermName = termName(s) def toEncodedTypeName = encodedTypeName(s) diff --git a/src/dotty/tools/dotc/core/Names.scala b/src/dotty/tools/dotc/core/Names.scala index abf902a1c..8a13964e3 100644 --- a/src/dotty/tools/dotc/core/Names.scala +++ b/src/dotty/tools/dotc/core/Names.scala @@ -13,6 +13,11 @@ import collection.generic.CanBuildFrom object Names { + trait PreName extends Any { + def toTypeName: TypeName + def toTermName: TermName + } + /** A name is essentially a string, with three differences * 1. Names belong in one of two universes: they are type names or term names. * The same string can correspond both to a type name and to a term name. @@ -22,6 +27,7 @@ object Names { * The encoding will be applied when converting a string to a name. */ abstract class Name extends DotClass + with PreName with Seq[Char] with IndexedSeqOptimized[Char, Name] { @@ -82,6 +88,16 @@ object Names { fromChars(s.toCharArray, 0, s.length) } + def replace(from: Char, to: Char): ThisName = { + val cs = new Array[Char](length) + Array.copy(chrs, start, cs, 0, length) + for (i <- 0 until length) { + val c = cs(i) + chrs(i) = if (c == from) to else c + } + fromChars(cs, 0, length) + } + // ----- Collections integration ------------------------------------- override protected[this] def thisCollection: WrappedString = new WrappedString(repr.toString) -- cgit v1.2.3