Make vectors uglier ([]/~). Sorry. Should be temporary. Closes #2725.

This commit is contained in:
Michael Sullivan
2012-06-25 20:00:46 -07:00
parent c087aaf56b
commit 329eca6044
418 changed files with 4123 additions and 4034 deletions

View File

@@ -170,7 +170,7 @@ mod tests {
#[test]
fn manually_share_arc() {
let v = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
let v = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]/~;
let arc_v = arc::arc(v);
let p = port();
@@ -182,7 +182,7 @@ mod tests {
let arc_v = p.recv();
let v = *arc::get::<[int]>(&arc_v);
let v = *arc::get::<[int]/~>(&arc_v);
assert v[3] == 4;
};
@@ -196,7 +196,7 @@ mod tests {
#[test]
fn auto_share_arc() {
let v = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
let v = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]/~;
let (_res, arc_c) = shared_arc(v);
let p = port();
@@ -216,7 +216,7 @@ mod tests {
#[test]
#[ignore] // this can probably infinite loop too.
fn exclusive_arc() {
let mut futures = [];
let mut futures = []/~;
let num_tasks = 10u;
let count = 1000u;
@@ -231,7 +231,7 @@ mod tests {
**count += 1u;
}
}
})];
})]/~;
};
for futures.each {|f| f.get() };

View File

@@ -65,7 +65,7 @@ pure fn is_uppercase(c: char) -> bool {
#[doc = "
Indicates whether a character is whitespace, defined in
terms of the Unicode General Categories 'Zs', 'Zl', 'Zp'
additional 'Cc'-category control codes in the range [0x09, 0x0d]
additional 'Cc'-category control codes in the range [0x09, 0x0d]/~
"]
pure fn is_whitespace(c: char) -> bool {
ret ('\x09' <= c && c <= '\x0d')
@@ -128,8 +128,8 @@ Return the hexadecimal unicode escape of a char.
The rules are as follows:
- chars in [0,0xff] get 2-digit escapes: `\\xNN`
- chars in [0x100,0xffff] get 4-digit escapes: `\\uNNNN`
- chars in [0,0xff]/~ get 2-digit escapes: `\\xNN`
- chars in [0x100,0xffff]/~ get 4-digit escapes: `\\uNNNN`
- chars above 0x10000 get 8-digit escapes: `\\UNNNNNNNN`
"]
fn escape_unicode(c: char) -> str {
@@ -154,7 +154,7 @@ languages. The exact rules are:
- Tab, CR and LF are escaped as '\t', '\r' and '\n' respectively.
- Single-quote, double-quote and backslash chars are backslash-escaped.
- Any other chars in the range [0x20,0x7e] are not escaped.
- Any other chars in the range [0x20,0x7e]/~ are not escaped.
- Any other chars are given hex unicode escapes; see `escape_unicode`.
"]
fn escape_default(c: char) -> str {

View File

@@ -217,7 +217,7 @@ fn peek_(p: *rust_port) -> bool {
#[doc = "Receive on one of two ports"]
fn select2<A: send, B: send>(p_a: port<A>, p_b: port<B>)
-> either<A, B> {
let ports = [(**p_a).po, (**p_b).po];
let ports = [(**p_a).po, (**p_b).po]/~;
let n_ports = 2 as libc::size_t;
let yield = 0u, yieldp = ptr::addr_of(yield);

View File

@@ -32,7 +32,7 @@ may permit read-only access during iteration or other use.
# WARNING
For maximum performance, this type is implemented using some rather
unsafe code. In particular, this innocent looking `[mut A]` pointer
unsafe code. In particular, this innocent looking `[mut A]/~` pointer
*may be null!* Therefore, it is important you not reach into the
data structure manually but instead use the provided extensions.
@@ -48,27 +48,26 @@ type could only produce 47 million pushes/second.
"]
type dvec<A> = {
mut data: [mut A]
mut data: [mut A]/~
};
#[doc = "Creates a new, empty dvec"]
fn dvec<A>() -> dvec<A> {
{mut data: [mut]}
{mut data: [mut]/~}
}
#[doc = "Creates a new dvec with a single element"]
fn from_elt<A>(+e: A) -> dvec<A> {
{mut data: [mut e]}
{mut data: [mut e]/~}
}
#[doc = "Creates a new dvec with the contents of a vector"]
fn from_vec<A>(+v: [mut A]) -> dvec<A> {
fn from_vec<A>(+v: [mut A]/~) -> dvec<A> {
{mut data: v}
}
#[doc = "Consumes the vector and returns its contents"]
fn unwrap<A>(-d: dvec<A>) -> [mut A] {
fn unwrap<A>(-d: dvec<A>) -> [mut A]/~ {
let {data: v} <- d;
ret v;
}
@@ -84,7 +83,7 @@ impl private_methods<A> for dvec<A> {
}
#[inline(always)]
fn borrow<B>(f: fn(-[mut A]) -> B) -> B {
fn borrow<B>(f: fn(-[mut A]/~) -> B) -> B {
unsafe {
let mut data = unsafe::reinterpret_cast(null::<()>());
data <-> self.data;
@@ -95,7 +94,7 @@ impl private_methods<A> for dvec<A> {
}
#[inline(always)]
fn return(-data: [mut A]) {
fn return(-data: [mut A]/~) {
unsafe {
self.data <- data;
}
@@ -114,7 +113,7 @@ impl extensions<A> for dvec<A> {
"]
#[inline(always)]
fn swap(f: fn(-[mut A]) -> [mut A]) {
fn swap(f: fn(-[mut A]/~) -> [mut A]/~) {
self.borrow { |v| self.return(f(v)) }
}
@@ -128,7 +127,7 @@ impl extensions<A> for dvec<A> {
}
#[doc = "Overwrite the current contents"]
fn set(+w: [mut A]) {
fn set(+w: [mut A]/~) {
self.check_not_borrowed();
self.data <- w;
}
@@ -151,7 +150,7 @@ impl extensions<A> for dvec<A> {
let data_ptr: *() = unsafe::reinterpret_cast(data);
if data_ptr.is_null() { fail "Recursive use of dvec"; }
log(error, "a");
self.data <- [mut t] + data;
self.data <- [mut t]/~ + data;
log(error, "b");
}
}
@@ -219,7 +218,7 @@ impl extensions<A:copy> for dvec<A> {
}
};
for ts.each { |t| v += [t] };
for ts.each { |t| v += [t]/~ };
v
}
}
@@ -229,7 +228,7 @@ impl extensions<A:copy> for dvec<A> {
See `unwrap()` if you do not wish to copy the contents.
"]
fn get() -> [A] {
fn get() -> [A]/~ {
self.borrow { |v|
let w = vec::from_mut(copy v);
self.return(v);
@@ -271,4 +270,4 @@ impl extensions<A:copy> for dvec<A> {
fn last() -> A {
self.get_elt(self.len() - 1u)
}
}
}

View File

@@ -21,28 +21,28 @@ fn either<T, U, V>(f_left: fn(T) -> V,
alt value { left(l) { f_left(l) } right(r) { f_right(r) } }
}
fn lefts<T: copy, U>(eithers: [either<T, U>]) -> [T] {
fn lefts<T: copy, U>(eithers: [either<T, U>]/~) -> [T]/~ {
#[doc = "Extracts from a vector of either all the left values"];
let mut result: [T] = [];
let mut result: [T]/~ = []/~;
for vec::each(eithers) {|elt|
alt elt { left(l) { result += [l]; } _ {/* fallthrough */ } }
alt elt { left(l) { result += [l]/~; } _ {/* fallthrough */ } }
}
ret result;
}
fn rights<T, U: copy>(eithers: [either<T, U>]) -> [U] {
fn rights<T, U: copy>(eithers: [either<T, U>]/~) -> [U]/~ {
#[doc = "Extracts from a vector of either all the right values"];
let mut result: [U] = [];
let mut result: [U]/~ = []/~;
for vec::each(eithers) {|elt|
alt elt { right(r) { result += [r]; } _ {/* fallthrough */ } }
alt elt { right(r) { result += [r]/~; } _ {/* fallthrough */ } }
}
ret result;
}
fn partition<T: copy, U: copy>(eithers: [either<T, U>])
-> {lefts: [T], rights: [U]} {
fn partition<T: copy, U: copy>(eithers: [either<T, U>]/~)
-> {lefts: [T]/~, rights: [U]/~} {
#[doc = "
Extracts from a vector of either all the left values and right values
@@ -50,10 +50,10 @@ fn partition<T: copy, U: copy>(eithers: [either<T, U>])
right values.
"];
let mut lefts: [T] = [];
let mut rights: [U] = [];
let mut lefts: [T]/~ = []/~;
let mut rights: [U]/~ = []/~;
for vec::each(eithers) {|elt|
alt elt { left(l) { lefts += [l]; } right(r) { rights += [r]; } }
alt elt { left(l) { lefts += [l]/~; } right(r) { rights += [r]/~; } }
}
ret {lefts: lefts, rights: rights};
}
@@ -112,49 +112,49 @@ fn test_either_right() {
#[test]
fn test_lefts() {
let input = [left(10), right(11), left(12), right(13), left(14)];
let input = [left(10), right(11), left(12), right(13), left(14)]/~;
let result = lefts(input);
assert (result == [10, 12, 14]);
assert (result == [10, 12, 14]/~);
}
#[test]
fn test_lefts_none() {
let input: [either<int, int>] = [right(10), right(10)];
let input: [either<int, int>]/~ = [right(10), right(10)]/~;
let result = lefts(input);
assert (vec::len(result) == 0u);
}
#[test]
fn test_lefts_empty() {
let input: [either<int, int>] = [];
let input: [either<int, int>]/~ = []/~;
let result = lefts(input);
assert (vec::len(result) == 0u);
}
#[test]
fn test_rights() {
let input = [left(10), right(11), left(12), right(13), left(14)];
let input = [left(10), right(11), left(12), right(13), left(14)]/~;
let result = rights(input);
assert (result == [11, 13]);
assert (result == [11, 13]/~);
}
#[test]
fn test_rights_none() {
let input: [either<int, int>] = [left(10), left(10)];
let input: [either<int, int>]/~ = [left(10), left(10)]/~;
let result = rights(input);
assert (vec::len(result) == 0u);
}
#[test]
fn test_rights_empty() {
let input: [either<int, int>] = [];
let input: [either<int, int>]/~ = []/~;
let result = rights(input);
assert (vec::len(result) == 0u);
}
#[test]
fn test_partition() {
let input = [left(10), right(11), left(12), right(13), left(14)];
let input = [left(10), right(11), left(12), right(13), left(14)]/~;
let result = partition(input);
assert (result.lefts[0] == 10);
assert (result.lefts[1] == 12);
@@ -165,7 +165,7 @@ fn test_partition() {
#[test]
fn test_partition_no_lefts() {
let input: [either<int, int>] = [right(10), right(11)];
let input: [either<int, int>]/~ = [right(10), right(11)]/~;
let result = partition(input);
assert (vec::len(result.lefts) == 0u);
assert (vec::len(result.rights) == 2u);
@@ -173,7 +173,7 @@ fn test_partition_no_lefts() {
#[test]
fn test_partition_no_rights() {
let input: [either<int, int>] = [left(10), left(11)];
let input: [either<int, int>]/~ = [left(10), left(11)]/~;
let result = partition(input);
assert (vec::len(result.lefts) == 2u);
assert (vec::len(result.rights) == 0u);
@@ -181,7 +181,7 @@ fn test_partition_no_rights() {
#[test]
fn test_partition_empty() {
let input: [either<int, int>] = [];
let input: [either<int, int>]/~ = []/~;
let result = partition(input);
assert (vec::len(result.lefts) == 0u);
assert (vec::len(result.rights) == 0u);

View File

@@ -9,12 +9,12 @@ The 'fmt' extension is modeled on the posix printf system.
A posix conversion ostensibly looks like this
> %[parameter][flags][width][.precision][length]type
> %[parameter]/~[flags]/~[width]/~[.precision]/~[length]/~type
Given the different numeric type bestiary we have, we omit the 'length'
parameter and support slightly different conversions for 'type'
> %[parameter][flags][width][.precision]type
> %[parameter]/~[flags]/~[width]/~[.precision]/~type
we also only support translating-to-rust a tiny subset of the possible
combinations at the moment.
@@ -71,7 +71,7 @@ mod ct {
// A formatted conversion from an expression to a string
type conv =
{param: option<int>,
flags: [flag],
flags: [flag]/~,
width: count,
precision: count,
ty: ty};
@@ -81,14 +81,14 @@ mod ct {
enum piece { piece_string(str), piece_conv(conv), }
type error_fn = fn@(str) -> ! ;
fn parse_fmt_string(s: str, error: error_fn) -> [piece] {
let mut pieces: [piece] = [];
fn parse_fmt_string(s: str, error: error_fn) -> [piece]/~ {
let mut pieces: [piece]/~ = []/~;
let lim = str::len(s);
let mut buf = "";
fn flush_buf(buf: str, &pieces: [piece]) -> str {
fn flush_buf(buf: str, &pieces: [piece]/~) -> str {
if str::len(buf) > 0u {
let piece = piece_string(buf);
pieces += [piece];
pieces += [piece]/~;
}
ret "";
}
@@ -108,7 +108,7 @@ mod ct {
} else {
buf = flush_buf(buf, pieces);
let rs = parse_conversion(s, i, lim, error);
pieces += [rs.piece];
pieces += [rs.piece]/~;
i = rs.next;
}
} else { buf += curr; i += size; }
@@ -162,16 +162,16 @@ mod ct {
};
}
fn parse_flags(s: str, i: uint, lim: uint) ->
{flags: [flag], next: uint} {
let noflags: [flag] = [];
{flags: [flag]/~, next: uint} {
let noflags: [flag]/~ = []/~;
if i >= lim { ret {flags: noflags, next: i}; }
fn more_(f: flag, s: str, i: uint, lim: uint) ->
{flags: [flag], next: uint} {
{flags: [flag]/~, next: uint} {
let next = parse_flags(s, i + 1u, lim);
let rest = next.flags;
let j = next.next;
let curr: [flag] = [f];
let curr: [flag]/~ = [f]/~;
ret {flags: curr + rest, next: j};
}
let more = {|x|more_(x, s, i, lim)};
@@ -262,7 +262,7 @@ mod ct {
// Functions used by the fmt extension at runtime. For now there are a lot of
// decisions made a runtime. If it proves worthwhile then some of these
// conditions can be evaluated at compile-time. For now though it's cleaner to
// implement it this way, I think.
// implement it 0this way, I think.
mod rt {
enum flag {
flag_left_justify,
@@ -276,7 +276,7 @@ mod rt {
// FIXME (#1993): May not want to use a vector here for flags; instead
// just use a bool per flag.
type conv = {flags: [flag], width: count, precision: count, ty: ty};
type conv = {flags: [flag]/~, width: count, precision: count, ty: ty};
fn conv_int(cv: conv, i: int) -> str {
let radix = 10u;
@@ -430,12 +430,13 @@ mod rt {
}
ret padstr + s;
}
fn have_flag(flags: [flag], f: flag) -> bool {
fn have_flag(flags: [flag]/~, f: flag) -> bool {
for vec::each(flags) {|candidate| if candidate == f { ret true; } }
ret false;
}
}
// Local Variables:
// mode: rust;
// fill-column: 78;

View File

@@ -116,10 +116,10 @@ fn to_str_common(num: float, digits: uint, exact: bool) -> str {
let mut frac = num - (trunc as float);
// stack of digits
let mut fractionalParts = [];
let mut fractionalParts = []/~;
// FIXME: (#2608)
// This used to return right away without rounding, as "[-]num",
// This used to return right away without rounding, as "[-]/~num",
// but given epsilon like in f64.rs, I don't see how the comparison
// to epsilon did much when only used there.
// if (frac < epsilon && !exact) || digits == 0u { ret accum; }
@@ -236,7 +236,7 @@ Leading and trailing whitespace are ignored.
# Return value
`none` if the string did not represent a valid number. Otherwise, `some(n)`
where `n` is the floating-point number represented by `[num]`.
where `n` is the floating-point number represented by `[num]/~`.
"]
fn from_str(num: str) -> option<float> {
if num == "inf" {
@@ -261,7 +261,7 @@ fn from_str(num: str) -> option<float> {
_ { ret none; }
}
//Determine if first char is '-'/'+'. Set [pos] and [neg] accordingly.
//Determine if first char is '-'/'+'. Set [pos]/~ and [neg]/~ accordingly.
let mut neg = false; //Sign of the result
alt str::char_at(num, 0u) {
'-' {
@@ -345,7 +345,7 @@ fn from_str(num: str) -> option<float> {
pos = char_range.next;
}
let multiplier = pow_with_uint(10u, exponent);
//Note: not [int::pow], otherwise, we'll quickly
//Note: not [int::pow]/~, otherwise, we'll quickly
//end up with a nice overflow
if neg_exponent {
total = total / multiplier;

View File

@@ -66,7 +66,7 @@ Parse a buffer of bytes
* buf - A byte buffer
* radix - The base of the number
"]
fn parse_buf(buf: [u8], radix: uint) -> option<T> {
fn parse_buf(buf: [u8]/~, radix: uint) -> option<T> {
if vec::len(buf) == 0u { ret none; }
let mut i = vec::len(buf) - 1u;
let mut start = 0u;

View File

@@ -11,7 +11,7 @@ pure fn hash(&&x: int) -> uint { ret x as uint; }
#[doc = "Returns `base` raised to the power of `exponent`"]
fn pow(base: int, exponent: uint) -> int {
if exponent == 0u { ret 1; } //Not mathemtically true if [base == 0]
if exponent == 0u { ret 1; } //Not mathemtically true if [base == 0]/~
if base == 0 { ret 0; }
let mut my_pow = exponent;
let mut acc = 1;

View File

@@ -30,7 +30,7 @@ enum seek_style { seek_set, seek_end, seek_cur, }
// The raw underlying reader iface. All readers must implement this.
iface reader {
// FIXME (#2004): Seekable really should be orthogonal.
fn read_bytes(uint) -> [u8];
fn read_bytes(uint) -> [u8]/~;
fn read_byte() -> int;
fn unread_byte(int);
fn eof() -> bool;
@@ -41,9 +41,9 @@ iface reader {
// Generic utility functions defined on readers
impl reader_util for reader {
fn read_chars(n: uint) -> [char] {
fn read_chars(n: uint) -> [char]/~ {
// returns the (consumed offset, n_req), appends characters to &chars
fn chars_from_buf(buf: [u8], &chars: [char]) -> (uint, uint) {
fn chars_from_buf(buf: [u8]/~, &chars: [char]/~) -> (uint, uint) {
let mut i = 0u;
while i < vec::len(buf) {
let b0 = buf[i];
@@ -52,7 +52,7 @@ impl reader_util for reader {
i += 1u;
assert (w > 0u);
if w == 1u {
chars += [ b0 as char ];
chars += [ b0 as char ]/~;
cont;
}
// can't satisfy this char with the existing data
@@ -71,12 +71,12 @@ impl reader_util for reader {
// See str::char_at
val += ((b0 << ((w + 1u) as u8)) as uint)
<< (w - 1u) * 6u - w - 1u;
chars += [ val as char ];
chars += [ val as char ]/~;
}
ret (i, 0u);
}
let mut buf: [u8] = [];
let mut chars: [char] = [];
let mut buf: [u8]/~ = []/~;
let mut chars: [char]/~ = []/~;
// might need more bytes, but reading n will never over-read
let mut nbread = n;
while nbread > 0u {
@@ -110,20 +110,20 @@ impl reader_util for reader {
}
fn read_line() -> str {
let mut buf: [u8] = [];
let mut buf: [u8]/~ = []/~;
loop {
let ch = self.read_byte();
if ch == -1 || ch == 10 { break; }
buf += [ch as u8];
buf += [ch as u8]/~;
}
str::from_bytes(buf)
}
fn read_c_str() -> str {
let mut buf: [u8] = [];
let mut buf: [u8]/~ = []/~;
loop {
let ch = self.read_byte();
if ch < 1 { break; } else { buf += [ch as u8]; }
if ch < 1 { break; } else { buf += [ch as u8]/~; }
}
str::from_bytes(buf)
}
@@ -156,8 +156,8 @@ impl reader_util for reader {
val
}
fn read_whole_stream() -> [u8] {
let mut buf: [u8] = [];
fn read_whole_stream() -> [u8]/~ {
let mut buf: [u8]/~ = []/~;
while !self.eof() { buf += self.read_bytes(2048u); }
buf
}
@@ -192,8 +192,8 @@ fn convert_whence(whence: seek_style) -> i32 {
}
impl of reader for *libc::FILE {
fn read_bytes(len: uint) -> [u8] {
let mut buf : [mut u8] = [mut];
fn read_bytes(len: uint) -> [u8]/~ {
let mut buf : [mut u8]/~ = [mut]/~;
vec::reserve(buf, len);
vec::as_mut_buf(buf) {|b|
let read = libc::fread(b as *mut c_void, 1u as size_t,
@@ -216,7 +216,7 @@ impl of reader for *libc::FILE {
// duration of its lifetime.
// FIXME there really should be a better way to do this // #2004
impl <T: reader, C> of reader for {base: T, cleanup: C} {
fn read_bytes(len: uint) -> [u8] { self.base.read_bytes(len) }
fn read_bytes(len: uint) -> [u8]/~ { self.base.read_bytes(len) }
fn read_byte() -> int { self.base.read_byte() }
fn unread_byte(byte: int) { self.base.unread_byte(byte); }
fn eof() -> bool { self.base.eof() }
@@ -260,10 +260,10 @@ fn file_reader(path: str) -> result<reader, str> {
// Byte buffer readers
// TODO: const u8, but this fails with rustboot.
type byte_buf = {buf: [u8], mut pos: uint, len: uint};
type byte_buf = {buf: [u8]/~, mut pos: uint, len: uint};
impl of reader for byte_buf {
fn read_bytes(len: uint) -> [u8] {
fn read_bytes(len: uint) -> [u8]/~ {
let rest = self.len - self.pos;
let mut to_read = len;
if rest < to_read { to_read = rest; }
@@ -286,19 +286,19 @@ impl of reader for byte_buf {
fn tell() -> uint { self.pos }
}
fn bytes_reader(bytes: [u8]) -> reader {
fn bytes_reader(bytes: [u8]/~) -> reader {
bytes_reader_between(bytes, 0u, vec::len(bytes))
}
fn bytes_reader_between(bytes: [u8], start: uint, end: uint) -> reader {
fn bytes_reader_between(bytes: [u8]/~, start: uint, end: uint) -> reader {
{buf: bytes, mut pos: start, len: end} as reader
}
fn with_bytes_reader<t>(bytes: [u8], f: fn(reader) -> t) -> t {
fn with_bytes_reader<t>(bytes: [u8]/~, f: fn(reader) -> t) -> t {
f(bytes_reader(bytes))
}
fn with_bytes_reader_between<t>(bytes: [u8], start: uint, end: uint,
fn with_bytes_reader_between<t>(bytes: [u8]/~, start: uint, end: uint,
f: fn(reader) -> t) -> t {
f(bytes_reader_between(bytes, start, end))
}
@@ -402,7 +402,7 @@ fn fd_writer(fd: fd_t, cleanup: bool) -> writer {
}
fn mk_file_writer(path: str, flags: [fileflag])
fn mk_file_writer(path: str, flags: [fileflag]/~)
-> result<writer, str> {
#[cfg(windows)]
@@ -451,9 +451,9 @@ fn u64_to_le_bytes<T>(n: u64, size: uint, f: fn([u8]/&) -> T) -> T {
(n >> 56) as u8]/&) }
_ {
let mut bytes: [u8] = [], i = size, n = n;
let mut bytes: [u8]/~ = []/~, i = size, n = n;
while i > 0u {
bytes += [(n & 255_u64) as u8];
bytes += [(n & 255_u64) as u8]/~;
n >>= 8_u64;
i -= 1u;
}
@@ -481,11 +481,11 @@ fn u64_to_be_bytes<T>(n: u64, size: uint, f: fn([u8]/&) -> T) -> T {
(n >> 8) as u8,
n as u8]/&) }
_ {
let mut bytes: [u8] = [];
let mut bytes: [u8]/~ = []/~;
let mut i = size;
while i > 0u {
let shift = ((i - 1u) * 8u) as u64;
bytes += [(n >> shift) as u8];
bytes += [(n >> shift) as u8]/~;
i -= 1u;
}
f(bytes)
@@ -493,7 +493,7 @@ fn u64_to_be_bytes<T>(n: u64, size: uint, f: fn([u8]/&) -> T) -> T {
}
}
fn u64_from_be_bytes(data: [u8], start: uint, size: uint) -> u64 {
fn u64_from_be_bytes(data: [u8]/~, start: uint, size: uint) -> u64 {
let mut sz = size;
assert (sz <= 8u);
let mut val = 0_u64;
@@ -577,7 +577,7 @@ impl writer_util for writer {
fn write_u8(n: u8) { self.write([n]/&) }
}
fn file_writer(path: str, flags: [fileflag]) -> result<writer, str> {
fn file_writer(path: str, flags: [fileflag]/~) -> result<writer, str> {
result::chain(mk_file_writer(path, flags), { |w| result::ok(w)})
}
@@ -638,7 +638,7 @@ fn mem_buffer() -> mem_buffer {
@{buf: dvec(), mut pos: 0u}
}
fn mem_buffer_writer(b: mem_buffer) -> writer { b as writer }
fn mem_buffer_buf(b: mem_buffer) -> [u8] { b.buf.get() }
fn mem_buffer_buf(b: mem_buffer) -> [u8]/~ { b.buf.get() }
fn mem_buffer_str(b: mem_buffer) -> str {
str::from_bytes(b.buf.get())
}
@@ -650,7 +650,7 @@ fn with_str_writer(f: fn(writer)) -> str {
io::mem_buffer_str(buf)
}
fn with_buf_writer(f: fn(writer)) -> [u8] {
fn with_buf_writer(f: fn(writer)) -> [u8]/~ {
let buf = mem_buffer();
let wr = mem_buffer_writer(buf);
f(wr);
@@ -679,7 +679,7 @@ fn read_whole_file_str(file: str) -> result<str, str> {
// FIXME (#2004): implement this in a low-level way. Going through the
// abstractions is pointless.
fn read_whole_file(file: str) -> result<[u8], str> {
fn read_whole_file(file: str) -> result<[u8]/~, str> {
result::chain(file_reader(file), { |rdr|
result::ok(rdr.read_whole_stream())
})
@@ -772,7 +772,7 @@ mod tests {
{
let out: io::writer =
result::get(
io::file_writer(tmpfile, [io::create, io::truncate]));
io::file_writer(tmpfile, [io::create, io::truncate]/~));
out.write_str(frood);
}
let inp: io::reader = result::get(io::file_reader(tmpfile));
@@ -784,22 +784,22 @@ mod tests {
#[test]
fn test_readchars_empty() {
let inp : io::reader = io::str_reader("");
let res : [char] = inp.read_chars(128u);
let res : [char]/~ = inp.read_chars(128u);
assert(vec::len(res) == 0u);
}
#[test]
fn test_readchars_wide() {
let wide_test = "生锈的汤匙切肉汤hello生锈的汤匙切肉汤";
let ivals : [int] = [
let ivals : [int]/~ = [
29983, 38152, 30340, 27748,
21273, 20999, 32905, 27748,
104, 101, 108, 108, 111,
29983, 38152, 30340, 27748,
21273, 20999, 32905, 27748];
fn check_read_ln(len : uint, s: str, ivals: [int]) {
21273, 20999, 32905, 27748]/~;
fn check_read_ln(len : uint, s: str, ivals: [int]/~) {
let inp : io::reader = io::str_reader(s);
let res : [char] = inp.read_chars(len);
let res : [char]/~ = inp.read_chars(len);
if (len <= vec::len(ivals)) {
assert(vec::len(res) == len);
}
@@ -841,7 +841,7 @@ mod tests {
#[test]
fn file_writer_bad_name() {
alt io::file_writer("?/?", []) {
alt io::file_writer("?/?", []/~) {
result::err(e) {
assert str::starts_with(e, "error opening ?/?");
}
@@ -862,16 +862,16 @@ mod tests {
#[test]
fn mem_buffer_overwrite() {
let mbuf = mem_buffer();
mbuf.write([0u8, 1u8, 2u8, 3u8]);
assert mem_buffer_buf(mbuf) == [0u8, 1u8, 2u8, 3u8];
mbuf.write([0u8, 1u8, 2u8, 3u8]/~);
assert mem_buffer_buf(mbuf) == [0u8, 1u8, 2u8, 3u8]/~;
mbuf.seek(-2, seek_cur);
mbuf.write([4u8, 5u8, 6u8, 7u8]);
assert mem_buffer_buf(mbuf) == [0u8, 1u8, 4u8, 5u8, 6u8, 7u8];
mbuf.write([4u8, 5u8, 6u8, 7u8]/~);
assert mem_buffer_buf(mbuf) == [0u8, 1u8, 4u8, 5u8, 6u8, 7u8]/~;
mbuf.seek(-2, seek_end);
mbuf.write([8u8]);
mbuf.write([8u8]/~);
mbuf.seek(1, seek_set);
mbuf.write([9u8]);
assert mem_buffer_buf(mbuf) == [0u8, 9u8, 4u8, 5u8, 8u8, 7u8];
mbuf.write([9u8]/~);
assert mem_buffer_buf(mbuf) == [0u8, 9u8, 4u8, 5u8, 8u8, 7u8]/~;
}
}

View File

@@ -19,14 +19,14 @@ impl extensions<A> of iter::base_iter<A> for IMPL_T<A> {
}
impl extensions<A:copy> for IMPL_T<A> {
fn filter_to_vec(pred: fn(A) -> bool) -> [A] {
fn filter_to_vec(pred: fn(A) -> bool) -> [A]/~ {
iter::filter_to_vec(self, pred)
}
fn map_to_vec<B>(op: fn(A) -> B) -> [B] { iter::map_to_vec(self, op) }
fn to_vec() -> [A] { iter::to_vec(self) }
fn map_to_vec<B>(op: fn(A) -> B) -> [B]/~ { iter::map_to_vec(self, op) }
fn to_vec() -> [A]/~ { iter::to_vec(self) }
// FIXME--bug in resolve prevents this from working (#2611)
// fn flat_map_to_vec<B:copy,IB:base_iter<B>>(op: fn(A) -> IB) -> [B] {
// fn flat_map_to_vec<B:copy,IB:base_iter<B>>(op: fn(A) -> IB) -> [B]/~ {
// iter::flat_map_to_vec(self, op)
// }

View File

@@ -26,8 +26,8 @@ fn any<A,IA:base_iter<A>>(self: IA, blk: fn(A) -> bool) -> bool {
}
fn filter_to_vec<A:copy,IA:base_iter<A>>(self: IA,
prd: fn(A) -> bool) -> [A] {
let mut result = [];
prd: fn(A) -> bool) -> [A]/~ {
let mut result = []/~;
self.size_hint().iter {|hint| vec::reserve(result, hint); }
for self.each {|a|
if prd(a) { vec::push(result, a); }
@@ -35,8 +35,8 @@ fn filter_to_vec<A:copy,IA:base_iter<A>>(self: IA,
ret result;
}
fn map_to_vec<A:copy,B,IA:base_iter<A>>(self: IA, op: fn(A) -> B) -> [B] {
let mut result = [];
fn map_to_vec<A:copy,B,IA:base_iter<A>>(self: IA, op: fn(A) -> B) -> [B]/~ {
let mut result = []/~;
self.size_hint().iter {|hint| vec::reserve(result, hint); }
for self.each {|a|
vec::push(result, op(a));
@@ -45,9 +45,9 @@ fn map_to_vec<A:copy,B,IA:base_iter<A>>(self: IA, op: fn(A) -> B) -> [B] {
}
fn flat_map_to_vec<A:copy,B:copy,IA:base_iter<A>,IB:base_iter<B>>(
self: IA, op: fn(A) -> IB) -> [B] {
self: IA, op: fn(A) -> IB) -> [B]/~ {
let mut result = [];
let mut result = []/~;
for self.each {|a|
for op(a).each {|b|
vec::push(result, b);
@@ -64,8 +64,8 @@ fn foldl<A,B,IA:base_iter<A>>(self: IA, +b0: B, blk: fn(B, A) -> B) -> B {
ret b;
}
fn to_vec<A:copy,IA:base_iter<A>>(self: IA) -> [A] {
foldl::<A,[A],IA>(self, [], {|r, a| r + [a]})
fn to_vec<A:copy,IA:base_iter<A>>(self: IA) -> [A]/~ {
foldl::<A,[A]/~,IA>(self, []/~, {|r, a| r + [a]/~})
}
fn contains<A,IA:base_iter<A>>(self: IA, x: A) -> bool {
@@ -135,17 +135,17 @@ fn test_enumerate() {
#[test]
fn test_map_and_to_vec() {
let a = bind vec::iter([0, 1, 2], _);
let a = bind vec::iter([0, 1, 2]/~, _);
let b = bind map(a, {|i| 2*i}, _);
let c = to_vec(b);
assert c == [0, 2, 4];
assert c == [0, 2, 4]/~;
}
#[test]
fn test_map_directly_on_vec() {
let b = bind map([0, 1, 2], {|i| 2*i}, _);
let b = bind map([0, 1, 2]/~, {|i| 2*i}, _);
let c = to_vec(b);
assert c == [0, 2, 4];
assert c == [0, 2, 4]/~;
}
#[test]
@@ -155,7 +155,7 @@ fn test_filter_on_int_range() {
}
let l = to_vec(bind filter(bind int::range(0, 10, _), is_even, _));
assert l == [0, 2, 4, 6, 8];
assert l == [0, 2, 4, 6, 8]/~;
}
#[test]
@@ -165,7 +165,7 @@ fn test_filter_on_uint_range() {
}
let l = to_vec(bind filter(bind uint::range(0u, 10u, _), is_even, _));
assert l == [0u, 2u, 4u, 6u, 8u];
assert l == [0u, 2u, 4u, 6u, 8u]/~;
}
#[test]
@@ -180,7 +180,7 @@ fn test_filter_map() {
let l = to_vec(bind filter_map(
bind int::range(0, 5, _), negativate_the_evens, _));
assert l == [0, -2, -4];
assert l == [0, -2, -4]/~;
}
#[test]
@@ -190,70 +190,70 @@ fn test_flat_map_with_option() {
else { none }
}
let a = bind vec::iter([0, 1, 2], _);
let a = bind vec::iter([0, 1, 2]/~, _);
let b = bind flat_map(a, if_even, _);
let c = to_vec(b);
assert c == [0, 2];
assert c == [0, 2]/~;
}
#[test]
fn test_flat_map_with_list() {
fn repeat(&&i: int) -> [int] {
let mut r = [];
int::range(0, i) {|_j| r += [i]; }
fn repeat(&&i: int) -> [int]/~ {
let mut r = []/~;
int::range(0, i) {|_j| r += [i]/~; }
r
}
let a = bind vec::iter([0, 1, 2, 3], _);
let a = bind vec::iter([0, 1, 2, 3]/~, _);
let b = bind flat_map(a, repeat, _);
let c = to_vec(b);
#debug["c = %?", c];
assert c == [1, 2, 2, 3, 3, 3];
assert c == [1, 2, 2, 3, 3, 3]/~;
}
#[test]
fn test_repeat() {
let mut c = [], i = 0u;
let mut c = []/~, i = 0u;
repeat(5u) {||
c += [(i * i)];
c += [(i * i)]/~;
i += 1u;
};
#debug["c = %?", c];
assert c == [0u, 1u, 4u, 9u, 16u];
assert c == [0u, 1u, 4u, 9u, 16u]/~;
}
#[test]
fn test_min() {
assert min([5, 4, 1, 2, 3]) == 1;
assert min([5, 4, 1, 2, 3]/~) == 1;
}
#[test]
#[should_fail]
#[ignore(cfg(windows))]
fn test_min_empty() {
min::<int, [int]>([]);
min::<int, [int]/~>([]/~);
}
#[test]
fn test_max() {
assert max([1, 2, 4, 2, 3]) == 4;
assert max([1, 2, 4, 2, 3]/~) == 4;
}
#[test]
#[should_fail]
#[ignore(cfg(windows))]
fn test_max_empty() {
max::<int, [int]>([]);
max::<int, [int]/~>([]/~);
}
#[test]
fn test_reversed() {
assert to_vec(bind reversed([1, 2, 3], _)) == [3, 2, 1];
assert to_vec(bind reversed([1, 2, 3]/~, _)) == [3, 2, 1]/~;
}
#[test]
fn test_count() {
assert count([1, 2, 1, 2, 1], 1) == 3u;
assert count([1, 2, 1, 2, 1]/~, 1) == 3u;
}
#[test]
@@ -261,7 +261,7 @@ fn test_foldr() {
fn sub(&&a: int, &&b: int) -> int {
a - b
}
let sum = foldr([1, 2, 3, 4], 0, sub);
let sum = foldr([1, 2, 3, 4]/~, 0, sub);
assert sum == -2;
}
*/

View File

@@ -40,23 +40,23 @@ export walk_dir;
export as_c_charp, fill_charp_buf;
native mod rustrt {
fn rust_env_pairs() -> [str];
fn rust_env_pairs() -> [str]/~;
fn rust_getcwd() -> str;
fn rust_path_is_dir(path: *libc::c_char) -> c_int;
fn rust_path_exists(path: *libc::c_char) -> c_int;
fn rust_list_files(path: str) -> [str];
fn rust_list_files(path: str) -> [str]/~;
fn rust_process_wait(handle: c_int) -> c_int;
fn last_os_error() -> str;
fn rust_set_exit_status(code: libc::intptr_t);
}
fn env() -> [(str,str)] {
let mut pairs = [];
fn env() -> [(str,str)]/~ {
let mut pairs = []/~;
for vec::each(rustrt::rust_env_pairs()) {|p|
let vs = str::splitn_char(p, '=', 1u);
assert vec::len(vs) == 2u;
pairs += [(vs[0], vs[1])];
vec::push(pairs, (vs[0], vs[1]));
}
ret pairs;
}
@@ -116,7 +116,7 @@ mod win32 {
fn as_utf16_p<T>(s: str, f: fn(*u16) -> T) -> T {
let mut t = str::to_utf16(s);
// Null terminate before passing on.
t += [0u16];
t += [0u16]/~;
vec::as_buf(t, f)
}
}
@@ -373,7 +373,7 @@ fn self_exe_path() -> option<path> {
fill_charp_buf() {|buf, sz|
let mib = [CTL_KERN as c_int,
KERN_PROC as c_int,
KERN_PROC_PATHNAME as c_int, -1 as c_int];
KERN_PROC_PATHNAME as c_int, -1 as c_int]/~;
sysctl(vec::unsafe::to_ptr(mib), vec::len(mib) as c_uint,
buf as *mut c_void, ptr::mut_addr_of(sz),
ptr::null(), 0u as size_t) == (0 as c_int)
@@ -553,7 +553,7 @@ fn make_dir(p: path, mode: c_int) -> bool {
}
#[doc = "Lists the contents of a directory"]
fn list_dir(p: path) -> [str] {
fn list_dir(p: path) -> [str]/~ {
#[cfg(unix)]
fn star(p: str) -> str { p }
@@ -579,7 +579,7 @@ Lists the contents of a directory
This version prepends each entry with the directory.
"]
fn list_dir_path(p: path) -> [str] {
fn list_dir_path(p: path) -> [str]/~ {
let mut p = p;
let pl = str::len(p);
if pl == 0u || (p[pl - 1u] as char != path::consts::path_sep
@@ -670,7 +670,7 @@ fn copy_file(from: path, to: path) -> bool {
fclose(istream);
ret false;
}
let mut buf : [mut u8] = [mut];
let mut buf : [mut u8]/~ = [mut]/~;
let bufsize = 8192u;
vec::reserve(buf, bufsize);
let mut done = false;
@@ -978,7 +978,7 @@ mod tests {
};
assert (ostream as uint != 0u);
let s = "hello";
let mut buf = vec::to_mut(str::bytes(s) + [0 as u8]);
let mut buf = vec::to_mut(str::bytes(s) + [0 as u8]/~);
vec::as_mut_buf(buf) {|b|
assert (libc::fwrite(b as *c_void, 1u as size_t,
(str::len(s) + 1u) as size_t, ostream)
@@ -989,7 +989,7 @@ mod tests {
fail (#fmt("%s doesn't exist", in));
}
assert(rs);
let rslt = run::run_program("diff", [in, out]);
let rslt = run::run_program("diff", [in, out]/~);
assert (rslt == 0);
assert (remove_file(in));
assert (remove_file(out));

View File

@@ -127,7 +127,7 @@ Connects a vector of path segments into a single path.
Inserts path separators as needed.
"]
fn connect_many(paths: [path]) -> path {
fn connect_many(paths: [path]/~) -> path {
ret if vec::len(paths) == 1u {
paths[0]
} else {
@@ -144,7 +144,7 @@ each piece of the path. On Windows, if the path is absolute then
the first element of the returned vector will be the drive letter
followed by a colon.
"]
fn split(p: path) -> [path] {
fn split(p: path) -> [path]/~ {
str::split_nonempty(p, {|c|
c == consts::path_sep || c == consts::alt_path_sep
})
@@ -234,7 +234,7 @@ fn normalize(p: path) -> path {
ret s;
fn strip_dots(s: [path]) -> [path] {
fn strip_dots(s: [path]/~) -> [path]/~ {
vec::filter_map(s, { |elem|
if elem == "." {
option::none
@@ -244,12 +244,12 @@ fn normalize(p: path) -> path {
})
}
fn rollup_doubledots(s: [path]) -> [path] {
fn rollup_doubledots(s: [path]/~) -> [path]/~ {
if vec::is_empty(s) {
ret [];
ret []/~;
}
let mut t = [];
let mut t = []/~;
let mut i = vec::len(s);
let mut skip = 0;
while i != 0u {
@@ -258,7 +258,7 @@ fn normalize(p: path) -> path {
skip += 1;
} else {
if skip == 0 {
t += [s[i]];
vec::push(t, s[i]);
} else {
skip -= 1;
}
@@ -266,7 +266,7 @@ fn normalize(p: path) -> path {
}
let mut t = vec::reversed(t);
while skip > 0 {
t += [".."];
vec::push(t, "..");
skip -= 1;
}
ret t;
@@ -322,28 +322,28 @@ mod tests {
#[test]
fn split1() {
let actual = split("a" + ps() + "b");
let expected = ["a", "b"];
let expected = ["a", "b"]/~;
assert actual == expected;
}
#[test]
fn split2() {
let actual = split("a" + aps() + "b");
let expected = ["a", "b"];
let expected = ["a", "b"]/~;
assert actual == expected;
}
#[test]
fn split3() {
let actual = split(ps() + "a" + ps() + "b");
let expected = ["a", "b"];
let expected = ["a", "b"]/~;
assert actual == expected;
}
#[test]
fn split4() {
let actual = split("a" + ps() + "b" + aps() + "c");
let expected = ["a", "b", "c"];
let expected = ["a", "b", "c"]/~;
assert actual == expected;
}

View File

@@ -149,8 +149,8 @@ fn test() {
assert (p.fst == 50);
assert (p.snd == 60);
let v0 = [32000u16, 32001u16, 32002u16];
let v1 = [0u16, 0u16, 0u16];
let v0 = [32000u16, 32001u16, 32002u16]/~;
let v1 = [0u16, 0u16, 0u16]/~;
ptr::memcpy(ptr::offset(vec::unsafe::to_ptr(v1), 1u),
ptr::offset(vec::unsafe::to_ptr(v0), 1u), 1u);
@@ -185,7 +185,7 @@ fn test_buf_len() {
str::as_c_str(s0) {|p0|
str::as_c_str(s1) {|p1|
str::as_c_str(s2) {|p2|
let v = [p0, p1, p2, null()];
let v = [p0, p1, p2, null()]/~;
vec::as_buf(v) {|vp|
assert unsafe { buf_len(vp) } == 3u;
}

View File

@@ -7,9 +7,9 @@ enum rctx {}
#[abi = "cdecl"]
native mod rustrt {
fn rand_seed() -> [u8];
fn rand_seed() -> [u8]/~;
fn rand_new() -> *rctx;
fn rand_new_seeded(seed: [u8]) -> *rctx;
fn rand_new_seeded(seed: [u8]/~) -> *rctx;
fn rand_next(c: *rctx) -> u32;
fn rand_free(c: *rctx);
}
@@ -151,19 +151,19 @@ impl extensions for rng {
}
#[doc = "Return a random byte string of the specified length"]
fn gen_bytes(len: uint) -> [u8] {
fn gen_bytes(len: uint) -> [u8]/~ {
vec::from_fn(len) {|_i|
self.gen_u8()
}
}
#[doc = "Choose an item randomly, failing if values is empty"]
fn choose<T:copy>(values: [T]) -> T {
fn choose<T:copy>(values: [T]/~) -> T {
self.choose_option(values).get()
}
#[doc = "Choose some(item) randomly, returning none if values is empty"]
fn choose_option<T:copy>(values: [T]) -> option<T> {
fn choose_option<T:copy>(values: [T]/~) -> option<T> {
if values.is_empty() {
none
} else {
@@ -173,13 +173,13 @@ impl extensions for rng {
#[doc = "Choose an item respecting the relative weights, failing if \
the sum of the weights is 0"]
fn choose_weighted<T: copy>(v : [weighted<T>]) -> T {
fn choose_weighted<T: copy>(v : [weighted<T>]/~) -> T {
self.choose_weighted_option(v).get()
}
#[doc = "Choose some(item) respecting the relative weights, returning \
none if the sum of the weights is 0"]
fn choose_weighted_option<T:copy>(v: [weighted<T>]) -> option<T> {
fn choose_weighted_option<T:copy>(v: [weighted<T>]/~) -> option<T> {
let mut total = 0u;
for v.each {|item|
total += item.weight;
@@ -200,25 +200,25 @@ impl extensions for rng {
#[doc = "Return a vec containing copies of the items, in order, where \
the weight of the item determines how many copies there are"]
fn weighted_vec<T:copy>(v: [weighted<T>]) -> [T] {
let mut r = [];
fn weighted_vec<T:copy>(v: [weighted<T>]/~) -> [T]/~ {
let mut r = []/~;
for v.each {|item|
for uint::range(0u, item.weight) {|_i|
r += [item.item];
r += [item.item]/~;
}
}
r
}
#[doc = "Shuffle a vec"]
fn shuffle<T:copy>(values: [T]) -> [T] {
fn shuffle<T:copy>(values: [T]/~) -> [T]/~ {
let mut m = vec::to_mut(values);
self.shuffle_mut(m);
ret vec::from_mut(m);
}
#[doc = "Shuffle a mutable vec in place"]
fn shuffle_mut<T>(&&values: [mut T]) {
fn shuffle_mut<T>(&&values: [mut T]/~) {
let mut i = values.len();
while i >= 2u {
// invariant: elements with index >= i have been locked in place.
@@ -241,7 +241,7 @@ impl of rng for @rand_res {
}
#[doc = "Create a new random seed for seeded_rng"]
fn seed() -> [u8] {
fn seed() -> [u8]/~ {
rustrt::rand_seed()
}
@@ -254,7 +254,7 @@ fn rng() -> rng {
generator constructed with a given seed will generate the same \
sequence of values as all other generators constructed with the \
same seed. The seed may be any length."]
fn seeded_rng(seed: [u8]) -> rng {
fn seeded_rng(seed: [u8]/~) -> rng {
@rand_res(rustrt::rand_new_seeded(seed)) as rng
}
@@ -301,7 +301,7 @@ mod tests {
#[test]
fn rng_seeded_custom_seed() {
// much shorter than generated seeds which are 1024 bytes
let seed = [2u8, 32u8, 4u8, 32u8, 51u8];
let seed = [2u8, 32u8, 4u8, 32u8, 51u8]/~;
let ra = rand::seeded_rng(seed);
let rb = rand::seeded_rng(seed);
assert ra.gen_str(100u) == rb.gen_str(100u);
@@ -309,7 +309,7 @@ mod tests {
#[test]
fn rng_seeded_custom_seed2() {
let seed = [2u8, 32u8, 4u8, 32u8, 51u8];
let seed = [2u8, 32u8, 4u8, 32u8, 51u8]/~;
let ra = rand::seeded_rng(seed);
// Regression test that isaac is actually using the above vector
let r = ra.next();
@@ -387,55 +387,56 @@ mod tests {
#[test]
fn choose() {
let r = rand::rng();
assert r.choose([1, 1, 1]) == 1;
assert r.choose([1, 1, 1]/~) == 1;
}
#[test]
fn choose_option() {
let r = rand::rng();
assert r.choose_option([]) == none::<int>;
assert r.choose_option([1, 1, 1]) == some(1);
assert r.choose_option([]/~) == none::<int>;
assert r.choose_option([1, 1, 1]/~) == some(1);
}
#[test]
fn choose_weighted() {
let r = rand::rng();
assert r.choose_weighted([{weight: 1u, item: 42}]) == 42;
assert r.choose_weighted([{weight: 1u, item: 42}]/~) == 42;
assert r.choose_weighted([
{weight: 0u, item: 42},
{weight: 1u, item: 43}
]) == 43;
]/~) == 43;
}
#[test]
fn choose_weighted_option() {
let r = rand::rng();
assert r.choose_weighted_option([{weight: 1u, item: 42}]) == some(42);
assert r.choose_weighted_option([{weight: 1u, item: 42}]/~) ==
some(42);
assert r.choose_weighted_option([
{weight: 0u, item: 42},
{weight: 1u, item: 43}
]) == some(43);
assert r.choose_weighted_option([]) == none::<int>;
]/~) == some(43);
assert r.choose_weighted_option([]/~) == none::<int>;
}
#[test]
fn weighted_vec() {
let r = rand::rng();
let empty: [int] = [];
assert r.weighted_vec([]) == empty;
let empty: [int]/~ = []/~;
assert r.weighted_vec([]/~) == empty;
assert r.weighted_vec([
{weight: 0u, item: 3u},
{weight: 1u, item: 2u},
{weight: 2u, item: 1u}
]) == [2u, 1u, 1u];
]/~) == [2u, 1u, 1u]/~;
}
#[test]
fn shuffle() {
let r = rand::rng();
let empty: [int] = [];
assert r.shuffle([]) == empty;
assert r.shuffle([1, 1, 1]) == [1, 1, 1];
let empty: [int]/~ = []/~;
assert r.shuffle([]/~) == empty;
assert r.shuffle([1, 1, 1]/~) == [1, 1, 1]/~;
}
}

View File

@@ -245,18 +245,18 @@ checking for overflow:
if x == uint::max_value { ret err(\"overflow\"); }
else { ret ok(x+1u); }
}
map([1u, 2u, 3u], inc_conditionally).chain {|incd|
assert incd == [2u, 3u, 4u];
map([1u, 2u, 3u]/~, inc_conditionally).chain {|incd|
assert incd == [2u, 3u, 4u]/~;
}
"]
fn map_vec<T,U:copy,V:copy>(
ts: [T], op: fn(T) -> result<V,U>) -> result<[V],U> {
ts: [T]/~, op: fn(T) -> result<V,U>) -> result<[V]/~,U> {
let mut vs: [V] = [];
let mut vs: [V]/~ = []/~;
vec::reserve(vs, vec::len(ts));
for vec::each(ts) {|t|
alt op(t) {
ok(v) { vs += [v]; }
ok(v) { vs += [v]/~; }
err(u) { ret err(u); }
}
}
@@ -284,16 +284,17 @@ length. While we do not often use preconditions in the standard
library, a precondition is used here because result::t is generally
used in 'careful' code contexts where it is both appropriate and easy
to accommodate an error like the vectors being of different lengths."]
fn map_vec2<S,T,U:copy,V:copy>(ss: [S], ts: [T], op: fn(S,T) -> result<V,U>)
: vec::same_length(ss, ts) -> result<[V],U> {
fn map_vec2<S,T,U:copy,V:copy>(ss: [S]/~, ts: [T]/~,
op: fn(S,T) -> result<V,U>)
: vec::same_length(ss, ts) -> result<[V]/~,U> {
let n = vec::len(ts);
let mut vs = [];
let mut vs = []/~;
vec::reserve(vs, n);
let mut i = 0u;
while i < n {
alt op(ss[i],ts[i]) {
ok(v) { vs += [v]; }
ok(v) { vs += [v]/~; }
err(u) { ret err(u); }
}
i += 1u;
@@ -306,7 +307,7 @@ Applies op to the pairwise elements from `ss` and `ts`, aborting on
error. This could be implemented using `map2()` but it is more efficient
on its own as no result vector is built.
"]
fn iter_vec2<S,T,U:copy>(ss: [S], ts: [T],
fn iter_vec2<S,T,U:copy>(ss: [S]/~, ts: [T]/~,
op: fn(S,T) -> result<(),U>)
: vec::same_length(ss, ts)
-> result<(),U> {

View File

@@ -62,8 +62,8 @@ Run a program, providing stdin, stdout and stderr handles
The process id of the spawned process
"]
fn spawn_process(prog: str, args: [str],
env: option<[(str,str)]>,
fn spawn_process(prog: str, args: [str]/~,
env: option<[(str,str)]/~>,
dir: option<str>,
in_fd: c_int, out_fd: c_int, err_fd: c_int)
-> pid_t {
@@ -77,36 +77,36 @@ fn spawn_process(prog: str, args: [str],
}
}
fn with_argv<T>(prog: str, args: [str],
fn with_argv<T>(prog: str, args: [str]/~,
cb: fn(**libc::c_char) -> T) -> T {
let mut argptrs = str::as_c_str(prog) {|b| [b] };
let mut tmps = [];
let mut argptrs = str::as_c_str(prog) {|b| [b]/~ };
let mut tmps = []/~;
for vec::each(args) {|arg|
let t = @arg;
tmps += [t];
argptrs += str::as_c_str(*t) {|b| [b] };
tmps += [t]/~;
argptrs += str::as_c_str(*t) {|b| [b]/~ };
}
argptrs += [ptr::null()];
argptrs += [ptr::null()]/~;
vec::as_buf(argptrs, cb)
}
#[cfg(unix)]
fn with_envp<T>(env: option<[(str,str)]>,
fn with_envp<T>(env: option<[(str,str)]/~>,
cb: fn(*c_void) -> T) -> T {
// On posixy systems we can pass a char** for envp, which is
// a null-terminated array of "k=v\n" strings.
alt env {
some(es) if !vec::is_empty(es) {
let mut tmps = [];
let mut ptrs = [];
let mut tmps = []/~;
let mut ptrs = []/~;
for vec::each(es) {|e|
let (k,v) = e;
let t = @(#fmt("%s=%s", k, v));
vec::push(tmps, t);
ptrs += str::as_c_str(*t) {|b| [b]};
ptrs += str::as_c_str(*t) {|b| [b]/~};
}
ptrs += [ptr::null()];
ptrs += [ptr::null()]/~;
vec::as_buf(ptrs) { |p|
unsafe { cb(::unsafe::reinterpret_cast(p)) }
}
@@ -118,7 +118,7 @@ fn with_envp<T>(env: option<[(str,str)]>,
}
#[cfg(windows)]
fn with_envp<T>(env: option<[(str,str)]>,
fn with_envp<T>(env: option<[(str,str)]/~>,
cb: fn(*c_void) -> T) -> T {
// On win32 we pass an "environment block" which is not a char**, but
// rather a concatenation of null-terminated k=v\0 sequences, with a final
@@ -126,15 +126,15 @@ fn with_envp<T>(env: option<[(str,str)]>,
unsafe {
alt env {
some(es) if !vec::is_empty(es) {
let mut blk : [u8] = [];
let mut blk : [u8]/~ = []/~;
for vec::each(es) {|e|
let (k,v) = e;
let t = #fmt("%s=%s", k, v);
let mut v : [u8] = ::unsafe::reinterpret_cast(t);
let mut v : [u8]/~ = ::unsafe::reinterpret_cast(t);
blk += v;
::unsafe::forget(v);
}
blk += [0_u8];
blk += [0_u8]/~;
vec::as_buf(blk) {|p| cb(::unsafe::reinterpret_cast(p)) }
}
_ {
@@ -164,7 +164,7 @@ Spawns a process and waits for it to terminate
The process id
"]
fn run_program(prog: str, args: [str]) -> int {
fn run_program(prog: str, args: [str]/~) -> int {
let pid = spawn_process(prog, args, none, none,
0i32, 0i32, 0i32);
if pid == -1 as pid_t { fail; }
@@ -187,7 +187,7 @@ The class will ensure that file descriptors are closed properly.
A class with a <program> field
"]
fn start_program(prog: str, args: [str]) -> program {
fn start_program(prog: str, args: [str]/~) -> program {
let pipe_input = os::pipe();
let pipe_output = os::pipe();
let pipe_err = os::pipe();
@@ -271,7 +271,7 @@ contents of stdout and stderr.
A record, {status: int, out: str, err: str} containing the exit code,
the contents of stdout and the contents of stderr.
"]
fn program_output(prog: str, args: [str]) ->
fn program_output(prog: str, args: [str]/~) ->
{status: int, out: str, err: str} {
let pipe_in = os::pipe();
@@ -397,9 +397,9 @@ mod tests {
// Regression test for memory leaks
#[ignore(cfg(windows))] // FIXME (#2626)
fn test_leaks() {
run::run_program("echo", []);
run::start_program("echo", []);
run::program_output("echo", []);
run::run_program("echo", []/~);
run::start_program("echo", []/~);
run::program_output("echo", []/~);
}
#[test]
@@ -410,7 +410,7 @@ mod tests {
let pid =
run::spawn_process(
"cat", [], none, none,
"cat", []/~, none, none,
pipe_in.in, pipe_out.out, pipe_err.out);
os::close(pipe_in.in);
os::close(pipe_out.out);
@@ -430,7 +430,7 @@ mod tests {
#[test]
fn waitpid() {
let pid = run::spawn_process("false", [],
let pid = run::spawn_process("false", []/~,
none, none,
0i32, 0i32, 0i32);
let status = run::waitpid(pid);

View File

@@ -122,7 +122,7 @@ Convert a vector of bytes to a UTF-8 string
Fails if invalid UTF-8
"]
pure fn from_bytes(vv: [u8]) -> str {
pure fn from_bytes(vv: [u8]/~) -> str {
assert is_utf8(vv);
ret unsafe { unsafe::from_bytes(vv) };
}
@@ -136,7 +136,7 @@ Fails if invalid UTF-8
"]
pure fn from_byte(b: u8) -> str {
assert b < 128u8;
let mut v = [b, 0u8];
let mut v = [b, 0u8]/~;
unsafe { ::unsafe::transmute(v) }
}
@@ -209,7 +209,7 @@ fn push_char(&s: str, ch: char) {
}
as_bytes(s) {|bytes|
let mut mut_bytes: [u8] = ::unsafe::reinterpret_cast(bytes);
let mut mut_bytes: [u8]/~ = ::unsafe::reinterpret_cast(bytes);
vec::unsafe::set_len(mut_bytes, new_len + 1u);
::unsafe::forget(mut_bytes);
}
@@ -322,10 +322,10 @@ Converts a string to a vector of bytes
The result vector is not null-terminated.
"]
pure fn bytes(s: str) -> [u8] {
pure fn bytes(s: str) -> [u8]/~ {
unsafe {
let mut s_copy = s;
let mut v: [u8] = ::unsafe::transmute(s_copy);
let mut v: [u8]/~ = ::unsafe::transmute(s_copy);
vec::unsafe::set_len(v, len(s));
ret v;
}
@@ -342,12 +342,12 @@ pure fn byte_slice<T>(s: str/&, f: fn([u8]/&) -> T) -> T {
}
#[doc = "Convert a string to a vector of characters"]
pure fn chars(s: str/&) -> [char] {
let mut buf = [], i = 0u;
pure fn chars(s: str/&) -> [char]/~ {
let mut buf = []/~, i = 0u;
let len = len(s);
while i < len {
let {ch, next} = char_range_at(s, i);
buf += [ch];
buf += [ch]/~;
i = next;
}
ret buf;
@@ -378,7 +378,7 @@ pure fn slice(s: str/&, begin: uint, end: uint) -> str {
#[doc = "
Splits a string into substrings at each occurrence of a given character
"]
pure fn split_char(s: str/&, sep: char) -> [str] {
pure fn split_char(s: str/&, sep: char) -> [str]/~ {
split_char_inner(s, sep, len(s), true)
}
@@ -388,27 +388,27 @@ character up to 'count' times
The byte must be a valid UTF-8/ASCII byte
"]
pure fn splitn_char(s: str/&, sep: char, count: uint) -> [str] {
pure fn splitn_char(s: str/&, sep: char, count: uint) -> [str]/~ {
split_char_inner(s, sep, count, true)
}
#[doc = "
Like `split_char`, but omits empty strings from the returned vector
"]
pure fn split_char_nonempty(s: str/&, sep: char) -> [str] {
pure fn split_char_nonempty(s: str/&, sep: char) -> [str]/~ {
split_char_inner(s, sep, len(s), false)
}
pure fn split_char_inner(s: str/&, sep: char, count: uint, allow_empty: bool)
-> [str] {
-> [str]/~ {
if sep < 128u as char {
let b = sep as u8, l = len(s);
let mut result = [], done = 0u;
let mut result = []/~, done = 0u;
let mut i = 0u, start = 0u;
while i < l && done < count {
if s[i] == b {
if allow_empty || start < i {
result += [unsafe { unsafe::slice_bytes(s, start, i) }];
result += [unsafe { unsafe::slice_bytes(s, start, i) }]/~;
}
start = i + 1u;
done += 1u;
@@ -416,7 +416,7 @@ pure fn split_char_inner(s: str/&, sep: char, count: uint, allow_empty: bool)
i += 1u;
}
if allow_empty || start < l {
result += [unsafe { unsafe::slice_bytes(s, start, l) }];
result += [unsafe { unsafe::slice_bytes(s, start, l) }]/~;
}
result
} else {
@@ -426,7 +426,7 @@ pure fn split_char_inner(s: str/&, sep: char, count: uint, allow_empty: bool)
#[doc = "Splits a string into substrings using a character function"]
pure fn split(s: str/&, sepfn: fn(char) -> bool) -> [str] {
pure fn split(s: str/&, sepfn: fn(char) -> bool) -> [str]/~ {
split_inner(s, sepfn, len(s), true)
}
@@ -434,24 +434,24 @@ pure fn split(s: str/&, sepfn: fn(char) -> bool) -> [str] {
Splits a string into substrings using a character function, cutting at
most `count` times.
"]
pure fn splitn(s: str/&, sepfn: fn(char) -> bool, count: uint) -> [str] {
pure fn splitn(s: str/&, sepfn: fn(char) -> bool, count: uint) -> [str]/~ {
split_inner(s, sepfn, count, true)
}
#[doc = "Like `split`, but omits empty strings from the returned vector"]
pure fn split_nonempty(s: str/&, sepfn: fn(char) -> bool) -> [str] {
pure fn split_nonempty(s: str/&, sepfn: fn(char) -> bool) -> [str]/~ {
split_inner(s, sepfn, len(s), false)
}
pure fn split_inner(s: str/&, sepfn: fn(cc: char) -> bool, count: uint,
allow_empty: bool) -> [str] {
allow_empty: bool) -> [str]/~ {
let l = len(s);
let mut result = [], i = 0u, start = 0u, done = 0u;
let mut result = []/~, i = 0u, start = 0u, done = 0u;
while i < l && done < count {
let {ch, next} = char_range_at(s, i);
if sepfn(ch) {
if allow_empty || start < i {
result += [unsafe { unsafe::slice_bytes(s, start, i) }];
result += [unsafe { unsafe::slice_bytes(s, start, i) }]/~;
}
start = next;
done += 1u;
@@ -459,7 +459,7 @@ pure fn split_inner(s: str/&, sepfn: fn(cc: char) -> bool, count: uint,
i = next;
}
if allow_empty || start < l {
result += [unsafe { unsafe::slice_bytes(s, start, l) }];
result += [unsafe { unsafe::slice_bytes(s, start, l) }]/~;
}
result
}
@@ -510,19 +510,19 @@ Splits a string into a vector of the substrings separated by a given string
assert [\"\", \"XXX\", \"YYY\", \"\"] == split_str(\".XXX.YYY.\", \".\")
~~~
"]
pure fn split_str(s: str/&a, sep: str/&b) -> [str] {
let mut result = [];
pure fn split_str(s: str/&a, sep: str/&b) -> [str]/~ {
let mut result = []/~;
iter_between_matches(s, sep) {|from, to|
unsafe { result += [unsafe::slice_bytes(s, from, to)]; }
unsafe { result += [unsafe::slice_bytes(s, from, to)]/~; }
}
result
}
pure fn split_str_nonempty(s: str/&a, sep: str/&b) -> [str] {
let mut result = [];
pure fn split_str_nonempty(s: str/&a, sep: str/&b) -> [str]/~ {
let mut result = []/~;
iter_between_matches(s, sep) {|from, to|
if to > from {
unsafe { result += [unsafe::slice_bytes(s, from, to)]; }
unsafe { result += [unsafe::slice_bytes(s, from, to)]/~; }
}
}
result
@@ -531,13 +531,13 @@ pure fn split_str_nonempty(s: str/&a, sep: str/&b) -> [str] {
#[doc = "
Splits a string into a vector of the substrings separated by LF ('\\n')
"]
pure fn lines(s: str/&) -> [str] { split_char(s, '\n') }
pure fn lines(s: str/&) -> [str]/~ { split_char(s, '\n') }
#[doc = "
Splits a string into a vector of the substrings separated by LF ('\\n')
and/or CR LF ('\\r\\n')
"]
pure fn lines_any(s: str/&) -> [str] {
pure fn lines_any(s: str/&) -> [str]/~ {
vec::map(lines(s), {|s|
let l = len(s);
let mut cp = s;
@@ -551,7 +551,7 @@ pure fn lines_any(s: str/&) -> [str] {
#[doc = "
Splits a string into a vector of the substrings separated by whitespace
"]
pure fn words(s: str/&) -> [str] {
pure fn words(s: str/&) -> [str]/~ {
split_nonempty(s, {|c| char::is_whitespace(c)})
}
@@ -1264,8 +1264,8 @@ pure fn is_utf16(v: [const u16]/&) -> bool {
}
#[doc = "Converts to a vector of `u16` encoded as UTF-16"]
pure fn to_utf16(s: str/&) -> [u16] {
let mut u = [];
pure fn to_utf16(s: str/&) -> [u16]/~ {
let mut u = []/~;
chars_iter(s) {|cch|
// Arithmetic with u32 literals is easier on the eyes than chars.
let mut ch = cch as u32;
@@ -1273,14 +1273,14 @@ pure fn to_utf16(s: str/&) -> [u16] {
if (ch & 0xFFFF_u32) == ch {
// The BMP falls through (assuming non-surrogate, as it should)
assert ch <= 0xD7FF_u32 || ch >= 0xE000_u32;
u += [ch as u16]
u += [ch as u16]/~
} else {
// Supplementary planes break into surrogates.
assert ch >= 0x1_0000_u32 && ch <= 0x10_FFFF_u32;
ch -= 0x1_0000_u32;
let w1 = 0xD800_u16 | ((ch >> 10) as u16);
let w2 = 0xDC00_u16 | ((ch as u16) & 0x3FF_u16);
u += [w1, w2]
u += [w1, w2]/~
}
}
ret u;
@@ -1568,9 +1568,9 @@ interop.
let i = str::as_bytes(\"Hello World\") { |bytes| vec::len(bytes) };
~~~
"]
pure fn as_bytes<T>(s: str, f: fn([u8]) -> T) -> T {
pure fn as_bytes<T>(s: str, f: fn([u8]/~) -> T) -> T {
unsafe {
let v: *[u8] = ::unsafe::reinterpret_cast(ptr::addr_of(s));
let v: *[u8]/~ = ::unsafe::reinterpret_cast(ptr::addr_of(s));
f(*v)
}
}
@@ -1723,7 +1723,7 @@ mod unsafe {
#[doc = "Create a Rust string from a *u8 buffer of the given length"]
unsafe fn from_buf_len(buf: *u8, len: uint) -> str {
let mut v: [u8] = [];
let mut v: [u8]/~ = []/~;
vec::reserve(v, len + 1u);
vec::as_buf(v) {|b| ptr::memcpy(b, buf, len); }
vec::unsafe::set_len(v, len);
@@ -1750,9 +1750,9 @@ mod unsafe {
Does not verify that the vector contains valid UTF-8.
"]
unsafe fn from_bytes(v: [const u8]) -> str {
unsafe fn from_bytes(v: [const u8]/~) -> str {
unsafe {
let mut vcopy : [u8] = ::unsafe::transmute(copy v);
let mut vcopy = ::unsafe::transmute(copy v);
vec::push(vcopy, 0u8);
::unsafe::transmute(vcopy)
}
@@ -1763,7 +1763,7 @@ mod unsafe {
Does not verify that the byte is valid UTF-8.
"]
unsafe fn from_byte(u: u8) -> str { unsafe::from_bytes([u]) }
unsafe fn from_byte(u: u8) -> str { unsafe::from_bytes([u]/~) }
#[doc = "
Takes a bytewise (not UTF-8) slice from a string.
@@ -1780,7 +1780,7 @@ mod unsafe {
assert (begin <= end);
assert (end <= n);
let mut v = [];
let mut v = []/~;
vec::reserve(v, end - begin + 1u);
unsafe {
vec::as_buf(v) { |vbuf|
@@ -1788,7 +1788,7 @@ mod unsafe {
ptr::memcpy(vbuf, src, end - begin);
}
vec::unsafe::set_len(v, end - begin);
v += [0u8];
v += [0u8]/~;
::unsafe::transmute(v)
}
}
@@ -1800,7 +1800,7 @@ mod unsafe {
}
#[doc = "Appends a vector of bytes to a string. (Not UTF-8 safe)."]
unsafe fn push_bytes(&s: str, bytes: [u8]) {
unsafe fn push_bytes(&s: str, bytes: [u8]/~) {
for vec::each(bytes) {|byte| rustrt::rust_str_push(s, byte); }
}
@@ -1839,7 +1839,7 @@ mod unsafe {
#[test]
fn test_from_buf_len() {
unsafe {
let a = [65u8, 65u8, 65u8, 65u8, 65u8, 65u8, 65u8, 0u8];
let a = [65u8, 65u8, 65u8, 65u8, 65u8, 65u8, 65u8, 0u8]/~;
let b = vec::unsafe::to_ptr(a);
let c = from_buf_len(b, 3u);
assert (c == "AAA");
@@ -1920,18 +1920,18 @@ impl extensions/& for str/& {
fn slice(begin: uint, end: uint) -> str { slice(self, begin, end) }
#[doc = "Splits a string into substrings using a character function"]
#[inline]
fn split(sepfn: fn(char) -> bool) -> [str] { split(self, sepfn) }
fn split(sepfn: fn(char) -> bool) -> [str]/~ { split(self, sepfn) }
#[doc = "
Splits a string into substrings at each occurrence of a given character
"]
#[inline]
fn split_char(sep: char) -> [str] { split_char(self, sep) }
fn split_char(sep: char) -> [str]/~ { split_char(self, sep) }
#[doc = "
Splits a string into a vector of the substrings separated by a given
string
"]
#[inline]
fn split_str(sep: str/&a) -> [str] { split_str(self, sep) }
fn split_str(sep: str/&a) -> [str]/~ { split_str(self, sep) }
#[doc = "Returns true if one string starts with another"]
#[inline]
fn starts_with(needle: str/&a) -> bool { starts_with(self, needle) }
@@ -2032,79 +2032,79 @@ mod tests {
#[test]
fn test_split_char() {
fn t(s: str, c: char, u: [str]) {
fn t(s: str, c: char, u: [str]/~) {
log(debug, "split_byte: " + s);
let v = split_char(s, c);
#debug("split_byte to: %?", v);
assert vec::all2(v, u, { |a,b| a == b });
}
t("abc.hello.there", '.', ["abc", "hello", "there"]);
t(".hello.there", '.', ["", "hello", "there"]);
t("...hello.there.", '.', ["", "", "", "hello", "there", ""]);
t("abc.hello.there", '.', ["abc", "hello", "there"]/~);
t(".hello.there", '.', ["", "hello", "there"]/~);
t("...hello.there.", '.', ["", "", "", "hello", "there", ""]/~);
assert ["", "", "", "hello", "there", ""]
assert ["", "", "", "hello", "there", ""]/~
== split_char("...hello.there.", '.');
assert [""] == split_char("", 'z');
assert ["",""] == split_char("z", 'z');
assert ["ok"] == split_char("ok", 'z');
assert [""]/~ == split_char("", 'z');
assert ["",""]/~ == split_char("z", 'z');
assert ["ok"]/~ == split_char("ok", 'z');
}
#[test]
fn test_split_char_2() {
let data = "ประเทศไทย中华Việt Nam";
assert ["ประเทศไทย中华", "iệt Nam"]
assert ["ประเทศไทย中华", "iệt Nam"]/~
== split_char(data, 'V');
assert ["ประเ", "ศไ", "ย中华Việt Nam"]
assert ["ประเ", "ศไ", "ย中华Việt Nam"]/~
== split_char(data, 'ท');
}
#[test]
fn test_splitn_char() {
fn t(s: str, c: char, n: uint, u: [str]) {
fn t(s: str, c: char, n: uint, u: [str]/~) {
log(debug, "splitn_byte: " + s);
let v = splitn_char(s, c, n);
#debug("split_byte to: %?", v);
#debug("comparing vs. %?", u);
assert vec::all2(v, u, { |a,b| a == b });
}
t("abc.hello.there", '.', 0u, ["abc.hello.there"]);
t("abc.hello.there", '.', 1u, ["abc", "hello.there"]);
t("abc.hello.there", '.', 2u, ["abc", "hello", "there"]);
t("abc.hello.there", '.', 3u, ["abc", "hello", "there"]);
t(".hello.there", '.', 0u, [".hello.there"]);
t(".hello.there", '.', 1u, ["", "hello.there"]);
t("...hello.there.", '.', 3u, ["", "", "", "hello.there."]);
t("...hello.there.", '.', 5u, ["", "", "", "hello", "there", ""]);
t("abc.hello.there", '.', 0u, ["abc.hello.there"]/~);
t("abc.hello.there", '.', 1u, ["abc", "hello.there"]/~);
t("abc.hello.there", '.', 2u, ["abc", "hello", "there"]/~);
t("abc.hello.there", '.', 3u, ["abc", "hello", "there"]/~);
t(".hello.there", '.', 0u, [".hello.there"]/~);
t(".hello.there", '.', 1u, ["", "hello.there"]/~);
t("...hello.there.", '.', 3u, ["", "", "", "hello.there."]/~);
t("...hello.there.", '.', 5u, ["", "", "", "hello", "there", ""]/~);
assert [""] == splitn_char("", 'z', 5u);
assert ["",""] == splitn_char("z", 'z', 5u);
assert ["ok"] == splitn_char("ok", 'z', 5u);
assert ["z"] == splitn_char("z", 'z', 0u);
assert ["w.x.y"] == splitn_char("w.x.y", '.', 0u);
assert ["w","x.y"] == splitn_char("w.x.y", '.', 1u);
assert [""]/~ == splitn_char("", 'z', 5u);
assert ["",""]/~ == splitn_char("z", 'z', 5u);
assert ["ok"]/~ == splitn_char("ok", 'z', 5u);
assert ["z"]/~ == splitn_char("z", 'z', 0u);
assert ["w.x.y"]/~ == splitn_char("w.x.y", '.', 0u);
assert ["w","x.y"]/~ == splitn_char("w.x.y", '.', 1u);
}
#[test]
fn test_splitn_char_2 () {
let data = "ประเทศไทย中华Việt Nam";
assert ["ประเทศไทย中", "Việt Nam"]
assert ["ประเทศไทย中", "Việt Nam"]/~
== splitn_char(data, '华', 1u);
assert ["", "", "XXX", "YYYzWWWz"]
assert ["", "", "XXX", "YYYzWWWz"]/~
== splitn_char("zzXXXzYYYzWWWz", 'z', 3u);
assert ["",""] == splitn_char("z", 'z', 5u);
assert [""] == splitn_char("", 'z', 5u);
assert ["ok"] == splitn_char("ok", 'z', 5u);
assert ["",""]/~ == splitn_char("z", 'z', 5u);
assert [""]/~ == splitn_char("", 'z', 5u);
assert ["ok"]/~ == splitn_char("ok", 'z', 5u);
}
#[test]
fn test_splitn_char_3() {
let data = "ประเทศไทย中华Việt Nam";
assert ["ประเทศไทย中华", "iệt Nam"]
assert ["ประเทศไทย中华", "iệt Nam"]/~
== splitn_char(data, 'V', 1u);
assert ["ประเ", "ศไทย中华Việt Nam"]
assert ["ประเ", "ศไทย中华Việt Nam"]/~
== splitn_char(data, 'ท', 1u);
}
@@ -2125,40 +2125,40 @@ mod tests {
t("::hello::there::", "::", 3, "");
let data = "ประเทศไทย中华Việt Nam";
assert ["ประเทศไทย", "Việt Nam"]
assert ["ประเทศไทย", "Việt Nam"]/~
== split_str (data, "中华");
assert ["", "XXX", "YYY", ""]
assert ["", "XXX", "YYY", ""]/~
== split_str("zzXXXzzYYYzz", "zz");
assert ["zz", "zYYYz"]
assert ["zz", "zYYYz"]/~
== split_str("zzXXXzYYYz", "XXX");
assert ["", "XXX", "YYY", ""] == split_str(".XXX.YYY.", ".");
assert [""] == split_str("", ".");
assert ["",""] == split_str("zz", "zz");
assert ["ok"] == split_str("ok", "z");
assert ["","z"] == split_str("zzz", "zz");
assert ["","","z"] == split_str("zzzzz", "zz");
assert ["", "XXX", "YYY", ""]/~ == split_str(".XXX.YYY.", ".");
assert [""]/~ == split_str("", ".");
assert ["",""]/~ == split_str("zz", "zz");
assert ["ok"]/~ == split_str("ok", "z");
assert ["","z"]/~ == split_str("zzz", "zz");
assert ["","","z"]/~ == split_str("zzzzz", "zz");
}
#[test]
fn test_split() {
let data = "ประเทศไทย中华Việt Nam";
assert ["ประเทศไทย中", "Việt Nam"]
assert ["ประเทศไทย中", "Việt Nam"]/~
== split (data, {|cc| cc == '华'});
assert ["", "", "XXX", "YYY", ""]
assert ["", "", "XXX", "YYY", ""]/~
== split("zzXXXzYYYz", char::is_lowercase);
assert ["zz", "", "", "z", "", "", "z"]
assert ["zz", "", "", "z", "", "", "z"]/~
== split("zzXXXzYYYz", char::is_uppercase);
assert ["",""] == split("z", {|cc| cc == 'z'});
assert [""] == split("", {|cc| cc == 'z'});
assert ["ok"] == split("ok", {|cc| cc == 'z'});
assert ["",""]/~ == split("z", {|cc| cc == 'z'});
assert [""]/~ == split("", {|cc| cc == 'z'});
assert ["ok"]/~ == split("ok", {|cc| cc == 'z'});
}
#[test]
@@ -2166,34 +2166,34 @@ mod tests {
let lf = "\nMary had a little lamb\nLittle lamb\n";
let crlf = "\r\nMary had a little lamb\r\nLittle lamb\r\n";
assert ["", "Mary had a little lamb", "Little lamb", ""]
assert ["", "Mary had a little lamb", "Little lamb", ""]/~
== lines(lf);
assert ["", "Mary had a little lamb", "Little lamb", ""]
assert ["", "Mary had a little lamb", "Little lamb", ""]/~
== lines_any(lf);
assert ["\r", "Mary had a little lamb\r", "Little lamb\r", ""]
assert ["\r", "Mary had a little lamb\r", "Little lamb\r", ""]/~
== lines(crlf);
assert ["", "Mary had a little lamb", "Little lamb", ""]
assert ["", "Mary had a little lamb", "Little lamb", ""]/~
== lines_any(crlf);
assert [""] == lines ("");
assert [""] == lines_any("");
assert ["",""] == lines ("\n");
assert ["",""] == lines_any("\n");
assert ["banana"] == lines ("banana");
assert ["banana"] == lines_any("banana");
assert [""]/~ == lines ("");
assert [""]/~ == lines_any("");
assert ["",""]/~ == lines ("\n");
assert ["",""]/~ == lines_any("\n");
assert ["banana"]/~ == lines ("banana");
assert ["banana"]/~ == lines_any("banana");
}
#[test]
fn test_words () {
let data = "\nMary had a little lamb\nLittle lamb\n";
assert ["Mary","had","a","little","lamb","Little","lamb"]
assert ["Mary","had","a","little","lamb","Little","lamb"]/~
== words(data);
assert ["ok"] == words("ok");
assert [] == words("");
assert ["ok"]/~ == words("ok");
assert []/~ == words("");
}
#[test]
@@ -2250,22 +2250,23 @@ mod tests {
#[test]
fn test_concat() {
fn t(v: [str], s: str) { assert (eq(concat(v), s)); }
t(["you", "know", "I'm", "no", "good"], "youknowI'mnogood");
let v: [str] = [];
fn t(v: [str]/~, s: str) { assert (eq(concat(v), s)); }
t(["you", "know", "I'm", "no", "good"]/~, "youknowI'mnogood");
let v: [str]/~ = []/~;
t(v, "");
t(["hi"], "hi");
t(["hi"]/~, "hi");
}
#[test]
fn test_connect() {
fn t(v: [str], sep: str, s: str) {
fn t(v: [str]/~, sep: str, s: str) {
assert (eq(connect(v, sep), s));
}
t(["you", "know", "I'm", "no", "good"], " ", "you know I'm no good");
let v: [str] = [];
t(["you", "know", "I'm", "no", "good"]/~,
" ", "you know I'm no good");
let v: [str]/~ = []/~;
t(v, " ", "");
t(["hi"], " ", "hi");
t(["hi"]/~, " ", "hi");
}
#[test]
@@ -2517,7 +2518,7 @@ mod tests {
#[test]
fn test_unsafe_from_bytes() {
let a = [65u8, 65u8, 65u8, 65u8, 65u8, 65u8, 65u8];
let a = [65u8, 65u8, 65u8, 65u8, 65u8, 65u8, 65u8]/~;
let b = unsafe { unsafe::from_bytes(a) };
assert (b == "AAAAAAA");
}
@@ -2534,7 +2535,7 @@ mod tests {
0x56_u8, 0x69_u8, 0xe1_u8,
0xbb_u8, 0x87_u8, 0x74_u8,
0x20_u8, 0x4e_u8, 0x61_u8,
0x6d_u8];
0x6d_u8]/~;
assert ss == from_bytes(bb);
}
@@ -2552,7 +2553,7 @@ mod tests {
0x56_u8, 0x69_u8, 0xe1_u8,
0xbb_u8, 0x87_u8, 0x74_u8,
0x20_u8, 0x4e_u8, 0x61_u8,
0x6d_u8];
0x6d_u8]/~;
let _x = from_bytes(bb);
}
@@ -2560,7 +2561,7 @@ mod tests {
#[test]
fn test_from_buf() {
unsafe {
let a = [65u8, 65u8, 65u8, 65u8, 65u8, 65u8, 65u8, 0u8];
let a = [65u8, 65u8, 65u8, 65u8, 65u8, 65u8, 65u8, 0u8]/~;
let b = vec::unsafe::to_ptr(a);
let c = unsafe::from_buf(b);
assert (c == "AAAAAAA");
@@ -2609,7 +2610,7 @@ mod tests {
fn vec_str_conversions() {
let s1: str = "All mimsy were the borogoves";
let v: [u8] = bytes(s1);
let v: [u8]/~ = bytes(s1);
let s2: str = from_bytes(v);
let mut i: uint = 0u;
let n1: uint = len(s1);
@@ -2774,7 +2775,7 @@ mod tests {
#[test]
fn test_chars() {
let ss = "ศไทย中华Việt Nam";
assert ['ศ','ไ','ท','ย','中','华','V','i','ệ','t',' ','N','a','m']
assert ['ศ','ไ','ท','ย','中','华','V','i','ệ','t',' ','N','a','m']/~
== chars(ss);
}
@@ -2785,7 +2786,7 @@ mod tests {
[0xd800_u16, 0xdf45_u16, 0xd800_u16, 0xdf3f_u16,
0xd800_u16, 0xdf3b_u16, 0xd800_u16, 0xdf46_u16,
0xd800_u16, 0xdf39_u16, 0xd800_u16, 0xdf3b_u16,
0xd800_u16, 0xdf30_u16, 0x000a_u16]),
0xd800_u16, 0xdf30_u16, 0x000a_u16]/~),
("𐐒𐑉𐐮𐑀𐐲𐑋 𐐏𐐲𐑍\n",
[0xd801_u16, 0xdc12_u16, 0xd801_u16,
@@ -2793,7 +2794,7 @@ mod tests {
0xdc40_u16, 0xd801_u16, 0xdc32_u16, 0xd801_u16,
0xdc4b_u16, 0x0020_u16, 0xd801_u16, 0xdc0f_u16,
0xd801_u16, 0xdc32_u16, 0xd801_u16, 0xdc4d_u16,
0x000a_u16]),
0x000a_u16]/~),
("𐌀𐌖𐌋𐌄𐌑𐌉·𐌌𐌄𐌕𐌄𐌋𐌉𐌑\n",
[0xd800_u16, 0xdf00_u16, 0xd800_u16, 0xdf16_u16,
@@ -2802,7 +2803,7 @@ mod tests {
0x00b7_u16, 0xd800_u16, 0xdf0c_u16, 0xd800_u16,
0xdf04_u16, 0xd800_u16, 0xdf15_u16, 0xd800_u16,
0xdf04_u16, 0xd800_u16, 0xdf0b_u16, 0xd800_u16,
0xdf09_u16, 0xd800_u16, 0xdf11_u16, 0x000a_u16 ]),
0xdf09_u16, 0xd800_u16, 0xdf11_u16, 0x000a_u16 ]/~),
("𐒋𐒘𐒈𐒑𐒛𐒒 𐒕𐒓 𐒈𐒚𐒍 𐒏𐒜𐒒𐒖𐒆 𐒕𐒆\n",
[0xd801_u16, 0xdc8b_u16, 0xd801_u16, 0xdc98_u16,
@@ -2815,7 +2816,7 @@ mod tests {
0xdc9c_u16, 0xd801_u16, 0xdc92_u16, 0xd801_u16,
0xdc96_u16, 0xd801_u16, 0xdc86_u16, 0x0020_u16,
0xd801_u16, 0xdc95_u16, 0xd801_u16, 0xdc86_u16,
0x000a_u16 ]) ];
0x000a_u16 ]/~) ]/~;
for vec::each(pairs) {|p|
let (s, u) = p;

View File

@@ -56,7 +56,7 @@ impl <A: to_str copy, B: to_str copy, C: to_str copy> of to_str for (A, B, C){
}
}
impl <A: to_str> of to_str for [A] {
impl <A: to_str> of to_str for [A]/~ {
fn to_str() -> str {
let mut acc = "[", first = true;
for vec::each(self) {|elt|
@@ -98,11 +98,12 @@ mod tests {
}
fn test_vectors() {
let x: [int] = [];
assert x.to_str() == "[]";
assert [1].to_str() == "[1]";
assert [1, 2, 3].to_str() == "[1, 2, 3]";
assert [[], [1], [1, 1]].to_str() == "[[], [1], [1, 1]]";
let x: [int]/~ = []/~;
assert x.to_str() == "[]/~";
assert [1]/~.to_str() == "[1]/~";
assert [1, 2, 3]/~.to_str() == "[1, 2, 3]/~";
assert [[]/~, [1]/~, [1, 1]/~]/~.to_str() ==
"[[]/~, [1]/~, [1, 1]/~]/~";
}
fn test_pointer_types() {

View File

@@ -88,7 +88,7 @@ Parse a buffer of bytes
`buf` must not be empty
"]
fn parse_buf(buf: [u8], radix: uint) -> option<T> {
fn parse_buf(buf: [u8]/~, radix: uint) -> option<T> {
if vec::len(buf) == 0u { ret none; }
let mut i = vec::len(buf) - 1u;
let mut power = 1u as T;

View File

@@ -33,7 +33,7 @@ Both types must have the same size and alignment.
# Example
assert transmute(\"L\") == [76u8, 0u8];
assert transmute(\"L\") == [76u8, 0u8]/~;
"]
unsafe fn transmute<L, G>(-thing: L) -> G {
let newthing = reinterpret_cast(thing);
@@ -62,7 +62,7 @@ mod tests {
#[test]
fn test_transmute2() {
unsafe {
assert transmute("L") == [76u8, 0u8];
assert transmute("L") == [76u8, 0u8]/~;
}
}
}

File diff suppressed because it is too large Load Diff