Skip to content

Commit a440278

Browse files
committed
track pattern with vec
1 parent f066a4e commit a440278

File tree

5 files changed

+68
-8
lines changed

5 files changed

+68
-8
lines changed

Cargo.lock

Lines changed: 3 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,11 @@ crate-type = ["dylib"] # Creates dynamic lib
1313
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
1414

1515
[dependencies]
16-
cirru_edn = "0.6.4-a3"
16+
cirru_edn = "0.6.4-a4"
1717
# cirru_edn = { path = "/Users/chenyong/repo/cirru/edn.rs" }
1818
cirru_parser = "0.1.29"
1919
regex = "1.10.4"
20+
lazy_static = "1.4.0"
2021

2122
# [profile.release]
2223
# debug = true

calcit.cirru

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

compact.cirru

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,12 @@
7070
assert= "\"22" $ re-find |q22 pattern
7171
assert= ([] |1 |2 |3) (re-find-all |1q2q3 pattern)
7272
assert= |XabXcX $ re-replace-all |1ab22c333 pattern "\"X"
73+
w-log $ re-drop pattern
74+
w-log $ re-drop pattern
7375
:ns $ %{} :CodeEntry (:doc |)
7476
:code $ quote
7577
ns regex.test $ :require
76-
regex.core :refer $ re-matches re-find-index re-find re-find-all re-split re-replace-all re-pattern
78+
regex.core :refer $ re-matches re-find-index re-find re-find-all re-split re-replace-all re-pattern re-drop
7779
regex.$meta :refer $ calcit-dirname calcit-filename
7880
|regex.util $ %{} :FileEntry
7981
:defs $ {}

src/lib.rs

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
1-
use std::{cell::RefCell, sync::Arc};
1+
use std::{
2+
cell::RefCell,
3+
sync::{Arc, Mutex},
4+
};
25

36
use cirru_edn::{Edn, EdnAnyRef, EdnListView};
7+
use lazy_static::lazy_static;
48
use regex::Regex;
59

10+
lazy_static! {
11+
static ref PATTERNS: Mutex<Vec<EdnAnyRef>> = Mutex::from(vec![]);
12+
}
13+
614
#[no_mangle]
715
pub fn abi_version() -> String {
816
String::from("0.0.8")
@@ -15,9 +23,11 @@ pub fn re_pattern(args: Vec<Edn>) -> Result<Edn, String> {
1523
Edn::Str(s) => match Regex::new(s) {
1624
Ok(pattern) => {
1725
let p = Arc::from(RefCell::new(pattern));
18-
let p2 = p.to_owned();
19-
std::mem::forget(p);
20-
Ok(Edn::AnyRef(EdnAnyRef(p2)))
26+
// push to PATTERNS
27+
let v = EdnAnyRef(p);
28+
let mut patterns = PATTERNS.lock().unwrap();
29+
patterns.push(v.to_owned());
30+
Ok(Edn::AnyRef(v))
2131
}
2232
Err(e) => Err(format!("re-pattern failed, {}", e)),
2333
},
@@ -28,6 +38,37 @@ pub fn re_pattern(args: Vec<Edn>) -> Result<Edn, String> {
2838
}
2939
}
3040

41+
/// re_drop drops pattern from global PATTERNS
42+
#[no_mangle]
43+
pub fn re_drop(args: Vec<Edn>) -> Result<Edn, String> {
44+
if args.len() == 1 {
45+
match &args[0] {
46+
Edn::AnyRef(p) => {
47+
let mut patterns = PATTERNS.lock().unwrap();
48+
let mut i = 0;
49+
let mut found = false;
50+
for v in patterns.iter() {
51+
if v == p {
52+
found = true;
53+
break;
54+
}
55+
i += 1;
56+
}
57+
println!("re-drop found {} at {}\n", found, i);
58+
if found {
59+
patterns.remove(i);
60+
Ok(Edn::from(true))
61+
} else {
62+
Ok(Edn::from(false))
63+
}
64+
}
65+
_ => Err(format!("re-drop expect 1 pattern, got {:?}", args)),
66+
}
67+
} else {
68+
Err(format!("re-drop expect 1 pattern, got {:?}", args))
69+
}
70+
}
71+
3172
#[no_mangle]
3273
pub fn re_matches(args: Vec<Edn>) -> Result<Edn, String> {
3374
if args.len() == 2 {

0 commit comments

Comments
 (0)