From eeaf66ae7f3ea42c9f8b660966d178fbabf9e18c Mon Sep 17 00:00:00 2001 From: mengning997 Date: Tue, 6 Sep 2022 18:21:33 +0800 Subject: [PATCH] FindAndConsume works well --- Makefile | 4 +-- re2/re2.cc | 25 ++++++++++----- regex-capi/include/rure.h | 9 ++++++ regex-capi/src/rure.rs | 67 ++++++++++++++++++++++++++++++++++++++- 4 files changed, 94 insertions(+), 11 deletions(-) diff --git a/Makefile b/Makefile index 3643ede..b0d9dcd 100644 --- a/Makefile +++ b/Makefile @@ -314,8 +314,8 @@ shared: obj/so/libre2.$(SOEXT) .PHONY: shared-install shared-install: obj/so/libre2.$(SOEXT) common-install - $(INSTALL) regex-capi/include/rure.h /usr/include/rure.h - $(INSTALL) target/release/librure.so /usr/lib/librure.so + $(INSTALL) regex-capi/include/rure.h $(DESTDIR)$(includedir)/rure.h + $(INSTALL) target/release/librure.so $(DESTDIR)$(libdir)/librure.so $(INSTALL) obj/so/libre2.$(SOEXT) $(DESTDIR)$(libdir)/libre2rust.$(SOEXTVER00) ln -sf libre2rust.$(SOEXTVER00) $(DESTDIR)$(libdir)/libre2rust.$(SOEXTVER) ln -sf libre2rust.$(SOEXTVER00) $(DESTDIR)$(libdir)/libre2rust.$(SOEXT) diff --git a/re2/re2.cc b/re2/re2.cc index 0ab23c0..bab8bd1 100644 --- a/re2/re2.cc +++ b/re2/re2.cc @@ -268,6 +268,7 @@ namespace re2 // error_code_如何进行赋值,RegexpErrorToRE2删除了??? // error_code_ = RE2::NoError; // rure_free(re); + suffix_regexp_ = (re2::Regexp *)rure_new((const uint8_t *)pattern.data(), pattern.size()); } // Returns rprog_, computing it if needed. @@ -291,14 +292,14 @@ namespace re2 RE2::~RE2() { - if (suffix_regexp_) - // suffix_regexp_->Decref(); - if (entire_regexp_) - // entire_regexp_->Decref(); - // delete prog_; - // delete rprog_; - if (error_ != empty_string) - delete error_; + // if (suffix_regexp_) + // // suffix_regexp_->Decref(); + // if (entire_regexp_) + // // entire_regexp_->Decref(); + // // delete prog_; + // // delete rprog_; + // if (error_ != empty_string) + // delete error_; if (named_groups_ != NULL && named_groups_ != empty_named_groups) delete named_groups_; if (group_names_ != NULL && group_names_ != empty_group_names) @@ -930,6 +931,14 @@ namespace re2 // RE has fewer capturing groups than number of Arg pointers passed in. return false; } + + rure_match match; + if(consumed && n == 0 && + rure_consume((rure *)suffix_regexp_, (const uint8_t *)text.data(), (size_t)text.size(), &match)) + { + *consumed = match.end; + return true; + } /* // 判断是否FullMatch, 判空 diff --git a/regex-capi/include/rure.h b/regex-capi/include/rure.h index dd78108..597344b 100644 --- a/regex-capi/include/rure.h +++ b/regex-capi/include/rure.h @@ -604,6 +604,15 @@ const char *rure_replace(rure *re, const uint8_t *haystack, size_t len_h, const char *rure_replace_all(rure *re, const uint8_t *haystack, size_t len_h, const uint8_t *rewrite, size_t len_r); +/* + * Simple way to use regex + */ +// rure *rure_compile(const uint8_t *pattern, size_t length, +// uint32_t flags, rure_options *options, +// rure_error *error); +rure *rure_new(const uint8_t *pattern, size_t length); +bool rure_consume(rure *re, const uint8_t *haystack, size_t length, rure_match *match); + #ifdef __cplusplus } #endif diff --git a/regex-capi/src/rure.rs b/regex-capi/src/rure.rs index 87af892..411f024 100644 --- a/regex-capi/src/rure.rs +++ b/regex-capi/src/rure.rs @@ -160,7 +160,7 @@ ffi_fn! { re: *const Regex, haystack: *const u8, len: size_t, - start: size_t, + _start: size_t, ) -> bool { let re = unsafe { &*re }; let haystack = unsafe { slice::from_raw_parts(haystack, len) }; @@ -678,3 +678,68 @@ ffi_fn! { } } + +/* + * Simple way to use regex + */ + +/* + * Simple way to use regex + */ + + ffi_fn! { + fn rure_new( + pattern: *const u8, + length: size_t, + ) -> *const Regex { + let pat = unsafe { slice::from_raw_parts(pattern, length) }; + let pat = match str::from_utf8(pat) { + Ok(pat) => pat, + Err(err) => { + unsafe { + return ptr::null(); + } + } + }; + let exp = match bytes::Regex::new(pat) { + Ok(val) => Box::into_raw(Box::new(val)), + Err(_) => ptr::null() + }; + exp as *const Regex + } +} + +ffi_fn! { + fn rure_consume( + re: *const Regex, + haystack: *const u8, + len: size_t, + match_info: *mut rure_match, + ) -> bool { + let exp = unsafe { &*re }; + let haystack = unsafe { slice::from_raw_parts(haystack, len) }; + exp.find(haystack).map(|m| unsafe { + if !match_info.is_null() { + (*match_info).start = m.start(); + (*match_info).end = m.end(); + } + }).is_some() + } + // fn rure_consume( + // raw_exp: *mut Regex, + // p: *const u8, + // len: size_t, + // match_info: *mut rure_match, + // ) -> bool { + // let exp = unsafe { Box::from_raw(raw_exp) }; + // let s = unsafe { slice::from_raw_parts(p, len as usize) }; + // let m = exp.find(s).unwrap(); + // unsafe { + // if !match_info.is_null() { + // (*match_info).start = m.start(); + // (*match_info).end = m.end(); + // } + // } + // true + // } +} \ No newline at end of file -- Gitee