Clojure

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

Details

  • Type: Defect Defect
  • Status: Closed Closed
  • Priority: Minor Minor
  • Resolution: Completed
  • Affects Version/s: Release 1.2, Release 1.3
  • Fix Version/s: Release 1.5
  • Component/s: None
  • Labels:
    None
  • Environment:
    HW/OS/SW indipendant - issue is part of java interface
  • Patch:
    Code and Test
  • Approval:
    Ok

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.

  1. 0001-Add-tests-for-870.patch
    05/Jan/12 8:53 PM
    0.9 kB
    Alan Malloy
  2. 0002-Fix-870-by-using-quoteReplacement.patch
    05/Jan/12 8:53 PM
    1 kB
    Alan Malloy
  3. CLJ-753-870-905-combined-fix3.patch
    28/Feb/12 12:38 PM
    9 kB
    Andy Fingerhut
  4. CLJ-753-870-905-combined-fix3.readme.txt
    28/Feb/12 12:38 PM
    3 kB
    Andy Fingerhut
  5. clj-753-870-905-combined-fix4.patch
    15/Aug/12 11:19 AM
    9 kB
    Andy Fingerhut

Activity

Stuart Sierra made changes -
Field Original Value New Value
Patch Code and Test [ 10002 ]
Approval Vetted [ 10003 ]
Affects Version/s Release 1.3 [ 10038 ]
Fix Version/s Backlog [ 10035 ]
Alan Malloy made changes -
Andy Fingerhut made changes -
Attachment CLJ-870-also-fix-replace-first.patch [ 10762 ]
Andy Fingerhut made changes -
Attachment CLJ-753-870-905-combined-fix.patch [ 10768 ]
Stuart Sierra made changes -
Assignee Stuart Sierra [ stuart.sierra ]
Andy Fingerhut made changes -
Attachment CLJ-753-870-905-combined-fix2.patch [ 10801 ]
Andy Fingerhut made changes -
Patch Code and Test [ 10002 ]
Andy Fingerhut made changes -
Patch Code and Test [ 10002 ]
Andy Fingerhut made changes -
Patch Code and Test [ 10002 ]
Andy Fingerhut made changes -
Andy Fingerhut made changes -
Attachment CLJ-753-870-905-combined-fix2.patch [ 10801 ]
Andy Fingerhut made changes -
Attachment CLJ-753-870-905-combined-fix.patch [ 10768 ]
Andy Fingerhut made changes -
Attachment CLJ-870-also-fix-replace-first.patch [ 10762 ]
Rich Hickey made changes -
Fix Version/s Backlog [ 10035 ]
Fix Version/s Release 1.5 [ 10150 ]
Aaron Bedra made changes -
Approval Vetted [ 10003 ] Incomplete [ 10006 ]
Andy Fingerhut made changes -
Andy Fingerhut made changes -
Approval Incomplete [ 10006 ] Vetted [ 10003 ]
Fogus made changes -
Assignee Stuart Sierra [ stuart.sierra ] Fogus [ fogus ]
Fogus made changes -
Assignee Fogus [ fogus ] Stuart Sierra [ stuart.sierra ]
Stuart Sierra made changes -
Waiting On richhickey
Approval Vetted [ 10003 ] Screened [ 10004 ]
Rich Hickey made changes -
Approval Screened [ 10004 ] Ok [ 10007 ]
Stuart Halloway made changes -
Resolution Completed [ 1 ]
Status Open [ 1 ] Closed [ 6 ]

People

Vote (2)
Watch (3)

Dates

  • Created:
    Updated:
    Resolved: