@@ -434,10 +434,17 @@ pub fn each_split_within<'a>(ss: &'a str,
434434 let mut last_start = 0 ;
435435 let mut last_end = 0 ;
436436 let mut state = A ;
437+ let mut fake_i = ss. len ( ) ;
438+ let mut lim = lim;
437439
438440 let mut cont = true ;
439441 let slice: & fn ( ) = || { cont = it ( ss. slice ( slice_start, last_end) ) } ;
440442
443+ // if the limit is larger than the string, lower it to save cycles
444+ if ( lim >= fake_i) {
445+ lim = fake_i;
446+ }
447+
441448 let machine: & fn ( ( uint , char ) ) -> bool = |( i, c) | {
442449 let whitespace = if char:: is_whitespace ( c) { Ws } else { Cr } ;
443450 let limit = if ( i - slice_start + 1 ) <= lim { UnderLim } else { OverLim } ;
@@ -466,7 +473,6 @@ pub fn each_split_within<'a>(ss: &'a str,
466473 ss. iter ( ) . enumerate ( ) . advance ( machine) ;
467474
468475 // Let the automaton 'run out' by supplying trailing whitespace
469- let mut fake_i = ss. len ( ) ;
470476 while cont && match state { B | C => true , A => false } {
471477 machine ( ( fake_i, ' ' ) ) ;
472478 fake_i += 1 ;
@@ -2299,6 +2305,7 @@ mod tests {
22992305 use libc;
23002306 use ptr;
23012307 use str::*;
2308+ use uint;
23022309 use vec;
23032310 use vec::{ImmutableVector, CopyableVector};
23042311 use cmp::{TotalOrd, Less, Equal, Greater};
@@ -2444,6 +2451,8 @@ mod tests {
24442451 t(" hello", 15, [~" hello"]);
24452452 t("\n Mary had a little lamb\n Little lamb\n " , 15 ,
24462453 [ ~"Mary had a", ~" little lamb", ~" Little lamb"]);
2454+ t("\n Mary had a little lamb\n Little lamb\n " , uint:: max_value,
2455+ [ ~"Mary had a little lamb\n Little lamb"]);
24472456 }
24482457
24492458 #[test]
0 commit comments