Skip to main content

Crate escaped_delimiter

Crate escaped_delimiter 

Source
Expand description

escaped-delimiter provides an iterator of a delimited slice, considering an escape character.

See the examples below.

§Examples

§Iterator

An Iter implements both Iterator and DoubleEndedIterator.

use escaped_delimiter::iter;

// Without escape characters
let s = b"The quick brown fox";
let s_vec: Vec<_> = iter(s, b' ', b'\\').collect();
assert_eq!(s_vec, &[&b"The"[..], &b"quick"[..], &b"brown"[..], &b"fox"[..]]);

// Reverse it (`DoubleEndedIterator`)
let s = b"The quick brown fox";
let s_vec: Vec<_> = iter(s, b' ', b'\\').rev().collect();
assert_eq!(s_vec, &[&b"fox"[..], &b"brown"[..], &b"quick"[..], &b"The"[..]]);

// With escape characters
let s = b"a\\ b\\\\ c\\\\\\ d\\\\\\\\ e";
let s_vec: Vec<_> = iter(s, b' ', b'\\').collect();
assert_eq!(s_vec, &[&b"a\\ b\\\\"[..], &b"c\\\\\\ d\\\\\\\\"[..], &b"e"[..]]);

§Cursor

Unlike an Iter, a Cursor can move forward/backward relative to the current position.

use escaped_delimiter::cursor;

let s = b"The quick brown fox";
let mut cursor = cursor(s, b' ', b'\\');
assert_eq!(cursor.curr(), b"The");

cursor.move_next();
assert_eq!(cursor.curr(), b"quick");

cursor.move_next();
assert_eq!(cursor.curr(), b"brown");

cursor.move_prev();
assert_eq!(cursor.curr(), b"quick");

cursor.move_next();
assert_eq!(cursor.curr(), b"brown");

cursor.move_next();
assert_eq!(cursor.curr(), b"fox");

cursor.move_next();
assert_eq!(cursor.curr(), b"");

cursor.move_prev();
assert_eq!(cursor.curr(), b"fox");

Of course, a Cursor recognizes an escape characters properly.

use escaped_delimiter::cursor;

let s = b"a\\ b\\\\ c\\\\\\ d\\\\\\\\ e";
let mut cursor = cursor(s, b' ', b'\\');
assert_eq!(cursor.curr(), b"a\\ b\\\\");

cursor.move_next();
assert_eq!(cursor.curr(), b"c\\\\\\ d\\\\\\\\");

cursor.move_next();
assert_eq!(cursor.curr(), b"e");

cursor.move_next();
assert_eq!(cursor.curr(), b"");

Structs§

Cursor
Cursor over a delimited string with an escape character. See the top-level documentation.
Iter
Iterator over a delimited string with an escape character. See the top-level documentation.

Enums§

CursorPosition

Functions§

cursor
Alias of Cursor::from_slice().
iter
Alias of Iter::from_slice().