1/*!
2Optimized routines for parsing INI.
34This module provides 2 functions: `find_nl` and `find_nl_chr`:
56* `fn find_nl(s: &[u8]) -> usize`
78 Finds the first `b'\r'` or `b'\n'` in the input byte string and returns its index.
9 If no match was found returns the length of the input.
1011* `fn find_nl_chr(s: &[u8], chr: u8) -> usize`
1213 Finds the first `b'\r'`, `b'\n'` or `chr` in the input byte string and returns its index.
14 If no match was found returns the length of the input.
1516For more information on the SWAR approaches see: <http://0x80.pl/articles/simd-strfind.html#swar>.
17In reality I only see minor improvements with SWAR (about 33% faster).
1819*/
2021// LLVM is big dum dum, trust me I'm a human
22#[cfg(not(debug_assertions))]
23macro_rules! unsafe_assert {
24 ($e:expr) => { unsafe { if !$e { ::core::hint::unreachable_unchecked(); } } };
25}
26#[cfg(debug_assertions)]
27macro_rules! unsafe_assert {
28 ($e:expr) => {};
29}
3031mod generic;
3233cfg_if::cfg_if! {
34// These optimizations are little endian specific
35if #[cfg(not(target_endian = "little"))] {
36pub use self::generic::*;
37 }
38else if #[cfg(all(any(target_arch = "x86", target_arch = "x86_64"), target_feature = "avx2"))] {
39mod avx2;
40pub use self::avx2::*;
41 }
42else if #[cfg(all(any(target_arch = "x86", target_arch = "x86_64"), target_feature = "sse2"))] {
43mod sse2;
44pub use self::sse2::*;
45 }
46else if #[cfg(target_pointer_width = "64")] {
47mod swar64;
48pub use self::swar64::*;
49 }
50else if #[cfg(target_pointer_width = "32")] {
51mod swar32;
52pub use self::swar32::*;
53 }
54else {
55pub use self::generic::*;
56 }
57}
5859#[test]
60fn test_parse() {
61let mut buffer = [b'-'; 254];
62for i in 0..buffer.len() {
63 buffer[i] = b'\n';
6465// Check reference implementation
66assert_eq!(generic::find_nl(&buffer), i);
67assert_eq!(generic::find_nl_chr(&buffer, b'='), i);
6869// Check target implementation
70assert_eq!(find_nl(&buffer), i);
71assert_eq!(find_nl_chr(&buffer, b'='), i);
7273// Write annoying byte back
74buffer[i] = if i & 1 == 0 { !0x0D } else { !0x0A };
75 }
76}