Created
January 6, 2023 13:17
-
-
Save linuxsoares/313b32b79b03aca462b92a0153fd01b5 to your computer and use it in GitHub Desktop.
Matcher text java x rust
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extern crate jni; | |
use aho_corasick::AhoCorasickBuilder; | |
use jni::objects::JClass; | |
use jni::objects::JList; | |
use jni::objects::JString; | |
use jni::sys::jint; | |
use jni::sys::jstring; | |
use jni::JNIEnv; | |
#[no_mangle] | |
#[allow(non_snake_case)] | |
pub extern "C" fn Java_App_match( | |
_env: JNIEnv, | |
_class: JClass, | |
input: JString, | |
keywords: JString, | |
) -> jstring { | |
let mut matches = vec![]; | |
let mut result = JList::from_env( | |
&_env, | |
_env.new_object("java/util/ArrayList", "()V", &[]).unwrap(), | |
) | |
.unwrap(); | |
let input_text: String = _env | |
.get_string(input) | |
.expect("Couldn't get java string!") | |
.into(); | |
let string_keys: String = _env | |
.get_string(keywords) | |
.expect("Couldn't get java string!") | |
.into(); | |
let words: Vec<&str> = string_keys.split_whitespace().collect(); | |
let ac = AhoCorasickBuilder::new() | |
.ascii_case_insensitive(true) | |
.build(&*words); | |
for mat in ac.find_iter(&input_text) { | |
matches.push(vec![mat.start(), mat.end()]); | |
} | |
for item in matches.iter() { | |
println!("{:?}", item); | |
result.add( | |
item | |
); | |
} | |
let output = _env | |
.new_string(format!("Hello, {}!", input_text)) | |
.expect("Couldn't create java string!"); | |
// Finally, extract the raw pointer to return. | |
output.into_inner() | |
// matches | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment