Scala Programming Language
  1. Scala Programming Language
  2. SI-5614

SIP-11 (String interpolation): escape characters in processed strings are (somehow) interpreted

    Details

    • Type: Bug Bug
    • Status: Closed
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: Scala 2.10.0-M2
    • Fix Version/s: Scala 2.10.0-M2
    • Component/s: Misc Library
    • Labels:
      None
    • Environment:

      % scala -version -Xexperimental
      Scala code runner version 2.10.0-M2 – Copyright 2002-2011, LAMP/EPFL

      Description

      The interpretation of StringContext.s is strange when applied to the string literal s"a\nb". According to SIP-11 (String Interpolation) the standard interpolation should replace every escape character sequence by the denoted character, therefore I expected a result of length 3.

      scala> s"a\nb"
      res0: String =
      a\nb
      b
      
      scala> res0.length
      res1: Int = 6
      

        Activity

        Hide
        Dominik Gruntz added a comment -

        Fix:

        Change (in method treatEscapes, line 146 of File StringContext.scala)

            def output(ch: Char) = {
              bldr append str substring (start, cur)
              bldr append ch
              start = idx
            }
        

        to

            def output(ch: Char) = {
              bldr append str.substring (start, cur)
              bldr append ch
              start = idx
            }
        

        Explanation: in the current version, str is appended to the buffer, and then on the buffer substring is called (which has no effect), i.e. without sugar:
        bldr.append(str).subsring(start, cur)

        Show
        Dominik Gruntz added a comment - Fix: Change (in method treatEscapes, line 146 of File StringContext.scala) def output(ch: Char) = { bldr append str substring (start, cur) bldr append ch start = idx } to def output(ch: Char) = { bldr append str.substring (start, cur) bldr append ch start = idx } Explanation: in the current version, str is appended to the buffer, and then on the buffer substring is called (which has no effect), i.e. without sugar: bldr.append(str).subsring(start, cur)
        Hide
        Christopher Vogt added a comment -

        Can you please create a pull request including a test? Ask me if you have questions. Thx

        Show
        Christopher Vogt added a comment - Can you please create a pull request including a test? Ask me if you have questions. Thx
        Show
        Dominik Gruntz added a comment - fixed with https://github.com/scala/scala/commit/017f48e00b863ecfc6a39c16c89a6ccb1dcde13d

          People

          • Assignee:
            Dominik Gruntz
            Reporter:
            Dominik Gruntz
          • Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development