1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628
use core::char;
use core::cmp;
use core::ops::Range;
use core::str;
use crate::{
StreamError,
StrSpan,
TextPos,
XmlByteExt,
XmlCharExt,
};
type Result<T> = ::core::result::Result<T, StreamError>;
/// Representation of the [Reference](https://www.w3.org/TR/xml/#NT-Reference) value.
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
pub enum Reference<'a> {
/// An entity reference.
///
/// <https://www.w3.org/TR/xml/#NT-EntityRef>
Entity(&'a str),
/// A character reference.
///
/// <https://www.w3.org/TR/xml/#NT-CharRef>
Char(char),
}
/// A streaming XML parsing interface.
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
pub struct Stream<'a> {
pos: usize,
end: usize,
span: StrSpan<'a>,
}
impl<'a> From<&'a str> for Stream<'a> {
#[inline]
fn from(text: &'a str) -> Self {
Stream {
pos: 0,
end: text.len(),
span: text.into(),
}
}
}
impl<'a> From<StrSpan<'a>> for Stream<'a> {
#[inline]
fn from(span: StrSpan<'a>) -> Self {
Stream {
pos: 0,
end: span.as_str().len(),
span,
}
}
}
impl<'a> Stream<'a> {
/// Creates a new stream from a specified `text` substring.
#[inline]
pub fn from_substr(text: &'a str, fragment: Range<usize>) -> Self {
Stream {
pos: fragment.start,
end: fragment.end,
span: text.into(),
}
}
/// Returns an underling string span.
#[inline]
pub fn span(&self) -> StrSpan<'a> {
self.span
}
/// Returns current position.
#[inline]
pub fn pos(&self) -> usize {
self.pos
}
/// Sets current position equal to the end.
///
/// Used to indicate end of parsing on error.
#[inline]
pub fn jump_to_end(&mut self) {
self.pos = self.end;
}
/// Checks if the stream is reached the end.
///
/// Any [`pos()`] value larger than original text length indicates stream end.
///
/// Accessing stream after reaching end via safe methods will produce
/// an `UnexpectedEndOfStream` error.
///
/// Accessing stream after reaching end via *_unchecked methods will produce
/// a Rust's bound checking error.
///
/// [`pos()`]: #method.pos
#[inline]
pub fn at_end(&self) -> bool {
self.pos >= self.end
}
/// Returns a byte from a current stream position.
///
/// # Errors
///
/// - `UnexpectedEndOfStream`
#[inline]
pub fn curr_byte(&self) -> Result<u8> {
if self.at_end() {
return Err(StreamError::UnexpectedEndOfStream);
}
Ok(self.curr_byte_unchecked())
}
/// Returns a byte from a current stream position.
///
/// # Panics
///
/// - if the current position is after the end of the data
#[inline]
pub fn curr_byte_unchecked(&self) -> u8 {
self.span.as_bytes()[self.pos]
}
/// Returns a next byte from a current stream position.
///
/// # Errors
///
/// - `UnexpectedEndOfStream`
#[inline]
pub fn next_byte(&self) -> Result<u8> {
if self.pos + 1 >= self.end {
return Err(StreamError::UnexpectedEndOfStream);
}
Ok(self.span.as_bytes()[self.pos + 1])
}
/// Advances by `n` bytes.
///
/// # Examples
///
/// ```rust,should_panic
/// use xmlparser::Stream;
///
/// let mut s = Stream::from("text");
/// s.advance(2); // ok
/// s.advance(20); // will cause a panic via debug_assert!().
/// ```
#[inline]
pub fn advance(&mut self, n: usize) {
debug_assert!(self.pos + n <= self.end);
self.pos += n;
}
/// Checks that the stream starts with a selected text.
///
/// We are using `&[u8]` instead of `&str` for performance reasons.
///
/// # Examples
///
/// ```
/// use xmlparser::Stream;
///
/// let mut s = Stream::from("Some text.");
/// s.advance(5);
/// assert_eq!(s.starts_with(b"text"), true);
/// assert_eq!(s.starts_with(b"long"), false);
/// ```
#[inline]
pub fn starts_with(&self, text: &[u8]) -> bool {
self.span.as_bytes()[self.pos..self.end].starts_with(text)
}
/// Consumes the current byte if it's equal to the provided byte.
///
/// # Errors
///
/// - `InvalidChar`
/// - `UnexpectedEndOfStream`
///
/// # Examples
///
/// ```
/// use xmlparser::Stream;
///
/// let mut s = Stream::from("Some text.");
/// assert!(s.consume_byte(b'S').is_ok());
/// assert!(s.consume_byte(b'o').is_ok());
/// assert!(s.consume_byte(b'm').is_ok());
/// assert!(s.consume_byte(b'q').is_err());
/// ```
pub fn consume_byte(&mut self, c: u8) -> Result<()> {
let curr = self.curr_byte()?;
if curr != c {
return Err(StreamError::InvalidChar(curr, c, self.gen_text_pos()));
}
self.advance(1);
Ok(())
}
/// Tries to consume the current byte if it's equal to the provided byte.
///
/// Unlike `consume_byte()` will not return any errors.
pub fn try_consume_byte(&mut self, c: u8) -> bool {
match self.curr_byte() {
Ok(b) if b == c => {
self.advance(1);
true
}
_ => false,
}
}
/// Skips selected string.
///
/// # Errors
///
/// - `InvalidString`
pub fn skip_string(&mut self, text: &'static [u8]) -> Result<()> {
if !self.starts_with(text) {
let pos = self.gen_text_pos();
// Assume that all input `text` are valid UTF-8 strings, so unwrap is safe.
let expected = str::from_utf8(text).unwrap();
return Err(StreamError::InvalidString(expected, pos));
}
self.advance(text.len());
Ok(())
}
/// Consumes bytes by the predicate and returns them.
///
/// The result can be empty.
#[inline]
pub fn consume_bytes<F>(&mut self, f: F) -> StrSpan<'a>
where F: Fn(&Stream, u8) -> bool
{
let start = self.pos;
self.skip_bytes(f);
self.slice_back(start)
}
/// Skips bytes by the predicate.
pub fn skip_bytes<F>(&mut self, f: F)
where F: Fn(&Stream, u8) -> bool
{
while !self.at_end() && f(self, self.curr_byte_unchecked()) {
self.advance(1);
}
}
/// Consumes chars by the predicate and returns them.
///
/// The result can be empty.
#[inline]
pub fn consume_chars<F>(&mut self, f: F) -> Result<StrSpan<'a>>
where F: Fn(&Stream, char) -> bool
{
let start = self.pos;
self.skip_chars(f)?;
Ok(self.slice_back(start))
}
/// Skips chars by the predicate.
#[inline]
pub fn skip_chars<F>(&mut self, f: F) -> Result<()>
where F: Fn(&Stream, char) -> bool
{
for c in self.chars() {
if !c.is_xml_char() {
return Err(StreamError::NonXmlChar(c, self.gen_text_pos()));
} else if f(self, c) {
self.advance(c.len_utf8());
} else {
break;
}
}
Ok(())
}
#[inline]
pub(crate) fn chars(&self) -> str::Chars<'a> {
self.span.as_str()[self.pos..self.end].chars()
}
/// Slices data from `pos` to the current position.
#[inline]
pub fn slice_back(&self, pos: usize) -> StrSpan<'a> {
self.span.slice_region(pos, self.pos)
}
/// Slices data from the current position to the end.
#[inline]
pub fn slice_tail(&self) -> StrSpan<'a> {
self.span.slice_region(self.pos, self.end)
}
/// Skips whitespaces.
///
/// Accepted values: `' ' \n \r \t`.
#[inline]
pub fn skip_spaces(&mut self) {
while !self.at_end() && self.curr_byte_unchecked().is_xml_space() {
self.advance(1);
}
}
/// Checks if the stream is starts with a space.
#[inline]
pub fn starts_with_space(&self) -> bool {
!self.at_end() && self.curr_byte_unchecked().is_xml_space()
}
/// Consumes whitespaces.
///
/// Like [`skip_spaces()`], but checks that first char is actually a space.
///
/// [`skip_spaces()`]: #method.skip_spaces
///
/// # Errors
///
/// - `InvalidSpace`
pub fn consume_spaces(&mut self) -> Result<()> {
if self.at_end() {
return Err(StreamError::UnexpectedEndOfStream);
}
if !self.starts_with_space() {
return Err(StreamError::InvalidSpace(self.curr_byte_unchecked(), self.gen_text_pos()));
}
self.skip_spaces();
Ok(())
}
/// Consumes an XML character reference if there is one.
///
/// On error will reset the position to the original.
pub fn try_consume_reference(&mut self) -> Option<Reference<'a>> {
let start = self.pos();
// Consume reference on a substream.
let mut s = self.clone();
match s.consume_reference() {
Ok(r) => {
// If the current data is a reference than advance the current stream
// by number of bytes read by substream.
self.advance(s.pos() - start);
Some(r)
}
Err(_) => {
None
}
}
}
/// Consumes an XML reference.
///
/// Consumes according to: <https://www.w3.org/TR/xml/#NT-Reference>
///
/// # Errors
///
/// - `InvalidReference`
pub fn consume_reference(&mut self) -> Result<Reference<'a>> {
self._consume_reference().map_err(|_| StreamError::InvalidReference)
}
#[inline(never)]
fn _consume_reference(&mut self) -> Result<Reference<'a>> {
if !self.try_consume_byte(b'&') {
return Err(StreamError::InvalidReference);
}
let reference = if self.try_consume_byte(b'#') {
let (value, radix) = if self.try_consume_byte(b'x') {
let value = self.consume_bytes(|_, c| c.is_xml_hex_digit()).as_str();
(value, 16)
} else {
let value = self.consume_bytes(|_, c| c.is_xml_digit()).as_str();
(value, 10)
};
let n = u32::from_str_radix(value, radix).map_err(|_| StreamError::InvalidReference)?;
let c = char::from_u32(n).unwrap_or('\u{FFFD}');
if !c.is_xml_char() {
return Err(StreamError::InvalidReference);
}
Reference::Char(c)
} else {
let name = self.consume_name()?;
match name.as_str() {
"quot" => Reference::Char('"'),
"amp" => Reference::Char('&'),
"apos" => Reference::Char('\''),
"lt" => Reference::Char('<'),
"gt" => Reference::Char('>'),
_ => Reference::Entity(name.as_str()),
}
};
self.consume_byte(b';')?;
Ok(reference)
}
/// Consumes an XML name and returns it.
///
/// Consumes according to: <https://www.w3.org/TR/xml/#NT-Name>
///
/// # Errors
///
/// - `InvalidName` - if name is empty or starts with an invalid char
/// - `UnexpectedEndOfStream`
pub fn consume_name(&mut self) -> Result<StrSpan<'a>> {
let start = self.pos();
self.skip_name()?;
let name = self.slice_back(start);
if name.is_empty() {
return Err(StreamError::InvalidName);
}
Ok(name)
}
/// Skips an XML name.
///
/// The same as `consume_name()`, but does not return a consumed name.
///
/// # Errors
///
/// - `InvalidName` - if name is empty or starts with an invalid char
pub fn skip_name(&mut self) -> Result<()> {
let mut iter = self.chars();
if let Some(c) = iter.next() {
if c.is_xml_name_start() {
self.advance(c.len_utf8());
} else {
return Err(StreamError::InvalidName);
}
}
for c in iter {
if c.is_xml_name() {
self.advance(c.len_utf8());
} else {
break;
}
}
Ok(())
}
/// Consumes a qualified XML name and returns it.
///
/// Consumes according to: <https://www.w3.org/TR/xml-names/#ns-qualnames>
///
/// # Errors
///
/// - `InvalidName` - if name is empty or starts with an invalid char
#[inline(never)]
pub fn consume_qname(&mut self) -> Result<(StrSpan<'a>, StrSpan<'a>)> {
let start = self.pos();
let mut splitter = None;
while !self.at_end() {
// Check for ASCII first for performance reasons.
let b = self.curr_byte_unchecked();
if b < 128 {
if b == b':' {
if splitter.is_none() {
splitter = Some(self.pos());
self.advance(1);
} else {
// Multiple `:` is an error.
return Err(StreamError::InvalidName);
}
} else if b.is_xml_name() {
self.advance(1);
} else {
break;
}
} else {
// Fallback to Unicode code point.
match self.chars().nth(0) {
Some(c) if c.is_xml_name() => {
self.advance(c.len_utf8());
}
_ => break,
}
}
}
let (prefix, local) = if let Some(splitter) = splitter {
let prefix = self.span().slice_region(start, splitter);
let local = self.slice_back(splitter + 1);
(prefix, local)
} else {
let local = self.slice_back(start);
("".into(), local)
};
// Prefix must start with a `NameStartChar`.
if let Some(c) = prefix.as_str().chars().nth(0) {
if !c.is_xml_name_start() {
return Err(StreamError::InvalidName);
}
}
// Local name must start with a `NameStartChar`.
if let Some(c) = local.as_str().chars().nth(0) {
if !c.is_xml_name_start() {
return Err(StreamError::InvalidName);
}
} else {
// If empty - error.
return Err(StreamError::InvalidName);
}
Ok((prefix, local))
}
/// Consumes `=`.
///
/// Consumes according to: <https://www.w3.org/TR/xml/#NT-Eq>
///
/// # Errors
///
/// - `InvalidChar`
/// - `UnexpectedEndOfStream`
pub fn consume_eq(&mut self) -> Result<()> {
self.skip_spaces();
self.consume_byte(b'=')?;
self.skip_spaces();
Ok(())
}
/// Consumes quote.
///
/// Consumes `'` or `"` and returns it.
///
/// # Errors
///
/// - `InvalidQuote`
/// - `UnexpectedEndOfStream`
pub fn consume_quote(&mut self) -> Result<u8> {
let c = self.curr_byte()?;
if c == b'\'' || c == b'"' {
self.advance(1);
Ok(c)
} else {
Err(StreamError::InvalidQuote(c, self.gen_text_pos()))
}
}
/// Calculates a current absolute position.
///
/// This operation is very expensive. Use only for errors.
#[inline(never)]
pub fn gen_text_pos(&self) -> TextPos {
let text = self.span.as_str();
let end = self.pos;
let row = Self::calc_curr_row(text, end);
let col = Self::calc_curr_col(text, end);
TextPos::new(row, col)
}
/// Calculates an absolute position at `pos`.
///
/// This operation is very expensive. Use only for errors.
///
/// # Examples
///
/// ```
/// let s = xmlparser::Stream::from("text");
///
/// assert_eq!(s.gen_text_pos_from(2), xmlparser::TextPos::new(1, 3));
/// assert_eq!(s.gen_text_pos_from(9999), xmlparser::TextPos::new(1, 5));
/// ```
#[inline(never)]
pub fn gen_text_pos_from(&self, pos: usize) -> TextPos {
let mut s = self.clone();
s.pos = cmp::min(pos, s.span.as_str().len());
s.gen_text_pos()
}
fn calc_curr_row(text: &str, end: usize) -> u32 {
let mut row = 1;
for c in &text.as_bytes()[..end] {
if *c == b'\n' {
row += 1;
}
}
row
}
fn calc_curr_col(text: &str, end: usize) -> u32 {
let mut col = 1;
for c in text[..end].chars().rev() {
if c == '\n' {
break;
} else {
col += 1;
}
}
col
}
}