Skip to content

Commit

Permalink
BREAKING: put 'ByteSlice::fields' behind 'unicode' feature
Browse files Browse the repository at this point in the history
It currently uses 'char::is_whitespace', but this is more of an
implementation detail. While 'char::is_whitespace' is available in
'core', it's plausible that we might use our own data some data. In
particular, 'trim' already uses its own data.

I believe this is the only routine that makes direct use of some kind of
Unicode data that wasn't previously gated behind the 'unicode' feature.

Ref #40
  • Loading branch information
BurntSushi committed Jul 5, 2022
1 parent 3256d07 commit a73aab1
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/ext_slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1053,8 +1053,9 @@ pub trait ByteSlice: Sealed {
byteset::rfind_not(self.as_bytes(), byteset.as_ref())
}

/// Returns an iterator over the fields in a byte string, separated by
/// contiguous whitespace.
/// Returns an iterator over the fields in a byte string, separated
/// by contiguous whitespace (according to the Unicode property
/// `White_Space`).
///
/// # Example
///
Expand All @@ -1075,6 +1076,7 @@ pub trait ByteSlice: Sealed {
///
/// assert_eq!(0, B(" \n\t\u{2003}\n \t").fields().count());
/// ```
#[cfg(feature = "unicode")]
#[inline]
fn fields(&self) -> Fields<'_> {
Fields::new(self.as_bytes())
Expand Down Expand Up @@ -3356,21 +3358,27 @@ impl<'a> iter::FusedIterator for Bytes<'a> {}

/// An iterator over the fields in a byte string, separated by whitespace.
///
/// Whitespace for this iterator is defined by the Unicode property
/// `White_Space`.
///
/// This iterator splits on contiguous runs of whitespace, such that the fields
/// in `foo\t\t\n \nbar` are `foo` and `bar`.
///
/// `'a` is the lifetime of the byte string being split.
#[cfg(feature = "unicode")]
#[derive(Debug)]
pub struct Fields<'a> {
it: FieldsWith<'a, fn(char) -> bool>,
}

#[cfg(feature = "unicode")]
impl<'a> Fields<'a> {
fn new(bytes: &'a [u8]) -> Fields<'a> {
Fields { it: bytes.fields_with(|ch| ch.is_whitespace()) }
}
}

#[cfg(feature = "unicode")]
impl<'a> Iterator for Fields<'a> {
type Item = &'a [u8];

Expand Down

0 comments on commit a73aab1

Please sign in to comment.