From 2dfe46c579a2f1b872e7d2300f38c0bc436bc6d0 Mon Sep 17 00:00:00 2001 From: tiye Date: Mon, 13 May 2024 11:31:58 +0800 Subject: [PATCH 01/14] upgrade deps; tag 0.0.4 --- .github/workflows/check.yaml | 12 +---- .rustfmt.toml | 3 ++ Cargo.lock | 102 +++++++++++++++++++++++++++++++---- Cargo.toml | 8 +-- src/lib.rs | 21 ++++---- 5 files changed, 111 insertions(+), 35 deletions(-) create mode 100644 .rustfmt.toml diff --git a/.github/workflows/check.yaml b/.github/workflows/check.yaml index b3569b5..dd405de 100644 --- a/.github/workflows/check.yaml +++ b/.github/workflows/check.yaml @@ -14,17 +14,9 @@ jobs: steps: - uses: actions/checkout@v4 - - uses: supplypike/setup-bin@v3 + - uses: calcit-lang/setup-cr@0.0.3 with: - uri: 'https://github.com/calcit-lang/calcit/releases/download/0.8.9/cr' - name: 'cr' - version: '0.8.9' - - - uses: supplypike/setup-bin@v3 - with: - uri: 'https://github.com/calcit-lang/calcit/releases/download/0.8.9/caps' - name: 'caps' - version: '0.8.9' + version: "0.8.50" - uses: dtolnay/rust-toolchain@stable with: diff --git a/.rustfmt.toml b/.rustfmt.toml new file mode 100644 index 0000000..79bf0b7 --- /dev/null +++ b/.rustfmt.toml @@ -0,0 +1,3 @@ + +max_width = 136 +tab_spaces = 2 diff --git a/Cargo.lock b/Cargo.lock index e3ce95e..b765875 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -11,9 +11,28 @@ dependencies = [ "memchr", ] +[[package]] +name = "bincode" +version = "2.0.0-rc.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f11ea1a0346b94ef188834a65c068a03aec181c94896d481d7a0a40d85b0ce95" +dependencies = [ + "bincode_derive", + "serde", +] + +[[package]] +name = "bincode_derive" +version = "2.0.0-rc.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7e30759b3b99a1b802a7a3aa21c85c3ded5c28e1c83170d82d70f08bbf7f3e4c" +dependencies = [ + "virtue", +] + [[package]] name = "calcit_regex" -version = "0.0.2" +version = "0.0.4" dependencies = [ "cirru_edn", "cirru_parser", @@ -22,10 +41,11 @@ dependencies = [ [[package]] name = "cirru_edn" -version = "0.5.0" +version = "0.6.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8a35d38ee59f7409023f9dee361dc98760f33c9ad1f21283e536939d9561ed44" +checksum = "b4e435d0f66a1d16f727720f46a89c7a203e306bf466483ba5ff65903fd0f05c" dependencies = [ + "bincode", "cirru_parser", "hex", "lazy_static", @@ -33,9 +53,12 @@ dependencies = [ [[package]] name = "cirru_parser" -version = "0.1.25" +version = "0.1.29" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1297388204c9d3c72d1cafea80682fd6384e79c66dfe980460886bff7387e684" +checksum = "c96247e15778d471f8c94b321fa11220dfb987546fcf8fb73b1a5c2b3a018a78" +dependencies = [ + "bincode", +] [[package]] name = "hex" @@ -55,11 +78,29 @@ version = "2.6.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f665ee40bc4a3c5590afb1e9677db74a508659dfd71e126420da8274909a0167" +[[package]] +name = "proc-macro2" +version = "1.0.82" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ad3d49ab951a01fbaafe34f2ec74122942fe18a3f9814c3268f1bb72042131b" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.36" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0fa76aaf39101c457836aec0ce2316dbdc3ab723cdda1c6bd4e6ad4208acaca7" +dependencies = [ + "proc-macro2", +] + [[package]] name = "regex" -version = "1.10.2" +version = "1.10.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "380b951a9c5e80ddfd6136919eef32310721aa4aacd4889a8d39124b026ab343" +checksum = "c117dbdfde9c8308975b6a18d71f3f385c89461f7b3fb054288ecf2a2058ba4c" dependencies = [ "aho-corasick", "memchr", @@ -69,9 +110,9 @@ dependencies = [ [[package]] name = "regex-automata" -version = "0.4.3" +version = "0.4.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5f804c7828047e88b2d32e2d7fe5a105da8ee3264f01902f796c8e067dc2483f" +checksum = "86b83b8b9847f9bf95ef68afb0b8e6cdb80f498442f5179a29fad448fcc1eaea" dependencies = [ "aho-corasick", "memchr", @@ -83,3 +124,46 @@ name = "regex-syntax" version = "0.8.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c08c74e62047bb2de4ff487b251e4a92e24f48745648451635cec7d591162d9f" + +[[package]] +name = "serde" +version = "1.0.201" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "780f1cebed1629e4753a1a38a3c72d30b97ec044f0aef68cb26650a3c5cf363c" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.201" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c5e405930b9796f1c00bee880d03fc7e0bb4b9a11afc776885ffe84320da2865" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "syn" +version = "2.0.63" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bf5be731623ca1a1fb7d8be6f261a3be6d3e2337b8a1f97be944d020c8fcb704" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "unicode-ident" +version = "1.0.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" + +[[package]] +name = "virtue" +version = "0.0.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9dcc60c0624df774c82a0ef104151231d37da4962957d691c011c852b2473314" diff --git a/Cargo.toml b/Cargo.toml index 835ab96..819994e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "calcit_regex" -version = "0.0.2" +version = "0.0.4" authors = ["jiyinyiyong "] edition = "2021" @@ -13,6 +13,6 @@ crate-type = ["dylib"] # Creates dynamic lib # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -cirru_edn = "0.5.0" -cirru_parser = "0.1.25" -regex = "1.8.4" +cirru_edn = "0.6.3" +cirru_parser = "0.1.29" +regex = "1.10.4" diff --git a/src/lib.rs b/src/lib.rs index 840f2d4..520183c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,9 +1,9 @@ -use cirru_edn::Edn; +use cirru_edn::{Edn, EdnListView}; use regex::Regex; #[no_mangle] pub fn abi_version() -> String { - String::from("0.0.6") + String::from("0.0.7") } #[no_mangle] @@ -51,8 +51,8 @@ pub fn re_find(args: Vec) -> Result { Ok(p) => { let mut matched = p.find_iter(s); match matched.next() { - Some(v) => Ok(Edn::Str(v.as_str().to_string().into_boxed_str())), - None => Ok(Edn::Str("".to_owned().into_boxed_str())), // TODO maybe nil + Some(v) => Ok(Edn::Str(v.as_str().to_string().into())), + None => Ok(Edn::Str("".to_owned().into())), // TODO maybe nil } } Err(e) => Err(format!("re-find failed, {}", e)), @@ -73,9 +73,9 @@ pub fn re_find_all(args: Vec) -> Result { Ok(p) => { let mut ys: Vec = vec![]; for v in p.find_iter(s) { - ys.push(Edn::Str(v.as_str().to_string().into_boxed_str())) + ys.push(Edn::Str(v.as_str().to_string().into())) } - Ok(Edn::List(ys)) + Ok(Edn::List(EdnListView(ys))) } Err(e) => Err(format!("re-find-all failed, {}", e)), }, @@ -96,7 +96,7 @@ pub fn re_split(args: Vec) -> Result { for piece in p.split(s) { ys.push(Edn::str(piece)); } - Ok(Edn::List(ys)) + Ok(Edn::List(EdnListView(ys))) } Err(e) => Err(format!("re-split failed, {}", e)), }, @@ -112,13 +112,10 @@ 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())), + 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 - )), + (a, b, c) => Err(format!("re-replace-all expected 3 strings: {} {} {}", a, b, c)), } } else { Err(format!("re-replace-all expected 3 strings: {:?}", args)) From 9431bcb07f9b50bad00b10a7270fd3473d3cb80a Mon Sep 17 00:00:00 2001 From: tiye Date: Thu, 16 May 2024 02:37:33 +0800 Subject: [PATCH 02/14] experimentally holding regex with any-ref; tag 0.0.5-a1 --- .github/workflows/check.yaml | 2 +- Cargo.lock | 6 +-- Cargo.toml | 5 +- README.md | 3 ++ calcit.cirru | 101 ++++++++++++++++++++++++++++++++++- compact.cirru | 18 ++++++- src/lib.rs | 82 ++++++++++++++++++++++++++-- 7 files changed, 204 insertions(+), 13 deletions(-) diff --git a/.github/workflows/check.yaml b/.github/workflows/check.yaml index dd405de..d35d9f3 100644 --- a/.github/workflows/check.yaml +++ b/.github/workflows/check.yaml @@ -16,7 +16,7 @@ jobs: - uses: calcit-lang/setup-cr@0.0.3 with: - version: "0.8.50" + version: "0.8.51-a1" - uses: dtolnay/rust-toolchain@stable with: diff --git a/Cargo.lock b/Cargo.lock index b765875..5e233f9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -32,7 +32,7 @@ dependencies = [ [[package]] name = "calcit_regex" -version = "0.0.4" +version = "0.0.5-a1" dependencies = [ "cirru_edn", "cirru_parser", @@ -41,9 +41,9 @@ dependencies = [ [[package]] name = "cirru_edn" -version = "0.6.3" +version = "0.6.4-a1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b4e435d0f66a1d16f727720f46a89c7a203e306bf466483ba5ff65903fd0f05c" +checksum = "a3f8c8e9e455019d1b9d9b558f95c201cedd30db276edd0bf6be625306857055" dependencies = [ "bincode", "cirru_parser", diff --git a/Cargo.toml b/Cargo.toml index 819994e..f63535a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "calcit_regex" -version = "0.0.4" +version = "0.0.5-a1" authors = ["jiyinyiyong "] edition = "2021" @@ -13,6 +13,7 @@ crate-type = ["dylib"] # Creates dynamic lib # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -cirru_edn = "0.6.3" +cirru_edn = "0.6.4-a1" +# cirru_edn = { path = "/Users/chenyong/repo/cirru/edn.rs" } cirru_parser = "0.1.29" regex = "1.10.4" diff --git a/README.md b/README.md index 6445641..23a1aac 100644 --- a/README.md +++ b/README.md @@ -25,6 +25,9 @@ regex.core/re-replace-all |1ab22c333 |\d{2} "\"X" regex.core/re-split |1ab22c333 |\d{2} ; [] "\"1ab" "\"c" "\"3" + +regex.core/re-pattern |\d+ +; "creates any-ref to hold a native regex pattern" ``` Install to `~/.config/calcit/modules/`, compile and provide `*.{dylib,so}` file with `./build.sh`. diff --git a/calcit.cirru b/calcit.cirru index 3aa54fb..09d13b3 100644 --- a/calcit.cirru +++ b/calcit.cirru @@ -1,6 +1,6 @@ {} (:package |regex) - :configs $ {} (:init-fn |regex.test/main!) (:port 6001) (:reload-fn |regex.test/reload!) (:version |0.0.3) + :configs $ {} (:init-fn |regex.test/main!) (:port 6001) (:reload-fn |regex.test/reload!) (:version |0.0.5-a1) :modules $ [] :entries $ {} :files $ {} @@ -82,6 +82,23 @@ |r $ %{} :Leaf (:at 1636960151241) (:by |u0) (:text "|\"re_matches") |v $ %{} :Leaf (:at 1636960151241) (:by |u0) (:text |s) |x $ %{} :Leaf (:at 1636960151241) (:by |u0) (:text |pattern) + |re-pattern $ %{} :CodeEntry (:doc |) + :code $ %{} :Expr (:at 1715795933947) (:by |u0) + :data $ {} + |T $ %{} :Leaf (:at 1715795933947) (:by |u0) (:text |defn) + |b $ %{} :Leaf (:at 1715795933947) (:by |u0) (:text |re-pattern) + |h $ %{} :Expr (:at 1715795940312) (:by |u0) + :data $ {} + |b $ %{} :Leaf (:at 1715795940312) (:by |u0) (:text |pattern) + |l $ %{} :Expr (:at 1715795940312) (:by |u0) + :data $ {} + |T $ %{} :Leaf (:at 1715795940312) (:by |u0) (:text |&call-dylib-edn) + |b $ %{} :Expr (:at 1715795940312) (:by |u0) + :data $ {} + |T $ %{} :Leaf (:at 1715795940312) (:by |u0) (:text |get-dylib-path) + |b $ %{} :Leaf (:at 1715795940312) (:by |u0) (:text "|\"/dylibs/libcalcit_std") + |h $ %{} :Leaf (:at 1715795950203) (:by |u0) (:text "|\"re_pattern") + |o $ %{} :Leaf (:at 1715795940312) (:by |u0) (:text |pattern) |re-replace-all $ %{} :CodeEntry (:doc |) :code $ %{} :Expr (:at 1637157516835) (:by |u0) :data $ {} @@ -293,6 +310,87 @@ |T $ %{} :Leaf (:at 1637157620368) (:by |u0) (:text |re-split) |j $ %{} :Leaf (:at 1637157591536) (:by |u0) (:text ||1ab22c333) |r $ %{} :Leaf (:at 1637157596985) (:by |u0) (:text ||\d{2}) + |yz $ %{} :Expr (:at 1715796182324) (:by |u0) + :data $ {} + |T $ %{} :Leaf (:at 1715796183478) (:by |u0) (:text |println) + |b $ %{} :Leaf (:at 1715796196505) (:by |u0) (:text "|\"%%% test variable holding regex") + |z $ %{} :Expr (:at 1715795960911) (:by |u0) + :data $ {} + |T $ %{} :Leaf (:at 1715795963731) (:by |u0) (:text |let) + |b $ %{} :Expr (:at 1715795964022) (:by |u0) + :data $ {} + |T $ %{} :Expr (:at 1715795964146) (:by |u0) + :data $ {} + |T $ %{} :Leaf (:at 1715795966487) (:by |u0) (:text |pattern) + |b $ %{} :Expr (:at 1715795969557) (:by |u0) + :data $ {} + |T $ %{} :Leaf (:at 1715795972580) (:by |u0) (:text |re-pattern) + |b $ %{} :Leaf (:at 1715795974817) (:by |u0) (:text "|\"\\d+") + |e $ %{} :Expr (:at 1715797292331) (:by |u0) + :data $ {} + |T $ %{} :Leaf (:at 1715797294062) (:by |u0) (:text |println) + |X $ %{} :Leaf (:at 1715797301485) (:by |u0) (:text "|\"Pattern is:") + |b $ %{} :Leaf (:at 1715797296817) (:by |u0) (:text |pattern) + |h $ %{} :Expr (:at 1715796117788) (:by |u0) + :data $ {} + |T $ %{} :Leaf (:at 1715796117788) (:by |u0) (:text |assert=) + |b $ %{} :Leaf (:at 1715796117788) (:by |u0) (:text |true) + |h $ %{} :Expr (:at 1715796117788) (:by |u0) + :data $ {} + |T $ %{} :Leaf (:at 1715796117788) (:by |u0) (:text |re-matches) + |b $ %{} :Leaf (:at 1715796117788) (:by |u0) (:text ||2) + |h $ %{} :Leaf (:at 1715796124899) (:by |u0) (:text |pattern) + |j $ %{} :Expr (:at 1715796117788) (:by |u0) + :data $ {} + |T $ %{} :Leaf (:at 1715796117788) (:by |u0) (:text |assert=) + |b $ %{} :Leaf (:at 1715796117788) (:by |u0) (:text |true) + |h $ %{} :Expr (:at 1715796117788) (:by |u0) + :data $ {} + |T $ %{} :Leaf (:at 1715796117788) (:by |u0) (:text |re-matches) + |b $ %{} :Leaf (:at 1715796136285) (:by |u0) (:text ||23) + |h $ %{} :Leaf (:at 1715796124899) (:by |u0) (:text |pattern) + |l $ %{} :Expr (:at 1715796117788) (:by |u0) + :data $ {} + |T $ %{} :Leaf (:at 1715796117788) (:by |u0) (:text |assert=) + |b $ %{} :Leaf (:at 1715796171969) (:by |u0) (:text |false) + |h $ %{} :Expr (:at 1715796117788) (:by |u0) + :data $ {} + |T $ %{} :Leaf (:at 1715796117788) (:by |u0) (:text |re-matches) + |b $ %{} :Leaf (:at 1715796130255) (:by |u0) (:text ||qq) + |h $ %{} :Leaf (:at 1715796124899) (:by |u0) (:text |pattern) + |o $ %{} :Expr (:at 1715796202090) (:by |u0) + :data $ {} + |T $ %{} :Leaf (:at 1715796204524) (:by |u0) (:text |assert=) + |b $ %{} :Leaf (:at 1715796236610) (:by |u0) (:text "|\"22") + |h $ %{} :Expr (:at 1715796212276) (:by |u0) + :data $ {} + |T $ %{} :Leaf (:at 1715796226879) (:by |u0) (:text |re-find) + |b $ %{} :Leaf (:at 1715796215253) (:by |u0) (:text ||q22) + |h $ %{} :Leaf (:at 1715796212276) (:by |u0) (:text |pattern) + |q $ %{} :Expr (:at 1715796244650) (:by |u0) + :data $ {} + |T $ %{} :Leaf (:at 1715796244650) (:by |u0) (:text |assert=) + |b $ %{} :Expr (:at 1715796244650) (:by |u0) + :data $ {} + |T $ %{} :Leaf (:at 1715796244650) (:by |u0) (:text |[]) + |b $ %{} :Leaf (:at 1715796244650) (:by |u0) (:text ||1) + |h $ %{} :Leaf (:at 1715796244650) (:by |u0) (:text ||2) + |l $ %{} :Leaf (:at 1715796244650) (:by |u0) (:text ||3) + |h $ %{} :Expr (:at 1715796244650) (:by |u0) + :data $ {} + |T $ %{} :Leaf (:at 1715796244650) (:by |u0) (:text |re-find-all) + |b $ %{} :Leaf (:at 1715796249538) (:by |u0) (:text ||1q2q3) + |h $ %{} :Leaf (:at 1715796253498) (:by |u0) (:text |pattern) + |s $ %{} :Expr (:at 1715796262291) (:by |u0) + :data $ {} + |T $ %{} :Leaf (:at 1715796262291) (:by |u0) (:text |assert=) + |b $ %{} :Leaf (:at 1715796290165) (:by |u0) (:text ||XabXcX) + |h $ %{} :Expr (:at 1715796262291) (:by |u0) + :data $ {} + |T $ %{} :Leaf (:at 1715796262291) (:by |u0) (:text |re-replace-all) + |b $ %{} :Leaf (:at 1715796262291) (:by |u0) (:text ||1ab22c333) + |h $ %{} :Leaf (:at 1715796267870) (:by |u0) (:text |pattern) + |l $ %{} :Leaf (:at 1715796262291) (:by |u0) (:text "|\"X") :ns $ %{} :CodeEntry (:doc |) :code $ %{} :Expr (:at 1636960219938) (:by |u0) :data $ {} @@ -313,6 +411,7 @@ |v $ %{} :Leaf (:at 1636960219938) (:by |u0) (:text |re-find-all) |x $ %{} :Leaf (:at 1637157659783) (:by |u0) (:text |re-split) |y $ %{} :Leaf (:at 1637157663541) (:by |u0) (:text |re-replace-all) + |z $ %{} :Leaf (:at 1715795979178) (:by |u0) (:text |re-pattern) |r $ %{} :Expr (:at 1636960219938) (:by |u0) :data $ {} |T $ %{} :Leaf (:at 1636960219938) (:by |u0) (:text |regex.$meta) diff --git a/compact.cirru b/compact.cirru index 213fcea..109f626 100644 --- a/compact.cirru +++ b/compact.cirru @@ -1,6 +1,6 @@ {} (:package |regex) - :configs $ {} (:init-fn |regex.test/main!) (:reload-fn |regex.test/reload!) (:version |0.0.3) + :configs $ {} (:init-fn |regex.test/main!) (:reload-fn |regex.test/reload!) (:version |0.0.5-a1) :modules $ [] :entries $ {} :files $ {} @@ -22,6 +22,10 @@ :code $ quote defn re-matches (s pattern) &call-dylib-edn (get-dylib-path "\"/dylibs/libcalcit_std") "\"re_matches" s pattern + |re-pattern $ %{} :CodeEntry (:doc |) + :code $ quote + defn re-pattern (pattern) + &call-dylib-edn (get-dylib-path "\"/dylibs/libcalcit_std") "\"re_pattern" pattern |re-replace-all $ %{} :CodeEntry (:doc |) :code $ quote defn re-replace-all (s pattern next) @@ -52,10 +56,20 @@ 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}) + println "\"%%% test variable holding regex" + let + pattern $ re-pattern "\"\\d+" + println "\"Pattern is:" pattern + assert= true $ re-matches |2 pattern + assert= true $ re-matches |23 pattern + assert= false $ re-matches |qq pattern + assert= "\"22" $ re-find |q22 pattern + assert= ([] |1 |2 |3) (re-find-all |1q2q3 pattern) + assert= |XabXcX $ re-replace-all |1ab22c333 pattern "\"X" :ns $ %{} :CodeEntry (:doc |) :code $ quote ns regex.test $ :require - regex.core :refer $ re-matches re-find-index re-find re-find-all re-split re-replace-all + regex.core :refer $ re-matches re-find-index re-find re-find-all re-split re-replace-all re-pattern regex.$meta :refer $ calcit-dirname calcit-filename |regex.util $ %{} :FileEntry :defs $ {} diff --git a/src/lib.rs b/src/lib.rs index 520183c..d9dcd05 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,9 +1,26 @@ -use cirru_edn::{Edn, EdnListView}; +use std::sync::Arc; + +use cirru_edn::{Edn, EdnAnyRef, EdnListView}; use regex::Regex; #[no_mangle] pub fn abi_version() -> String { - String::from("0.0.7") + String::from("0.0.8") +} + +#[no_mangle] +pub fn re_pattern(args: Vec) -> Result { + if args.len() == 1 { + match &args[0] { + Edn::Str(s) => match Regex::new(s) { + Ok(pattern) => Ok(Edn::AnyRef(EdnAnyRef(Arc::from(pattern)))), + Err(e) => Err(format!("re-pattern failed, {}", e)), + }, + _ => Err(format!("re-pattern expect 1 string, got {:?}", args)), + } + } else { + Err(format!("re-pattern expect 1 string, got {:?}", args)) + } } #[no_mangle] @@ -14,6 +31,13 @@ pub fn re_matches(args: Vec) -> Result { Ok(p) => Ok(Edn::Bool(p.is_match(s))), Err(e) => Err(format!("re-matches failed, {}", e)), }, + (Edn::Str(s), Edn::AnyRef(EdnAnyRef(p))) => { + if let Some(pattern) = p.downcast_ref::() { + Ok(Edn::Bool(pattern.is_match(s))) + } else { + Err(format!("re-matches expected a regex, got {:?}", p)) + } + } (_, _) => Err(format!("re-matches expected 2 strings: {:?}", args)), } } else { @@ -34,6 +58,16 @@ pub fn re_find_index(args: Vec) -> Result { Err(e) => Err(format!("re-find-index failed, {}", e)), } } + (Edn::Str(s), Edn::AnyRef(EdnAnyRef(p))) => { + if let Some(pattern) = p.downcast_ref::() { + match pattern.find(s) { + Some(matched) => Ok(Edn::Number(matched.start() as f64)), + None => Ok(Edn::Number(-1.0)), // TODO maybe nil + } + } else { + Err(format!("re-find-index expected a regex, got {:?}", p)) + } + } (_, _) => Err(format!("re-find-index expected 2 strings: {:?}", args)), } } else { @@ -51,13 +85,24 @@ pub fn re_find(args: Vec) -> Result { Ok(p) => { let mut matched = p.find_iter(s); match matched.next() { - Some(v) => Ok(Edn::Str(v.as_str().to_string().into())), - None => Ok(Edn::Str("".to_owned().into())), // TODO maybe nil + Some(v) => Ok(Edn::str(v.as_str().to_string())), + None => Ok(Edn::from("")), // TODO maybe nil } } Err(e) => Err(format!("re-find failed, {}", e)), } } + (Edn::Str(s), Edn::AnyRef(EdnAnyRef(p))) => { + if let Some(pattern) = p.downcast_ref::() { + let mut matched = pattern.find_iter(s); + match matched.next() { + Some(v) => Ok(Edn::str(v.as_str().to_string())), + None => Ok(Edn::from("")), // TODO maybe nil + } + } else { + Err(format!("re-find expected a regex, got {:?}", p)) + } + } (_, _) => Err(format!("re-find expected 2 strings: {:?}", args)), } } else { @@ -79,6 +124,17 @@ pub fn re_find_all(args: Vec) -> Result { } Err(e) => Err(format!("re-find-all failed, {}", e)), }, + (Edn::Str(s), Edn::AnyRef(EdnAnyRef(p))) => { + if let Some(pattern) = p.downcast_ref::() { + let mut ys: Vec = vec![]; + for v in pattern.find_iter(s) { + ys.push(Edn::Str(v.as_str().to_string().into())) + } + Ok(Edn::List(EdnListView(ys))) + } else { + Err(format!("re-find-all expected a regex, got {:?}", p)) + } + } (_, _) => Err(format!("re-find-all expected 2 strings: {:?}", args)), } } else { @@ -100,6 +156,17 @@ pub fn re_split(args: Vec) -> Result { } Err(e) => Err(format!("re-split failed, {}", e)), }, + (Edn::Str(s), Edn::AnyRef(EdnAnyRef(p))) => { + if let Some(pattern) = p.downcast_ref::() { + let mut ys: Vec = vec![]; + for piece in pattern.split(s) { + ys.push(Edn::str(piece)); + } + Ok(Edn::List(EdnListView(ys))) + } else { + Err(format!("re-split expected a regex, got {:?}", p)) + } + } (_, _) => Err(format!("re-split expected 2 strings: {:?}", args)), } } else { @@ -115,6 +182,13 @@ pub fn re_replace_all(args: Vec) -> Result { Ok(p) => Ok(Edn::str(p.replace_all(s, &**next).into_owned())), Err(e) => Err(format!("re-replace-all failed, {}", e)), }, + (Edn::Str(s), Edn::AnyRef(EdnAnyRef(p)), Edn::Str(next)) => { + if let Some(pattern) = p.downcast_ref::() { + Ok(Edn::str(pattern.replace_all(s, &**next).into_owned())) + } else { + Err(format!("re-replace-all expected a regex, got {:?}", p)) + } + } (a, b, c) => Err(format!("re-replace-all expected 3 strings: {} {} {}", a, b, c)), } } else { From a986f8888b658c44b0a6a2ce3d0bffc6dadd55ca Mon Sep 17 00:00:00 2001 From: tiye Date: Thu, 16 May 2024 20:11:34 +0800 Subject: [PATCH 03/14] leak re-pattern result; tag 0.8.51-a2 --- .github/workflows/check.yaml | 2 +- Cargo.lock | 6 +++--- Cargo.toml | 4 ++-- src/lib.rs | 7 ++++++- 4 files changed, 12 insertions(+), 7 deletions(-) diff --git a/.github/workflows/check.yaml b/.github/workflows/check.yaml index d35d9f3..89e2c1c 100644 --- a/.github/workflows/check.yaml +++ b/.github/workflows/check.yaml @@ -16,7 +16,7 @@ jobs: - uses: calcit-lang/setup-cr@0.0.3 with: - version: "0.8.51-a1" + version: "0.8.51-a2" - uses: dtolnay/rust-toolchain@stable with: diff --git a/Cargo.lock b/Cargo.lock index 5e233f9..e424fc3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -32,7 +32,7 @@ dependencies = [ [[package]] name = "calcit_regex" -version = "0.0.5-a1" +version = "0.0.5-a2" dependencies = [ "cirru_edn", "cirru_parser", @@ -41,9 +41,9 @@ dependencies = [ [[package]] name = "cirru_edn" -version = "0.6.4-a1" +version = "0.6.4-a2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a3f8c8e9e455019d1b9d9b558f95c201cedd30db276edd0bf6be625306857055" +checksum = "cdbe878f563d28480a502f3a4fb3bccf6fb5080a4cf00b32ae31f9eb7e89d838" dependencies = [ "bincode", "cirru_parser", diff --git a/Cargo.toml b/Cargo.toml index f63535a..43748c7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "calcit_regex" -version = "0.0.5-a1" +version = "0.0.5-a2" authors = ["jiyinyiyong "] edition = "2021" @@ -13,7 +13,7 @@ crate-type = ["dylib"] # Creates dynamic lib # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -cirru_edn = "0.6.4-a1" +cirru_edn = "0.6.4-a2" # cirru_edn = { path = "/Users/chenyong/repo/cirru/edn.rs" } cirru_parser = "0.1.29" regex = "1.10.4" diff --git a/src/lib.rs b/src/lib.rs index d9dcd05..ab3f13e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -13,7 +13,12 @@ pub fn re_pattern(args: Vec) -> Result { if args.len() == 1 { match &args[0] { Edn::Str(s) => match Regex::new(s) { - Ok(pattern) => Ok(Edn::AnyRef(EdnAnyRef(Arc::from(pattern)))), + Ok(pattern) => { + let p = Arc::from(pattern); + let p2 = p.to_owned(); + // std::mem::forget(p); + Ok(Edn::AnyRef(EdnAnyRef(p2))) + } Err(e) => Err(format!("re-pattern failed, {}", e)), }, _ => Err(format!("re-pattern expect 1 string, got {:?}", args)), From dde5bb73f94c60ba0c00abcab6c8e4b1859d5e97 Mon Sep 17 00:00:00 2001 From: tiye Date: Fri, 17 May 2024 02:33:35 +0800 Subject: [PATCH 04/14] rename lib; set toolchain; tag 0.0.5-a3 --- .github/workflows/check.yaml | 2 +- Cargo.lock | 2 +- Cargo.toml | 8 ++++++-- calcit.cirru | 14 +++++++------- compact.cirru | 14 +++++++------- rust-toolchain.toml | 3 +++ 6 files changed, 25 insertions(+), 18 deletions(-) create mode 100644 rust-toolchain.toml diff --git a/.github/workflows/check.yaml b/.github/workflows/check.yaml index 89e2c1c..37eebb0 100644 --- a/.github/workflows/check.yaml +++ b/.github/workflows/check.yaml @@ -16,7 +16,7 @@ jobs: - uses: calcit-lang/setup-cr@0.0.3 with: - version: "0.8.51-a2" + version: "0.8.51-a4" - uses: dtolnay/rust-toolchain@stable with: diff --git a/Cargo.lock b/Cargo.lock index e424fc3..4553eb8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -32,7 +32,7 @@ dependencies = [ [[package]] name = "calcit_regex" -version = "0.0.5-a2" +version = "0.0.5-a3" dependencies = [ "cirru_edn", "cirru_parser", diff --git a/Cargo.toml b/Cargo.toml index 43748c7..11ac02d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,11 +1,11 @@ [package] name = "calcit_regex" -version = "0.0.5-a2" +version = "0.0.5-a3" authors = ["jiyinyiyong "] edition = "2021" [lib] -name = "calcit_std" +name = "calcit_regex" path = "src/lib.rs" crate-type = ["dylib"] # Creates dynamic lib @@ -17,3 +17,7 @@ cirru_edn = "0.6.4-a2" # cirru_edn = { path = "/Users/chenyong/repo/cirru/edn.rs" } cirru_parser = "0.1.29" regex = "1.10.4" + +# [profile.release] +# debug = true +# opt-level = 1 diff --git a/calcit.cirru b/calcit.cirru index 09d13b3..10f9aa1 100644 --- a/calcit.cirru +++ b/calcit.cirru @@ -21,7 +21,7 @@ |j $ %{} :Expr (:at 1636960151241) (:by |u0) :data $ {} |T $ %{} :Leaf (:at 1636960151241) (:by |u0) (:text |get-dylib-path) - |j $ %{} :Leaf (:at 1636960151241) (:by |u0) (:text "|\"/dylibs/libcalcit_std") + |j $ %{} :Leaf (:at 1715883726895) (:by |u0) (:text "|\"/dylibs/libcalcit_regex") |r $ %{} :Leaf (:at 1636960151241) (:by |u0) (:text "|\"re_find") |v $ %{} :Leaf (:at 1636960151241) (:by |u0) (:text |s) |x $ %{} :Leaf (:at 1636960151241) (:by |u0) (:text |pattern) @@ -40,7 +40,7 @@ |j $ %{} :Expr (:at 1636960151241) (:by |u0) :data $ {} |T $ %{} :Leaf (:at 1636960151241) (:by |u0) (:text |get-dylib-path) - |j $ %{} :Leaf (:at 1636960151241) (:by |u0) (:text "|\"/dylibs/libcalcit_std") + |j $ %{} :Leaf (:at 1715883745892) (:by |u0) (:text "|\"/dylibs/libcalcit_regex") |r $ %{} :Leaf (:at 1636960151241) (:by |u0) (:text "|\"re_find_all") |v $ %{} :Leaf (:at 1636960151241) (:by |u0) (:text |s) |x $ %{} :Leaf (:at 1636960151241) (:by |u0) (:text |pattern) @@ -59,7 +59,7 @@ |j $ %{} :Expr (:at 1636960151241) (:by |u0) :data $ {} |T $ %{} :Leaf (:at 1636960151241) (:by |u0) (:text |get-dylib-path) - |j $ %{} :Leaf (:at 1636960151241) (:by |u0) (:text "|\"/dylibs/libcalcit_std") + |j $ %{} :Leaf (:at 1715883751340) (:by |u0) (:text "|\"/dylibs/libcalcit_regex") |r $ %{} :Leaf (:at 1636960151241) (:by |u0) (:text "|\"re_find_index") |v $ %{} :Leaf (:at 1636960151241) (:by |u0) (:text |s) |x $ %{} :Leaf (:at 1636960151241) (:by |u0) (:text |pattern) @@ -78,7 +78,7 @@ |j $ %{} :Expr (:at 1636960151241) (:by |u0) :data $ {} |T $ %{} :Leaf (:at 1636960151241) (:by |u0) (:text |get-dylib-path) - |j $ %{} :Leaf (:at 1636960151241) (:by |u0) (:text "|\"/dylibs/libcalcit_std") + |j $ %{} :Leaf (:at 1715883754761) (:by |u0) (:text "|\"/dylibs/libcalcit_regex") |r $ %{} :Leaf (:at 1636960151241) (:by |u0) (:text "|\"re_matches") |v $ %{} :Leaf (:at 1636960151241) (:by |u0) (:text |s) |x $ %{} :Leaf (:at 1636960151241) (:by |u0) (:text |pattern) @@ -96,7 +96,7 @@ |b $ %{} :Expr (:at 1715795940312) (:by |u0) :data $ {} |T $ %{} :Leaf (:at 1715795940312) (:by |u0) (:text |get-dylib-path) - |b $ %{} :Leaf (:at 1715795940312) (:by |u0) (:text "|\"/dylibs/libcalcit_std") + |b $ %{} :Leaf (:at 1715883759286) (:by |u0) (:text "|\"/dylibs/libcalcit_regex") |h $ %{} :Leaf (:at 1715795950203) (:by |u0) (:text "|\"re_pattern") |o $ %{} :Leaf (:at 1715795940312) (:by |u0) (:text |pattern) |re-replace-all $ %{} :CodeEntry (:doc |) @@ -115,7 +115,7 @@ |j $ %{} :Expr (:at 1637157540472) (:by |u0) :data $ {} |T $ %{} :Leaf (:at 1637157540472) (:by |u0) (:text |get-dylib-path) - |j $ %{} :Leaf (:at 1637157540472) (:by |u0) (:text "|\"/dylibs/libcalcit_std") + |j $ %{} :Leaf (:at 1715883763269) (:by |u0) (:text "|\"/dylibs/libcalcit_regex") |r $ %{} :Leaf (:at 1637157553738) (:by |u0) (:text "|\"re_replace_all") |v $ %{} :Leaf (:at 1637157540472) (:by |u0) (:text |s) |x $ %{} :Leaf (:at 1637157540472) (:by |u0) (:text |pattern) @@ -135,7 +135,7 @@ |j $ %{} :Expr (:at 1637157527168) (:by |u0) :data $ {} |T $ %{} :Leaf (:at 1637157527168) (:by |u0) (:text |get-dylib-path) - |j $ %{} :Leaf (:at 1637157527168) (:by |u0) (:text "|\"/dylibs/libcalcit_std") + |j $ %{} :Leaf (:at 1715883766379) (:by |u0) (:text "|\"/dylibs/libcalcit_regex") |r $ %{} :Leaf (:at 1637157535118) (:by |u0) (:text "|\"re_split") |v $ %{} :Leaf (:at 1637157527168) (:by |u0) (:text |s) |x $ %{} :Leaf (:at 1637157527168) (:by |u0) (:text |pattern) diff --git a/compact.cirru b/compact.cirru index 109f626..248b181 100644 --- a/compact.cirru +++ b/compact.cirru @@ -9,31 +9,31 @@ |re-find $ %{} :CodeEntry (:doc |) :code $ quote defn re-find (s pattern) - &call-dylib-edn (get-dylib-path "\"/dylibs/libcalcit_std") "\"re_find" s pattern + &call-dylib-edn (get-dylib-path "\"/dylibs/libcalcit_regex") "\"re_find" s pattern |re-find-all $ %{} :CodeEntry (:doc |) :code $ quote defn re-find-all (s pattern) - &call-dylib-edn (get-dylib-path "\"/dylibs/libcalcit_std") "\"re_find_all" s pattern + &call-dylib-edn (get-dylib-path "\"/dylibs/libcalcit_regex") "\"re_find_all" s pattern |re-find-index $ %{} :CodeEntry (:doc |) :code $ quote defn re-find-index (s pattern) - &call-dylib-edn (get-dylib-path "\"/dylibs/libcalcit_std") "\"re_find_index" s pattern + &call-dylib-edn (get-dylib-path "\"/dylibs/libcalcit_regex") "\"re_find_index" s pattern |re-matches $ %{} :CodeEntry (:doc |) :code $ quote defn re-matches (s pattern) - &call-dylib-edn (get-dylib-path "\"/dylibs/libcalcit_std") "\"re_matches" s pattern + &call-dylib-edn (get-dylib-path "\"/dylibs/libcalcit_regex") "\"re_matches" s pattern |re-pattern $ %{} :CodeEntry (:doc |) :code $ quote defn re-pattern (pattern) - &call-dylib-edn (get-dylib-path "\"/dylibs/libcalcit_std") "\"re_pattern" pattern + &call-dylib-edn (get-dylib-path "\"/dylibs/libcalcit_regex") "\"re_pattern" pattern |re-replace-all $ %{} :CodeEntry (:doc |) :code $ quote defn re-replace-all (s pattern next) - &call-dylib-edn (get-dylib-path "\"/dylibs/libcalcit_std") "\"re_replace_all" s pattern next + &call-dylib-edn (get-dylib-path "\"/dylibs/libcalcit_regex") "\"re_replace_all" s pattern next |re-split $ %{} :CodeEntry (:doc |) :code $ quote defn re-split (s pattern) - &call-dylib-edn (get-dylib-path "\"/dylibs/libcalcit_std") "\"re_split" s pattern + &call-dylib-edn (get-dylib-path "\"/dylibs/libcalcit_regex") "\"re_split" s pattern :ns $ %{} :CodeEntry (:doc |) :code $ quote ns regex.core $ :require diff --git a/rust-toolchain.toml b/rust-toolchain.toml new file mode 100644 index 0000000..97cc1c9 --- /dev/null +++ b/rust-toolchain.toml @@ -0,0 +1,3 @@ + +[toolchain] +channel = "stable" From 56f5b9b05a816a7a30ef57fd3f96d85774e20a29 Mon Sep 17 00:00:00 2001 From: tiye Date: Fri, 17 May 2024 02:44:49 +0800 Subject: [PATCH 05/14] give ownership of arc; tag 0.0.5-a4 --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/lib.rs | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 4553eb8..6c2eb2d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -32,7 +32,7 @@ dependencies = [ [[package]] name = "calcit_regex" -version = "0.0.5-a3" +version = "0.0.5-a4" dependencies = [ "cirru_edn", "cirru_parser", diff --git a/Cargo.toml b/Cargo.toml index 11ac02d..99cd17a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "calcit_regex" -version = "0.0.5-a3" +version = "0.0.5-a4" authors = ["jiyinyiyong "] edition = "2021" diff --git a/src/lib.rs b/src/lib.rs index ab3f13e..0528a38 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -16,7 +16,7 @@ pub fn re_pattern(args: Vec) -> Result { Ok(pattern) => { let p = Arc::from(pattern); let p2 = p.to_owned(); - // std::mem::forget(p); + std::mem::forget(p); Ok(Edn::AnyRef(EdnAnyRef(p2))) } Err(e) => Err(format!("re-pattern failed, {}", e)), From f066a4e2ceae28dbe4d4a6fe62c77cbdc7c154b5 Mon Sep 17 00:00:00 2001 From: tiye Date: Sat, 18 May 2024 01:24:10 +0800 Subject: [PATCH 06/14] upgrade edn --- Cargo.lock | 4 ++-- Cargo.toml | 2 +- calcit.cirru | 17 +++++++++++++++++ compact.cirru | 4 ++++ src/lib.rs | 16 ++++++++-------- 5 files changed, 32 insertions(+), 11 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 6c2eb2d..8966b58 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -41,9 +41,9 @@ dependencies = [ [[package]] name = "cirru_edn" -version = "0.6.4-a2" +version = "0.6.4-a3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cdbe878f563d28480a502f3a4fb3bccf6fb5080a4cf00b32ae31f9eb7e89d838" +checksum = "9b6749d9f769097030c64bc06712e048f65ecd90935d6971356a3e65cc145958" dependencies = [ "bincode", "cirru_parser", diff --git a/Cargo.toml b/Cargo.toml index 99cd17a..4b097ea 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,7 +13,7 @@ crate-type = ["dylib"] # Creates dynamic lib # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -cirru_edn = "0.6.4-a2" +cirru_edn = "0.6.4-a3" # cirru_edn = { path = "/Users/chenyong/repo/cirru/edn.rs" } cirru_parser = "0.1.29" regex = "1.10.4" diff --git a/calcit.cirru b/calcit.cirru index 10f9aa1..feb7038 100644 --- a/calcit.cirru +++ b/calcit.cirru @@ -6,6 +6,23 @@ :files $ {} |regex.core $ %{} :FileEntry :defs $ {} + |re-drop $ %{} :CodeEntry (:doc |) + :code $ %{} :Expr (:at 1715964831711) (:by |u0) + :data $ {} + |T $ %{} :Leaf (:at 1715964831711) (:by |u0) (:text |defn) + |b $ %{} :Leaf (:at 1715964831711) (:by |u0) (:text |re-drop) + |h $ %{} :Expr (:at 1715964838668) (:by |u0) + :data $ {} + |T $ %{} :Leaf (:at 1715964838668) (:by |u0) (:text |pattern) + |l $ %{} :Expr (:at 1715964838668) (:by |u0) + :data $ {} + |T $ %{} :Leaf (:at 1715964838668) (:by |u0) (:text |&call-dylib-edn) + |b $ %{} :Expr (:at 1715964838668) (:by |u0) + :data $ {} + |T $ %{} :Leaf (:at 1715964838668) (:by |u0) (:text |get-dylib-path) + |b $ %{} :Leaf (:at 1715964838668) (:by |u0) (:text "|\"/dylibs/libcalcit_regex") + |h $ %{} :Leaf (:at 1715964846736) (:by |u0) (:text "|\"re_drop") + |l $ %{} :Leaf (:at 1715964838668) (:by |u0) (:text |pattern) |re-find $ %{} :CodeEntry (:doc |) :code $ %{} :Expr (:at 1636960151241) (:by |u0) :data $ {} diff --git a/compact.cirru b/compact.cirru index 248b181..e38879a 100644 --- a/compact.cirru +++ b/compact.cirru @@ -6,6 +6,10 @@ :files $ {} |regex.core $ %{} :FileEntry :defs $ {} + |re-drop $ %{} :CodeEntry (:doc |) + :code $ quote + defn re-drop (pattern) + &call-dylib-edn (get-dylib-path "\"/dylibs/libcalcit_regex") "\"re_drop" pattern |re-find $ %{} :CodeEntry (:doc |) :code $ quote defn re-find (s pattern) diff --git a/src/lib.rs b/src/lib.rs index 0528a38..31544a2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,4 +1,4 @@ -use std::sync::Arc; +use std::{cell::RefCell, sync::Arc}; use cirru_edn::{Edn, EdnAnyRef, EdnListView}; use regex::Regex; @@ -14,7 +14,7 @@ pub fn re_pattern(args: Vec) -> Result { match &args[0] { Edn::Str(s) => match Regex::new(s) { Ok(pattern) => { - let p = Arc::from(pattern); + let p = Arc::from(RefCell::new(pattern)); let p2 = p.to_owned(); std::mem::forget(p); Ok(Edn::AnyRef(EdnAnyRef(p2))) @@ -37,7 +37,7 @@ pub fn re_matches(args: Vec) -> Result { Err(e) => Err(format!("re-matches failed, {}", e)), }, (Edn::Str(s), Edn::AnyRef(EdnAnyRef(p))) => { - if let Some(pattern) = p.downcast_ref::() { + if let Some(pattern) = p.borrow().downcast_ref::() { Ok(Edn::Bool(pattern.is_match(s))) } else { Err(format!("re-matches expected a regex, got {:?}", p)) @@ -64,7 +64,7 @@ pub fn re_find_index(args: Vec) -> Result { } } (Edn::Str(s), Edn::AnyRef(EdnAnyRef(p))) => { - if let Some(pattern) = p.downcast_ref::() { + if let Some(pattern) = p.borrow().downcast_ref::() { match pattern.find(s) { Some(matched) => Ok(Edn::Number(matched.start() as f64)), None => Ok(Edn::Number(-1.0)), // TODO maybe nil @@ -98,7 +98,7 @@ pub fn re_find(args: Vec) -> Result { } } (Edn::Str(s), Edn::AnyRef(EdnAnyRef(p))) => { - if let Some(pattern) = p.downcast_ref::() { + if let Some(pattern) = p.borrow().downcast_ref::() { let mut matched = pattern.find_iter(s); match matched.next() { Some(v) => Ok(Edn::str(v.as_str().to_string())), @@ -130,7 +130,7 @@ pub fn re_find_all(args: Vec) -> Result { Err(e) => Err(format!("re-find-all failed, {}", e)), }, (Edn::Str(s), Edn::AnyRef(EdnAnyRef(p))) => { - if let Some(pattern) = p.downcast_ref::() { + if let Some(pattern) = p.borrow().downcast_ref::() { let mut ys: Vec = vec![]; for v in pattern.find_iter(s) { ys.push(Edn::Str(v.as_str().to_string().into())) @@ -162,7 +162,7 @@ pub fn re_split(args: Vec) -> Result { Err(e) => Err(format!("re-split failed, {}", e)), }, (Edn::Str(s), Edn::AnyRef(EdnAnyRef(p))) => { - if let Some(pattern) = p.downcast_ref::() { + if let Some(pattern) = p.borrow().downcast_ref::() { let mut ys: Vec = vec![]; for piece in pattern.split(s) { ys.push(Edn::str(piece)); @@ -188,7 +188,7 @@ pub fn re_replace_all(args: Vec) -> Result { Err(e) => Err(format!("re-replace-all failed, {}", e)), }, (Edn::Str(s), Edn::AnyRef(EdnAnyRef(p)), Edn::Str(next)) => { - if let Some(pattern) = p.downcast_ref::() { + if let Some(pattern) = p.borrow().downcast_ref::() { Ok(Edn::str(pattern.replace_all(s, &**next).into_owned())) } else { Err(format!("re-replace-all expected a regex, got {:?}", p)) From a4402782956071d3e2bcedea8819d97c06185a4e Mon Sep 17 00:00:00 2001 From: tiye Date: Sat, 18 May 2024 02:23:26 +0800 Subject: [PATCH 07/14] track pattern with vec --- Cargo.lock | 5 +++-- Cargo.toml | 3 ++- calcit.cirru | 15 +++++++++++++++ compact.cirru | 4 +++- src/lib.rs | 49 +++++++++++++++++++++++++++++++++++++++++++++---- 5 files changed, 68 insertions(+), 8 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 8966b58..d55e62a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -36,14 +36,15 @@ version = "0.0.5-a4" dependencies = [ "cirru_edn", "cirru_parser", + "lazy_static", "regex", ] [[package]] name = "cirru_edn" -version = "0.6.4-a3" +version = "0.6.4-a4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9b6749d9f769097030c64bc06712e048f65ecd90935d6971356a3e65cc145958" +checksum = "cb50912f507b3426bae64f6c15d1c5849ce5be46511afc5709db5876e3a38cef" dependencies = [ "bincode", "cirru_parser", diff --git a/Cargo.toml b/Cargo.toml index 4b097ea..18c5cd9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,10 +13,11 @@ crate-type = ["dylib"] # Creates dynamic lib # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -cirru_edn = "0.6.4-a3" +cirru_edn = "0.6.4-a4" # cirru_edn = { path = "/Users/chenyong/repo/cirru/edn.rs" } cirru_parser = "0.1.29" regex = "1.10.4" +lazy_static = "1.4.0" # [profile.release] # debug = true diff --git a/calcit.cirru b/calcit.cirru index feb7038..c3e26e3 100644 --- a/calcit.cirru +++ b/calcit.cirru @@ -408,6 +408,20 @@ |b $ %{} :Leaf (:at 1715796262291) (:by |u0) (:text ||1ab22c333) |h $ %{} :Leaf (:at 1715796267870) (:by |u0) (:text |pattern) |l $ %{} :Leaf (:at 1715796262291) (:by |u0) (:text "|\"X") + |t $ %{} :Expr (:at 1715969898334) (:by |u0) + :data $ {} + |D $ %{} :Leaf (:at 1715969899030) (:by |u0) (:text |w-log) + |T $ %{} :Expr (:at 1715969887322) (:by |u0) + :data $ {} + |T $ %{} :Leaf (:at 1715969887538) (:by |u0) (:text |re-drop) + |b $ %{} :Leaf (:at 1715969889794) (:by |u0) (:text |pattern) + |u $ %{} :Expr (:at 1715969895351) (:by |u0) + :data $ {} + |D $ %{} :Leaf (:at 1715969896514) (:by |u0) (:text |w-log) + |T $ %{} :Expr (:at 1715969887322) (:by |u0) + :data $ {} + |T $ %{} :Leaf (:at 1715969887538) (:by |u0) (:text |re-drop) + |b $ %{} :Leaf (:at 1715969889794) (:by |u0) (:text |pattern) :ns $ %{} :CodeEntry (:doc |) :code $ %{} :Expr (:at 1636960219938) (:by |u0) :data $ {} @@ -429,6 +443,7 @@ |x $ %{} :Leaf (:at 1637157659783) (:by |u0) (:text |re-split) |y $ %{} :Leaf (:at 1637157663541) (:by |u0) (:text |re-replace-all) |z $ %{} :Leaf (:at 1715795979178) (:by |u0) (:text |re-pattern) + |zD $ %{} :Leaf (:at 1715969884579) (:by |u0) (:text |re-drop) |r $ %{} :Expr (:at 1636960219938) (:by |u0) :data $ {} |T $ %{} :Leaf (:at 1636960219938) (:by |u0) (:text |regex.$meta) diff --git a/compact.cirru b/compact.cirru index e38879a..c2a2501 100644 --- a/compact.cirru +++ b/compact.cirru @@ -70,10 +70,12 @@ assert= "\"22" $ re-find |q22 pattern assert= ([] |1 |2 |3) (re-find-all |1q2q3 pattern) assert= |XabXcX $ re-replace-all |1ab22c333 pattern "\"X" + w-log $ re-drop pattern + w-log $ re-drop pattern :ns $ %{} :CodeEntry (:doc |) :code $ quote ns regex.test $ :require - regex.core :refer $ re-matches re-find-index re-find re-find-all re-split re-replace-all re-pattern + regex.core :refer $ re-matches re-find-index re-find re-find-all re-split re-replace-all re-pattern re-drop regex.$meta :refer $ calcit-dirname calcit-filename |regex.util $ %{} :FileEntry :defs $ {} diff --git a/src/lib.rs b/src/lib.rs index 31544a2..d94f626 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,8 +1,16 @@ -use std::{cell::RefCell, sync::Arc}; +use std::{ + cell::RefCell, + sync::{Arc, Mutex}, +}; use cirru_edn::{Edn, EdnAnyRef, EdnListView}; +use lazy_static::lazy_static; use regex::Regex; +lazy_static! { + static ref PATTERNS: Mutex> = Mutex::from(vec![]); +} + #[no_mangle] pub fn abi_version() -> String { String::from("0.0.8") @@ -15,9 +23,11 @@ pub fn re_pattern(args: Vec) -> Result { Edn::Str(s) => match Regex::new(s) { Ok(pattern) => { let p = Arc::from(RefCell::new(pattern)); - let p2 = p.to_owned(); - std::mem::forget(p); - Ok(Edn::AnyRef(EdnAnyRef(p2))) + // push to PATTERNS + let v = EdnAnyRef(p); + let mut patterns = PATTERNS.lock().unwrap(); + patterns.push(v.to_owned()); + Ok(Edn::AnyRef(v)) } Err(e) => Err(format!("re-pattern failed, {}", e)), }, @@ -28,6 +38,37 @@ pub fn re_pattern(args: Vec) -> Result { } } +/// re_drop drops pattern from global PATTERNS +#[no_mangle] +pub fn re_drop(args: Vec) -> Result { + if args.len() == 1 { + match &args[0] { + Edn::AnyRef(p) => { + let mut patterns = PATTERNS.lock().unwrap(); + let mut i = 0; + let mut found = false; + for v in patterns.iter() { + if v == p { + found = true; + break; + } + i += 1; + } + println!("re-drop found {} at {}\n", found, i); + if found { + patterns.remove(i); + Ok(Edn::from(true)) + } else { + Ok(Edn::from(false)) + } + } + _ => Err(format!("re-drop expect 1 pattern, got {:?}", args)), + } + } else { + Err(format!("re-drop expect 1 pattern, got {:?}", args)) + } +} + #[no_mangle] pub fn re_matches(args: Vec) -> Result { if args.len() == 2 { From 2ce5772ec9017f7516d6464b8c7bdfaac9fd7821 Mon Sep 17 00:00:00 2001 From: tiye Date: Sat, 18 May 2024 13:20:10 +0800 Subject: [PATCH 08/14] try extra API for holding memory --- Cargo.lock | 2 -- Cargo.toml | 4 ++-- calcit.cirru | 32 +++++++++++++++++++++++++++++++- compact.cirru | 11 +++++++++-- src/lib.rs | 10 ++++++++++ 5 files changed, 52 insertions(+), 7 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d55e62a..1bbacd7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -43,8 +43,6 @@ dependencies = [ [[package]] name = "cirru_edn" version = "0.6.4-a4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cb50912f507b3426bae64f6c15d1c5849ce5be46511afc5709db5876e3a38cef" dependencies = [ "bincode", "cirru_parser", diff --git a/Cargo.toml b/Cargo.toml index 18c5cd9..8bc9d94 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,8 +13,8 @@ crate-type = ["dylib"] # Creates dynamic lib # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -cirru_edn = "0.6.4-a4" -# cirru_edn = { path = "/Users/chenyong/repo/cirru/edn.rs" } +# cirru_edn = "0.6.4-a4" +cirru_edn = { path = "/Users/chenyong/repo/cirru/edn.rs" } cirru_parser = "0.1.29" regex = "1.10.4" lazy_static = "1.4.0" diff --git a/calcit.cirru b/calcit.cirru index c3e26e3..58d078e 100644 --- a/calcit.cirru +++ b/calcit.cirru @@ -6,6 +6,25 @@ :files $ {} |regex.core $ %{} :FileEntry :defs $ {} + |hold-patterns-memory! $ %{} :CodeEntry (:doc |) + :code $ %{} :Expr (:at 1716009462651) (:by |u0) + :data $ {} + |T $ %{} :Leaf (:at 1716009462651) (:by |u0) (:text |defn) + |b $ %{} :Leaf (:at 1716009462651) (:by |u0) (:text |hold-patterns-memory!) + |h $ %{} :Expr (:at 1716009462651) (:by |u0) + :data $ {} + |l $ %{} :Expr (:at 1716009475106) (:by |u0) + :data $ {} + |T $ %{} :Leaf (:at 1716009475106) (:by |u0) (:text |&call-dylib-edn) + |b $ %{} :Expr (:at 1716009475106) (:by |u0) + :data $ {} + |T $ %{} :Leaf (:at 1716009475106) (:by |u0) (:text |get-dylib-path) + |b $ %{} :Leaf (:at 1716009475106) (:by |u0) (:text "|\"/dylibs/libcalcit_regex") + |h $ %{} :Leaf (:at 1716009480713) (:by |u0) (:text "|\"holding_patterns_memory") + |o $ %{} :Expr (:at 1716009497048) (:by |u0) + :data $ {} + |T $ %{} :Leaf (:at 1716009498134) (:by |u0) (:text |println) + |b $ %{} :Leaf (:at 1716009528860) (:by |u0) (:text "|\"memory of regex patterns are set to hold!") |re-drop $ %{} :CodeEntry (:doc |) :code $ %{} :Expr (:at 1715964831711) (:by |u0) :data $ {} @@ -331,6 +350,9 @@ :data $ {} |T $ %{} :Leaf (:at 1715796183478) (:by |u0) (:text |println) |b $ %{} :Leaf (:at 1715796196505) (:by |u0) (:text "|\"%%% test variable holding regex") + |yzD $ %{} :Expr (:at 1716009493262) (:by |u0) + :data $ {} + |T $ %{} :Leaf (:at 1716009493262) (:by |u0) (:text |hold-patterns-memory!) |z $ %{} :Expr (:at 1715795960911) (:by |u0) :data $ {} |T $ %{} :Leaf (:at 1715795963731) (:by |u0) (:text |let) @@ -343,6 +365,13 @@ :data $ {} |T $ %{} :Leaf (:at 1715795972580) (:by |u0) (:text |re-pattern) |b $ %{} :Leaf (:at 1715795974817) (:by |u0) (:text "|\"\\d+") + |b $ %{} :Expr (:at 1716006619232) (:by |u0) + :data $ {} + |T $ %{} :Leaf (:at 1716006620455) (:by |u0) (:text |p2) + |b $ %{} :Expr (:at 1716006621237) (:by |u0) + :data $ {} + |T $ %{} :Leaf (:at 1716006623615) (:by |u0) (:text |re-pattern) + |b $ %{} :Leaf (:at 1716006626938) (:by |u0) (:text "|\"\\w+") |e $ %{} :Expr (:at 1715797292331) (:by |u0) :data $ {} |T $ %{} :Leaf (:at 1715797294062) (:by |u0) (:text |println) @@ -414,7 +443,7 @@ |T $ %{} :Expr (:at 1715969887322) (:by |u0) :data $ {} |T $ %{} :Leaf (:at 1715969887538) (:by |u0) (:text |re-drop) - |b $ %{} :Leaf (:at 1715969889794) (:by |u0) (:text |pattern) + |b $ %{} :Leaf (:at 1716006630983) (:by |u0) (:text |p2) |u $ %{} :Expr (:at 1715969895351) (:by |u0) :data $ {} |D $ %{} :Leaf (:at 1715969896514) (:by |u0) (:text |w-log) @@ -444,6 +473,7 @@ |y $ %{} :Leaf (:at 1637157663541) (:by |u0) (:text |re-replace-all) |z $ %{} :Leaf (:at 1715795979178) (:by |u0) (:text |re-pattern) |zD $ %{} :Leaf (:at 1715969884579) (:by |u0) (:text |re-drop) + |zP $ %{} :Leaf (:at 1716009488667) (:by |u0) (:text |hold-patterns-memory!) |r $ %{} :Expr (:at 1636960219938) (:by |u0) :data $ {} |T $ %{} :Leaf (:at 1636960219938) (:by |u0) (:text |regex.$meta) diff --git a/compact.cirru b/compact.cirru index c2a2501..2e117b2 100644 --- a/compact.cirru +++ b/compact.cirru @@ -6,6 +6,11 @@ :files $ {} |regex.core $ %{} :FileEntry :defs $ {} + |hold-patterns-memory! $ %{} :CodeEntry (:doc |) + :code $ quote + defn hold-patterns-memory! () + &call-dylib-edn (get-dylib-path "\"/dylibs/libcalcit_regex") "\"holding_patterns_memory" + println "\"memory of regex patterns are set to hold!" |re-drop $ %{} :CodeEntry (:doc |) :code $ quote defn re-drop (pattern) @@ -61,8 +66,10 @@ assert= |1abXcX3 $ re-replace-all |1ab22c333 |\d{2} "\"X" assert= ([] "\"1ab" "\"c" "\"3") (re-split |1ab22c333 |\d{2}) println "\"%%% test variable holding regex" + hold-patterns-memory! let pattern $ re-pattern "\"\\d+" + p2 $ re-pattern "\"\\w+" println "\"Pattern is:" pattern assert= true $ re-matches |2 pattern assert= true $ re-matches |23 pattern @@ -70,12 +77,12 @@ assert= "\"22" $ re-find |q22 pattern assert= ([] |1 |2 |3) (re-find-all |1q2q3 pattern) assert= |XabXcX $ re-replace-all |1ab22c333 pattern "\"X" - w-log $ re-drop pattern + w-log $ re-drop p2 w-log $ re-drop pattern :ns $ %{} :CodeEntry (:doc |) :code $ quote ns regex.test $ :require - regex.core :refer $ re-matches re-find-index re-find re-find-all re-split re-replace-all re-pattern re-drop + regex.core :refer $ re-matches re-find-index re-find re-find-all re-split re-replace-all re-pattern re-drop hold-patterns-memory! regex.$meta :refer $ calcit-dirname calcit-filename |regex.util $ %{} :FileEntry :defs $ {} diff --git a/src/lib.rs b/src/lib.rs index d94f626..13c5281 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -16,6 +16,15 @@ pub fn abi_version() -> String { String::from("0.0.8") } +/// just call mem::forget to avoid GC +#[no_mangle] +pub fn holding_patterns_memory() -> Result { + let patterns = PATTERNS.lock().unwrap(); + let data = patterns.to_owned(); + std::mem::forget(data); + Ok(Edn::Nil) +} + #[no_mangle] pub fn re_pattern(args: Vec) -> Result { if args.len() == 1 { @@ -47,6 +56,7 @@ pub fn re_drop(args: Vec) -> Result { let mut patterns = PATTERNS.lock().unwrap(); let mut i = 0; let mut found = false; + println!("patterns size {}", patterns.len()); for v in patterns.iter() { if v == p { found = true; From 76fb39058f6c8f9bb08082a2304dbe5faf8571ab Mon Sep 17 00:00:00 2001 From: tiye Date: Sat, 18 May 2024 14:33:01 +0800 Subject: [PATCH 09/14] upgrade edn; no memory holding does not work --- .github/workflows/check.yaml | 2 +- Cargo.lock | 4 +++- Cargo.toml | 4 ++-- calcit.cirru | 30 +++++++----------------------- compact.cirru | 9 ++------- src/lib.rs | 9 --------- 6 files changed, 15 insertions(+), 43 deletions(-) diff --git a/.github/workflows/check.yaml b/.github/workflows/check.yaml index 37eebb0..e301018 100644 --- a/.github/workflows/check.yaml +++ b/.github/workflows/check.yaml @@ -16,7 +16,7 @@ jobs: - uses: calcit-lang/setup-cr@0.0.3 with: - version: "0.8.51-a4" + version: "0.8.51-a6" - uses: dtolnay/rust-toolchain@stable with: diff --git a/Cargo.lock b/Cargo.lock index 1bbacd7..0374f4a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -42,7 +42,9 @@ dependencies = [ [[package]] name = "cirru_edn" -version = "0.6.4-a4" +version = "0.6.4-a5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "500776efb8ce469a6da13b3ab01f945644aca2b7803fcbad4c4139d10654e9e8" dependencies = [ "bincode", "cirru_parser", diff --git a/Cargo.toml b/Cargo.toml index 8bc9d94..e820300 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,8 +13,8 @@ crate-type = ["dylib"] # Creates dynamic lib # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -# cirru_edn = "0.6.4-a4" -cirru_edn = { path = "/Users/chenyong/repo/cirru/edn.rs" } +cirru_edn = "0.6.4-a5" +# cirru_edn = { path = "/Users/chenyong/repo/cirru/edn.rs" } cirru_parser = "0.1.29" regex = "1.10.4" lazy_static = "1.4.0" diff --git a/calcit.cirru b/calcit.cirru index 58d078e..a780121 100644 --- a/calcit.cirru +++ b/calcit.cirru @@ -6,25 +6,6 @@ :files $ {} |regex.core $ %{} :FileEntry :defs $ {} - |hold-patterns-memory! $ %{} :CodeEntry (:doc |) - :code $ %{} :Expr (:at 1716009462651) (:by |u0) - :data $ {} - |T $ %{} :Leaf (:at 1716009462651) (:by |u0) (:text |defn) - |b $ %{} :Leaf (:at 1716009462651) (:by |u0) (:text |hold-patterns-memory!) - |h $ %{} :Expr (:at 1716009462651) (:by |u0) - :data $ {} - |l $ %{} :Expr (:at 1716009475106) (:by |u0) - :data $ {} - |T $ %{} :Leaf (:at 1716009475106) (:by |u0) (:text |&call-dylib-edn) - |b $ %{} :Expr (:at 1716009475106) (:by |u0) - :data $ {} - |T $ %{} :Leaf (:at 1716009475106) (:by |u0) (:text |get-dylib-path) - |b $ %{} :Leaf (:at 1716009475106) (:by |u0) (:text "|\"/dylibs/libcalcit_regex") - |h $ %{} :Leaf (:at 1716009480713) (:by |u0) (:text "|\"holding_patterns_memory") - |o $ %{} :Expr (:at 1716009497048) (:by |u0) - :data $ {} - |T $ %{} :Leaf (:at 1716009498134) (:by |u0) (:text |println) - |b $ %{} :Leaf (:at 1716009528860) (:by |u0) (:text "|\"memory of regex patterns are set to hold!") |re-drop $ %{} :CodeEntry (:doc |) :code $ %{} :Expr (:at 1715964831711) (:by |u0) :data $ {} @@ -350,9 +331,6 @@ :data $ {} |T $ %{} :Leaf (:at 1715796183478) (:by |u0) (:text |println) |b $ %{} :Leaf (:at 1715796196505) (:by |u0) (:text "|\"%%% test variable holding regex") - |yzD $ %{} :Expr (:at 1716009493262) (:by |u0) - :data $ {} - |T $ %{} :Leaf (:at 1716009493262) (:by |u0) (:text |hold-patterns-memory!) |z $ %{} :Expr (:at 1715795960911) (:by |u0) :data $ {} |T $ %{} :Leaf (:at 1715795963731) (:by |u0) (:text |let) @@ -451,6 +429,13 @@ :data $ {} |T $ %{} :Leaf (:at 1715969887538) (:by |u0) (:text |re-drop) |b $ %{} :Leaf (:at 1715969889794) (:by |u0) (:text |pattern) + |v $ %{} :Expr (:at 1715969895351) (:by |u0) + :data $ {} + |D $ %{} :Leaf (:at 1715969896514) (:by |u0) (:text |w-log) + |T $ %{} :Expr (:at 1715969887322) (:by |u0) + :data $ {} + |T $ %{} :Leaf (:at 1715969887538) (:by |u0) (:text |re-drop) + |b $ %{} :Leaf (:at 1715969889794) (:by |u0) (:text |pattern) :ns $ %{} :CodeEntry (:doc |) :code $ %{} :Expr (:at 1636960219938) (:by |u0) :data $ {} @@ -473,7 +458,6 @@ |y $ %{} :Leaf (:at 1637157663541) (:by |u0) (:text |re-replace-all) |z $ %{} :Leaf (:at 1715795979178) (:by |u0) (:text |re-pattern) |zD $ %{} :Leaf (:at 1715969884579) (:by |u0) (:text |re-drop) - |zP $ %{} :Leaf (:at 1716009488667) (:by |u0) (:text |hold-patterns-memory!) |r $ %{} :Expr (:at 1636960219938) (:by |u0) :data $ {} |T $ %{} :Leaf (:at 1636960219938) (:by |u0) (:text |regex.$meta) diff --git a/compact.cirru b/compact.cirru index 2e117b2..e952707 100644 --- a/compact.cirru +++ b/compact.cirru @@ -6,11 +6,6 @@ :files $ {} |regex.core $ %{} :FileEntry :defs $ {} - |hold-patterns-memory! $ %{} :CodeEntry (:doc |) - :code $ quote - defn hold-patterns-memory! () - &call-dylib-edn (get-dylib-path "\"/dylibs/libcalcit_regex") "\"holding_patterns_memory" - println "\"memory of regex patterns are set to hold!" |re-drop $ %{} :CodeEntry (:doc |) :code $ quote defn re-drop (pattern) @@ -66,7 +61,6 @@ assert= |1abXcX3 $ re-replace-all |1ab22c333 |\d{2} "\"X" assert= ([] "\"1ab" "\"c" "\"3") (re-split |1ab22c333 |\d{2}) println "\"%%% test variable holding regex" - hold-patterns-memory! let pattern $ re-pattern "\"\\d+" p2 $ re-pattern "\"\\w+" @@ -79,10 +73,11 @@ assert= |XabXcX $ re-replace-all |1ab22c333 pattern "\"X" w-log $ re-drop p2 w-log $ re-drop pattern + w-log $ re-drop pattern :ns $ %{} :CodeEntry (:doc |) :code $ quote ns regex.test $ :require - regex.core :refer $ re-matches re-find-index re-find re-find-all re-split re-replace-all re-pattern re-drop hold-patterns-memory! + regex.core :refer $ re-matches re-find-index re-find re-find-all re-split re-replace-all re-pattern re-drop regex.$meta :refer $ calcit-dirname calcit-filename |regex.util $ %{} :FileEntry :defs $ {} diff --git a/src/lib.rs b/src/lib.rs index 13c5281..77da605 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -16,15 +16,6 @@ pub fn abi_version() -> String { String::from("0.0.8") } -/// just call mem::forget to avoid GC -#[no_mangle] -pub fn holding_patterns_memory() -> Result { - let patterns = PATTERNS.lock().unwrap(); - let data = patterns.to_owned(); - std::mem::forget(data); - Ok(Edn::Nil) -} - #[no_mangle] pub fn re_pattern(args: Vec) -> Result { if args.len() == 1 { From 677cd59750498f12accebdcde2b84ddad3baa283 Mon Sep 17 00:00:00 2001 From: tiye Date: Sat, 18 May 2024 14:35:50 +0800 Subject: [PATCH 10/14] experiment on "no drop" --- calcit.cirru | 29 ----------------------------- compact.cirru | 6 +----- src/lib.rs | 49 +++---------------------------------------------- 3 files changed, 4 insertions(+), 80 deletions(-) diff --git a/calcit.cirru b/calcit.cirru index a780121..feb7038 100644 --- a/calcit.cirru +++ b/calcit.cirru @@ -343,13 +343,6 @@ :data $ {} |T $ %{} :Leaf (:at 1715795972580) (:by |u0) (:text |re-pattern) |b $ %{} :Leaf (:at 1715795974817) (:by |u0) (:text "|\"\\d+") - |b $ %{} :Expr (:at 1716006619232) (:by |u0) - :data $ {} - |T $ %{} :Leaf (:at 1716006620455) (:by |u0) (:text |p2) - |b $ %{} :Expr (:at 1716006621237) (:by |u0) - :data $ {} - |T $ %{} :Leaf (:at 1716006623615) (:by |u0) (:text |re-pattern) - |b $ %{} :Leaf (:at 1716006626938) (:by |u0) (:text "|\"\\w+") |e $ %{} :Expr (:at 1715797292331) (:by |u0) :data $ {} |T $ %{} :Leaf (:at 1715797294062) (:by |u0) (:text |println) @@ -415,27 +408,6 @@ |b $ %{} :Leaf (:at 1715796262291) (:by |u0) (:text ||1ab22c333) |h $ %{} :Leaf (:at 1715796267870) (:by |u0) (:text |pattern) |l $ %{} :Leaf (:at 1715796262291) (:by |u0) (:text "|\"X") - |t $ %{} :Expr (:at 1715969898334) (:by |u0) - :data $ {} - |D $ %{} :Leaf (:at 1715969899030) (:by |u0) (:text |w-log) - |T $ %{} :Expr (:at 1715969887322) (:by |u0) - :data $ {} - |T $ %{} :Leaf (:at 1715969887538) (:by |u0) (:text |re-drop) - |b $ %{} :Leaf (:at 1716006630983) (:by |u0) (:text |p2) - |u $ %{} :Expr (:at 1715969895351) (:by |u0) - :data $ {} - |D $ %{} :Leaf (:at 1715969896514) (:by |u0) (:text |w-log) - |T $ %{} :Expr (:at 1715969887322) (:by |u0) - :data $ {} - |T $ %{} :Leaf (:at 1715969887538) (:by |u0) (:text |re-drop) - |b $ %{} :Leaf (:at 1715969889794) (:by |u0) (:text |pattern) - |v $ %{} :Expr (:at 1715969895351) (:by |u0) - :data $ {} - |D $ %{} :Leaf (:at 1715969896514) (:by |u0) (:text |w-log) - |T $ %{} :Expr (:at 1715969887322) (:by |u0) - :data $ {} - |T $ %{} :Leaf (:at 1715969887538) (:by |u0) (:text |re-drop) - |b $ %{} :Leaf (:at 1715969889794) (:by |u0) (:text |pattern) :ns $ %{} :CodeEntry (:doc |) :code $ %{} :Expr (:at 1636960219938) (:by |u0) :data $ {} @@ -457,7 +429,6 @@ |x $ %{} :Leaf (:at 1637157659783) (:by |u0) (:text |re-split) |y $ %{} :Leaf (:at 1637157663541) (:by |u0) (:text |re-replace-all) |z $ %{} :Leaf (:at 1715795979178) (:by |u0) (:text |re-pattern) - |zD $ %{} :Leaf (:at 1715969884579) (:by |u0) (:text |re-drop) |r $ %{} :Expr (:at 1636960219938) (:by |u0) :data $ {} |T $ %{} :Leaf (:at 1636960219938) (:by |u0) (:text |regex.$meta) diff --git a/compact.cirru b/compact.cirru index e952707..e38879a 100644 --- a/compact.cirru +++ b/compact.cirru @@ -63,7 +63,6 @@ println "\"%%% test variable holding regex" let pattern $ re-pattern "\"\\d+" - p2 $ re-pattern "\"\\w+" println "\"Pattern is:" pattern assert= true $ re-matches |2 pattern assert= true $ re-matches |23 pattern @@ -71,13 +70,10 @@ assert= "\"22" $ re-find |q22 pattern assert= ([] |1 |2 |3) (re-find-all |1q2q3 pattern) assert= |XabXcX $ re-replace-all |1ab22c333 pattern "\"X" - w-log $ re-drop p2 - w-log $ re-drop pattern - w-log $ re-drop pattern :ns $ %{} :CodeEntry (:doc |) :code $ quote ns regex.test $ :require - regex.core :refer $ re-matches re-find-index re-find re-find-all re-split re-replace-all re-pattern re-drop + regex.core :refer $ re-matches re-find-index re-find re-find-all re-split re-replace-all re-pattern regex.$meta :refer $ calcit-dirname calcit-filename |regex.util $ %{} :FileEntry :defs $ {} diff --git a/src/lib.rs b/src/lib.rs index 77da605..cc0726b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,16 +1,8 @@ -use std::{ - cell::RefCell, - sync::{Arc, Mutex}, -}; +use std::{cell::RefCell, sync::Arc}; use cirru_edn::{Edn, EdnAnyRef, EdnListView}; -use lazy_static::lazy_static; use regex::Regex; -lazy_static! { - static ref PATTERNS: Mutex> = Mutex::from(vec![]); -} - #[no_mangle] pub fn abi_version() -> String { String::from("0.0.8") @@ -23,11 +15,8 @@ pub fn re_pattern(args: Vec) -> Result { Edn::Str(s) => match Regex::new(s) { Ok(pattern) => { let p = Arc::from(RefCell::new(pattern)); - // push to PATTERNS - let v = EdnAnyRef(p); - let mut patterns = PATTERNS.lock().unwrap(); - patterns.push(v.to_owned()); - Ok(Edn::AnyRef(v)) + + Ok(Edn::AnyRef(EdnAnyRef(p))) } Err(e) => Err(format!("re-pattern failed, {}", e)), }, @@ -38,38 +27,6 @@ pub fn re_pattern(args: Vec) -> Result { } } -/// re_drop drops pattern from global PATTERNS -#[no_mangle] -pub fn re_drop(args: Vec) -> Result { - if args.len() == 1 { - match &args[0] { - Edn::AnyRef(p) => { - let mut patterns = PATTERNS.lock().unwrap(); - let mut i = 0; - let mut found = false; - println!("patterns size {}", patterns.len()); - for v in patterns.iter() { - if v == p { - found = true; - break; - } - i += 1; - } - println!("re-drop found {} at {}\n", found, i); - if found { - patterns.remove(i); - Ok(Edn::from(true)) - } else { - Ok(Edn::from(false)) - } - } - _ => Err(format!("re-drop expect 1 pattern, got {:?}", args)), - } - } else { - Err(format!("re-drop expect 1 pattern, got {:?}", args)) - } -} - #[no_mangle] pub fn re_matches(args: Vec) -> Result { if args.len() == 2 { From 0a4ae70220de78cfeb2f545a1f76a2e3327714f4 Mon Sep 17 00:00:00 2001 From: tiye Date: Sat, 18 May 2024 21:16:31 +0800 Subject: [PATCH 11/14] bump 0.0.5 --- .github/workflows/check.yaml | 2 +- Cargo.lock | 6 +++--- Cargo.toml | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/.github/workflows/check.yaml b/.github/workflows/check.yaml index e301018..8515fd1 100644 --- a/.github/workflows/check.yaml +++ b/.github/workflows/check.yaml @@ -16,7 +16,7 @@ jobs: - uses: calcit-lang/setup-cr@0.0.3 with: - version: "0.8.51-a6" + version: "0.8.51" - uses: dtolnay/rust-toolchain@stable with: diff --git a/Cargo.lock b/Cargo.lock index 0374f4a..70c4437 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -32,7 +32,7 @@ dependencies = [ [[package]] name = "calcit_regex" -version = "0.0.5-a4" +version = "0.0.5" dependencies = [ "cirru_edn", "cirru_parser", @@ -42,9 +42,9 @@ dependencies = [ [[package]] name = "cirru_edn" -version = "0.6.4-a5" +version = "0.6.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "500776efb8ce469a6da13b3ab01f945644aca2b7803fcbad4c4139d10654e9e8" +checksum = "cf2af443516dfa62939fbfe82812601c1cb5fc89fdf7356eee519d7b056a264d" dependencies = [ "bincode", "cirru_parser", diff --git a/Cargo.toml b/Cargo.toml index e820300..ab111da 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "calcit_regex" -version = "0.0.5-a4" +version = "0.0.5" authors = ["jiyinyiyong "] edition = "2021" @@ -13,7 +13,7 @@ crate-type = ["dylib"] # Creates dynamic lib # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -cirru_edn = "0.6.4-a5" +cirru_edn = "0.6.5" # cirru_edn = { path = "/Users/chenyong/repo/cirru/edn.rs" } cirru_parser = "0.1.29" regex = "1.10.4" From 3306cb09ac7783a7cefb2d5a2fe988c3427ef545 Mon Sep 17 00:00:00 2001 From: tiye Date: Sat, 18 May 2024 21:19:26 +0800 Subject: [PATCH 12/14] add demo of re-pattern usage --- README.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/README.md b/README.md index 23a1aac..556586e 100644 --- a/README.md +++ b/README.md @@ -30,6 +30,12 @@ regex.core/re-pattern |\d+ ; "creates any-ref to hold a native regex pattern" ``` +```cirru +let + pattern $ regex.core/re-pattern "|\d+" + regex.core/re-find |a4 |\d +``` + Install to `~/.config/calcit/modules/`, compile and provide `*.{dylib,so}` file with `./build.sh`. ### Workflow From 11bb133e781ee81bd24d73b892722a7e4ac11a4c Mon Sep 17 00:00:00 2001 From: tiye Date: Sun, 19 May 2024 15:56:42 +0800 Subject: [PATCH 13/14] any-ref changed to RwLock; ABI 0.0.9 ; tag 0.0.6 --- .github/workflows/check.yaml | 2 +- Cargo.lock | 6 +++--- Cargo.toml | 4 ++-- calcit.cirru | 2 +- compact.cirru | 2 +- src/lib.rs | 18 +++++++++--------- 6 files changed, 17 insertions(+), 17 deletions(-) diff --git a/.github/workflows/check.yaml b/.github/workflows/check.yaml index 8515fd1..94c95f9 100644 --- a/.github/workflows/check.yaml +++ b/.github/workflows/check.yaml @@ -16,7 +16,7 @@ jobs: - uses: calcit-lang/setup-cr@0.0.3 with: - version: "0.8.51" + version: "0.8.52" - uses: dtolnay/rust-toolchain@stable with: diff --git a/Cargo.lock b/Cargo.lock index 70c4437..9104d61 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -32,7 +32,7 @@ dependencies = [ [[package]] name = "calcit_regex" -version = "0.0.5" +version = "0.0.6" dependencies = [ "cirru_edn", "cirru_parser", @@ -42,9 +42,9 @@ dependencies = [ [[package]] name = "cirru_edn" -version = "0.6.5" +version = "0.6.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cf2af443516dfa62939fbfe82812601c1cb5fc89fdf7356eee519d7b056a264d" +checksum = "51e76586e1d99e801ffa01acedffbcf7bff79f2463ce9ecf09b72dd25c306df5" dependencies = [ "bincode", "cirru_parser", diff --git a/Cargo.toml b/Cargo.toml index ab111da..690011d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "calcit_regex" -version = "0.0.5" +version = "0.0.6" authors = ["jiyinyiyong "] edition = "2021" @@ -13,7 +13,7 @@ crate-type = ["dylib"] # Creates dynamic lib # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -cirru_edn = "0.6.5" +cirru_edn = "0.6.6" # cirru_edn = { path = "/Users/chenyong/repo/cirru/edn.rs" } cirru_parser = "0.1.29" regex = "1.10.4" diff --git a/calcit.cirru b/calcit.cirru index feb7038..62eecb8 100644 --- a/calcit.cirru +++ b/calcit.cirru @@ -1,6 +1,6 @@ {} (:package |regex) - :configs $ {} (:init-fn |regex.test/main!) (:port 6001) (:reload-fn |regex.test/reload!) (:version |0.0.5-a1) + :configs $ {} (:init-fn |regex.test/main!) (:port 6001) (:reload-fn |regex.test/reload!) (:version |0.0.6) :modules $ [] :entries $ {} :files $ {} diff --git a/compact.cirru b/compact.cirru index e38879a..cdc818b 100644 --- a/compact.cirru +++ b/compact.cirru @@ -1,6 +1,6 @@ {} (:package |regex) - :configs $ {} (:init-fn |regex.test/main!) (:reload-fn |regex.test/reload!) (:version |0.0.5-a1) + :configs $ {} (:init-fn |regex.test/main!) (:reload-fn |regex.test/reload!) (:version |0.0.6) :modules $ [] :entries $ {} :files $ {} diff --git a/src/lib.rs b/src/lib.rs index cc0726b..77af81e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,11 +1,11 @@ -use std::{cell::RefCell, sync::Arc}; +use std::sync::{Arc, RwLock}; use cirru_edn::{Edn, EdnAnyRef, EdnListView}; use regex::Regex; #[no_mangle] pub fn abi_version() -> String { - String::from("0.0.8") + String::from("0.0.9") } #[no_mangle] @@ -14,7 +14,7 @@ pub fn re_pattern(args: Vec) -> Result { match &args[0] { Edn::Str(s) => match Regex::new(s) { Ok(pattern) => { - let p = Arc::from(RefCell::new(pattern)); + let p = Arc::from(RwLock::new(pattern)); Ok(Edn::AnyRef(EdnAnyRef(p))) } @@ -36,7 +36,7 @@ pub fn re_matches(args: Vec) -> Result { Err(e) => Err(format!("re-matches failed, {}", e)), }, (Edn::Str(s), Edn::AnyRef(EdnAnyRef(p))) => { - if let Some(pattern) = p.borrow().downcast_ref::() { + if let Some(pattern) = p.read().map_err(|e| e.to_string())?.downcast_ref::() { Ok(Edn::Bool(pattern.is_match(s))) } else { Err(format!("re-matches expected a regex, got {:?}", p)) @@ -63,7 +63,7 @@ pub fn re_find_index(args: Vec) -> Result { } } (Edn::Str(s), Edn::AnyRef(EdnAnyRef(p))) => { - if let Some(pattern) = p.borrow().downcast_ref::() { + if let Some(pattern) = p.read().map_err(|e| e.to_string())?.downcast_ref::() { match pattern.find(s) { Some(matched) => Ok(Edn::Number(matched.start() as f64)), None => Ok(Edn::Number(-1.0)), // TODO maybe nil @@ -97,7 +97,7 @@ pub fn re_find(args: Vec) -> Result { } } (Edn::Str(s), Edn::AnyRef(EdnAnyRef(p))) => { - if let Some(pattern) = p.borrow().downcast_ref::() { + if let Some(pattern) = p.read().map_err(|e| e.to_string())?.downcast_ref::() { let mut matched = pattern.find_iter(s); match matched.next() { Some(v) => Ok(Edn::str(v.as_str().to_string())), @@ -129,7 +129,7 @@ pub fn re_find_all(args: Vec) -> Result { Err(e) => Err(format!("re-find-all failed, {}", e)), }, (Edn::Str(s), Edn::AnyRef(EdnAnyRef(p))) => { - if let Some(pattern) = p.borrow().downcast_ref::() { + if let Some(pattern) = p.read().map_err(|e| e.to_string())?.downcast_ref::() { let mut ys: Vec = vec![]; for v in pattern.find_iter(s) { ys.push(Edn::Str(v.as_str().to_string().into())) @@ -161,7 +161,7 @@ pub fn re_split(args: Vec) -> Result { Err(e) => Err(format!("re-split failed, {}", e)), }, (Edn::Str(s), Edn::AnyRef(EdnAnyRef(p))) => { - if let Some(pattern) = p.borrow().downcast_ref::() { + if let Some(pattern) = p.read().map_err(|e| e.to_string())?.downcast_ref::() { let mut ys: Vec = vec![]; for piece in pattern.split(s) { ys.push(Edn::str(piece)); @@ -187,7 +187,7 @@ pub fn re_replace_all(args: Vec) -> Result { Err(e) => Err(format!("re-replace-all failed, {}", e)), }, (Edn::Str(s), Edn::AnyRef(EdnAnyRef(p)), Edn::Str(next)) => { - if let Some(pattern) = p.borrow().downcast_ref::() { + if let Some(pattern) = p.read().map_err(|e| e.to_string())?.downcast_ref::() { Ok(Edn::str(pattern.replace_all(s, &**next).into_owned())) } else { Err(format!("re-replace-all expected a regex, got {:?}", p)) From 87a14911025ac31821610f9a7d92fcb1c3b00d31 Mon Sep 17 00:00:00 2001 From: tiye Date: Mon, 20 May 2024 01:53:13 +0800 Subject: [PATCH 14/14] upgrade edn; tag 0.0.7 --- Cargo.lock | 6 +++--- Cargo.toml | 4 ++-- calcit.cirru | 2 +- compact.cirru | 2 +- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 9104d61..5c8de03 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -32,7 +32,7 @@ dependencies = [ [[package]] name = "calcit_regex" -version = "0.0.6" +version = "0.0.7" dependencies = [ "cirru_edn", "cirru_parser", @@ -42,9 +42,9 @@ dependencies = [ [[package]] name = "cirru_edn" -version = "0.6.6" +version = "0.6.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "51e76586e1d99e801ffa01acedffbcf7bff79f2463ce9ecf09b72dd25c306df5" +checksum = "b4aa8a3c939e1480883ea1371deab36c179e621779f9a7731554b9bec3840189" dependencies = [ "bincode", "cirru_parser", diff --git a/Cargo.toml b/Cargo.toml index 690011d..637ac30 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "calcit_regex" -version = "0.0.6" +version = "0.0.7" authors = ["jiyinyiyong "] edition = "2021" @@ -13,7 +13,7 @@ crate-type = ["dylib"] # Creates dynamic lib # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -cirru_edn = "0.6.6" +cirru_edn = "0.6.8" # cirru_edn = { path = "/Users/chenyong/repo/cirru/edn.rs" } cirru_parser = "0.1.29" regex = "1.10.4" diff --git a/calcit.cirru b/calcit.cirru index 62eecb8..ee9b347 100644 --- a/calcit.cirru +++ b/calcit.cirru @@ -1,6 +1,6 @@ {} (:package |regex) - :configs $ {} (:init-fn |regex.test/main!) (:port 6001) (:reload-fn |regex.test/reload!) (:version |0.0.6) + :configs $ {} (:init-fn |regex.test/main!) (:port 6001) (:reload-fn |regex.test/reload!) (:version |0.0.7) :modules $ [] :entries $ {} :files $ {} diff --git a/compact.cirru b/compact.cirru index cdc818b..0d654ec 100644 --- a/compact.cirru +++ b/compact.cirru @@ -1,6 +1,6 @@ {} (:package |regex) - :configs $ {} (:init-fn |regex.test/main!) (:reload-fn |regex.test/reload!) (:version |0.0.6) + :configs $ {} (:init-fn |regex.test/main!) (:reload-fn |regex.test/reload!) (:version |0.0.7) :modules $ [] :entries $ {} :files $ {}