2018-03-02 17:29:29 +03:00
|
|
|
(system-include "carp_pattern.h")
|
|
|
|
|
|
|
|
(defmodule Pattern
|
2018-05-12 16:58:49 +03:00
|
|
|
(doc find "Finds the index of a pattern in a string. Returns -1 otherwise.")
|
2018-03-02 17:29:29 +03:00
|
|
|
(register find (Fn [&Pattern &String] Int))
|
2018-11-01 17:55:51 +03:00
|
|
|
(doc find-all "Finds all indices of a pattern in a string. Returns [] otherwise.")
|
|
|
|
(register find-all (Fn [&Pattern &String] (Array Int)))
|
2018-11-14 23:21:56 +03:00
|
|
|
(doc match-groups "Finds the match groups of the first match of a pattern in a string. Returns [] otherwise.")
|
|
|
|
(register match-groups (Fn [&Pattern &String] (Array String)))
|
2018-05-12 16:58:49 +03:00
|
|
|
(doc match-str "Finds the first match of a pattern in a string. Returns [] otherwise.")
|
2018-03-02 17:29:29 +03:00
|
|
|
(register match-str (Fn [&Pattern &String] String))
|
2018-05-12 16:58:49 +03:00
|
|
|
(doc global-match "Finds all matches of a pattern in a string as a nested array. Returns [] otherwise.")
|
2018-03-02 17:29:29 +03:00
|
|
|
(register global-match (Fn [&Pattern &String] (Array (Array String))))
|
2018-05-12 16:58:49 +03:00
|
|
|
(doc substitute "Finds all matches of a pattern in a string and replaces it by another n times (-1 for all occurrences).")
|
2018-03-02 17:29:29 +03:00
|
|
|
(register substitute (Fn [&Pattern &String &String Int] String))
|
2018-05-12 16:58:49 +03:00
|
|
|
(doc matches? "Checks whether a pattern matches a string.")
|
2018-03-02 17:29:29 +03:00
|
|
|
(defn matches? [pat s] (/= (find pat s) -1))
|
2018-03-03 16:59:12 +03:00
|
|
|
|
|
|
|
(register str (Fn [&Pattern] String))
|
|
|
|
(register prn (Fn [&Pattern] String))
|
|
|
|
|
|
|
|
(register init (Fn [&String] Pattern))
|
|
|
|
(register = (Fn [&Pattern &Pattern] Bool))
|
|
|
|
(register delete (Fn [Pattern] ()))
|
|
|
|
(register copy (Fn [&Pattern] Pattern))
|
2018-03-13 17:15:31 +03:00
|
|
|
|
2018-05-12 16:58:49 +03:00
|
|
|
(doc from-chars "Creates a pattern that matches a group of characters from a list of those characters.")
|
2018-03-13 17:15:31 +03:00
|
|
|
(defn from-chars [chars]
|
2018-06-15 17:38:34 +03:00
|
|
|
(Pattern.init &(str* @"[" (String.from-chars chars) @"]")))
|
2018-03-02 17:29:29 +03:00
|
|
|
)
|
2018-03-13 13:59:48 +03:00
|
|
|
|
|
|
|
(defmodule String
|
2018-05-12 16:58:49 +03:00
|
|
|
(doc in? "Checks whether a string contains another string.")
|
2018-03-13 13:59:48 +03:00
|
|
|
(defn in? [s sub]
|
|
|
|
(Pattern.matches? &(Pattern.init sub) s))
|
|
|
|
|
2018-05-12 16:58:49 +03:00
|
|
|
(doc upper? "Checks whether a string is all uppercase.")
|
2018-03-13 13:59:48 +03:00
|
|
|
(defn upper? [s]
|
|
|
|
(Pattern.matches? #"^[\u\s\p]*$" s))
|
|
|
|
|
2018-05-12 16:58:49 +03:00
|
|
|
(doc lower? "Checks whether a string is all lowercase.")
|
2018-03-13 13:59:48 +03:00
|
|
|
(defn lower? [s]
|
|
|
|
(Pattern.matches? #"^[\l\s\p]*$" s))
|
|
|
|
|
2018-05-12 16:58:49 +03:00
|
|
|
(doc num? "Checks whether a string is numerical.")
|
2018-03-13 13:59:48 +03:00
|
|
|
(defn num? [s]
|
|
|
|
(Pattern.matches? #"^[0-9]*$" s))
|
|
|
|
|
2018-03-27 10:05:58 +03:00
|
|
|
(doc alpha? "Check if a string contains only alpha characters (a-Z).")
|
2018-03-13 13:59:48 +03:00
|
|
|
(defn alpha? [s]
|
|
|
|
(Pattern.matches? #"^[\u\l]*$" s))
|
|
|
|
|
2018-05-12 16:58:49 +03:00
|
|
|
(doc alphanum? "Checks whether a string is alphanumerical.")
|
2018-03-13 13:59:48 +03:00
|
|
|
(defn alphanum? [s]
|
|
|
|
(Pattern.matches? #"^[\w]*$" s))
|
|
|
|
|
2018-05-12 16:58:49 +03:00
|
|
|
(doc hex? "Checks whether a string is hexadecimal.")
|
2018-03-13 13:59:48 +03:00
|
|
|
(defn hex? [s]
|
|
|
|
(Pattern.matches? #"^[\x]*$" s))
|
|
|
|
|
2018-05-12 16:58:49 +03:00
|
|
|
(doc trim-left "Trims whitespace from the left of a string.")
|
2018-03-13 13:59:48 +03:00
|
|
|
(defn trim-left [s]
|
|
|
|
(Pattern.substitute #"^\s+" s "" 1))
|
|
|
|
|
2018-05-12 16:58:49 +03:00
|
|
|
(doc trim-right "Trims whitespace from the right of a string.")
|
2018-03-13 13:59:48 +03:00
|
|
|
(defn trim-right [s]
|
|
|
|
(Pattern.substitute #"\s+$" s "" 1))
|
|
|
|
|
2018-05-12 16:58:49 +03:00
|
|
|
(doc trim "Trims whitespace from both sides of a string.")
|
2018-03-13 13:59:48 +03:00
|
|
|
(defn trim [s]
|
|
|
|
(trim-left &(trim-right s)))
|
|
|
|
|
2018-05-12 16:58:49 +03:00
|
|
|
(doc chomp "Trims newline from the end of a string.")
|
2018-03-13 13:59:48 +03:00
|
|
|
(defn chomp [s]
|
|
|
|
(Pattern.substitute #"\n$" s "" 1))
|
|
|
|
|
2018-05-12 16:58:49 +03:00
|
|
|
(doc collapse-whitespace "Collapse groups of whitespace into single spaces.")
|
2018-03-13 13:59:48 +03:00
|
|
|
(defn collapse-whitespace [s]
|
|
|
|
(Pattern.substitute #"\s+" s " " -1))
|
2018-03-13 17:15:31 +03:00
|
|
|
|
2018-05-12 16:58:49 +03:00
|
|
|
(doc split-by "Split a string by separators.")
|
2018-11-01 17:55:51 +03:00
|
|
|
(defn split-by [s separators]
|
|
|
|
(let-do [pat (Pattern.from-chars separators)
|
|
|
|
idx (Pattern.find-all &pat s)
|
|
|
|
lidx (Array.length &idx)
|
|
|
|
result (Array.allocate (Int.inc lidx))]
|
|
|
|
(Array.aset-uninitialized! &result 0
|
|
|
|
(substring s 0 (if (> lidx 0) @(Array.nth &idx 0) (length s))))
|
|
|
|
(for [i 0 (Int.dec (Array.length &idx))]
|
|
|
|
(Array.aset-uninitialized! &result (Int.inc i)
|
|
|
|
(substring s (Int.inc @(Array.nth &idx i)) @(Array.nth &idx (Int.inc i)))))
|
|
|
|
(when (> lidx 0)
|
|
|
|
(Array.aset-uninitialized! &result lidx
|
|
|
|
(suffix-string s (Int.inc @(Array.nth &idx (Int.dec lidx))))))
|
|
|
|
result))
|
2018-03-13 17:15:31 +03:00
|
|
|
|
2018-05-12 16:58:49 +03:00
|
|
|
(doc words "Split a string into words.")
|
2018-03-13 17:15:31 +03:00
|
|
|
(defn words [s]
|
|
|
|
(split-by s &[\tab \space]))
|
|
|
|
|
2018-05-12 16:58:49 +03:00
|
|
|
(doc lines "Split a string into lines.")
|
2018-03-13 17:15:31 +03:00
|
|
|
(defn lines [s]
|
|
|
|
(split-by s &[\newline]))
|
2018-03-13 13:59:48 +03:00
|
|
|
)
|