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