Skip to content

Commit 66ca7dc

Browse files
committed
add ascii_fields method
1 parent 8904072 commit 66ca7dc

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed

src/ext_slice.rs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1101,12 +1101,42 @@ pub trait ByteSlice: private::Sealed {
11011101
///
11021102
/// assert_eq!(0, B(" \n\t\u{2003}\n \t").fields().count());
11031103
/// ```
1104+
#[doc(alias = "split_whitespace")]
11041105
#[cfg(feature = "unicode")]
11051106
#[inline]
11061107
fn fields(&self) -> Fields<'_> {
11071108
Fields::new(self.as_bytes())
11081109
}
11091110

1111+
/// Returns an iterator over the fields in a byte string, separated
1112+
/// by contiguous ASCII whitespace.
1113+
///
1114+
/// # Example
1115+
///
1116+
/// Basic usage:
1117+
///
1118+
/// ```
1119+
/// use bstr::{B, ByteSlice};
1120+
///
1121+
/// let s = B(" foo\tbar\t\nquux \n");
1122+
/// let ascii_fields: Vec<&[u8]> = s.ascii_fields().collect();
1123+
/// assert_eq!(ascii_fields, vec![B("foo"), B("bar"), B("quux")]);
1124+
/// ```
1125+
///
1126+
/// A byte string consisting of just whitespace yields no elements:
1127+
///
1128+
/// ```
1129+
/// use bstr::{B, ByteSlice};
1130+
///
1131+
/// assert_eq!(0, B(" \n\t\n \t").fields().count());
1132+
/// ```
1133+
#[doc(alias = "split_ascii_whitespace")]
1134+
#[cfg(feature = "unicode")]
1135+
#[inline]
1136+
fn ascii_fields(&self) -> AsciiFields<'_> {
1137+
AsciiFields::new(self.as_bytes())
1138+
}
1139+
11101140
/// Returns an iterator over the fields in a byte string, separated by
11111141
/// contiguous codepoints satisfying the given predicate.
11121142
///
@@ -3356,6 +3386,38 @@ impl<'a> ExactSizeIterator for Bytes<'a> {
33563386

33573387
impl<'a> iter::FusedIterator for Bytes<'a> {}
33583388

3389+
/// An iterator over the fields in a byte string, separated by ASCII whitespace.
3390+
///
3391+
/// This iterator splits on contiguous runs of whitespace, such that the fields
3392+
/// in `foo\t\t\n \nbar` are `foo` and `bar`.
3393+
///
3394+
/// `'a` is the lifetime of the byte string being split.
3395+
#[doc(alias = "SplitAsciiWhitespace")]
3396+
#[cfg(feature = "unicode")]
3397+
#[derive(Clone, Debug)]
3398+
pub struct AsciiFields<'a> {
3399+
it: FieldsWith<'a, fn(char) -> bool>,
3400+
}
3401+
3402+
#[cfg(feature = "unicode")]
3403+
impl<'a> AsciiFields<'a> {
3404+
fn new(bytes: &'a [u8]) -> AsciiFields<'a> {
3405+
AsciiFields {
3406+
it: bytes.fields_with(|x| char::is_ascii_whitespace(&x)),
3407+
}
3408+
}
3409+
}
3410+
3411+
#[cfg(feature = "unicode")]
3412+
impl<'a> Iterator for AsciiFields<'a> {
3413+
type Item = &'a [u8];
3414+
3415+
#[inline]
3416+
fn next(&mut self) -> Option<&'a [u8]> {
3417+
self.it.next()
3418+
}
3419+
}
3420+
33593421
/// An iterator over the fields in a byte string, separated by whitespace.
33603422
///
33613423
/// Whitespace for this iterator is defined by the Unicode property
@@ -3365,6 +3427,7 @@ impl<'a> iter::FusedIterator for Bytes<'a> {}
33653427
/// in `foo\t\t\n \nbar` are `foo` and `bar`.
33663428
///
33673429
/// `'a` is the lifetime of the byte string being split.
3430+
#[doc(alias = "SplitWhitespace")]
33683431
#[cfg(feature = "unicode")]
33693432
#[derive(Clone, Debug)]
33703433
pub struct Fields<'a> {

0 commit comments

Comments
 (0)