Switch all vases of vec += elt to vec += vec. Prohibit former in rustboot. Tweak std lib vec fns in process.

This commit is contained in:
Graydon Hoare 2011-03-16 14:58:02 -07:00
parent 23eef4da22
commit 54587bdccb
29 changed files with 278 additions and 225 deletions

View file

@ -111,6 +111,11 @@ fn unsafe_from_bytes(vec[u8] v) -> str {
ret rustrt.str_from_vec(v);
}
fn unsafe_from_byte(u8 u) -> str {
ret rustrt.str_from_vec(vec(u));
}
fn refcount(str s) -> uint {
auto r = rustrt.refcount[u8](s);
if (r == dbg.const_refcount) {
@ -190,7 +195,6 @@ fn starts_with(str haystack, str needle) -> bool {
ret eq(substr(haystack, 0u, needle_len), needle);
}
fn ends_with(str haystack, str needle) -> bool {
let uint haystack_len = byte_len(haystack);
let uint needle_len = byte_len(needle);
@ -206,34 +210,60 @@ fn ends_with(str haystack, str needle) -> bool {
needle);
}
fn substr(str s, uint begin, uint len) -> str {
let str accum = "";
let uint i = begin;
while (i < begin+len) {
accum += s.(i);
accum += unsafe_from_byte(s.(i));
i += 1u;
}
ret accum;
}
fn shift_byte(&mutable str s) -> u8 {
auto len = byte_len(s);
check(len > 0u);
auto b = s.(0);
s = substr(s, 1u, len - 1u);
ret b;
}
fn pop_byte(&mutable str s) -> u8 {
auto len = byte_len(s);
check(len > 0u);
auto b = s.(len - 1u);
s = substr(s, 0u, len - 1u);
ret b;
}
fn push_byte(&mutable str s, u8 b) {
s += unsafe_from_byte(b);
}
fn unshift_byte(&mutable str s, u8 b) {
auto res = alloc(byte_len(s) + 1u);
res += unsafe_from_byte(b);
res += s;
s = res;
}
fn split(str s, u8 sep) -> vec[str] {
let vec[str] v = vec();
let str accum = "";
let bool ends_with_sep = false;
for (u8 c in s) {
if (c == sep) {
v += accum;
v += vec(accum);
accum = "";
ends_with_sep = true;
} else {
accum += c;
accum += unsafe_from_byte(c);
ends_with_sep = false;
}
}
if (_str.byte_len(accum) != 0u ||
ends_with_sep) {
v += accum;
v += vec(accum);
}
ret v;
}

View file

@ -61,7 +61,7 @@ fn to_str(mutable uint n, uint radix) -> str
let str s = "";
while (n != 0u) {
s += digit(n % radix) as u8;
s += _str.unsafe_from_byte(digit(n % radix) as u8);
n /= radix;
}
@ -69,7 +69,7 @@ fn to_str(mutable uint n, uint radix) -> str
let uint len = _str.byte_len(s);
while (len != 0u) {
len -= 1u;
s1 += s.(len);
s1 += _str.unsafe_from_byte(s.(len));
}
ret s1;

View file

@ -103,28 +103,32 @@ fn slice[T](vec[T] v, uint start, uint end) -> vec[T] {
ret result;
}
fn shift[T](vec[T] v) -> vec[T] {
check(len[T](v) > 0u);
ret slice[T](v, 1u, len[T](v));
fn shift[T](&mutable vec[T] v) -> T {
auto ln = len[T](v);
check(ln > 0u);
auto e = v.(0);
v = slice[T](v, 1u, ln);
ret e;
}
fn pop[T](vec[T] v) -> vec[T] {
check(len[T](v) > 0u);
ret slice[T](v, 0u, len[T](v) - 1u);
fn pop[T](&mutable vec[T] v) -> T {
auto ln = len[T](v);
check(ln > 0u);
ln -= 1u;
auto e = v.(ln);
v = slice[T](v, 0u, ln);
ret e;
}
fn push[T](vec[T] v, &T t) -> vec[T] {
v += t;
ret v;
fn push[T](&mutable vec[T] v, &T t) {
v += vec(t);
}
fn unshift[T](vec[T] v, &T t) -> vec[T] {
fn unshift[T](&mutable vec[T] v, &T t) {
auto res = alloc[T](len[T](v) + 1u);
res += t;
for (T t_ in v) {
res += t_;
}
ret res;
res += vec(t);
res += v;
v = res;
}
fn grow[T](&mutable vec[T] v, int n, &T initval) {
@ -152,7 +156,7 @@ fn map2[T,U,V](&operator2[T,U,V] f, &vec[T] v0, &vec[U] v1) -> vec[V] {
let vec[V] u = alloc[V](v0_len);
auto i = 0u;
while (i < v0_len) {
u += f(v0.(i), v1.(i));
u += vec(f(v0.(i), v1.(i)));
i += 1u;
}

View file

@ -38,8 +38,18 @@ impure fn list_dir(path p) -> vec[str] {
let vec[str] full_paths = vec();
for (str filename in os_fs.list_dir(p)) {
if (!_str.eq(filename, ".")) {if (!_str.eq(filename, "..")) {
full_paths = _vec.push[str](full_paths, p + filename);
_vec.push[str](full_paths, p + filename);
}}
}
ret full_paths;
}
// Local Variables:
// mode: rust;
// fill-column: 78;
// indent-tabs-mode: nil
// c-basic-offset: 4
// buffer-file-coding-system: utf-8-unix
// compile-command: "make -k -C .. 2>&1 | sed -e 's/\\/x\\//x:\\//g'";
// End:

View file

@ -10,10 +10,19 @@ impure fn list_dir(str path) -> vec[str] {
while (true) {
auto ent = os.libc.readdir(dir);
if (ent as int == 0) {break;}
result = _vec.push[str](result, rustrt.rust_dirent_filename(ent));
_vec.push[str](result, rustrt.rust_dirent_filename(ent));
}
os.libc.closedir(dir);
ret result;
}
const char path_sep = '/';
// Local Variables:
// mode: rust;
// fill-column: 78;
// indent-tabs-mode: nil
// c-basic-offset: 4
// buffer-file-coding-system: utf-8-unix
// compile-command: "make -k -C .. 2>&1 | sed -e 's/\\/x\\//x:\\//g'";
// End:

View file

@ -8,9 +8,9 @@ native "rust" mod rustrt {
fn argvec(str prog, vec[str] args) -> vec[sbuf] {
auto argptrs = vec(_str.buf(prog));
for (str arg in args) {
argptrs = _vec.push[sbuf](argptrs, _str.buf(arg));
_vec.push[sbuf](argptrs, _str.buf(arg));
}
argptrs = _vec.push[sbuf](argptrs, 0 as sbuf);
_vec.push[sbuf](argptrs, 0 as sbuf);
ret argptrs;
}

View file

@ -169,10 +169,11 @@ fn mk_sha1() -> sha1 {
let vec[u8] res = vec();
for (u32 hpart in st.h) {
res += (hpart >> 24u32) & 0xFFu32 as u8;
res += (hpart >> 16u32) & 0xFFu32 as u8;
res += (hpart >> 8u32) & 0xFFu32 as u8;
res += hpart & 0xFFu32 as u8;
auto a = (hpart >> 24u32) & 0xFFu32 as u8;
auto b = (hpart >> 16u32) & 0xFFu32 as u8;
auto c = (hpart >> 8u32) & 0xFFu32 as u8;
auto d = (hpart & 0xFFu32 as u8);
res += vec(a,b,c,d);
}
ret res;
}

View file

@ -5,38 +5,38 @@ type lteq[T] = fn(&T a, &T b) -> bool;
fn merge_sort[T](lteq[T] le, vec[T] v) -> vec[T] {
fn merge[T](lteq[T] le, vec[T] a, vec[T] b) -> vec[T] {
let vec[T] res = vec();
let uint a_len = len[T](a);
let uint a_ix = 0u;
let uint b_len = len[T](b);
let uint b_ix = 0u;
while (a_ix < a_len && b_ix < b_len) {
if (le(a.(a_ix), b.(b_ix))) {
res += a.(a_ix);
a_ix += 1u;
} else {
res += b.(b_ix);
b_ix += 1u;
}
fn merge[T](lteq[T] le, vec[T] a, vec[T] b) -> vec[T] {
let vec[T] res = vec();
let uint a_len = len[T](a);
let uint a_ix = 0u;
let uint b_len = len[T](b);
let uint b_ix = 0u;
while (a_ix < a_len && b_ix < b_len) {
if (le(a.(a_ix), b.(b_ix))) {
res += vec(a.(a_ix));
a_ix += 1u;
} else {
res += vec(b.(b_ix));
b_ix += 1u;
}
}
res += slice[T](a, a_ix, a_len);
res += slice[T](b, b_ix, b_len);
ret res;
}
res += slice[T](a, a_ix, a_len);
res += slice[T](b, b_ix, b_len);
ret res;
}
let uint v_len = len[T](v);
let uint v_len = len[T](v);
if (v_len <= 1u) {
ret v;
}
if (v_len <= 1u) {
ret v;
}
let uint mid = v_len / 2u;
let vec[T] a = slice[T](v, 0u, mid);
let vec[T] b = slice[T](v, mid, v_len);
ret merge[T](le,
merge_sort[T](le, a),
merge_sort[T](le, b));
let uint mid = v_len / 2u;
let vec[T] a = slice[T](v, 0u, mid);
let vec[T] b = slice[T](v, mid, v_len);
ret merge[T](le,
merge_sort[T](le, a),
merge_sort[T](le, b));
}
// Local Variables:

View file

@ -34,6 +34,17 @@ auth _str = unsafe;
auth _vec = unsafe;
auth _task = unsafe;
// FIXME: impure on these will infect caller in a way that is totally
// beyond reason, if the caller's mutated-argument doesn't escape;
// 'impure' needs work.
auth _str.unshift_byte = impure;
auth _str.shift_byte = impure;
auth _str.pop_byte = impure;
auth _vec.shift = impure;
auth _vec.unshift = impure;
auth _vec.pop = impure;
auth dbg = unsafe;
auth _uint.next_power_of_two = unsafe;

View file

@ -8,3 +8,12 @@ impure fn list_dir(str path) -> vec[str] {
}
const char path_sep = '\\';
// Local Variables:
// mode: rust;
// fill-column: 78;
// indent-tabs-mode: nil
// c-basic-offset: 4
// buffer-file-coding-system: utf-8-unix
// compile-command: "make -k -C .. 2>&1 | sed -e 's/\\/x\\//x:\\//g'";
// End: