Skip to content

Commit 9d5e81c

Browse files
authored
Merge pull request #1 from calcit-lang/new-apis
new apis: re-split , re-replace-all
2 parents 3424fc5 + 73aec31 commit 9d5e81c

File tree

6 files changed

+132
-9
lines changed

6 files changed

+132
-9
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "calcit_regex"
3-
version = "0.0.1"
3+
version = "0.0.2"
44
authors = ["jiyinyiyong <[email protected]>"]
55
edition = "2018"
66

README.md

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,22 @@ API 设计: https://github.com/calcit-lang/calcit_runner.rs/discussions/116 .
99
APIs:
1010

1111
```cirru
12-
calcit.std.regex/re-matches |2 |\d
13-
calcit.std.regex/re-find |a4 |\d
14-
calcit.std.regex/re-find-index |a1 |\d
15-
calcit.std.regex/re-find-all |123 |\d+
12+
regex.core/re-matches |2 |\d
13+
; "returns bool"
14+
15+
; "find first matched item"
16+
regex.core/re-find |a4 |\d
17+
regex.core/re-find-index |a1 |\d
18+
19+
regex.core/re-find-all |123 |\d+
20+
21+
22+
regex.core/re-replace-all |1ab22c333 |\d{2} "\"X"
23+
; |1abXcX3
24+
25+
26+
regex.core/re-split |1ab22c333 |\d{2}
27+
; [] "\"1ab" "\"c" "\"3"
1628
```
1729

1830
Install to `~/.config/calcit/modules/`, compile and provide `*.{dylib,so}` file with `./build.sh`.

calcit.cirru

Lines changed: 65 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

compact.cirru

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
{} (:package |regex)
33
:configs $ {} (:init-fn |regex.test/main!) (:reload-fn |regex.test/reload!)
44
:modules $ []
5-
:version |0.0.1
5+
:version |0.0.2
66
:entries $ {}
77
:files $ {}
88
|regex.core $ {}
@@ -23,6 +23,12 @@
2323
|re-find $ quote
2424
defn re-find (s pattern)
2525
&call-dylib-edn (get-dylib-path "\"/dylibs/libcalcit_std") "\"re_find" s pattern
26+
|re-split $ quote
27+
defn re-split (s pattern)
28+
&call-dylib-edn (get-dylib-path "\"/dylibs/libcalcit_std") "\"re_split" s pattern
29+
|re-replace-all $ quote
30+
defn re-replace-all (s pattern next)
31+
&call-dylib-edn (get-dylib-path "\"/dylibs/libcalcit_std") "\"re_replace_all" s pattern next
2632
|regex.util $ {}
2733
:ns $ quote
2834
ns regex.util $ :require
@@ -39,7 +45,7 @@
3945
|regex.test $ {}
4046
:ns $ quote
4147
ns regex.test $ :require
42-
regex.core :refer $ re-matches re-find-index re-find re-find-all
48+
regex.core :refer $ re-matches re-find-index re-find re-find-all re-split re-replace-all
4349
regex.$meta :refer $ calcit-dirname calcit-filename
4450
:defs $ {}
4551
|main! $ quote
@@ -54,3 +60,5 @@
5460
assert= ([] |123) (re-find-all |123 |\d+)
5561
assert= ([] |1 |2 |3) (re-find-all |1a2a3 |\d+)
5662
assert= ([] |1 |2 |34) (re-find-all |1a2a34 |\d+)
63+
assert= |1abXcX3 $ re-replace-all |1ab22c333 |\d{2} "\"X"
64+
assert= ([] "\"1ab" "\"c" "\"3") (re-split |1ab22c333 |\d{2})

src/lib.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,42 @@ pub fn re_find_all(args: Vec<Edn>) -> Result<Edn, String> {
8585
Err(format!("re-find-all expected 2 strings: {:?}", args))
8686
}
8787
}
88+
89+
#[no_mangle]
90+
pub fn re_split(args: Vec<Edn>) -> Result<Edn, String> {
91+
if args.len() == 2 {
92+
match (&args[0], &args[1]) {
93+
(Edn::Str(s), Edn::Str(pattern)) => match Regex::new(pattern) {
94+
Ok(p) => {
95+
let mut ys: Vec<Edn> = vec![];
96+
for piece in p.split(s) {
97+
ys.push(Edn::str(piece));
98+
}
99+
Ok(Edn::List(ys))
100+
}
101+
Err(e) => Err(format!("re-split failed, {}", e)),
102+
},
103+
(_, _) => Err(format!("re-split expected 2 strings: {:?}", args)),
104+
}
105+
} else {
106+
Err(format!("re-split expected 2 strings: {:?}", args))
107+
}
108+
}
109+
110+
#[no_mangle]
111+
pub fn re_replace_all(args: Vec<Edn>) -> Result<Edn, String> {
112+
if args.len() == 3 {
113+
match (&args[0], &args[1], &args[2]) {
114+
(Edn::Str(s), Edn::Str(pattern), Edn::Str(next)) => match Regex::new(pattern) {
115+
Ok(p) => Ok(Edn::str(p.replace_all(&**s, &**next).into_owned())),
116+
Err(e) => Err(format!("re-replace-all failed, {}", e)),
117+
},
118+
(a, b, c) => Err(format!(
119+
"re-replace-all expected 3 strings: {} {} {}",
120+
a, b, c
121+
)),
122+
}
123+
} else {
124+
Err(format!("re-replace-all expected 3 strings: {:?}", args))
125+
}
126+
}

0 commit comments

Comments
 (0)