diff --git a/keyvi/bin/keyvi_c/c_api.cpp b/keyvi/bin/keyvi_c/c_api.cpp index 7925dd6a8..55905c312 100644 --- a/keyvi/bin/keyvi_c/c_api.cpp +++ b/keyvi/bin/keyvi_c/c_api.cpp @@ -109,8 +109,8 @@ char* keyvi_dictionary_get_statistics(const keyvi_dictionary* dict) { return std_2_c_string(dict->obj_->GetStatistics()); } -keyvi_match* keyvi_dictionary_get(const keyvi_dictionary* dict, const char* key) { - return new keyvi_match(dict->obj_->operator[](key)); +keyvi_match* keyvi_dictionary_get(const keyvi_dictionary* dict, const char* key, const size_t key_len) { + return new keyvi_match(dict->obj_->operator[](std::string(key, key_len))); } keyvi_match_iterator* keyvi_dictionary_get_all_items(const keyvi_dictionary* dict) { diff --git a/keyvi/include/keyvi/c_api/c_api.h b/keyvi/include/keyvi/c_api/c_api.h index d20337598..3c2d35fb8 100644 --- a/keyvi/include/keyvi/c_api/c_api.h +++ b/keyvi/include/keyvi/c_api/c_api.h @@ -65,7 +65,7 @@ size_t keyvi_dictionary_get_size(const struct keyvi_dictionary*); char* keyvi_dictionary_get_statistics(const struct keyvi_dictionary*); -struct keyvi_match* keyvi_dictionary_get(const struct keyvi_dictionary*, const char*); +struct keyvi_match* keyvi_dictionary_get(const struct keyvi_dictionary*, const char*, const size_t); struct keyvi_match_iterator* keyvi_dictionary_get_all_items(const struct keyvi_dictionary*); diff --git a/rust/src/dictionary.rs b/rust/src/dictionary.rs index f7dcb0b2a..448dc6fef 100644 --- a/rust/src/dictionary.rs +++ b/rust/src/dictionary.rs @@ -65,8 +65,9 @@ impl Dictionary { } pub fn get(&self, key: &str) -> KeyviMatch { - let key_c = CString::new(key).unwrap(); - let match_ptr = unsafe { root::keyvi_dictionary_get(self.dict, key_c.as_ptr()) }; + let match_ptr = unsafe { + root::keyvi_dictionary_get(self.dict, key.as_ptr() as *const i8, key.len() as u64) + }; KeyviMatch::new(match_ptr) } diff --git a/rust/test_data/test.kv b/rust/test_data/test.kv index e5c37d8af..2beaf72cb 100644 Binary files a/rust/test_data/test.kv and b/rust/test_data/test.kv differ diff --git a/rust/tests/tests.rs b/rust/tests/tests.rs index 3aafced1d..4612c0d20 100644 --- a/rust/tests/tests.rs +++ b/rust/tests/tests.rs @@ -26,7 +26,7 @@ mod tests { #[test] fn dictionary_size() { let dict = dictionary::Dictionary::new("test_data/test.kv").unwrap(); - assert_eq!(dict.size(), 3); + assert_eq!(dict.size(), 5); } #[test] @@ -77,18 +77,25 @@ mod tests { #[test] fn match_msgpacked_value_non_existing_key() { - let m = dictionary::Dictionary::new("test_data/test.kv") - .unwrap() - .get("non-existing-key"); + let d = dictionary::Dictionary::new("test_data/test.kv").unwrap(); + let m = d.get("non-existing-key"); + assert!(m.get_value_as_string().is_empty()); + + let m = d.get("non-existing-key-with-\0-in-middle"); assert!(m.get_value_as_string().is_empty()); } #[test] fn match_value() { - let m = dictionary::Dictionary::new("test_data/test.kv") - .unwrap() - .get("a"); + let d = dictionary::Dictionary::new("test_data/test.kv").unwrap(); + let m = d.get("a"); assert_eq!(m.get_value_as_string(), "[12,13]"); + + let m = d.get("d\0"); + assert_eq!(m.get_value_as_string(), "[1,2]"); + + let m = d.get("e\0f"); + assert_eq!(m.get_value_as_string(), "[3,4]"); } #[test]