Skip to content

Commit

Permalink
Update documentation and add #[inline].
Browse files Browse the repository at this point in the history
  • Loading branch information
TheVeryDarkness committed Oct 10, 2024
1 parent 46596e4 commit f45b1a5
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
20 changes: 20 additions & 0 deletions src/stream/ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ pub trait CharExt {
}

impl CharExt for FixedUtf8Char {
#[inline]
fn len_utf8(&self) -> usize {
Self::len_utf8(self)
}
}

impl CharExt for char {
#[inline]
fn len_utf8(&self) -> usize {
Self::len_utf8(*self)
}
Expand All @@ -36,10 +38,12 @@ pub trait StrExt<'s, C: CharExt> {
impl<'s> StrExt<'s, FixedUtf8Char> for &'s str {
type Iterator = IterFixedUtf8Char<'s>;

#[inline]
fn chars_ext(self) -> Self::Iterator {
IterFixedUtf8Char::new(self)
}

#[inline]
fn first_char(self) -> Option<FixedUtf8Char> {
FixedUtf8Char::from_first_char(self)
}
Expand All @@ -48,10 +52,12 @@ impl<'s> StrExt<'s, FixedUtf8Char> for &'s str {
impl<'s> StrExt<'s, char> for &'s str {
type Iterator = std::str::Chars<'s>;

#[inline]
fn chars_ext(self) -> Self::Iterator {
self.chars()
}

#[inline]
fn first_char(self) -> Option<char> {
self.chars().next()
}
Expand Down Expand Up @@ -83,6 +89,7 @@ pub trait Pattern: Sized {
fn find_first_matching(self, s: &str) -> Option<usize>;

/// Find the first matching character or the whole length.
#[inline]
fn find_first_matching_or_whole_length(self, s: &str) -> usize {
self.find_first_matching(s).unwrap_or(s.len())
}
Expand All @@ -91,6 +98,7 @@ pub trait Pattern: Sized {
fn find_first_not_matching(self, s: &str) -> Option<usize>;

/// Find the first not matching character or the whole length.
#[inline]
fn find_first_not_matching_or_whole_length(self, s: &str) -> usize {
self.find_first_not_matching(s).unwrap_or(s.len())
}
Expand All @@ -101,10 +109,12 @@ impl Pattern for &[FixedUtf8Char] {

const EOL: [Self::Item; 2] = [LF, CR];

#[inline]
fn matches(&self, c: Self::Item) -> bool {
self.contains(&c)
}

#[inline]
fn trim_end(self, s: &str) -> &str {
let mut line = s;
while let Some(c) = self.iter().find(|&&c| line.ends_with(c.as_str())) {
Expand All @@ -115,6 +125,7 @@ impl Pattern for &[FixedUtf8Char] {
line
}

#[inline]
fn trim_start(self, s: &str) -> &str {
let mut line = s;
while let Some(c) = self.iter().find(|&&c| line.starts_with(c.as_str())) {
Expand All @@ -125,10 +136,12 @@ impl Pattern for &[FixedUtf8Char] {
line
}

#[inline]
fn trim(self, s: &str) -> &str {
self.trim_end(self.trim_start(s))
}

#[inline]
fn find_first_matching(self, s: &str) -> Option<usize> {
let mut cursor = 0;
for c in <&str as StrExt<FixedUtf8Char>>::chars_ext(s) {
Expand All @@ -140,6 +153,7 @@ impl Pattern for &[FixedUtf8Char] {
None
}

#[inline]
fn find_first_not_matching(self, s: &str) -> Option<usize> {
let mut cursor = 0;
for c in <&str as StrExt<FixedUtf8Char>>::chars_ext(s) {
Expand All @@ -157,26 +171,32 @@ impl Pattern for &[char] {

const EOL: [Self::Item; 2] = ['\n', '\r'];

#[inline]
fn matches(&self, c: Self::Item) -> bool {
self.contains(&c)
}

#[inline]
fn trim_start(self, s: &str) -> &str {
s.trim_start_matches(self)
}

#[inline]
fn trim_end(self, s: &str) -> &str {
s.trim_end_matches(self)
}

#[inline]
fn trim(self, s: &str) -> &str {
s.trim_matches(self)
}

#[inline]
fn find_first_matching(self, s: &str) -> Option<usize> {
s.find(self)
}

#[inline]
fn find_first_not_matching(self, s: &str) -> Option<usize> {
let l = s.trim_start_matches(self).len();
if l == 0 {
Expand Down
4 changes: 3 additions & 1 deletion src/stream/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ where
}

/// Get the current line. Read a new line if current line is empty.
#[inline]
fn get_line(&mut self) -> Result<&str, StreamError> {
let _: bool = self.fill_buf_if_eol()?;
let line: &str = self.get_cur_line();
Expand Down Expand Up @@ -101,6 +102,7 @@ where
/// - Returns `Ok(true)` if a new line is read.
/// - Returns `Ok(false)` if the current line is not empty.
/// - Returns `Err` if the buffer cannot be filled with a new line.
#[inline]
fn fill_buf_if_eol(&mut self) -> Result<bool, StreamError> {
if self.is_eol() {
self.fill_buf()?;
Expand Down Expand Up @@ -245,7 +247,7 @@ where

/// Get a single line. The trailing white spaces will be consumed and trimmed.
///
/// It can returns an empty string.
/// It can return an empty string.
#[inline]
fn try_get_line_trimmed(&mut self, white: &[Char]) -> Result<&str, StreamError> {
let line = self.try_get_line()?;
Expand Down

0 comments on commit f45b1a5

Please sign in to comment.