Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix case-sensitive search in NumByStr dictionary #1

Merged
merged 3 commits into from
Nov 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .cargo/config
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@

[build]
target = "i686-pc-windows-msvc"
out-dir = "..\\..\\exe\\lib"
out-dir = "..\\..\\exe\\lib"

[alias]
b = "build -Z unstable-options"
2 changes: 1 addition & 1 deletion 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
@@ -1,6 +1,6 @@
[package]
name = "sanny_builder_core"
version = "0.1.0"
version = "0.1.1"
authors = ["Seemann <[email protected]>"]
edition = "2018"

Expand Down
62 changes: 9 additions & 53 deletions src/dictionary/dictionary_num_by_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub extern "C" fn dictionary_num_by_str_new(
) -> *mut DictNumByStr {
ptr_new(Dict::new(
duplicates.into(),
CaseFormat::NoFormat,
CaseFormat::LowerCase,
pchar_to_string(comments).unwrap_or(String::new()),
pchar_to_string(delimiters).unwrap_or(String::new()),
trim,
Expand Down Expand Up @@ -53,35 +53,13 @@ pub unsafe extern "C" fn dictionary_num_by_str_find(
out: *mut i32,
) -> bool {
boolclosure! {{
let key = CString::new(pchar_to_str(key)?).ok()?;
*out = *dict.as_mut()?.map.get(&key)?;
Some(())
}}
}

#[no_mangle]
pub unsafe extern "C" fn dictionary_num_by_str_get_entry(
dict: *mut DictNumByStr,
index: usize,
out_key: *mut PChar,
out_value: *mut i32,
) -> bool {
boolclosure! {{
let (key, value) = dict.as_mut()?.map.iter().nth(index)?;
*out_key = key.as_ptr();
*out_value = *value;
let d = dict.as_mut()?;
let key = apply_format(pchar_to_str(key)?, &d.case_format)?;
*out = *d.map.get(&key)?;
Some(())
}}
}

#[no_mangle]
pub unsafe extern "C" fn dictionary_num_by_str_get_count(dict: *mut DictNumByStr) -> usize {
if let Some(ptr) = dict.as_mut() {
return ptr.map.len();
}
return 0;
}

#[no_mangle]
pub unsafe extern "C" fn dictionary_num_by_str_free(ptr: *mut DictNumByStr) {
ptr_free(ptr);
Expand All @@ -91,25 +69,6 @@ pub unsafe extern "C" fn dictionary_num_by_str_free(ptr: *mut DictNumByStr) {
mod tests {
use super::*;

#[test]
fn test_dictionary_num_by_str_get_count() {
unsafe {
let f = dictionary_num_by_str_new(
Duplicates::Replace.into(),
false,
pchar!(";"),
pchar!(",="),
true,
);

assert!(f.as_mut().is_some());
let loaded =
dictionary_num_by_str_load_file(f, pchar!("src/dictionary/test/keywords.txt"));
assert!(loaded);
assert_eq!(dictionary_num_by_str_get_count(f), 2);
}
}

#[test]
fn test_dictionary_num_by_str_find() {
unsafe {
Expand All @@ -126,9 +85,8 @@ mod tests {
dictionary_num_by_str_load_file(f, pchar!("src/dictionary/test/keywords-hex.txt"));
assert!(loaded);

assert_eq!(dictionary_num_by_str_get_count(f), 23);
let mut i = 0;
assert!(dictionary_num_by_str_find(f, pchar!("wait"), &mut i));
assert!(dictionary_num_by_str_find(f, pchar!("Wait"), &mut i));
assert_eq!(i, 1);
i = -1;
assert!(!dictionary_num_by_str_find(f, pchar!(""), &mut i));
Expand All @@ -146,14 +104,14 @@ mod tests {
pchar!(",="),
true,
);
assert!(f.as_mut().is_some());
assert!(f.as_ref().is_some());

assert_eq!(f.as_ref().unwrap().case_format, CaseFormat::LowerCase);

let loaded =
dictionary_num_by_str_load_file(f, pchar!("src/dictionary/test/keywords-dups.txt"));
assert!(loaded);

assert_eq!(dictionary_num_by_str_get_count(f), 2);

let mut i = 0;
assert!(dictionary_num_by_str_find(f, pchar!("wait"), &mut i));
assert_eq!(i, 1);
Expand All @@ -172,14 +130,12 @@ mod tests {
pchar!(",="),
true,
);
assert!(f.as_mut().is_some());
assert!(f.as_ref().is_some());

let loaded =
dictionary_num_by_str_load_file(f, pchar!("src/dictionary/test/keywords-dups.txt"));
assert!(loaded);

assert_eq!(dictionary_num_by_str_get_count(f), 2);

let mut i = 0;
assert!(dictionary_num_by_str_find(f, pchar!("wait"), &mut i));
assert_eq!(i, 1);
Expand Down
5 changes: 3 additions & 2 deletions src/dictionary/dictionary_str_by_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,9 @@ pub unsafe extern "C" fn dictionary_str_by_str_find(
out: *mut PChar,
) -> bool {
boolclosure! {{
let key = CString::new(pchar_to_str(key)?.to_ascii_lowercase()).ok()?;
*out = dict.as_mut()?.map.get(&key)?.as_ptr();
let d = dict.as_mut()?;
let key = apply_format(pchar_to_str(key)?, &d.case_format)?;
*out = d.map.get(&key)?.as_ptr();
Some(())
}}
}
Expand Down
10 changes: 4 additions & 6 deletions src/dictionary/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,10 @@ where
Some(())
}

pub fn add_raw(&mut self, key: &str, value: &str) {
if let Some((key, value)) =
<(T, U)>::get_key_value(key, value, self.hex_keys, &self.case_format)
{
self.add(key, value)
}
pub fn add_raw(&mut self, key: &str, value: &str) -> Option<()> {
let (key, value) = <(T, U)>::get_key_value(key, value, self.hex_keys, &self.case_format)?;
self.add(key, value);
Some(())
}

pub fn add(&mut self, key: T, value: U) {
Expand Down
91 changes: 91 additions & 0 deletions src/dictionary/list_num_by_str.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
use crate::common_ffi::*;
use crate::dictionary::ffi::*;
use std::ffi::CString;

pub type ListNumByStr = Dict<CString, i32>;

#[no_mangle]
pub extern "C" fn list_num_by_str_new(
duplicates: u8,
hex_keys: bool,
comments: PChar,
delimiters: PChar,
trim: bool,
) -> *mut ListNumByStr {
ptr_new(Dict::new(
duplicates.into(),
CaseFormat::NoFormat,
pchar_to_string(comments).unwrap_or(String::new()),
pchar_to_string(delimiters).unwrap_or(String::new()),
trim,
hex_keys,
))
}

#[no_mangle]
pub unsafe extern "C" fn list_num_by_str_load_file(
list: *mut ListNumByStr,
file_name: PChar,
) -> bool {
boolclosure! {{
list.as_mut()?.load_file(pchar_to_str(file_name)?)
}}
}

#[no_mangle]
pub unsafe extern "C" fn list_num_by_str_get_entry(
list: *mut ListNumByStr,
index: usize,
out_key: *mut PChar,
out_value: *mut i32,
) -> bool {
boolclosure! {{
let (key, value) = list.as_mut()?.map.iter().nth(index)?;
*out_key = key.as_ptr();
*out_value = *value;
Some(())
}}
}

#[no_mangle]
pub unsafe extern "C" fn list_num_by_str_get_count(list: *mut ListNumByStr) -> usize {
if let Some(ptr) = list.as_mut() {
return ptr.map.len();
}
return 0;
}

#[no_mangle]
pub unsafe extern "C" fn list_num_by_str_free(ptr: *mut ListNumByStr) {
ptr_free(ptr);
}

#[test]
fn test_list_num_by_str_get_entry() {
unsafe {
let f = list_num_by_str_new(
Duplicates::Replace.into(),
true,
pchar!(";"),
pchar!(",="),
true,
);
assert!(f.as_mut().is_some());
assert_eq!(f.as_ref().unwrap().case_format, CaseFormat::NoFormat);

let loaded = list_num_by_str_load_file(f, pchar!("src/dictionary/test/keywords-hex.txt"));
assert!(loaded);

let mut key = pchar!("");
let mut value = 0;
let res = list_num_by_str_get_entry(f, 1, &mut key, &mut value);
assert!(res);
assert_eq!(pchar_to_str(key).unwrap(), "goto");
assert_eq!(value, 0x0002);

let res = list_num_by_str_get_entry(f, 4, &mut key, &mut value);
assert!(res);
assert_eq!(pchar_to_str(key).unwrap(), "create_thread");
assert_eq!(value, 0x004f);
}
}
1 change: 1 addition & 0 deletions src/dictionary/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ pub mod dictionary_num_by_str;
pub mod dictionary_str_by_num;
pub mod dictionary_str_by_str;
pub mod ffi;
pub mod list_num_by_str;
2 changes: 1 addition & 1 deletion src/dictionary/test/keywords-hex.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
0001=wait
0001=WaIt
0002=goto
0002=jump
0003=shake_camera
Expand Down
2 changes: 1 addition & 1 deletion src/namespaces/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ pub unsafe extern "C" fn classes_filter_props_by_name(
ns: *mut Namespaces,
class_name: PChar,
needle: PChar,
dict: *mut crate::dictionary::dictionary_num_by_str::DictNumByStr,
dict: *mut crate::dictionary::list_num_by_str::ListNumByStr,
) -> bool {
boolclosure! {{
let items = ns.as_mut()?.filter_class_props_by_name(pchar_to_str(class_name)?, pchar_to_str(needle)?)?;
Expand Down