Skip to content

camsteffen/count-tts

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Count Token Trees Macro for Rust

crates.io Docs License

A simple macro for counting tokens.

use count_tts::count_tts;

assert_eq!(
    count_tts!(1 2 3 4 5 6 7),
    7
);

macro_rules! alternative

If you prefer to avoid adding a dependency, here is an equivalent macro_rules! definition that you may copy into your code.

/// `macro_rules!` implementation of `count_tts`.
/// Source: https://github.com/camsteffen/count-tts
macro_rules! count_tts {
    () => (0);
    ($one:tt) => (1);
    ($($a:tt $b:tt)+) => (count_tts!($($a)+) << 1);
    ($odd:tt $($a:tt $b:tt)+) => (count_tts!($($a)+) << 1 | 1);
}

This is slightly less optimal than the macro provided by the crate since it is not a procedural macro. Instead of just a number, it generates an equivalent expression of 1's and bitwise operators. It is optimized for logarithmic recursion and output length.

About

A simple macro for counting tokens

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages