diff --git a/.github/workflows/check.yaml b/.github/workflows/check.yaml index b3569b5..94c95f9 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.52" - 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..5c8de03 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -11,21 +11,42 @@ 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.7" dependencies = [ "cirru_edn", "cirru_parser", + "lazy_static", "regex", ] [[package]] name = "cirru_edn" -version = "0.5.0" +version = "0.6.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8a35d38ee59f7409023f9dee361dc98760f33c9ad1f21283e536939d9561ed44" +checksum = "b4aa8a3c939e1480883ea1371deab36c179e621779f9a7731554b9bec3840189" dependencies = [ + "bincode", "cirru_parser", "hex", "lazy_static", @@ -33,9 +54,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 +79,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 +111,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 +125,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..637ac30 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,11 +1,11 @@ [package] name = "calcit_regex" -version = "0.0.2" +version = "0.0.7" authors = ["jiyinyiyong "] edition = "2021" [lib] -name = "calcit_std" +name = "calcit_regex" path = "src/lib.rs" crate-type = ["dylib"] # Creates dynamic lib @@ -13,6 +13,12 @@ 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.8" +# 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 +# opt-level = 1 diff --git a/README.md b/README.md index 6445641..556586e 100644 --- a/README.md +++ b/README.md @@ -25,6 +25,15 @@ 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" +``` + +```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`. diff --git a/calcit.cirru b/calcit.cirru index 3aa54fb..ee9b347 100644 --- a/calcit.cirru +++ b/calcit.cirru @@ -1,11 +1,28 @@ {} (: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.7) :modules $ [] :entries $ {} :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 $ {} @@ -21,7 +38,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 +57,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 +76,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,10 +95,27 @@ |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) + |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 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 |) :code $ %{} :Expr (:at 1637157516835) (:by |u0) :data $ {} @@ -98,7 +132,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) @@ -118,7 +152,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) @@ -293,6 +327,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 +428,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..0d654ec 100644 --- a/compact.cirru +++ b/compact.cirru @@ -1,35 +1,43 @@ {} (: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.7) :modules $ [] :entries $ {} :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) - &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_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 @@ -52,10 +60,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/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" diff --git a/src/lib.rs b/src/lib.rs index 840f2d4..77af81e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,9 +1,30 @@ -use cirru_edn::Edn; +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.6") + String::from("0.0.9") +} + +#[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) => { + let p = Arc::from(RwLock::new(pattern)); + + Ok(Edn::AnyRef(EdnAnyRef(p))) + } + 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 +35,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.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)) + } + } (_, _) => Err(format!("re-matches expected 2 strings: {:?}", args)), } } else { @@ -34,6 +62,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.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 + } + } else { + Err(format!("re-find-index expected a regex, got {:?}", p)) + } + } (_, _) => Err(format!("re-find-index expected 2 strings: {:?}", args)), } } else { @@ -51,13 +89,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_boxed_str())), - None => Ok(Edn::Str("".to_owned().into_boxed_str())), // 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.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())), + 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 { @@ -73,12 +122,23 @@ 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)), }, + (Edn::Str(s), Edn::AnyRef(EdnAnyRef(p))) => { + 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())) + } + 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 { @@ -96,10 +156,21 @@ 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)), }, + (Edn::Str(s), Edn::AnyRef(EdnAnyRef(p))) => { + 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)); + } + Ok(Edn::List(EdnListView(ys))) + } else { + Err(format!("re-split expected a regex, got {:?}", p)) + } + } (_, _) => Err(format!("re-split expected 2 strings: {:?}", args)), } } else { @@ -112,13 +183,17 @@ 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 - )), + (Edn::Str(s), Edn::AnyRef(EdnAnyRef(p)), Edn::Str(next)) => { + 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)) + } + } + (a, b, c) => Err(format!("re-replace-all expected 3 strings: {} {} {}", a, b, c)), } } else { Err(format!("re-replace-all expected 3 strings: {:?}", args))