Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala raw strings error in unicode escape

In a Scala String need to include this literal verbatim: \usepackage{x}. Thus, desired would be that for

val s = """ ... \usepackage{X} ... """

println(s)
... \usepackage{X} ...

Attempts so far include,

scala> """\usepackage{X}"""
<console>:1: error: error in unicode escape
       """\usepackage{X}"""
            ^

scala> raw"""\usepackage{X}"""
<console>:1: error: error in unicode escape
       raw"""\usepackage{X}"""
               ^

Single double-quoted strings prove unsuccessful as well.

Following http://docs.scala-lang.org/overviews/core/string-interpolation.html , a working example includes

scala> raw"a\nb"
res1: String = a\nb

which does not cover unicode cases.

like image 593
elm Avatar asked Jun 05 '14 11:06

elm


People also ask

What are unicode escapes?

A unicode escape sequence is a backslash followed by the letter 'u' followed by four hexadecimal digits (0-9a-fA-F). It matches a character in the target sequence with the value specified by the four digits. For example, ”\u0041“ matches the target sequence ”A“ when the ASCII character encoding is used.

What is string literal in Scala?

A string literal is a sequence of characters in double quotes. The characters are either printable unicode character or are described by escape sequences. If the string literal contains a double quote character, it must be escaped, i.e. "\"" . The value of a string literal is an instance of class String .

What escape sequence defines a unicode character in Java?

According to section 3.3 of the Java Language Specification (JLS) a unicode escape consists of a backslash character (\) followed by one or more 'u' characters and four hexadecimal digits.


1 Answers

You appear to be facing issue SI-4706: Unicode literal syntax thwarts common use cases for triple-quotes.

In Scala, unicode escape sequences are processed not only inside character or string literals. It may not be obvious that the following code would work:

scala> 5 \u002B 10
res0: Int = 15

Unfortunately, there doesn't seem to be a good way around this if you don't want to disable unicode escapes completely (-Xno-uescape, only available until Scala 2.13.1, see PR #8282 and ee8c1ef8).

One of workarounds suggested in the SI-4706 issue is separating the backslash character:

scala> """\""" + """usepackage{X}"""
res1: String = \usepackage{X}
like image 137
izstas Avatar answered Oct 01 '22 18:10

izstas