Struct lyon::svg::parser::Stream
[−]
[src]
pub struct Stream<'a> { /* fields omitted */ }
Streaming interface for &[u8]
data.
Methods
impl<'a> Stream<'a>
fn new(text: &[u8]) -> Stream
Constructs a new Stream
from data.
fn sub_stream(other: &Stream<'a>, start: usize, end: usize) -> Stream<'a>
Constructs a new Stream
from exists.
Used to properly detect a current position on the error.
fn pos(&self) -> usize
Returns current position.
fn set_pos_raw(&mut self, pos: usize)
Sets current position.
fn left(&self) -> usize
Returns number of chars left.
Examples
use svgparser::Stream; let mut s = Stream::new(b"text"); s.advance_raw(4); assert_eq!(s.at_end(), true); assert_eq!(s.left(), 0);
fn at_end(&self) -> bool
Returns true
if we are at the end of the stream.
Any pos()
value larger than original text length indicates stream end.
Accessing stream after reaching end via safe methods will produce svgparser::Error
.
Accessing stream after reaching end via unsafe/_raw methods will produce rust bound checking error.
Examples
use svgparser::Stream; let mut s = Stream::new(b"text"); s.advance_raw(2); assert_eq!(s.curr_char_raw(), b'x'); assert_eq!(s.at_end(), false); s.advance_raw(2); assert_eq!(s.at_end(), true);
fn curr_char(&self) -> Result<u8, Error>
Returns a char from current stream position.
Errors
Returns Error::UnexpectedEndOfStream
if we at the end of the stream.
fn curr_char_raw(&self) -> u8
Unsafe version of curr_char()
.
fn is_char_eq(&self, c: u8) -> Result<bool, Error>
Compares selected char with char from current stream position.
Errors
Returns Error::UnexpectedEndOfStream
if we at the end of the stream.
fn is_char_eq_raw(&self, c: u8) -> bool
Unsafe version of is_char_eq()
.
fn char_at(&self, pos: isize) -> Result<u8, Error>
Returns char at the position relative to current.
Errors
Returns Error::AdvanceError
if we are out of the stream bounds.
Examples
use svgparser::Stream; let mut s = Stream::new(b"text"); s.advance_raw(2); assert_eq!(s.char_at(-2).unwrap(), b't'); assert_eq!(s.char_at(-1).unwrap(), b'e'); assert_eq!(s.char_at(0).unwrap(), b'x'); assert_eq!(s.char_at(1).unwrap(), b't');
fn back(&mut self, n: usize) -> Result<(), Error>
Moves back by n
chars.
fn advance(&mut self, n: usize) -> Result<(), Error>
Advance by n
chars.
Errors
Returns Error::AdvanceError
if new position beyond stream end.
Examples
use svgparser::{Stream, Error, ErrorPos}; let mut s = Stream::new(b"text"); s.advance(2).unwrap(); // ok assert_eq!(s.pos(), 2); s.advance(2).unwrap(); // also ok, we at end now assert_eq!(s.pos(), 4); // fail assert_eq!(s.advance(2).err().unwrap(), Error::AdvanceError{ expected: 6, total: 4, pos: ErrorPos::new(1, 5), });
fn advance_raw(&mut self, n: usize)
Unsafe version of advance()
.
Examples
use svgparser::Stream; let mut s = Stream::new(b"text"); s.advance_raw(2); // ok s.advance_raw(20); // will cause panic via debug_assert!().
fn is_space(&self) -> Result<bool, Error>
Checks that char at the current position is (white)space.
Accepted chars: ' ', '\n', '\r', '\t'.
Examples
use svgparser::Stream; let mut s = Stream::new(b"t e x t"); assert_eq!(s.is_space().unwrap(), false); s.advance_raw(1); assert_eq!(s.is_space().unwrap(), true);
fn is_space_raw(&self) -> bool
Unsafe version of is_space()
.
fn skip_spaces(&mut self)
Skips (white)space's.
Examples
use svgparser::Stream; let mut s = Stream::new(b"Some \t\n\rtext"); s.advance_raw(4); s.skip_spaces(); assert_eq!(s.slice_tail(), b"text");
fn len_to(&self, c: u8) -> Result<usize, Error>
Calculates length to the selected char.
Errors
Returns Error::UnexpectedEndOfStream
if no such char.
Examples
use svgparser::Stream; let s = Stream::new(b"Some long text."); assert_eq!(s.len_to(b'l').unwrap(), 5);
fn len_to_char_or_end(&self, c: u8) -> usize
Calculates length to the selected char.
If char not found - returns length to the end of the stream.
Examples
use svgparser::Stream; let s = Stream::new(b"Some long text."); assert_eq!(s.len_to_char_or_end(b'l'), 5); assert_eq!(s.len_to_char_or_end(b'q'), 15);
fn len_to_space_or_end(&self) -> usize
Calculates length to the 'space' char.
Checked according to is_space()
method.
Examples
use svgparser::Stream; let s = Stream::new(b"Some\ntext."); assert_eq!(s.len_to_space_or_end(), 4);
fn jump_to(&mut self, c: u8) -> Result<(), Error>
Jumps to the selected char.
Errors
Returns Error::UnexpectedEndOfStream
if no such char.
Examples
use svgparser::Stream; let mut s = Stream::new(b"Some text."); s.jump_to(b't').unwrap(); assert_eq!(s.pos(), 5);
fn jump_to_char_or_end(&mut self, c: u8)
Jumps to the selected char or the end of the stream.
Examples
use svgparser::Stream; let mut s = Stream::new(b"Some text."); s.jump_to_char_or_end(b'q'); assert_eq!(s.at_end(), true);
fn jump_to_end(&mut self)
Jumps to the end of the stream.
Examples
use svgparser::Stream; let mut s = Stream::new(b"Some text."); s.jump_to_end(); assert_eq!(s.at_end(), true);
fn read_raw(&mut self, len: usize) -> &'a [u8]
Returns reference to data with length len
and advance stream to the same length.
Examples
use svgparser::Stream; let mut s = Stream::new(b"Some text."); assert_eq!(s.read_raw(4), b"Some"); assert_eq!(s.pos(), 4);
fn read_to(&mut self, c: u8) -> Result<&'a [u8], Error>
Returns reference to data until selected char and advance stream by the data length.
Shorthand for: len_to()
+ read_raw()
.
Errors
Returns Error::UnexpectedEndOfStream
if no such char.
Examples
use svgparser::Stream; let mut s = Stream::new(b"Some text."); assert_eq!(s.read_to(b'm').unwrap(), b"So"); assert_eq!(s.pos(), 2);
fn slice_next_raw(&self, len: usize) -> &'a [u8]
Returns next data of stream with selected length.
Examples
use svgparser::Stream; let s = Stream::new(b"Text"); assert_eq!(s.slice_next_raw(3), b"Tex");
fn slice_region_raw(&self, start: usize, end: usize) -> &'a [u8]
Returns data of stream within selected region.
Examples
use svgparser::Stream; let s = Stream::new(b"Text"); assert_eq!(s.slice_region_raw(1, 3), b"ex");
fn slice(&self) -> &'a [u8]
Returns complete data of stream.
Examples
use svgparser::Stream; let mut s = Stream::new(b"Text"); s.advance(2).unwrap(); assert_eq!(s.slice(), b"Text");
fn slice_tail(&self) -> &'a [u8]
Returns tail data of stream.
Examples
use svgparser::Stream; let mut s = Stream::new(b"Some text."); s.advance(5).unwrap(); assert_eq!(s.slice_tail(), b"text.");
fn starts_with(&self, text: &[u8]) -> bool
Returns true
if stream data at current position starts with selected text.
Examples
use svgparser::Stream; let mut s = Stream::new(b"Some text."); s.advance(5).unwrap(); assert_eq!(s.starts_with(b"text"), true); assert_eq!(s.starts_with(b"long"), false);
fn consume_char(&mut self, c: u8) -> Result<(), Error>
Consumes selected char.
Errors
If current char is not equal to selected - we will get Error::InvalidChar
.
Examples
use svgparser::Stream; let mut s = Stream::new(b"Some text."); s.consume_char(b'S').unwrap(); s.consume_char(b'o').unwrap(); s.consume_char(b'm').unwrap(); // s.consume_char(b'q').unwrap(); // will produce error
fn parse_number(&mut self) -> Result<f64, Error>
Parses number from the stream.
We use own parser instead of Rust parser because we didn't know number length, to pass it to Rust parser and it will not return a number of consumed chars, which we need. And to detect length we need to actually parse the number, so getting only a length will be pointless.
https://www.w3.org/TR/SVG/types.html#DataTypeNumber
Errors
Can return most of the Error
errors.
Examples
use svgparser::Stream; let mut s = Stream::new(b"3.14"); assert_eq!(s.parse_number().unwrap(), 3.14); assert_eq!(s.at_end(), true);
fn parse_list_number(&mut self) -> Result<f64, Error>
Parses number from the list of numbers.
Examples
use svgparser::Stream; let mut s = Stream::new(b"3.14, 12,5 , 20-4"); assert_eq!(s.parse_list_number().unwrap(), 3.14); assert_eq!(s.parse_list_number().unwrap(), 12.0); assert_eq!(s.parse_list_number().unwrap(), 5.0); assert_eq!(s.parse_list_number().unwrap(), 20.0); assert_eq!(s.parse_list_number().unwrap(), -4.0);
fn parse_integer(&mut self) -> Result<i32, Error>
Parses integer number from the stream.
Same as parse_number()
, but only for integer. Does not refer to any SVG type.
fn parse_list_integer(&mut self) -> Result<i32, Error>
Parses integer from the list of numbers.
fn parse_length(&mut self) -> Result<Length, Error>
Parses length from the stream.
https://www.w3.org/TR/SVG/types.html#DataTypeLength
Examples
use svgparser::{Stream, Length, LengthUnit}; let mut s = Stream::new(b"30%"); assert_eq!(s.parse_length().unwrap(), Length::new(30.0, LengthUnit::Percent));
Notes
- Suffix must be lowercase, otherwise it will be skipped.
fn parse_list_length(&mut self) -> Result<Length, Error>
Parses length from the list of lengths.
fn gen_error_pos(&self) -> ErrorPos
Calculates a current absolute position.
fn gen_end_of_stream_error(&self) -> Error
Generates a new UnexpectedEndOfStream
error from the current position.