Crates.io | rangecutter |
lib.rs | rangecutter |
version | 0.1.0 |
source | src |
created_at | 2024-04-18 02:02:37.554019 |
updated_at | 2024-04-18 02:02:37.554019 |
description | Utilities for working with adjacent ranges |
homepage | |
repository | https://github.com/literallyvoid/rangecutter |
max_upload_size | |
id | 1212038 |
size | 9,576 |
Utilities for working with adjacent ranges.
use std::ops::Range;
use rangecutter::RangeExt;
let source = "Line 1\nLine 2\n";
fn first_newline(s: &str) -> Option<Range<usize>> {
let sep = '\n';
let start = s.find(sep)?;
let end = start + sep.len_utf8();
Some(start..end)
}
let mut cursor = 0..source.len();
let lines_with_endings = std::iter::from_fn(|| {
let separator = match first_newline(&source[cursor.clone()]) {
Some(newline) => cursor.compose(&newline),
None if !cursor.is_empty() => {
cursor.end..cursor.end
}
None => return None,
};
let line;
(line, cursor) = cursor.cut(&separator);
let line_with_separator = line.concat(&separator);
Some(&source[line_with_separator])
}).collect::<Vec<_>>();
assert_eq!(
lines_with_endings,
[
"Line 1\n",
"Line 2\n",
],
);
Only fully-bounded inclusive ranges (Range<T>
) are supported.
License: MIT