Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- extern crate simple_regex;
- extern crate libc;
- use std::vec::Vec;
- use std::boxed::Box;
- use std::ffi::CStr;
- use std::str;
- use libc::{c_char, size_t, c_void};
- pub struct RegexpSearcher {
- results: Vec<size_t>
- }
- impl RegexpSearcher {
- fn new() -> RegexpSearcher {
- RegexpSearcher {
- results: Vec::new(),
- }
- }
- fn search(&mut self, text: &str, regexp: &str) -> Vec<size_t> {
- simple_regex::search(text, regexp)
- }
- /* fn set_results(&mut self, results: Vec<size_t>) {
- self.results = results;
- }
- */
- }
- #[no_mangle]
- pub extern "C" fn createRegexpSearcher() -> *mut c_void {
- Box::into_raw(Box::new(RegexpSearcher::new())) as *mut c_void
- }
- #[no_mangle]
- pub extern "C" fn freeRegexpSearcher(ptr: *mut c_void) {
- if ptr.is_null() {
- return
- }
- unsafe {
- Box::from_raw(ptr);
- }
- }
- #[no_mangle]
- pub extern "C" fn search(ptr: *mut c_void, regexp: *const c_char, text: *const c_char) -> *mut size_t {
- let searcher = unsafe {
- assert!(!ptr.is_null());
- &mut *(ptr as *mut RegexpSearcher)
- };
- let text_c_str = unsafe {
- assert!(!text.is_null());
- CStr::from_ptr(text)
- };
- let text_str = text_c_str.to_str().unwrap();
- let regexp_c_str = unsafe {
- assert!(!regexp.is_null());
- CStr::from_ptr(regexp)
- };
- let regexp_str = regexp_c_str.to_str().unwrap();
- let ans = searcher.search(text_str, regexp_str);
- searcher.results = ans;
- let res = &searcher.results;
- res.as_ptr() as *mut size_t
- }
- #[cfg(test)]
- mod tests {
- #[test]
- fn it_works() {
- assert_eq!(2 + 2, 4);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement