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
Scala 2.10 String Interpolation inside a multiline String with backslash #6631
Comments
Imported From: https://issues.scala-lang.org/browse/SI-6631?orig=1 |
@paulp said: scala> s"""\"""
java.lang.StringIndexOutOfBoundsException: String index out of range: 1
at java.lang.String.charAt(String.java:658)
at scala.collection.immutable.StringOps$.apply$extension(StringOps.scala:39)
at scala.StringContext$.treatEscapes(StringContext.scala:202)
at scala.StringContext$$anonfun$s$1.apply(StringContext.scala:90) |
Santosh Gokak (santoshgokak) said: |
@paulp said: Try "c:\foo\bar\fileName.csv" in the repl. |
@Ichoran said: s"\" String interpolation treats everything as a raw string. (But the escape parser is buggy.) |
@paulp said: """\t""" and s"""\t""" Get me one string with a tab and one with a \t ? I can't believe anyone thought that was a good idea. |
@Ichoran said: In case of ambiguity, you will find a life jacket under the seat in front of you. The person to your right, however, should use their seat cushion as a floatation device. It's simple enough to fix the bug (note: octal parsing is buggy also), but the correct behavior is nonobvious without a nontrivial change to something. |
@dcsobral said: String interpolators make it possible to have any particular combination, by letting multiline be decided by the number of quotes, and escaping be decided by the interpolator. Though I do see the potential for confusion, and I'm certain plenty will arise from it, I think it was the right choice. I wouldn't be bold enough to make it, but I find it easy to support Odersky on this. :-) This particular ticket is not a bug, because there's nothing for the \ before $ to escape. If you use the raw interpolator, it will work. If you double the backslashes, it will work. Everything is according to spec, and, at most, a better error message is required. I'd call it enhancement. |
@Ichoran said: |
@Ichoran said: (1) Throws the correct exception on an error (2) Does not fail on a short octal at the end of a string (3) Uses the def treatEscapes(str: String): String = {
lazy val bldr = new java.lang.StringBuilder
val len = str.length
var start = 0
var cur = 0
var idx = 0
def output(ch: Char) = {
bldr.append(str, start, cur)
bldr append ch
start = idx
}
while (idx < len) {
cur = idx
if (str(idx) == '\\') {
idx += 1
if (idx >= len) throw new InvalidEscapeException(str, cur)
if ('0' <= str(idx) && str(idx) <= '7') {
val leadch = str(idx)
var oct = leadch - '0'
idx += 1
if (idx < len && '0' <= str(idx) && str(idx) <= '7') {
oct = oct * 8 + str(idx) - '0'
idx += 1
if (idx < len && leadch <= '3' && '0' <= str(idx) && str(idx) <= '7') {
oct = oct * 8 + str(idx) - '0'
idx += 1
}
}
output(oct.toChar)
} else {
val ch = str(idx)
idx += 1
output {
ch match {
case 'b' => '\b'
case 't' => '\t'
case 'n' => '\n'
case 'f' => '\f'
case 'r' => '\r'
case '\"' => '\"'
case '\'' => '\''
case '\\' => '\\'
case _ => throw new InvalidEscapeException(str, cur)
}
}
}
} else {
idx += 1
}
}
if (start == 0) str
else bldr.append(str, start, idx).toString
} It does not address the issue of it being weird that |
Eran Medan (eranation) said (edited on Nov 26, 2012 4:12:23 AM UTC): |
@retronym said: To the original reporter: you should use |
@retronym said: Santosh: you can use But beware, |
@adriaanm said: |
picked up from http://stackoverflow.com/questions/13296411/is-this-a-bug-in-scala-2-10-string-interpolation-inside-a-multiline-string-with.
Here is the output from scala repl.
Welcome to Scala version 2.10.0-RC1 (Java HotSpot(TM) 64-Bit Server VM, Java 1.6
.0_33).
Type in expressions to have them evaluated.
Type :help for more information.
scala> val fileName = "test"
fileName: String = test
scala> val path = s"""c:\foo\bar$fileName.csv"""
java.lang.StringIndexOutOfBoundsException: String index out of range: 11
at java.lang.String.charAt(String.java:686)
at scala.collection.immutable.StringOps$.apply$extension(StringOps.scala
:39)
at scala.StringContext$.treatEscapes(StringContext.scala:202)
at scala.StringContext$$anonfun$s$1.apply(StringContext.scala:90)
at scala.StringContext$$anonfun$s$1.apply(StringContext.scala:90)
at scala.StringContext.standardInterpolator(StringContext.scala:120)
at scala.StringContext.s(StringContext.scala:90)
at .(:8)
at .()
at .(:7)
at .()
...tuncated
scala> val path = s"""c:\foo\bar$fileName.csv"""
path: String = c:♀oo?artest.csv
scala>
The text was updated successfully, but these errors were encountered: