about summary refs log tree commit diff
path: root/src/libstd
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2013-06-29 21:13:31 -0700
committerbors <bors@rust-lang.org>2013-06-29 21:13:31 -0700
commitc6b0d4f51610401355a27eb2c5f607e913b74be9 (patch)
tree0aa2b362291d84ee93fcc0897fb4adf4f6fb55c8 /src/libstd
parent30173432325654ccd3353f09aa0c7ccd37c84391 (diff)
parent647b4a6bcd921507943a60fcc413f4ae69b2609c (diff)
downloadrust-c6b0d4f51610401355a27eb2c5f607e913b74be9.tar.gz
rust-c6b0d4f51610401355a27eb2c5f607e913b74be9.zip
auto merge of #7475 : Seldaek/rust/fixsplit, r=cmr
I almost got locked out of my machine because I misunderstood the purpose of the function and called it with a limit of uint::max_value, which turned this function into an almost endless loop.
Diffstat (limited to 'src/libstd')
-rw-r--r--src/libstd/str.rs11
1 files changed, 10 insertions, 1 deletions
diff --git a/src/libstd/str.rs b/src/libstd/str.rs
index a06d858e424..8e0b3b6ad35 100644
--- a/src/libstd/str.rs
+++ b/src/libstd/str.rs
@@ -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 };
@@ -466,7 +473,6 @@ pub fn each_split_within<'a>(ss: &'a str,
     ss.iter().enumerate().advance(|x| machine(x));
 
     // 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;
@@ -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};
@@ -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]