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

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

Environment

None

Attachments

3
  • 28 Nov 2011, 03:46 PM
  • 28 Nov 2011, 03:31 PM
  • 21 Mar 2011, 01:36 PM

Activity

Show:

Stuart Sierra August 24, 2012 at 1:41 PM

https://clojure.atlassian.net/browse/CLJ-753#icft=CLJ-753 patch applied for CLJ-870 addresses this one, too, applied Aug 18, 2012

Andy Fingerhut August 18, 2012 at 6:09 PM

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

Andy Fingerhut January 12, 2012 at 6:06 AM

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

Stuart Sierra November 29, 2011 at 2:45 PM

Vetted & moved to Approved Backlog.

Andy Fingerhut November 28, 2011 at 11:09 PM

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.

Completed

Details

Assignee

Reporter

Approval

Vetted

Patch

Code and Test

Priority

Fix versions

Created March 11, 2011 at 1:54 PM
Updated July 26, 2013 at 4:35 PM
Resolved July 26, 2013 at 4:35 PM

Flag notifications