1 use onig_sys;
2 use std::ffi::{CStr, CString};
3 use std::mem;
4 
5 /// Get Version
6 ///
7 /// Returns the version information for the underlying Oniguruma
8 /// API. This is separate from the Rust Onig and onig_sys versions.
version() -> String9 pub fn version() -> String {
10     let raw_version = unsafe { CStr::from_ptr(onig_sys::onig_version()) };
11     raw_version.to_string_lossy().into_owned()
12 }
13 
14 /// Get Copyright
15 ///
16 /// Returns copyright information for the underlying Oniguruma
17 /// API. Rust onig is licensed seperately. For more information see
18 /// LICENSE.md in the source distribution.
copyright() -> String19 pub fn copyright() -> String {
20     let raw_copy = unsafe { CStr::from_ptr(onig_sys::onig_copyright()) };
21     raw_copy.to_string_lossy().into_owned()
22 }
23 
24 pub type CodePointRange = (onig_sys::OnigCodePoint, onig_sys::OnigCodePoint);
25 
26 /// Create a User Defined Proeprty
27 ///
28 /// Creates a new user defined property from the given OnigCodePoint vlaues.
define_user_property(name: &str, ranges: &[CodePointRange]) -> i3229 pub fn define_user_property(name: &str, ranges: &[CodePointRange]) -> i32 {
30     let mut raw_ranges = vec![ranges.len() as onig_sys::OnigCodePoint];
31     for &(start, end) in ranges {
32         raw_ranges.push(start);
33         raw_ranges.push(end);
34     }
35     let name = CString::new(name).unwrap();
36     let r = unsafe {
37         onig_sys::onig_unicode_define_user_property(name.as_ptr(), raw_ranges.as_mut_ptr())
38     };
39 
40     // Deliberately leak the memory here as Onig expects to be able to
41     // hang on to the pointer we just gave it. I'm not happy about it
42     // but this does work and the amounts of memory leaked should be
43     // trivial.
44     mem::forget(raw_ranges);
45     r
46 }
47 
48 #[cfg(test)]
49 mod tests {
50     use super::*;
51 
52     #[test]
utils_get_copyright_is_not_emtpy()53     pub fn utils_get_copyright_is_not_emtpy() {
54         let copyright = copyright();
55         assert!(copyright.len() > 0);
56     }
57 }
58