Skip to content

Commit

Permalink
added take_line for strings
Browse files Browse the repository at this point in the history
  • Loading branch information
TimLuq committed Aug 18, 2024
1 parent 92f878b commit 07b9f07
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/queue/byte_queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ impl<'a> ByteQueue<'a> {
let (start, end) = self.check_range(range);
super::DrainBytes::new(self, start, end)
}

/// Move data to the returned `ByteQueue` until the byte predicate returns `false`.
#[inline]
#[must_use]
Expand Down
22 changes: 19 additions & 3 deletions src/queue/string_queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -365,14 +365,16 @@ impl<'a> StringQueue<'a> {
// SAFETY: The range is checked to be valid UTF-8.
unsafe { super::DrainChars::new(self, start, end) }
}

/// Find the first byte position of a char in the queue.
#[inline]
#[must_use]
pub fn find_char<F: FnMut(char) -> bool>(&mut self, mut fun: F) -> Option<usize> {
self.chars_indecies().find(|&(_, ch)| fun(ch)).map(|(position, _)| position)
self.chars_indecies()
.find(|&(_, ch)| fun(ch))
.map(|(position, _)| position)
}

/// Move data to the returned `StringQueue` until the char predicate returns `false`.
#[inline]
#[must_use]
Expand All @@ -387,6 +389,20 @@ impl<'a> StringQueue<'a> {
core::mem::swap(&mut self.queue, &mut ret);
Self { queue: ret }
}

/// Takes and removes the first line from the queue.
/// If a newline (`'\n'`) is found, the returned queue will contain all data up to, and including, the newline.
/// If the queue does not contain a newline character, the returned queue will contain all data currently in the queue.
#[inline]
#[must_use]
pub fn take_line(&mut self) -> Self {
let Some(position) = self.find_char(|ch| ch == '\n') else {
return core::mem::replace(self, Self::new());
};
let mut ret = self.queue.split_off(position + 1);
core::mem::swap(&mut self.queue, &mut ret);
Self { queue: ret }
}
}

impl<'a> From<StringData<'a>> for StringQueue<'a> {
Expand Down
14 changes: 14 additions & 0 deletions src/stringdata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,20 @@ impl<'a> StringData<'a> {
av
}

/// Takes and removes the first line from the string.
/// If a newline (`'\n'`) is found, the returned string will contain all data up to, and including, the newline.
/// If the queue does not contain a newline character, the returned string will contain all data currently in the queue.
#[inline]
#[must_use]
pub fn take_line(&mut self) -> Self {
let Some(position) = self.as_str().find('\n') else {
return core::mem::replace(self, Self::empty());
};
let av = self.sliced(0..position);
self.make_sliced(position..);
av
}

/// Split the `StringData` at the given position.
#[inline]
#[must_use]
Expand Down

0 comments on commit 07b9f07

Please sign in to comment.