Struct svgparser::Stream [] [src]

pub struct Stream<'a> { /* fields omitted */ }

Streaming interface for &[u8] data.

Methods

impl<'a> Stream<'a>
[src]

Constructs a new Stream from data.

Constructs a new Stream from exists.

Used to properly detect a current position on the error.

Returns current position.

Sets current position.

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

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

Returns a char from current stream position.

Errors

Returns Error::UnexpectedEndOfStream if we at the end of the stream.

Unsafe version of curr_char().

Compares selected char with char from current stream position.

Errors

Returns Error::UnexpectedEndOfStream if we at the end of the stream.

Unsafe version of is_char_eq().

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

Moves back by n chars.

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

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

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

Unsafe version of is_space().

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Parses integer number from the stream.

Same as parse_number(), but only for integer. Does not refer to any SVG type.

Parses integer from the list of numbers.

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.

Parses length from the list of lengths.

Calculates a current absolute position.

Generates a new UnexpectedEndOfStream error from the current position.

Trait Implementations

impl<'a> PartialEq for Stream<'a>
[src]

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

impl<'a> Clone for Stream<'a>
[src]

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

impl<'a> Copy for Stream<'a>
[src]

impl<'a> Debug for Stream<'a>
[src]

Formats the value using the given formatter.