use std::ffi::c_int;
use std::ops::{BitAnd, BitAndAssign, BitOr, BitOrAssign, Not};
use crate::tre;
pub type RegFlags = c_int;
macro_rules! flags {
(
$(#[$meta:meta])*
$name:ident {
$($(#[$flag_meta:meta])* $flag:ident = $value:expr;)*
}
) => {
$(#[$meta])*
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(transparent)]
pub struct $name(RegFlags);
impl $name {
pub const NONE: Self = Self(0);
$($(#[$flag_meta])* pub const $flag: Self = Self($value);)*
#[must_use]
pub const fn new() -> Self {
Self::NONE
}
#[must_use]
pub const fn from_bits_retain(bits: RegFlags) -> Self {
Self(bits)
}
#[must_use]
pub const fn bits(self) -> RegFlags {
self.0
}
#[must_use]
pub const fn is_empty(self) -> bool {
self.0 == 0
}
#[must_use]
pub const fn contains(self, other: Self) -> bool {
self.0 & other.0 == other.0
}
#[must_use]
pub const fn intersects(self, other: Self) -> bool {
self.0 & other.0 != 0
}
#[must_use]
pub const fn add(self, flag: Self) -> Self {
Self(self.0 | flag.0)
}
#[must_use]
pub const fn remove(self, flag: Self) -> Self {
Self(self.0 & !flag.0)
}
}
impl BitOr for $name {
type Output = Self;
fn bitor(self, rhs: Self) -> Self { Self(self.0 | rhs.0) }
}
impl BitOrAssign for $name {
fn bitor_assign(&mut self, rhs: Self) { self.0 |= rhs.0; }
}
impl BitAnd for $name {
type Output = Self;
fn bitand(self, rhs: Self) -> Self { Self(self.0 & rhs.0) }
}
impl BitAndAssign for $name {
fn bitand_assign(&mut self, rhs: Self) { self.0 &= rhs.0; }
}
impl Not for $name {
type Output = Self;
fn not(self) -> Self { Self(!self.0) }
}
};
}
flags! {
RegcompFlags {
BASIC = tre::REG_BASIC;
EXTENDED = tre::REG_EXTENDED;
ICASE = tre::REG_ICASE;
LITERAL = tre::REG_LITERAL;
NOSPEC = tre::REG_NOSPEC;
NEWLINE = tre::REG_NEWLINE;
NOSUB = tre::REG_NOSUB;
RIGHT_ASSOC = tre::REG_RIGHT_ASSOC;
UNGREEDY = tre::REG_UNGREEDY;
USEBYTES = tre::REG_USEBYTES;
}
}
flags! {
RegexecFlags {
APPROX_MATCHER = tre::REG_APPROX_MATCHER;
BACKTRACKING_MATCHER = tre::REG_BACKTRACKING_MATCHER;
NOTBOL = tre::REG_NOTBOL;
NOTEOL = tre::REG_NOTEOL;
}
}