[][src]Struct nom5_locate::LocatedSpan

pub struct LocatedSpan<T> {
    pub offset: usize,
    pub line: u32,
    pub fragment: T,
}

A LocatedSpan is a set of meta information about the location of a token.

The LocatedSpan structure can be used as an input of the nom parsers. It implements all the necessary traits for LocatedSpan<&str> and LocatedSpan<&[u8]>

Fields

offset: usize

The offset represents the position of the fragment relatively to the input of the parser. It starts at offset 0.

line: u32

The line number of the fragment relatively to the input of the parser. It starts at line 1.

fragment: T

The fragment that is spanned. The fragment represents a part of the input of the parser.

Methods

impl<T: AsBytes> LocatedSpan<T>[src]

pub fn new(program: T) -> LocatedSpan<T>[src]

Create a span for a particular input with default offset and line values. You can compute the column through the get_column or get_utf8_column methods.

offset starts at 0, line starts at 1, and column starts at 1.

Example of use

use nom_locate::LocatedSpan;

let span = LocatedSpan::new(b"foobar");

assert_eq!(span.offset,         0);
assert_eq!(span.line,           1);
assert_eq!(span.get_column(),   1);
assert_eq!(span.fragment,       &b"foobar"[..]);

pub fn get_column(&self) -> usize[src]

Return the column index, assuming 1 byte = 1 column.

Use it for ascii text, or use get_utf8_column for UTF8.

Example of use


let span = LocatedSpan::new("foobar");

assert_eq!(span.slice(3..).get_column(), 4);

pub fn get_utf8_column(&self) -> usize[src]

Return the column index for UTF8 text. Return value is unspecified for non-utf8 text.

This version uses bytecount's hyper algorithm to count characters. This is much faster for long lines, but is non-negligibly slower for short slices (below around 100 bytes). This is also sped up significantly more depending on architecture and enabling the simd feature gates. If you expect primarily short lines, you may get a noticeable speedup in parsing by using naive_get_utf8_column instead. Benchmark your specific use case!

Example of use


let span = LocatedSpan::new("メカジキ");
let indexOf3dKanji = span.find_substring("ジ").unwrap();

assert_eq!(span.slice(indexOf3dKanji..).get_column(), 7);
assert_eq!(span.slice(indexOf3dKanji..).get_utf8_column(), 3);

pub fn naive_get_utf8_column(&self) -> usize[src]

Return the column index for UTF8 text. Return value is unspecified for non-utf8 text.

A simpler implementation of get_utf8_column that may be faster on shorter lines. If benchmarking shows that this is faster, you can use it instead of get_utf8_column. Prefer defaulting to get_utf8_column unless this legitimately is a performance bottleneck.

Example of use


let span = LocatedSpan::new("メカジキ");
let indexOf3dKanji = span.find_substring("ジ").unwrap();

assert_eq!(span.slice(indexOf3dKanji..).get_column(), 7);
assert_eq!(span.slice(indexOf3dKanji..).naive_get_utf8_column(), 3);

Trait Implementations

impl<T: PartialEq> PartialEq<LocatedSpan<T>> for LocatedSpan<T>[src]

impl<T: Clone> Clone for LocatedSpan<T>[src]

fn clone_from(&mut self, source: &Self)1.0.0[src]

Performs copy-assignment from source. Read more

impl<T: ToString> ToString for LocatedSpan<T>[src]

impl<T: Copy> Copy for LocatedSpan<T>[src]

impl<T: Debug> Debug for LocatedSpan<T>[src]

impl<'a> HexDisplay for LocatedSpan<&'a str>[src]

impl<'a> HexDisplay for LocatedSpan<&'a [u8]>[src]

impl<T: InputLength> InputLength for LocatedSpan<T>[src]

impl<T> Offset for LocatedSpan<T>[src]

impl<'a> InputIter for LocatedSpan<&'a str>[src]

type Item = char

the current input type is a sequence of that Item type. Read more

type Iter = CharIndices<'a>

an iterator over the input type, producing the item and its position for use with [Slice]. If we're iterating over &str, the position corresponds to the byte index of the character Read more

type IterElem = Chars<'a>

an iterator over the input type, producing the item

impl<'a> InputIter for LocatedSpan<&'a [u8]>[src]

type Item = u8

the current input type is a sequence of that Item type. Read more

type Iter = Enumerate<Self::IterElem>

an iterator over the input type, producing the item and its position for use with [Slice]. If we're iterating over &str, the position corresponds to the byte index of the character Read more

type IterElem = Map<Iter<'a, Self::Item>, fn(_: &u8) -> u8>

an iterator over the input type, producing the item

impl<T> InputTake for LocatedSpan<T> where
    Self: Slice<RangeFrom<usize>> + Slice<RangeTo<usize>>, 
[src]

impl<T> InputTakeAtPosition for LocatedSpan<T> where
    T: InputTakeAtPosition + InputLength + InputIter + Debug,
    Self: Slice<RangeFrom<usize>> + Slice<RangeTo<usize>> + Clone
[src]

type Item = <T as InputIter>::Item

the current input type is a sequence of that Item type. Read more

impl<'a, 'b> Compare<&'a str> for LocatedSpan<&'b str>[src]

impl<'a, 'b> Compare<&'a [u8]> for LocatedSpan<&'b [u8]>[src]

impl<'a, 'b> Compare<&'a str> for LocatedSpan<&'b [u8]>[src]

impl<A: Compare<B>, B> Compare<LocatedSpan<B>> for LocatedSpan<A>[src]

impl<Fragment: FindToken<Token>, Token> FindToken<Token> for LocatedSpan<Fragment>[src]

impl<'a, T> FindSubstring<&'a str> for LocatedSpan<T> where
    T: FindSubstring<&'a str>, 
[src]

impl<R: FromStr, T> ParseTo<R> for LocatedSpan<T> where
    T: ParseTo<R>, 
[src]

impl<'a> Slice<Range<usize>> for LocatedSpan<&'a str>[src]

impl<'a> Slice<RangeTo<usize>> for LocatedSpan<&'a str>[src]

impl<'a> Slice<RangeFrom<usize>> for LocatedSpan<&'a str>[src]

impl<'a> Slice<RangeFull> for LocatedSpan<&'a str>[src]

impl<'a> Slice<Range<usize>> for LocatedSpan<&'a [u8]>[src]

impl<'a> Slice<RangeTo<usize>> for LocatedSpan<&'a [u8]>[src]

impl<'a> Slice<RangeFrom<usize>> for LocatedSpan<&'a [u8]>[src]

impl<'a> Slice<RangeFull> for LocatedSpan<&'a [u8]>[src]

impl<'a> ExtendInto for LocatedSpan<&'a str>[src]

type Item = char

the current input type is a sequence of that Item type. Read more

type Extender = String

the type that will be produced

impl<'a> ExtendInto for LocatedSpan<&'a [u8]>[src]

type Item = u8

the current input type is a sequence of that Item type. Read more

type Extender = Vec<u8>

the type that will be produced

Auto Trait Implementations

impl<T> Unpin for LocatedSpan<T> where
    T: Unpin

impl<T> Sync for LocatedSpan<T> where
    T: Sync

impl<T> Send for LocatedSpan<T> where
    T: Send

impl<T> RefUnwindSafe for LocatedSpan<T> where
    T: RefUnwindSafe

impl<T> UnwindSafe for LocatedSpan<T> where
    T: UnwindSafe

Blanket Implementations

impl<T> From<T> for T[src]

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

type Owned = T

The resulting type after obtaining ownership.

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

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

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

type Error = Infallible

The type returned in the event of a conversion error.

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

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

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

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

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

impl<T> InputTakeAtPosition for T where
    T: InputLength + InputIter + InputTake + UnspecializedInput + Clone
[src]

type Item = <T as InputIter>::Item

the current input type is a sequence of that Item type. Read more