about summary refs log tree commit diff
diff options
context:
space:
mode:
authorKevin Cantu <me@kevincantu.org>2012-01-23 03:02:44 -0800
committerNiko Matsakis <niko@alum.mit.edu>2012-01-23 22:28:25 -0800
commitff9502917125f3bfb8ebad252a1c8d3a4f6e0320 (patch)
tree22a17b75325639d51c176ce36f4f29def6917905
parentd4b287e8525758896e78b39eca6f6e1c0f32cd98 (diff)
downloadrust-ff9502917125f3bfb8ebad252a1c8d3a4f6e0320.tar.gz
rust-ff9502917125f3bfb8ebad252a1c8d3a4f6e0320.zip
Added str::words_iter
-rw-r--r--src/libcore/str.rs36
1 files changed, 34 insertions, 2 deletions
diff --git a/src/libcore/str.rs b/src/libcore/str.rs
index df564c7bded..6dda818624c 100644
--- a/src/libcore/str.rs
+++ b/src/libcore/str.rs
@@ -14,7 +14,7 @@ export eq, lteq, hash, is_empty, is_not_empty, is_whitespace, byte_len,
        char_at, bytes, is_ascii, shift_byte, pop_byte,
        unsafe_from_byte, unsafe_from_bytes, from_char, char_range_at,
        from_cstr, sbuf, as_buf, push_byte, utf8_char_width, safe_slice,
-       contains, iter_chars, chars_iter, bytes_iter,
+       contains, iter_chars, chars_iter, bytes_iter, words_iter,
        loop_chars, loop_chars_sub, escape, any, all, map, windowed;
 
 #[abi = "cdecl"]
@@ -915,6 +915,15 @@ fn words(ss: str) -> [str] {
 }
 
 /*
+Function: words_iter
+
+Apply a function to each word
+*/
+fn words_iter(ss: str, ff: fn&(&&str)) {
+   vec::iter(words(ss), ff)
+}
+
+/*
 Function: concat
 
 Concatenate a vector of strings
@@ -1649,7 +1658,7 @@ mod tests {
             i += 1;
         }
 
-        iter_chars("") {|ch| fail; } // should not fail
+        iter_chars("") {|_ch| fail; } // should not fail
     }
 
     #[test]
@@ -1684,6 +1693,29 @@ mod tests {
     }
 
     #[test]
+    fn test_words_iter() {
+        let data = "\nMary had a little lamb\nLittle lamb\n";
+
+        let ii = 0;
+
+        words_iter(data) {|ww|
+            alt ii {
+              0 { assert "Mary"   == ww; }
+              1 { assert "had"    == ww; }
+              2 { assert "a"      == ww; }
+              3 { assert "little" == ww; }
+              _ { () }
+            }
+            ii += 1;
+        }
+    }
+
+    #[test]
+    fn test_words_iter_() {
+        words_iter("") {|_ww| fail; } // should not fail
+    }
+
+    #[test]
     fn test_escape() {
         assert(escape("abcdef") == "abcdef");
         assert(escape("abc\\def") == "abc\\\\def");