add c_export

This commit is contained in:
2020-01-30 21:03:38 +08:00
parent 9b6409e134
commit ce78aa2a93
6 changed files with 82 additions and 0 deletions

17
c_export/src/lib.rs Normal file
View File

@@ -0,0 +1,17 @@
extern crate libc;
use std::ffi::CStr;
use libc::c_char;
#[no_mangle]
pub extern "C" fn count_substrings(value: *const c_char, substr: *const c_char) -> i32 {
let c_value = unsafe { CStr::from_ptr(value) };
let c_substr = unsafe { CStr::from_ptr(substr) };
match c_value.to_str() {
Ok(value) => match c_substr.to_str() {
Ok(substr) => value.match_indices(substr).count() as i32,
Err(_) => -1,
},
Err(_) => -1,
}
}