diff --git a/Cargo.lock b/Cargo.lock index 86596b3..267ce99 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -13,7 +13,7 @@ dependencies = [ [[package]] name = "calcit_regex" -version = "0.0.1" +version = "0.0.2" dependencies = [ "cirru_edn", "cirru_parser", diff --git a/Cargo.toml b/Cargo.toml index e36ad3c..b8707c3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "calcit_regex" -version = "0.0.1" +version = "0.0.2" authors = ["jiyinyiyong "] edition = "2018" diff --git a/README.md b/README.md index 7744e57..f33a338 100644 --- a/README.md +++ b/README.md @@ -10,9 +10,18 @@ APIs: ```cirru calcit.std.regex/re-matches |2 |\d +; "returns bool" + +; "find first matched item" calcit.std.regex/re-find |a4 |\d calcit.std.regex/re-find-index |a1 |\d + calcit.std.regex/re-find-all |123 |\d+ + + +assert= |1abXcX3 $ re-replace-all |1ab22c333 |\d{2} "\"X" + +assert= ([] "\"1ab" "\"c" "\"3") $ re-split |1ab22c333 |\d{2} ``` Install to `~/.config/calcit/modules/`, compile and provide `*.{dylib,so}` file with `./build.sh`. diff --git a/calcit.cirru b/calcit.cirru index f0d14b0..143ef69 100644 --- a/calcit.cirru +++ b/calcit.cirru @@ -99,6 +99,44 @@ |r $ {} (:type :leaf) (:by |u0) (:at 1636960151241) (:text "|\"re_find") |v $ {} (:type :leaf) (:by |u0) (:at 1636960151241) (:text |s) |x $ {} (:type :leaf) (:by |u0) (:at 1636960151241) (:text |pattern) + |re-split $ {} (:type :expr) (:by |u0) (:at 1637157509604) + :data $ {} + |T $ {} (:type :leaf) (:by |u0) (:at 1637157509604) (:text |defn) + |j $ {} (:type :leaf) (:by |u0) (:at 1637157509604) (:text |re-split) + |r $ {} (:type :expr) (:by |u0) (:at 1637157527168) + :data $ {} + |T $ {} (:type :leaf) (:by |u0) (:at 1637157527168) (:text |s) + |j $ {} (:type :leaf) (:by |u0) (:at 1637157527168) (:text |pattern) + |v $ {} (:type :expr) (:by |u0) (:at 1637157527168) + :data $ {} + |T $ {} (:type :leaf) (:by |u0) (:at 1637157527168) (:text |&call-dylib-edn) + |j $ {} (:type :expr) (:by |u0) (:at 1637157527168) + :data $ {} + |T $ {} (:type :leaf) (:by |u0) (:at 1637157527168) (:text |get-dylib-path) + |j $ {} (:type :leaf) (:by |u0) (:at 1637157527168) (:text "|\"/dylibs/libcalcit_std") + |r $ {} (:type :leaf) (:by |u0) (:at 1637157535118) (:text "|\"re_split") + |v $ {} (:type :leaf) (:by |u0) (:at 1637157527168) (:text |s) + |x $ {} (:type :leaf) (:by |u0) (:at 1637157527168) (:text |pattern) + |re-replace-all $ {} (:type :expr) (:by |u0) (:at 1637157516835) + :data $ {} + |T $ {} (:type :leaf) (:by |u0) (:at 1637157516835) (:text |defn) + |j $ {} (:type :leaf) (:by |u0) (:at 1637157516835) (:text |re-replace-all) + |r $ {} (:type :expr) (:by |u0) (:at 1637157540472) + :data $ {} + |T $ {} (:type :leaf) (:by |u0) (:at 1637157540472) (:text |s) + |j $ {} (:type :leaf) (:by |u0) (:at 1637157540472) (:text |pattern) + |r $ {} (:type :leaf) (:by |u0) (:at 1637157547063) (:text |next) + |v $ {} (:type :expr) (:by |u0) (:at 1637157540472) + :data $ {} + |T $ {} (:type :leaf) (:by |u0) (:at 1637157540472) (:text |&call-dylib-edn) + |j $ {} (:type :expr) (:by |u0) (:at 1637157540472) + :data $ {} + |T $ {} (:type :leaf) (:by |u0) (:at 1637157540472) (:text |get-dylib-path) + |j $ {} (:type :leaf) (:by |u0) (:at 1637157540472) (:text "|\"/dylibs/libcalcit_std") + |r $ {} (:type :leaf) (:by |u0) (:at 1637157553738) (:text "|\"re_replace_all") + |v $ {} (:type :leaf) (:by |u0) (:at 1637157540472) (:text |s) + |x $ {} (:type :leaf) (:by |u0) (:at 1637157540472) (:text |pattern) + |y $ {} (:type :leaf) (:by |u0) (:at 1637157556670) (:text |next) |regex.util $ {} :ns $ {} (:type :expr) (:by |u0) (:at 1633181044360) :data $ {} @@ -192,6 +230,8 @@ |j $ {} (:type :leaf) (:by |u0) (:at 1636960219938) (:text |re-find-index) |r $ {} (:type :leaf) (:by |u0) (:at 1636960219938) (:text |re-find) |v $ {} (:type :leaf) (:by |u0) (:at 1636960219938) (:text |re-find-all) + |x $ {} (:type :leaf) (:by |u0) (:at 1637157659783) (:text |re-split) + |y $ {} (:type :leaf) (:by |u0) (:at 1637157663541) (:text |re-replace-all) |r $ {} (:type :expr) (:by |u0) (:at 1636960219938) :data $ {} |T $ {} (:type :leaf) (:by |u0) (:at 1636960219938) (:text |regex.$meta) @@ -243,6 +283,30 @@ |T $ {} (:type :leaf) (:by |u0) (:at 1636960219938) (:text |re-find-all) |j $ {} (:type :leaf) (:by |u0) (:at 1636960219938) (:text ||1a2a34) |r $ {} (:type :leaf) (:by |u0) (:at 1636960219938) (:text ||\d+) + |yyv $ {} (:type :expr) (:by |u0) (:at 1637157564769) + :data $ {} + |T $ {} (:type :leaf) (:by |u0) (:at 1637157566683) (:text |assert=) + |j $ {} (:type :leaf) (:by |u0) (:at 1637157612595) (:text ||1abXcX3) + |r $ {} (:type :expr) (:by |u0) (:at 1637157570399) + :data $ {} + |T $ {} (:type :leaf) (:by |u0) (:at 1637157576826) (:text |re-replace-all) + |j $ {} (:type :leaf) (:by |u0) (:at 1637157591536) (:text ||1ab22c333) + |r $ {} (:type :leaf) (:by |u0) (:at 1637157596985) (:text ||\d{2}) + |v $ {} (:type :leaf) (:by |u0) (:at 1637157603611) (:text "|\"X") + |yyx $ {} (:type :expr) (:by |u0) (:at 1637157564769) + :data $ {} + |T $ {} (:type :leaf) (:by |u0) (:at 1637157566683) (:text |assert=) + |r $ {} (:type :expr) (:by |u0) (:at 1637157570399) + :data $ {} + |T $ {} (:type :leaf) (:by |u0) (:at 1637157620368) (:text |re-split) + |j $ {} (:type :leaf) (:by |u0) (:at 1637157591536) (:text ||1ab22c333) + |r $ {} (:type :leaf) (:by |u0) (:at 1637157596985) (:text ||\d{2}) + |f $ {} (:type :expr) (:by |u0) (:at 1637157629847) + :data $ {} + |T $ {} (:type :leaf) (:by |u0) (:at 1637157630274) (:text |[]) + |j $ {} (:type :leaf) (:by |u0) (:at 1637157635843) (:text "|\"1ab") + |r $ {} (:type :leaf) (:by |u0) (:at 1637157639002) (:text "|\"c") + |v $ {} (:type :leaf) (:by |u0) (:at 1637157640155) (:text "|\"3") |yT $ {} (:type :expr) (:by |u0) (:at 1636960219938) :data $ {} |T $ {} (:type :leaf) (:by |u0) (:at 1636960219938) (:text |assert=) @@ -325,5 +389,5 @@ |r $ {} (:type :leaf) (:by |u0) (:at 1636960219938) (:text ||\d) :configs $ {} (:port 6001) (:init-fn |regex.test/main!) (:reload-fn |regex.test/reload!) :modules $ [] - :version |0.0.1 + :version |0.0.2 :entries $ {} diff --git a/compact.cirru b/compact.cirru index 92b5e18..e569d2a 100644 --- a/compact.cirru +++ b/compact.cirru @@ -2,7 +2,7 @@ {} (:package |regex) :configs $ {} (:init-fn |regex.test/main!) (:reload-fn |regex.test/reload!) :modules $ [] - :version |0.0.1 + :version |0.0.2 :entries $ {} :files $ {} |regex.core $ {} @@ -23,6 +23,12 @@ |re-find $ quote defn re-find (s pattern) &call-dylib-edn (get-dylib-path "\"/dylibs/libcalcit_std") "\"re_find" s pattern + |re-split $ quote + defn re-split (s pattern) + &call-dylib-edn (get-dylib-path "\"/dylibs/libcalcit_std") "\"re_split" s pattern + |re-replace-all $ quote + defn re-replace-all (s pattern next) + &call-dylib-edn (get-dylib-path "\"/dylibs/libcalcit_std") "\"re_replace_all" s pattern next |regex.util $ {} :ns $ quote ns regex.util $ :require @@ -39,7 +45,7 @@ |regex.test $ {} :ns $ quote ns regex.test $ :require - regex.core :refer $ re-matches re-find-index re-find re-find-all + regex.core :refer $ re-matches re-find-index re-find re-find-all re-split re-replace-all regex.$meta :refer $ calcit-dirname calcit-filename :defs $ {} |main! $ quote @@ -54,3 +60,5 @@ assert= ([] |123) (re-find-all |123 |\d+) assert= ([] |1 |2 |3) (re-find-all |1a2a3 |\d+) assert= ([] |1 |2 |34) (re-find-all |1a2a34 |\d+) + assert= |1abXcX3 $ re-replace-all |1ab22c333 |\d{2} "\"X" + assert= ([] "\"1ab" "\"c" "\"3") (re-split |1ab22c333 |\d{2}) diff --git a/src/lib.rs b/src/lib.rs index 540c618..840f2d4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -85,3 +85,42 @@ pub fn re_find_all(args: Vec) -> Result { Err(format!("re-find-all expected 2 strings: {:?}", args)) } } + +#[no_mangle] +pub fn re_split(args: Vec) -> Result { + if args.len() == 2 { + match (&args[0], &args[1]) { + (Edn::Str(s), Edn::Str(pattern)) => match Regex::new(pattern) { + Ok(p) => { + let mut ys: Vec = vec![]; + for piece in p.split(s) { + ys.push(Edn::str(piece)); + } + Ok(Edn::List(ys)) + } + Err(e) => Err(format!("re-split failed, {}", e)), + }, + (_, _) => Err(format!("re-split expected 2 strings: {:?}", args)), + } + } else { + Err(format!("re-split expected 2 strings: {:?}", args)) + } +} + +#[no_mangle] +pub fn re_replace_all(args: Vec) -> Result { + if args.len() == 3 { + match (&args[0], &args[1], &args[2]) { + (Edn::Str(s), Edn::Str(pattern), Edn::Str(next)) => match Regex::new(pattern) { + Ok(p) => Ok(Edn::str(p.replace_all(&**s, &**next).into_owned())), + Err(e) => Err(format!("re-replace-all failed, {}", e)), + }, + (a, b, c) => Err(format!( + "re-replace-all expected 3 strings: {} {} {}", + a, b, c + )), + } + } else { + Err(format!("re-replace-all expected 3 strings: {:?}", args)) + } +}