summaryrefslogtreecommitdiff
path: root/src/compiler/scala/tools/nsc/interpreter/ReplStrings.scala
diff options
context:
space:
mode:
authorPaul Phillips <paulp@improving.org>2011-04-11 03:04:47 +0000
committerPaul Phillips <paulp@improving.org>2011-04-11 03:04:47 +0000
commit6b7ff287fcd56109d1db51ff82fce0ed0cdbcca6 (patch)
tree4b9bae5d44cf3f26f8d7063570d6c3d86c0a8a74 /src/compiler/scala/tools/nsc/interpreter/ReplStrings.scala
parent35f82e66d1fd3aa0d3d9171a1ab92b8a00e4f7bb (diff)
downloadscala-6b7ff287fcd56109d1db51ff82fce0ed0cdbcca6.tar.gz
scala-6b7ff287fcd56109d1db51ff82fce0ed0cdbcca6.tar.bz2
scala-6b7ff287fcd56109d1db51ff82fce0ed0cdbcca6.zip
Improving the repl help infrastructure, and mad...
Improving the repl help infrastructure, and made the :wrap command more robust. No review.
Diffstat (limited to 'src/compiler/scala/tools/nsc/interpreter/ReplStrings.scala')
-rw-r--r--src/compiler/scala/tools/nsc/interpreter/ReplStrings.scala45
1 files changed, 45 insertions, 0 deletions
diff --git a/src/compiler/scala/tools/nsc/interpreter/ReplStrings.scala b/src/compiler/scala/tools/nsc/interpreter/ReplStrings.scala
new file mode 100644
index 0000000000..cef46c963a
--- /dev/null
+++ b/src/compiler/scala/tools/nsc/interpreter/ReplStrings.scala
@@ -0,0 +1,45 @@
+/* NSC -- new Scala compiler
+ * Copyright 2005-2011 LAMP/EPFL
+ * @author Paul Phillips
+ */
+
+package scala.tools.nsc
+package interpreter
+
+import scala.collection.{ mutable, immutable }
+import scala.PartialFunction.cond
+import scala.reflect.NameTransformer
+import util.Chars
+
+trait ReplStrings {
+ // Longest common prefix
+ def longestCommonPrefix(xs: List[String]): String = {
+ if (xs.isEmpty || xs.contains("")) ""
+ else xs.head.head match {
+ case ch =>
+ if (xs.tail forall (_.head == ch)) "" + ch + longestCommonPrefix(xs map (_.tail))
+ else ""
+ }
+ }
+ /** Convert a string into code that can recreate the string.
+ * This requires replacing all special characters by escape
+ * codes. It does not add the surrounding " marks. */
+ def string2code(str: String): String = {
+ val res = new StringBuilder
+ for (c <- str) c match {
+ case '"' | '\'' | '\\' => res += '\\' ; res += c
+ case _ if c.isControl => res ++= Chars.char2uescape(c)
+ case _ => res += c
+ }
+ res.toString
+ }
+
+ def string2codeQuoted(str: String) =
+ "\"" + string2code(str) + "\""
+
+ def any2stringOf(x: Any, maxlen: Int) =
+ "scala.runtime.ScalaRunTime.replStringOf(%s, %s)".format(x, maxlen)
+
+ def words(s: String) = s.trim split "\\s+" filterNot (_ == "") toList
+ def isQuoted(s: String) = (s.length >= 2) && (s.head == s.last) && ("\"'" contains s.head)
+}