Skip to content

Commit

Permalink
upgrade edn
Browse files Browse the repository at this point in the history
  • Loading branch information
tiye committed May 17, 2024
1 parent 56f5b9b commit f066a4e
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 11 deletions.
4 changes: 2 additions & 2 deletions 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
Expand Up @@ -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"
Expand Down
17 changes: 17 additions & 0 deletions calcit.cirru

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

4 changes: 4 additions & 0 deletions compact.cirru
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
16 changes: 8 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::sync::Arc;
use std::{cell::RefCell, sync::Arc};

use cirru_edn::{Edn, EdnAnyRef, EdnListView};
use regex::Regex;
Expand All @@ -14,7 +14,7 @@ pub fn re_pattern(args: Vec<Edn>) -> Result<Edn, String> {
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)))
Expand All @@ -37,7 +37,7 @@ pub fn re_matches(args: Vec<Edn>) -> Result<Edn, String> {
Err(e) => Err(format!("re-matches failed, {}", e)),
},
(Edn::Str(s), Edn::AnyRef(EdnAnyRef(p))) => {
if let Some(pattern) = p.downcast_ref::<Regex>() {
if let Some(pattern) = p.borrow().downcast_ref::<Regex>() {
Ok(Edn::Bool(pattern.is_match(s)))
} else {
Err(format!("re-matches expected a regex, got {:?}", p))
Expand All @@ -64,7 +64,7 @@ pub fn re_find_index(args: Vec<Edn>) -> Result<Edn, String> {
}
}
(Edn::Str(s), Edn::AnyRef(EdnAnyRef(p))) => {
if let Some(pattern) = p.downcast_ref::<Regex>() {
if let Some(pattern) = p.borrow().downcast_ref::<Regex>() {
match pattern.find(s) {
Some(matched) => Ok(Edn::Number(matched.start() as f64)),
None => Ok(Edn::Number(-1.0)), // TODO maybe nil
Expand Down Expand Up @@ -98,7 +98,7 @@ pub fn re_find(args: Vec<Edn>) -> Result<Edn, String> {
}
}
(Edn::Str(s), Edn::AnyRef(EdnAnyRef(p))) => {
if let Some(pattern) = p.downcast_ref::<Regex>() {
if let Some(pattern) = p.borrow().downcast_ref::<Regex>() {
let mut matched = pattern.find_iter(s);
match matched.next() {
Some(v) => Ok(Edn::str(v.as_str().to_string())),
Expand Down Expand Up @@ -130,7 +130,7 @@ pub fn re_find_all(args: Vec<Edn>) -> Result<Edn, String> {
Err(e) => Err(format!("re-find-all failed, {}", e)),
},
(Edn::Str(s), Edn::AnyRef(EdnAnyRef(p))) => {
if let Some(pattern) = p.downcast_ref::<Regex>() {
if let Some(pattern) = p.borrow().downcast_ref::<Regex>() {
let mut ys: Vec<Edn> = vec![];
for v in pattern.find_iter(s) {
ys.push(Edn::Str(v.as_str().to_string().into()))
Expand Down Expand Up @@ -162,7 +162,7 @@ pub fn re_split(args: Vec<Edn>) -> Result<Edn, String> {
Err(e) => Err(format!("re-split failed, {}", e)),
},
(Edn::Str(s), Edn::AnyRef(EdnAnyRef(p))) => {
if let Some(pattern) = p.downcast_ref::<Regex>() {
if let Some(pattern) = p.borrow().downcast_ref::<Regex>() {
let mut ys: Vec<Edn> = vec![];
for piece in pattern.split(s) {
ys.push(Edn::str(piece));
Expand All @@ -188,7 +188,7 @@ pub fn re_replace_all(args: Vec<Edn>) -> Result<Edn, String> {
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::<Regex>() {
if let Some(pattern) = p.borrow().downcast_ref::<Regex>() {
Ok(Edn::str(pattern.replace_all(s, &**next).into_owned()))
} else {
Err(format!("re-replace-all expected a regex, got {:?}", p))
Expand Down

0 comments on commit f066a4e

Please sign in to comment.