Carp/core/Format.carp

29 lines
1.5 KiB
Plaintext
Raw Normal View History

2018-05-09 01:43:07 +03:00
(private fmt-internal)
(hidden fmt-internal)
2017-12-26 21:38:48 +03:00
(defdynamic fmt-internal [s args]
(let [idx (String.index-of s \%)
len (String.length s)]
(if (= idx -1)
(list 'copy s) ; no more splits found, just return string
2017-12-30 16:45:27 +03:00
(if (= \% (String.char-at s (inc idx))) ; this is an escaped %
(list 'StringCopy.append
(list 'copy "%")
(fmt-internal (String.substring s (+ idx 2) len) args))
(if (= 0 (length args)) ; we need to insert something, but have nothing
(macro-error "error in format string: not enough arguments to format string")
2017-12-30 16:45:27 +03:00
; okay, this is the meat:
; get the next % after our escaper
(let [next (String.index-of (String.substring s (inc idx) len) \%)]
2017-12-30 20:21:59 +03:00
(if (= -1 next)
(if (< 1 (length args))
(macro-error "error in format string: too many arguments to format string")
(list 'format s (car args)))
2017-12-30 17:04:01 +03:00
(let [slice (String.substring s 0 (+ (inc idx) next))]
(list 'StringCopy.append (list 'format slice (car args))
(fmt-internal (String.substring s (+ (inc idx) next) len)
(cdr args)))))))))))
2017-12-26 21:38:48 +03:00
2018-05-09 01:43:07 +03:00
(doc fmt "fmt formats a string. It supports all of the string interpolations defined in format of the type that should be interpolated (e.g. %d and %x on integers).")
2017-12-30 17:04:01 +03:00
(defmacro fmt [s :rest args]
2017-12-26 21:38:48 +03:00
(fmt-internal s args))