2018-02-27 16:14:14 +03:00
|
|
|
(system-include "carp_string.h")
|
|
|
|
|
2017-06-26 12:15:03 +03:00
|
|
|
(defmodule String
|
2017-12-12 14:57:13 +03:00
|
|
|
|
2017-10-20 18:00:47 +03:00
|
|
|
(register = (Fn [&String &String] Bool))
|
2018-05-20 07:04:47 +03:00
|
|
|
(register append (Fn [&String &String] String))
|
2017-10-20 18:00:47 +03:00
|
|
|
(register delete (Fn [String] ()))
|
|
|
|
(register copy (Fn [&String] String))
|
|
|
|
(register count (Fn [&String] Int))
|
|
|
|
(register duplicate (Fn [&String] String))
|
|
|
|
(register cstr (Fn [&String] (Ptr Char)))
|
|
|
|
(register str (Fn [&String] String))
|
2018-02-06 20:37:15 +03:00
|
|
|
(register prn (Fn [&String] String))
|
2017-12-18 18:19:29 +03:00
|
|
|
(register char-at (Fn [&String Int] Char))
|
2017-10-20 18:00:47 +03:00
|
|
|
(register chars (Fn [&String] (Array Char)))
|
2018-03-12 16:56:05 +03:00
|
|
|
(register from-chars (Fn [&(Array Char)] String))
|
2017-12-04 17:25:18 +03:00
|
|
|
(register tail (λ [(Ref String)] String))
|
2017-12-30 22:16:31 +03:00
|
|
|
(register format (Fn [&String &String] String))
|
2018-05-19 09:49:48 +03:00
|
|
|
(register string-set! (Fn [&String Int Char] ()))
|
2018-05-20 06:53:30 +03:00
|
|
|
(register string-set-at! (Fn [&String Int &String] ()))
|
2018-05-20 08:18:15 +03:00
|
|
|
(register allocate (Fn [Int, Char] String))
|
2017-10-20 18:00:47 +03:00
|
|
|
|
2017-12-19 20:01:06 +03:00
|
|
|
(defn /= [a b]
|
|
|
|
(not (= (the (Ref String) a) b)))
|
|
|
|
|
2018-05-19 06:09:54 +03:00
|
|
|
(doc head "Returns the character at start of string.")
|
2017-12-18 18:06:17 +03:00
|
|
|
(defn head [s]
|
|
|
|
(char-at s 0))
|
2017-10-20 18:00:47 +03:00
|
|
|
|
2018-05-19 06:09:54 +03:00
|
|
|
(doc repeat "Returns a new string which is `inpt` repeated `n` times.")
|
2017-10-20 18:00:47 +03:00
|
|
|
(defn repeat [n inpt]
|
2017-12-23 16:30:45 +03:00
|
|
|
(let [str @""]
|
2017-10-20 18:00:47 +03:00
|
|
|
(do
|
|
|
|
(for [i 0 n]
|
2018-05-20 07:04:47 +03:00
|
|
|
(set! str (append &str inpt)))
|
2017-12-23 16:30:45 +03:00
|
|
|
str)))
|
2017-12-04 00:30:48 +03:00
|
|
|
|
2017-11-29 23:33:35 +03:00
|
|
|
(defn pad-left [len pad s]
|
2017-12-18 17:46:25 +03:00
|
|
|
(let [x (Int.max 0 (- len (count s)))]
|
2018-05-20 07:04:47 +03:00
|
|
|
(append &(from-chars &(Array.replicate x &pad)) s)))
|
2017-11-29 23:33:35 +03:00
|
|
|
|
|
|
|
(defn pad-right [len pad s]
|
2017-12-18 17:46:25 +03:00
|
|
|
(let [x (Int.max 0 (- len (count s)))]
|
2018-05-20 07:04:47 +03:00
|
|
|
(append s &(from-chars &(Array.replicate x &pad)))))
|
2017-12-18 17:46:25 +03:00
|
|
|
|
2018-05-19 06:09:54 +03:00
|
|
|
(doc count-char "Returns the number of occurrences of `c` in the string `s`.")
|
2017-12-18 17:46:25 +03:00
|
|
|
(defn count-char [s c]
|
|
|
|
(let-do [n 0]
|
|
|
|
(for [i 0 (count s)]
|
2017-12-18 18:19:29 +03:00
|
|
|
(when (= c (char-at s i))
|
2018-02-02 09:19:10 +03:00
|
|
|
(set! n (Int.inc n))))
|
2017-12-18 17:46:25 +03:00
|
|
|
n))
|
|
|
|
|
2018-05-19 06:09:54 +03:00
|
|
|
(doc reverse "Produce a new string which is `s` reversed.")
|
2017-12-18 17:46:25 +03:00
|
|
|
(defn reverse [s]
|
2018-03-12 16:56:05 +03:00
|
|
|
(from-chars &(Array.reverse (chars s))))
|
2017-11-29 23:33:35 +03:00
|
|
|
|
2018-05-19 06:09:54 +03:00
|
|
|
(doc empty? "Check if the string is the empty string.")
|
2017-11-29 23:33:35 +03:00
|
|
|
(defn empty? [s]
|
|
|
|
(Int.= (count s) 0))
|
2017-12-28 20:10:38 +03:00
|
|
|
|
2018-01-02 20:13:52 +03:00
|
|
|
(defn substring [s a b]
|
2018-03-12 16:56:05 +03:00
|
|
|
(from-chars &(Array.subarray &(chars s) a b)))
|
2018-01-02 20:13:52 +03:00
|
|
|
|
2018-05-19 06:09:54 +03:00
|
|
|
(doc prefix-string "Return the first `a` characters of the string `s`.")
|
2018-01-02 20:13:52 +03:00
|
|
|
(defn prefix-string [s a]
|
2018-03-12 16:56:05 +03:00
|
|
|
(from-chars &(Array.prefix-array &(chars s) a)))
|
2018-01-02 20:13:52 +03:00
|
|
|
|
2018-05-19 06:09:54 +03:00
|
|
|
(doc suffix-string "Return the last `b` characters of the string `s`.")
|
2018-01-02 20:13:52 +03:00
|
|
|
(defn suffix-string [s b]
|
2018-03-12 16:56:05 +03:00
|
|
|
(from-chars &(Array.suffix-array &(chars s) b)))
|
2018-01-02 20:13:52 +03:00
|
|
|
|
2018-05-19 06:09:54 +03:00
|
|
|
(doc starts-with? "Check if the string `s` begins with the string `sub`.")
|
2018-01-02 20:13:52 +03:00
|
|
|
(defn starts-with? [s sub]
|
|
|
|
(= sub &(prefix-string s (count sub))))
|
|
|
|
|
2018-05-19 06:09:54 +03:00
|
|
|
(doc starts-with? "Check if the string `s` ends with the string `sub`.")
|
2018-01-02 20:13:52 +03:00
|
|
|
(defn ends-with? [s sub]
|
2018-01-03 14:49:46 +03:00
|
|
|
(= sub &(suffix-string s (- (count s) (count sub)))))
|
2018-01-29 00:54:59 +03:00
|
|
|
|
2018-05-19 06:09:54 +03:00
|
|
|
(doc zero "The empty string.")
|
2018-01-29 00:54:59 +03:00
|
|
|
(defn zero [] @"")
|
2018-03-11 17:09:30 +03:00
|
|
|
|
2018-05-20 09:51:00 +03:00
|
|
|
(doc sum-count "Returns the sum of counts from an array of Strings.")
|
2018-05-20 09:48:20 +03:00
|
|
|
(defn sum-count [strings]
|
|
|
|
(let-do [sum 0
|
|
|
|
lstrings (Array.count strings)]
|
|
|
|
(for [i 0 lstrings]
|
|
|
|
(set! sum (+ sum (String.count (Array.nth strings i)))))
|
|
|
|
sum))
|
|
|
|
|
2018-05-19 09:02:03 +03:00
|
|
|
(doc concat "Returns a new string which is the concatenation of the provided `strings`.")
|
|
|
|
(defn concat [strings]
|
2018-05-20 09:48:20 +03:00
|
|
|
;; This is using a StringBuilder pattern to only perform one allocation and
|
|
|
|
;; to only copy each of the incoming strings once.
|
|
|
|
;; This currently performs wasted String.count calls, as we call it for each
|
|
|
|
;; string once here and once in sum-count.
|
|
|
|
(let-do [j 0
|
|
|
|
result (String.allocate (sum-count strings) \ )
|
|
|
|
lstrings (Array.count strings)]
|
|
|
|
(for [i 0 lstrings]
|
|
|
|
(let-do [str (Array.nth strings i)
|
|
|
|
len (String.count str)]
|
|
|
|
(string-set-at! &result j str)
|
|
|
|
(set! j (+ j len))))
|
|
|
|
result))
|
2018-05-19 06:17:06 +03:00
|
|
|
|
|
|
|
;; TODO: Should use some kind of "StringBuilder" instead of generating intermediate strings.
|
2018-05-19 09:02:03 +03:00
|
|
|
(doc join "Returns a new string which is the concatenation of the provided `strings` separated by `sep`.")
|
|
|
|
(defn join [sep strings]
|
2018-05-19 06:17:06 +03:00
|
|
|
(let-do [result @""
|
|
|
|
len (Array.count strings)]
|
|
|
|
(for [i 0 len]
|
|
|
|
(do
|
|
|
|
(when (> i 0)
|
2018-05-20 07:04:47 +03:00
|
|
|
(set! result (String.append &result sep)))
|
|
|
|
(set! result (String.append &result (Array.nth strings i)))))
|
2018-05-19 06:17:06 +03:00
|
|
|
result))
|
2017-11-29 23:33:35 +03:00
|
|
|
)
|
2017-10-20 18:00:47 +03:00
|
|
|
|
2017-12-22 13:17:19 +03:00
|
|
|
(defmodule StringCopy
|
2018-05-20 07:04:47 +03:00
|
|
|
(register append (Fn [String String] String))
|
|
|
|
|
2017-12-22 13:17:19 +03:00
|
|
|
(defn = [a b]
|
|
|
|
(String.= &a &b))
|
2017-12-19 19:53:27 +03:00
|
|
|
|
2017-12-22 13:17:19 +03:00
|
|
|
(defn /= [a b]
|
|
|
|
(String./= &a &b))
|
|
|
|
)
|
2017-10-20 18:00:47 +03:00
|
|
|
|
|
|
|
(defdynamic string-join- [strs]
|
|
|
|
(if (= (count strs) 0)
|
2017-12-17 17:19:31 +03:00
|
|
|
'(String.copy "")
|
2018-05-20 07:04:47 +03:00
|
|
|
(list 'StringCopy.append (car strs) (string-join- (cdr strs)))))
|
2017-10-20 18:00:47 +03:00
|
|
|
|
2018-03-11 17:09:30 +03:00
|
|
|
;; TODO: Remove this function and replace uses with 'str*'
|
2017-10-20 18:00:47 +03:00
|
|
|
(defmacro string-join [:rest strs]
|
|
|
|
(string-join- strs))
|
2018-02-06 18:48:14 +03:00
|
|
|
|
2018-02-27 16:49:06 +03:00
|
|
|
(defmodule Bool
|
|
|
|
(register str (Fn [Bool] String))
|
|
|
|
(register format (Fn [&String Bool] String))
|
|
|
|
)
|
|
|
|
|
|
|
|
(defmodule Int
|
|
|
|
(register str (Fn [Int] String))
|
|
|
|
(register format (Fn [&String Int] String))
|
|
|
|
(register from-string (λ [&String] Int))
|
|
|
|
)
|
|
|
|
|
|
|
|
(defmodule Float
|
|
|
|
(register str (Fn [Float] String))
|
|
|
|
(register format (Fn [&String Float] String))
|
|
|
|
)
|
|
|
|
|
|
|
|
(defmodule Long
|
|
|
|
(register str (Fn [Long] String))
|
|
|
|
(register format (Fn [&String Long] String))
|
|
|
|
(register from-string (λ [&String] Long))
|
|
|
|
)
|
|
|
|
|
|
|
|
(defmodule Double
|
|
|
|
(register str (Fn [Double] String))
|
|
|
|
(register format (Fn [&String Double] String))
|
|
|
|
)
|
|
|
|
|
|
|
|
(defmodule Char
|
|
|
|
(register str (Fn [Char] String))
|
|
|
|
(register format (Fn [&String Char] String))
|
|
|
|
)
|
2018-02-27 17:30:22 +03:00
|
|
|
|
|
|
|
(defmodule Int (defn prn [x] (Int.str x)))
|
|
|
|
(defmodule Long (defn prn [x] (Long.str x)))
|
|
|
|
(defmodule Float (defn prn [x] (Float.str x)))
|
|
|
|
(defmodule Double (defn prn [x] (Double.str x)))
|
|
|
|
(defmodule Bool (defn prn [x] (Bool.str x)))
|
|
|
|
(defmodule Char (defn prn [x] (Char.str x)))
|
|
|
|
(defmodule Array (defn prn [x] (Array.str x)))
|