This page is no longer maintained — Please continue to the home page at www.scala-lang.org

How should I report?

No replies
Michael Schmitz
Joined: 2011-11-01,
User offline. Last seen 42 years 45 weeks ago.

Hi, I noticed some odd behavior today.

scala> "anything".r.replaceAllIn("anything", "\\")
java.lang.StringIndexOutOfBoundsException: String index out of range: 1
at java.lang.String.charAt(String.java:694)
at java.util.regex.Matcher.appendReplacement(Matcher.java:716)
at java.util.regex.Matcher.replaceAll(Matcher.java:823)
at scala.util.matching.Regex.replaceAllIn(Regex.scala:103)
at .(:9)
at .()
at .(:11)
at .()
at $print()
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:616)
at scala.tools.nsc.interpreter.IMain$ReadEvalPrint.call(IMain.scala:704)
at scala.tools.nsc.interpreter.IMain$Request$$anonfun$14.apply(IMain.scala:920)
at scala.tools.nsc.interpreter.Line$$anonfun$1.apply$mcV$sp(Line.scala:43)
at scala.tools.nsc.io.package$$anon$2.run(package.scala:25)
at java.lang.Thread.run(Thread.java:679)

As the stacktrace suggests, Java
(Pattern.compile("anything").matcher("anything").replaceAll("\\")) has
the same behavior. This is unfortunate because replaceAllIn provides
an anonymous function to build replacement text as a function of the
match groups. And if I'm using a match group in that anonymous
function, I don't know/remember to escape my \ and $...

At the least, the behavior should be commented in the scaladoc. It is
commented in the Javadoc.

"Note that backslashes (\) and dollar signs ($) in the replacement
string may cause the results to be different than if it were being
treated as a literal replacement string. Dollar signs may be treated
as references to captured subsequences as described above, and
backslashes are used to escape literal characters in the replacement
string. "

Should I file a bug report or send a pull request that improves the
comment with the Javadoc?

Peace. Michael

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland