#![allow(clippy::transmute_ptr_to_ptr)]
#![allow(clippy::transmute_ptr_to_ref)]
use super::{MetaCharType, RegexOptions, SyntaxBehavior, SyntaxOperator};
use std::mem::transmute;
#[derive(Copy, Clone)]
pub enum MetaChar {
Character(char),
Ineffective,
}
#[derive(Debug, Clone, Copy)]
#[repr(transparent)]
pub struct Syntax {
raw: onig_sys::OnigSyntaxType,
}
impl Syntax {
pub fn python() -> &'static Syntax {
unsafe { transmute(&onig_sys::OnigSyntaxPython) }
}
pub fn asis() -> &'static Syntax {
unsafe { transmute(&onig_sys::OnigSyntaxASIS) }
}
pub fn posix_basic() -> &'static Syntax {
unsafe { transmute(&onig_sys::OnigSyntaxPosixBasic) }
}
pub fn posix_extended() -> &'static Syntax {
unsafe { transmute(&onig_sys::OnigSyntaxPosixExtended) }
}
pub fn emacs() -> &'static Syntax {
unsafe { transmute(&onig_sys::OnigSyntaxEmacs) }
}
pub fn grep() -> &'static Syntax {
unsafe { transmute(&onig_sys::OnigSyntaxGrep) }
}
pub fn gnu_regex() -> &'static Syntax {
unsafe { transmute(&onig_sys::OnigSyntaxGnuRegex) }
}
pub fn java() -> &'static Syntax {
unsafe { transmute(&onig_sys::OnigSyntaxJava) }
}
pub fn perl() -> &'static Syntax {
unsafe { transmute(&onig_sys::OnigSyntaxPerl) }
}
pub fn perl_ng() -> &'static Syntax {
unsafe { transmute(&onig_sys::OnigSyntaxPerl_NG) }
}
pub fn ruby() -> &'static Syntax {
unsafe { transmute(&onig_sys::OnigSyntaxRuby) }
}
pub fn oniguruma() -> &'static Syntax {
unsafe { transmute(&onig_sys::OnigSyntaxOniguruma) }
}
pub fn default() -> &'static Syntax {
unsafe { transmute(onig_sys::OnigDefaultSyntax) }
}
pub fn operators(&self) -> SyntaxOperator {
SyntaxOperator::from_bits_truncate(self.operators_bits())
}
fn operators_bits(&self) -> u64 {
unsafe {
let op = onig_sys::onig_get_syntax_op(self.raw_mut());
let op2 = onig_sys::onig_get_syntax_op2(self.raw_mut());
u64::from(op) + (u64::from(op2) << 32)
}
}
pub fn set_operators(&mut self, operators: SyntaxOperator) {
self.set_operators_bits(operators.bits())
}
fn set_operators_bits(&mut self, operators_bits: u64) {
let op = operators_bits as onig_sys::OnigSyntaxOp;
let op2 = (operators_bits >> 32) as onig_sys::OnigSyntaxOp2;
unsafe {
onig_sys::onig_set_syntax_op(&mut self.raw, op);
onig_sys::onig_set_syntax_op2(&mut self.raw, op2)
}
}
pub fn enable_operators(&mut self, operators: SyntaxOperator) {
let operators = self.operators_bits() | operators.bits();
self.set_operators_bits(operators)
}
pub fn disable_operators(&mut self, operators: SyntaxOperator) {
let operators = self.operators_bits() & !operators.bits();
self.set_operators_bits(operators)
}
pub fn behavior(&self) -> SyntaxBehavior {
SyntaxBehavior::from_bits_truncate(unsafe {
onig_sys::onig_get_syntax_behavior(self.raw_mut())
})
}
pub fn set_behavior(&mut self, behavior: SyntaxBehavior) {
let behavior = behavior.bits() as onig_sys::OnigSyntaxBehavior;
unsafe {
onig_sys::onig_set_syntax_behavior(&mut self.raw, behavior);
}
}
pub fn enable_behavior(&mut self, behavior: SyntaxBehavior) {
let behavior = self.behavior() | behavior;
self.set_behavior(behavior)
}
pub fn disable_behavior(&mut self, behavior: SyntaxBehavior) {
let behavior = self.behavior() & !behavior;
self.set_behavior(behavior)
}
pub fn options(&self) -> RegexOptions {
RegexOptions::from_bits_truncate(unsafe {
onig_sys::onig_get_syntax_options(self.raw_mut())
})
}
pub fn set_options(&mut self, options: RegexOptions) {
let options = options.bits() as onig_sys::OnigOptionType;
unsafe {
onig_sys::onig_set_syntax_options(&mut self.raw, options);
}
}
pub fn set_meta_char(&mut self, what: MetaCharType, meta: MetaChar) {
let what = what.bits();
let code = match meta {
MetaChar::Ineffective => onig_sys::ONIG_INEFFECTIVE_META_CHAR,
MetaChar::Character(char) => char as u32,
};
unsafe {
onig_sys::onig_set_meta_char(&mut self.raw, what, code);
}
}
fn raw_mut(&self) -> *mut onig_sys::OnigSyntaxType {
&self.raw as *const onig_sys::OnigSyntaxType as *mut onig_sys::OnigSyntaxType
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn round_trip_bits() {
let mut syn = Syntax::python().clone();
syn.enable_operators(SyntaxOperator::SYNTAX_OPERATOR_ESC_X_BRACE_HEX8);
assert_ne!(Syntax::python().raw, syn.raw);
syn.disable_operators(SyntaxOperator::SYNTAX_OPERATOR_ESC_X_BRACE_HEX8);
assert_eq!(Syntax::python().raw, syn.raw);
}
}