Skip to content

Commit 8f29ec6

Browse files
author
Richard Dodd
committed
More docs
1 parent 300d0fc commit 8f29ec6

File tree

2 files changed

+55
-5
lines changed

2 files changed

+55
-5
lines changed

markup5ever/util/buffer_queue.rs

Lines changed: 54 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,18 +51,21 @@ pub struct BufferQueue {
5151

5252
impl BufferQueue {
5353
/// Create an empty BufferQueue.
54+
#[inline]
5455
pub fn new() -> BufferQueue {
5556
BufferQueue {
5657
buffers: VecDeque::with_capacity(16),
5758
}
5859
}
5960

6061
/// Returns whether the queue is empty.
62+
#[inline]
6163
pub fn is_empty(&self) -> bool {
6264
self.buffers.is_empty()
6365
}
6466

6567
/// Get the buffer at the beginning of the queue.
68+
#[inline]
6669
pub fn pop_front(&mut self) -> Option<StrTendril> {
6770
self.buffers.pop_front()
6871
}
@@ -89,8 +92,8 @@ impl BufferQueue {
8992

9093
/// Look at the next available character without removing it, if the queue is not empty.
9194
pub fn peek(&self) -> Option<char> {
92-
// Invariant: all buffers in the queue are non-empty.
93-
debug_assert!(self.buffers.iter().skip_while(|el| el.len32() != 0).next().is_none());
95+
debug_assert!(self.buffers.iter().skip_while(|el| el.len32() != 0).next().is_none(),
96+
"invariant \"all buffers in the queue are non-empty\" failed");
9497
self.buffers.front().map(|b| b.chars().next().unwrap())
9598
}
9699

@@ -114,9 +117,32 @@ impl BufferQueue {
114117
}
115118

116119
/// Pops and returns either a single character from the given set, or
117-
/// a `StrTendril` of characters none of which are in the set. The set
118-
/// is represented as a bitmask and so can only contain the first 64
119-
/// ASCII characters.
120+
/// a buffer of characters none of which are in the set.
121+
///
122+
/// # Examples
123+
///
124+
/// ```
125+
/// # #[macro_use] extern crate markup5ever;
126+
/// # #[macro_use] extern crate tendril;
127+
/// # fn main() {
128+
/// use markup5ever::buffer_queue::{BufferQueue, SetResult};
129+
///
130+
/// let mut queue = BufferQueue::new();
131+
/// queue.push_back(format_tendril!(r#"<some_tag attr="text">SomeText</some_tag>"#));
132+
/// let set = small_char_set!(b'<' b'>' b' ' b'=' b'"' b'/');
133+
/// let tag = format_tendril!("some_tag");
134+
/// let attr = format_tendril!("attr");
135+
/// let attr_val = format_tendril!("text");
136+
/// assert_eq!(queue.pop_except_from(set), Some(SetResult::FromSet('<')));
137+
/// assert_eq!(queue.pop_except_from(set), Some(SetResult::NotFromSet(tag)));
138+
/// assert_eq!(queue.pop_except_from(set), Some(SetResult::FromSet(' ')));
139+
/// assert_eq!(queue.pop_except_from(set), Some(SetResult::NotFromSet(attr)));
140+
/// assert_eq!(queue.pop_except_from(set), Some(SetResult::FromSet('=')));
141+
/// assert_eq!(queue.pop_except_from(set), Some(SetResult::FromSet('"')));
142+
/// assert_eq!(queue.pop_except_from(set), Some(SetResult::NotFromSet(attr_val)));
143+
/// // ...
144+
/// # }
145+
/// ```
120146
pub fn pop_except_from(&mut self, set: SmallCharSet) -> Option<SetResult> {
121147
let (result, now_empty) = match self.buffers.front_mut() {
122148
None => (None, false),
@@ -150,6 +176,29 @@ impl BufferQueue {
150176
// If so, consume them and return Some(true).
151177
// If they do not match, return Some(false).
152178
// If not enough characters are available to know, return None.
179+
/// Consume bytes matching the pattern, using a custom comparison function `eq`.
180+
///
181+
/// Returns `Some(true)` if there is a match, `Some(false)` if there is no match, or `None` if
182+
/// it wasn't possible to know (more data is needed).
183+
///
184+
/// The custom comparison function is used elsewhere to compare ascii-case-insensitively.
185+
///
186+
/// # Examples
187+
///
188+
/// ```
189+
/// # extern crate markup5ever;
190+
/// # #[macro_use] extern crate tendril;
191+
/// # fn main() {
192+
/// use markup5ever::buffer_queue::{BufferQueue};
193+
///
194+
/// let mut queue = BufferQueue::new();
195+
/// queue.push_back(format_tendril!("testtext"));
196+
/// let test_str = "test";
197+
/// assert_eq!(queue.eat("test", |&a, &b| a == b), Some(true));
198+
/// assert_eq!(queue.eat("text", |&a, &b| a == b), Some(true));
199+
/// assert!(queue.is_empty());
200+
/// # }
201+
/// ```
153202
pub fn eat<F: Fn(&u8, &u8) -> bool>(&mut self, pat: &str, eq: F) -> Option<bool> {
154203
let mut buffers_exhausted = 0;
155204
let mut consumed_from_last = 0;

markup5ever/util/smallcharset.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
/// values less than 64.
1717
///
1818
/// This is stored as a bitmap, with 1 bit for each value.
19+
#[derive(Debug, Eq, PartialEq, Clone, Copy, Hash)]
1920
pub struct SmallCharSet {
2021
pub bits: u64,
2122
}

0 commit comments

Comments
 (0)