Struct svgparser::Stream
[−]
[src]
pub struct Stream<'a> { /* fields omitted */ }
Streaming interface for &[u8]
data.
Methods
impl<'a> Stream<'a>
[src]
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);Run
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);Run
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');Run
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::InvalidAdvance{ expected: 6, total: 4, pos: ErrorPos::new(1, 5), });Run
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!().Run
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);Run
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");Run
fn trim_trailing_spaces(&mut self)
Decreases the stream size by removing trailing spaces.
Examples
use svgparser::Stream; let mut s = Stream::new(b"Some text "); assert_eq!(s.left(), 11); s.trim_trailing_spaces(); assert_eq!(s.left(), 9);Run
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);Run
fn len_to_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_or_end(b'l'), 5); assert_eq!(s.len_to_or_end(b'q'), 15);Run
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);Run
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);Run
fn jump_to_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_or_end(b'q'); assert_eq!(s.at_end(), true);Run
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);Run
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);Run
fn read_to(&mut self, c: u8) -> Result<&'a [u8], Error>
Returns reference to the 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);Run
fn read_to_trimmed(&mut self, c: u8) -> Result<&'a [u8], Error>
Returns reference to the trimmed data until selected char and advance stream by the data length.
Same as read_to()
, but also trim spaces from the both sides.
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_trimmed(b'.').unwrap(), b"Some text"); let mut s = Stream::new(b"Some text."); assert_eq!(s.read_to_trimmed(b'.').unwrap(), b"Some text");Run
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");Run
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");Run
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");Run
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.");Run
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);Run
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 errorRun
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);Run
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);Run
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));Run
Notes
- Suffix must be lowercase, otherwise it will be an error.
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.
Trait Implementations
impl<'a> PartialEq for Stream<'a>
[src]
fn eq(&self, __arg_0: &Stream<'a>) -> bool
This method tests for self
and other
values to be equal, and is used by ==
. Read more
fn ne(&self, __arg_0: &Stream<'a>) -> bool
This method tests for !=
.
impl<'a> Clone for Stream<'a>
[src]
fn clone(&self) -> Stream<'a>
Returns a copy of the value. Read more
fn clone_from(&mut self, source: &Self)
1.0.0
Performs copy-assignment from source
. Read more