|0.1.1||Mar 20, 2021|
|0.1.0||Mar 14, 2021|
#1868 in Rust patterns
Helps parse strings that use delimeters to separate values.
The following retrieves a value wrapped by matched delimiters:
assert_eq!(Delimited::matched("abc:12:def", ":"), Some(12));
This retrieves a value wrapped within mismatched patterns:
assert_eq!(Delimited::mismatched("abc<<12*&def", "<<", "*&"), Some(12));
The following shows how using a Delimited instance along with consume_matched can be used to extract two different delimited values from a string.
let mut d = Delimited::new("aaa :12: bbb :34: ccc"); // Consume first value (expecting 12) let first = d.consume_matched(":"); // Consume second value (expecting 34) let second = d.consume_matched(":"); assert_eq!(first, Some(12)); assert_eq!(second, Some(34));
Delimited Pattern Types
There are three primary delimited pattern types:
- Matched delimiters
- Mismatched delimiters
Additionally, a number of bytes can be consumed raw (without delimiters) and either returned as a string or parsed to a specified type.
Raw Bytes Example
In the following string, the value 12 occupies the first two bytes.
The first two bytes can be consumed as a u8 with the following:
let d = Delimited::new("12abc"); assert_eq!(d.bytes::<u8>(2), Some(12));
Matched Delimiters Example
In the following string, the value 12 is wrapped by matched ":" delimiters.
Mismatched Delimiters Example
In this string, the value 12 is wrapped by mismatched ":" and "<" delimiters.
In this string, we decide that the value 12 is prefixed by |.
abc|12def Prefixed delimiter searches requires the value length to be specified.