Add clojure.string functions for portability to ClojureScript

Description

It would be useful if a few common functions from Java's String were available as Clojure functions for the purposes of increasing portability to other Clojure platforms like ClojureScript.

The functions below also cover the vast majority of cases where Clojure users currently drop into Java interop for String calls - this tends to be an issue for discoverability and learning. While the goal of this ticket is increased portability, improving that is a nice secondary benefit.

Proposed clojure.string fn

java.lang.String method

index-of

indexOf

last-index-of

lastIndexOf

starts-with?

startsWith

ends-with?

endsWith

includes?

contains

Patch:

  • clj-1449-7.patch - uses nil to indicate not-found in index-of

Performance: Tested the following with criterium for execution mean time (all times in ns).

Java

Clojure

Java

Clojure (-4 patch)

Clojure (-6 patch)

(.indexOf "banana" "n")

(clojure.string/index-of "banana" "n")

8.70

9.03

9.27

(.indexOf "banana" "n" 1)

(clojure.string/index-of "banana" "n" 1)

7.29

7.61

7.66

(.indexOf "banana" (int \n))

(clojure.string/index-of "banana" \n)

5.34

6.20

6.20

(.indexOf "banana" (int \n) 1)

(clojure.string/index-of "banana" \n 1)

5.38

6.19

6.24

(.startsWith "apple" "a")

(clojure.string/starts-with? "apple" "a")

4.84

5.71

5.65

(.endsWith "apple" "e")

(clojure.string/ends-with? "apple" "e")

4.82

5.68

5.70

(.contains "baked apple pie" "apple")

(clojure.string/includes? "baked apple pie" "apple")

10.78

11.99

12.17

Screened by: Stu, who prefers nil over -1 - add_functions_to_strings-6.patch

Note: In both Java and JavaScript, indexOf will return -1 to indicate not-found, so this is (at least in these two cases) the status quo. The benefit of nil return in Clojure is that it can be used as a found/not-found condition. The benefit of a -1 return is that it matches Java/JavaScript and that the return type can be hinted as a primitive long, allowing you to feed it into some other arithmetic or string operation without boxing.

Environment

None

Attachments

8

Activity

Show:

Tom Marble September 3, 2015 at 9:46 PM

I have just proposed an analogous patch to ClojureScript under
http://dev.clojure.org/jira/browse/CLJS-1441

Alex Miller August 31, 2015 at 4:03 AM

Hi Colin, that's a good idea but you have missed the window on this ticket - feel free to file a different one.

Colin Taylor August 31, 2015 at 12:48 AM

Bit late on this - but .equalsIgnoreCase (=ic? equals-ic ? ) is currently a visible wart for our portability.

Alex Miller August 24, 2015 at 6:19 PM

jinx - same patch! Replacing with Nola's.

Alex Miller August 24, 2015 at 5:59 PM

Added clj-1449-7.patch that is identical to -6 but adds statement of return expectation on index-of and last-index-of.

Completed

Details

Assignee

Reporter

Labels

Approval

Ok

Patch

Code and Test

Priority

Affects versions

Fix versions

Created June 19, 2014 at 3:51 PM
Updated September 3, 2015 at 9:46 PM
Resolved September 3, 2015 at 9:46 PM