Clojure

clojure.string/replace-first returns nil with replacement fn when regex doesn't match

Details

  • Type: Defect Defect
  • Status: Closed Closed
  • Priority: Minor Minor
  • Resolution: Completed
  • Affects Version/s: None
  • Fix Version/s: Backlog
  • Component/s: None
  • Labels:
    None
  • Patch:
    Code and Test
  • Approval:
    Vetted

Description

Originally reported by Takahiro Hozumi

With a function as the "replacement" argument, clojure.string/replace-first returns nil if there is no match, instead of returning the original string unchanged.

user=> (use 'clojure.string)
nil
user=> (replace-first "abcdef" "ghi" "jkl")
"abcdef"
user=> (replace-first "abcdef" #"ghi" "jkl")
"abcdef"
user=> (replace-first "abcdef" #"ghi" (fn [a] "jkl"))
nil

Activity

Hide
Federico Brubacher added a comment -

This is my patch for this issue. I have a CA signed. Any suggestions and i can try again. Federico

Show
Federico Brubacher added a comment - This is my patch for this issue. I have a CA signed. Any suggestions and i can try again. Federico
Federico Brubacher made changes -
Field Original Value New Value
Attachment 0001-clojure.string-replace-first-now-returns-the-origina.patch [ 10157 ]
Hide
Chris Perkins added a comment -

Same fix as Frederico's patch, but also removes unnecessary allocation of a StringBuffer when there is no match, for both replace and replace first.

Show
Chris Perkins added a comment - Same fix as Frederico's patch, but also removes unnecessary allocation of a StringBuffer when there is no match, for both replace and replace first.
Chris Perkins made changes -
Hide
Chris Perkins added a comment -

Same again, but with docstring typo fixes too.

Show
Chris Perkins added a comment - Same again, but with docstring typo fixes too.
Chris Perkins made changes -
Hide
Steve Miner added a comment -

You should use StringBuilder instead of StringBuffer.

http://stackoverflow.com/questions/355089/stringbuilder-and-stringbuffer-in-java

Show
Steve Miner added a comment - You should use StringBuilder instead of StringBuffer. http://stackoverflow.com/questions/355089/stringbuilder-and-stringbuffer-in-java
Hide
Chris Perkins added a comment -

Yup, that's what I thought, but it turns out it doesn't work because java's Matcher/appendReplacement requires a StringBuffer, unfortunately.

Show
Chris Perkins added a comment - Yup, that's what I thought, but it turns out it doesn't work because java's Matcher/appendReplacement requires a StringBuffer, unfortunately.
Hide
Andy Fingerhut added a comment - - edited

I've tested Chris's clojure-string-with-no-match-returns-original-2.patch against latest github Clojure as of this morning, and the tests pass. Cool that he found a straightforward performance improvement for the no match case. The code appears correct. I also verified that for the reason Chris mentions, StringBuilder will not work as a replacement for StringBuffer. At least there will never be any contention on the locks implementing the StringBuffer synchronization the way they are used here.

Show
Andy Fingerhut added a comment - - edited I've tested Chris's clojure-string-with-no-match-returns-original-2.patch against latest github Clojure as of this morning, and the tests pass. Cool that he found a straightforward performance improvement for the no match case. The code appears correct. I also verified that for the reason Chris mentions, StringBuilder will not work as a replacement for StringBuffer. At least there will never be any contention on the locks implementing the StringBuffer synchronization the way they are used here.
Hide
Stuart Sierra added a comment -

Vetted & moved to Approved Backlog.

Show
Stuart Sierra added a comment - Vetted & moved to Approved Backlog.
Stuart Sierra made changes -
Patch Code and Test [ 10002 ]
Approval Vetted [ 10003 ]
Affects Version/s Backlog [ 10035 ]
Fix Version/s Backlog [ 10035 ]
Fix Version/s Approved Backlog [ 10034 ]
Hide
Andy Fingerhut added a comment -

CLJ-870 has an attached proposed combined patch for this issue and that one.

Show
Andy Fingerhut added a comment - CLJ-870 has an attached proposed combined patch for this issue and that one.
Fogus made changes -
Assignee Stuart Sierra [ stuart.sierra ] Fogus [ fogus ]
Fogus made changes -
Assignee Fogus [ fogus ] Stuart Sierra [ stuart.sierra ]
Hide
Andy Fingerhut added a comment -

This issue should now be corrected with the patch, attached to CLJ-870, that was applied on Aug 18, 2012.

Show
Andy Fingerhut added a comment - This issue should now be corrected with the patch, attached to CLJ-870, that was applied on Aug 18, 2012.
Hide
Stuart Sierra added a comment -

CLJ-753 patch applied for CLJ-870 addresses this one, too, applied Aug 18, 2012

Show
Stuart Sierra added a comment - CLJ-753 patch applied for CLJ-870 addresses this one, too, applied Aug 18, 2012
Stuart Sierra made changes -
Resolution Completed [ 1 ]
Status Open [ 1 ] Resolved [ 5 ]
Stuart Halloway made changes -
Status Resolved [ 5 ] Closed [ 6 ]
Alex Miller made changes -
Fix Version/s Approved Backlog [ 10034 ]
Fix Version/s Backlog [ 10035 ]

People

Vote (2)
Watch (2)

Dates

  • Created:
    Updated:
    Resolved: