[][src]Struct asciifile::position::Position

pub struct Position<'t> { /* fields omitted */ }

Methods

impl<'t> Position<'t>
[src]

Create a new Position object pointing at the first character of a file. Returns None for empty files.

Get the character at this position

Guranteed to be within the ASCII range of UTF-8 that does not use the upper half of the bytes. Use byte() if you need a single byte representation instead.

Get the byte at this position

For matching on the character or comparisons, you probably want chr() instead.

Return the row of the character's position within the file.

The row is zero based, meaning characters on the first line of the file are in row 0. To get the line number, add 1 to the return value.

Return the character's line number. Identical to row() + 1

Return the column of the characters position within the file.

The column is zero based, meaning the first characters of a line/row is positioned at column 0.

Return the column of the characters position within the file.

The column is zero based, meaning the first characters of a line/row is positioned at column 0.

Get the position immediatly following this position or None if this is the last position in the file.

The same as next() but in-place.

Fails if there is no next position and returns the unchanged position.

Check if we are at the end of the file (the last valid position/the last character of the file).

Get the position immediatly following this position or None if this is the last position in the file.

The same as prev() but in-place.

Fails if there is no prev position and returns the unchanged position.

Get source code line containing the position.

The returned Span will include the trailing newline character of the line.

use asciifile::{AsciiFile, Position, Span};

let file = AsciiFile::new(b"banana\napple\n\nkiwi").unwrap();

let lines = file
    .iter()
    .map(|position| {
        let line = position.line();
        (position.chr(), line.as_str().to_string())
    })
    .collect::<Vec<_>>();

let expected: Vec<(char, String)> = vec![
    ('b', "banana\n"),
    ('a', "banana\n"),
    ('n', "banana\n"),
    ('a', "banana\n"),
    ('n', "banana\n"),
    ('a', "banana\n"),
    ('\n', "banana\n"),
    ('a', "apple\n"),
    ('p', "apple\n"),
    ('p', "apple\n"),
    ('l', "apple\n"),
    ('e', "apple\n"),
    ('\n', "apple\n"),
    ('\n', "\n"),
    ('k', "kiwi"),
    ('i', "kiwi"),
    ('w', "kiwi"),
    ('i', "kiwi"),
]
.into_iter()
.map(|(c, s)| (c, s.to_string()))
.collect();

assert_eq!(expected, lines);

Windows style line endings are not considered. This means '\r' will be evaluated as a normal character without any special meaning.

Important traits for PositionIterator<'t>

Important traits for ReversePositionIterator<'t>

Trait Implementations

impl<'_> Ord for Position<'_>
[src]

Compares and returns the maximum of two values. Read more

Compares and returns the minimum of two values. Read more

impl<'_> PartialEq<Position<'_>> for Position<'_>
[src]

This method tests for !=.

impl<'t> Clone for Position<'t>
[src]

Performs copy-assignment from source. Read more

impl<'t> Copy for Position<'t>
[src]

impl<'_> Eq for Position<'_>
[src]

impl<'_> PartialOrd<Position<'_>> for Position<'_>
[src]

This method tests less than (for self and other) and is used by the < operator. Read more

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more

This method tests greater than (for self and other) and is used by the > operator. Read more

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

impl<'_> Display for Position<'_>
[src]

impl<'_> Debug for Position<'_>
[src]

Auto Trait Implementations

impl<'t> Send for Position<'t>

impl<'t> Sync for Position<'t>

Blanket Implementations

impl<T> From for T
[src]

impl<T, U> Into for T where
    U: From<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

impl<T> ToString for T where
    T: Display + ?Sized
[src]

impl<T, U> TryFrom for T where
    T: From<U>, 
[src]

🔬 This is a nightly-only experimental API. (try_from)

The type returned in the event of a conversion error.

impl<T> Borrow for T where
    T: ?Sized
[src]

impl<T> BorrowMut for T where
    T: ?Sized
[src]

impl<T, U> TryInto for T where
    U: TryFrom<T>, 
[src]

🔬 This is a nightly-only experimental API. (try_from)

The type returned in the event of a conversion error.

impl<T> Any for T where
    T: 'static + ?Sized
[src]