diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/macros.rs | 33 | ||||
-rw-r--r-- | src/tokenizer/char_ref/mod.rs | 1 | ||||
-rw-r--r-- | src/tokenizer/mod.rs | 1 | ||||
-rw-r--r-- | src/util/str.rs | 3 |
4 files changed, 33 insertions, 5 deletions
diff --git a/src/macros.rs b/src/macros.rs index 558a4a9..55977a4 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -31,3 +31,36 @@ macro_rules! time { (result, dt) }}; } + +/// Conditionally perform string formatting. +/// +/// If `$enabled` is true, then do the formatting and return a `Cow::Owned`. +/// +/// Otherwise, just return the borrowed (often `'static`) string +/// `$borrowed`. +/// +/// When `$enabled` is false, this avoids the overhead of allocating +/// and writing to a buffer, as well as any overhead or side effects +/// of the format arguments. +#[macro_export] +macro_rules! format_if { + ($enabled:expr, $borrowed:expr, $fmt:expr, $($args:expr),*) => { + if $enabled { + ::std::borrow::Cow::Owned(format!($fmt, $($args),*)) as ::std::borrow::Cow<str> + } else { + ::std::borrow::Cow::Borrowed($borrowed) + } + } +} + +/// Generate a test function `$name` which asserts that `$left` and `$right` +/// are equal. +#[macro_export] +macro_rules! test_eq { + ($name:ident, $left:expr, $right:expr) => { + #[test] + fn $name() { + assert_eq!($left, $right); + } + } +}
\ No newline at end of file diff --git a/src/tokenizer/char_ref/mod.rs b/src/tokenizer/char_ref/mod.rs index 6daeb13..336e0df 100644 --- a/src/tokenizer/char_ref/mod.rs +++ b/src/tokenizer/char_ref/mod.rs @@ -12,7 +12,6 @@ use crate::util::buffer_queue::BufferQueue; use crate::util::str::is_ascii_alnum; use log::debug; -use mac::format_if; use std::borrow::Cow::Borrowed; use std::char::from_u32; diff --git a/src/tokenizer/mod.rs b/src/tokenizer/mod.rs index eb22b11..8626191 100644 --- a/src/tokenizer/mod.rs +++ b/src/tokenizer/mod.rs @@ -24,7 +24,6 @@ use self::char_ref::{CharRef, CharRefTokenizer}; use crate::util::{smallcharset::SmallCharSet, str::lower_ascii_letter}; use log::debug; -use mac::{_tt_as_expr_hack, format_if, matches}; use std::borrow::Cow::{self, Borrowed}; use std::collections::BTreeMap; use std::default::Default; diff --git a/src/util/str.rs b/src/util/str.rs index c0f89f0..84604bc 100644 --- a/src/util/str.rs +++ b/src/util/str.rs @@ -7,8 +7,6 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -use mac::{_tt_as_expr_hack, matches}; - /// If `c` is an ASCII letter, return the corresponding lowercase /// letter, otherwise None. pub fn lower_ascii_letter(c: char) -> Option<char> { @@ -28,7 +26,6 @@ pub fn is_ascii_alnum(c: char) -> bool { #[allow(non_snake_case)] mod test { use super::{is_ascii_alnum, lower_ascii_letter}; - use mac::test_eq; test_eq!(lower_letter_a_is_a, lower_ascii_letter('a'), Some('a')); test_eq!(lower_letter_A_is_a, lower_ascii_letter('A'), Some('a')); |