ini_core-0.2.0/.cargo_vcs_info.json0000644000000001360000000000100126220ustar { "git": { "sha1": "707d11381919670c0419b9355c01c28acb2d977e" }, "path_in_vcs": "" }ini_core-0.2.0/.github/workflows/ci.yml000064400000000000000000000015170072674642500161610ustar 00000000000000name: CI on: push: branches: [ master ] pull_request: branches: [ master ] # Allows you to run this workflow manually from the Actions tab workflow_dispatch: jobs: ci: runs-on: ubuntu-latest strategy: matrix: rust: - stable - beta - nightly steps: - name: Checkout sources uses: actions/checkout@v2 - name: Install toolchain uses: actions-rs/toolchain@v1 with: profile: minimal toolchain: ${{ matrix.rust }} default: true - name: Run tests (debug) uses: actions-rs/cargo@v1 with: command: test - name: Run tests (release) uses: actions-rs/cargo@v1 with: command: test args: --release ini_core-0.2.0/.gitignore000064400000000000000000000000360072674642500134310ustar 00000000000000# Cargo target/ Cargo.lock ini_core-0.2.0/Cargo.toml0000644000000017160000000000100106250ustar # THIS FILE IS AUTOMATICALLY GENERATED BY CARGO # # When uploading crates to the registry Cargo will automatically # "normalize" Cargo.toml files for maximal compatibility # with all versions of Cargo and also rewrite `path` dependencies # to registry (e.g., crates.io) dependencies. # # If you are reading this file be aware that the original Cargo.toml # will likely look very different (and much more reasonable). # See Cargo.toml.orig for the original contents. [package] edition = "2021" name = "ini_core" version = "0.2.0" authors = ["Casper "] description = "Implements a pretty bare-bones streaming INI parser." documentation = "https://docs.rs/ini_core/" readme = "readme.md" keywords = [ "ini", "core", "config", "configuration", "parser", ] categories = [ "config", "parser-implementations", ] license = "MIT" repository = "https://github.com/CasualX/ini_core" [dependencies.cfg-if] version = "1.0" ini_core-0.2.0/Cargo.toml.orig000064400000000000000000000007420072674642500143340ustar 00000000000000[package] name = "ini_core" version = "0.2.0" edition = "2021" license = "MIT" authors = ["Casper "] description = "Implements a pretty bare-bones streaming INI parser." documentation = "https://docs.rs/ini_core/" repository = "https://github.com/CasualX/ini_core" readme = "readme.md" keywords = ["ini", "core", "config", "configuration", "parser"] categories = ["config", "parser-implementations"] [dependencies] cfg-if = "1.0" ini_core-0.2.0/license.txt000064400000000000000000000021110072674642500136200ustar 00000000000000Copyright (c) 2020-2021 Casper 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. ini_core-0.2.0/readme.md000064400000000000000000000110130072674642500132150ustar 00000000000000Ini streaming parser ==================== [![MIT License](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT) [![crates.io](https://img.shields.io/crates/v/ini_core.svg)](https://crates.io/crates/ini_core) [![docs.rs](https://docs.rs/ini_core/badge.svg)](https://docs.rs/ini_core) [![Build status](https://github.com/CasualX/ini_core/workflows/CI/badge.svg)](https://github.com/CasualX/ini_core/actions) Compatible with `no_std`. This library implements a pretty bare-bones, but super fast, streaming INI parser. Examples -------- ```rust use ini_core as ini; let document = "\ [SECTION] ;this is a comment Key=Value"; let elements = [ ini::Item::SectionEnd, ini::Item::Section("SECTION"), ini::Item::Comment("this is a comment"), ini::Item::Property("Key", Some("Value")), ini::Item::SectionEnd, ]; for (index, item) in ini::Parser::new(document).enumerate() { assert_eq!(item, elements[index]); } ``` The `SectionEnd` pseudo element is returned before a new section and at the end of the document. This helps processing sections after their properties finished parsing. The parser is very much line-based, it will continue no matter what and return nonsense as an item: ```rust use ini_core as ini; let document = "\ [SECTION nonsense"; let elements = [ ini::Item::SectionEnd, ini::Item::Error("[SECTION"), ini::Item::Property("nonsense", None), ini::Item::SectionEnd, ]; for (index, item) in ini::Parser::new(document).enumerate() { assert_eq!(item, elements[index]); } ``` Lines starting with `[` but contain either no closing `]` or a closing `]` not followed by a newline are returned as `Item::Error`. Lines missing a `=` are returned as `Item::Property` with `None` value. Format ------ INI is not a well specified format, this parser tries to make as little assumptions as possible but it does make decisions. * Newline is either `"\r\n"`, `"\n"` or `"\r"`. It can be mixed in a single document but this is not recommended. * Section header is `"[" section "]" newline`. `section` can be anything except contain newlines. * Property is `key "=" value newline`. `key` and `value` can be anything except contain newlines. * Comment is `";" comment newline` and Blank is just `newline`. The comment character can be customized. Note that padding whitespace is not trimmed by default: Section `[ SECTION ]`'s name is `SECTION`. Property `KEY = VALUE` has key `KEY` and value `VALUE`. Comment `; comment`'s comment is `comment`. No further processing of the input is done, eg. if escape sequences are necessary they must be processed by the user. Performance ----------- Tested against the other INI parsers available on crates.io. Fair warning all parsers except `light_ini` use a document model which allocate using `HashMap` and `String` which isn't exactly fair... In any case `ini_core` still mops the floor with `light_ini` which is a callback-based streaming parser. These tests were run with `-C target-cpu=native` on an AMD Ryzen 5 3600X. Parsing a big INI file (241 KB, 7640 lines): ```text running 5 tests test configparser ... bench: 1,921,162 ns/iter (+/- 207,479) = 128 MB/s test ini_core ... bench: 99,860 ns/iter (+/- 2,396) = 2472 MB/s test light_ini ... bench: 320,363 ns/iter (+/- 33,904) = 770 MB/s test simpleini ... bench: 7,814,605 ns/iter (+/- 271,016) = 31 MB/s test tini ... bench: 2,613,870 ns/iter (+/- 188,756) = 94 MB/s test result: ok. 0 passed; 0 failed; 0 ignored; 5 measured; 0 filtered out; finished in 19.99s ``` Parsing a smaller INI file (17.2 KB, 800 lines): ```text running 5 tests test configparser ... bench: 266,602 ns/iter (+/- 21,394) = 66 MB/s test ini_core ... bench: 8,179 ns/iter (+/- 845) = 2159 MB/s test light_ini ... bench: 149,990 ns/iter (+/- 16,379) = 117 MB/s test simpleini ... bench: 563,204 ns/iter (+/- 55,080) = 31 MB/s test tini ... bench: 340,383 ns/iter (+/- 28,667) = 51 MB/s test result: ok. 0 passed; 0 failed; 0 ignored; 5 measured; 0 filtered out; finished in 15.75s ``` The competition is not even close. 😎 License ------- Licensed under [MIT License](https://opensource.org/licenses/MIT), see [license.txt](license.txt). ### Contribution Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, shall be licensed as above, without any additional terms or conditions. ini_core-0.2.0/src/lib.rs000064400000000000000000000221360072674642500133510ustar 00000000000000/*! Ini streaming parser ==================== Simple, straight forward, super fast, `no_std` compatible streaming INI parser. Examples -------- ``` use ini_core as ini; let document = "\ [SECTION] ;this is a comment Key=Value"; let elements = [ ini::Item::SectionEnd, ini::Item::Section("SECTION"), ini::Item::Comment("this is a comment"), ini::Item::Property("Key", Some("Value")), ini::Item::SectionEnd, ]; for (index, item) in ini::Parser::new(document).enumerate() { assert_eq!(item, elements[index]); } ``` The `SectionEnd` pseudo element is returned before a new section and at the end of the document. This helps processing sections after their properties finished parsing. The parser is very much line-based, it will continue no matter what and return nonsense as an item: ``` use ini_core as ini; let document = "\ [SECTION nonsense"; let elements = [ ini::Item::SectionEnd, ini::Item::Error("[SECTION"), ini::Item::Property("nonsense", None), ini::Item::SectionEnd, ]; for (index, item) in ini::Parser::new(document).enumerate() { assert_eq!(item, elements[index]); } ``` Lines starting with `[` but contain either no closing `]` or a closing `]` not followed by a newline are returned as [`Item::Error`]. Lines missing a `=` are returned as [`Item::Property`] with `None` value. See below for more details. Format ------ INI is not a well specified format, this parser tries to make as little assumptions as possible but it does make decisions. * Newline is either `"\r\n"`, `"\n"` or `"\r"`. It can be mixed in a single document but this is not recommended. * Section header is `"[" section "]" newline`. `section` can be anything except contain newlines. * Property is `key "=" value newline`. `key` and `value` can be anything except contain newlines. * Comment is `";" comment newline` and Blank is just `newline`. The comment character can be customized. Note that padding whitespace is not trimmed by default: * Section `[ SECTION ]`'s name is `SECTION`. * Property `KEY = VALUE` has key `KEY` and value `VALUE`. * Comment `; comment`'s comment is `comment`. No further processing of the input is done, eg. if escape sequences are necessary they must be processed by the caller. */ #![cfg_attr(not(test), no_std)] #[allow(unused_imports)] use core::{fmt, str}; // All the routines here work only with and slice only at ascii characters // This means conversion between `&str` and `&[u8]` is a noop even when slicing #[inline] fn from_utf8(v: &[u8]) -> &str { #[cfg(not(debug_assertions))] return unsafe { str::from_utf8_unchecked(v) }; #[cfg(debug_assertions)] return str::from_utf8(v).unwrap(); } mod parse; /// Ini element. /// /// # Notes /// /// Strings are not checked or escaped when displaying the item. /// /// Ensure that they do not contain newlines or invalid characters. #[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)] pub enum Item<'a> { /// Syntax error. /// /// Section header element was malformed. /// Malformed section headers are defined by a line starting with `[` but not ending with `]`. /// /// ``` /// assert_eq!( /// ini_core::Parser::new("[Error").nth(1), /// Some(ini_core::Item::Error("[Error"))); /// ``` Error(&'a str), /// Section header element. /// /// ``` /// assert_eq!( /// ini_core::Parser::new("[Section]").nth(1), /// Some(ini_core::Item::Section("Section"))); /// ``` Section(&'a str), /// End of section. /// /// Pseudo element emitted before a [`Section`](Item::Section) and at the end of the document. /// This helps processing sections after their properties finished parsing. /// /// ``` /// assert_eq!( /// ini_core::Parser::new("").next(), /// Some(ini_core::Item::SectionEnd)); /// ``` SectionEnd, /// Property element. /// /// Key value must not contain `=`. /// /// The value is `None` if there is no `=`. /// /// ``` /// assert_eq!( /// ini_core::Parser::new("Key=Value").next(), /// Some(ini_core::Item::Property("Key", Some("Value")))); /// assert_eq!( /// ini_core::Parser::new("Key").next(), /// Some(ini_core::Item::Property("Key", None))); /// ``` Property(&'a str, Option<&'a str>), /// Comment. /// /// ``` /// assert_eq!( /// ini_core::Parser::new(";comment").next(), /// Some(ini_core::Item::Comment("comment"))); /// ``` Comment(&'a str), /// Blank line. /// /// Allows faithful reproduction of the whole ini document including blank lines. /// /// ``` /// assert_eq!( /// ini_core::Parser::new("\n").next(), /// Some(ini_core::Item::Blank)); /// ``` Blank, } impl<'a> fmt::Display for Item<'a> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { &Item::Error(error) => write!(f, "{}\n", error), &Item::Section(section) => write!(f, "[{}]\n", section), &Item::SectionEnd => Ok(()), &Item::Property(key, Some(value)) => write!(f, "{}={}\n", key, value), &Item::Property(key, None) => write!(f, "{}\n", key), &Item::Comment(comment) => write!(f, ";{}\n", comment), &Item::Blank => f.write_str("\n"), } } } /// Trims ascii whitespace from the start and end of the string slice. /// /// See also [`Parser::auto_trim`] to automatically trim strings. #[inline(never)] pub fn trim(s: &str) -> &str { s.trim_matches(|chr: char| chr.is_ascii_whitespace()) } /// Ini streaming parser. /// /// The whole document must be available before parsing starts. /// The parser then returns each element as it is being parsed. /// /// See [`crate`] documentation for more information. #[derive(Clone, Debug)] pub struct Parser<'a> { line: u32, comment_char: u8, auto_trim: bool, section_ended: bool, state: &'a [u8], } impl<'a> Parser<'a> { /// Constructs a new `Parser` instance. #[inline] pub const fn new(s: &'a str) -> Parser<'a> { let state = s.as_bytes(); Parser { line: 0, comment_char: b';', auto_trim: false, section_ended: false, state } } /// Sets the comment character, eg. `b'#'`. /// /// The default is `b';'`. #[must_use] #[inline] pub const fn comment_char(self, chr: u8) -> Parser<'a> { // Mask off high bit to ensure we don't corrupt utf8 strings let comment_char = chr & 0x7f; Parser { comment_char, ..self } } /// Sets auto trimming of all returned strings. /// /// The default is `false`. #[must_use] #[inline] pub const fn auto_trim(self, auto_trim: bool) -> Parser<'a> { Parser { auto_trim, ..self } } /// Returns the line number the parser is currently at. #[inline] pub const fn line(&self) -> u32 { self.line } /// Returns the remainder of the input string. #[inline] pub fn remainder(&self) -> &'a str { from_utf8(self.state) } } impl<'a> Iterator for Parser<'a> { type Item = Item<'a>; // #[cfg_attr(test, mutagen::mutate)] #[inline(never)] fn next(&mut self) -> Option> { let mut s = self.state; match s.first().cloned() { // Terminal case None => { if self.section_ended { None } else { self.section_ended = true; Some(Item::SectionEnd) } }, // Blank Some(b'\r' | b'\n') => { self.skip_ln(s); Some(Item::Blank) }, // Comment Some(chr) if chr == self.comment_char => { s = &s[1..]; let i = parse::find_nl(s); let comment = from_utf8(&s[..i]); let comment = if self.auto_trim { trim(comment) } else { comment }; self.skip_ln(&s[i..]); Some(Item::Comment(comment)) }, // Section Some(b'[') => { if self.section_ended { self.section_ended = false; let i = parse::find_nl(s); if s[i - 1] != b']' { let error = from_utf8(&s[..i]); self.skip_ln(&s[i..]); return Some(Item::Error(error)); } let section = from_utf8(&s[1..i - 1]); let section = if self.auto_trim { trim(section) } else { section }; self.skip_ln(&s[i..]); Some(Item::Section(section)) } else { self.section_ended = true; Some(Item::SectionEnd) } }, // Property _ => { let key = { let i = parse::find_nl_chr(s, b'='); let key = from_utf8(&s[..i]); let key = if self.auto_trim { trim(key) } else { key }; if s.get(i) != Some(&b'=') { self.skip_ln(&s[i..]); if key.is_empty() { return Some(Item::Blank); } return Some(Item::Property(key, None)); } s = &s[i + 1..]; key }; let value = { let i = parse::find_nl(s); let value = from_utf8(&s[..i]); let value = if self.auto_trim { trim(value) } else { value }; self.skip_ln(&s[i..]); value }; Some(Item::Property(key, Some(value))) }, } } } impl<'a> core::iter::FusedIterator for Parser<'a> {} impl<'a> Parser<'a> { #[inline] fn skip_ln(&mut self, mut s: &'a [u8]) { if s.len() > 0 { if s[0] == b'\r' { s = &s[1..]; } if s.len() > 0 { if s[0] == b'\n' { s = &s[1..]; } } self.line += 1; } self.state = s; } } #[cfg(test)] mod tests; ini_core-0.2.0/src/parse/avx2.rs000064400000000000000000000032030072674642500145670ustar 00000000000000 #[cfg(target_arch = "x86")] use core::arch::x86::*; #[cfg(target_arch = "x86_64")] use core::arch::x86_64::*; #[inline] pub fn find_nl(s: &[u8]) -> usize { let mut offset = 0; unsafe { let n_lit = _mm256_set1_epi8(b'\n' as i8); let r_lit = _mm256_set1_epi8(b'\r' as i8); while offset + 32 <= s.len() { let block = _mm256_lddqu_si256(s.as_ptr().add(offset) as *const _); let n_eq = _mm256_cmpeq_epi8(n_lit, block); let r_eq = _mm256_cmpeq_epi8(r_lit, block); let mask = _mm256_movemask_epi8(_mm256_or_si256(n_eq, r_eq)); if mask != 0 { return offset + mask.trailing_zeros() as usize; } offset += 32; } } unsafe_assert!(offset <= s.len()); offset += super::generic::find_nl(&s[offset..]); unsafe_assert!(offset <= s.len()); return offset; } #[inline] pub fn find_nl_chr(s: &[u8], chr: u8) -> usize { let mut offset = 0; unsafe { let n_lit = _mm256_set1_epi8(b'\n' as i8); let r_lit = _mm256_set1_epi8(b'\r' as i8); let c_lit = _mm256_set1_epi8(chr as i8); while offset + 32 <= s.len() { let block = _mm256_lddqu_si256(s.as_ptr().add(offset) as *const _); let n_eq = _mm256_cmpeq_epi8(n_lit, block); let r_eq = _mm256_cmpeq_epi8(r_lit, block); let c_eq = _mm256_cmpeq_epi8(c_lit, block); let mask = _mm256_movemask_epi8(_mm256_or_si256(_mm256_or_si256(n_eq, r_eq), c_eq)); if mask != 0 { return offset + mask.trailing_zeros() as usize; } offset += 32; } } unsafe_assert!(offset <= s.len()); offset += super::generic::find_nl_chr(&s[offset..], chr); unsafe_assert!(offset <= s.len()); return offset; } ini_core-0.2.0/src/parse/generic.rs000064400000000000000000000006700072674642500153300ustar 00000000000000 #[inline] pub fn find_nl(s: &[u8]) -> usize { let mut i = 0; while i < s.len() { if s[i] == b'\n' || s[i] == b'\r' { break; } i += 1; } unsafe_assert!(i <= s.len()); return i; } #[inline] pub fn find_nl_chr(s: &[u8], chr: u8) -> usize { let mut i = 0; while i < s.len() { if s[i] == b'\n' || s[i] == b'\r' || s[i] == chr { break; } i += 1; } unsafe_assert!(i <= s.len()); return i; } ini_core-0.2.0/src/parse/mod.rs000064400000000000000000000040600072674642500144700ustar 00000000000000/*! Optimized routines for parsing INI. This module provides 2 functions: `find_nl` and `find_nl_chr`: * `fn find_nl(s: &[u8]) -> usize` Finds the first `b'\r'` or `b'\n'` in the input byte string and returns its index. If no match was found returns the length of the input. * `fn find_nl_chr(s: &[u8], chr: u8) -> usize` Finds the first `b'\r'`, `b'\n'` or `chr` in the input byte string and returns its index. If no match was found returns the length of the input. For more information on the SWAR approaches see: . In reality I only see minor improvements with SWAR (about 33% faster). */ // LLVM is big dum dum, trust me I'm a human #[cfg(not(debug_assertions))] macro_rules! unsafe_assert { ($e:expr) => { unsafe { if !$e { ::core::hint::unreachable_unchecked(); } } }; } #[cfg(debug_assertions)] macro_rules! unsafe_assert { ($e:expr) => {}; } mod generic; cfg_if::cfg_if! { // These optimizations are little endian specific if #[cfg(not(target_endian = "little"))] { pub use self::generic::*; } else if #[cfg(all(any(target_arch = "x86", target_arch = "x86_64"), target_feature = "avx2"))] { mod avx2; pub use self::avx2::*; } else if #[cfg(all(any(target_arch = "x86", target_arch = "x86_64"), target_feature = "sse2"))] { mod sse2; pub use self::sse2::*; } else if #[cfg(target_pointer_width = "64")] { mod swar64; pub use self::swar64::*; } else if #[cfg(target_pointer_width = "32")] { mod swar32; pub use self::swar32::*; } else { pub use self::generic::*; } } #[test] fn test_parse() { let mut buffer = [b'-'; 254]; for i in 0..buffer.len() { buffer[i] = b'\n'; // Check reference implementation assert_eq!(generic::find_nl(&buffer), i); assert_eq!(generic::find_nl_chr(&buffer, b'='), i); // Check target implementation assert_eq!(find_nl(&buffer), i); assert_eq!(find_nl_chr(&buffer, b'='), i); // Write annoying byte back buffer[i] = if i & 1 == 0 { !0x0D } else { !0x0A }; } } ini_core-0.2.0/src/parse/sse2.rs000064400000000000000000000031200072674642500145610ustar 00000000000000 #[cfg(target_arch = "x86")] use core::arch::x86::*; #[cfg(target_arch = "x86_64")] use core::arch::x86_64::*; #[inline] pub fn find_nl(s: &[u8]) -> usize { let mut offset = 0; unsafe { let n_lit = _mm_set1_epi8(b'\n' as i8); let r_lit = _mm_set1_epi8(b'\r' as i8); while offset + 16 <= s.len() { let block = _mm_loadu_si128(s.as_ptr().add(offset) as *const _); let n_eq = _mm_cmpeq_epi8(n_lit, block); let r_eq = _mm_cmpeq_epi8(r_lit, block); let mask = _mm_movemask_epi8(_mm_or_si128(n_eq, r_eq)); if mask != 0 { return offset + mask.trailing_zeros() as usize; } offset += 16; } } unsafe_assert!(offset <= s.len()); offset += super::generic::find_nl(&s[offset..]); unsafe_assert!(offset <= s.len()); return offset; } #[inline] pub fn find_nl_chr(s: &[u8], chr: u8) -> usize { let mut offset = 0; unsafe { let n_lit = _mm_set1_epi8(b'\n' as i8); let r_lit = _mm_set1_epi8(b'\r' as i8); let c_lit = _mm_set1_epi8(chr as i8); while offset + 16 <= s.len() { let block = _mm_loadu_si128(s.as_ptr().add(offset) as *const _); let n_eq = _mm_cmpeq_epi8(n_lit, block); let r_eq = _mm_cmpeq_epi8(r_lit, block); let c_eq = _mm_cmpeq_epi8(c_lit, block); let mask = _mm_movemask_epi8(_mm_or_si128(_mm_or_si128(n_eq, r_eq), c_eq)); if mask != 0 { return offset + mask.trailing_zeros() as usize; } offset += 16; } } unsafe_assert!(offset <= s.len()); offset += super::generic::find_nl_chr(&s[offset..], chr); unsafe_assert!(offset <= s.len()); return offset; } ini_core-0.2.0/src/parse/swar32.rs000064400000000000000000000026250072674642500150370ustar 00000000000000 #[inline] pub fn find_nl(s: &[u8]) -> usize { let mut offset = 0; let n_lit = b'\n' as u32 * 0x01010101u32; let r_lit = b'\r' as u32 * 0x01010101u32; while offset + 4 <= s.len() { let word = unsafe { (s.as_ptr().add(offset) as *const u32).read_unaligned() }; let mask = cmpeq(n_lit, word) | cmpeq(r_lit, word); if mask != 0 { return offset + (mask.trailing_zeros() >> 3) as usize; } offset += 4; } unsafe_assert!(offset <= s.len()); offset += super::generic::find_nl(&s[offset..]); unsafe_assert!(offset <= s.len()); return offset; } #[inline] pub fn find_nl_chr(s: &[u8], chr: u8) -> usize { let mut offset = 0; let n_lit = b'\n' as u32 * 0x01010101u32; let r_lit = b'\r' as u32 * 0x01010101u32; let c_lit = chr as u32 * 0x01010101u32; while offset + 4 <= s.len() { let word = unsafe { (s.as_ptr().add(offset) as *const u32).read_unaligned() }; let mask = cmpeq(n_lit, word) | cmpeq(r_lit, word) | cmpeq(c_lit, word); if mask != 0 { return offset + (mask.trailing_zeros() >> 3) as usize; } offset += 4; } unsafe_assert!(offset <= s.len()); offset += super::generic::find_nl_chr(&s[offset..], chr); unsafe_assert!(offset <= s.len()); return offset; } #[inline] fn cmpeq(needle: u32, haystack: u32) -> u32 { let neq = !(needle ^ haystack); let t0 = (neq & 0x7f7f7f7f) + 0x01010101; let t1 = neq & 0x80808080; t0 & t1 } ini_core-0.2.0/src/parse/swar64.rs000064400000000000000000000027250072674642500150450ustar 00000000000000 #[inline] pub fn find_nl(s: &[u8]) -> usize { let mut offset = 0; let n_lit = b'\n' as u64 * 0x0101010101010101u64; let r_lit = b'\r' as u64 * 0x0101010101010101u64; while offset + 8 <= s.len() { let word = unsafe { (s.as_ptr().add(offset) as *const u64).read_unaligned() }; let mask = cmpeq(n_lit, word) | cmpeq(r_lit, word); if mask != 0 { return offset + (mask.trailing_zeros() >> 3) as usize; } offset += 8; } unsafe_assert!(offset <= s.len()); offset += super::generic::find_nl(&s[offset..]); unsafe_assert!(offset <= s.len()); return offset; } #[inline] pub fn find_nl_chr(s: &[u8], chr: u8) -> usize { let mut offset = 0; let n_lit = b'\n' as u64 * 0x0101010101010101u64; let r_lit = b'\r' as u64 * 0x0101010101010101u64; let c_lit = chr as u64 * 0x0101010101010101u64; while offset + 8 <= s.len() { let word = unsafe { (s.as_ptr().add(offset) as *const u64).read_unaligned() }; let mask = cmpeq(n_lit, word) | cmpeq(r_lit, word) | cmpeq(c_lit, word); if mask != 0 { return offset + (mask.trailing_zeros() >> 3) as usize; } offset += 8; } unsafe_assert!(offset <= s.len()); offset += super::generic::find_nl_chr(&s[offset..], chr); unsafe_assert!(offset <= s.len()); return offset; } #[inline] fn cmpeq(needle: u64, haystack: u64) -> u64 { let neq = !(needle ^ haystack); let t0 = (neq & 0x7f7f7f7f7f7f7f7f) + 0x0101010101010101; let t1 = neq & 0x8080808080808080; t0 & t1 } ini_core-0.2.0/src/tests.rs000064400000000000000000000053270072674642500137500ustar 00000000000000use crate::*; #[track_caller] fn check(s: &str, expected: &[Item]) { let value: Vec<_> = Parser::new(s).collect(); assert_eq!(value, expected); } #[track_caller] fn check_err(s: &str, line: usize) { for (index, item) in Parser::new(s).enumerate() { if index == line { assert!(matches!(item, Item::Error(_))); } } } #[test] fn test_eos() { check("\r\n[SECTION]", &[Item::Blank, Item::SectionEnd, Item::Section("SECTION"), Item::SectionEnd]); check("\r\n[SECTION]\n", &[Item::Blank, Item::SectionEnd, Item::Section("SECTION"), Item::SectionEnd]); check("\r\n;comment", &[Item::Blank, Item::Comment("comment"), Item::SectionEnd]); check("\r\n;comment\n", &[Item::Blank, Item::Comment("comment"), Item::SectionEnd]); check("\r\nKey=Value", &[Item::Blank, Item::Property("Key", Some("Value")), Item::SectionEnd]); check("\r\nKey=Value\n", &[Item::Blank, Item::Property("Key", Some("Value")), Item::SectionEnd]); check("\r\nKey=Value\r", &[Item::Blank, Item::Property("Key", Some("Value")), Item::SectionEnd]); check("\r\nKey=Value\r\n", &[Item::Blank, Item::Property("Key", Some("Value")), Item::SectionEnd]); check("\r\nAction", &[Item::Blank, Item::Property("Action", None), Item::SectionEnd]); check("\r\nAction\n", &[Item::Blank, Item::Property("Action", None), Item::SectionEnd]); check("\r\nAction\r", &[Item::Blank, Item::Property("Action", None), Item::SectionEnd]); check("\r\nAction\r\n", &[Item::Blank, Item::Property("Action", None), Item::SectionEnd]); } #[test] fn test_empty_strings() { check("[]\n=\r\n = \n;\n \r= \r\n =\n=", &[ Item::SectionEnd, Item::Section(""), Item::Property("", Some("")), Item::Property(" ", Some(" ")), Item::Comment(""), Item::Property(" ", None), Item::Property("", Some(" ")), Item::Property(" ", Some("")), Item::Property("", Some("")), Item::SectionEnd, ]); } #[test] fn test_syntax_errors() { check_err("[foo] ", 1); check_err("[foo] \r", 1); check_err("[foo] \n", 1); check_err("[foo", 1); check_err("[foo\r", 1); check_err("[foo\n", 1); check_err("[", 1); check_err("[\r", 1); check_err("[\n", 1); check_err("[foo]\n[", 3); } #[test] fn test_blank_lines() { check("\n\r\n\r", &[Item::Blank, Item::Blank, Item::Blank, Item::SectionEnd]); check("\r\r\n\r", &[Item::Blank, Item::Blank, Item::Blank, Item::SectionEnd]); check("\r\r\r\n", &[Item::Blank, Item::Blank, Item::Blank, Item::SectionEnd]); } #[test] fn test_terminates() { // Ensure syntax errors advance the internal parser state check("[\n[] \r\n", &[ Item::SectionEnd, Item::Error("["), Item::SectionEnd, Item::Error("[] "), Item::SectionEnd, ]); for _ in Parser::new("[") {} for _ in Parser::new("[] ") {} }