fn each_split_within<'a, F>(ss: &'a str, lim: usize, mut it: F)
                            -> bool where F: FnMut(&'a str) -> bool {
    // Just for fun, let's write this as a state machine:

    let mut slice_start = 0;
    let mut last_start = 0;
    let mut last_end = 0;
    let mut state = A;
    let mut fake_i = ss.len();
    let mut lim = lim;

    let mut cont = true;

    // if the limit is larger than the string, lower it to save cycles
    if lim >= fake_i {
        lim = fake_i;
    }

    let mut machine = |cont: &mut bool, (i, c): (usize, char)| {
        let whitespace = if c.is_whitespace() { Ws }       else { Cr };
        let limit      = if (i - slice_start + 1) <= lim  { UnderLim } else { OverLim };

        state = match (state, whitespace, limit) {
            (A, Ws, _)        => { A }
            (A, Cr, _)        => { slice_start = i; last_start = i; B }

            (B, Cr, UnderLim) => { B }
            (B, Cr, OverLim)  if (i - last_start + 1) > lim
                            => panic!("word starting with {} longer than limit!",
                                      &ss[last_start..i + 1]),
            (B, Cr, OverLim)  => {
                *cont = it(&ss[slice_start..last_end]);
                slice_start = last_start;
                B
            }
            (B, Ws, UnderLim) => {
                last_end = i;
                C
            }
            (B, Ws, OverLim)  => {
                last_end = i;
                *cont = it(&ss[slice_start..last_end]);
                A
            }

            (C, Cr, UnderLim) => {
                last_start = i;
                B
            }
            (C, Cr, OverLim)  => {
                *cont = it(&ss[slice_start..last_end]);
                slice_start = i;
                last_start = i;
                last_end = i;
                B
            }
            (C, Ws, OverLim)  => {
                *cont = it(&ss[slice_start..last_end]);
                A
            }
            (C, Ws, UnderLim) => {
                C
            }
        };

        *cont
    };

    ss.char_indices().all(|x| machine(&mut cont, x));

    // Let the automaton 'run out' by supplying trailing whitespace
    while cont && match state { B | C => true, A => false } {
        machine(&mut cont, (fake_i, ' '));
        fake_i += 1;
    }
    return cont;
}
/// Splits a string into substrings with possibly internal whitespace,
/// each of them at most `lim` bytes long. The substrings have leading and trailing
/// whitespace removed, and are only cut at whitespace boundaries.
///
/// Note: Function was moved here from `std::str` because this module is the only place that
/// uses it, and because it was too specific for a general string function.
///
/// # Panics
///
/// Panics during iteration if the string contains a non-whitespace
/// sequence longer than the limit.
Splits a string into substrings with possibly internal whitespace,
each of them at most `lim` bytes long. The substrings have leading and trailing
whitespace removed, and are only cut at whitespace boundaries.

Function was moved here from `std::str` because this module is the only place that
uses it, and because it was too specific for a general string function.
Splits a string into substrings with possibly internal whitespace each of them at most ` lim ` bytes long . The substrings have leading and trailing whitespace removed and are only cut at whitespace boundaries . Function was moved here from ` std :: str ` because this module is the only place that uses it and because it was too specific for a general string function .
fn each_split_within<'a, F>(ss: &'a str, lim: usize, mut it: F)
                            -> bool where F: FnMut(&'a str) -> bool {
    // Just for fun, let's write this as a state machine:
    let mut slice_start = 0;
    let mut last_start = 0;
    let mut last_end = 0;
    let mut state = A;
    let mut fake_i = ss.len();
    let mut lim = lim;
    let mut cont = true;
    // if the limit is larger than the string, lower it to save cycles
    if lim >= fake_i {
        lim = fake_i;
    }
    let mut machine = |cont: &mut bool, (i, c): (usize, char)| {
        let whitespace = if c.is_whitespace() { Ws }       else { Cr };
        let limit      = if (i - slice_start + 1) <= lim  { UnderLim } else { OverLim };
        state = match (state, whitespace, limit) {
            (A, Ws, _)        => { A }
            (A, Cr, _)        => { slice_start = i; last_start = i; B }
            (B, Cr, UnderLim) => { B }
            (B, Cr, OverLim)  if (i - last_start + 1) > lim
                            => panic!("word starting with {} longer than limit!",
                                      &ss[last_start..i + 1]),
            (B, Cr, OverLim)  => {
                *cont = it(&ss[slice_start..last_end]);
                slice_start = last_start;
                B
            }
            (B, Ws, UnderLim) => {
                last_end = i;
                C
            }
            (B, Ws, OverLim)  => {
                last_end = i;
                *cont = it(&ss[slice_start..last_end]);
                A
            }
            (C, Cr, UnderLim) => {
                last_start = i;
                B
            }
            (C, Cr, OverLim)  => {
                *cont = it(&ss[slice_start..last_end]);
                slice_start = i;
                last_start = i;
                last_end = i;
                B
            }
            (C, Ws, OverLim)  => {
                *cont = it(&ss[slice_start..last_end]);
                A
            }
            (C, Ws, UnderLim) => {
                C
            }
        };
        *cont
    };
    ss.char_indices().all(|x| machine(&mut cont, x));
    // Let the automaton 'run out' by supplying trailing whitespace
    while cont && match state { B | C => true, A => false } {
        machine(&mut cont, (fake_i, ' '));
        fake_i += 1;
    }
    return cont;
}
fn each_split_within < ' a , F > ( ss : & ' a str , lim : usize , mut it : F ) -> bool where F : FnMut ( & ' a str ) -> bool { // Just for fun, let's write this as a state machine: let mut slice_start = 0 ; let mut last_start = 0 ; let mut last_end = 0 ; let mut state = A ; let mut fake_i = ss . len ( ) ; let mut lim = lim ; let mut cont = true ; // if the limit is larger than the string, lower it to save cycles if lim >= fake_i { lim = fake_i ; } let mut machine = | cont : & mut bool , ( i , c ) : ( usize , char ) | { let whitespace = if c . is_whitespace ( ) { Ws } else { Cr } ; let limit = if ( i - slice_start + 1 ) <= lim { UnderLim } else { OverLim } ; state = match ( state , whitespace , limit ) { ( A , Ws , _ ) => { A } ( A , Cr , _ ) => { slice_start = i ; last_start = i ; B } ( B , Cr , UnderLim ) => { B } ( B , Cr , OverLim ) if ( i - last_start + 1 ) > lim => panic ! ( " " ss [ last_start i 1 ] ) , ( B , Cr , OverLim ) => { * cont = it ( & ss [ slice_start .. last_end ] ) ; slice_start = last_start ; B } ( B , Ws , UnderLim ) => { last_end = i ; C } ( B , Ws , OverLim ) => { last_end = i ; * cont = it ( & ss [ slice_start .. last_end ] ) ; A } ( C , Cr , UnderLim ) => { last_start = i ; B } ( C , Cr , OverLim ) => { * cont = it ( & ss [ slice_start .. last_end ] ) ; slice_start = i ; last_start = i ; last_end = i ; B } ( C , Ws , OverLim ) => { * cont = it ( & ss [ slice_start .. last_end ] ) ; A } ( C , Ws , UnderLim ) => { C } } ; * cont } ; ss . char_indices ( ) . all ( | x | machine ( & mut cont , x ) ) ; // Let the automaton 'run out' by supplying trailing whitespace while cont && match state { B | C => true , A => false } { machine ( & mut cont , ( fake_i , ' ' ) ) ; fake_i += 1 ; } return cont ; }
Splits a string into substrings with possibly internal whitespace,
each of them at most `lim` bytes long.
Splits a string into substrings with possibly internal whitespace each of them at most ` lim ` bytes long .
ss
&'a str
lim
usize
it
F
ss
&'a str
lim
usize
it
F
panics
Panics during iteration if the string contains a non-whitespace
sequence longer than the limit.
Panics during iteration if the string contains a non - whitespace sequence longer than the limit .