Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions src/cargo/pgp.rs
Original file line number Diff line number Diff line change
@@ -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);
}
Expand Down Expand Up @@ -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 };
}
20 changes: 4 additions & 16 deletions src/compiletest/runtest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import common::config;
import header::load_props;
import header::test_props;
import util::logv;
import str::iterable;

export run;

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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] {
Expand Down Expand Up @@ -356,21 +357,8 @@ fn make_run_args(config: config, _props: test_props, testfile: str) ->
}

fn split_maybe_args(argstr: option<str>) -> [str] {
fn rm_whitespace(v: [str]) -> [str] {
fn flt(&&s: str) -> option<str> {
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 { [] }
}
}
Expand Down
8 changes: 5 additions & 3 deletions src/libcore/char.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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]
Expand Down
124 changes: 117 additions & 7 deletions src/libcore/iter.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
export iterable, enumerate, filter, map, flat_map,
foldl, to_list, repeat, all, any, each,
take, drop, head, tail;

iface iterable<A> {
fn iter(blk: fn(A));
}
Expand Down Expand Up @@ -33,12 +37,6 @@ impl<A> of iterable<A> for option<A> {
}
}

impl of iterable<char> for str {
fn iter(blk: fn(&&char)) {
str::chars_iter(self) { |ch| blk(ch) }
}
}

fn enumerate<A,IA:iterable<A>>(self: IA, blk: fn(uint, A)) {
let mut i = 0u;
self.iter {|a|
Expand Down Expand Up @@ -126,6 +124,22 @@ fn repeat(times: uint, blk: fn()) {
}
}

fn all<A, IA:iterable<A>>(self: IA, prd: fn(A) -> bool) -> bool {
let mut r: bool = true;
self.iter {|a|
r = r && prd(a)
}
ret r;
}

fn any<A, IA:iterable<A>>(self: IA, prd: fn(A) -> bool) -> bool {
!all(self, {|c| !prd(c) })
}

fn each<A, IA:iterable<A>>(self: IA, blk: fn(A)) {
self.iter(blk);
}

fn min<A:copy,IA:iterable<A>>(self: IA) -> A {
alt foldl::<A,option<A>,IA>(self, none) {|a, b|
alt a {
Expand Down Expand Up @@ -156,6 +170,34 @@ fn max<A:copy,IA:iterable<A>>(self: IA) -> A {
}
}

fn take<A:copy, IA:iterable<A>>(self:IA, n:uint, blk:fn(A)) {
let mut i = 0u;
self.iter() {|a|
if (i < n) {
blk(a);
i += 1u;
}
}
}

fn head<A:copy, IA:iterable<A>>(self:IA, blk:fn(A)) {
take(self, 1u, blk)
}

fn drop<A:copy, IA:iterable<A>>(self:IA, n:uint, blk:fn(A)) {
let mut i:uint = 0u;
self.iter {|a|
if (i >= n) {
blk(a);
}
i += 1u;
}
}

fn tail<A:copy, IA:iterable<A>>(self:IA, blk:fn(A)) {
drop(self, 1u, blk)
}

#[test]
fn test_enumerate() {
enumerate(["0", "1", "2"]) {|i,j|
Expand Down Expand Up @@ -294,4 +336,72 @@ fn test_foldr() {
}
let sum = foldr([1, 2, 3, 4], 0, sub);
assert sum == -2;
}
}

#[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;
}
Loading