Skip to content

Instantly share code, notes, and snippets.

@shadowmint
Created March 11, 2014 02:30
Show Gist options
  • Save shadowmint/9478400 to your computer and use it in GitHub Desktop.
Save shadowmint/9478400 to your computer and use it in GitHub Desktop.
rust nstrcmp
macro_rules! trace(
($($arg:tt)*) => (
{ let x = ::std::io::stdout().write_line(format_args!(::std::fmt::format, $($arg)*)); println!("{}", x); }
);
)
macro_rules! nstrcmp(
($x:ident, $y:ident, $n:expr) => (
$x.len() >= $n && $y.len() >= $n && $x.slice(0, $n) == $y.slice(0, $n)
);
)
#[test]
fn test_nstrcmp() {
let x = ~"Hello World";
let y = ~"Hello";
let z = ~"hello";
trace!("nstrcmp: {:?}", nstrcmp!(x, y, 5));
trace!("nstrcmp: {:?}", nstrcmp!(x, y, 10));
trace!("nstrcmp: {:?}", nstrcmp!(x, y, 3));
trace!("nstrcmp: {:?}", nstrcmp!(z, y, 4));
}
@shadowmint
Copy link
Author

strcmp: true
nstrcmp: false
nstrcmp: true
nstrcmp: false
test nstrcmp::test_nstrcmp ... ok

test result: ok. 2 passed; 0 failed; 0 ignored; 0 measured

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment