Skip to content

Commit

Permalink
Merge pull request #1 from calcit-lang/new-apis
Browse files Browse the repository at this point in the history
new apis: re-split , re-replace-all
  • Loading branch information
soyaine authored Nov 19, 2021
2 parents 3424fc5 + 73aec31 commit 9d5e81c
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "calcit_regex"
version = "0.0.1"
version = "0.0.2"
authors = ["jiyinyiyong <[email protected]>"]
edition = "2018"

Expand Down
20 changes: 16 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,22 @@ API 设计: https://github.com/calcit-lang/calcit_runner.rs/discussions/116 .
APIs:

```cirru
calcit.std.regex/re-matches |2 |\d
calcit.std.regex/re-find |a4 |\d
calcit.std.regex/re-find-index |a1 |\d
calcit.std.regex/re-find-all |123 |\d+
regex.core/re-matches |2 |\d
; "returns bool"
; "find first matched item"
regex.core/re-find |a4 |\d
regex.core/re-find-index |a1 |\d
regex.core/re-find-all |123 |\d+
regex.core/re-replace-all |1ab22c333 |\d{2} "\"X"
; |1abXcX3
regex.core/re-split |1ab22c333 |\d{2}
; [] "\"1ab" "\"c" "\"3"
```

Install to `~/.config/calcit/modules/`, compile and provide `*.{dylib,so}` file with `./build.sh`.
Expand Down
66 changes: 65 additions & 1 deletion calcit.cirru

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 10 additions & 2 deletions compact.cirru
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
{} (:package |regex)
:configs $ {} (:init-fn |regex.test/main!) (:reload-fn |regex.test/reload!)
:modules $ []
:version |0.0.1
:version |0.0.2
:entries $ {}
:files $ {}
|regex.core $ {}
Expand All @@ -23,6 +23,12 @@
|re-find $ quote
defn re-find (s pattern)
&call-dylib-edn (get-dylib-path "\"/dylibs/libcalcit_std") "\"re_find" s pattern
|re-split $ quote
defn re-split (s pattern)
&call-dylib-edn (get-dylib-path "\"/dylibs/libcalcit_std") "\"re_split" s pattern
|re-replace-all $ quote
defn re-replace-all (s pattern next)
&call-dylib-edn (get-dylib-path "\"/dylibs/libcalcit_std") "\"re_replace_all" s pattern next
|regex.util $ {}
:ns $ quote
ns regex.util $ :require
Expand All @@ -39,7 +45,7 @@
|regex.test $ {}
:ns $ quote
ns regex.test $ :require
regex.core :refer $ re-matches re-find-index re-find re-find-all
regex.core :refer $ re-matches re-find-index re-find re-find-all re-split re-replace-all
regex.$meta :refer $ calcit-dirname calcit-filename
:defs $ {}
|main! $ quote
Expand All @@ -54,3 +60,5 @@
assert= ([] |123) (re-find-all |123 |\d+)
assert= ([] |1 |2 |3) (re-find-all |1a2a3 |\d+)
assert= ([] |1 |2 |34) (re-find-all |1a2a34 |\d+)
assert= |1abXcX3 $ re-replace-all |1ab22c333 |\d{2} "\"X"
assert= ([] "\"1ab" "\"c" "\"3") (re-split |1ab22c333 |\d{2})
39 changes: 39 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,42 @@ pub fn re_find_all(args: Vec<Edn>) -> Result<Edn, String> {
Err(format!("re-find-all expected 2 strings: {:?}", args))
}
}

#[no_mangle]
pub fn re_split(args: Vec<Edn>) -> Result<Edn, String> {
if args.len() == 2 {
match (&args[0], &args[1]) {
(Edn::Str(s), Edn::Str(pattern)) => match Regex::new(pattern) {
Ok(p) => {
let mut ys: Vec<Edn> = vec![];
for piece in p.split(s) {
ys.push(Edn::str(piece));
}
Ok(Edn::List(ys))
}
Err(e) => Err(format!("re-split failed, {}", e)),
},
(_, _) => Err(format!("re-split expected 2 strings: {:?}", args)),
}
} else {
Err(format!("re-split expected 2 strings: {:?}", args))
}
}

#[no_mangle]
pub fn re_replace_all(args: Vec<Edn>) -> Result<Edn, String> {
if args.len() == 3 {
match (&args[0], &args[1], &args[2]) {
(Edn::Str(s), Edn::Str(pattern), Edn::Str(next)) => match Regex::new(pattern) {
Ok(p) => Ok(Edn::str(p.replace_all(&**s, &**next).into_owned())),
Err(e) => Err(format!("re-replace-all failed, {}", e)),
},
(a, b, c) => Err(format!(
"re-replace-all expected 3 strings: {} {} {}",
a, b, c
)),
}
} else {
Err(format!("re-replace-all expected 3 strings: {:?}", args))
}
}

0 comments on commit 9d5e81c

Please sign in to comment.