pub fn each_split_within<'a>(ss: &'a str,
                              lim: uint,
                              it: &fn(&'a str) -> bool) -> bool {
    // Just for fun, let's write this as an state machine:

    enum SplitWithinState {
        A,  // leading whitespace, initial state
        B,  // words
        C,  // internal and trailing whitespace
    }
    enum Whitespace {
        Ws, // current char is whitespace
        Cr  // current char is not whitespace
    }
    enum LengthLimit {
        UnderLim, // current char makes current substring still fit in limit
        OverLim   // current char makes current substring no longer fit in limit
    }

    let mut slice_start = 0;
    let mut last_start = 0;
    let mut last_end = 0;
    let mut state = A;

    let mut cont = true;
    let slice: &fn() = || { cont = it(ss.slice(slice_start, last_end)) };

    let machine: &fn((uint, char)) -> bool = |(i, c)| {
        let whitespace = if char::is_whitespace(c)       { 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
                              => fail!("word starting with %? longer than limit!",
                                       ss.slice(last_start, i + 1)),
            (B, Cr, OverLim)  => { slice(); slice_start = last_start; B }
            (B, Ws, UnderLim) => { last_end = i; C }
            (B, Ws, OverLim)  => { last_end = i; slice(); A }

            (C, Cr, UnderLim) => { last_start = i; B }
            (C, Cr, OverLim)  => { slice(); slice_start = i; last_start = i; last_end = i; B }
            (C, Ws, OverLim)  => { slice(); A }
            (C, Ws, UnderLim) => { C }
        };

        cont
    };

    ss.iter().enumerate().advance(machine);

    // Let the automaton 'run out' by supplying trailing whitespace
    let mut fake_i = ss.len();
    while cont && match state { B | C => true, A => false } {
        machine((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.
 *
 *  #Failure:
 *
 *  Fails 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.

#Failure.

Fails 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 . #Failure . Fails during iteration if the string contains a non - whitespace sequence longer than the limit .
pub fn each_split_within<'a>(ss: &'a str,
                              lim: uint,
                              it: &fn(&'a str) -> bool) -> bool {
    // Just for fun, let's write this as an state machine:
    enum SplitWithinState {
        A,  // leading whitespace, initial state
        B,  // words
        C,  // internal and trailing whitespace
    }
    enum Whitespace {
        Ws, // current char is whitespace
        Cr  // current char is not whitespace
    }
    enum LengthLimit {
        UnderLim, // current char makes current substring still fit in limit
        OverLim   // current char makes current substring no longer fit in limit
    }
    let mut slice_start = 0;
    let mut last_start = 0;
    let mut last_end = 0;
    let mut state = A;
    let mut cont = true;
    let slice: &fn() = || { cont = it(ss.slice(slice_start, last_end)) };
    let machine: &fn((uint, char)) -> bool = |(i, c)| {
        let whitespace = if char::is_whitespace(c)       { 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
                              => fail!("word starting with %? longer than limit!",
                                       ss.slice(last_start, i + 1)),
            (B, Cr, OverLim)  => { slice(); slice_start = last_start; B }
            (B, Ws, UnderLim) => { last_end = i; C }
            (B, Ws, OverLim)  => { last_end = i; slice(); A }
            (C, Cr, UnderLim) => { last_start = i; B }
            (C, Cr, OverLim)  => { slice(); slice_start = i; last_start = i; last_end = i; B }
            (C, Ws, OverLim)  => { slice(); A }
            (C, Ws, UnderLim) => { C }
        };
        cont
    };
    ss.iter().enumerate().advance(machine);
    // Let the automaton 'run out' by supplying trailing whitespace
    let mut fake_i = ss.len();
    while cont && match state { B | C => true, A => false } {
        machine((fake_i, ' '));
        fake_i += 1;
    }
    return cont;
}
pub fn each_split_within < ' a > ( ss : & ' a str , lim : uint , it : & fn ( & ' a str ) -> bool ) -> bool { // Just for fun, let's write this as an state machine: enum SplitWithinState { A , // leading whitespace, initial state B , // words C , // internal and trailing whitespace } enum Whitespace { Ws , // current char is whitespace Cr // current char is not whitespace } enum LengthLimit { UnderLim , // current char makes current substring still fit in limit OverLim // current char makes current substring no longer fit in limit } let mut slice_start = 0 ; let mut last_start = 0 ; let mut last_end = 0 ; let mut state = A ; let mut cont = true ; let slice : & fn ( ) = | | { cont = it ( ss . slice ( slice_start , last_end ) ) } ; let machine : & fn ( ( uint , char ) ) -> bool = | ( i , c ) | { let whitespace = if char :: is_whitespace ( c ) { 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 => fail ! ( " " ss slice ( last_start i 1 ) ) , ( B , Cr , OverLim ) => { slice ( ) ; slice_start = last_start ; B } ( B , Ws , UnderLim ) => { last_end = i ; C } ( B , Ws , OverLim ) => { last_end = i ; slice ( ) ; A } ( C , Cr , UnderLim ) => { last_start = i ; B } ( C , Cr , OverLim ) => { slice ( ) ; slice_start = i ; last_start = i ; last_end = i ; B } ( C , Ws , OverLim ) => { slice ( ) ; A } ( C , Ws , UnderLim ) => { C } } ; cont } ; ss . iter ( ) . enumerate ( ) . advance ( machine ) ; // Let the automaton 'run out' by supplying trailing whitespace let mut fake_i = ss . len ( ) ; while cont && match state { B | C => true , A => false } { machine ( ( 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
uint
it
&fn(&'a str) -> bool