@@ -51,18 +51,21 @@ pub struct BufferQueue {
51
51
52
52
impl BufferQueue {
53
53
/// Create an empty BufferQueue.
54
+ #[ inline]
54
55
pub fn new ( ) -> BufferQueue {
55
56
BufferQueue {
56
57
buffers : VecDeque :: with_capacity ( 16 ) ,
57
58
}
58
59
}
59
60
60
61
/// Returns whether the queue is empty.
62
+ #[ inline]
61
63
pub fn is_empty ( & self ) -> bool {
62
64
self . buffers . is_empty ( )
63
65
}
64
66
65
67
/// Get the buffer at the beginning of the queue.
68
+ #[ inline]
66
69
pub fn pop_front ( & mut self ) -> Option < StrTendril > {
67
70
self . buffers . pop_front ( )
68
71
}
@@ -89,8 +92,8 @@ impl BufferQueue {
89
92
90
93
/// Look at the next available character without removing it, if the queue is not empty.
91
94
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" ) ;
94
97
self . buffers . front ( ) . map ( |b| b. chars ( ) . next ( ) . unwrap ( ) )
95
98
}
96
99
@@ -114,9 +117,32 @@ impl BufferQueue {
114
117
}
115
118
116
119
/// 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
+ /// ```
120
146
pub fn pop_except_from ( & mut self , set : SmallCharSet ) -> Option < SetResult > {
121
147
let ( result, now_empty) = match self . buffers . front_mut ( ) {
122
148
None => ( None , false ) ,
@@ -150,6 +176,29 @@ impl BufferQueue {
150
176
// If so, consume them and return Some(true).
151
177
// If they do not match, return Some(false).
152
178
// 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
+ /// ```
153
202
pub fn eat < F : Fn ( & u8 , & u8 ) -> bool > ( & mut self , pat : & str , eq : F ) -> Option < bool > {
154
203
let mut buffers_exhausted = 0 ;
155
204
let mut consumed_from_last = 0 ;
0 commit comments