Skip to content

Commit 539cfe8

Browse files
committed
Merge pull request #1336 from erickt/master
misc fixes and library functions
2 parents ebdf3ef + 02d84d8 commit 539cfe8

File tree

8 files changed

+143
-27
lines changed

8 files changed

+143
-27
lines changed

src/comp/back/link.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ mod write {
321321
*
322322
* There are a few issues to handle:
323323
*
324-
* - Linnkers operate on a flat namespace, so we have to flatten names.
324+
* - Linkers operate on a flat namespace, so we have to flatten names.
325325
* We do this using the C++ namespace-mangling technique. Foo::bar
326326
* symbols and such.
327327
*

src/etc/vim/syntax/rust.vim

+4-2
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ syn match rustFloat display contained "\d\+e[-+]\=\d\+[fl]\=\>"
4646
syn match rustCharacter "'[^']*'"
4747

4848
syn case match
49-
syn region rustComment start="/\*" end="\*/"
49+
syn region rustComment start="/\*" end="\*/" contains=rustComment
5050
syn region rustComment start="//" skip="\\$" end="$" keepend
5151

5252
hi def link rustString String
@@ -59,5 +59,7 @@ hi def link rustComment Comment
5959
hi def link rustMacro Macro
6060
hi def link rustType Type
6161

62-
let b:current_syntax = "rust"
62+
syn sync minlines=200
63+
syn sync maxlines=500
6364

65+
let b:current_syntax = "rust"

src/libcore/str.rs

+31-5
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ String manipulation.
66

77
export eq, lteq, hash, is_empty, is_not_empty, is_whitespace, byte_len,
88
byte_len_range, index,
9-
rindex, find, starts_with, ends_with, substr, slice, split, split_str,
10-
concat, connect, to_upper, replace, char_slice, trim_left, trim_right,
11-
trim, unshift_char, shift_char, pop_char, push_char, is_utf8,
12-
from_chars, to_chars, char_len, char_len_range, char_at, bytes,
13-
is_ascii, shift_byte, pop_byte,
9+
rindex, find, starts_with, ends_with, substr, slice, split, splitn,
10+
split_str, concat, connect, to_upper, replace, char_slice, trim_left,
11+
trim_right, trim, unshift_char, shift_char, pop_char, push_char,
12+
is_utf8, from_chars, to_chars, char_len, char_len_range, char_at,
13+
bytes, is_ascii, shift_byte, pop_byte,
1414
unsafe_from_byte, unsafe_from_bytes, from_char, char_range_at,
1515
str_from_cstr, sbuf, as_buf, push_byte, utf8_char_width, safe_slice,
1616
contains, iter_chars, loop_chars, loop_chars_sub,
@@ -744,6 +744,32 @@ fn split(s: str, sep: u8) -> [str] {
744744
ret v;
745745
}
746746

747+
/*
748+
Function: splitn
749+
750+
Split a string at each occurance of a given separator up to count times.
751+
752+
Returns:
753+
754+
A vector containing all the strings between each occurance of the separator
755+
*/
756+
fn splitn(s: str, sep: u8, count: uint) -> [str] {
757+
let v = [];
758+
let accum = "";
759+
let n = count;
760+
let ends_with_sep: bool = false;
761+
for c in s {
762+
if n > 0u && c == sep {
763+
n -= 1u;
764+
v += [accum];
765+
accum = "";
766+
ends_with_sep = true;
767+
} else { accum += unsafe_from_byte(c); ends_with_sep = false; }
768+
}
769+
if byte_len(accum) != 0u || ends_with_sep { v += [accum]; }
770+
ret v;
771+
}
772+
747773
/*
748774
Function: split_str
749775

src/libcore/vec.rs

+42
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,15 @@ fn pop<copy T>(&v: [const T]) -> T {
300300
ret e;
301301
}
302302

303+
/*
304+
Function: push
305+
306+
Append an element to a vector and return it
307+
*/
308+
fn push<copy T>(&v: [T], initval: T) {
309+
grow(v, 1u, initval)
310+
}
311+
303312
// TODO: More.
304313

305314

@@ -507,6 +516,24 @@ fn any<T>(v: [T], f: block(T) -> bool) -> bool {
507516
ret false;
508517
}
509518

519+
/*
520+
Function: any2
521+
522+
Return true if a predicate matches any elements in both vectors.
523+
524+
If the vectors contains no elements then false is returned.
525+
*/
526+
fn any2<T, U>(v0: [T], v1: [U], f: block(T, U) -> bool) -> bool {
527+
let v0_len = len(v0);
528+
let v1_len = len(v1);
529+
let i = 0u;
530+
while i < v0_len && i < v1_len {
531+
if f(v0[i], v1[i]) { ret true; };
532+
i += 1u;
533+
}
534+
ret false;
535+
}
536+
510537
/*
511538
Function: all
512539
@@ -519,6 +546,21 @@ fn all<T>(v: [T], f: block(T) -> bool) -> bool {
519546
ret true;
520547
}
521548

549+
/*
550+
Function: all2
551+
552+
Return true if a predicate matches all elements in both vectors.
553+
554+
If the vectors are not the same size then false is returned.
555+
*/
556+
fn all2<T, U>(v0: [T], v1: [U], f: block(T, U) -> bool) -> bool {
557+
let v0_len = len(v0);
558+
if v0_len != len(v1) { ret false; }
559+
let i = 0u;
560+
while i < v0_len { if !f(v0[i], v1[i]) { ret false; }; i += 1u; }
561+
ret true;
562+
}
563+
522564
/*
523565
Function: member
524566

src/libstd/getopts.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -149,10 +149,7 @@ fn name_str(nm: name) -> str {
149149
}
150150

151151
fn find_opt(opts: [opt], nm: name) -> option::t<uint> {
152-
let i = 0u;
153-
let l = vec::len::<opt>(opts);
154-
while i < l { if opts[i].name == nm { ret some::<uint>(i); } i += 1u; }
155-
ret none::<uint>;
152+
vec::position_pred(opts, { |opt| opt.name == nm })
156153
}
157154

158155
/*

src/test/stdtest/getopts.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -444,10 +444,11 @@ fn test_unrecognized_option_short() {
444444
fn test_combined() {
445445
let args =
446446
["prog", "free1", "-s", "20", "free2", "--flag", "--long=30", "-f",
447-
"-m", "40", "-m", "50"];
447+
"-m", "40", "-m", "50", "-n", "-A B", "-n", "-60 70"];
448448
let opts =
449449
[opt::optopt("s"), opt::optflag("flag"), opt::reqopt("long"),
450-
opt::optflag("f"), opt::optmulti("m"), opt::optopt("notpresent")];
450+
opt::optflag("f"), opt::optmulti("m"), opt::optmulti("n"),
451+
opt::optopt("notpresent")];
451452
let rs = opt::getopts(args, opts);
452453
alt rs {
453454
ok(m) {
@@ -460,6 +461,8 @@ fn test_combined() {
460461
assert (opt::opt_present(m, "f"));
461462
assert (opt::opt_strs(m, "m")[0] == "40");
462463
assert (opt::opt_strs(m, "m")[1] == "50");
464+
assert (opt::opt_strs(m, "n")[0] == "-A B");
465+
assert (opt::opt_strs(m, "n")[1] == "-60 70");
463466
assert (!opt::opt_present(m, "notpresent"));
464467
}
465468
_ { fail; }

src/test/stdtest/str.rs

+28-13
Original file line numberDiff line numberDiff line change
@@ -41,22 +41,37 @@ fn test_index_and_rindex() {
4141

4242
#[test]
4343
fn test_split() {
44-
fn t(s: str, c: char, i: int, k: str) {
45-
log "splitting: " + s;
46-
log i;
44+
fn t(s: str, c: char, u: [str]) {
45+
log "split: " + s;
4746
let v = str::split(s, c as u8);
4847
log "split to: ";
49-
for z: str in v { log z; }
50-
log "comparing: " + v[i] + " vs. " + k;
51-
assert (str::eq(v[i], k));
48+
log v;
49+
assert (vec::all2(v, u, { |a,b| a == b }));
5250
}
53-
t("abc.hello.there", '.', 0, "abc");
54-
t("abc.hello.there", '.', 1, "hello");
55-
t("abc.hello.there", '.', 2, "there");
56-
t(".hello.there", '.', 0, "");
57-
t(".hello.there", '.', 1, "hello");
58-
t("...hello.there.", '.', 3, "hello");
59-
t("...hello.there.", '.', 5, "");
51+
t("abc.hello.there", '.', ["abc", "hello", "there"]);
52+
t(".hello.there", '.', ["", "hello", "there"]);
53+
t("...hello.there.", '.', ["", "", "", "hello", "there", ""]);
54+
}
55+
56+
#[test]
57+
fn test_splitn() {
58+
fn t(s: str, c: char, n: uint, u: [str]) {
59+
log "splitn: " + s;
60+
let v = str::splitn(s, c as u8, n);
61+
log "split to: ";
62+
log v;
63+
log "comparing vs. ";
64+
log u;
65+
assert (vec::all2(v, u, { |a,b| a == b }));
66+
}
67+
t("abc.hello.there", '.', 0u, ["abc.hello.there"]);
68+
t("abc.hello.there", '.', 1u, ["abc", "hello.there"]);
69+
t("abc.hello.there", '.', 2u, ["abc", "hello", "there"]);
70+
t("abc.hello.there", '.', 3u, ["abc", "hello", "there"]);
71+
t(".hello.there", '.', 0u, [".hello.there"]);
72+
t(".hello.there", '.', 1u, ["", "hello.there"]);
73+
t("...hello.there.", '.', 3u, ["", "", "", "hello.there."]);
74+
t("...hello.there.", '.', 5u, ["", "", "", "hello", "there", ""]);
6075
}
6176

6277
#[test]

src/test/stdtest/vec.rs

+31
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ pure fn is_three(&&n: uint) -> bool { ret n == 3u; }
1616

1717
pure fn is_odd(&&n: uint) -> bool { ret n % 2u == 1u; }
1818

19+
pure fn is_equal(&&x: uint, &&y:uint) -> bool { ret x == y; }
20+
1921
fn square_if_odd(&&n: uint) -> option::t<uint> {
2022
ret if n % 2u == 1u { some(n * n) } else { none };
2123
}
@@ -168,6 +170,21 @@ fn test_pop() {
168170
assert (e == 5);
169171
}
170172

173+
#[test]
174+
fn test_push() {
175+
// Test on-stack push().
176+
let v = [];
177+
vec::push(v, 1);
178+
assert (vec::len(v) == 1u);
179+
assert (v[0] == 1);
180+
181+
// Test on-heap push().
182+
vec::push(v, 2);
183+
assert (vec::len(v) == 2u);
184+
assert (v[0] == 1);
185+
assert (v[1] == 2);
186+
}
187+
171188
#[test]
172189
fn test_grow() {
173190
// Test on-stack grow().
@@ -401,6 +418,20 @@ fn test_any_and_all() {
401418
assert (!vec::all([3u, 3u, 0u, 1u, 2u], is_three));
402419
}
403420

421+
#[test]
422+
fn test_any2_and_all2() {
423+
424+
assert (vec::any2([2u, 4u, 6u], [2u, 4u, 6u], is_equal));
425+
assert (vec::any2([1u, 2u, 3u], [4u, 5u, 3u], is_equal));
426+
assert (!vec::any2([1u, 2u, 3u], [4u, 5u, 6u], is_equal));
427+
assert (vec::any2([2u, 4u, 6u], [2u, 4u], is_equal));
428+
429+
assert (vec::all2([2u, 4u, 6u], [2u, 4u, 6u], is_equal));
430+
assert (!vec::all2([1u, 2u, 3u], [4u, 5u, 3u], is_equal));
431+
assert (!vec::all2([1u, 2u, 3u], [4u, 5u, 6u], is_equal));
432+
assert (!vec::all2([2u, 4u, 6u], [2u, 4u], is_equal));
433+
}
434+
404435
#[test]
405436
fn test_zip_unzip() {
406437
let v1 = [1, 2, 3];

0 commit comments

Comments
 (0)