Uploaded image for project: 'Clojure'
  1. CLJ-870

clojure.string/replace behaves unexpectedly when \ or $ are part of the result string

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Minor
    • Resolution: Completed
    • Affects versions: Release 1.3, Release 1.2
    • Fix versions: Release 1.5
    • Labels:
      None
    • Environment:

      HW/OS/SW indipendant - issue is part of java interface

    • Approval:
      Ok
    • Patch:
      Code and Test

      Description

      clojure.string/replace uses javas replace function to do it's work, the replace function has the tricky habit of 'double evaluating' the replacement string (third argument). This means that every \ in the string (so i.e. "
      ") is evaluated by the java code behind replace.

      Since this behavior isn't documented it can lead to confusing errors, for example (made up semi real world example to explain the issue):

      (clojure.string/replace "c:/windows/" #"/" "\\")

      This should replace all unix folder separators with windows separators, this crashes with a index out of bound exemption since java tries to evaluate "
      " as a semi regexp.

      or

      (println (str "\"" (clojure.string/replace "my string with \\ and \" in it" #"[\"\\]" (fn [x] (str "\\" x))) "\""))

      The expected result would be:

      "my string with \\ and \" in it"

      the actual result is:

      "my string with \ and " in it"

      This should return an 'escaped' string, it does not since the 'double evaluation' of the return string results in \\\\ being reduced to
      again.

        Attachments

          Activity

            People

            • Assignee:
              stuart.sierra Stuart Sierra
              Reporter:
              alex+import import
            • Votes:
              2 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: