From f066a4e2ceae28dbe4d4a6fe62c77cbdc7c154b5 Mon Sep 17 00:00:00 2001 From: tiye Date: Sat, 18 May 2024 01:24:10 +0800 Subject: [PATCH] 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))