add mutability annotations to libcore
This commit is contained in:
parent
674587cfe5
commit
713006c7b6
@ -180,7 +180,7 @@ fn select2<A: send, B: send>(
|
||||
rustrt::rust_port_select(dptr, ports, n_ports, yield)
|
||||
}
|
||||
|
||||
let ports = [];
|
||||
let mut ports = [];
|
||||
ports += [***p_a, ***p_b];
|
||||
let n_ports = 2 as ctypes::size_t;
|
||||
let yield = 0u;
|
||||
|
@ -40,7 +40,7 @@ Function: lefts
|
||||
Extracts from a vector of either all the left values.
|
||||
*/
|
||||
fn lefts<T: copy, U>(eithers: [t<T, U>]) -> [T] {
|
||||
let result: [T] = [];
|
||||
let mut result: [T] = [];
|
||||
for elt: t<T, U> in eithers {
|
||||
alt elt { left(l) { result += [l]; } _ {/* fallthrough */ } }
|
||||
}
|
||||
@ -53,7 +53,7 @@ Function: rights
|
||||
Extracts from a vector of either all the right values
|
||||
*/
|
||||
fn rights<T, U: copy>(eithers: [t<T, U>]) -> [U] {
|
||||
let result: [U] = [];
|
||||
let mut result: [U] = [];
|
||||
for elt: t<T, U> in eithers {
|
||||
alt elt { right(r) { result += [r]; } _ {/* fallthrough */ } }
|
||||
}
|
||||
@ -70,8 +70,8 @@ right values.
|
||||
*/
|
||||
fn partition<T: copy, U: copy>(eithers: [t<T, U>])
|
||||
-> {lefts: [T], rights: [U]} {
|
||||
let lefts: [T] = [];
|
||||
let rights: [U] = [];
|
||||
let mut lefts: [T] = [];
|
||||
let mut rights: [U] = [];
|
||||
for elt: t<T, U> in eithers {
|
||||
alt elt { left(l) { lefts += [l]; } right(r) { rights += [r]; } }
|
||||
}
|
||||
|
@ -81,9 +81,9 @@ mod ct {
|
||||
type error_fn = fn@(str) -> ! ;
|
||||
|
||||
fn parse_fmt_string(s: str, error: error_fn) -> [piece] unsafe {
|
||||
let pieces: [piece] = [];
|
||||
let mut pieces: [piece] = [];
|
||||
let lim = str::len(s);
|
||||
let buf = "";
|
||||
let mut buf = "";
|
||||
fn flush_buf(buf: str, &pieces: [piece]) -> str {
|
||||
if str::len(buf) > 0u {
|
||||
let piece = piece_string(buf);
|
||||
@ -91,7 +91,7 @@ mod ct {
|
||||
}
|
||||
ret "";
|
||||
}
|
||||
let i = 0u;
|
||||
let mut i = 0u;
|
||||
while i < lim {
|
||||
let curr = str::slice(s, i, i+1u);
|
||||
if str::eq(curr, "%") {
|
||||
@ -285,7 +285,7 @@ mod rt {
|
||||
fn conv_int(cv: conv, i: int) -> str {
|
||||
let radix = 10u;
|
||||
let prec = get_int_precision(cv);
|
||||
let s = int_to_str_prec(i, radix, prec);
|
||||
let mut s = int_to_str_prec(i, radix, prec);
|
||||
if 0 <= i {
|
||||
if have_flag(cv.flags, flag_sign_always) {
|
||||
s = "+" + s;
|
||||
@ -335,7 +335,7 @@ mod rt {
|
||||
count_is(c) { (float::to_str_exact, c as uint) }
|
||||
count_implied { (float::to_str, 6u) }
|
||||
};
|
||||
let s = to_str(f, digits);
|
||||
let mut s = to_str(f, digits);
|
||||
if 0.0 <= f {
|
||||
if have_flag(cv.flags, flag_sign_always) {
|
||||
s = "+" + s;
|
||||
@ -389,42 +389,38 @@ mod rt {
|
||||
}
|
||||
enum pad_mode { pad_signed, pad_unsigned, pad_nozero, }
|
||||
fn pad(cv: conv, s: str, mode: pad_mode) -> str unsafe {
|
||||
let uwidth;
|
||||
alt cv.width {
|
||||
let uwidth = alt cv.width {
|
||||
count_implied { ret s; }
|
||||
count_is(width) {
|
||||
// FIXME: Maybe width should be uint
|
||||
|
||||
uwidth = width as uint;
|
||||
width as uint
|
||||
}
|
||||
}
|
||||
};
|
||||
let strlen = str::char_len(s);
|
||||
if uwidth <= strlen { ret s; }
|
||||
let padchar = ' ';
|
||||
let mut padchar = ' ';
|
||||
let diff = uwidth - strlen;
|
||||
if have_flag(cv.flags, flag_left_justify) {
|
||||
let padstr = str_init_elt(diff, padchar);
|
||||
ret s + padstr;
|
||||
}
|
||||
let might_zero_pad = false;
|
||||
let signed = false;
|
||||
alt mode {
|
||||
pad_nozero {
|
||||
// fallthrough
|
||||
|
||||
}
|
||||
pad_signed { might_zero_pad = true; signed = true; }
|
||||
pad_unsigned { might_zero_pad = true; }
|
||||
}
|
||||
let {might_zero_pad, signed} = alt mode {
|
||||
pad_nozero { {might_zero_pad:false, signed:false} }
|
||||
pad_signed { {might_zero_pad:true, signed:true } }
|
||||
pad_unsigned { {might_zero_pad:true, signed:false} }
|
||||
};
|
||||
fn have_precision(cv: conv) -> bool {
|
||||
ret alt cv.precision { count_implied { false } _ { true } };
|
||||
}
|
||||
let zero_padding = false;
|
||||
if might_zero_pad && have_flag(cv.flags, flag_left_zero_pad) &&
|
||||
!have_precision(cv) {
|
||||
padchar = '0';
|
||||
zero_padding = true;
|
||||
}
|
||||
let zero_padding = {
|
||||
if might_zero_pad && have_flag(cv.flags, flag_left_zero_pad) &&
|
||||
!have_precision(cv) {
|
||||
padchar = '0';
|
||||
true
|
||||
} else {
|
||||
false
|
||||
}
|
||||
};
|
||||
let padstr = str_init_elt(diff, padchar);
|
||||
// This is completely heinous. If we have a signed value then
|
||||
// potentially rip apart the intermediate result and insert some
|
||||
|
@ -49,14 +49,14 @@ exact - Whether to enforce the exact number of significant digits
|
||||
*/
|
||||
fn to_str_common(num: float, digits: uint, exact: bool) -> str {
|
||||
if is_NaN(num) { ret "NaN"; }
|
||||
let (num, accum) = if num < 0.0 { (-num, "-") } else { (num, "") };
|
||||
let mut (num, accum) = if num < 0.0 { (-num, "-") } else { (num, "") };
|
||||
let trunc = num as uint;
|
||||
let frac = num - (trunc as float);
|
||||
let mut frac = num - (trunc as float);
|
||||
accum += uint::str(trunc);
|
||||
if (frac < epsilon && !exact) || digits == 0u { ret accum; }
|
||||
accum += ".";
|
||||
let i = digits;
|
||||
let epsilon = 1. / pow_with_uint(10u, i);
|
||||
let mut i = digits;
|
||||
let mut epsilon = 1. / pow_with_uint(10u, i);
|
||||
while i > 0u && (frac >= epsilon || exact) {
|
||||
frac *= 10.0;
|
||||
epsilon *= 10.0;
|
||||
@ -132,13 +132,13 @@ Otherwise, some(n) where n is the floating-point
|
||||
number represented by [num].
|
||||
*/
|
||||
fn from_str(num: str) -> option<float> {
|
||||
let pos = 0u; //Current byte position in the string.
|
||||
//Used to walk the string in O(n).
|
||||
let len = str::len(num); //Length of the string, in bytes.
|
||||
let mut pos = 0u; //Current byte position in the string.
|
||||
//Used to walk the string in O(n).
|
||||
let len = str::len(num); //Length of the string, in bytes.
|
||||
|
||||
if len == 0u { ret none; }
|
||||
let total = 0f; //Accumulated result
|
||||
let c = 'z'; //Latest char.
|
||||
let mut total = 0f; //Accumulated result
|
||||
let mut c = 'z'; //Latest char.
|
||||
|
||||
//The string must start with one of the following characters.
|
||||
alt str::char_at(num, 0u) {
|
||||
@ -147,7 +147,7 @@ fn from_str(num: str) -> option<float> {
|
||||
}
|
||||
|
||||
//Determine if first char is '-'/'+'. Set [pos] and [neg] accordingly.
|
||||
let neg = false; //Sign of the result
|
||||
let mut neg = false; //Sign of the result
|
||||
alt str::char_at(num, 0u) {
|
||||
'-' {
|
||||
neg = true;
|
||||
@ -179,7 +179,7 @@ fn from_str(num: str) -> option<float> {
|
||||
}
|
||||
|
||||
if c == '.' {//Examine decimal part
|
||||
let decimal = 1f;
|
||||
let mut decimal = 1f;
|
||||
while(pos < len) {
|
||||
let char_range = str::char_range_at(num, pos);
|
||||
c = char_range.ch;
|
||||
@ -200,8 +200,8 @@ fn from_str(num: str) -> option<float> {
|
||||
}
|
||||
|
||||
if (c == 'e') | (c == 'E') {//Examine exponent
|
||||
let exponent = 0u;
|
||||
let neg_exponent = false;
|
||||
let mut exponent = 0u;
|
||||
let mut neg_exponent = false;
|
||||
if(pos < len) {
|
||||
let char_range = str::char_range_at(num, pos);
|
||||
c = char_range.ch;
|
||||
@ -275,9 +275,9 @@ fn pow_with_uint(base: uint, pow: uint) -> float {
|
||||
}
|
||||
ret 0.;
|
||||
}
|
||||
let my_pow = pow;
|
||||
let total = 1f;
|
||||
let multiplier = base as float;
|
||||
let mut my_pow = pow;
|
||||
let mut total = 1f;
|
||||
let mut multiplier = base as float;
|
||||
while (my_pow > 0u) {
|
||||
if my_pow % 2u == 1u {
|
||||
total = total * multiplier;
|
||||
|
@ -93,7 +93,7 @@ fn spawn<A:send>(+blk: fn~() -> A) -> future<A> {
|
||||
|
||||
"];
|
||||
|
||||
let po = comm::port();
|
||||
let mut po = comm::port();
|
||||
let ch = comm::chan(po);
|
||||
task::spawn {||
|
||||
comm::send(ch, blk())
|
||||
|
@ -26,7 +26,7 @@ pure fn nonnegative(x: i16) -> bool { x >= 0i16 }
|
||||
|
||||
#[doc = "Iterate over the range [`lo`..`hi`)"]
|
||||
fn range(lo: i16, hi: i16, it: fn(i16)) {
|
||||
let i = lo;
|
||||
let mut i = lo;
|
||||
while i < hi { it(i); i += 1i16; }
|
||||
}
|
||||
|
||||
|
@ -26,7 +26,7 @@ pure fn nonnegative(x: i32) -> bool { x >= 0i32 }
|
||||
|
||||
#[doc = "Iterate over the range [`lo`..`hi`)"]
|
||||
fn range(lo: i32, hi: i32, it: fn(i32)) {
|
||||
let i = lo;
|
||||
let mut i = lo;
|
||||
while i < hi { it(i); i += 1i32; }
|
||||
}
|
||||
|
||||
|
@ -26,7 +26,7 @@ pure fn nonnegative(x: i64) -> bool { x >= 0i64 }
|
||||
|
||||
#[doc = "Iterate over the range [`lo`..`hi`)"]
|
||||
fn range(lo: i64, hi: i64, it: fn(i64)) {
|
||||
let i = lo;
|
||||
let mut i = lo;
|
||||
while i < hi { it(i); i += 1i64; }
|
||||
}
|
||||
|
||||
|
@ -26,7 +26,7 @@ pure fn nonnegative(x: i8) -> bool { x >= 0i8 }
|
||||
|
||||
#[doc = "Iterate over the range [`lo`..`hi`)"]
|
||||
fn range(lo: i8, hi: i8, it: fn(i8)) {
|
||||
let i = lo;
|
||||
let mut i = lo;
|
||||
while i < hi { it(i); i += 1i8; }
|
||||
}
|
||||
|
||||
|
@ -90,7 +90,7 @@ Function: range
|
||||
Iterate over the range [`lo`..`hi`)
|
||||
*/
|
||||
fn range(lo: int, hi: int, it: fn(int)) {
|
||||
let i = lo;
|
||||
let mut i = lo;
|
||||
while i < hi { it(i); i += 1; }
|
||||
}
|
||||
|
||||
@ -106,15 +106,15 @@ radix - The base of the number
|
||||
*/
|
||||
fn parse_buf(buf: [u8], radix: uint) -> option<int> {
|
||||
if vec::len(buf) == 0u { ret none; }
|
||||
let i = vec::len(buf) - 1u;
|
||||
let start = 0u;
|
||||
let power = 1;
|
||||
let mut i = vec::len(buf) - 1u;
|
||||
let mut start = 0u;
|
||||
let mut power = 1;
|
||||
|
||||
if buf[0] == ('-' as u8) {
|
||||
power = -1;
|
||||
start = 1u;
|
||||
}
|
||||
let n = 0;
|
||||
let mut n = 0;
|
||||
while true {
|
||||
alt char::to_digit(buf[i] as char, radix) {
|
||||
some(d) { n += (d as int) * power; }
|
||||
@ -161,9 +161,9 @@ 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 base == 0 { ret 0; }
|
||||
let my_pow = exponent;
|
||||
let acc = 1;
|
||||
let multiplier = base;
|
||||
let mut my_pow = exponent;
|
||||
let mut acc = 1;
|
||||
let mut multiplier = base;
|
||||
while(my_pow > 0u) {
|
||||
if my_pow % 2u == 1u {
|
||||
acc *= multiplier;
|
||||
|
@ -40,7 +40,7 @@ impl of iterable<char> for str {
|
||||
}
|
||||
|
||||
fn enumerate<A,IA:iterable<A>>(self: IA, blk: fn(uint, A)) {
|
||||
let i = 0u;
|
||||
let mut i = 0u;
|
||||
self.iter {|a|
|
||||
blk(i, a);
|
||||
i += 1u;
|
||||
@ -82,7 +82,7 @@ fn flat_map<A,B,IA:iterable<A>,IB:iterable<B>>(
|
||||
}
|
||||
|
||||
fn foldl<A,B,IA:iterable<A>>(self: IA, +b0: B, blk: fn(-B, A) -> B) -> B {
|
||||
let b <- b0;
|
||||
let mut b <- b0;
|
||||
self.iter {|a|
|
||||
b = blk(b, a);
|
||||
}
|
||||
@ -92,7 +92,7 @@ fn foldl<A,B,IA:iterable<A>>(self: IA, +b0: B, blk: fn(-B, A) -> B) -> B {
|
||||
fn foldr<A:copy,B,IA:iterable<A>>(
|
||||
self: IA, +b0: B, blk: fn(A, -B) -> B) -> B {
|
||||
|
||||
let b <- b0;
|
||||
let mut b <- b0;
|
||||
reversed(self) {|a|
|
||||
b = blk(a, b);
|
||||
}
|
||||
@ -119,7 +119,7 @@ fn count<A,IA:iterable<A>>(self: IA, x: A) -> uint {
|
||||
}
|
||||
|
||||
fn repeat(times: uint, blk: fn()) {
|
||||
let i = 0u;
|
||||
let mut i = 0u;
|
||||
while i < times {
|
||||
blk();
|
||||
i += 1u;
|
||||
|
@ -41,7 +41,7 @@ native mod rustrt {
|
||||
|
||||
|
||||
fn env() -> [(str,str)] {
|
||||
let pairs = [];
|
||||
let mut pairs = [];
|
||||
for p in rustrt::rust_env_pairs() {
|
||||
let vs = str::splitn_char(p, '=', 1u);
|
||||
assert vec::len(vs) == 2u;
|
||||
@ -87,7 +87,7 @@ mod win32 {
|
||||
}
|
||||
|
||||
fn as_utf16_p<T>(s: str, f: fn(*u16) -> T) -> T {
|
||||
let t = str::to_utf16(s);
|
||||
let mut t = str::to_utf16(s);
|
||||
// Null terminate before passing on.
|
||||
t += [0u16];
|
||||
vec::as_buf(t, f)
|
||||
@ -468,13 +468,13 @@ fn list_dir(p: path) -> [str] {
|
||||
#[cfg(target_os = "win32")]
|
||||
fn star() -> str { "*" }
|
||||
|
||||
let p = p;
|
||||
let mut p = p;
|
||||
let pl = str::len(p);
|
||||
if pl == 0u || (p[pl - 1u] as char != path::consts::path_sep
|
||||
&& p[pl - 1u] as char != path::consts::alt_path_sep) {
|
||||
p += path::path_sep();
|
||||
}
|
||||
let full_paths: [str] = [];
|
||||
let mut full_paths: [str] = [];
|
||||
for filename: str in rustrt::rust_list_files(p + star()) {
|
||||
if !str::eq(filename, ".") {
|
||||
if !str::eq(filename, "..") {
|
||||
|
@ -112,8 +112,8 @@ with a single path separator between them.
|
||||
*/
|
||||
|
||||
fn connect(pre: path, post: path) -> path unsafe {
|
||||
let pre_ = pre;
|
||||
let post_ = post;
|
||||
let mut pre_ = pre;
|
||||
let mut post_ = post;
|
||||
let sep = consts::path_sep as u8;
|
||||
let pre_len = str::len(pre);
|
||||
let post_len = str::len(post);
|
||||
@ -246,9 +246,9 @@ fn normalize(p: path) -> path {
|
||||
ret [];
|
||||
}
|
||||
|
||||
let t = [];
|
||||
let i = vec::len(s);
|
||||
let skip = 0;
|
||||
let mut t = [];
|
||||
let mut i = vec::len(s);
|
||||
let mut skip = 0;
|
||||
do {
|
||||
i -= 1u;
|
||||
if s[i] == ".." {
|
||||
@ -261,7 +261,7 @@ fn normalize(p: path) -> path {
|
||||
}
|
||||
}
|
||||
} while i != 0u;
|
||||
let t = vec::reversed(t);
|
||||
let mut t = vec::reversed(t);
|
||||
while skip > 0 {
|
||||
t += [".."];
|
||||
skip -= 1;
|
||||
|
@ -127,7 +127,7 @@ Convert a byte to a UTF-8 string. Fails if invalid UTF-8.
|
||||
*/
|
||||
fn from_byte(b: u8) -> str unsafe {
|
||||
assert b < 128u8;
|
||||
let v = [b, 0u8];
|
||||
let mut v = [b, 0u8];
|
||||
let s: str = ::unsafe::reinterpret_cast(v);
|
||||
::unsafe::leak(v);
|
||||
s
|
||||
@ -176,7 +176,7 @@ Function: from_char
|
||||
Convert a char to a string
|
||||
*/
|
||||
fn from_char(ch: char) -> str {
|
||||
let buf = "";
|
||||
let mut buf = "";
|
||||
push_char(buf, ch);
|
||||
ret buf;
|
||||
}
|
||||
@ -187,7 +187,7 @@ Function: from_chars
|
||||
Convert a vector of chars to a string
|
||||
*/
|
||||
fn from_chars(chs: [char]) -> str {
|
||||
let buf = "";
|
||||
let mut buf = "";
|
||||
reserve(buf, chs.len());
|
||||
for ch in chs { push_char(buf, ch); }
|
||||
ret buf;
|
||||
@ -199,7 +199,7 @@ Function: from_cstr
|
||||
Create a Rust string from a null-terminated C string
|
||||
*/
|
||||
fn from_cstr(cstr: sbuf) -> str unsafe {
|
||||
let curr = cstr, i = 0u;
|
||||
let mut curr = cstr, i = 0u;
|
||||
while *curr != 0u8 {
|
||||
i += 1u;
|
||||
curr = ptr::offset(cstr, i);
|
||||
@ -213,7 +213,7 @@ Function: from_cstr_len
|
||||
Create a Rust string from a C string of the given length
|
||||
*/
|
||||
fn from_cstr_len(cstr: sbuf, len: uint) -> str unsafe {
|
||||
let buf: [u8] = [];
|
||||
let mut buf: [u8] = [];
|
||||
vec::reserve(buf, len + 1u);
|
||||
vec::as_buf(buf) {|b| ptr::memcpy(b, cstr, len); }
|
||||
vec::unsafe::set_len(buf, len);
|
||||
@ -231,7 +231,7 @@ Function: concat
|
||||
Concatenate a vector of strings
|
||||
*/
|
||||
fn concat(v: [str]) -> str {
|
||||
let s: str = "";
|
||||
let mut s: str = "";
|
||||
for ss: str in v { s += ss; }
|
||||
ret s;
|
||||
}
|
||||
@ -242,7 +242,7 @@ Function: connect
|
||||
Concatenate a vector of strings, placing a given separator between each
|
||||
*/
|
||||
fn connect(v: [str], sep: str) -> str {
|
||||
let s = "", first = true;
|
||||
let mut s = "", first = true;
|
||||
for ss: str in v {
|
||||
if first { first = false; } else { s += sep; }
|
||||
s += ss;
|
||||
@ -350,7 +350,8 @@ Function: chars
|
||||
Convert a string to a vector of characters
|
||||
*/
|
||||
fn chars(s: str) -> [char] {
|
||||
let buf = [], i = 0u, len = len(s);
|
||||
let mut buf = [], i = 0u;
|
||||
let len = len(s);
|
||||
while i < len {
|
||||
let {ch, next} = char_range_at(s, i);
|
||||
buf += [ch];
|
||||
@ -408,8 +409,9 @@ fn split_char_nonempty(s: str, sep: char) -> [str] {
|
||||
fn split_char_inner(s: str, sep: char, count: uint, allow_empty: bool)
|
||||
-> [str] unsafe {
|
||||
if sep < 128u as char {
|
||||
let result = [], b = sep as u8, l = len(s), done = 0u;
|
||||
let i = 0u, start = 0u;
|
||||
let b = sep as u8, l = len(s);
|
||||
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 {
|
||||
@ -458,7 +460,8 @@ fn split_nonempty(s: str, sepfn: fn(char) -> bool) -> [str] {
|
||||
|
||||
fn split_inner(s: str, sepfn: fn(cc: char) -> bool, count: uint,
|
||||
allow_empty: bool) -> [str] unsafe {
|
||||
let result = [], i = 0u, l = len(s), start = 0u, done = 0u;
|
||||
let l = len(s);
|
||||
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) {
|
||||
@ -480,7 +483,7 @@ fn split_inner(s: str, sepfn: fn(cc: char) -> bool, count: uint,
|
||||
fn iter_matches(s: str, sep: str, f: fn(uint, uint)) {
|
||||
let sep_len = len(sep), l = len(s);
|
||||
assert sep_len > 0u;
|
||||
let i = 0u, match_start = 0u, match_i = 0u;
|
||||
let mut i = 0u, match_start = 0u, match_i = 0u;
|
||||
|
||||
while i < l {
|
||||
if s[i] == sep[match_i] {
|
||||
@ -505,7 +508,7 @@ fn iter_matches(s: str, sep: str, f: fn(uint, uint)) {
|
||||
}
|
||||
|
||||
fn iter_between_matches(s: str, sep: str, f: fn(uint, uint)) {
|
||||
let last_end = 0u;
|
||||
let mut last_end = 0u;
|
||||
iter_matches(s, sep) {|from, to|
|
||||
f(last_end, from);
|
||||
last_end = to;
|
||||
@ -522,7 +525,7 @@ Note that this has recently been changed. For example:
|
||||
> assert ["", "XXX", "YYY", ""] == split_str(".XXX.YYY.", ".")
|
||||
*/
|
||||
fn split_str(s: str, sep: str) -> [str] {
|
||||
let result = [];
|
||||
let mut result = [];
|
||||
iter_between_matches(s, sep) {|from, to|
|
||||
unsafe { result += [unsafe::slice_bytes(s, from, to)]; }
|
||||
}
|
||||
@ -530,7 +533,7 @@ fn split_str(s: str, sep: str) -> [str] {
|
||||
}
|
||||
|
||||
fn split_str_nonempty(s: str, sep: str) -> [str] {
|
||||
let result = [];
|
||||
let mut result = [];
|
||||
iter_between_matches(s, sep) {|from, to|
|
||||
if to > from {
|
||||
unsafe { result += [unsafe::slice_bytes(s, from, to)]; }
|
||||
@ -555,7 +558,8 @@ separated by LF ('\n') and/or CR LF ('\r\n')
|
||||
*/
|
||||
fn lines_any(s: str) -> [str] {
|
||||
vec::map(lines(s), {|s|
|
||||
let l = len(s), cp = s;
|
||||
let l = len(s);
|
||||
let mut cp = s;
|
||||
if l > 0u && s[l - 1u] == '\r' as u8 {
|
||||
unsafe { unsafe::set_len(cp, l - 1u); }
|
||||
}
|
||||
@ -607,7 +611,7 @@ Returns:
|
||||
The original string with all occurances of `from` replaced with `to`
|
||||
*/
|
||||
fn replace(s: str, from: str, to: str) -> str unsafe {
|
||||
let result = "", first = true;
|
||||
let mut result = "", first = true;
|
||||
iter_between_matches(s, from) {|start, end|
|
||||
if first { first = false; } else { result += to; }
|
||||
unsafe { result += unsafe::slice_bytes(s, start, end); }
|
||||
@ -641,7 +645,7 @@ String hash function
|
||||
fn hash(&&s: str) -> uint {
|
||||
// djb hash.
|
||||
// FIXME: replace with murmur.
|
||||
let u: uint = 5381u;
|
||||
let mut u: uint = 5381u;
|
||||
for c: u8 in s { u *= 33u; u += c as uint; }
|
||||
ret u;
|
||||
}
|
||||
@ -676,7 +680,7 @@ Function: map
|
||||
Apply a function to each character
|
||||
*/
|
||||
fn map(ss: str, ff: fn(char) -> char) -> str {
|
||||
let result = "";
|
||||
let mut result = "";
|
||||
reserve(result, len(ss));
|
||||
chars_iter(ss) {|cc| str::push_char(result, ff(cc));}
|
||||
result
|
||||
@ -688,7 +692,7 @@ Function: bytes_iter
|
||||
Iterate over the bytes in a string
|
||||
*/
|
||||
fn bytes_iter(ss: str, it: fn(u8)) {
|
||||
let pos = 0u;
|
||||
let mut pos = 0u;
|
||||
let len = len(ss);
|
||||
|
||||
while (pos < len) {
|
||||
@ -703,7 +707,8 @@ Function: chars_iter
|
||||
Iterate over the characters in a string
|
||||
*/
|
||||
fn chars_iter(s: str, it: fn(char)) {
|
||||
let pos = 0u, len = len(s);
|
||||
let mut pos = 0u;
|
||||
let len = len(s);
|
||||
while (pos < len) {
|
||||
let {ch, next} = char_range_at(s, pos);
|
||||
pos = next;
|
||||
@ -778,7 +783,8 @@ fn find_char_between(s: str, c: char, start: uint, end: uint)
|
||||
if c < 128u as char {
|
||||
assert start <= end;
|
||||
assert end <= len(s);
|
||||
let i = start, b = c as u8;
|
||||
let mut i = start;
|
||||
let b = c as u8;
|
||||
while i < end {
|
||||
if s[i] == b { ret some(i); }
|
||||
i += 1u;
|
||||
@ -815,7 +821,8 @@ fn rfind_char_between(s: str, c: char, start: uint, end: uint)
|
||||
if c < 128u as char {
|
||||
assert start >= end;
|
||||
assert start <= len(s);
|
||||
let i = start, b = c as u8;
|
||||
let mut i = start;
|
||||
let b = c as u8;
|
||||
while i > end {
|
||||
i -= 1u;
|
||||
if s[i] == b { ret some(i); }
|
||||
@ -851,7 +858,7 @@ fn find_between(s: str, start: uint, end: uint, f: fn(char) -> bool)
|
||||
assert start <= end;
|
||||
assert end <= len(s);
|
||||
assert is_char_boundary(s, start);
|
||||
let i = start;
|
||||
let mut i = start;
|
||||
while i < end {
|
||||
let {ch, next} = char_range_at(s, i);
|
||||
if f(ch) { ret some(i); }
|
||||
@ -886,7 +893,7 @@ fn rfind_between(s: str, start: uint, end: uint, f: fn(char) -> bool)
|
||||
assert start >= end;
|
||||
assert start <= len(s);
|
||||
assert is_char_boundary(s, start);
|
||||
let i = start;
|
||||
let mut i = start;
|
||||
while i > end {
|
||||
let {ch, prev} = char_range_at_reverse(s, i);
|
||||
if f(ch) { ret some(prev); }
|
||||
@ -897,7 +904,7 @@ fn rfind_between(s: str, start: uint, end: uint, f: fn(char) -> bool)
|
||||
|
||||
// Utility used by various searching functions
|
||||
fn match_at(haystack: str, needle: str, at: uint) -> bool {
|
||||
let i = at;
|
||||
let mut i = at;
|
||||
for c in needle { if haystack[i] != c { ret false; } i += 1u; }
|
||||
ret true;
|
||||
}
|
||||
@ -931,7 +938,8 @@ fn find_str_between(haystack: str, needle: str, start: uint, end:uint)
|
||||
if needle_len == 0u { ret some(start); }
|
||||
if needle_len > end { ret none; }
|
||||
|
||||
let i = start, e = end - needle_len;
|
||||
let mut i = start;
|
||||
let e = end - needle_len;
|
||||
while i <= e {
|
||||
if match_at(haystack, needle, i) { ret some(i); }
|
||||
i += 1u;
|
||||
@ -995,7 +1003,7 @@ Function: is_ascii
|
||||
Determines if a string contains only ASCII characters
|
||||
*/
|
||||
fn is_ascii(s: str) -> bool {
|
||||
let i: uint = len(s);
|
||||
let mut i: uint = len(s);
|
||||
while i > 0u { i -= 1u; if !u8::is_ascii(s[i]) { ret false; } }
|
||||
ret true;
|
||||
}
|
||||
@ -1048,10 +1056,10 @@ Function: is_utf8
|
||||
Determines if a vector of bytes contains valid UTF-8
|
||||
*/
|
||||
fn is_utf8(v: [const u8]) -> bool {
|
||||
let i = 0u;
|
||||
let mut i = 0u;
|
||||
let total = vec::len::<u8>(v);
|
||||
while i < total {
|
||||
let chsize = utf8_char_width(v[i]);
|
||||
let mut chsize = utf8_char_width(v[i]);
|
||||
if chsize == 0u { ret false; }
|
||||
if i + chsize > total { ret false; }
|
||||
i += 1u;
|
||||
@ -1067,7 +1075,7 @@ fn is_utf8(v: [const u8]) -> bool {
|
||||
|
||||
fn is_utf16(v: [const u16]) -> bool {
|
||||
let len = v.len();
|
||||
let i = 0u;
|
||||
let mut i = 0u;
|
||||
while (i < len) {
|
||||
let u = v[i];
|
||||
|
||||
@ -1085,12 +1093,11 @@ fn is_utf16(v: [const u16]) -> bool {
|
||||
ret true;
|
||||
}
|
||||
|
||||
|
||||
fn to_utf16(s: str) -> [u16] {
|
||||
let u = [];
|
||||
let mut u = [];
|
||||
chars_iter(s) {|cch|
|
||||
// Arithmetic with u32 literals is easier on the eyes than chars.
|
||||
let ch = cch as u32;
|
||||
let mut ch = cch as u32;
|
||||
|
||||
if (ch & 0xFFFF_u32) == ch {
|
||||
// The BMP falls through (assuming non-surrogate, as it should)
|
||||
@ -1110,9 +1117,9 @@ fn to_utf16(s: str) -> [u16] {
|
||||
|
||||
fn utf16_chars(v: [const u16], f: fn(char)) {
|
||||
let len = v.len();
|
||||
let i = 0u;
|
||||
let mut i = 0u;
|
||||
while (i < len && v[i] != 0u16) {
|
||||
let u = v[i];
|
||||
let mut u = v[i];
|
||||
|
||||
if u <= 0xD7FF_u16 || u >= 0xE000_u16 {
|
||||
f(u as char);
|
||||
@ -1122,7 +1129,7 @@ fn utf16_chars(v: [const u16], f: fn(char)) {
|
||||
let u2 = v[i+1u];
|
||||
assert u >= 0xD800_u16 && u <= 0xDBFF_u16;
|
||||
assert u2 >= 0xDC00_u16 && u2 <= 0xDFFF_u16;
|
||||
let c = (u - 0xD800_u16) as char;
|
||||
let mut c = (u - 0xD800_u16) as char;
|
||||
c = c << 10;
|
||||
c |= (u2 - 0xDC00_u16) as char;
|
||||
c |= 0x1_0000_u32 as char;
|
||||
@ -1134,7 +1141,7 @@ fn utf16_chars(v: [const u16], f: fn(char)) {
|
||||
|
||||
|
||||
fn from_utf16(v: [const u16]) -> str {
|
||||
let buf = "";
|
||||
let mut buf = "";
|
||||
reserve(buf, v.len());
|
||||
utf16_chars(v) {|ch| push_char(buf, ch); }
|
||||
ret buf;
|
||||
@ -1157,7 +1164,7 @@ Returns:
|
||||
fn count_chars(s: str, start: uint, end: uint) -> uint {
|
||||
assert is_char_boundary(s, start);
|
||||
assert is_char_boundary(s, end);
|
||||
let i = start, len = 0u;
|
||||
let mut i = start, len = 0u;
|
||||
while i < end {
|
||||
let {next, _} = char_range_at(s, i);
|
||||
len += 1u;
|
||||
@ -1172,7 +1179,8 @@ fn count_chars(s: str, start: uint, end: uint) -> uint {
|
||||
// `start`.
|
||||
fn count_bytes(s: str, start: uint, n: uint) -> uint {
|
||||
assert is_char_boundary(s, start);
|
||||
let end = start, cnt = n, l = len(s);
|
||||
let mut end = start, cnt = n;
|
||||
let l = len(s);
|
||||
while cnt > 0u {
|
||||
assert end < l;
|
||||
let {next, _} = char_range_at(s, end);
|
||||
@ -1260,9 +1268,9 @@ fn char_range_at(s: str, i: uint) -> {ch: char, next: uint} {
|
||||
let w = utf8_char_width(b0);
|
||||
assert (w != 0u);
|
||||
if w == 1u { ret {ch: b0 as char, next: i + 1u}; }
|
||||
let val = 0u;
|
||||
let mut val = 0u;
|
||||
let end = i + w;
|
||||
let i = i + 1u;
|
||||
let mut i = i + 1u;
|
||||
while i < end {
|
||||
let byte = s[i];
|
||||
assert (byte & 192u8 == tag_cont_u8);
|
||||
@ -1289,7 +1297,7 @@ fn char_at(s: str, i: uint) -> char { ret char_range_at(s, i).ch; }
|
||||
// Given a byte position and a str, return the previous char and its position
|
||||
// This function can be used to iterate over a unicode string in reverse.
|
||||
fn char_range_at_reverse(ss: str, start: uint) -> {ch: char, prev: uint} {
|
||||
let prev = start;
|
||||
let mut prev = start;
|
||||
|
||||
// while there is a previous byte == 10......
|
||||
while prev > 0u && ss[prev - 1u] & 192u8 == tag_cont_u8 {
|
||||
@ -1327,7 +1335,7 @@ Safety note:
|
||||
*/
|
||||
fn all_between(s: str, start: uint, end: uint, it: fn(char) -> bool) -> bool {
|
||||
assert is_char_boundary(s, start);
|
||||
let i = start;
|
||||
let mut i = start;
|
||||
while i < end {
|
||||
let {ch, next} = char_range_at(s, i);
|
||||
if !it(ch) { ret false; }
|
||||
@ -1366,7 +1374,7 @@ Example:
|
||||
|
||||
*/
|
||||
fn as_bytes<T>(s: str, f: fn([u8]) -> T) -> T unsafe {
|
||||
let v: [u8] = ::unsafe::reinterpret_cast(s);
|
||||
let mut v: [u8] = ::unsafe::reinterpret_cast(s);
|
||||
let r = f(v);
|
||||
::unsafe::leak(v);
|
||||
r
|
||||
@ -1421,7 +1429,7 @@ mod unsafe {
|
||||
// Converts a vector of bytes to a string. Does not verify that the
|
||||
// vector contains valid UTF-8.
|
||||
unsafe fn from_bytes(v: [const u8]) -> str unsafe {
|
||||
let vcopy: [u8] = v + [0u8];
|
||||
let mut vcopy: [u8] = v + [0u8];
|
||||
let scopy: str = ::unsafe::reinterpret_cast(vcopy);
|
||||
::unsafe::leak(vcopy);
|
||||
ret scopy;
|
||||
@ -1448,7 +1456,7 @@ mod unsafe {
|
||||
assert (begin <= end);
|
||||
assert (end <= len(s));
|
||||
|
||||
let v = as_bytes(s) { |v| vec::slice(v, begin, end) };
|
||||
let mut v = as_bytes(s) { |v| vec::slice(v, begin, end) };
|
||||
v += [0u8];
|
||||
let s: str = ::unsafe::reinterpret_cast(v);
|
||||
::unsafe::leak(v);
|
||||
|
@ -309,7 +309,7 @@ fn future_result(builder: task_builder) -> future::future<task_result> {
|
||||
fn future_task(builder: task_builder) -> future::future<task> {
|
||||
#[doc = "Get a future representing the handle to the new task"];
|
||||
|
||||
let po = comm::port();
|
||||
let mut po = comm::port();
|
||||
let ch = comm::chan(po);
|
||||
add_wrapper(builder) {|body|
|
||||
fn~() {
|
||||
@ -349,7 +349,7 @@ fn run_listener<A:send>(-builder: task_builder,
|
||||
|
||||
run(builder) {||
|
||||
let po = comm::port();
|
||||
let ch = comm::chan(po);
|
||||
let mut ch = comm::chan(po);
|
||||
comm::send(setup_ch, ch);
|
||||
f(po);
|
||||
}
|
||||
@ -421,7 +421,7 @@ fn spawn_sched(mode: sched_mode, +f: fn~()) {
|
||||
|
||||
")];
|
||||
|
||||
let builder = mk_task_builder();
|
||||
let mut builder = mk_task_builder();
|
||||
set_opts(builder, {
|
||||
sched: some({
|
||||
mode: mode,
|
||||
@ -448,7 +448,7 @@ fn try<T:send>(+f: fn~() -> T) -> result::t<T,()> {
|
||||
|
||||
let po = comm::port();
|
||||
let ch = comm::chan(po);
|
||||
let builder = mk_task_builder();
|
||||
let mut builder = mk_task_builder();
|
||||
unsupervise(builder);
|
||||
let result = future_result(builder);
|
||||
run(builder) {||
|
||||
@ -467,7 +467,7 @@ fn yield() {
|
||||
#[doc = "Yield control to the task scheduler"];
|
||||
|
||||
let task_ = rustrt::rust_get_task();
|
||||
let killed = false;
|
||||
let mut killed = false;
|
||||
rusti::task_yield(task_, killed);
|
||||
if killed && !failing() {
|
||||
fail "killed";
|
||||
@ -499,7 +499,7 @@ type rust_closure = ctypes::void;
|
||||
|
||||
fn spawn_raw(opts: task_opts, +f: fn~()) unsafe {
|
||||
|
||||
let f = if opts.supervise {
|
||||
let mut f = if opts.supervise {
|
||||
f
|
||||
} else {
|
||||
// FIXME: The runtime supervision API is weird here because it
|
||||
@ -924,4 +924,4 @@ fn test_avoid_copying_the_body_unsupervise() {
|
||||
f();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -37,7 +37,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] {
|
||||
fn to_str() -> str {
|
||||
let acc = "[", first = true;
|
||||
let mut acc = "[", first = true;
|
||||
for elt in self {
|
||||
if first { first = false; }
|
||||
else { acc += ", "; }
|
||||
|
@ -26,7 +26,7 @@ pure fn nonnegative(x: u16) -> bool { x >= 0u16 }
|
||||
|
||||
#[doc = "Iterate over the range [`lo`..`hi`)"]
|
||||
fn range(lo: u16, hi: u16, it: fn(u16)) {
|
||||
let i = lo;
|
||||
let mut i = lo;
|
||||
while i < hi { it(i); i += 1u16; }
|
||||
}
|
||||
|
||||
|
@ -60,7 +60,7 @@ Function: range
|
||||
Iterate over the range [`lo`..`hi`)
|
||||
*/
|
||||
fn range(lo: u32, hi: u32, it: fn(u32)) {
|
||||
let i = lo;
|
||||
let mut i = lo;
|
||||
while i < hi { it(i); i += 1u32; }
|
||||
}
|
||||
|
||||
|
@ -60,7 +60,7 @@ Function: range
|
||||
Iterate over the range [`lo`..`hi`)
|
||||
*/
|
||||
fn range(lo: u64, hi: u64, it: fn(u64)) {
|
||||
let i = lo;
|
||||
let mut i = lo;
|
||||
while i < hi { it(i); i += 1u64; }
|
||||
}
|
||||
|
||||
@ -98,9 +98,9 @@ fn to_str(n: u64, radix: uint) -> str {
|
||||
|
||||
if n == 0u64 { ret "0"; }
|
||||
|
||||
let s = "";
|
||||
let mut s = "";
|
||||
|
||||
let n = n;
|
||||
let mut n = n;
|
||||
while n > 0u64 { s = digit(n % r64) + s; n /= r64; }
|
||||
ret s;
|
||||
}
|
||||
@ -119,8 +119,8 @@ Parse a string as an unsigned integer.
|
||||
*/
|
||||
fn from_str(buf: str, radix: u64) -> option<u64> {
|
||||
if str::len(buf) == 0u { ret none; }
|
||||
let i = str::len(buf) - 1u;
|
||||
let power = 1u64, n = 0u64;
|
||||
let mut i = str::len(buf) - 1u;
|
||||
let mut power = 1u64, n = 0u64;
|
||||
while true {
|
||||
alt char::to_digit(buf[i] as char, radix as uint) {
|
||||
some(d) { n += d as u64 * power; }
|
||||
|
@ -60,7 +60,7 @@ Function: range
|
||||
Iterate over the range [`lo`..`hi`)
|
||||
*/
|
||||
fn range(lo: u8, hi: u8, it: fn(u8)) {
|
||||
let i = lo;
|
||||
let mut i = lo;
|
||||
while i < hi { it(i); i += 1u8; }
|
||||
}
|
||||
|
||||
|
@ -133,7 +133,7 @@ Iterate over the range [`lo`..`hi`)
|
||||
*/
|
||||
#[inline(always)]
|
||||
fn range(lo: uint, hi: uint, it: fn(uint)) {
|
||||
let i = lo;
|
||||
let mut i = lo;
|
||||
while i < hi { it(i); i += 1u; }
|
||||
}
|
||||
|
||||
@ -154,7 +154,7 @@ Returns:
|
||||
that is if `it` returned `false` at any point.
|
||||
*/
|
||||
fn loop(lo: uint, hi: uint, it: fn(uint) -> bool) -> bool {
|
||||
let i = lo;
|
||||
let mut i = lo;
|
||||
while i < hi {
|
||||
if (!it(i)) { ret false; }
|
||||
i += 1u;
|
||||
@ -169,8 +169,8 @@ Returns the smallest power of 2 greater than or equal to `n`
|
||||
*/
|
||||
fn next_power_of_two(n: uint) -> uint {
|
||||
let halfbits: uint = sys::size_of::<uint>() * 4u;
|
||||
let tmp: uint = n - 1u;
|
||||
let shift: uint = 1u;
|
||||
let mut tmp: uint = n - 1u;
|
||||
let mut shift: uint = 1u;
|
||||
while shift <= halfbits { tmp |= tmp >> shift; shift <<= 1u; }
|
||||
ret tmp + 1u;
|
||||
}
|
||||
@ -191,9 +191,9 @@ buf must not be empty
|
||||
*/
|
||||
fn parse_buf(buf: [u8], radix: uint) -> option<uint> {
|
||||
if vec::len(buf) == 0u { ret none; }
|
||||
let i = vec::len(buf) - 1u;
|
||||
let power = 1u;
|
||||
let n = 0u;
|
||||
let mut i = vec::len(buf) - 1u;
|
||||
let mut power = 1u;
|
||||
let mut n = 0u;
|
||||
while true {
|
||||
alt char::to_digit(buf[i] as char, radix) {
|
||||
some(d) { n += d * power; }
|
||||
@ -219,7 +219,7 @@ Function: to_str
|
||||
Convert to a string in a given base
|
||||
*/
|
||||
fn to_str(num: uint, radix: uint) -> str {
|
||||
let n = num;
|
||||
let mut n = num;
|
||||
assert (0u < radix && radix <= 16u);
|
||||
fn digit(n: uint) -> char {
|
||||
ret alt n {
|
||||
@ -243,13 +243,13 @@ fn to_str(num: uint, radix: uint) -> str {
|
||||
};
|
||||
}
|
||||
if n == 0u { ret "0"; }
|
||||
let s: str = "";
|
||||
let mut s: str = "";
|
||||
while n != 0u {
|
||||
s += str::from_byte(digit(n % radix) as u8);
|
||||
n /= radix;
|
||||
}
|
||||
let s1: str = "";
|
||||
let len: uint = str::len(s);
|
||||
let mut s1: str = "";
|
||||
let mut len: uint = str::len(s);
|
||||
while len != 0u { len -= 1u; s1 += str::from_byte(s[len]); }
|
||||
ret s1;
|
||||
}
|
||||
|
@ -90,9 +90,9 @@ Creates an immutable vector of size `n_elts` and initializes the elements
|
||||
to the value returned by the function `op`.
|
||||
*/
|
||||
fn init_fn<T>(n_elts: uint, op: init_op<T>) -> [T] {
|
||||
let v = [];
|
||||
let mut v = [];
|
||||
reserve(v, n_elts);
|
||||
let i: uint = 0u;
|
||||
let mut i: uint = 0u;
|
||||
while i < n_elts { v += [op(i)]; i += 1u; }
|
||||
ret v;
|
||||
}
|
||||
@ -106,9 +106,9 @@ Creates an immutable vector of size `n_elts` and initializes the elements
|
||||
to the value `t`.
|
||||
*/
|
||||
fn init_elt<T: copy>(n_elts: uint, t: T) -> [T] {
|
||||
let v = [];
|
||||
let mut v = [];
|
||||
reserve(v, n_elts);
|
||||
let i: uint = 0u;
|
||||
let mut i: uint = 0u;
|
||||
while i < n_elts { v += [t]; i += 1u; }
|
||||
ret v;
|
||||
}
|
||||
@ -217,9 +217,9 @@ Returns a copy of the elements from [`start`..`end`) from `v`.
|
||||
fn slice<T: copy>(v: [const T], start: uint, end: uint) -> [T] {
|
||||
assert (start <= end);
|
||||
assert (end <= len(v));
|
||||
let result = [];
|
||||
let mut result = [];
|
||||
reserve(result, end - start);
|
||||
let i = start;
|
||||
let mut i = start;
|
||||
while i < end { result += [v[i]]; i += 1u; }
|
||||
ret result;
|
||||
}
|
||||
@ -233,8 +233,8 @@ fn split<T: copy>(v: [const T], f: fn(T) -> bool) -> [[T]] {
|
||||
let ln = len(v);
|
||||
if (ln == 0u) { ret [] }
|
||||
|
||||
let start = 0u;
|
||||
let result = [];
|
||||
let mut start = 0u;
|
||||
let mut result = [];
|
||||
while start < ln {
|
||||
alt position_from(v, start, ln, f) {
|
||||
none { break }
|
||||
@ -258,9 +258,9 @@ fn splitn<T: copy>(v: [const T], n: uint, f: fn(T) -> bool) -> [[T]] {
|
||||
let ln = len(v);
|
||||
if (ln == 0u) { ret [] }
|
||||
|
||||
let start = 0u;
|
||||
let count = n;
|
||||
let result = [];
|
||||
let mut start = 0u;
|
||||
let mut count = n;
|
||||
let mut result = [];
|
||||
while start < ln && count > 0u {
|
||||
alt position_from(v, start, ln, f) {
|
||||
none { break }
|
||||
@ -286,8 +286,8 @@ fn rsplit<T: copy>(v: [const T], f: fn(T) -> bool) -> [[T]] {
|
||||
let ln = len(v);
|
||||
if (ln == 0u) { ret [] }
|
||||
|
||||
let end = ln;
|
||||
let result = [];
|
||||
let mut end = ln;
|
||||
let mut result = [];
|
||||
while end > 0u {
|
||||
alt rposition_from(v, 0u, end, f) {
|
||||
none { break }
|
||||
@ -311,9 +311,9 @@ fn rsplitn<T: copy>(v: [const T], n: uint, f: fn(T) -> bool) -> [[T]] {
|
||||
let ln = len(v);
|
||||
if (ln == 0u) { ret [] }
|
||||
|
||||
let end = ln;
|
||||
let count = n;
|
||||
let result = [];
|
||||
let mut end = ln;
|
||||
let mut count = n;
|
||||
let mut result = [];
|
||||
while end > 0u && count > 0u {
|
||||
alt rposition_from(v, 0u, end, f) {
|
||||
none { break }
|
||||
@ -385,7 +385,7 @@ initval - The value for the new elements
|
||||
*/
|
||||
fn grow<T: copy>(&v: [const T], n: uint, initval: T) {
|
||||
reserve(v, next_power_of_two(len(v) + n));
|
||||
let i: uint = 0u;
|
||||
let mut i: uint = 0u;
|
||||
while i < n { v += [initval]; i += 1u; }
|
||||
}
|
||||
|
||||
@ -405,7 +405,7 @@ init_op - A function to call to retreive each appended element's value
|
||||
*/
|
||||
fn grow_fn<T>(&v: [const T], n: uint, op: init_op<T>) {
|
||||
reserve(v, next_power_of_two(len(v) + n));
|
||||
let i: uint = 0u;
|
||||
let mut i: uint = 0u;
|
||||
while i < n { v += [op(i)]; i += 1u; }
|
||||
}
|
||||
|
||||
@ -433,7 +433,7 @@ Function: map
|
||||
Apply a function to each element of a vector and return the results
|
||||
*/
|
||||
fn map<T, U>(v: [T], f: fn(T) -> U) -> [U] {
|
||||
let result = [];
|
||||
let mut result = [];
|
||||
reserve(result, len(v));
|
||||
for elem: T in v { result += [f(elem)]; }
|
||||
ret result;
|
||||
@ -448,8 +448,8 @@ fn map2<T: copy, U: copy, V>(v0: [const T], v1: [const U],
|
||||
f: fn(T, U) -> V) -> [V] {
|
||||
let v0_len = len(v0);
|
||||
if v0_len != len(v1) { fail; }
|
||||
let u: [V] = [];
|
||||
let i = 0u;
|
||||
let mut u: [V] = [];
|
||||
let mut i = 0u;
|
||||
while i < v0_len { u += [f(copy v0[i], copy v1[i])]; i += 1u; }
|
||||
ret u;
|
||||
}
|
||||
@ -464,7 +464,7 @@ the resulting vector.
|
||||
*/
|
||||
fn filter_map<T: copy, U: copy>(v: [const T], f: fn(T) -> option<U>)
|
||||
-> [U] {
|
||||
let result = [];
|
||||
let mut result = [];
|
||||
for elem: T in v {
|
||||
alt f(copy elem) {
|
||||
none {/* no-op */ }
|
||||
@ -484,7 +484,7 @@ Apply function `f` to each element of `v` and return a vector containing
|
||||
only those elements for which `f` returned true.
|
||||
*/
|
||||
fn filter<T: copy>(v: [T], f: fn(T) -> bool) -> [T] {
|
||||
let result = [];
|
||||
let mut result = [];
|
||||
for elem: T in v {
|
||||
if f(elem) { result += [elem]; }
|
||||
}
|
||||
@ -498,7 +498,7 @@ Concatenate a vector of vectors. Flattens a vector of vectors of T into
|
||||
a single vector of T.
|
||||
*/
|
||||
fn concat<T: copy>(v: [const [const T]]) -> [T] {
|
||||
let new: [T] = [];
|
||||
let mut new: [T] = [];
|
||||
for inner: [T] in v { new += inner; }
|
||||
ret new;
|
||||
}
|
||||
@ -509,8 +509,8 @@ Function: connect
|
||||
Concatenate a vector of vectors, placing a given separator between each
|
||||
*/
|
||||
fn connect<T: copy>(v: [const [const T]], sep: T) -> [T] {
|
||||
let new: [T] = [];
|
||||
let first = true;
|
||||
let mut new: [T] = [];
|
||||
let mut first = true;
|
||||
for inner: [T] in v {
|
||||
if first { first = false; } else { push(new, sep); }
|
||||
new += inner;
|
||||
@ -524,7 +524,7 @@ Function: foldl
|
||||
Reduce a vector from left to right
|
||||
*/
|
||||
fn foldl<T: copy, U>(z: T, v: [const U], p: fn(T, U) -> T) -> T {
|
||||
let accum = z;
|
||||
let mut accum = z;
|
||||
iter(v) { |elt|
|
||||
accum = p(accum, elt);
|
||||
}
|
||||
@ -537,7 +537,7 @@ Function: foldr
|
||||
Reduce a vector from right to left
|
||||
*/
|
||||
fn foldr<T, U: copy>(v: [const T], z: U, p: fn(T, U) -> U) -> U {
|
||||
let accum = z;
|
||||
let mut accum = z;
|
||||
riter(v) { |elt|
|
||||
accum = p(elt, accum);
|
||||
}
|
||||
@ -566,7 +566,7 @@ If the vectors contains no elements then false is returned.
|
||||
fn any2<T, U>(v0: [const T], v1: [U], f: fn(T, U) -> bool) -> bool {
|
||||
let v0_len = len(v0);
|
||||
let v1_len = len(v1);
|
||||
let i = 0u;
|
||||
let mut i = 0u;
|
||||
while i < v0_len && i < v1_len {
|
||||
if f(v0[i], v1[i]) { ret true; };
|
||||
i += 1u;
|
||||
@ -596,7 +596,7 @@ If the vectors are not the same size then false is returned.
|
||||
fn all2<T, U>(v0: [const T], v1: [const U], f: fn(T, U) -> bool) -> bool {
|
||||
let v0_len = len(v0);
|
||||
if v0_len != len(v1) { ret false; }
|
||||
let i = 0u;
|
||||
let mut i = 0u;
|
||||
while i < v0_len { if !f(v0[i], v1[i]) { ret false; }; i += 1u; }
|
||||
ret true;
|
||||
}
|
||||
@ -617,7 +617,7 @@ Function: count
|
||||
Returns the number of elements that are equal to a given value
|
||||
*/
|
||||
fn count<T>(v: [const T], x: T) -> uint {
|
||||
let cnt = 0u;
|
||||
let mut cnt = 0u;
|
||||
for elt: T in v { if x == elt { cnt += 1u; } }
|
||||
ret cnt;
|
||||
}
|
||||
@ -716,7 +716,7 @@ fn position_from<T>(v: [const T], start: uint, end: uint,
|
||||
f: fn(T) -> bool) -> option<uint> {
|
||||
assert start <= end;
|
||||
assert end <= len(v);
|
||||
let i = start;
|
||||
let mut i = start;
|
||||
while i < end { if f(v[i]) { ret some::<uint>(i); } i += 1u; }
|
||||
ret none;
|
||||
}
|
||||
@ -761,7 +761,7 @@ fn rposition_from<T>(v: [const T], start: uint, end: uint,
|
||||
f: fn(T) -> bool) -> option<uint> {
|
||||
assert start <= end;
|
||||
assert end <= len(v);
|
||||
let i = end;
|
||||
let mut i = end;
|
||||
while i > start {
|
||||
if f(v[i - 1u]) { ret some::<uint>(i - 1u); }
|
||||
i -= 1u;
|
||||
@ -784,7 +784,7 @@ and the i-th element of the second vector contains the second element
|
||||
of the i-th tuple of the input vector.
|
||||
*/
|
||||
fn unzip<T: copy, U: copy>(v: [const (T, U)]) -> ([T], [U]) {
|
||||
let as = [], bs = [];
|
||||
let mut as = [], bs = [];
|
||||
for (a, b) in v { as += [a]; bs += [b]; }
|
||||
ret (as, bs);
|
||||
}
|
||||
@ -802,8 +802,9 @@ Preconditions:
|
||||
<same_length> (v, u)
|
||||
*/
|
||||
fn zip<T: copy, U: copy>(v: [const T], u: [const U]) -> [(T, U)] {
|
||||
let zipped = [];
|
||||
let sz = len(v), i = 0u;
|
||||
let mut zipped = [];
|
||||
let sz = len(v);
|
||||
let mut i = 0u;
|
||||
assert sz == len(u);
|
||||
while i < sz { zipped += [(v[i], u[i])]; i += 1u; }
|
||||
ret zipped;
|
||||
@ -829,7 +830,7 @@ Function: reverse
|
||||
Reverse the order of elements in a vector, in place
|
||||
*/
|
||||
fn reverse<T>(v: [mutable T]) {
|
||||
let i: uint = 0u;
|
||||
let mut i: uint = 0u;
|
||||
let ln = len::<T>(v);
|
||||
while i < ln / 2u { v[i] <-> v[ln - i - 1u]; i += 1u; }
|
||||
}
|
||||
@ -841,8 +842,8 @@ Function: reversed
|
||||
Returns a vector with the order of elements reversed
|
||||
*/
|
||||
fn reversed<T: copy>(v: [const T]) -> [T] {
|
||||
let rs: [T] = [];
|
||||
let i = len::<T>(v);
|
||||
let mut rs: [T] = [];
|
||||
let mut i = len::<T>(v);
|
||||
if i == 0u { ret rs; } else { i -= 1u; }
|
||||
while i != 0u { rs += [v[i]]; i -= 1u; }
|
||||
rs += [v[0]];
|
||||
@ -857,8 +858,8 @@ Returns a vector containing a range of chars
|
||||
*/
|
||||
fn enum_chars(start: u8, end: u8) -> [char] {
|
||||
assert start < end;
|
||||
let i = start;
|
||||
let r = [];
|
||||
let mut i = start;
|
||||
let mut r = [];
|
||||
while i <= end { r += [i as char]; i += 1u as u8; }
|
||||
ret r;
|
||||
}
|
||||
@ -871,8 +872,8 @@ Returns a vector containing a range of uints
|
||||
*/
|
||||
fn enum_uints(start: uint, end: uint) -> [uint] {
|
||||
assert start < end;
|
||||
let i = start;
|
||||
let r = [];
|
||||
let mut i = start;
|
||||
let mut r = [];
|
||||
while i <= end { r += [i]; i += 1u; }
|
||||
ret r;
|
||||
}
|
||||
@ -907,7 +908,7 @@ Iterates over two vectors in parallel
|
||||
*/
|
||||
#[inline]
|
||||
fn iter2<U, T>(v: [ U], v2: [const T], f: fn(U, T)) {
|
||||
let i = 0;
|
||||
let mut i = 0;
|
||||
for elt in v { f(elt, v2[i]); i += 1; }
|
||||
}
|
||||
|
||||
@ -921,7 +922,8 @@ element's value and index.
|
||||
*/
|
||||
#[inline(always)]
|
||||
fn iteri<T>(v: [const T], f: fn(uint, T)) {
|
||||
let i = 0u, l = len(v);
|
||||
let mut i = 0u;
|
||||
let l = len(v);
|
||||
while i < l { f(i, v[i]); i += 1u; }
|
||||
}
|
||||
|
||||
@ -947,7 +949,7 @@ Iterates over vector `v` and, for each element, calls function `f` with the
|
||||
element's value and index.
|
||||
*/
|
||||
fn riteri<T>(v: [const T], f: fn(uint, T)) {
|
||||
let i = len(v);
|
||||
let mut i = len(v);
|
||||
while 0u < i {
|
||||
i -= 1u;
|
||||
f(i, v[i]);
|
||||
@ -969,7 +971,7 @@ fn permute<T: copy>(v: [T], put: fn([T])) {
|
||||
if ln == 0u {
|
||||
put([]);
|
||||
} else {
|
||||
let i = 0u;
|
||||
let mut i = 0u;
|
||||
while i < ln {
|
||||
let elt = v[i];
|
||||
let rest = slice(v, 0u, i) + slice(v, i+1u, ln);
|
||||
@ -980,7 +982,7 @@ fn permute<T: copy>(v: [T], put: fn([T])) {
|
||||
}
|
||||
|
||||
fn windowed <TT: copy> (nn: uint, xx: [const TT]) -> [[TT]] {
|
||||
let ww = [];
|
||||
let mut ww = [];
|
||||
|
||||
assert 1u <= nn;
|
||||
|
||||
@ -1153,7 +1155,7 @@ mod u8 {
|
||||
// djb hash.
|
||||
// FIXME: replace with murmur.
|
||||
|
||||
let u: uint = 5381u;
|
||||
let mut u: uint = 5381u;
|
||||
vec::iter(s, { |c| u *= 33u; u += c as uint; });
|
||||
ret u;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user