1 #[doc(hidden)] 2 #[macro_export] 3 macro_rules! regex ( 4 ($re: ident, $s:expr) => ( 5 lazy_static! { 6 static ref $re: $crate::lib::regex::Regex = $crate::lib::regex::Regex::new($s).unwrap(); 7 } 8 ); 9 ); 10 11 #[doc(hidden)] 12 #[macro_export] 13 macro_rules! regex_bytes ( 14 ($re: ident, $s:expr) => ( 15 lazy_static! { 16 static ref $re: $crate::lib::regex::bytes::Regex = $crate::lib::regex::bytes::Regex::new($s).unwrap(); 17 } 18 ); 19 ); 20 21 /// `re_match!(regexp) => &[T] -> IResult<&[T], &[T]>` 22 /// Returns the whole input if a match is found 23 /// 24 /// requires the `regexp` feature 25 #[macro_export(local_inner_macros)] 26 macro_rules! re_match ( 27 ($i:expr, $re:expr) => ( 28 { 29 use $crate::lib::std::result::Result::*; 30 use $crate::{Err,error::ErrorKind,IResult}; 31 32 use $crate::InputLength; 33 use $crate::Slice; 34 let re = $crate::lib::regex::Regex::new($re).unwrap(); 35 if re.is_match(&$i) { 36 Ok(($i.slice($i.input_len()..), $i)) 37 } else { 38 let res: IResult<_,_> = Err(Err::Error(error_position!($i, ErrorKind::RegexpMatch))); 39 res 40 } 41 } 42 ) 43 ); 44 45 #[cfg(feature = "regexp_macros")] 46 /// `re_match_static!(regexp) => &[T] -> IResult<&[T], &[T]>` 47 /// Returns the whole input if a match is found. Regular expression calculated at compile time 48 /// 49 /// requires the `regexp_macros` feature 50 #[macro_export(local_inner_macros)] 51 macro_rules! re_match_static ( 52 ($i:expr, $re:expr) => ( 53 { 54 use $crate::lib::std::result::Result::*; 55 use $crate::{Err,error::ErrorKind,IResult}; 56 57 use $crate::InputLength; 58 use $crate::Slice; 59 regex!(RE, $re); 60 if RE.is_match(&$i) { 61 Ok(($i.slice($i.input_len()..), $i)) 62 } else { 63 let res: IResult<_,_> = Err(Err::Error(error_position!($i, ErrorKind::RegexpMatch))); 64 res 65 } 66 } 67 ) 68 ); 69 70 /// `re_bytes_match!(regexp) => &[T] -> IResult<&[T], &[T]>` 71 /// Returns the whole input if a match is found 72 /// 73 /// requires the `regexp` feature 74 #[macro_export(local_inner_macros)] 75 macro_rules! re_bytes_match ( 76 ($i:expr, $re:expr) => ( 77 { 78 use $crate::lib::std::result::Result::*; 79 use $crate::{Err,error::ErrorKind,IResult}; 80 81 use $crate::InputLength; 82 use $crate::Slice; 83 let re = $crate::lib::regex::bytes::Regex::new($re).unwrap(); 84 if re.is_match(&$i) { 85 Ok(($i.slice($i.input_len()..), $i)) 86 } else { 87 let res: IResult<_,_> = Err(Err::Error(error_position!($i, ErrorKind::RegexpMatch))); 88 res 89 } 90 } 91 ) 92 ); 93 94 #[cfg(feature = "regexp_macros")] 95 /// `re_bytes_match_static!(regexp) => &[T] -> IResult<&[T], &[T]>` 96 /// Returns the whole input if a match is found. Regular expression calculated at compile time 97 /// 98 /// requires the `regexp_macros` feature 99 #[macro_export(local_inner_macros)] 100 macro_rules! re_bytes_match_static ( 101 ($i:expr, $re:expr) => ( 102 { 103 use $crate::lib::std::result::Result::*; 104 use $crate::{Err,error::ErrorKind,IResult}; 105 106 use $crate::InputLength; 107 use $crate::Slice; 108 regex_bytes!(RE, $re); 109 if RE.is_match(&$i) { 110 Ok(($i.slice($i.input_len()..), $i)) 111 } else { 112 let res: IResult<_,_> = Err(Err::Error(error_position!($i, ErrorKind::RegexpMatch))); 113 res 114 } 115 } 116 ) 117 ); 118 119 /// `re_find!(regexp) => &[T] -> IResult<&[T], &[T]>` 120 /// Returns the first match 121 /// 122 /// requires the `regexp` feature 123 #[macro_export(local_inner_macros)] 124 macro_rules! re_find ( 125 ($i:expr, $re:expr) => ( 126 { 127 use $crate::lib::std::result::Result::*; 128 use $crate::{Err,error::ErrorKind,IResult}; 129 130 use $crate::Slice; 131 let re = $crate::lib::regex::Regex::new($re).unwrap(); 132 if let Some(m) = re.find(&$i) { 133 Ok(($i.slice(m.end()..), $i.slice(m.start()..m.end()))) 134 } else { 135 let res: IResult<_,_> = Err(Err::Error(error_position!($i, ErrorKind::RegexpFind))); 136 res 137 } 138 } 139 ) 140 ); 141 142 #[cfg(feature = "regexp_macros")] 143 /// `re_find_static!(regexp) => &[T] -> IResult<&[T], &[T]>` 144 /// Returns the first match. Regular expression calculated at compile time 145 /// 146 /// requires the `regexp_macros` feature 147 #[macro_export(local_inner_macros)] 148 macro_rules! re_find_static ( 149 ($i:expr, $re:expr) => ( 150 { 151 use $crate::lib::std::result::Result::*; 152 use $crate::{Err,error::ErrorKind,IResult}; 153 154 use $crate::Slice; 155 regex!(RE, $re); 156 if let Some(m) = RE.find(&$i) { 157 Ok(($i.slice(m.end()..), $i.slice(m.start()..m.end()))) 158 } else { 159 let res: IResult<_,_> = Err(Err::Error(error_position!($i, ErrorKind::RegexpFind))); 160 res 161 } 162 } 163 164 ) 165 ); 166 167 /// `re_bytes_find!(regexp) => &[T] -> IResult<&[T], &[T]>` 168 /// Returns the first match 169 /// 170 /// requires the `regexp` feature 171 #[macro_export(local_inner_macros)] 172 macro_rules! re_bytes_find ( 173 ($i:expr, $re:expr) => ( 174 { 175 use $crate::lib::std::result::Result::*; 176 use $crate::{Err,error::ErrorKind,IResult}; 177 178 use $crate::Slice; 179 let re = $crate::lib::regex::bytes::Regex::new($re).unwrap(); 180 if let Some(m) = re.find(&$i) { 181 Ok(($i.slice(m.end()..), $i.slice(m.start()..m.end()))) 182 } else { 183 let res: IResult<_,_> = Err(Err::Error(error_position!($i, ErrorKind::RegexpFind))); 184 res 185 } 186 } 187 ) 188 ); 189 190 #[cfg(feature = "regexp_macros")] 191 /// `re_bytes_find!(regexp) => &[T] -> IResult<&[T], &[T]>` 192 /// Returns the first match. Regular expression calculated at compile time 193 /// 194 /// requires the `regexp_macros` feature 195 #[macro_export(local_inner_macros)] 196 macro_rules! re_bytes_find_static ( 197 ($i:expr, $re:expr) => ( 198 { 199 use $crate::lib::std::result::Result::*; 200 use $crate::{Err,error::ErrorKind,IResult}; 201 202 use $crate::Slice; 203 regex_bytes!(RE, $re); 204 if let Some(m) = RE.find(&$i) { 205 Ok(($i.slice(m.end()..), $i.slice(m.start()..m.end()))) 206 } else { 207 let res: IResult<_,_> = Err(Err::Error(error_position!($i, ErrorKind::RegexpFind))); 208 res 209 } 210 } 211 212 ) 213 ); 214 215 /// `re_matches!(regexp) => &[T] -> IResult<&[T], Vec<&[T]>>` 216 /// Returns all the matched parts 217 /// 218 /// requires the `regexp` feature 219 #[macro_export(local_inner_macros)] 220 macro_rules! re_matches ( 221 ($i:expr, $re:expr) => ( 222 { 223 use $crate::lib::std::result::Result::*; 224 use $crate::{Err,error::ErrorKind,IResult}; 225 226 use $crate::Slice; 227 let re = $crate::lib::regex::Regex::new($re).unwrap(); 228 let v: Vec<_> = re.find_iter(&$i).map(|m| $i.slice(m.start()..m.end())).collect(); 229 if v.len() != 0 { 230 let offset = { 231 let end = v.last().unwrap(); 232 end.as_ptr() as usize + end.len() - $i.as_ptr() as usize 233 }; 234 Ok(($i.slice(offset..), v)) 235 } else { 236 let res: IResult<_,_> = Err(Err::Error(error_position!($i, ErrorKind::RegexpMatches))); 237 res 238 } 239 } 240 ) 241 ); 242 243 #[cfg(feature = "regexp_macros")] 244 /// `re_matches_static!(regexp) => &[T] -> IResult<&[T], Vec<&[T]>>` 245 /// Returns all the matched parts. Regular expression calculated at compile time 246 /// 247 /// requires the `regexp_macros` feature 248 #[macro_export(local_inner_macros)] 249 macro_rules! re_matches_static ( 250 ($i:expr, $re:expr) => ( 251 { 252 use $crate::lib::std::result::Result::*; 253 use $crate::{Err,error::ErrorKind,IResult}; 254 255 use $crate::Slice; 256 regex!(RE, $re); 257 let v: Vec<_> = RE.find_iter(&$i).map(|m| $i.slice(m.start()..m.end())).collect(); 258 if v.len() != 0 { 259 let offset = { 260 let end = v.last().unwrap(); 261 end.as_ptr() as usize + end.len() - $i.as_ptr() as usize 262 }; 263 Ok(($i.slice(offset..), v)) 264 } else { 265 let res: IResult<_,_> = Err(Err::Error(error_position!($i, ErrorKind::RegexpMatches))); 266 res 267 } 268 } 269 ) 270 ); 271 272 /// `re_bytes_matches!(regexp) => &[T] -> IResult<&[T], Vec<&[T]>>` 273 /// Returns all the matched parts 274 /// 275 /// requires the `regexp` feature 276 #[macro_export(local_inner_macros)] 277 macro_rules! re_bytes_matches ( 278 ($i:expr, $re:expr) => ( 279 { 280 use $crate::lib::std::result::Result::*; 281 use $crate::{Err,error::ErrorKind,IResult}; 282 283 use $crate::Slice; 284 let re = $crate::lib::regex::bytes::Regex::new($re).unwrap(); 285 let v: Vec<_> = re.find_iter(&$i).map(|m| $i.slice(m.start()..m.end())).collect(); 286 if v.len() != 0 { 287 let offset = { 288 let end = v.last().unwrap(); 289 end.as_ptr() as usize + end.len() - $i.as_ptr() as usize 290 }; 291 Ok(($i.slice(offset..), v)) 292 } else { 293 let res: IResult<_,_> = Err(Err::Error(error_position!($i, ErrorKind::RegexpMatches))); 294 res 295 } 296 } 297 ) 298 ); 299 300 #[cfg(feature = "regexp_macros")] 301 /// `re_bytes_matches_static!(regexp) => &[T] -> IResult<&[T], Vec<&[T]>>` 302 /// Returns all the matched parts. Regular expression calculated at compile time 303 /// 304 /// requires the `regexp_macros` feature 305 #[macro_export(local_inner_macros)] 306 macro_rules! re_bytes_matches_static ( 307 ($i:expr, $re:expr) => ( 308 { 309 use $crate::lib::std::result::Result::*; 310 use $crate::{Err,error::ErrorKind,IResult}; 311 312 use $crate::Slice; 313 regex_bytes!(RE, $re); 314 let v: Vec<_> = RE.find_iter(&$i).map(|m| $i.slice(m.start()..m.end())).collect(); 315 if v.len() != 0 { 316 let offset = { 317 let end = v.last().unwrap(); 318 end.as_ptr() as usize + end.len() - $i.as_ptr() as usize 319 }; 320 Ok(($i.slice(offset..), v)) 321 } else { 322 let res: IResult<_,_> = Err(Err::Error(error_position!($i, ErrorKind::RegexpMatches))); 323 res 324 } 325 } 326 ) 327 ); 328 329 /// `re_capture!(regexp) => &[T] -> IResult<&[T], Vec<&[T]>>` 330 /// Returns the first capture group 331 /// 332 /// requires the `regexp` feature 333 #[macro_export(local_inner_macros)] 334 macro_rules! re_capture ( 335 ($i:expr, $re:expr) => ( 336 { 337 use $crate::lib::std::result::Result::*; 338 use $crate::{Err,error::ErrorKind,IResult}; 339 340 use $crate::Slice; 341 let re = $crate::lib::regex::Regex::new($re).unwrap(); 342 if let Some(c) = re.captures(&$i) { 343 let v:Vec<_> = c.iter().filter(|el| el.is_some()).map(|el| el.unwrap()).map(|m| $i.slice(m.start()..m.end())).collect(); 344 let offset = { 345 let end = v.last().unwrap(); 346 end.as_ptr() as usize + end.len() - $i.as_ptr() as usize 347 }; 348 Ok(($i.slice(offset..), v)) 349 } else { 350 let res: IResult<_,_> = Err(Err::Error(error_position!($i, ErrorKind::RegexpCapture))); 351 res 352 } 353 } 354 ) 355 ); 356 357 #[cfg(feature = "regexp_macros")] 358 /// `re_capture_static!(regexp) => &[T] -> IResult<&[T], Vec<&[T]>>` 359 /// Returns the first capture group. Regular expression calculated at compile time 360 /// 361 /// requires the `regexp_macros` feature 362 #[macro_export(local_inner_macros)] 363 macro_rules! re_capture_static ( 364 ($i:expr, $re:expr) => ( 365 { 366 use $crate::lib::std::result::Result::*; 367 use $crate::{Err,error::ErrorKind,IResult}; 368 369 use $crate::Slice; 370 regex!(RE, $re); 371 if let Some(c) = RE.captures(&$i) { 372 let v:Vec<_> = c.iter().filter(|el| el.is_some()).map(|el| el.unwrap()).map(|m| $i.slice(m.start()..m.end())).collect(); 373 let offset = { 374 let end = v.last().unwrap(); 375 end.as_ptr() as usize + end.len() - $i.as_ptr() as usize 376 }; 377 Ok(($i.slice(offset..), v)) 378 } else { 379 let res: IResult<_,_> = Err(Err::Error(error_position!($i, ErrorKind::RegexpCapture))); 380 res 381 } 382 } 383 ) 384 ); 385 386 /// `re_bytes_capture!(regexp) => &[T] -> IResult<&[T], Vec<&[T]>>` 387 /// Returns the first capture group 388 /// 389 /// requires the `regexp` feature 390 #[macro_export(local_inner_macros)] 391 macro_rules! re_bytes_capture ( 392 ($i:expr, $re:expr) => ( 393 { 394 use $crate::lib::std::result::Result::*; 395 use $crate::{Err,error::ErrorKind,IResult}; 396 397 use $crate::Slice; 398 let re = $crate::lib::regex::bytes::Regex::new($re).unwrap(); 399 if let Some(c) = re.captures(&$i) { 400 let v:Vec<_> = c.iter().filter(|el| el.is_some()).map(|el| el.unwrap()).map(|m| $i.slice(m.start()..m.end())).collect(); 401 let offset = { 402 let end = v.last().unwrap(); 403 end.as_ptr() as usize + end.len() - $i.as_ptr() as usize 404 }; 405 Ok(($i.slice(offset..), v)) 406 } else { 407 let res: IResult<_,_> = Err(Err::Error(error_position!($i, ErrorKind::RegexpCapture))); 408 res 409 } 410 } 411 ) 412 ); 413 414 #[cfg(feature = "regexp_macros")] 415 /// `re_bytes_capture_static!(regexp) => &[T] -> IResult<&[T], Vec<&[T]>>` 416 /// Returns the first capture group. Regular expression calculated at compile time 417 /// 418 /// requires the `regexp_macros` feature 419 #[macro_export(local_inner_macros)] 420 macro_rules! re_bytes_capture_static ( 421 ($i:expr, $re:expr) => ( 422 { 423 use $crate::lib::std::result::Result::*; 424 use $crate::{Err,error::ErrorKind,IResult}; 425 426 use $crate::Slice; 427 regex_bytes!(RE, $re); 428 if let Some(c) = RE.captures(&$i) { 429 let v:Vec<_> = c.iter().filter(|el| el.is_some()).map(|el| el.unwrap()).map(|m| $i.slice(m.start()..m.end())).collect(); 430 let offset = { 431 let end = v.last().unwrap(); 432 end.as_ptr() as usize + end.len() - $i.as_ptr() as usize 433 }; 434 Ok(($i.slice(offset..), v)) 435 } else { 436 let res: IResult<_,_> = Err(Err::Error(error_position!($i, ErrorKind::RegexpCapture))); 437 res 438 } 439 } 440 ) 441 ); 442 443 /// `re_captures!(regexp) => &[T] -> IResult<&[T], Vec<Vec<&[T]>>>` 444 /// Returns all the capture groups 445 /// 446 /// requires the `regexp` feature 447 #[macro_export(local_inner_macros)] 448 macro_rules! re_captures ( 449 ($i:expr, $re:expr) => ( 450 { 451 use $crate::lib::std::result::Result::*; 452 use $crate::{Err,error::ErrorKind,IResult}; 453 454 use $crate::Slice; 455 let re = $crate::lib::regex::Regex::new($re).unwrap(); 456 let v:Vec<Vec<_>> = re.captures_iter(&$i) 457 .map(|c| c.iter().filter(|el| el.is_some()).map(|el| el.unwrap()) 458 .map(|m| $i.slice(m.start()..m.end())).collect()).collect(); 459 if v.len() != 0 { 460 let offset = { 461 let end = v.last().unwrap().last().unwrap(); 462 end.as_ptr() as usize + end.len() - $i.as_ptr() as usize 463 }; 464 Ok(($i.slice(offset..), v)) 465 } else { 466 let res: IResult<_,_> = Err(Err::Error(error_position!($i, ErrorKind::RegexpCapture))); 467 res 468 } 469 } 470 ) 471 ); 472 473 #[cfg(feature = "regexp_macros")] 474 /// `re_captures_static!(regexp) => &[T] -> IResult<&[T], Vec<Vec<&[T]>>>` 475 /// Returns all the capture groups. Regular expression calculated at compile time 476 /// 477 /// requires the `regexp_macros` feature 478 #[macro_export(local_inner_macros)] 479 macro_rules! re_captures_static ( 480 ($i:expr, $re:expr) => ( 481 { 482 use $crate::lib::std::result::Result::*; 483 use $crate::{Err,error::ErrorKind,IResult}; 484 485 use $crate::Slice; 486 regex!(RE, $re); 487 let v:Vec<Vec<_>> = RE.captures_iter(&$i) 488 .map(|c| c.iter().filter(|el| el.is_some()).map(|el| el.unwrap()).map(|m| $i.slice(m.start()..m.end())).collect()).collect(); 489 if v.len() != 0 { 490 let offset = { 491 let end = v.last().unwrap().last().unwrap(); 492 end.as_ptr() as usize + end.len() - $i.as_ptr() as usize 493 }; 494 Ok(($i.slice(offset..), v)) 495 } else { 496 let res: IResult<_,_> = Err(Err::Error(error_position!($i, ErrorKind::RegexpCapture))); 497 res 498 } 499 } 500 ) 501 ); 502 503 /// `re_bytes_captures!(regexp) => &[T] -> IResult<&[T], Vec<Vec<&[T]>>>` 504 /// Returns all the capture groups 505 /// 506 /// requires the `regexp` feature 507 #[macro_export(local_inner_macros)] 508 macro_rules! re_bytes_captures ( 509 ($i:expr, $re:expr) => ( 510 { 511 use $crate::lib::std::result::Result::*; 512 use $crate::{Err,error::ErrorKind,IResult}; 513 514 use $crate::Slice; 515 let re = $crate::lib::regex::bytes::Regex::new($re).unwrap(); 516 let v:Vec<Vec<_>> = re.captures_iter(&$i) 517 .map(|c| c.iter().filter(|el| el.is_some()).map(|el| el.unwrap()).map(|m| $i.slice(m.start()..m.end())).collect()).collect(); 518 if v.len() != 0 { 519 let offset = { 520 let end = v.last().unwrap().last().unwrap(); 521 end.as_ptr() as usize + end.len() - $i.as_ptr() as usize 522 }; 523 Ok(($i.slice(offset..), v)) 524 } else { 525 let res: IResult<_,_> = Err(Err::Error(error_position!($i, ErrorKind::RegexpCapture))); 526 res 527 } 528 } 529 ) 530 ); 531 532 #[cfg(feature = "regexp_macros")] 533 /// `re_bytes_captures_static!(regexp) => &[T] -> IResult<&[T], Vec<Vec<&[T]>>>` 534 /// Returns all the capture groups. Regular expression calculated at compile time 535 /// 536 /// requires the `regexp_macros` feature 537 #[macro_export(local_inner_macros)] 538 macro_rules! re_bytes_captures_static ( 539 ($i:expr, $re:expr) => ( 540 { 541 use $crate::lib::std::result::Result::*; 542 use $crate::{Err,error::ErrorKind,IResult}; 543 544 use $crate::Slice; 545 regex_bytes!(RE, $re); 546 let v:Vec<Vec<_>> = RE.captures_iter(&$i) 547 .map(|c| c.iter().filter(|el| el.is_some()).map(|el| el.unwrap()).map(|m| $i.slice(m.start()..m.end())).collect()).collect(); 548 if v.len() != 0 { 549 let offset = { 550 let end = v.last().unwrap().last().unwrap(); 551 end.as_ptr() as usize + end.len() - $i.as_ptr() as usize 552 }; 553 Ok(($i.slice(offset..), v)) 554 } else { 555 let res: IResult<_,_> = Err(Err::Error(error_position!($i, ErrorKind::RegexpCapture))); 556 res 557 } 558 } 559 ) 560 ); 561 #[cfg(test)] 562 mod tests { 563 #[cfg(feature = "alloc")] 564 use crate::lib::std::vec::Vec; 565 use crate::error::ErrorKind; 566 use crate::internal::Err; 567 568 #[test] re_match()569 fn re_match() { 570 named!(rm<&str,&str>, re_match!(r"^\d{4}-\d{2}-\d{2}")); 571 assert_eq!(rm("2015-09-07"), Ok(("", "2015-09-07"))); 572 assert_eq!( 573 rm("blah"), 574 Err(Err::Error(error_position!( 575 &"blah"[..], 576 ErrorKind::RegexpMatch 577 ),)) 578 ); 579 assert_eq!(rm("2015-09-07blah"), Ok(("", "2015-09-07blah"))); 580 } 581 582 #[cfg(feature = "regexp_macros")] 583 #[test] re_match_static()584 fn re_match_static() { 585 named!(rm<&str,&str>, re_match_static!(r"^\d{4}-\d{2}-\d{2}")); 586 assert_eq!(rm("2015-09-07"), Ok(("", "2015-09-07"))); 587 assert_eq!( 588 rm("blah"), 589 Err(Err::Error(error_position!( 590 &"blah"[..], 591 ErrorKind::RegexpMatch 592 ),)) 593 ); 594 assert_eq!(rm("2015-09-07blah"), Ok(("", "2015-09-07blah"))); 595 } 596 597 #[test] re_find()598 fn re_find() { 599 named!(rm<&str,&str>, re_find!(r"^\d{4}-\d{2}-\d{2}")); 600 assert_eq!(rm("2015-09-07"), Ok(("", "2015-09-07"))); 601 assert_eq!( 602 rm("blah"), 603 Err(Err::Error(error_position!( 604 &"blah"[..], 605 ErrorKind::RegexpFind 606 ),)) 607 ); 608 assert_eq!(rm("2015-09-07blah"), Ok(("blah", "2015-09-07"))); 609 } 610 611 #[cfg(feature = "regexp_macros")] 612 #[test] re_find_static()613 fn re_find_static() { 614 named!(rm<&str,&str>, re_find_static!(r"^\d{4}-\d{2}-\d{2}")); 615 assert_eq!(rm("2015-09-07"), Ok(("", "2015-09-07"))); 616 assert_eq!( 617 rm("blah"), 618 Err(Err::Error(error_position!( 619 &"blah"[..], 620 ErrorKind::RegexpFind 621 ),)) 622 ); 623 assert_eq!(rm("2015-09-07blah"), Ok(("blah", "2015-09-07"))); 624 } 625 626 #[cfg(feature = "alloc")] 627 #[test] re_matches()628 fn re_matches() { 629 named!(rm< &str,Vec<&str> >, re_matches!(r"\d{4}-\d{2}-\d{2}")); 630 assert_eq!(rm("2015-09-07"), Ok(("", vec!["2015-09-07"]))); 631 assert_eq!( 632 rm("blah"), 633 Err(Err::Error(error_position!( 634 &"blah"[..], 635 ErrorKind::RegexpMatches 636 ))) 637 ); 638 assert_eq!( 639 rm("aaa2015-09-07blah2015-09-09pouet"), 640 Ok(("pouet", vec!["2015-09-07", "2015-09-09"])) 641 ); 642 } 643 644 #[cfg(feature = "regexp_macros")] 645 #[cfg(feature = "alloc")] 646 #[test] re_matches_static()647 fn re_matches_static() { 648 named!(rm< &str,Vec<&str> >, re_matches_static!(r"\d{4}-\d{2}-\d{2}")); 649 assert_eq!(rm("2015-09-07"), Ok(("", vec!["2015-09-07"]))); 650 assert_eq!( 651 rm("blah"), 652 Err(Err::Error(error_position!( 653 &"blah"[..], 654 ErrorKind::RegexpMatches 655 ))) 656 ); 657 assert_eq!( 658 rm("aaa2015-09-07blah2015-09-09pouet"), 659 Ok(("pouet", vec!["2015-09-07", "2015-09-09"])) 660 ); 661 } 662 663 #[cfg(feature = "alloc")] 664 #[test] re_capture()665 fn re_capture() { 666 named!(rm< &str,Vec<&str> >, re_capture!(r"([[:alpha:]]+)\s+((\d+).(\d+).(\d+))")); 667 assert_eq!( 668 rm("blah nom 0.3.11pouet"), 669 Ok(("pouet", vec!["nom 0.3.11", "nom", "0.3.11", "0", "3", "11"])) 670 ); 671 assert_eq!( 672 rm("blah"), 673 Err(Err::Error(error_position!( 674 &"blah"[..], 675 ErrorKind::RegexpCapture 676 ))) 677 ); 678 assert_eq!( 679 rm("hello nom 0.3.11 world regex 0.1.41"), 680 Ok(( 681 " world regex 0.1.41", 682 vec!["nom 0.3.11", "nom", "0.3.11", "0", "3", "11"] 683 )) 684 ); 685 } 686 687 #[cfg(feature = "alloc")] 688 #[cfg(feature = "regexp_macros")] 689 #[test] re_capture_static()690 fn re_capture_static() { 691 named!(rm< &str,Vec<&str> >, re_capture_static!(r"([[:alpha:]]+)\s+((\d+).(\d+).(\d+))")); 692 assert_eq!( 693 rm("blah nom 0.3.11pouet"), 694 Ok(("pouet", vec!["nom 0.3.11", "nom", "0.3.11", "0", "3", "11"])) 695 ); 696 assert_eq!( 697 rm("blah"), 698 Err(Err::Error(error_position!( 699 &"blah"[..], 700 ErrorKind::RegexpCapture 701 ))) 702 ); 703 assert_eq!( 704 rm("hello nom 0.3.11 world regex 0.1.41"), 705 Ok(( 706 " world regex 0.1.41", 707 vec!["nom 0.3.11", "nom", "0.3.11", "0", "3", "11"] 708 )) 709 ); 710 } 711 712 #[cfg(feature = "alloc")] 713 #[test] re_captures()714 fn re_captures() { 715 named!(rm< &str,Vec<Vec<&str>> >, re_captures!(r"([[:alpha:]]+)\s+((\d+).(\d+).(\d+))")); 716 assert_eq!( 717 rm("blah nom 0.3.11pouet"), 718 Ok(( 719 "pouet", 720 vec![vec!["nom 0.3.11", "nom", "0.3.11", "0", "3", "11"]] 721 )) 722 ); 723 assert_eq!( 724 rm("blah"), 725 Err(Err::Error(error_position!( 726 &"blah"[..], 727 ErrorKind::RegexpCapture 728 ))) 729 ); 730 assert_eq!( 731 rm("hello nom 0.3.11 world regex 0.1.41 aaa"), 732 Ok(( 733 " aaa", 734 vec![ 735 vec!["nom 0.3.11", "nom", "0.3.11", "0", "3", "11"], 736 vec!["regex 0.1.41", "regex", "0.1.41", "0", "1", "41"], 737 ] 738 )) 739 ); 740 } 741 742 #[cfg(feature = "alloc")] 743 #[cfg(feature = "regexp_macros")] 744 #[test] re_captures_static()745 fn re_captures_static() { 746 named!(rm< &str,Vec<Vec<&str>> >, re_captures_static!(r"([[:alpha:]]+)\s+((\d+).(\d+).(\d+))")); 747 assert_eq!( 748 rm("blah nom 0.3.11pouet"), 749 Ok(( 750 "pouet", 751 vec![vec!["nom 0.3.11", "nom", "0.3.11", "0", "3", "11"]] 752 )) 753 ); 754 assert_eq!( 755 rm("blah"), 756 Err(Err::Error(error_position!( 757 &"blah"[..], 758 ErrorKind::RegexpCapture 759 ))) 760 ); 761 assert_eq!( 762 rm("hello nom 0.3.11 world regex 0.1.41 aaa"), 763 Ok(( 764 " aaa", 765 vec![ 766 vec!["nom 0.3.11", "nom", "0.3.11", "0", "3", "11"], 767 vec!["regex 0.1.41", "regex", "0.1.41", "0", "1", "41"], 768 ] 769 )) 770 ); 771 } 772 773 #[test] re_bytes_match()774 fn re_bytes_match() { 775 named!(rm, re_bytes_match!(r"^\d{4}-\d{2}-\d{2}")); 776 assert_eq!(rm(&b"2015-09-07"[..]), Ok((&b""[..], &b"2015-09-07"[..]))); 777 assert_eq!( 778 rm(&b"blah"[..]), 779 Err(Err::Error(error_position!( 780 &b"blah"[..], 781 ErrorKind::RegexpMatch 782 ))) 783 ); 784 assert_eq!( 785 rm(&b"2015-09-07blah"[..]), 786 Ok((&b""[..], &b"2015-09-07blah"[..])) 787 ); 788 } 789 790 #[cfg(feature = "regexp_macros")] 791 #[test] re_bytes_match_static()792 fn re_bytes_match_static() { 793 named!(rm, re_bytes_match_static!(r"^\d{4}-\d{2}-\d{2}")); 794 assert_eq!(rm(&b"2015-09-07"[..]), Ok((&b""[..], &b"2015-09-07"[..]))); 795 assert_eq!( 796 rm(&b"blah"[..]), 797 Err(Err::Error(error_position!( 798 &b"blah"[..], 799 ErrorKind::RegexpMatch 800 ))) 801 ); 802 assert_eq!( 803 rm(&b"2015-09-07blah"[..]), 804 Ok((&b""[..], &b"2015-09-07blah"[..])) 805 ); 806 } 807 808 #[test] re_bytes_find()809 fn re_bytes_find() { 810 named!(rm, re_bytes_find!(r"^\d{4}-\d{2}-\d{2}")); 811 assert_eq!(rm(&b"2015-09-07"[..]), Ok((&b""[..], &b"2015-09-07"[..]))); 812 assert_eq!( 813 rm(&b"blah"[..]), 814 Err(Err::Error(error_position!( 815 &b"blah"[..], 816 ErrorKind::RegexpFind 817 ))) 818 ); 819 assert_eq!( 820 rm(&b"2015-09-07blah"[..]), 821 Ok((&b"blah"[..], &b"2015-09-07"[..])) 822 ); 823 } 824 825 #[cfg(feature = "regexp_macros")] 826 #[test] re_bytes_find_static()827 fn re_bytes_find_static() { 828 named!(rm, re_bytes_find_static!(r"^\d{4}-\d{2}-\d{2}")); 829 assert_eq!(rm(&b"2015-09-07"[..]), Ok((&b""[..], &b"2015-09-07"[..]))); 830 assert_eq!( 831 rm(&b"blah"[..]), 832 Err(Err::Error(error_position!( 833 &b"blah"[..], 834 ErrorKind::RegexpFind 835 ))) 836 ); 837 assert_eq!( 838 rm(&b"2015-09-07blah"[..]), 839 Ok((&b"blah"[..], &b"2015-09-07"[..])) 840 ); 841 } 842 843 #[cfg(feature = "alloc")] 844 #[test] re_bytes_matches()845 fn re_bytes_matches() { 846 named!(rm<Vec<&[u8]>>, re_bytes_matches!(r"\d{4}-\d{2}-\d{2}")); 847 assert_eq!( 848 rm(&b"2015-09-07"[..]), 849 Ok((&b""[..], vec![&b"2015-09-07"[..]])) 850 ); 851 assert_eq!( 852 rm(&b"blah"[..]), 853 Err(Err::Error(error_position!( 854 &b"blah"[..], 855 ErrorKind::RegexpMatches 856 ))) 857 ); 858 assert_eq!( 859 rm(&b"aaa2015-09-07blah2015-09-09pouet"[..]), 860 Ok((&b"pouet"[..], vec![&b"2015-09-07"[..], &b"2015-09-09"[..]])) 861 ); 862 } 863 864 #[cfg(feature = "alloc")] 865 #[cfg(feature = "regexp_macros")] 866 #[test] re_bytes_matches_static()867 fn re_bytes_matches_static() { 868 named!( 869 rm<Vec<&[u8]>>, 870 re_bytes_matches_static!(r"\d{4}-\d{2}-\d{2}") 871 ); 872 assert_eq!( 873 rm(&b"2015-09-07"[..]), 874 Ok((&b""[..], vec![&b"2015-09-07"[..]])) 875 ); 876 assert_eq!( 877 rm(&b"blah"[..]), 878 Err(Err::Error(error_position!( 879 &b"blah"[..], 880 ErrorKind::RegexpMatches 881 ))) 882 ); 883 assert_eq!( 884 rm(&b"aaa2015-09-07blah2015-09-09pouet"[..]), 885 Ok((&b"pouet"[..], vec![&b"2015-09-07"[..], &b"2015-09-09"[..]])) 886 ); 887 } 888 889 #[cfg(feature = "alloc")] 890 #[test] re_bytes_capture()891 fn re_bytes_capture() { 892 named!( 893 rm<Vec<&[u8]>>, 894 re_bytes_capture!(r"([[:alpha:]]+)\s+((\d+).(\d+).(\d+))") 895 ); 896 assert_eq!( 897 rm(&b"blah nom 0.3.11pouet"[..]), 898 Ok(( 899 &b"pouet"[..], 900 vec![ 901 &b"nom 0.3.11"[..], 902 &b"nom"[..], 903 &b"0.3.11"[..], 904 &b"0"[..], 905 &b"3"[..], 906 &b"11"[..], 907 ] 908 )) 909 ); 910 assert_eq!( 911 rm(&b"blah"[..]), 912 Err(Err::Error(error_position!( 913 &b"blah"[..], 914 ErrorKind::RegexpCapture 915 ))) 916 ); 917 assert_eq!( 918 rm(&b"hello nom 0.3.11 world regex 0.1.41"[..]), 919 Ok(( 920 &b" world regex 0.1.41"[..], 921 vec![ 922 &b"nom 0.3.11"[..], 923 &b"nom"[..], 924 &b"0.3.11"[..], 925 &b"0"[..], 926 &b"3"[..], 927 &b"11"[..], 928 ] 929 )) 930 ); 931 } 932 933 #[cfg(feature = "alloc")] 934 #[cfg(feature = "regexp_macros")] 935 #[test] re_bytes_capture_static()936 fn re_bytes_capture_static() { 937 named!( 938 rm<Vec<&[u8]>>, 939 re_bytes_capture_static!(r"([[:alpha:]]+)\s+((\d+).(\d+).(\d+))") 940 ); 941 assert_eq!( 942 rm(&b"blah nom 0.3.11pouet"[..]), 943 Ok(( 944 &b"pouet"[..], 945 vec![ 946 &b"nom 0.3.11"[..], 947 &b"nom"[..], 948 &b"0.3.11"[..], 949 &b"0"[..], 950 &b"3"[..], 951 &b"11"[..], 952 ] 953 )) 954 ); 955 assert_eq!( 956 rm(&b"blah"[..]), 957 Err(Err::Error(error_position!( 958 &b"blah"[..], 959 ErrorKind::RegexpCapture 960 ))) 961 ); 962 assert_eq!( 963 rm(&b"hello nom 0.3.11 world regex 0.1.41"[..]), 964 Ok(( 965 &b" world regex 0.1.41"[..], 966 vec![ 967 &b"nom 0.3.11"[..], 968 &b"nom"[..], 969 &b"0.3.11"[..], 970 &b"0"[..], 971 &b"3"[..], 972 &b"11"[..], 973 ] 974 )) 975 ); 976 } 977 978 #[cfg(feature = "alloc")] 979 #[test] re_bytes_captures()980 fn re_bytes_captures() { 981 named!( 982 rm<Vec<Vec<&[u8]>>>, 983 re_bytes_captures!(r"([[:alpha:]]+)\s+((\d+).(\d+).(\d+))") 984 ); 985 assert_eq!( 986 rm(&b"blah nom 0.3.11pouet"[..]), 987 Ok(( 988 &b"pouet"[..], 989 vec![ 990 vec![ 991 &b"nom 0.3.11"[..], 992 &b"nom"[..], 993 &b"0.3.11"[..], 994 &b"0"[..], 995 &b"3"[..], 996 &b"11"[..], 997 ], 998 ] 999 )) 1000 ); 1001 assert_eq!( 1002 rm(&b"blah"[..]), 1003 Err(Err::Error(error_position!( 1004 &b"blah"[..], 1005 ErrorKind::RegexpCapture 1006 ))) 1007 ); 1008 assert_eq!( 1009 rm(&b"hello nom 0.3.11 world regex 0.1.41 aaa"[..]), 1010 Ok(( 1011 &b" aaa"[..], 1012 vec![ 1013 vec![ 1014 &b"nom 0.3.11"[..], 1015 &b"nom"[..], 1016 &b"0.3.11"[..], 1017 &b"0"[..], 1018 &b"3"[..], 1019 &b"11"[..], 1020 ], 1021 vec![ 1022 &b"regex 0.1.41"[..], 1023 &b"regex"[..], 1024 &b"0.1.41"[..], 1025 &b"0"[..], 1026 &b"1"[..], 1027 &b"41"[..], 1028 ], 1029 ] 1030 )) 1031 ); 1032 } 1033 1034 #[cfg(feature = "alloc")] 1035 #[cfg(feature = "regexp_macros")] 1036 #[test] re_bytes_captures_static()1037 fn re_bytes_captures_static() { 1038 named!( 1039 rm<Vec<Vec<&[u8]>>>, 1040 re_bytes_captures_static!(r"([[:alpha:]]+)\s+((\d+).(\d+).(\d+))") 1041 ); 1042 assert_eq!( 1043 rm(&b"blah nom 0.3.11pouet"[..]), 1044 Ok(( 1045 &b"pouet"[..], 1046 vec![ 1047 vec![ 1048 &b"nom 0.3.11"[..], 1049 &b"nom"[..], 1050 &b"0.3.11"[..], 1051 &b"0"[..], 1052 &b"3"[..], 1053 &b"11"[..], 1054 ], 1055 ] 1056 )) 1057 ); 1058 assert_eq!( 1059 rm(&b"blah"[..]), 1060 Err(Err::Error(error_position!( 1061 &b"blah"[..], 1062 ErrorKind::RegexpCapture 1063 ))) 1064 ); 1065 assert_eq!( 1066 rm(&b"hello nom 0.3.11 world regex 0.1.41 aaa"[..]), 1067 Ok(( 1068 &b" aaa"[..], 1069 vec![ 1070 vec![ 1071 &b"nom 0.3.11"[..], 1072 &b"nom"[..], 1073 &b"0.3.11"[..], 1074 &b"0"[..], 1075 &b"3"[..], 1076 &b"11"[..], 1077 ], 1078 vec![ 1079 &b"regex 0.1.41"[..], 1080 &b"regex"[..], 1081 &b"0.1.41"[..], 1082 &b"0"[..], 1083 &b"1"[..], 1084 &b"41"[..], 1085 ], 1086 ] 1087 )) 1088 ); 1089 } 1090 } 1091