pulldown_cmark/scanners.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552
// Copyright 2015 Google Inc. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//! Scanners for fragments of CommonMark syntax
use std::char;
use crate::parse::HtmlScanGuard;
pub(crate) use crate::puncttable::{is_ascii_punctuation, is_punctuation};
use crate::strings::CowStr;
use crate::{entities, BlockQuoteKind, HeadingLevel};
use crate::{Alignment, LinkType};
use memchr::memchr;
// sorted for binary search
const HTML_TAGS: [&str; 62] = [
"address",
"article",
"aside",
"base",
"basefont",
"blockquote",
"body",
"caption",
"center",
"col",
"colgroup",
"dd",
"details",
"dialog",
"dir",
"div",
"dl",
"dt",
"fieldset",
"figcaption",
"figure",
"footer",
"form",
"frame",
"frameset",
"h1",
"h2",
"h3",
"h4",
"h5",
"h6",
"head",
"header",
"hr",
"html",
"iframe",
"legend",
"li",
"link",
"main",
"menu",
"menuitem",
"nav",
"noframes",
"ol",
"optgroup",
"option",
"p",
"param",
"search",
"section",
"summary",
"table",
"tbody",
"td",
"tfoot",
"th",
"thead",
"title",
"tr",
"track",
"ul",
];
/// Analysis of the beginning of a line, including indentation and container
/// markers.
#[derive(Clone)]
pub(crate) struct LineStart<'a> {
bytes: &'a [u8],
ix: usize,
// The index in `bytes` after the last tab we scanned; initially
// zero.
//
// Thus, there are no tab characters between `ix` and here, and for
// the purpose of defining block structure, this position can be
// considered to fall on a tab stop.
//
// This is only valid while scanning the initial portion of the
// line; methods that work with interior structure don't bother to
// update it.
tab_start: usize,
// In contexts where spaces help to define block structure, tabs
// behave as if they were replaced by spaces with a tab stop of 4
// characters.
//
// If we have scanned past a tab character but not consumed all
// the horizontal width it contributed, this is the number of
// spaces logically remaining, before the character at `ix`.
spaces_remaining: usize,
// no thematic breaks can occur before this offset.
// this prevents scanning over and over up to a certain point
min_hrule_offset: usize,
}
impl<'a> LineStart<'a> {
pub(crate) fn new(bytes: &[u8]) -> LineStart<'_> {
LineStart {
bytes,
tab_start: 0,
ix: 0,
spaces_remaining: 0,
min_hrule_offset: 0,
}
}
/// Try to scan a number of spaces.
///
/// Returns true if all spaces were consumed.
///
/// Note: consumes some spaces even if not successful.
pub(crate) fn scan_space(&mut self, n_space: usize) -> bool {
self.scan_space_inner(n_space) == 0
}
/// Scan a number of spaces up to a maximum.
///
/// Returns number of spaces scanned.
pub(crate) fn scan_space_upto(&mut self, n_space: usize) -> usize {
n_space - self.scan_space_inner(n_space)
}
/// Returns unused remainder of spaces.
fn scan_space_inner(&mut self, mut n_space: usize) -> usize {
// Consume any common prefix between the number of spaces we
// want and the number of unscanned tab-introduced spaces.
let n_from_remaining = self.spaces_remaining.min(n_space);
self.spaces_remaining -= n_from_remaining;
n_space -= n_from_remaining;
while n_space > 0 && self.ix < self.bytes.len() {
match self.bytes[self.ix] {
b' ' => {
self.ix += 1;
n_space -= 1;
}
b'\t' => {
let spaces = 4 - (self.ix - self.tab_start) % 4;
self.ix += 1;
self.tab_start = self.ix;
let n = spaces.min(n_space);
n_space -= n;
// Record the unscanned portion of the tab.
self.spaces_remaining = spaces - n;
}
_ => break,
}
}
n_space
}
/// Scan all available ASCII whitespace (not including eol).
pub(crate) fn scan_all_space(&mut self) {
self.spaces_remaining = 0;
self.ix += self.bytes[self.ix..]
.iter()
.take_while(|&&b| b == b' ' || b == b'\t')
.count();
}
/// Determine whether we're at end of line (includes end of file).
pub(crate) fn is_at_eol(&self) -> bool {
self.bytes
.get(self.ix)
.map(|&c| c == b'\r' || c == b'\n')
.unwrap_or(true)
}
fn scan_ch(&mut self, c: u8) -> bool {
if self.ix < self.bytes.len() && self.bytes[self.ix] == c {
self.ix += 1;
true
} else {
false
}
}
fn scan_case_insensitive(&mut self, tag: &[u8]) -> bool {
if self.bytes.len() - self.ix < tag.len() {
return false;
}
let prefix = &self.bytes[self.ix..self.ix + tag.len()];
let ok = prefix.eq_ignore_ascii_case(tag);
if ok {
self.ix += tag.len();
}
ok
}
pub(crate) fn scan_blockquote_tag(&mut self) -> Option<BlockQuoteKind> {
let saved_ix = self.ix;
let tag = if self.scan_ch(b'[') && self.scan_ch(b'!') {
let tag = if self.scan_case_insensitive(b"note") {
Some(BlockQuoteKind::Note)
} else if self.scan_case_insensitive(b"tip") {
Some(BlockQuoteKind::Tip)
} else if self.scan_case_insensitive(b"important") {
Some(BlockQuoteKind::Important)
} else if self.scan_case_insensitive(b"warning") {
Some(BlockQuoteKind::Warning)
} else if self.scan_case_insensitive(b"caution") {
Some(BlockQuoteKind::Caution)
} else {
None
};
if tag.is_some() && self.scan_ch(b']') {
if let Some(nl) = scan_blank_line(&self.bytes[self.ix..]) {
self.ix += nl;
tag
} else {
None
}
} else {
None
}
} else {
None
};
if tag.is_none() {
self.ix = saved_ix;
}
tag
}
pub(crate) fn scan_blockquote_marker(&mut self) -> bool {
if self.scan_ch(b'>') {
let _ = self.scan_space(1);
true
} else {
false
}
}
/// Scan a definition marker.
///
/// Definition markers are single colons, preceded by at most three spaces
/// and followed by at most three spaces. The indentation of following
/// lines is equal to the whole size of the marker, including the colon.
///
/// If one is found, it will make the preceding paragraph into a definition
/// list title.
///
/// Return value is the amount of indentation, or `None` if it's not a
/// definition list marker.
pub(crate) fn scan_definition_list_definition_marker_with_indent(
&mut self,
indent: usize,
) -> Option<usize> {
let save = self.clone();
if self.scan_ch(b':') {
let remaining = 4 - (indent + 1);
Some(indent + 1 + self.scan_space_upto(remaining))
} else {
*self = save;
None
}
}
/// Scan a list marker.
///
/// Return value is the character, the start index, and the indent in spaces.
/// For ordered list markers, the character will be one of b'.' or b')'. For
/// bullet list markers, it will be one of b'-', b'+', or b'*'.
pub(crate) fn scan_list_marker_with_indent(
&mut self,
indent: usize,
) -> Option<(u8, u64, usize)> {
let save = self.clone();
if self.ix < self.bytes.len() {
let c = self.bytes[self.ix];
if c == b'-' || c == b'+' || c == b'*' {
if self.ix >= self.min_hrule_offset {
// there could be an hrule here
if let Err(min_offset) = scan_hrule(&self.bytes[self.ix..]) {
self.min_hrule_offset = min_offset;
} else {
*self = save;
return None;
}
}
self.ix += 1;
if self.scan_space(1) || self.is_at_eol() {
return self.finish_list_marker(c, 0, indent + 2);
}
} else if c.is_ascii_digit() {
let start_ix = self.ix;
let mut ix = self.ix + 1;
let mut val = u64::from(c - b'0');
while ix < self.bytes.len() && ix - start_ix < 10 {
let c = self.bytes[ix];
ix += 1;
if c.is_ascii_digit() {
val = val * 10 + u64::from(c - b'0');
} else if c == b')' || c == b'.' {
self.ix = ix;
if self.scan_space(1) || self.is_at_eol() {
return self.finish_list_marker(c, val, indent + 1 + ix - start_ix);
} else {
break;
}
} else {
break;
}
}
}
}
*self = save;
None
}
fn finish_list_marker(
&mut self,
c: u8,
start: u64,
mut indent: usize,
) -> Option<(u8, u64, usize)> {
let save = self.clone();
// skip the rest of the line if it's blank
if scan_blank_line(&self.bytes[self.ix..]).is_some() {
return Some((c, start, indent));
}
let post_indent = self.scan_space_upto(4);
if post_indent < 4 {
indent += post_indent;
} else {
*self = save;
}
Some((c, start, indent))
}
/// Returns Some(is_checked) when a task list marker was found. Resets itself
/// to original state otherwise.
pub(crate) fn scan_task_list_marker(&mut self) -> Option<bool> {
let save = self.clone();
self.scan_space_upto(3);
if !self.scan_ch(b'[') {
*self = save;
return None;
}
let is_checked = match self.bytes.get(self.ix) {
Some(&c) if is_ascii_whitespace_no_nl(c) => {
self.ix += 1;
false
}
Some(b'x') | Some(b'X') => {
self.ix += 1;
true
}
_ => {
*self = save;
return None;
}
};
if !self.scan_ch(b']') {
*self = save;
return None;
}
if !self
.bytes
.get(self.ix)
.map(|&b| is_ascii_whitespace_no_nl(b))
.unwrap_or(false)
{
*self = save;
return None;
}
Some(is_checked)
}
pub(crate) fn bytes_scanned(&self) -> usize {
self.ix
}
pub(crate) fn remaining_space(&self) -> usize {
self.spaces_remaining
}
}
pub(crate) fn is_ascii_whitespace(c: u8) -> bool {
(0x09..=0x0d).contains(&c) || c == b' '
}
pub(crate) fn is_ascii_whitespace_no_nl(c: u8) -> bool {
c == b'\t' || c == 0x0b || c == 0x0c || c == b' '
}
fn is_ascii_alpha(c: u8) -> bool {
c.is_ascii_alphabetic()
}
fn is_ascii_alphanumeric(c: u8) -> bool {
matches!(c, b'0'..=b'9' | b'a'..=b'z' | b'A'..=b'Z')
}
fn is_ascii_letterdigitdash(c: u8) -> bool {
c == b'-' || is_ascii_alphanumeric(c)
}
fn is_digit(c: u8) -> bool {
c.is_ascii_digit()
}
fn is_valid_unquoted_attr_value_char(c: u8) -> bool {
!matches!(
c,
b'\'' | b'"' | b' ' | b'=' | b'>' | b'<' | b'`' | b'\n' | b'\r'
)
}
// scan a single character
pub(crate) fn scan_ch(data: &[u8], c: u8) -> usize {
if !data.is_empty() && data[0] == c {
1
} else {
0
}
}
pub(crate) fn scan_while<F>(data: &[u8], mut f: F) -> usize
where
F: FnMut(u8) -> bool,
{
data.iter().take_while(|&&c| f(c)).count()
}
pub(crate) fn scan_rev_while<F>(data: &[u8], mut f: F) -> usize
where
F: FnMut(u8) -> bool,
{
data.iter().rev().take_while(|&&c| f(c)).count()
}
pub(crate) fn scan_ch_repeat(data: &[u8], c: u8) -> usize {
scan_while(data, |x| x == c)
}
// Note: this scans ASCII whitespace only, for Unicode whitespace use
// a different function.
pub(crate) fn scan_whitespace_no_nl(data: &[u8]) -> usize {
scan_while(data, is_ascii_whitespace_no_nl)
}
fn scan_attr_value_chars(data: &[u8]) -> usize {
scan_while(data, is_valid_unquoted_attr_value_char)
}
pub(crate) fn scan_eol(bytes: &[u8]) -> Option<usize> {
if bytes.is_empty() {
return Some(0);
}
match bytes[0] {
b'\n' => Some(1),
b'\r' => Some(if bytes.get(1) == Some(&b'\n') { 2 } else { 1 }),
_ => None,
}
}
pub(crate) fn scan_blank_line(bytes: &[u8]) -> Option<usize> {
let i = scan_whitespace_no_nl(bytes);
scan_eol(&bytes[i..]).map(|n| i + n)
}
pub(crate) fn scan_nextline(bytes: &[u8]) -> usize {
memchr(b'\n', bytes).map_or(bytes.len(), |x| x + 1)
}
// return: end byte for closing code fence, or None
// if the line is not a closing code fence
pub(crate) fn scan_closing_code_fence(
bytes: &[u8],
fence_char: u8,
n_fence_char: usize,
) -> Option<usize> {
if bytes.is_empty() {
return Some(0);
}
let mut i = 0;
let num_fence_chars_found = scan_ch_repeat(&bytes[i..], fence_char);
if num_fence_chars_found < n_fence_char {
return None;
}
i += num_fence_chars_found;
let num_trailing_spaces = scan_ch_repeat(&bytes[i..], b' ');
i += num_trailing_spaces;
scan_eol(&bytes[i..]).map(|_| i)
}
// return: end byte for closing metadata block, or None
// if the line is not a closing metadata block
pub(crate) fn scan_closing_metadata_block(bytes: &[u8], fence_char: u8) -> Option<usize> {
let mut i = 0;
let mut num_fence_chars_found = scan_ch_repeat(&bytes[i..], fence_char);
if num_fence_chars_found != 3 {
// if YAML style metadata block the closing character can also be `.`
if fence_char == b'-' {
num_fence_chars_found = scan_ch_repeat(&bytes[i..], b'.');
if num_fence_chars_found != 3 {
return None;
}
} else {
return None;
}
}
i += num_fence_chars_found;
let num_trailing_spaces = scan_ch_repeat(&bytes[i..], b' ');
i += num_trailing_spaces;
scan_eol(&bytes[i..]).map(|_| i)
}
// returned pair is (number of bytes, number of spaces)
pub(crate) fn calc_indent(text: &[u8], max: usize) -> (usize, usize) {
let mut spaces = 0;
let mut offset = 0;
for (i, &b) in text.iter().enumerate() {
offset = i;
match b {
b' ' => {
spaces += 1;
if spaces == max {
break;
}
}
b'\t' => {
let new_spaces = spaces + 4 - (spaces & 3);
if new_spaces > max {
break;
}
spaces = new_spaces;
}
_ => break,
}
}
(offset, spaces)
}
/// Scan hrule opening sequence.
///
/// Returns Ok(x) when it finds an hrule, where x is the
/// size of line containing the hrule, including the trailing newline.
///
/// Returns Err(x) when it does not find an hrule and x is
/// the offset in data before no hrule can appear.
pub(crate) fn scan_hrule(bytes: &[u8]) -> Result<usize, usize> {
if bytes.len() < 3 {
return Err(0);
}
let c = bytes[0];
if !(c == b'*' || c == b'-' || c == b'_') {
return Err(0);
}
let mut n = 0;
let mut i = 0;
while i < bytes.len() {
match bytes[i] {
b'\n' | b'\r' => {
i += scan_eol(&bytes[i..]).unwrap_or(0);
break;
}
c2 if c2 == c => {
n += 1;
}
b' ' | b'\t' => (),
_ => return Err(i),
}
i += 1;
}
if n >= 3 {
Ok(i)
} else {
Err(i)
}
}
/// Scan an ATX heading opening sequence.
///
/// Returns number of bytes in prefix and level.
pub(crate) fn scan_atx_heading(data: &[u8]) -> Option<HeadingLevel> {
let level = scan_ch_repeat(data, b'#');
if data.get(level).copied().map_or(true, is_ascii_whitespace) {
HeadingLevel::try_from(level).ok()
} else {
None
}
}
/// Scan a setext heading underline.
///
/// Returns number of bytes in line (including trailing newline) and level.
pub(crate) fn scan_setext_heading(data: &[u8]) -> Option<(usize, HeadingLevel)> {
let c = *data.first()?;
let level = if c == b'=' {
HeadingLevel::H1
} else if c == b'-' {
HeadingLevel::H2
} else {
return None;
};
let mut i = 1 + scan_ch_repeat(&data[1..], c);
i += scan_blank_line(&data[i..])?;
Some((i, level))
}
// returns number of bytes in line (including trailing
// newline) and column alignments
pub(crate) fn scan_table_head(data: &[u8]) -> (usize, Vec<Alignment>) {
let (mut i, spaces) = calc_indent(data, 4);
if spaces > 3 || i == data.len() {
return (0, vec![]);
}
let mut cols = vec![];
let mut active_col = Alignment::None;
let mut start_col = true;
let mut found_pipe = false;
let mut found_hyphen = false;
let mut found_hyphen_in_col = false;
if data[i] == b'|' {
i += 1;
found_pipe = true;
}
for c in &data[i..] {
if let Some(n) = scan_eol(&data[i..]) {
i += n;
break;
}
match *c {
b' ' => (),
b':' => {
active_col = match (start_col, active_col) {
(true, Alignment::None) => Alignment::Left,
(false, Alignment::Left) => Alignment::Center,
(false, Alignment::None) => Alignment::Right,
_ => active_col,
};
start_col = false;
}
b'-' => {
start_col = false;
found_hyphen = true;
found_hyphen_in_col = true;
}
b'|' => {
start_col = true;
found_pipe = true;
cols.push(active_col);
active_col = Alignment::None;
if !found_hyphen_in_col {
// It isn't a table head if it has back-to-back pipes.
return (0, vec![]);
}
found_hyphen_in_col = false;
}
_ => {
// It isn't a table head if it has characters outside the allowed set.
return (0, vec![]);
}
}
i += 1;
}
if !start_col {
cols.push(active_col);
}
if !found_pipe || !found_hyphen {
// It isn't a table head if it doesn't have a least one pipe or hyphen.
// It's a list, a header, or a thematic break.
return (0, vec![]);
}
(i, cols)
}
/// Scan code fence.
///
/// Returns number of bytes scanned and the char that is repeated to make the code fence.
pub(crate) fn scan_code_fence(data: &[u8]) -> Option<(usize, u8)> {
let c = *data.first()?;
if !(c == b'`' || c == b'~') {
return None;
}
let i = 1 + scan_ch_repeat(&data[1..], c);
if i >= 3 {
if c == b'`' {
let suffix = &data[i..];
let next_line = i + scan_nextline(suffix);
// FIXME: make sure this is correct
if suffix[..(next_line - i)].iter().any(|&b| b == b'`') {
return None;
}
}
Some((i, c))
} else {
None
}
}
/// Scan metadata block, returning the number of delimiter bytes
/// (always 3 for now) and the delimiter character.
///
/// Differently to code blocks, metadata blocks must be closed with the closing
/// sequence not being a valid terminator the end of the file.
///
/// In addition, they cannot be empty (closing sequence in the next line) and
/// the next line cannot be an empty line.
pub(crate) fn scan_metadata_block(
data: &[u8],
yaml_style_enabled: bool,
pluses_style_enabled: bool,
) -> Option<(usize, u8)> {
// Only if metadata blocks are enabled
if yaml_style_enabled || pluses_style_enabled {
let c = *data.first()?;
if !((c == b'-' && yaml_style_enabled) || (c == b'+' && pluses_style_enabled)) {
return None;
}
let i = 1 + scan_ch_repeat(&data[1..], c);
// Only trailing spaces after the delimiters in the line
let next_line = scan_nextline(&data[i..]);
for c in &data[i..i + next_line] {
if !c.is_ascii_whitespace() {
return None;
}
}
if i == 3 {
// Search the closing sequence
let mut j = i;
let mut first_line = true;
while j < data.len() {
j += scan_nextline(&data[j..]);
let closed = scan_closing_metadata_block(&data[j..], c).is_some();
// The first line of the metadata block cannot be an empty line
// nor the end of the block
if first_line {
if closed || scan_blank_line(&data[j..]).is_some() {
return None;
}
first_line = false;
}
if closed {
return Some((i, c));
}
}
None
} else {
None
}
} else {
None
}
}
pub(crate) fn scan_blockquote_start(data: &[u8]) -> Option<usize> {
if data.first().copied() == Some(b'>') {
let space = if data.get(1).copied() == Some(b' ') {
1
} else {
0
};
Some(1 + space)
} else {
None
}
}
/// return number of bytes scanned, delimiter, start index, and indent
pub(crate) fn scan_listitem(bytes: &[u8]) -> Option<(usize, u8, usize, usize)> {
let mut c = *bytes.first()?;
let (w, start) = match c {
b'-' | b'+' | b'*' => (1, 0),
b'0'..=b'9' => {
let (length, start) = parse_decimal(bytes, 9);
c = *bytes.get(length)?;
if !(c == b'.' || c == b')') {
return None;
}
(length + 1, start)
}
_ => {
return None;
}
};
// TODO: replace calc_indent with scan_leading_whitespace, for tab correctness
let (mut postn, mut postindent) = calc_indent(&bytes[w..], 5);
if postindent == 0 {
scan_eol(&bytes[w..])?;
postindent += 1;
} else if postindent > 4 {
postn = 1;
postindent = 1;
}
if scan_blank_line(&bytes[w..]).is_some() {
postn = 0;
postindent = 1;
}
Some((w + postn, c, start, w + postindent))
}
// returns (number of bytes, parsed decimal)
fn parse_decimal(bytes: &[u8], limit: usize) -> (usize, usize) {
match bytes
.iter()
.take(limit)
.take_while(|&&b| is_digit(b))
.try_fold((0, 0usize), |(count, acc), c| {
let digit = usize::from(c - b'0');
match acc
.checked_mul(10)
.and_then(|ten_acc| ten_acc.checked_add(digit))
{
Some(number) => Ok((count + 1, number)),
// stop early on overflow
None => Err((count, acc)),
}
}) {
Ok(p) | Err(p) => p,
}
}
// returns (number of bytes, parsed hex)
fn parse_hex(bytes: &[u8], limit: usize) -> (usize, usize) {
match bytes
.iter()
.take(limit)
.try_fold((0, 0usize), |(count, acc), c| {
let mut c = *c;
let digit = if c.is_ascii_digit() {
usize::from(c - b'0')
} else {
// make lower case
c |= 0x20;
if (b'a'..=b'f').contains(&c) {
usize::from(c - b'a' + 10)
} else {
return Err((count, acc));
}
};
match acc
.checked_mul(16)
.and_then(|sixteen_acc| sixteen_acc.checked_add(digit))
{
Some(number) => Ok((count + 1, number)),
// stop early on overflow
None => Err((count, acc)),
}
}) {
Ok(p) | Err(p) => p,
}
}
fn char_from_codepoint(input: usize) -> Option<char> {
let codepoint = input.try_into().ok()?;
if codepoint == 0 {
return None;
}
char::from_u32(codepoint)
}
// doesn't bother to check data[0] == '&'
pub(crate) fn scan_entity(bytes: &[u8]) -> (usize, Option<CowStr<'static>>) {
let mut end = 1;
if scan_ch(&bytes[end..], b'#') == 1 {
end += 1;
let (bytecount, codepoint) = if end < bytes.len() && bytes[end] | 0x20 == b'x' {
end += 1;
parse_hex(&bytes[end..], 6)
} else {
parse_decimal(&bytes[end..], 7)
};
end += bytecount;
return if bytecount == 0 || scan_ch(&bytes[end..], b';') == 0 {
(0, None)
} else {
(
end + 1,
Some(char_from_codepoint(codepoint).unwrap_or('\u{FFFD}').into()),
)
};
}
end += scan_while(&bytes[end..], is_ascii_alphanumeric);
if scan_ch(&bytes[end..], b';') == 1 {
if let Some(value) = entities::get_entity(&bytes[1..end]) {
return (end + 1, Some(value.into()));
}
}
(0, None)
}
// note: dest returned is raw, still needs to be unescaped
// TODO: check that nested parens are really not allowed for refdefs
// TODO(performance): this func should probably its own unescaping
pub(crate) fn scan_link_dest(
data: &str,
start_ix: usize,
max_next: usize,
) -> Option<(usize, &str)> {
let bytes = &data.as_bytes()[start_ix..];
let mut i = scan_ch(bytes, b'<');
if i != 0 {
// pointy links
while i < bytes.len() {
match bytes[i] {
b'\n' | b'\r' | b'<' => return None,
b'>' => return Some((i + 1, &data[(start_ix + 1)..(start_ix + i)])),
b'\\' if i + 1 < bytes.len() && is_ascii_punctuation(bytes[i + 1]) => {
i += 1;
}
_ => {}
}
i += 1;
}
None
} else {
// non-pointy links
let mut nest = 0;
while i < bytes.len() {
match bytes[i] {
0x0..=0x20 => {
break;
}
b'(' => {
if nest > max_next {
return None;
}
nest += 1;
}
b')' => {
if nest == 0 {
break;
}
nest -= 1;
}
b'\\' if i + 1 < bytes.len() && is_ascii_punctuation(bytes[i + 1]) => {
i += 1;
}
_ => {}
}
i += 1;
}
if nest != 0 {
return None;
}
Some((i, &data[start_ix..(start_ix + i)]))
}
}
/// Returns bytes scanned
fn scan_attribute_name(data: &[u8]) -> Option<usize> {
let (&c, tail) = data.split_first()?;
if is_ascii_alpha(c) || c == b'_' || c == b':' {
Some(
1 + scan_while(tail, |c| {
is_ascii_alphanumeric(c) || c == b'_' || c == b'.' || c == b':' || c == b'-'
}),
)
} else {
None
}
}
/// Returns the index immediately following the attribute on success.
/// The argument `buffer_ix` refers to the index into `data` from which we
/// should copy into `buffer` when we find bytes to skip.
fn scan_attribute(
data: &[u8],
mut ix: usize,
newline_handler: Option<&dyn Fn(&[u8]) -> usize>,
buffer: &mut Vec<u8>,
buffer_ix: &mut usize,
) -> Option<usize> {
ix += scan_attribute_name(&data[ix..])?;
let ix_after_attribute = ix;
ix = scan_whitespace_with_newline_handler_without_buffer(data, ix, newline_handler)?;
if scan_ch(&data[ix..], b'=') == 1 {
ix = scan_whitespace_with_newline_handler(
data,
ix_after_attribute,
newline_handler,
buffer,
buffer_ix,
)?;
ix += 1;
ix = scan_whitespace_with_newline_handler(data, ix, newline_handler, buffer, buffer_ix)?;
ix = scan_attribute_value(data, ix, newline_handler, buffer, buffer_ix)?;
Some(ix)
} else {
// Leave whitespace for next attribute.
Some(ix_after_attribute)
}
}
/// Scans whitespace and possibly newlines according to the
/// behavior defined by the newline handler. When bytes are skipped,
/// all preceding non-skipped bytes are pushed to the buffer.
fn scan_whitespace_with_newline_handler(
data: &[u8],
mut i: usize,
newline_handler: Option<&dyn Fn(&[u8]) -> usize>,
buffer: &mut Vec<u8>,
buffer_ix: &mut usize,
) -> Option<usize> {
while i < data.len() {
if !is_ascii_whitespace(data[i]) {
return Some(i);
}
if let Some(eol_bytes) = scan_eol(&data[i..]) {
let handler = newline_handler?;
i += eol_bytes;
let skipped_bytes = handler(&data[i..]);
if skipped_bytes > 0 {
buffer.extend(&data[*buffer_ix..i]);
*buffer_ix = i + skipped_bytes;
}
i += skipped_bytes;
} else {
i += 1;
}
}
Some(i)
}
/// Scans whitespace and possible newlines according to the behavior defined
/// by the newline handler.
///
/// Unlike [`scan_whitespace_with_newline_handler`], this function doesn't
/// copy skipped data into a buffer. Typically, if this function
/// returns `Some`, a call to `scan_whitespace_with_newline_handler` will
/// soon follow.
fn scan_whitespace_with_newline_handler_without_buffer(
data: &[u8],
mut i: usize,
newline_handler: Option<&dyn Fn(&[u8]) -> usize>,
) -> Option<usize> {
while i < data.len() {
if !is_ascii_whitespace(data[i]) {
return Some(i);
}
if let Some(eol_bytes) = scan_eol(&data[i..]) {
let handler = newline_handler?;
i += eol_bytes;
let skipped_bytes = handler(&data[i..]);
i += skipped_bytes;
} else {
i += 1;
}
}
Some(i)
}
/// Returns the index immediately following the attribute value on success.
fn scan_attribute_value(
data: &[u8],
mut i: usize,
newline_handler: Option<&dyn Fn(&[u8]) -> usize>,
buffer: &mut Vec<u8>,
buffer_ix: &mut usize,
) -> Option<usize> {
match *data.get(i)? {
b @ b'"' | b @ b'\'' => {
i += 1;
while i < data.len() {
if data[i] == b {
return Some(i + 1);
}
if let Some(eol_bytes) = scan_eol(&data[i..]) {
let handler = newline_handler?;
i += eol_bytes;
let skipped_bytes = handler(&data[i..]);
if skipped_bytes > 0 {
buffer.extend(&data[*buffer_ix..i]);
*buffer_ix = i + skipped_bytes;
}
i += skipped_bytes;
} else {
i += 1;
}
}
return None;
}
b' ' | b'=' | b'>' | b'<' | b'`' | b'\n' | b'\r' => {
return None;
}
_ => {
// unquoted attribute value
i += scan_attr_value_chars(&data[i..]);
}
}
Some(i)
}
// Remove backslash escapes and resolve entities
pub(crate) fn unescape<'a, I: Into<CowStr<'a>>>(input: I, is_in_table: bool) -> CowStr<'a> {
let input = input.into();
let mut result = String::new();
let mut mark = 0;
let mut i = 0;
let bytes = input.as_bytes();
while i < bytes.len() {
match bytes[i] {
// Tables are special, because they're parsed as-if the tables
// were parsed in a discrete pass, changing `\|` to `|`, and then
// passing the changed string to the inline parser.
b'\\'
if is_in_table
&& i + 2 < bytes.len()
&& bytes[i + 1] == b'\\'
&& bytes[i + 2] == b'|' =>
{
// even number of `\`s before pipe
// odd number is handled in the normal way below
result.push_str(&input[mark..i]);
mark = i + 2;
i += 3;
}
b'\\' if i + 1 < bytes.len() && is_ascii_punctuation(bytes[i + 1]) => {
result.push_str(&input[mark..i]);
mark = i + 1;
i += 2;
}
b'&' => match scan_entity(&bytes[i..]) {
(n, Some(value)) => {
result.push_str(&input[mark..i]);
result.push_str(&value);
i += n;
mark = i;
}
_ => i += 1,
},
b'\r' => {
result.push_str(&input[mark..i]);
i += 1;
mark = i;
}
_ => i += 1,
}
}
if mark == 0 {
input
} else {
result.push_str(&input[mark..]);
result.into()
}
}
/// Assumes `data` is preceded by `<`.
pub(crate) fn starts_html_block_type_6(data: &[u8]) -> bool {
let i = scan_ch(data, b'/');
let tail = &data[i..];
let n = scan_while(tail, is_ascii_alphanumeric);
if !is_html_tag(&tail[..n]) {
return false;
}
// Starting condition says the next byte must be either a space, a tab,
// the end of the line, the string >, or the string />
let tail = &tail[n..];
tail.is_empty()
|| tail[0] == b' '
|| tail[0] == b'\t'
|| tail[0] == b'\r'
|| tail[0] == b'\n'
|| tail[0] == b'>'
|| tail.len() >= 2 && &tail[..2] == b"/>"
}
fn is_html_tag(tag: &[u8]) -> bool {
HTML_TAGS
.binary_search_by(|probe| {
let probe_bytes_iter = probe.as_bytes().iter();
let tag_bytes_iter = tag.iter();
probe_bytes_iter
.zip(tag_bytes_iter)
.find_map(|(&a, &b)| {
// We can compare case insensitively because the probes are
// all lower case alpha strings.
match a.cmp(&(b | 0x20)) {
std::cmp::Ordering::Equal => None,
inequality => Some(inequality),
}
})
.unwrap_or_else(|| probe.len().cmp(&tag.len()))
})
.is_ok()
}
/// Assumes that `data` starts with `<`.
/// Returns the index into data directly after the html tag on success.
pub(crate) fn scan_html_type_7(data: &[u8]) -> Option<usize> {
// Block type html does not allow for newlines, so we
// do not pass a newline handler.
let (_span, i) = scan_html_block_inner(data, None)?;
scan_blank_line(&data[i..])?;
Some(i)
}
/// Assumes that `data` starts with `<`.
/// Returns the number of bytes scanned and the html in case of
/// success.
/// When some bytes were skipped, because the html was split over
/// multiple leafs (e.g. over multiple lines in a blockquote),
/// the html is returned as a vector of bytes.
/// If no bytes were skipped, the buffer will be empty.
pub(crate) fn scan_html_block_inner(
data: &[u8],
newline_handler: Option<&dyn Fn(&[u8]) -> usize>,
) -> Option<(Vec<u8>, usize)> {
let mut buffer = Vec::new();
let mut last_buf_index = 0;
let close_tag_bytes = scan_ch(&data[1..], b'/');
let l = scan_while(&data[(1 + close_tag_bytes)..], is_ascii_alpha);
if l == 0 {
return None;
}
let mut i = 1 + close_tag_bytes + l;
i += scan_while(&data[i..], is_ascii_letterdigitdash);
if close_tag_bytes == 0 {
loop {
let old_i = i;
loop {
i += scan_whitespace_no_nl(&data[i..]);
if let Some(eol_bytes) = scan_eol(&data[i..]) {
if eol_bytes == 0 {
return None;
}
let handler = newline_handler?;
i += eol_bytes;
let skipped_bytes = handler(&data[i..]);
let data_len = data.len() - i;
debug_assert!(
skipped_bytes <= data_len,
"Handler tried to skip too many bytes, fed {}, skipped {}",
data_len,
skipped_bytes
);
if skipped_bytes > 0 {
buffer.extend(&data[last_buf_index..i]);
i += skipped_bytes;
last_buf_index = i;
}
} else {
break;
}
}
if let Some(b'/') | Some(b'>') = data.get(i) {
break;
}
if old_i == i {
// No whitespace, which is mandatory.
return None;
}
i = scan_attribute(data, i, newline_handler, &mut buffer, &mut last_buf_index)?;
}
}
i += scan_whitespace_no_nl(&data[i..]);
if close_tag_bytes == 0 {
i += scan_ch(&data[i..], b'/');
}
if scan_ch(&data[i..], b'>') == 0 {
None
} else {
i += 1;
if !buffer.is_empty() {
buffer.extend(&data[last_buf_index..i]);
}
Some((buffer, i))
}
}
/// Returns (next_byte_offset, uri, type)
pub(crate) fn scan_autolink(text: &str, start_ix: usize) -> Option<(usize, CowStr<'_>, LinkType)> {
scan_uri(text, start_ix)
.map(|(bytes, uri)| (bytes, uri, LinkType::Autolink))
.or_else(|| scan_email(text, start_ix).map(|(bytes, uri)| (bytes, uri, LinkType::Email)))
}
/// Returns (next_byte_offset, uri)
fn scan_uri(text: &str, start_ix: usize) -> Option<(usize, CowStr<'_>)> {
let bytes = &text.as_bytes()[start_ix..];
// scheme's first byte must be an ascii letter
if bytes.is_empty() || !is_ascii_alpha(bytes[0]) {
return None;
}
let mut i = 1;
while i < bytes.len() {
let c = bytes[i];
i += 1;
match c {
c if is_ascii_alphanumeric(c) => (),
b'.' | b'-' | b'+' => (),
b':' => break,
_ => return None,
}
}
// scheme length must be between 2 and 32 characters long. scheme
// must be followed by colon
if !(3..=33).contains(&i) {
return None;
}
while i < bytes.len() {
match bytes[i] {
b'>' => return Some((start_ix + i + 1, text[start_ix..(start_ix + i)].into())),
b'\0'..=b' ' | b'<' => return None,
_ => (),
}
i += 1;
}
None
}
/// Returns (next_byte_offset, email)
fn scan_email(text: &str, start_ix: usize) -> Option<(usize, CowStr<'_>)> {
// using a regex library would be convenient, but doing it by hand is not too bad
let bytes = &text.as_bytes()[start_ix..];
let mut i = 0;
while i < bytes.len() {
let c = bytes[i];
i += 1;
match c {
c if is_ascii_alphanumeric(c) => (),
b'.' | b'!' | b'#' | b'$' | b'%' | b'&' | b'\'' | b'*' | b'+' | b'/' | b'=' | b'?'
| b'^' | b'_' | b'`' | b'{' | b'|' | b'}' | b'~' | b'-' => (),
b'@' if i > 1 => break,
_ => return None,
}
}
loop {
let label_start_ix = i;
let mut fresh_label = true;
while i < bytes.len() {
match bytes[i] {
c if is_ascii_alphanumeric(c) => (),
b'-' if fresh_label => {
return None;
}
b'-' => (),
_ => break,
}
fresh_label = false;
i += 1;
}
if i == label_start_ix || i - label_start_ix > 63 || bytes[i - 1] == b'-' {
return None;
}
if scan_ch(&bytes[i..], b'.') == 0 {
break;
}
i += 1;
}
if scan_ch(&bytes[i..], b'>') == 0 {
return None;
}
Some((start_ix + i + 1, text[start_ix..(start_ix + i)].into()))
}
/// Scan comment, declaration, or CDATA section, with initial "<!" already consumed.
/// Returns byte offset on match.
pub(crate) fn scan_inline_html_comment(
bytes: &[u8],
mut ix: usize,
scan_guard: &mut HtmlScanGuard,
) -> Option<usize> {
let c = *bytes.get(ix)?;
ix += 1;
match c {
// An HTML comment consists of `<!-->`, `<!--->`, or `<!--`, a string of characters not
// including the string `-->`, and `-->`.
b'-' if ix > scan_guard.comment => {
// HTML comment needs two hyphens after the !.
if *bytes.get(ix)? != b'-' {
return None;
}
// Yes, we're intentionally going backwards.
// We want the cursor to point here:
//
// <!--
// ^
//
// This way, the `<!-->` case is covered by the loop below.
ix -= 1;
while let Some(x) = memchr(b'-', &bytes[ix..]) {
ix += x + 1;
scan_guard.comment = ix;
if scan_ch(&bytes[ix..], b'-') == 1 && scan_ch(&bytes[ix + 1..], b'>') == 1 {
return Some(ix + 2);
}
}
None
}
// A CDATA section consists of the string `<![CDATA[`, a string of characters not
// including the string `]]>`, and the string `]]>`.
b'[' if bytes[ix..].starts_with(b"CDATA[") && ix > scan_guard.cdata => {
ix += b"CDATA[".len();
ix = memchr(b']', &bytes[ix..]).map_or(bytes.len(), |x| ix + x);
let close_brackets = scan_ch_repeat(&bytes[ix..], b']');
ix += close_brackets;
if close_brackets == 0 || scan_ch(&bytes[ix..], b'>') == 0 {
scan_guard.cdata = ix;
None
} else {
Some(ix + 1)
}
}
// A declaration consists of the string `<!`, an ASCII letter, zero or more characters not
// including the character >, and the character >.
_ if c.is_ascii_alphabetic() && ix > scan_guard.declaration => {
ix = memchr(b'>', &bytes[ix..]).map_or(bytes.len(), |x| ix + x);
if scan_ch(&bytes[ix..], b'>') == 0 {
scan_guard.declaration = ix;
None
} else {
Some(ix + 1)
}
}
_ => None,
}
}
/// Scan processing directive, with initial "<?" already consumed.
/// Returns the next byte offset on success.
pub(crate) fn scan_inline_html_processing(
bytes: &[u8],
mut ix: usize,
scan_guard: &mut HtmlScanGuard,
) -> Option<usize> {
if ix <= scan_guard.processing {
return None;
}
while let Some(offset) = memchr(b'?', &bytes[ix..]) {
ix += offset + 1;
if scan_ch(&bytes[ix..], b'>') == 1 {
return Some(ix + 1);
}
}
scan_guard.processing = ix;
None
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn overflow_list() {
assert!(
scan_listitem(b"4444444444444444444444444444444444444444444444444444444444!").is_none()
);
}
#[test]
fn overflow_by_addition() {
assert!(scan_listitem(b"1844674407370955161615!").is_none());
}
#[test]
fn good_emails() {
const EMAILS: &[&str] = &[
"<a@b.c>",
"<a@b>",
"<a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-@example.com>",
"<a@sixty-three-letters-in-this-identifier-----------------------63>",
];
for email in EMAILS {
assert!(scan_email(email, 1).is_some());
}
}
#[test]
fn bad_emails() {
const EMAILS: &[&str] = &[
"<@b.c>",
"<foo@-example.com>",
"<foo@example-.com>",
"<a@notrailingperiod.>",
"<a(noparens)@example.com>",
"<\"noquotes\"@example.com>",
"<a@sixty-four-letters-in-this-identifier-------------------------64>",
];
for email in EMAILS {
assert!(scan_email(email, 1).is_none());
}
}
}