lexical_util/skip.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 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584
//! An iterator that skips values equal to a provided value.
//!
//! Iterators over a contiguous slice, returning all values
//! except for those matching the provided skip value.
//!
//! # Complexity
//!
//! Although superficially quite simple, the level of complexity
//! introduced by digit separators can be quite complex, due
//! the number of permutations during parsing.
//!
//! We can consume any combinations of of \[0,3\] items from the following set:
//! - \[l\]eading digit separators, where digit separators occur before
//! digits.
//! - \[i\]nternal digit separators, where digit separators occur between
//! digits.
//! - \[t\]railing digit separators, where digit separators occur after
//! digits.
//!
//! In addition to those combinations, we can also have:
//! - \[c\]onsecutive digit separators, which allows two digit separators to
//! be adjacent.
//!
//! # Shorthand
//!
//! We will use the term consumer to denote a function that consumes digits,
//! splitting an input buffer at an index, where the leading section contains
//! valid input digits, and the trailing section contains invalid characters.
//! Due to the number of combinations for consumers, we use the following
//! shorthand to denote consumers:
//! - `no`, does not use a digit separator.
//! - `l`, consumes leading digit separators.
//! - `i`, consumes internal digit separators.
//! - `t`, consumes trailing digit separators.
//! - `c`, consumes consecutive digit separators.
//!
//! The `next`/`iter` algorithms are therefore named `next_x`, where `x`
//! represents the shorthand name of the consumer, in sorted order.
//! For example, `next_ilt` means that consumer can skip internal,
//! leading, and trailing digit separators, but not consecutive ones.
#![cfg(all(feature = "format", feature = "parse"))]
use core::{mem, ptr};
use crate::digit::char_is_digit_const;
use crate::format::NumberFormat;
use crate::format_flags as flags;
use crate::iterator::{DigitsIter, Iter};
// IS_ILTC
// -------
// NOTE: The compiler optimizes all these methods pretty well: it's as
// efficient or almost as efficient as optimized assembly without unsafe
// code, especially since we have to do bounds checking
// before and the compiler can determine all cases correctly.
/// Helpers to get the next or previous elements for checks.
///
/// This has the non-consecutive iterator variants as well
/// as the consecutive ones. The consecutive ones will iteratively
/// process all digits.
macro_rules! indexing {
(@next $self:ident, $index:expr) => {
$index.wrapping_add(1)
};
(@nextc $self:ident, $index:expr) => {{
let mut index = $index;
let slc = $self.byte.slc;
while slc.get(index.wrapping_add(1)).map_or(false, |&x| $self.is_digit_separator(x)) {
index = index.wrapping_add(1);
}
index.wrapping_add(1)
}};
(@prev $self:ident, $index:expr) => {
$index.wrapping_sub(1)
};
(@prevc $self:ident, $index:expr) => {{
let mut index = $index;
let slc = $self.byte.slc;
while slc.get(index.wrapping_sub(1)).map_or(false, |&x| $self.is_digit_separator(x)) {
index = index.wrapping_sub(1);
}
index.wrapping_sub(1)
}};
}
/// Determine if a single digit separator is internal.
///
/// # Examples
///
/// - `1__1_23`- invalid
/// - `1_1__23`- invalid
/// - `1_1_23`- valid
/// - `11_x23`- invalid
/// - `_1123`- invalid
/// - `+_1123`- invalid
/// - `_+1123`- invalid
/// - `1123_`- invalid
/// - `1123_.`- invalid
/// - `112_3.`- valid
///
/// # Preconditions
///
/// Assumes `slc[index]` is a digit separator.
macro_rules! is_i {
(@first $self:ident, $index:expr) => {{
// NOTE: The conditions here then are that:
// - `index - 1` is a digit
// - `index + 1` is a digit
let prev = indexing!(@prev $self, $index);
let next = indexing!(@next $self, $index);
let slc = $self.byte.slc;
slc.get(prev).map_or(false, |&x| $self.is_digit(x)) &&
slc.get(next).map_or(false, |&x| $self.is_digit(x))
}};
(@first $self:ident) => {
is_i!(@first $self, $self.byte.index)
};
(@internal $self:ident, $index:expr) => {{
// NOTE: We must have validated `prev`, so this just checks `next`.
// NOTE: The conditions here then are that:
// - `index + 1` is a digit
let next = indexing!(@next $self, $index);
let slc = $self.byte.slc;
slc.get(next).map_or(false, |&x| $self.is_digit(x))
}};
(@internal $self:ident) => {
is_i!(@internal $self, $self.byte.index)
};
}
/// Determine if consecutive digit separators are internal.
///
/// # Examples
///
/// - `1__1_23`- valid
/// - `1_1__23`- valid
/// - `1_1_23`- valid
/// - `11_x23`- invalid
/// - `_1123`- invalid
/// - `+_1123`- invalid
/// - `_+1123`- invalid
/// - `1123_`- invalid
/// - `1123_.`- invalid
/// - `112_3.`- valid
///
/// # Preconditions
///
/// Assumes `slc[index]` is a digit separator.
macro_rules! is_ic {
(@first $self:ident, $index:expr) => {{
// NOTE: The conditions here then are that:
// - `index - 1` is a digit after consuming digit separators
// - `index + 1` is a digit after consuming digit separators
let prev = indexing!(@prevc $self, $index);
let next = indexing!(@nextc $self, $index);
let slc = $self.byte.slc;
slc.get(prev).map_or(false, |&x| $self.is_digit(x)) &&
slc.get(next).map_or(false, |&x| $self.is_digit(x))
}};
(@first $self:ident) => {
is_ic!(@first $self, $self.byte.index)
};
(@internal $self:ident, $index:expr) => {{
// NOTE: We must have validated `prev`, so this just checks `next`.
// NOTE: The conditions here then are that:
// - `index + 1` is a digit after consuming digit separators
let next = indexing!(@nextc $self, $index);
let slc = $self.byte.slc;
slc.get(next).map_or(false, |&x| $self.is_digit(x))
}};
(@internal $self:ident) => {
is_ic!(@internal $self, $self.byte.index)
};
}
/// Determine if a single digit separator is leading.
///
/// # Examples
///
/// - `__123`- invalid
/// - `+__123`- invalid
/// - `._123`- valid
/// - `_+123`- valid
/// - `_123`- valid
/// - `+_123`- valid
///
/// Having a subsequent sign character is fine since it might
/// be part of a partial parser.
///
/// # Preconditions
///
/// Assumes `slc[index]` is a digit separator.
macro_rules! is_l {
(@first $self:ident, $index:expr) => {{
// NOTE: The conditions here then are that:
// - `index - 1` is not a digit
// - `index - 1` is not a digit separator
// - `index + 1` is not a digit separator
let prev = indexing!(@prev $self, $index);
let next = indexing!(@next $self, $index);
let slc = $self.byte.slc;
slc.get(prev).map_or(true, |&x| !$self.is_digit(x) && !$self.is_digit_separator(x)) &&
slc.get(next).map_or(true, |&x| !$self.is_digit_separator(x))
}};
(@first $self:ident) => {
is_l!(@first $self, $self.byte.index)
};
(@internal $self:ident, $index:expr) => {{
// NOTE: Previous must have been a digit so this cannot be valid.
false
}};
(@internal $self:ident) => {
is_l!(@internal $self, $self.byte.index)
};
}
/// Determine if one or more digit separators are leading.
///
/// # Examples
///
/// - `__123`- valid
/// - `+__123`- valid
/// - `+__+123`- valid
/// - `+__.123`- valid
/// - `._123`- valid
/// - `_+123`- invalid
/// - `_123`- valid
/// - `+_123`- valid
///
/// Having a subsequent sign character is fine since it might
/// be part of a partial parser.
///
/// # Preconditions
///
/// Assumes `slc[index]` is a digit separator.
macro_rules! is_lc {
(@first $self:ident, $index:expr) => {{
// NOTE: The conditions here then are that:
// - `index - 1` is not a digit after removing digit separators
let prev = indexing!(@prevc $self, $index);
let slc = $self.byte.slc;
slc.get(prev).map_or(true, |&x| !$self.is_digit(x))
}};
(@first $self:ident) => {
is_lc!(@first $self, $self.byte.index)
};
(@internal $self:ident, $index:expr) => {{
// NOTE: Previous must have been a digit so this cannot be valid.
false
}};
(@internal $self:ident) => {
is_lc!(@internal $self, $self.byte.index)
};
}
/// Determine if a single digit separator is trailing.
///
/// # Examples
///
/// - `123_`- valid
/// - `123__`- invalid
/// - `123_.`- valid
/// - `123__.`- invalid
/// - `123_1`- invalid
/// - `123__1`- invalid
/// - _: valid
/// - _+: valid
/// - 1_+: valid
///
/// Having a subsequent sign character is fine since it might
/// be part of a partial parser.
///
/// # Preconditions
///
/// Assumes `slc[index]` is a digit separator.
macro_rules! is_t {
(@first $self:ident, $index:expr) => {{
// NOTE: The conditions here then are that:
// - `index + 1` is not a digit
// - `index + 1` is not a digit separator
// - `index - 1` is not a digit separator
let prev = indexing!(@prev $self, $index);
let next = indexing!(@next $self, $index);
let slc = $self.byte.slc;
slc.get(next).map_or(true, |&x| !$self.is_digit(x) && !$self.is_digit_separator(x)) &&
slc.get(prev).map_or(true, |&x| !$self.is_digit_separator(x))
}};
(@first $self:ident) => {
is_t!(@first $self, $self.byte.index)
};
(@internal $self:ident, $index:expr) => {{
// NOTE: We must have validated `prev`, so this just checks `next`.
// NOTE: The conditions here then are that:
// - `index + 1` is not a digit
// - `index + 1` is not a digit separator
let next = indexing!(@next $self, $index);
let slc = $self.byte.slc;
slc.get(next).map_or(true, |&x| !$self.is_digit(x) && !$self.is_digit_separator(x))
}};
(@internal $self:ident) => {
is_t!(@internal $self, $self.byte.index)
};
}
/// Determine if one or more digit separators are trailing.
///
/// # Examples
///
/// - `123_`- valid
/// - `123__`- valid
/// - `123_.`- valid
/// - `123__.`- valid
/// - `123_1`- invalid
/// - `123__1`- invalid
///
/// # Preconditions
///
/// Assumes `slc[index]` is a digit separator.
macro_rules! is_tc {
(@first $self:ident, $index:expr) => {{
// NOTE: The conditions here then are that:
// - `index + 1` is not a digit
let next = indexing!(@nextc $self, $index);
let slc = $self.byte.slc;
slc.get(next).map_or(true, |&x| !$self.is_digit(x))
}};
(@first $self:ident) => {
is_tc!(@first $self, $self.byte.index)
};
(@internal $self:ident, $index:expr) => {
// NOTE: This is already optimized for the first case.
is_tc!(@first $self, $index)
};
(@internal $self:ident) => {
is_tc!(@internal $self, $self.byte.index)
};
}
/// Determine if the digit separator is leading or internal.
///
/// # Examples
///
/// - `__123`- invalid
/// - `+__123`- invalid
/// - `._123`- valid
/// - `_+123`- valid
/// - `_123`- valid
/// - `+_123`- valid
/// - `+1_23`- valid
/// - `+1__23`- invalid
/// - `+123_`- invalid
/// - `+123__`- invalid
/// - _: valid
/// - _+: valid
/// - 1_+: invalid
///
/// # Preconditions
///
/// Assumes `slc[index]` is a digit separator.
macro_rules! is_il {
(@first $self:ident, $index:expr) => {{
// NOTE: The conditions here then are that:
// - `index + 1` is a digit
// - `index + 1` is not a digit separator
// - `index - 1` is not a digit separator
//
// # Logic
//
// If the previous character is a digit, then the
// next character must be a digit. If the previous
// character is not a digit, then the subsequent character can
// be anything besides a digit separator
let prev = indexing!(@prev $self, $index);
let next = indexing!(@next $self, $index);
let slc = $self.byte.slc;
if slc.get(prev).map_or(false, |&x| $self.is_digit(x)) {
slc.get(next).map_or(false, |&x| $self.is_digit(x))
} else {
slc.get(prev).map_or(true, |&x| !$self.is_digit_separator(x))
}
}};
(@first $self:ident) => {
is_il!(@first $self, $self.byte.index)
};
(@internal $self:ident, $index:expr) => {{
// NOTE: We must have validated `prev`, so this just checks `next`.
// NOTE: The conditions here then are that:
// - `index + 1` is a digit
let next = indexing!(@next $self, $index);
let slc = $self.byte.slc;
slc.get(next).map_or(false, |&x| $self.is_digit(x))
}};
(@internal $self:ident) => {
is_il!(@internal $self, $self.byte.index)
};
}
/// Determine if consecutive digit separators are leading or internal.
///
/// # Examples
///
/// - `__123`- valid
/// - `+__123`- valid
/// - `._123`- valid
/// - `_+123`- valid
/// - `_123`- valid
/// - `+_123`- valid
/// - `+1_23`- valid
/// - `+1__23`- valid
/// - `+123_`- invalid
/// - `+123__`- invalid
/// - _: valid
/// - _+: valid
/// - 1_+: invalid
///
/// # Preconditions
///
/// Assumes `slc[index]` is a digit separator.
macro_rules! is_ilc {
(@first $self:ident, $index:expr) => {{
// NOTE: The conditions here then are that:
// - `index + 1` is a digit after consuming digit separators
//
// # Logic
//
// We also need to consider the case where it's empty,
// that is, the previous one wasn't a digit if we don't
// have a digit.
let prev = indexing!(@prevc $self, $index);
let next = indexing!(@nextc $self, $index);
let slc = $self.byte.slc;
slc.get(next).map_or(false, |&x| $self.is_digit(x)) ||
slc.get(prev).map_or(true, |&x| !$self.is_digit(x))
}};
(@first $self:ident) => {
is_ilc!(@first $self, $self.byte.index)
};
(@internal $self:ident, $index:expr) => {{
// NOTE: The conditions here then are that:
// - `index + 1` is a digit after consuming digit separators
let next = indexing!(@nextc $self, $index);
let slc = $self.byte.slc;
slc.get(next).map_or(true, |&x| $self.is_digit(x))
}};
(@internal $self:ident) => {
is_ilc!(@internal $self, $self.byte.index)
};
}
/// Determine if the digit separator is internal or trailing.
///
/// # Examples
///
/// - `__123`- valid
/// - `+__123`- valid
/// - `._123`- valid
/// - `_+123`- valid
/// - `_123`- valid
/// - `+_123`- valid
/// - `+1_23`- valid
/// - `+1__23`- valid
/// - `+123_`- invalid
/// - `+123__`- invalid
/// - _: valid
/// - _+: valid
/// - 1_+: invalid
///
/// # Preconditions
///
/// Assumes `slc[index]` is a digit separator.
macro_rules! is_it {
(@first$self:ident, $index:expr) => {{
// NOTE: The conditions here then are that:
// - `index - 1` is a digit
// - `index - 1` is not a digit separator
// - `index + 1` is not a digit separator
//
// # Logic
//
// If the previous character is not a digit, there cannot
// be a digit for a following character. If the previous
// character is a digit, then the following one must be
// a digit as well.
let prev = indexing!(@prev $self, $index);
let next = indexing!(@next $self, $index);
let slc = $self.byte.slc;
if slc.get(prev).map_or(false, |&x| $self.is_digit(x)) {
// Have a digit, any character besides a digit separator is valid
slc.get(next).map_or(true, |&x| !$self.is_digit_separator(x))
} else {
// Not a digit, so we cannot have a digit or a digit separator
slc.get(next).map_or(true, |&x| !$self.is_digit(x) && !$self.is_digit_separator(x))
}
}};
(@first$self:ident) => {
is_it!(@first $self, $self.byte.index)
};
(@internal $self:ident, $index:expr) => {{
// NOTE: We must have validated `prev`, so this just checks `next`.
// NOTE: The conditions here then are that:
// - `index + 1` is not a digit separator
// Since we've previously had a digit, this is guaranteed to
// be internal or trailing.
let next = indexing!(@next $self, $index);
let slc = $self.byte.slc;
slc.get(next).map_or(true, |&x| !$self.is_digit_separator(x))
}};
(@internal $self:ident) => {
is_it!(@internal $self, $self.byte.index)
};
}
/// Determine if consecutive digit separators are internal or trailing.
///
/// # Examples
///
/// - `__123`- invalid
/// - `+__123`- invalid
/// - `._123`- invalid
/// - `_+123`- invalid
/// - `_123`- invalid
/// - `+_123`- invalid
/// - `+1_23`- valid
/// - `+1__23`- valid
/// - `+123_`- valid
/// - `+123__`- valid
/// - _: valid
/// - _+: valid
/// - 1_+: valid
///
/// # Preconditions
///
/// Assumes `slc[index]` is a digit separator.
macro_rules! is_itc {
(@first $self:ident, $index:expr) => {{
// NOTE: The conditions here then are that:
// - `index - 1` is not a digit after consuming digit separators
//
// # Logic
//
// We also need to consider the case where it's empty,
// that is, the previous one wasn't a digit if we don't
// have a digit.
let prev = indexing!(@prevc $self, $index);
let next = indexing!(@nextc $self, $index);
let slc = $self.byte.slc;
slc.get(prev).map_or(false, |&x| !$self.is_digit(x)) ||
slc.get(next).map_or(true, |&x| !$self.is_digit(x))
}};
(@first $self:ident) => {
is_itc!(@first $self, $self.byte.index)
};
(@internal $self:ident, $index:expr) => {
// NOTE: Previous must have been a digit so this must be valid.
true
};
(@internal $self:ident) => {
is_itc!(@internal $self, $self.byte.index)
};
}
/// Determine if the digit separator is leading or trailing.
///
/// # Examples
///
/// - `__123`- invalid
/// - `+__123`- invalid
/// - `._123`- valid
/// - `_+123`- valid
/// - `_123`- valid
/// - `+_123`- valid
/// - `+1_23`- invalid
/// - `+1__23`- invalid
/// - `+123_`- valid
/// - `+123__`- invalid
/// - _: valid
/// - _+: valid
/// - 1_+: valid
///
/// # Preconditions
///
/// Assumes `slc[index]` is a digit separator.
macro_rules! is_lt {
(@first $self:ident, $index:expr) => {{
// NOTE: The conditions here then are that:
// - not (`index - 1` is a digit and `index + 1` is a digit)
// - `index - 1` is not a digit separator
// - `index + 1` is not a digit separator
let prev = indexing!(@prev $self, $index);
let next = indexing!(@next $self, $index);
let slc = $self.byte.slc;
let prev_value = slc.get(prev);
let next_value = slc.get(next);
let is_prev_sep = prev_value.map_or(false, |&x| $self.is_digit_separator(x));
let is_prev_dig = prev_value.map_or(false, |&x| $self.is_digit(x));
let is_next_sep = next_value.map_or(false, |&x| $self.is_digit_separator(x));
let is_next_dig = next_value.map_or(false, |&x| $self.is_digit(x));
!is_prev_sep && !is_next_sep && !(is_prev_dig && is_next_dig)
}};
(@first $self:ident) => {
is_lt!(@first $self, $self.byte.index)
};
(@internal $self:ident, $index:expr) => {{
// NOTE: We must have validated `prev`, so this just checks `next`.
// NOTE: The conditions here then are that:
// - `index + 1` is not a digit
// - `index + 1` is not a digit separator
let next = indexing!(@next $self, $index);
let slc = $self.byte.slc;
slc.get(next).map_or(true, |&x| !$self.is_digit(x) && !$self.is_digit_separator(x))
}};
(@internal $self:ident) => {
is_lt!(@internal $self, $self.byte.index)
};
}
/// Determine if consecutive digit separators are leading or trailing.
///
/// # Examples
///
/// - `__123`- valid
/// - `+__123`- valid
/// - `._123`- valid
/// - `_+123`- valid
/// - `_123`- valid
/// - `+_123`- valid
/// - `+1_23`- invalid
/// - `+1__23`- invalid
/// - `+123_`- valid
/// - `+123__`- valid
/// - _: valid
/// - _+: valid
/// - 1_+: valid
///
/// # Preconditions
///
/// Assumes `slc[index]` is a digit separator.
macro_rules! is_ltc {
(@first $self:ident, $index:expr) => {{
// NOTE: The conditions here then are that (after consuming separators):
// - not (`index - 1` is a digit and `index + 1` is a digit)
let prev = indexing!(@prevc $self, $index);
let next = indexing!(@nextc $self, $index);
let slc = $self.byte.slc;
!(slc.get(prev).map_or(false, |&x| $self.is_digit(x)) && slc.get(next).map_or(false, |&x| $self.is_digit(x)))
}};
(@first $self:ident) => {
is_ltc!(@first $self, $self.byte.index)
};
(@internal $self:ident, $index:expr) => {{
// NOTE: We must have validated `prev`, so this just checks `next`.
// NOTE: The conditions here then are that:
// - `index + 1` is not a digit
let next = indexing!(@nextc $self, $index);
let slc = $self.byte.slc;
slc.get(next).map_or(true, |&x| !$self.is_digit(x))
}};
(@internal $self:ident) => {
is_ltc!(@internal $self, $self.byte.index)
};
}
/// Determine if a single digit separator is internal, leading, or trailing.
///
/// # Examples
///
/// - `__123`- invalid
/// - `+__123`- invalid
/// - `._123`- valid
/// - `_+123`- valid
/// - `_123`- valid
/// - `+_123`- valid
/// - `+1_23`- valid
/// - `+1__23`- invalid
/// - `+123_`- valid
/// - `+123__`- invalid
/// - _: valid
/// - _+: valid
/// - 1_+: valid
///
/// # Preconditions
///
/// Assumes `slc[index]` is a digit separator.
macro_rules! is_ilt {
(@first $self:ident, $index:expr) => {{
// NOTE: The conditions here then are that:
// - `index + 1` is not a digit separator
// - `index - 1` is not a digit separator
let prev = indexing!(@prev $self, $index);
let next = indexing!(@next $self, $index);
let slc = $self.byte.slc;
!slc.get(next).map_or(false, |&x| $self.is_digit_separator(x)) &&
!slc.get(prev).map_or(false, |&x| $self.is_digit_separator(x))
}};
(@first $self:ident) => {
is_ilt!(@first $self, $self.byte.index)
};
(@internal $self:ident, $index:expr) => {{
// NOTE: We must have validated `prev`, so this just checks `next`.
// NOTE: The conditions here then are that:
// - `index + 1` is not a digit separator
let next = indexing!(@next $self, $index);
let slc = $self.byte.slc;
slc.get(next).map_or(true, |&x| !$self.is_digit_separator(x))
}};
(@internal $self:ident) => {
is_ilt!(@internal $self, $self.byte.index)
};
}
/// Determine if consecutive digit separators are internal, leading, or
/// trailing.
///
/// This is always true.
///
/// # Examples
///
/// - `__123`- valid
/// - `+__123`- valid
/// - `._123`- valid
/// - `_+123`- valid
/// - `_123`- valid
/// - `+_123`- valid
/// - `+1_23`- valid
/// - `+1__23`- valid
/// - `+123_`- valid
/// - `+123__`- valid
/// - _: valid
/// - _+: valid
/// - 1_+: valid
///
/// # Preconditions
///
/// Assumes `slc[index]` is a digit separator.
macro_rules! is_iltc {
(@first $self:ident, $index:expr) => {
true
};
(@first $self:ident) => {
is_iltc!(@first $self, $self.byte.index)
};
(@internal $self:ident, $index:expr) => {
true
};
(@internal $self:ident) => {
is_iltc!(@internal $self, $self.byte.index)
};
}
// PEEK
// ----
/// Consumes 1 or more digit separators.
/// Peeks the next token that's not a digit separator.
macro_rules! peek_1 {
($self:ident, $is_skip:ident) => {{
// This will consume a single, non-consecutive digit separators.
let index = $self.cursor();
let buffer = $self.get_buffer();
let value = buffer.get(index)?;
let is_digit_separator = $self.is_digit_separator(*value);
// NOTE: We can do some pretty major optimizations for internal values,
// since we can check the location and don't need to check previous values.
if is_digit_separator {
// NOTE: This cannot iteratively search for the next value,
// or else the consecutive digit separator has no effect (#96).
let is_skip = if $self.current_count() == 0 {
$is_skip!(@first $self)
} else {
$is_skip!(@internal $self)
};
if is_skip {
// SAFETY: Safe since `index < buffer.len()`, so `index + 1 <= buffer.len()``
unsafe { $self.set_cursor(index + 1) };
buffer.get(index + 1)
} else {
Some(value)
}
} else {
// Have 1 of 2 conditions:
// 1. A non-digit separator character.
// 2. A digit separator that is not valid in the context.
Some(value)
}
}};
}
/// Consumes 1 or more digit separators.
/// Peeks the next token that's not a digit separator.
macro_rules! peek_n {
($self:ident, $is_skip:ident) => {{
// This will consume consecutive digit separators.
let mut index = $self.cursor();
let buffer = $self.get_buffer();
let value = buffer.get(index)?;
let is_digit_separator = $self.is_digit_separator(*value);
// NOTE: We can do some pretty major optimizations for internal values,
// since we can check the location and don't need to check previous values.
if is_digit_separator {
let is_skip = if $self.current_count() == 0 {
$is_skip!(@first $self)
} else {
$is_skip!(@internal $self)
};
if is_skip {
// Have a skippable digit separator: keep incrementing until we find
// a non-digit separator character. Don't need any complex checks
// here, since we've already done them above.
index += 1;
while index < buffer.len()
&& buffer.get(index).map_or(false, |&x| $self.is_digit_separator(x))
{
index += 1;
}
// SAFETY: Safe since `index <= buffer.len()`.
unsafe { $self.set_cursor(index) };
buffer.get(index)
} else {
Some(value)
}
} else {
// Have 1 of 2 conditions:
// 1. A non-digit separator character.
// 2. A digit separator that is not valid in the context.
Some(value)
}
}};
}
/// Consumes no digit separators and peeks the next value.
macro_rules! peek_noskip {
($self:ident) => {
$self.byte.slc.get($self.byte.index)
};
}
/// Consumes at most 1 leading digit separator and peeks the next value.
macro_rules! peek_l {
($self:ident) => {
peek_1!($self, is_l)
};
}
/// Consumes at most 1 internal digit separator and peeks the next value.
macro_rules! peek_i {
($self:ident) => {
peek_1!($self, is_i)
};
}
/// Consumes at most 1 trailing digit separator and peeks the next value.
macro_rules! peek_t {
($self:ident) => {
peek_1!($self, is_t)
};
}
/// Consumes at most 1 internal/leading digit separator and peeks the next
/// value.
macro_rules! peek_il {
($self:ident) => {
peek_1!($self, is_il)
};
}
/// Consumes at most 1 internal/trailing digit separator and peeks the next
/// value.
macro_rules! peek_it {
($self:ident) => {
peek_1!($self, is_it)
};
}
/// Consumes at most 1 leading/trailing digit separator and peeks the next
/// value.
macro_rules! peek_lt {
($self:ident) => {
peek_1!($self, is_lt)
};
}
/// Consumes at most 1 digit separator and peeks the next value.
macro_rules! peek_ilt {
($self:ident) => {
peek_1!($self, is_ilt)
};
}
/// Consumes 1 or more leading digit separators and peeks the next value.
macro_rules! peek_lc {
($self:ident) => {
peek_n!($self, is_lc)
};
}
/// Consumes 1 or more internal digit separators and peeks the next value.
macro_rules! peek_ic {
($self:ident) => {
peek_n!($self, is_ic)
};
}
/// Consumes 1 or more trailing digit separators and peeks the next value.
macro_rules! peek_tc {
($self:ident) => {
peek_n!($self, is_tc)
};
}
/// Consumes 1 or more internal/leading digit separators and peeks the next
/// value.
macro_rules! peek_ilc {
($self:ident) => {
peek_n!($self, is_ilc)
};
}
/// Consumes 1 or more internal/trailing digit separators and peeks the next
/// value.
macro_rules! peek_itc {
($self:ident) => {
peek_n!($self, is_itc)
};
}
/// Consumes 1 or more leading/trailing digit separators and peeks the next
/// value.
macro_rules! peek_ltc {
($self:ident) => {
peek_n!($self, is_ltc)
};
}
/// Consumes 1 or more digit separators and peeks the next value.
macro_rules! peek_iltc {
($self:ident) => {
peek_n!($self, is_iltc)
};
}
// AS DIGITS
// ---------
/// Trait to simplify creation of a `Bytes` object.
pub trait AsBytes<'a> {
/// Create `Bytes` from object.
fn bytes<const FORMAT: u128>(&'a self) -> Bytes<'a, FORMAT>;
}
impl<'a> AsBytes<'a> for [u8] {
#[inline(always)]
fn bytes<const FORMAT: u128>(&'a self) -> Bytes<'a, FORMAT> {
Bytes::new(self)
}
}
// DIGITS
// ------
/// Slice iterator that skips characters matching a given value.
///
/// This wraps an iterator over a contiguous block of memory,
/// and only returns values that are not equal to skip.
///
/// The format allows us to dictate the actual behavior of
/// the iterator: in what contexts does it skip digit separators.
///
/// `FORMAT` is required to tell us what the digit separator is, and where
/// the digit separators are allowed, as well tell us the radix.
/// The radix is required to allow us to differentiate digit from
/// non-digit characters (see [`DigitSeparators`](/docs/DigitSeparators.md)
/// for a detailed explanation on why).
#[derive(Clone)]
pub struct Bytes<'a, const FORMAT: u128> {
/// The raw slice for the iterator.
slc: &'a [u8],
/// Current index of the iterator in the slice.
index: usize,
/// The current count of integer digits returned by the iterator.
/// This is only used if the iterator is not contiguous.
integer_count: usize,
/// The current count of fraction digits returned by the iterator.
/// This is only used if the iterator is not contiguous.
fraction_count: usize,
/// The current count of exponent digits returned by the iterator.
/// This is only used if the iterator is not contiguous.
exponent_count: usize,
}
impl<'a, const FORMAT: u128> Bytes<'a, FORMAT> {
/// Create new byte object.
#[inline(always)]
pub const fn new(slc: &'a [u8]) -> Self {
Self {
slc,
index: 0,
integer_count: 0,
fraction_count: 0,
exponent_count: 0,
}
}
/// Initialize the slice from raw parts.
///
/// # Safety
/// This is safe if and only if the index is <= `slc.len()`.
/// For this reason, since it's easy to get wrong, we only
/// expose it to our `DigitsIterator`s and nothing else.
///
/// This is only ever used for contiguous iterators. However,
/// it's not guaranteed to only valid for our contiguous
/// iterators.
#[inline(always)]
const unsafe fn from_parts(slc: &'a [u8], index: usize) -> Self {
debug_assert!(index <= slc.len());
Self {
slc,
index,
integer_count: 0,
fraction_count: 0,
exponent_count: 0,
}
}
/// Get iterator over integer digits.
#[inline(always)]
pub fn integer_iter<'b>(&'b mut self) -> IntegerDigitsIterator<'a, 'b, FORMAT> {
IntegerDigitsIterator {
byte: self,
}
}
/// Get iterator over fraction digits.
#[inline(always)]
pub fn fraction_iter<'b>(&'b mut self) -> FractionDigitsIterator<'a, 'b, FORMAT> {
FractionDigitsIterator {
byte: self,
}
}
/// Get iterator over exponent digits.
#[inline(always)]
pub fn exponent_iter<'b>(&'b mut self) -> ExponentDigitsIterator<'a, 'b, FORMAT> {
ExponentDigitsIterator {
byte: self,
}
}
/// Get iterator over special floating point values.
#[inline(always)]
pub fn special_iter<'b>(&'b mut self) -> SpecialDigitsIterator<'a, 'b, FORMAT> {
SpecialDigitsIterator {
byte: self,
}
}
/// Internal implementation that handles if it's contiguous.
///
/// # Safety
///
/// Safe if the buffer has at least `N` elements.
#[inline(always)]
unsafe fn step_by_unchecked_impl(&mut self, count: usize, is_contiguous: bool) {
// NOTE: THIS IS NOT a duplicate calling `step_by_unchecked` from a digits
// iterator on the byte, since they can have different contiguousness.
if is_contiguous {
// Contiguous, can skip most of these checks.
debug_assert!(self.as_slice().len() >= count);
} else {
// Since this isn't contiguous, it only works
// if the value is in the range `[0, 1]`.
// We also need to make sure the **current** value
// isn't a digit separator.
let format = NumberFormat::<{ FORMAT }> {};
debug_assert!(self.as_slice().len() >= count);
debug_assert!(count == 0 || count == 1);
debug_assert!(
count == 0 || self.slc.get(self.index) != Some(&format.digit_separator())
);
}
self.index += count;
}
/// Internal implementation that handles if it's contiguous.
///
/// If it's contiguous or not does not affect the safety guarantees,
/// however, it can affect correctness.
///
/// # Safety
///
/// Safe if the buffer has at least `size_of::<V>` elements.
#[inline(always)]
unsafe fn peek_many_unchecked_impl<V>(&self, is_contiguous: bool) -> V {
// NOTE: THIS IS NOT a duplicate calling `peek_many_unchecked` from a digits
// iterator on the byte, since they can have different contiguousness.
debug_assert!(is_contiguous);
debug_assert!(self.as_slice().len() >= mem::size_of::<V>());
let slc = self.as_slice();
// SAFETY: safe as long as the slice has at least count elements.
unsafe { ptr::read_unaligned::<V>(slc.as_ptr() as *const _) }
}
}
unsafe impl<'a, const FORMAT: u128> Iter<'a> for Bytes<'a, FORMAT> {
/// If each yielded value is adjacent in memory.
const IS_CONTIGUOUS: bool = NumberFormat::<{ FORMAT }>::DIGIT_SEPARATOR == 0;
#[inline(always)]
fn get_buffer(&self) -> &'a [u8] {
self.slc
}
/// Get the current index of the iterator in the slice.
#[inline(always)]
fn cursor(&self) -> usize {
self.index
}
/// Set the current index of the iterator in the slice.
///
/// # Safety
///
/// Safe if `index <= self.buffer_length()`.
#[inline(always)]
unsafe fn set_cursor(&mut self, index: usize) {
debug_assert!(index <= self.buffer_length());
self.index = index;
}
/// Get the current number of digits returned by the iterator.
///
/// For contiguous iterators, this can include the sign character, decimal
/// point, and the exponent sign (that is, it is always the cursor). For
/// non-contiguous iterators, this must always be the only the number of
/// digits returned.
#[inline(always)]
fn current_count(&self) -> usize {
// If the buffer is contiguous, then we don't need to track the
// number of values: the current index is enough.
if Self::IS_CONTIGUOUS {
self.index
} else {
self.integer_count + self.fraction_count + self.exponent_count
}
}
#[inline(always)]
unsafe fn step_by_unchecked(&mut self, count: usize) {
// SAFETY: Safe if the buffer has at least `N` elements.
unsafe { self.step_by_unchecked_impl(count, Self::IS_CONTIGUOUS) }
}
#[inline(always)]
unsafe fn peek_many_unchecked<V>(&self) -> V {
// SAFETY: Safe if the buffer has at least `size_of::<V>` elements.
unsafe { self.peek_many_unchecked_impl(Self::IS_CONTIGUOUS) }
}
}
// ITERATOR HELPERS
// ----------------
/// Create skip iterator definition.
macro_rules! skip_iterator {
($iterator:ident, $doc:literal) => {
#[doc = $doc]
pub struct $iterator<'a: 'b, 'b, const FORMAT: u128> {
/// The internal byte object for the skip iterator.
byte: &'b mut Bytes<'a, FORMAT>,
}
};
}
macro_rules! is_sign {
() => {
pub const fn is_sign(&self, value: u8) -> bool {
matches!(value, b'+' | b'-')
}
};
}
macro_rules! is_digit_separator {
($format:ident) => {
/// Determine if the character is a digit separator.
pub const fn is_digit_separator(&self, value: u8) -> bool {
let format = NumberFormat::<{ $format }> {};
let digit_separator = format.digit_separator();
if digit_separator == 0 {
// Check at compile time if we have an invalid digit separator.
// b'\x00', or the NUL character, is this invalid value.
false
} else {
value == digit_separator
}
}
};
}
/// Create impl block for skip iterator.
macro_rules! skip_iterator_impl {
($iterator:ident, $radix_cb:ident) => {
impl<'a: 'b, 'b, const FORMAT: u128> $iterator<'a, 'b, FORMAT> {
is_sign!();
is_digit_separator!(FORMAT);
/// Create a new digits iterator from the bytes underlying item.
#[inline(always)]
pub fn new(byte: &'b mut Bytes<'a, FORMAT>) -> Self {
Self {
byte,
}
}
/// Take the first N digits from the iterator.
///
/// This only takes the digits if we have a contiguous iterator.
/// It takes the digits, validating the bounds, and then advanced
/// the iterators state. It does not support non-contiguous iterators
/// since we would lose information on the count.
#[cfg_attr(not(feature = "compact"), inline(always))]
#[allow(clippy::assertions_on_constants)] // reason="ensuring safety invariants are valid"
pub fn take_n(&mut self, n: usize) -> Option<Bytes<'a, FORMAT>> {
if Self::IS_CONTIGUOUS {
let end = self.byte.slc.len().min(n + self.cursor());
// NOTE: The compiler should be able to optimize this out.
let slc: &[u8] = &self.byte.slc[..end];
// SAFETY: Safe since we just ensured the underlying slice has that count
// elements, so both the underlying slice for this and this **MUST**
// have at least count elements. We do static checking on the bounds for this.
unsafe {
let byte: Bytes<'_, FORMAT> = Bytes::from_parts(slc, self.cursor());
unsafe { self.set_cursor(end) };
Some(byte)
}
} else {
None
}
}
}
};
}
/// Create impl Iterator block for skip iterator.
macro_rules! skip_iterator_iterator_impl {
($iterator:ident) => {
impl<'a: 'b, 'b, const FORMAT: u128> Iterator for $iterator<'a, 'b, FORMAT> {
type Item = &'a u8;
#[inline(always)]
fn next(&mut self) -> Option<Self::Item> {
// Peek will handle everything properly internally.
let value = self.peek()?;
// Increment the index so we know not to re-fetch it.
self.byte.index += 1;
// NOTE: Only increment the count if it's not contiguous, otherwise,
// this is an unnecessary performance penalty. We also need
// to check if it's a digit, which adds on additional cost but
// there's not much else we can do. Hopefully the previous inlining
// checks will minimize the performance hit.
if !Self::IS_CONTIGUOUS && self.is_digit(*value) {
self.increment_count();
}
Some(value)
}
}
};
}
/// Create base methods for the Iter block of a skip iterator.
macro_rules! skip_iterator_iter_base {
($format:ident, $mask:ident, $count:ident) => {
// It's contiguous if we don't skip over any values.
// IE, the digit separator flags for the iterator over
// the digits doesn't skip any values.
const IS_CONTIGUOUS: bool = $format & flags::$mask == 0;
#[inline(always)]
fn get_buffer(&self) -> &'a [u8] {
self.byte.get_buffer()
}
#[inline(always)]
fn cursor(&self) -> usize {
self.byte.cursor()
}
#[inline(always)]
unsafe fn set_cursor(&mut self, index: usize) {
debug_assert!(index <= self.buffer_length());
// SAFETY: safe if `index <= self.buffer_length()`.
unsafe { self.byte.set_cursor(index) };
}
/// Get the current number of digits returned by the iterator.
///
/// For contiguous iterators, this can include the sign character, decimal
/// point, and the exponent sign (that is, it is always the cursor). For
/// non-contiguous iterators, this must always be the only the number of
/// digits returned.
#[inline(always)]
fn current_count(&self) -> usize {
if Self::IS_CONTIGUOUS {
self.byte.current_count()
} else {
self.byte.$count
}
}
#[inline(always)]
unsafe fn step_by_unchecked(&mut self, count: usize) {
// SAFETY: Safe if the buffer has at least `N` elements.
unsafe { self.byte.step_by_unchecked_impl(count, Self::IS_CONTIGUOUS) }
}
#[inline(always)]
unsafe fn peek_many_unchecked<V>(&self) -> V {
// SAFETY: Safe if the buffer has at least `size_of::<V>` elements.
unsafe { self.byte.peek_many_unchecked_impl(Self::IS_CONTIGUOUS) }
}
};
}
/// Create base methods for the `DigitsIter` block of a skip iterator.
macro_rules! skip_iterator_digits_iter_base {
() => {
#[inline(always)]
fn is_consumed(&mut self) -> bool {
self.peek().is_none()
}
};
}
/// Create impl `ByteIter` block for skip iterator.
macro_rules! skip_iterator_bytesiter_impl {
($iterator:ident, $mask:ident, $count:ident, $i:ident, $l:ident, $t:ident, $c:ident) => {
unsafe impl<'a: 'b, 'b, const FORMAT: u128> Iter<'a> for $iterator<'a, 'b, FORMAT> {
skip_iterator_iter_base!(FORMAT, $mask, $count);
}
impl<'a: 'b, 'b, const FORMAT: u128> DigitsIter<'a> for $iterator<'a, 'b, FORMAT> {
skip_iterator_digits_iter_base!();
/// Increment the number of digits that have been returned by the iterator.
///
/// For contiguous iterators, this is a no-op. For non-contiguous iterators,
/// this increments the count by 1.
#[inline(always)]
fn increment_count(&mut self) {
self.byte.$count += 1;
}
/// Peek the next value of the iterator, without consuming it.
///
/// Note that this can modify the internal state, by skipping digits
/// for iterators that find the first non-zero value, etc. We optimize
/// this for the case where we have contiguous iterators, since
/// non-contiguous iterators already have a major performance penalty.
///
/// Checking if the character is a digit in the `next()` implementation
/// after skipping characters can:
/// 1. Likely be optimized out due to the use of macros and inlining.
/// 2. Is a small amount of overhead compared to the branching on
/// characters,
#[inline(always)]
fn peek(&mut self) -> Option<<Self as Iterator>::Item> {
let format = NumberFormat::<{ FORMAT }> {};
const I: u128 = flags::$i;
const L: u128 = flags::$l;
const T: u128 = flags::$t;
const C: u128 = flags::$c;
const IL: u128 = I | L;
const IT: u128 = I | T;
const LT: u128 = L | T;
const ILT: u128 = I | L | T;
const IC: u128 = I | C;
const LC: u128 = L | C;
const TC: u128 = T | C;
const ILC: u128 = IL | C;
const ITC: u128 = IT | C;
const LTC: u128 = LT | C;
const ILTC: u128 = ILT | C;
match format.digit_separator_flags() & flags::$mask {
0 => peek_noskip!(self),
I => peek_i!(self),
L => peek_l!(self),
T => peek_t!(self),
IL => peek_il!(self),
IT => peek_it!(self),
LT => peek_lt!(self),
ILT => peek_ilt!(self),
IC => peek_ic!(self),
LC => peek_lc!(self),
TC => peek_tc!(self),
ILC => peek_ilc!(self),
ITC => peek_itc!(self),
LTC => peek_ltc!(self),
ILTC => peek_iltc!(self),
_ => unreachable!(),
}
}
/// Determine if the character is a digit.
#[inline(always)]
fn is_digit(&self, value: u8) -> bool {
let format = NumberFormat::<{ FORMAT }> {};
char_is_digit_const(value, format.mantissa_radix())
}
}
};
}
// INTEGER DIGITS ITERATOR
// -----------------------
skip_iterator!(IntegerDigitsIterator, "Iterator that skips over digit separators in the integer.");
skip_iterator_impl!(IntegerDigitsIterator, mantissa_radix);
skip_iterator_iterator_impl!(IntegerDigitsIterator);
skip_iterator_bytesiter_impl!(
IntegerDigitsIterator,
INTEGER_DIGIT_SEPARATOR_FLAG_MASK,
integer_count,
INTEGER_INTERNAL_DIGIT_SEPARATOR,
INTEGER_LEADING_DIGIT_SEPARATOR,
INTEGER_TRAILING_DIGIT_SEPARATOR,
INTEGER_CONSECUTIVE_DIGIT_SEPARATOR
);
// FRACTION DIGITS ITERATOR
// ------------------------
skip_iterator!(
FractionDigitsIterator,
"Iterator that skips over digit separators in the fraction."
);
skip_iterator_impl!(FractionDigitsIterator, mantissa_radix);
skip_iterator_iterator_impl!(FractionDigitsIterator);
skip_iterator_bytesiter_impl!(
FractionDigitsIterator,
FRACTION_DIGIT_SEPARATOR_FLAG_MASK,
fraction_count,
FRACTION_INTERNAL_DIGIT_SEPARATOR,
FRACTION_LEADING_DIGIT_SEPARATOR,
FRACTION_TRAILING_DIGIT_SEPARATOR,
FRACTION_CONSECUTIVE_DIGIT_SEPARATOR
);
// EXPONENT DIGITS ITERATOR
// ------------------------
skip_iterator!(
ExponentDigitsIterator,
"Iterator that skips over digit separators in the exponent."
);
skip_iterator_impl!(ExponentDigitsIterator, exponent_radix);
skip_iterator_iterator_impl!(ExponentDigitsIterator);
skip_iterator_bytesiter_impl!(
ExponentDigitsIterator,
EXPONENT_DIGIT_SEPARATOR_FLAG_MASK,
exponent_count,
EXPONENT_INTERNAL_DIGIT_SEPARATOR,
EXPONENT_LEADING_DIGIT_SEPARATOR,
EXPONENT_TRAILING_DIGIT_SEPARATOR,
EXPONENT_CONSECUTIVE_DIGIT_SEPARATOR
);
// SPECIAL DIGITS ITERATOR
// -----------------------
skip_iterator!(
SpecialDigitsIterator,
"Iterator that skips over digit separators in special floats."
);
skip_iterator_iterator_impl!(SpecialDigitsIterator);
impl<'a: 'b, 'b, const FORMAT: u128> SpecialDigitsIterator<'a, 'b, FORMAT> {
is_sign!();
is_digit_separator!(FORMAT);
}
unsafe impl<'a: 'b, 'b, const FORMAT: u128> Iter<'a> for SpecialDigitsIterator<'a, 'b, FORMAT> {
skip_iterator_iter_base!(FORMAT, SPECIAL_DIGIT_SEPARATOR, integer_count);
}
impl<'a: 'b, 'b, const FORMAT: u128> DigitsIter<'a> for SpecialDigitsIterator<'a, 'b, FORMAT> {
skip_iterator_digits_iter_base!();
// Always a no-op.
#[inline(always)]
fn increment_count(&mut self) {
}
/// Peek the next value of the iterator, without consuming it.
#[inline(always)]
fn peek(&mut self) -> Option<<Self as Iterator>::Item> {
let format = NumberFormat::<{ FORMAT }> {};
if format.special_digit_separator() {
peek_iltc!(self)
} else {
peek_noskip!(self)
}
}
/// Determine if the character is a digit.
#[inline(always)]
fn is_digit(&self, value: u8) -> bool {
let format = NumberFormat::<{ FORMAT }> {};
char_is_digit_const(value, format.mantissa_radix())
}
}