Skip to content

Commit

Permalink
Optimize str::each_split_within when it is called with large limits
Browse files Browse the repository at this point in the history
  • Loading branch information
Seldaek committed Jun 29, 2013
1 parent e239346 commit 647b4a6
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/libstd/str.rs
Expand Up @@ -434,10 +434,17 @@ pub fn each_split_within<'a>(ss: &'a str,
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;
let slice: &fn() = || { cont = it(ss.slice(slice_start, last_end)) };

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

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 };
Expand Down Expand Up @@ -466,7 +473,6 @@ pub fn each_split_within<'a>(ss: &'a str,
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;
Expand Down Expand Up @@ -2299,6 +2305,7 @@ mod tests {
use libc;
use ptr;
use str::*;
use uint;
use vec;
use vec::{ImmutableVector, CopyableVector};
use cmp::{TotalOrd, Less, Equal, Greater};
Expand Down Expand Up @@ -2444,6 +2451,8 @@ mod tests {
t("hello", 15, [~"hello"]);
t("\nMary had a little lamb\nLittle lamb\n", 15,
[~"Mary had a", ~"little lamb", ~"Little lamb"]);
t("\nMary had a little lamb\nLittle lamb\n", uint::max_value,
[~"Mary had a little lamb\nLittle lamb"]);
}

#[test]
Expand Down

5 comments on commit 647b4a6

@bors
Copy link
Contributor

@bors bors commented on 647b4a6 Jun 30, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from cmr
at Seldaek@647b4a6

@bors
Copy link
Contributor

@bors bors commented on 647b4a6 Jun 30, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging Seldaek/rust/fixsplit = 647b4a6 into auto

@bors
Copy link
Contributor

@bors bors commented on 647b4a6 Jun 30, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seldaek/rust/fixsplit = 647b4a6 merged ok, testing candidate = c6b0d4f

@bors
Copy link
Contributor

@bors bors commented on 647b4a6 Jun 30, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-forwarding master to auto = c6b0d4f

Please sign in to comment.