use std::ffi::CString;
use std::os::raw::c_char;
#[macro_export]
macro_rules! take_until_and_consume (
( $i:expr, $needle:expr ) => (
{
let input: &[u8] = $i;
let (rem, res) = ::nom::take_until!(input, $needle)?;
let (rem, _) = ::nom::take!(rem, $needle.len())?;
Ok((rem, res))
}
);
);
#[cfg(not(feature = "debug-validate"))]
#[macro_export]
macro_rules! debug_validate_bug_on (
($item:expr) => {};
);
#[cfg(feature = "debug-validate")]
#[macro_export]
macro_rules! debug_validate_bug_on (
($item:expr) => {
if $item {
panic!("Condition check failed");
}
};
);
#[cfg(not(feature = "debug-validate"))]
#[macro_export]
macro_rules! debug_validate_fail (
($msg:expr) => {};
);
#[cfg(feature = "debug-validate")]
#[macro_export]
macro_rules! debug_validate_fail (
($msg:expr) => {
if true {
panic!($msg);
}
};
);
pub fn rust_string_to_c(s: String) -> *mut c_char {
CString::new(s)
.map(|c_str| c_str.into_raw())
.unwrap_or(std::ptr::null_mut())
}
#[no_mangle]
pub unsafe extern "C" fn rs_cstring_free(s: *mut c_char) {
if s.is_null() {
return;
}
drop(CString::from_raw(s));
}
pub fn to_hex(input: &[u8]) -> String {
static CHARS: &'static [u8] = b"0123456789abcdef";
return input.iter().map(
|b| vec![char::from(CHARS[(b >> 4) as usize]), char::from(CHARS[(b & 0xf) as usize])]
).flatten().collect();
}