Home

Awesome

stringer

A Clojure library for fast string operations. (See the Development section to run performance benchmarks.)

Installation

Leiningen coordinates: [stringer "0.4.1"]

Usage

Requiring namespace

(require '[stringer.core :as s])

String concatenation

strcat is a macro that accepts arguments to concatenate as a string.

(s/strcat "foo" :bar 707 nil 'baz)
=> "foo:bar707baz"

Joining string tokens by delimiter

strdel is a macro that accepts delimiter as first argument, and tokens as the remaining arguments.

(s/strdel ", " "foo" :bar 707 nil 'baz)
=> "foo, :bar, 707, , baz"

Formatting string

Positional parameters

strfmt is a macro that accepts format-string as first argument, and format parameters as arguments.

(s/strfmt "Hello %s, here are %d products costing %f each." "human" 42 23.35)
=> "Hello human, here are 42 products costing 23.350000 each."

Note: Only limited format-specifier support exists. See CHANGES.md for details.

Named parameters

nformat is a macro that accepts format-string as first argument, and format parameters as local vars or map argument.

(let [guest "human"
      nprod 42
      pcost 23.35]
  (s/nformat "Hello {guest}, here are {nprod} products costing {pcost} each."))
=> "Hello human, here are 42 products costing 23.35 each."
;; OR
(s/nformat "Hello {guest}, here are {nprod} products costing {pcost} each."
           {:guest "human"
            :nprod 42
            :pcost 23.35})
=> "Hello human, here are 42 products costing 23.35 each."

In case you need a function variant of nformat you can use nrender, which is a little slower but handles non-literal format string.

(let [format-string "Hello {guest}, here are {nprod} products costing {pcost} each."]
  (s/nrender format-string {:guest "human"
                            :nprod 42
                            :pcost 23.35}))
=> "Hello human, here are 42 products costing 23.35 each."

The fmt macro, on the other hand, prepares a function to render a format-string at a later point.

(def x (s/fmt "Hello {guest}, here are {nprod} products costing {pcost} each."))
(x {:guest "human"
    :nprod 42
    :pcost 23.35})
=> "Hello human, here are 42 products costing 23.35 each."

Work with java.lang.StringBuilder

(s/with-obj-str w
  (.append w "foo")
  (.append w 707))
=> "foo707"

There are append!, join-all! and join! macros to work with StringBuilder.

(s/with-obj-str w
  (s/append! w "foo" :bar 707 nil 'baz)
  (s/join-all! w ", " :quux :norf)
  (s/join! w \| ["quick" "brown" "fox"]))
=> "foo:bar707baz:quux, :norfquick|brown|fox"

The local can be bound to something else too:

(s/with-obj-str [w (java.util.ArrayList.)]
  (.add w 1)
  (.add w 2)
  (.add w 3))
=> "[1, 2, 3]"

Printing tables

The strtbl function generates string representation of a textual table equivalent to clojure.pprint/print-table.

(println (s/strtbl
           [{:name "Bill" :age 32 :gender :male}
            {:name "Anna" :age 47 :gender :female}
            {:name "Ravi" :age 39 :gender :male}]))

Caveats

Development

Running performance benchmarks

With Clojure 1.8: lein with-profile c08,perf do clean, test

With Clojure 1.9: lein with-profile c09,perf do clean, test

With Clojure 1.10: lein with-profile c10,perf do clean, test

To run with Clojure 1.8, 1.9 and 1.10 in order: lein cascade perf

If you are running the tests on a laptop, connect it to the power supply (so that the CPU is not clocked down) and turn the screensaver/suspend off.

License

Copyright © 2015-2019 Shantanu Kumar

Distributed under the Eclipse Public License either version 1.0 or (at your option) any later version.