diff --git a/src/cargo/pgp.rs b/src/cargo/pgp.rs index 7756f08e8f440..2c39cebce4cd0 100644 --- a/src/cargo/pgp.rs +++ b/src/cargo/pgp.rs @@ -1,5 +1,7 @@ use std; +import str::iterable; + fn gpg(args: [str]) -> { status: int, out: str, err: str } { ret run::program_output("gpg", args); } @@ -91,10 +93,5 @@ fn verify(root: str, data: str, sig: str, keyfp: str) -> bool { let p = gpg(["--homedir", path, "--with-fingerprint", "--verify", sig, data]); let res = "Primary key fingerprint: " + keyfp; - for line in str::split_char(p.err, '\n') { - if line == res { - ret true; - } - } - ret false; + ret iter::any(str::by_lines(p.err)) {|&&line| line == res }; } diff --git a/src/compiletest/runtest.rs b/src/compiletest/runtest.rs index 19cc968ab7e27..23099b94a4c4c 100644 --- a/src/compiletest/runtest.rs +++ b/src/compiletest/runtest.rs @@ -8,6 +8,7 @@ import common::config; import header::load_props; import header::test_props; import util::logv; +import str::iterable; export run; @@ -195,7 +196,7 @@ fn check_error_patterns(props: test_props, let next_err_idx = 0u; let next_err_pat = props.error_patterns[next_err_idx]; - for line: str in str::split_char(procres.stderr, '\n') { + iter::each(str::by_lines(procres.stderr)) {|line| if str::contains(line, next_err_pat) { #debug("found error pattern %s", next_err_pat); next_err_idx += 1u; @@ -243,7 +244,7 @@ fn check_expected_errors(expected_errors: [errors::expected_error], // filename:line1:col1: line2:col2: *warning:* msg // where line1:col1: is the starting point, line2:col2: // is the ending point, and * represents ANSI color codes. - for line: str in str::split_char(procres.stderr, '\n') { + iter::each(str::by_lines(procres.stderr)) {|line| let was_expected = false; vec::iteri(expected_errors) {|i, ee| if !found_flags[i] { @@ -356,21 +357,8 @@ fn make_run_args(config: config, _props: test_props, testfile: str) -> } fn split_maybe_args(argstr: option) -> [str] { - fn rm_whitespace(v: [str]) -> [str] { - fn flt(&&s: str) -> option { - if !is_whitespace(s) { option::some(s) } else { option::none } - } - - // FIXME: This should be in std - fn is_whitespace(s: str) -> bool { - for c: u8 in s { if c != ' ' as u8 { ret false; } } - ret true; - } - vec::filter_map(v, flt) - } - alt argstr { - option::some(s) { rm_whitespace(str::split_char(s, ' ')) } + option::some(s) { vec::filter(str::split_char(s, ' '), {|s| !str::is_whitespace(s) }) } option::none { [] } } } diff --git a/src/libcore/char.rs b/src/libcore/char.rs index a06669dfe0353..d46f4db1a702d 100644 --- a/src/libcore/char.rs +++ b/src/libcore/char.rs @@ -43,7 +43,7 @@ export is_alphabetic, import is_alphabetic = unicode::derived_property::Alphabetic; import is_XID_start = unicode::derived_property::XID_Start; import is_XID_continue = unicode::derived_property::XID_Continue; - +import str::iterable; #[doc = " Indicates whether a character is in lower case, defined @@ -222,8 +222,10 @@ fn test_to_upper() { #[test] fn test_is_ascii() unsafe { - assert str::all("banana", char::is_ascii); - assert ! str::all("ประเทศไทย中华Việt Nam", char::is_ascii); + assert iter::all(str::by_chars("banana"), {|&&ch| char::is_ascii(ch) }); + assert ! iter::all(str::by_chars("ประเทศไทย中华Việt Nam"), {|&&ch| + char::is_ascii(ch) + }); } #[test] diff --git a/src/libcore/iter.rs b/src/libcore/iter.rs index 7fd98d6e97cd2..12cc7a5f8ad5f 100644 --- a/src/libcore/iter.rs +++ b/src/libcore/iter.rs @@ -1,3 +1,7 @@ +export iterable, enumerate, filter, map, flat_map, + foldl, to_list, repeat, all, any, each, + take, drop, head, tail; + iface iterable { fn iter(blk: fn(A)); } @@ -33,12 +37,6 @@ impl of iterable for option { } } -impl of iterable for str { - fn iter(blk: fn(&&char)) { - str::chars_iter(self) { |ch| blk(ch) } - } -} - fn enumerate>(self: IA, blk: fn(uint, A)) { let mut i = 0u; self.iter {|a| @@ -126,6 +124,22 @@ fn repeat(times: uint, blk: fn()) { } } +fn all>(self: IA, prd: fn(A) -> bool) -> bool { + let mut r: bool = true; + self.iter {|a| + r = r && prd(a) + } + ret r; +} + +fn any>(self: IA, prd: fn(A) -> bool) -> bool { + !all(self, {|c| !prd(c) }) +} + +fn each>(self: IA, blk: fn(A)) { + self.iter(blk); +} + fn min>(self: IA) -> A { alt foldl::,IA>(self, none) {|a, b| alt a { @@ -156,6 +170,34 @@ fn max>(self: IA) -> A { } } +fn take>(self:IA, n:uint, blk:fn(A)) { + let mut i = 0u; + self.iter() {|a| + if (i < n) { + blk(a); + i += 1u; + } + } +} + +fn head>(self:IA, blk:fn(A)) { + take(self, 1u, blk) +} + +fn drop>(self:IA, n:uint, blk:fn(A)) { + let mut i:uint = 0u; + self.iter {|a| + if (i >= n) { + blk(a); + } + i += 1u; + } +} + +fn tail>(self:IA, blk:fn(A)) { + drop(self, 1u, blk) +} + #[test] fn test_enumerate() { enumerate(["0", "1", "2"]) {|i,j| @@ -294,4 +336,72 @@ fn test_foldr() { } let sum = foldr([1, 2, 3, 4], 0, sub); assert sum == -2; -} \ No newline at end of file +} + +#[test] +fn test_take() { + let i = 0u; + take([5, 4, 1, 2, 3], 1u) {|h| assert h == 5; i += 1u; }; + assert i == 1u; + + i = 0u; + take([5, 4, 1, 2, 3], 2u) {|j| + alt i { + 0u { assert 5 == j } + 1u { assert 4 == j } + _ { fail; } + } + i += 1u; + } + assert i == 2u; +} + +#[test] +fn test_drop() { + let i = 0u; + drop([5, 4, 1, 2, 3], 1u) {|j| + alt i { + 0u { assert 4 == j } + 1u { assert 1 == j } + 2u { assert 2 == j } + 3u { assert 3 == j } + _ { fail; } + } + i += 1u; + } + assert i == 4u; + + i = 0u; + drop([5, 4, 1, 2, 3], 3u) {|j| + alt i { + 0u { assert 2 == j } + 1u { assert 3 == j } + _ { fail; } + } + i += 1u; + } + assert i == 2u; +} + +#[test] +fn test_head() { + let i = 0u; + head([5, 4, 1, 2, 3]) {|h| assert h == 5; i += 1u; }; + assert i == 1u; +} + +#[test] +fn test_tail() { + let i = 0u; + tail([5, 4, 1, 2, 3]) {|j| + alt i { + 0u { assert 4 == j } + 1u { assert 1 == j } + 2u { assert 2 == j } + 3u { assert 3 == j } + _ { fail; } + } + i += 1u; + } + assert i == 4u; +} diff --git a/src/libcore/str.rs b/src/libcore/str.rs index b057d8d14831c..14054f3344346 100644 --- a/src/libcore/str.rs +++ b/src/libcore/str.rs @@ -7,6 +7,8 @@ for efficiency, but UTF-8 unsafe operations should be avoided. For some heavy-duty uses, try std::rope. "]; +import iter::iterable; + export // Creating a string from_bytes, @@ -49,15 +51,11 @@ export hash, // Iterating through strings - all, any, + iterable, + by_bytes, by_chars, by_lines, by_words, all_between, any_between, - map, - bytes_iter, - chars_iter, split_char_iter, splitn_char_iter, - words_iter, - lines_iter, // Searching find, find_from, find_between, @@ -95,7 +93,6 @@ export unsafe; - #[abi = "cdecl"] native mod rustrt { fn rust_str_push(&s: str, ch: u8); @@ -561,7 +558,7 @@ fn hash(&&s: str) -> uint { // djb hash. // FIXME: replace with murmur. let mut u: uint = 5381u; - for c: u8 in s { u *= 33u; u += c as uint; } + iter::each(by_bytes(s)) {|c| u *= 33u; u += c as uint; } ret u; } @@ -569,52 +566,14 @@ fn hash(&&s: str) -> uint { Section: Iterating through strings */ -#[doc = " -Return true if a predicate matches all characters or if the string -contains no characters -"] -fn all(s: str, it: fn(char) -> bool) -> bool { - all_between(s, 0u, len(s), it) -} - -#[doc = " -Return true if a predicate matches any character (and false if it -matches none or there are no characters) -"] -fn any(ss: str, pred: fn(char) -> bool) -> bool { - !all(ss, {|cc| !pred(cc)}) -} - #[doc = "Apply a function to each character"] fn map(ss: str, ff: fn(char) -> char) -> str { let mut result = ""; reserve(result, len(ss)); - chars_iter(ss) {|cc| str::push_char(result, ff(cc));} + iter::each(by_chars(ss)) {|cc| str::push_char(result, ff(cc));} result } -#[doc = "Iterate over the bytes in a string"] -fn bytes_iter(ss: str, it: fn(u8)) { - let mut pos = 0u; - let len = len(ss); - - while (pos < len) { - it(ss[pos]); - pos += 1u; - } -} - -#[doc = "Iterate over the characters in a string"] -fn chars_iter(s: str, it: fn(char)) { - let mut pos = 0u; - let len = len(s); - while (pos < len) { - let {ch, next} = char_range_at(s, pos); - pos = next; - it(ch); - } -} - #[doc = " Apply a function to each substring after splitting by character "] @@ -630,16 +589,6 @@ fn splitn_char_iter(ss: str, sep: char, count: uint, ff: fn(&&str)) unsafe { vec::iter(splitn_char(ss, sep, count), ff) } -#[doc = "Apply a function to each word"] -fn words_iter(ss: str, ff: fn(&&str)) { - vec::iter(words(ss), ff) -} - -#[doc = "Apply a function to each lines (by '\n')"] -fn lines_iter(ss: str, ff: fn(&&str)) { - vec::iter(lines(ss), ff) -} - /* Section: Searching */ @@ -896,7 +845,7 @@ Returns true if the string contains only whitespace Whitespace characters are determined by `char::is_whitespace` "] fn is_whitespace(s: str) -> bool { - ret all(s, char::is_whitespace); + ret iter::all(by_chars(s), {|&&ch| char::is_whitespace(ch) }); } #[doc = " @@ -954,7 +903,7 @@ fn is_utf16(v: [const u16]) -> bool { fn to_utf16(s: str) -> [u16] { let mut u = []; - chars_iter(s) {|cch| + iter::each(by_chars(s)) {|&&cch: char| // Arithmetic with u32 literals is easier on the eyes than chars. let mut ch = cch as u32; @@ -1353,6 +1302,77 @@ mod unsafe { } } +enum by_bytes = str; +impl of iterable for by_bytes { + fn iter(blk: fn(&&u8)) { + let mut pos = 0u; + let len = str::len(*self); + while (pos < len) { + blk(self[pos]); + pos += 1u; + } + } +} + +enum by_chars = str; +impl of iterable for by_chars { + fn iter(blk: fn(&&char)) { + let mut pos = 0u, len = str::len(*self); + while (pos < len) { + let {ch, next} = str::char_range_at(*self, pos); + pos = next; + blk(ch); + } + } +} + +enum by_words = str; +impl of iterable for by_words { + fn iter(blk: fn(&&str)) { + let mut buf = "", + buflen = 0u, + pos = 0u, + len = str::len(*self); + while (pos < len) { + let {ch, next} = str::char_range_at(*self, pos); + if (!char::is_whitespace(ch)) { + buf += str::from_char(ch); + buflen += 1u; + } else if (buflen > 0u) { + blk(buf); + buf = ""; + buflen = 0u; + } + pos = next; + } + if (buflen > 0u) { + blk(buf); + } + } +} + +enum by_lines = str; +impl of iterable for by_lines { + fn iter(blk: fn(&&str)) { + let mut buf = "", + buflen = 0u, + pos = 0u, + len = str::len(*self); + while (pos < len) { + let {ch, next} = str::char_range_at(*self, pos); + if (ch != '\n') { + buf += str::from_char(ch); + buflen += 1u; + } else { + blk(buf); + buf = ""; + buflen = 0u; + } + pos = next; + } + blk(buf); + } +} #[cfg(test)] mod tests { @@ -2024,37 +2044,6 @@ mod tests { assert !contains(data, "ไท华"); } - #[test] - fn test_chars_iter() { - let i = 0; - chars_iter("x\u03c0y") {|ch| - alt check i { - 0 { assert ch == 'x'; } - 1 { assert ch == '\u03c0'; } - 2 { assert ch == 'y'; } - } - i += 1; - } - - chars_iter("") {|_ch| fail; } // should not fail - } - - #[test] - fn test_bytes_iter() { - let i = 0; - - bytes_iter("xyz") {|bb| - alt check i { - 0 { assert bb == 'x' as u8; } - 1 { assert bb == 'y' as u8; } - 2 { assert bb == 'z' as u8; } - } - i += 1; - } - - bytes_iter("") {|bb| assert bb == 0u8; } - } - #[test] fn test_split_char_iter() { let data = "\nMary had a little lamb\nLittle lamb\n"; @@ -2090,44 +2079,6 @@ 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; - } - - words_iter("") {|_x| fail; } // should not fail - } - - #[test] - fn test_lines_iter () { - let lf = "\nMary had a little lamb\nLittle lamb\n"; - - let ii = 0; - - lines_iter(lf) {|x| - alt ii { - 0 { assert "" == x; } - 1 { assert "Mary had a little lamb" == x; } - 2 { assert "Little lamb" == x; } - 3 { assert "" == x; } - _ { () } - } - ii += 1; - } - } - #[test] fn test_map() { assert "" == map("", char::to_upper); @@ -2136,20 +2087,40 @@ mod tests { #[test] fn test_all() { - assert true == all("", char::is_uppercase); - assert false == all("ymca", char::is_uppercase); - assert true == all("YMCA", char::is_uppercase); - assert false == all("yMCA", char::is_uppercase); - assert false == all("YMCy", char::is_uppercase); + assert true == iter::all(by_chars(""), {|&&ch| + char::is_uppercase(ch) + }); + assert false == iter::all(by_chars("ymca"), {|&&ch| + char::is_uppercase(ch) + }); + assert true == iter::all(by_chars("YMCA"), {|&&ch| + char::is_uppercase(ch) + }); + assert false == iter::all(by_chars("yMCA"), {|&&ch| + char::is_uppercase(ch) + }); + assert false == iter::all(by_chars("YMCy"), {|&&ch| + char::is_uppercase(ch) + }); } #[test] fn test_any() { - assert false == any("", char::is_uppercase); - assert false == any("ymca", char::is_uppercase); - assert true == any("YMCA", char::is_uppercase); - assert true == any("yMCA", char::is_uppercase); - assert true == any("Ymcy", char::is_uppercase); + assert false == iter::any(by_chars(""), {|&&ch| + char::is_uppercase(ch) + }); + assert false == iter::any(by_chars("ymca"), {|&&ch| + char::is_uppercase(ch) + }); + assert true == iter::any(by_chars("YMCA"), {|&&ch| + char::is_uppercase(ch) + }); + assert true == iter::any(by_chars("yMCA"), {|&&ch| + char::is_uppercase(ch) + }); + assert true == iter::any(by_chars("Ymcy"), {|&&ch| + char::is_uppercase(ch) + }); } #[test] @@ -2205,4 +2176,170 @@ mod tests { assert to_utf16(from_utf16(u)) == u; } } + + #[test] + fn test_str_byte_iter() { + let i = 0u; + by_bytes("፩፪፫").iter {|&&b: u8| + alt i { + 0u { assert 0xe1_u8 == b } + 1u { assert 0x8d_u8 == b } + 2u { assert 0xa9_u8 == b } + 3u { assert 0xe1_u8 == b } + 4u { assert 0x8d_u8 == b } + 5u { assert 0xaa_u8 == b } + 6u { assert 0xe1_u8 == b } + 7u { assert 0x8d_u8 == b } + 8u { assert 0xab_u8 == b } + _ { fail; } + } + i += 1u; + } + assert(i == 9u); + } + + #[test] + fn test_str_char_iter() { + let i = 0u; + by_chars("፩፪፫").iter {|&&c: char| + alt i { + 0u { assert '፩' == c } + 1u { assert '፪' == c } + 2u { assert '፫' == c } + _ { fail; } + } + i += 1u; + } + assert(i == 3u); + } + + #[test] + fn test_str_all() { + assert true == iter::all(by_chars("፩፪፫")) {|&&c| + unicode::general_category::No(c) + }; + assert false == iter::all(by_chars("፩፪፫")) {|&&c| + unicode::general_category::Lu(c) + }; + assert false == iter::all(by_chars("፩፪3")) {|&&c| + unicode::general_category::No(c) + }; + assert true == iter::all(by_chars("")) {|&&c| + unicode::general_category::Lu(c) + }; + } + + #[test] + fn test_str_all_calls() { + let calls: uint = 0u; + assert false == iter::all(by_chars("፩2፫")) {|&&c| + calls += 1u; + unicode::general_category::No(c) + }; + assert calls == 2u; + } + + #[test] + fn test_str_any() { + assert true == iter::any(by_chars("፩፪፫")) {|&&c| + unicode::general_category::No(c) + }; + assert false == iter::any(by_chars("፩፪፫")) {|&&c| + unicode::general_category::Lu(c) + }; + assert true == iter::any(by_chars("1፪፫")) {|&&c| + unicode::general_category::No(c) + }; + assert false == iter::any(by_chars("")) {|&&c| + unicode::general_category::Lu(c) + }; + } + + #[test] + fn test_str_any_calls() { + let calls: uint = 0u; + assert true == iter::any(by_chars("፩2፫")) {|&&c| + calls += 1u; + unicode::general_category::Nd(c) + }; + assert calls == 2u; + } + + #[test] + fn test_words_iter() { + let data = "\nMary had a little lamb.\nLittle lamb\n"; + + let ii = 0; + + by_words(data).iter {|ww| + alt ii { + 0 { assert "Mary" == ww; } + 1 { assert "had" == ww; } + 2 { assert "a" == ww; } + 3 { assert "little" == ww; } + 4 { assert "lamb." == ww; } + 5 { assert "Little" == ww; } + 6 { assert "lamb" == ww; } + _ { fail } + } + ii += 1; + } + assert(ii == 7); + + by_words("").iter {|_x| fail; } // should not fail + } + + #[test] + fn test_words_iter2() { + let data = "\nMary had a"; + + let ii = 0; + + by_words(data).iter {|ww| + alt ii { + 0 { assert "Mary" == ww; } + 1 { assert "had" == ww; } + 2 { assert "a" == ww; } + _ { fail } + } + ii += 1; + } + assert(ii == 3); + } + + #[test] + fn test_lines_iter() { + let lf = "\nMary had a little lamb\r\nLittle lamb\n"; + + let ii = 0; + + iter::each(by_lines(lf)) {|x| + alt ii { + 0 { assert "" == x; } + 1 { assert "Mary had a little lamb\r" == x; } + 2 { assert "Little lamb" == x; } + 3 { assert "" == x; } + _ { fail } + } + ii += 1; + } + assert(ii == 4); + } + + #[test] + fn test_lines_iter2() { + let lf = "Mary had a little lamb\nLittle lamb"; + + let ii = 0; + + iter::each(by_lines(lf)) {|x| + alt ii { + 0 { assert "Mary had a little lamb" == x; } + 1 { assert "Little lamb" == x; } + _ { fail } + } + ii += 1; + } + assert(ii == 2); + } } diff --git a/src/libstd/json.rs b/src/libstd/json.rs index 4e197d30c9531..881ed36e5d0aa 100644 --- a/src/libstd/json.rs +++ b/src/libstd/json.rs @@ -8,6 +8,7 @@ import io; import io::{reader_util, writer_util}; import map; import map::hashmap; +import str::iterable; export json; export error; @@ -47,7 +48,7 @@ fn to_writer(wr: io::writer, j: json) { string(s) { wr.write_char('"'); let escaped = ""; - str::chars_iter(s) { |c| + iter::each(str::by_chars(s)) { |c| alt c { '"' { escaped += "\\\""; } '\\' { escaped += "\\\\"; } @@ -180,7 +181,7 @@ impl parser for parser { } fn parse_ident(ident: str, value: json) -> result { - if str::all(ident, { |c| c == self.next_char() }) { + if iter::all(str::by_chars(ident), { |c| c == self.next_char() }) { self.bump(); ok(value) } else { diff --git a/src/rustc/metadata/common.rs b/src/rustc/metadata/common.rs index 4ccb34055605a..218afefbb8c2a 100644 --- a/src/rustc/metadata/common.rs +++ b/src/rustc/metadata/common.rs @@ -1,3 +1,5 @@ +import str::iterable; + // EBML enum definitions and utils shared by the encoder and decoder const tag_paths: uint = 0x01u; @@ -106,7 +108,8 @@ fn hash_node_id(&&node_id: int) -> uint { ret 177573u ^ (node_id as uint); } fn hash_path(&&s: str) -> uint { let h = 5381u; - for ch: u8 in str::bytes(s) { h = (h << 5u) + h ^ (ch as uint); } + iter::each(str::by_bytes(s)) {|&&ch: u8| + h = (h << 5u) + h ^ (ch as uint); + } ret h; } - diff --git a/src/rustc/syntax/ext/qquote.rs b/src/rustc/syntax/ext/qquote.rs index f93a4019c31fc..02b462580383d 100644 --- a/src/rustc/syntax/ext/qquote.rs +++ b/src/rustc/syntax/ext/qquote.rs @@ -11,6 +11,7 @@ import syntax::parse::parser::{parser, parse_from_source_str}; import syntax::print::*; import io::*; +import str::iterable; import codemap::span; @@ -215,7 +216,7 @@ fn finish let state = active; let i = 0u, j = 0u; let g_len = vec::len(cx.gather); - str::chars_iter(*str) {|ch| + iter::each(str::by_chars(*str)) {|ch| if (j < g_len && i == cx.gather[j].lo) { assert ch == '$'; let repl = #fmt("$%u ", j); diff --git a/src/rustdoc/desc_to_brief_pass.rs b/src/rustdoc/desc_to_brief_pass.rs index 4ff63120efb47..94d509dfd72ad 100644 --- a/src/rustdoc/desc_to_brief_pass.rs +++ b/src/rustdoc/desc_to_brief_pass.rs @@ -7,6 +7,8 @@ is interpreted as the brief description. "]; +import str::iterable; + export mk_pass; fn mk_pass() -> pass { @@ -135,10 +137,9 @@ fn sentences(s: str) -> [str] { } fn paragraphs(s: str) -> [str] { - let lines = str::lines_any(s); let whitespace_lines = 0; let accum = ""; - let paras = vec::foldl([], lines) {|paras, line| + let paras = iter::foldl(str::by_lines(s), []) {|paras, line| let res = paras; if str::is_whitespace(line) { @@ -216,4 +217,4 @@ counties."); let brief = extract(desc); assert brief == some( "Warkworth Castle is a ruined medieval building in the town"); -} \ No newline at end of file +} diff --git a/src/rustdoc/markdown_pass.rs b/src/rustdoc/markdown_pass.rs index ad72e55ba1054..8d0d46eb5aef8 100644 --- a/src/rustdoc/markdown_pass.rs +++ b/src/rustdoc/markdown_pass.rs @@ -3,6 +3,7 @@ import markdown_writer::writer; import markdown_writer::writer_util; import markdown_writer::writer_factory; +import str::iterable; export mk_pass; export header_kind, header_name, header_text; @@ -489,8 +490,10 @@ fn write_sig(ctxt: ctxt, sig: option) { } fn code_block_indent(s: str) -> str { - let lines = str::lines_any(s); - let indented = par::seqmap(lines, { |line| #fmt(" %s", line) }); + let indented: [str] = []; + iter::each(str::by_lines(s), { + |line| indented += [#fmt(" %s", line)] + }); str::connect(indented, "\n") } diff --git a/src/rustdoc/sectionalize_pass.rs b/src/rustdoc/sectionalize_pass.rs index fc957835822a2..1ac5606e54891 100644 --- a/src/rustdoc/sectionalize_pass.rs +++ b/src/rustdoc/sectionalize_pass.rs @@ -1,5 +1,7 @@ #[doc = "Breaks rustdocs into sections according to their headers"]; +import str::iterable; + export mk_pass; fn mk_pass() -> pass { @@ -88,13 +90,11 @@ fn sectionalize(desc: option) -> (option, [doc::section]) { ret (none, []); } - let lines = str::lines(option::get(desc)); - let new_desc = none; let current_section = none; let sections = []; - for line in lines { + iter::each(str::by_lines(option::get(desc))) {|line| alt parse_header(line) { some(header) { if option::is_some(current_section) { diff --git a/src/rustdoc/unindent_pass.rs b/src/rustdoc/unindent_pass.rs index 48d7b595f5ff0..50f3d19ad533a 100644 --- a/src/rustdoc/unindent_pass.rs +++ b/src/rustdoc/unindent_pass.rs @@ -13,16 +13,17 @@ middle of a line, and each of the following lines is indented. export mk_pass; +import str::iterable; + fn mk_pass() -> pass { text_pass::mk_pass("unindent", unindent) } fn unindent(s: str) -> str { - let lines = str::lines_any(s); let saw_first_line = false; let saw_second_line = false; - let min_indent = vec::foldl(uint::max_value, lines) {|min_indent, line| - + let min_indent = iter::foldl(str::by_lines(s), uint::max_value) + {|min_indent, line| // After we see the first non-whitespace line, look at // the line we have. If it is not whitespace, and therefore // part of the first paragraph, then ignore the indentation @@ -47,7 +48,7 @@ fn unindent(s: str) -> str { } else { saw_first_line = true; let spaces = 0u; - str::all(line) {|char| + iter::all(str::by_chars(line)) {|char| // Only comparing against space because I wouldn't // know what to do with mixed whitespace chars if char == ' ' { @@ -61,20 +62,19 @@ fn unindent(s: str) -> str { } }; - if check vec::is_not_empty(lines) { - let unindented = [str::trim(vec::head(lines))] - + par::anymap(vec::tail(lines)) {|line| - if str::is_whitespace(line) { - line - } else { - assert str::len(line) >= min_indent; - str::slice(line, min_indent, str::len(line)) - } - }; - str::connect(unindented, "\n") - } else { - s + let unindented = []; + iter::head(str::by_lines(s)) {|line| + unindented += [str::trim(line)] } + iter::tail(str::by_lines(s)) {|line| + if str::is_whitespace(line) { + unindented += [line] + } else { + assert str::len(line) >= min_indent; + unindented += [str::slice(line, min_indent, str::len(line))]; + } + }; + str::connect(unindented, "\n") } #[test]