Skip to content

Fix #3788 escape backslash for char and string show #4107

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 21 additions & 27 deletions library/src/dotty/Show.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,42 +20,36 @@ object Show extends LowPrioShow {
ev.show(v)
}

implicit val stringShow: Show[String] = new Show[String] {
/** Adds escaping backslashes in a string so that it could be put in source code.
* For example, a newline character is shown as '\n' (without the quotes).
* Chars that don't have to be escaped are simply converted to a string.
* @param c the char to be escaped
* @return a string describing how to escape the give char in source code.
*/
private def escapeChar(c: Char): String = c match {
// From 2.12 spec, `charEscapeSeq`:
// ‘\‘ (‘b‘ | ‘t‘ | ‘n‘ | ‘f‘ | ‘r‘ | ‘"‘ | ‘'‘ | ‘\‘)
def show(str: String) = {
val sb = new StringBuilder
sb.append("\"")
str.foreach {
case '\b' => sb.append("\\b")
case '\t' => sb.append("\\t")
case '\n' => sb.append("\\n")
case '\f' => sb.append("\\f")
case '\r' => sb.append("\\r")
case '\'' => sb.append("\\'")
case '\"' => sb.append("\\\"")
case c => sb.append(c)
}
sb.append("\"")
sb.toString
}
case '\b' => "\\b"
case '\t' => "\\t"
case '\n' => "\\n"
case '\f' => "\\f"
case '\r' => "\\r"
case '\'' => "\\'"
case '\"' => "\\\""
case '\\' => "\\\\"
case c => c.toString
}

implicit val stringShow: Show[String] = new Show[String] {
def show(str: String) = str.flatMap(escapeChar).mkString("\"", "", "\"")
}

implicit val floatShow: Show[Float] = new Show[Float] {
def show(f: Float) = f + "f"
}

implicit val charShow: Show[Char] = new Show[Char] {
def show(c: Char) = "'" + (c match {
case '\b' => "\\b"
case '\t' => "\\t"
case '\n' => "\\n"
case '\f' => "\\f"
case '\r' => "\\r"
case '\'' => "\\'"
case '\"' => "\\\""
case c => c
}) + "'"
def show(c: Char) = s"'${escapeChar(c)}'"
}

implicit def showList[T](implicit st: Show[T]): Show[List[T]] = new Show[List[T]] {
Expand Down
4 changes: 3 additions & 1 deletion library/test/dotty/ShowTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ class ShowTests {
import Show._

@Test def showString = {
assertEquals(""""\\"""", "\\".show)
assertEquals("\"\\thello world!\"", "\thello world!".show)
assertEquals("\"\\nhello world!\"", "\nhello world!".show)
assertEquals("\"\\rhello world!\"", "\rhello world!".show)
assertEquals("\"\\b\\t\\n\\f\\r\\\'\\\"\"", "\b\t\n\f\r\'\"".show)
assertEquals(""""\b\t\n\f\r\'\"\\"""", "\b\t\n\f\r\'\"\\".show)
}

@Test def showFloat = {
Expand All @@ -31,6 +32,7 @@ class ShowTests {
assertEquals("'\\r'", '\r'.show)
assertEquals("'\\''", '\''.show)
assertEquals("'\\\"'", '\"'.show)
assertEquals("'\\\\'", '\\'.show)
}

@Test def showCar = {
Expand Down