diff options
Diffstat (limited to 'src/lib')
| -rw-r--r-- | src/lib/aio.rs | 58 | ||||
| -rw-r--r-- | src/lib/bitv.rs | 27 | ||||
| -rw-r--r-- | src/lib/char.rs | 80 | ||||
| -rw-r--r-- | src/lib/comm.rs | 42 | ||||
| -rw-r--r-- | src/lib/deque.rs | 33 | ||||
| -rw-r--r-- | src/lib/ebml.rs | 38 | ||||
| -rw-r--r-- | src/lib/either.rs | 19 | ||||
| -rw-r--r-- | src/lib/extfmt.rs | 52 | ||||
| -rw-r--r-- | src/lib/fs.rs | 8 | ||||
| -rw-r--r-- | src/lib/generic_os.rs | 2 | ||||
| -rw-r--r-- | src/lib/getopts.rs | 66 | ||||
| -rw-r--r-- | src/lib/int.rs | 2 | ||||
| -rw-r--r-- | src/lib/io.rs | 133 | ||||
| -rw-r--r-- | src/lib/list.rs | 21 | ||||
| -rw-r--r-- | src/lib/map.rs | 73 | ||||
| -rw-r--r-- | src/lib/net.rs | 23 | ||||
| -rw-r--r-- | src/lib/option.rs | 5 | ||||
| -rw-r--r-- | src/lib/rand.rs | 13 | ||||
| -rw-r--r-- | src/lib/run_program.rs | 38 | ||||
| -rw-r--r-- | src/lib/sha1.rs | 96 | ||||
| -rw-r--r-- | src/lib/sio.rs | 42 | ||||
| -rw-r--r-- | src/lib/smallintmap.rs | 4 | ||||
| -rw-r--r-- | src/lib/sort.rs | 32 | ||||
| -rw-r--r-- | src/lib/str.rs | 77 | ||||
| -rw-r--r-- | src/lib/task.rs | 146 | ||||
| -rw-r--r-- | src/lib/term.rs | 10 | ||||
| -rw-r--r-- | src/lib/test.rs | 102 | ||||
| -rw-r--r-- | src/lib/time.rs | 2 | ||||
| -rw-r--r-- | src/lib/u64.rs | 2 | ||||
| -rw-r--r-- | src/lib/ufind.rs | 12 | ||||
| -rw-r--r-- | src/lib/uint.rs | 8 | ||||
| -rw-r--r-- | src/lib/vec.rs | 109 | ||||
| -rw-r--r-- | src/lib/win32_fs.rs | 4 | ||||
| -rw-r--r-- | src/lib/win32_os.rs | 15 |
34 files changed, 640 insertions, 754 deletions
diff --git a/src/lib/aio.rs b/src/lib/aio.rs index fc10e40a327..c2e9c13dc89 100644 --- a/src/lib/aio.rs +++ b/src/lib/aio.rs @@ -28,26 +28,17 @@ native "rust" mod rustrt { // currently in the sendable kind, so we'll unsafely cast between ints. type server = rustrt::server; type client = rustrt::socket; -tag pending_connection { - remote(net::ip_addr,int); - incoming(server); -} +tag pending_connection { remote(net::ip_addr, int); incoming(server); } -tag socket_event { - connected(client); - closed; - received([u8]); -} +tag socket_event { connected(client); closed; received([u8]); } -tag server_event { - pending(_chan<_chan<socket_event>>); -} +tag server_event { pending(_chan<_chan<socket_event>>); } tag request { quit; - connect(pending_connection,_chan<socket_event>); - serve(net::ip_addr,int,_chan<server_event>,_chan<server>); - write(client,[u8],_chan<bool>); + connect(pending_connection, _chan<socket_event>); + serve(net::ip_addr, int, _chan<server_event>, _chan<server>); + write(client, [u8], _chan<bool>); close_server(server, _chan<bool>); close_client(client); } @@ -74,12 +65,12 @@ fn new_client(client: client, evt: _chan<socket_event>) { send(evt, connected(client)); - while (true) { + while true { log "waiting for bytes"; let data: [u8] = reader.recv(); log "got some bytes"; log vec::len::<u8>(data); - if (vec::len::<u8>(data) == 0u) { + if vec::len::<u8>(data) == 0u { log "got empty buffer, bailing"; break; } @@ -104,19 +95,17 @@ fn accept_task(client: client, events: _chan<server_event>) { fn server_task(ip: net::ip_addr, portnum: int, events: _chan<server_event>, server: _chan<server>) { let accepter: _port<client> = mk_port(); - send(server, rustrt::aio_serve(ip_to_sbuf(ip), portnum, - accepter.mk_chan())); + send(server, + rustrt::aio_serve(ip_to_sbuf(ip), portnum, accepter.mk_chan())); let client: client; - while (true) { + while true { log "preparing to accept a client"; client = accepter.recv(); - if (rustrt::aio_is_null_client(client)) { - log "client was actually null, returning"; - ret; - } else { - task::_spawn(bind accept_task(client, events)); - } + if rustrt::aio_is_null_client(client) { + log "client was actually null, returning"; + ret; + } else { task::_spawn(bind accept_task(client, events)); } } } @@ -128,7 +117,7 @@ fn request_task(c: _chan<ctx>) { log "uv run task spawned"; // Spin for requests let req: request; - while (true) { + while true { req = p.recv(); alt req { quit. { @@ -137,20 +126,19 @@ fn request_task(c: _chan<ctx>) { rustrt::aio_stop(); ret; } - connect(remote(ip,portnum),client) { + connect(remote(ip, portnum), client) { task::_spawn(bind connect_task(ip, portnum, client)); } - serve(ip,portnum,events,server) { + serve(ip, portnum, events, server) { task::_spawn(bind server_task(ip, portnum, events, server)); } - write(socket,v,status) { - rustrt::aio_writedata(socket, - vec::to_ptr::<u8>(v), vec::len::<u8>(v), - status); + write(socket, v, status) { + rustrt::aio_writedata(socket, vec::to_ptr::<u8>(v), + vec::len::<u8>(v), status); } - close_server(server,status) { + close_server(server, status) { log "closing server"; - rustrt::aio_close_server(server,status); + rustrt::aio_close_server(server, status); } close_client(client) { log "closing client"; diff --git a/src/lib/bitv.rs b/src/lib/bitv.rs index c42ef07bfb2..019aa32f36a 100644 --- a/src/lib/bitv.rs +++ b/src/lib/bitv.rs @@ -35,16 +35,16 @@ fn create(nbits: uint, init: bool) -> t { ret @{storage: storage, nbits: nbits}; } -fn process(op: &block(uint, uint) -> uint , v0: &t, v1: &t) -> bool { +fn process(op: &block(uint, uint) -> uint, v0: &t, v1: &t) -> bool { let len = vec::len(v1.storage); assert (vec::len(v0.storage) == len); assert (v0.nbits == v1.nbits); let changed = false; for each i: uint in uint::range(0u, len) { - let w0 = v0.storage.(i); - let w1 = v1.storage.(i); + let w0 = v0.storage[i]; + let w1 = v1.storage[i]; let w = op(w0, w1); - if w0 != w { changed = true; v0.storage.(i) = w; } + if w0 != w { changed = true; v0.storage[i] = w; } } ret changed; } @@ -70,7 +70,7 @@ fn assign(v0: &t, v1: t) -> bool { fn clone(v: t) -> t { let storage = vec::init_elt_mut::<uint>(0u, v.nbits / uint_bits() + 1u); let len = vec::len(v.storage); - for each i: uint in uint::range(0u, len) { storage.(i) = v.storage.(i); } + for each i: uint in uint::range(0u, len) { storage[i] = v.storage[i]; } ret @{storage: storage, nbits: v.nbits}; } @@ -79,7 +79,7 @@ fn get(v: &t, i: uint) -> bool { let bits = uint_bits(); let w = i / bits; let b = i % bits; - let x = 1u & v.storage.(w) >> b; + let x = 1u & v.storage[w] >> b; ret x == 1u; } @@ -90,7 +90,7 @@ fn equal(v0: &t, v1: &t) -> bool { let len = vec::len(v1.storage); let i = 0u; while i < len { - if v0.storage.(i) != v1.storage.(i) { ret false; } + if v0.storage[i] != v1.storage[i] { ret false; } i = i + 1u; } ret true; @@ -98,7 +98,7 @@ fn equal(v0: &t, v1: &t) -> bool { fn clear(v: &t) { for each i: uint in uint::range(0u, vec::len(v.storage)) { - v.storage.(i) = 0u; + v.storage[i] = 0u; } } @@ -108,7 +108,7 @@ fn set_all(v: &t) { fn invert(v: &t) { for each i: uint in uint::range(0u, vec::len(v.storage)) { - v.storage.(i) = !v.storage.(i); + v.storage[i] = !v.storage[i]; } } @@ -127,8 +127,7 @@ fn set(v: &t, i: uint, x: bool) { let w = i / bits; let b = i % bits; let flag = 1u << b; - v.storage.(w) = - if x { v.storage.(w) | flag } else { v.storage.(w) & !flag }; + v.storage[w] = if x { v.storage[w] | flag } else { v.storage[w] & !flag }; } @@ -154,9 +153,7 @@ fn to_vec(v: &t) -> [uint] { fn to_str(v: &t) -> str { let rs = ""; - for i: uint in to_vec(v) { - if i == 1u { rs += "1"; } else { rs += "0"; } - } + for i: uint in to_vec(v) { if i == 1u { rs += "1"; } else { rs += "0"; } } ret rs; } @@ -166,7 +163,7 @@ fn eq_vec(v0: &t, v1: &[uint]) -> bool { let i = 0u; while i < len { let w0 = get(v0, i); - let w1 = v1.(i); + let w1 = v1[i]; if !w0 && w1 != 0u || w0 && w1 == 0u { ret false; } i = i + 1u; } diff --git a/src/lib/char.rs b/src/lib/char.rs index 65e6de5373d..634c9b9647c 100644 --- a/src/lib/char.rs +++ b/src/lib/char.rs @@ -26,31 +26,55 @@ pred is_whitespace(c: char) -> bool { const ch_next_line: char = '\u0085'; const ch_no_break_space: char = '\u00a0'; - if c == ch_space { true } - else if c == ch_ogham_space_mark { true } - else if c == ch_mongolian_vowel_sep { true } - else if c == ch_en_quad { true } - else if c == ch_em_quad { true } - else if c == ch_en_space { true } - else if c == ch_em_space { true } - else if c == ch_three_per_em_space { true } - else if c == ch_four_per_em_space { true } - else if c == ch_six_per_em_space { true } - else if c == ch_figure_space { true } - else if c == ch_punctuation_space { true } - else if c == ch_thin_space { true } - else if c == ch_hair_space { true } - else if c == ch_narrow_no_break_space { true } - else if c == ch_medium_mathematical_space { true } - else if c == ch_ideographic_space { true } - else if c == ch_line_tabulation { true } - else if c == ch_paragraph_separator { true } - else if c == ch_character_tabulation { true } - else if c == ch_line_feed { true } - else if c == ch_line_tabulation { true } - else if c == ch_form_feed { true } - else if c == ch_carriage_return { true } - else if c == ch_next_line { true } - else if c == ch_no_break_space { true } - else { false } -} \ No newline at end of file + if c == ch_space { + true + } else if c == ch_ogham_space_mark { + true + } else if c == ch_mongolian_vowel_sep { + true + } else if c == ch_en_quad { + true + } else if c == ch_em_quad { + true + } else if c == ch_en_space { + true + } else if c == ch_em_space { + true + } else if c == ch_three_per_em_space { + true + } else if c == ch_four_per_em_space { + true + } else if c == ch_six_per_em_space { + true + } else if c == ch_figure_space { + true + } else if c == ch_punctuation_space { + true + } else if c == ch_thin_space { + true + } else if c == ch_hair_space { + true + } else if c == ch_narrow_no_break_space { + true + } else if c == ch_medium_mathematical_space { + true + } else if c == ch_ideographic_space { + true + } else if c == ch_line_tabulation { + true + } else if c == ch_paragraph_separator { + true + } else if c == ch_character_tabulation { + true + } else if c == ch_line_feed { + true + } else if c == ch_line_tabulation { + true + } else if c == ch_form_feed { + true + } else if c == ch_carriage_return { + true + } else if c == ch_next_line { + true + } else if c == ch_no_break_space { true } else { false } +} diff --git a/src/lib/comm.rs b/src/lib/comm.rs index 615686a49e0..ebe9c75bc39 100644 --- a/src/lib/comm.rs +++ b/src/lib/comm.rs @@ -17,22 +17,21 @@ native "rust" mod rustrt { type void; type rust_port; - fn chan_id_send<~T>(target_task : task_id, target_port : port_id, - data : -T); + fn chan_id_send<~T>(target_task: task_id, target_port: port_id, data: -T); - fn new_port(unit_sz : uint) -> *rust_port; - fn del_port(po : *rust_port); - fn drop_port(po : *rust_port); - fn get_port_id(po : *rust_port) -> port_id; + fn new_port(unit_sz: uint) -> *rust_port; + fn del_port(po: *rust_port); + fn drop_port(po: *rust_port); + fn get_port_id(po: *rust_port) -> port_id; } native "rust-intrinsic" mod rusti { - fn recv<~T>(port : *rustrt::rust_port) -> T; + fn recv<~T>(port: *rustrt::rust_port) -> T; } type port_id = int; -type chan_handle<~T> = { task : task_id, port : port_id}; +type chan_handle<~T> = {task: task_id, port: port_id}; tag chan<~T> { chan_t(chan_handle<T>); } type _chan<~T> = chan<T>; @@ -44,22 +43,16 @@ resource port_ptr(po: *rustrt::rust_port) { tag port<~T> { port_t(@port_ptr); } -obj port_obj<~T>(raw_port : port<T>) { - fn mk_chan() -> chan<T> { - chan(raw_port) - } +obj port_obj<~T>(raw_port: port<T>) { + fn mk_chan() -> chan<T> { chan(raw_port) } - fn recv() -> T { - recv(raw_port) - } + fn recv() -> T { recv(raw_port) } } type _port<~T> = port_obj<T>; -fn mk_port<~T>() -> _port<T> { - ret port_obj::<T>(port::<T>()); -} +fn mk_port<~T>() -> _port<T> { ret port_obj::<T>(port::<T>()); } -fn send<~T>(ch : &chan<T>, data : -T) { +fn send<~T>(ch: &chan<T>, data: -T) { rustrt::chan_id_send(ch.task, ch.port, data); } @@ -67,13 +60,8 @@ fn port<~T>() -> port<T> { port_t(@port_ptr(rustrt::new_port(sys::size_of::<T>()))) } -fn recv<~T>(p : &port<T>) -> T { - ret rusti::recv(***p) -} +fn recv<~T>(p: &port<T>) -> T { ret rusti::recv(***p) } -fn chan<~T>(p : &port<T>) -> chan<T> { - chan_t({ - task: task::get_task_id(), - port: rustrt::get_port_id(***p) - }) +fn chan<~T>(p: &port<T>) -> chan<T> { + chan_t({task: task::get_task_id(), port: rustrt::get_port_id(***p)}) } diff --git a/src/lib/deque.rs b/src/lib/deque.rs index 19d74f14789..5b2d8db418b 100644 --- a/src/lib/deque.rs +++ b/src/lib/deque.rs @@ -6,14 +6,14 @@ */ type t<T> = obj { - fn size() -> uint ; - fn add_front(&T) ; - fn add_back(&T) ; - fn pop_front() -> T ; - fn pop_back() -> T ; - fn peek_front() -> T ; - fn peek_back() -> T ; - fn get(int) -> T ; + fn size() -> uint; + fn add_front(&T); + fn add_back(&T); + fn pop_front() -> T; + fn pop_back() -> T; + fn peek_front() -> T; + fn peek_back() -> T; + fn get(int) -> T; }; fn create<@T>() -> t<T> { @@ -26,24 +26,25 @@ fn create<@T>() -> t<T> { */ + fn grow<@T>(nelts: uint, lo: uint, elts: &[mutable cell<T>]) -> [mutable cell<T>] { assert (nelts == vec::len(elts)); - let rv = ~[mutable]; + let rv = [mutable]; let i = 0u; let nalloc = uint::next_power_of_two(nelts + 1u); while i < nalloc { if i < nelts { - rv += ~[mutable elts.((lo + i) % nelts)]; - } else { rv += ~[mutable option::none]; } + rv += [mutable elts[(lo + i) % nelts]]; + } else { rv += [mutable option::none]; } i += 1u; } ret rv; } fn get<@T>(elts: &[mutable cell<T>], i: uint) -> T { - ret alt elts.(i) { option::some(t) { t } _ { fail } }; + ret alt elts[i] { option::some(t) { t } _ { fail } }; } obj deque<@T>(mutable nelts: uint, mutable lo: uint, @@ -60,7 +61,7 @@ fn create<@T>() -> t<T> { lo = vec::len::<cell<T>>(elts) - 1u; hi = nelts; } - elts.(lo) = option::some::<T>(t); + elts[lo] = option::some::<T>(t); nelts += 1u; } fn add_back(t: &T) { @@ -69,7 +70,7 @@ fn create<@T>() -> t<T> { lo = 0u; hi = nelts; } - elts.(hi) = option::some::<T>(t); + elts[hi] = option::some::<T>(t); hi = (hi + 1u) % vec::len::<cell<T>>(elts); nelts += 1u; } @@ -80,7 +81,7 @@ fn create<@T>() -> t<T> { */ fn pop_front() -> T { let t: T = get::<T>(elts, lo); - elts.(lo) = option::none::<T>; + elts[lo] = option::none::<T>; lo = (lo + 1u) % vec::len::<cell<T>>(elts); nelts -= 1u; ret t; @@ -90,7 +91,7 @@ fn create<@T>() -> t<T> { hi = vec::len::<cell<T>>(elts) - 1u; } else { hi -= 1u; } let t: T = get::<T>(elts, hi); - elts.(hi) = option::none::<T>; + elts[hi] = option::none::<T>; nelts -= 1u; ret t; } diff --git a/src/lib/ebml.rs b/src/lib/ebml.rs index 31814b8d098..09d0bee1a60 100644 --- a/src/lib/ebml.rs +++ b/src/lib/ebml.rs @@ -18,23 +18,23 @@ type ebml_state = {ebml_tag: ebml_tag, tag_pos: uint, data_pos: uint}; type doc = {data: @[u8], start: uint, end: uint}; fn vint_at(data: &[u8], start: uint) -> {val: uint, next: uint} { - let a = data.(start); + let a = data[start]; if a & 0x80u8 != 0u8 { ret {val: a & 0x7fu8 as uint, next: start + 1u}; } if a & 0x40u8 != 0u8 { - ret {val: (a & 0x3fu8 as uint) << 8u | (data.(start + 1u) as uint), + ret {val: (a & 0x3fu8 as uint) << 8u | (data[start + 1u] as uint), next: start + 2u}; - } else if (a & 0x20u8 != 0u8) { + } else if a & 0x20u8 != 0u8 { ret {val: (a & 0x1fu8 as uint) << 16u | - (data.(start + 1u) as uint) << 8u | - (data.(start + 2u) as uint), + (data[start + 1u] as uint) << 8u | + (data[start + 2u] as uint), next: start + 3u}; - } else if (a & 0x10u8 != 0u8) { + } else if a & 0x10u8 != 0u8 { ret {val: (a & 0x0fu8 as uint) << 24u | - (data.(start + 1u) as uint) << 16u | - (data.(start + 2u) as uint) << 8u | - (data.(start + 3u) as uint), + (data[start + 1u] as uint) << 16u | + (data[start + 2u] as uint) << 8u | + (data[start + 3u] as uint), next: start + 4u}; } else { log_err "vint too big"; fail; } } @@ -105,7 +105,7 @@ fn be_uint_from_bytes(data: &@[u8], start: uint, size: uint) -> uint { let pos = start; while sz > 0u { sz -= 1u; - val += (data.(pos) as uint) << sz * 8u; + val += (data[pos] as uint) << sz * 8u; pos += 1u; } ret val; @@ -122,17 +122,17 @@ type writer = {writer: io::buf_writer, mutable size_positions: [uint]}; fn write_sized_vint(w: &io::buf_writer, n: uint, size: uint) { let buf: [u8]; alt size { - 1u { buf = ~[0x80u8 | (n as u8)]; } - 2u { buf = ~[0x40u8 | (n >> 8u as u8), n & 0xffu as u8]; } + 1u { buf = [0x80u8 | (n as u8)]; } + 2u { buf = [0x40u8 | (n >> 8u as u8), n & 0xffu as u8]; } 3u { buf = - ~[0x20u8 | (n >> 16u as u8), n >> 8u & 0xffu as u8, - n & 0xffu as u8]; + [0x20u8 | (n >> 16u as u8), n >> 8u & 0xffu as u8, + n & 0xffu as u8]; } 4u { buf = - ~[0x10u8 | (n >> 24u as u8), n >> 16u & 0xffu as u8, - n >> 8u & 0xffu as u8, n & 0xffu as u8]; + [0x10u8 | (n >> 24u as u8), n >> 16u & 0xffu as u8, + n >> 8u & 0xffu as u8, n & 0xffu as u8]; } _ { log_err "vint to write too big"; fail; } } @@ -149,7 +149,7 @@ fn write_vint(w: &io::buf_writer, n: uint) { } fn create_writer(w: &io::buf_writer) -> writer { - let size_positions: [uint] = ~[]; + let size_positions: [uint] = []; ret {writer: w, mutable size_positions: size_positions}; } @@ -161,8 +161,8 @@ fn start_tag(w: &writer, tag_id: uint) { write_vint(w.writer, tag_id); // Write a placeholder four-byte size. - w.size_positions += ~[w.writer.tell()]; - let zeroes: [u8] = ~[0u8, 0u8, 0u8, 0u8]; + w.size_positions += [w.writer.tell()]; + let zeroes: [u8] = [0u8, 0u8, 0u8, 0u8]; w.writer.write(zeroes); } diff --git a/src/lib/either.rs b/src/lib/either.rs index 95f1d9aca21..6f2a27a958d 100644 --- a/src/lib/either.rs +++ b/src/lib/either.rs @@ -5,32 +5,33 @@ import option::none; tag t<T, U> { left(T); right(U); } -fn either<T, U, V>(f_left: &block(&T) -> V, f_right: &block(&U) -> V, - value: &t<T, U>) -> V { +fn either<T, U, + V>(f_left: &block(&T) -> V, f_right: &block(&U) -> V, + value: &t<T, U>) -> V { alt value { left(l) { f_left(l) } right(r) { f_right(r) } } } fn lefts<T, U>(eithers: &[t<T, U>]) -> [T] { - let result: [T] = ~[]; + let result: [T] = []; for elt: t<T, U> in eithers { - alt elt { left(l) { result += ~[l] } _ {/* fallthrough */ } } + alt elt { left(l) { result += [l] } _ {/* fallthrough */ } } } ret result; } fn rights<T, U>(eithers: &[t<T, U>]) -> [U] { - let result: [U] = ~[]; + let result: [U] = []; for elt: t<T, U> in eithers { - alt elt { right(r) { result += ~[r] } _ {/* fallthrough */ } } + alt elt { right(r) { result += [r] } _ {/* fallthrough */ } } } ret result; } fn partition<T, U>(eithers: &[t<T, U>]) -> {lefts: [T], rights: [U]} { - let lefts: [T] = ~[]; - let rights: [U] = ~[]; + let lefts: [T] = []; + let rights: [U] = []; 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] } } } ret {lefts: lefts, rights: rights}; } diff --git a/src/lib/extfmt.rs b/src/lib/extfmt.rs index d7460a6b24e..7f7605a1d85 100644 --- a/src/lib/extfmt.rs +++ b/src/lib/extfmt.rs @@ -69,16 +69,16 @@ mod ct { // A fragment of the output sequence tag piece { piece_string(str); piece_conv(conv); } - type error_fn = fn(str) -> ! ; + type error_fn = fn(str) -> ! ; fn parse_fmt_string(s: str, error: error_fn) -> [piece] { - let pieces: [piece] = ~[]; + let pieces: [piece] = []; let lim = str::byte_len(s); let buf = ""; fn flush_buf(buf: str, pieces: &mutable [piece]) -> str { if str::byte_len(buf) > 0u { let piece = piece_string(buf); - pieces += ~[piece]; + pieces += [piece]; } ret ""; } @@ -96,7 +96,7 @@ mod ct { } else { buf = flush_buf(buf, pieces); let rs = parse_conversion(s, i, lim, error); - pieces += ~[rs.piece]; + pieces += [rs.piece]; i = rs.next; } } else { buf += curr; i += 1u; } @@ -107,7 +107,7 @@ mod ct { fn peek_num(s: str, i: uint, lim: uint) -> option::t<{num: uint, next: uint}> { if i >= lim { ret none; } - let c = s.(i); + let c = s[i]; if !('0' as u8 <= c && c <= '9' as u8) { ret option::none; } let n = c - ('0' as u8) as uint; ret alt peek_num(s, i + 1u, lim) { @@ -143,7 +143,7 @@ mod ct { some(t) { let n = t.num; let j = t.next; - if j < lim && s.(j) == '$' as u8 { + if j < lim && s[j] == '$' as u8 { {param: some(n as int), next: j + 1u} } else { {param: none, next: i} } } @@ -151,7 +151,7 @@ mod ct { } fn parse_flags(s: str, i: uint, lim: uint) -> {flags: [flag], next: uint} { - let noflags: [flag] = ~[]; + let noflags: [flag] = []; if i >= lim { ret {flags: noflags, next: i}; } // FIXME: This recursion generates illegal instructions if the return @@ -161,27 +161,27 @@ mod ct { let next = parse_flags(s, i + 1u, lim); let rest = next.flags; let j = next.next; - let curr: [flag] = ~[f]; + let curr: [flag] = [f]; ret @{flags: curr + rest, next: j}; } let more = bind more_(_, s, i, lim); - let f = s.(i); + let f = s[i]; ret if f == '-' as u8 { *more(flag_left_justify) - } else if (f == '0' as u8) { + } else if f == '0' as u8 { *more(flag_left_zero_pad) - } else if (f == ' ' as u8) { + } else if f == ' ' as u8 { *more(flag_space_for_sign) - } else if (f == '+' as u8) { + } else if f == '+' as u8 { *more(flag_sign_always) - } else if (f == '#' as u8) { + } else if f == '#' as u8 { *more(flag_alternate) } else { {flags: noflags, next: i} }; } fn parse_count(s: str, i: uint, lim: uint) -> {count: count, next: uint} { ret if i >= lim { {count: count_implied, next: i} - } else if (s.(i) == '*' as u8) { + } else if s[i] == '*' as u8 { let param = parse_parameter(s, i + 1u, lim); let j = param.next; alt param.param { @@ -202,7 +202,7 @@ mod ct { {count: count, next: uint} { ret if i >= lim { {count: count_implied, next: i} - } else if (s.(i) == '.' as u8) { + } else if s[i] == '.' as u8 { let count = parse_count(s, i + 1u, lim); @@ -223,21 +223,21 @@ mod ct { let t = if str::eq(tstr, "b") { ty_bool - } else if (str::eq(tstr, "s")) { + } else if str::eq(tstr, "s") { ty_str - } else if (str::eq(tstr, "c")) { + } else if str::eq(tstr, "c") { ty_char - } else if (str::eq(tstr, "d") || str::eq(tstr, "i")) { + } else if str::eq(tstr, "d") || str::eq(tstr, "i") { ty_int(signed) - } else if (str::eq(tstr, "u")) { + } else if str::eq(tstr, "u") { ty_int(unsigned) - } else if (str::eq(tstr, "x")) { + } else if str::eq(tstr, "x") { ty_hex(case_lower) - } else if (str::eq(tstr, "X")) { + } else if str::eq(tstr, "X") { ty_hex(case_upper) - } else if (str::eq(tstr, "t")) { + } else if str::eq(tstr, "t") { ty_bits - } else if (str::eq(tstr, "o")) { + } else if str::eq(tstr, "o") { ty_octal } else { error("unknown type in conversion: " + tstr) }; ret {ty: t, next: i + 1u}; @@ -277,7 +277,7 @@ mod rt { if 0 <= i { if have_flag(cv.flags, flag_sign_always) { s = "+" + s; - } else if (have_flag(cv.flags, flag_space_for_sign)) { + } else if have_flag(cv.flags, flag_space_for_sign) { s = " " + s; } } @@ -404,9 +404,9 @@ mod rt { // instead. if signed && zero_padding && str::byte_len(s) > 0u { - let head = s.(0); + let head = s[0]; if head == '+' as u8 || head == '-' as u8 || head == ' ' as u8 { - let headstr = str::unsafe_from_bytes(~[head]); + let headstr = str::unsafe_from_bytes([head]); let bytelen = str::byte_len(s); let numpart = str::substr(s, 1u, bytelen - 1u); ret headstr + padstr + numpart; diff --git a/src/lib/fs.rs b/src/lib/fs.rs index e3eb47802b6..c49d77815a3 100644 --- a/src/lib/fs.rs +++ b/src/lib/fs.rs @@ -35,7 +35,7 @@ fn basename(p: path) -> path { // FIXME: Need some typestate to avoid bounds check when len(pre) == 0 fn connect(pre: path, post: path) -> path { let len = str::byte_len(pre); - ret if pre.(len - 1u) == os_fs::path_sep as u8 { + ret if pre[len - 1u] == os_fs::path_sep as u8 { // Trailing '/'? pre + post @@ -46,11 +46,11 @@ fn file_is_dir(p: path) -> bool { ret rustrt::rust_file_is_dir(p) != 0; } fn list_dir(p: path) -> [str] { let pl = str::byte_len(p); - if pl == 0u || p.(pl - 1u) as char != os_fs::path_sep { p += path_sep(); } - let full_paths: [str] = ~[]; + if pl == 0u || p[pl - 1u] as char != os_fs::path_sep { p += path_sep(); } + let full_paths: [str] = []; for filename: str in os_fs::list_dir(p) { if !str::eq(filename, ".") { - if !str::eq(filename, "..") { full_paths += ~[p + filename]; } + if !str::eq(filename, "..") { full_paths += [p + filename]; } } } ret full_paths; diff --git a/src/lib/generic_os.rs b/src/lib/generic_os.rs index acb9a772048..80bc7747b47 100644 --- a/src/lib/generic_os.rs +++ b/src/lib/generic_os.rs @@ -28,7 +28,7 @@ fn getenv(n: str) -> option::t<str> { let res = os::kernel32::GetEnvironmentVariableA(nbuf, vbuf, nsize); if res == 0u { ret option::none; - } else if (res < nsize) { + } else if res < nsize { ret option::some(str::str_from_cstr(vbuf)); } else { nsize = res; } } diff --git a/src/lib/getopts.rs b/src/lib/getopts.rs index 22628f8aa6e..95747e3c453 100644 --- a/src/lib/getopts.rs +++ b/src/lib/getopts.rs @@ -68,7 +68,7 @@ tag optval { val(str); given; } type match = {opts: [opt], vals: [mutable [optval]], free: [str]}; fn is_arg(arg: str) -> bool { - ret str::byte_len(arg) > 1u && arg.(0) == '-' as u8; + ret str::byte_len(arg) > 1u && arg[0] == '-' as u8; } fn name_str(nm: name) -> str { @@ -78,7 +78,7 @@ fn name_str(nm: name) -> str { fn find_opt(opts: &[opt], nm: name) -> option::t<uint> { let i = 0u; let l = vec::len::<opt>(opts); - while i < l { if opts.(i).name == nm { ret some::<uint>(i); } i += 1u; } + while i < l { if opts[i].name == nm { ret some::<uint>(i); } i += 1u; } ret none::<uint>; } @@ -108,30 +108,30 @@ tag result { success(match); failure(fail_); } fn getopts(args: &[str], opts: &[opt]) -> result { let n_opts = vec::len::<opt>(opts); - fn f(_x: uint) -> [optval] { ret ~[]; } + fn f(_x: uint) -> [optval] { ret []; } let vals = vec::init_fn_mut::<[optval]>(f, n_opts); - let free: [str] = ~[]; + let free: [str] = []; let l = vec::len::<str>(args); let i = 0u; while i < l { - let cur = args.(i); + let cur = args[i]; let curlen = str::byte_len(cur); if !is_arg(cur) { - free += ~[cur]; - } else if (str::eq(cur, "--")) { + free += [cur]; + } else if str::eq(cur, "--") { let j = i + 1u; - while j < l { free += ~[args.(j)]; j += 1u; } + while j < l { free += [args[j]]; j += 1u; } break; } else { let names; let i_arg = option::none::<str>; - if cur.(1) == '-' as u8 { + if cur[1] == '-' as u8 { let tail = str::slice(cur, 2u, curlen); let eq = str::index(tail, '=' as u8); if eq == -1 { - names = ~[long(tail)]; + names = [long(tail)]; } else { - names = ~[long(str::slice(tail, 0u, eq as uint))]; + names = [long(str::slice(tail, 0u, eq as uint))]; i_arg = option::some::<str>(str::slice(tail, (eq as uint) + 1u, @@ -139,10 +139,10 @@ fn getopts(args: &[str], opts: &[opt]) -> result { } } else { let j = 1u; - names = ~[]; + names = []; while j < curlen { let range = str::char_range_at(cur, j); - names += ~[short(range.ch)]; + names += [short(range.ch)]; j = range.next; } } @@ -154,27 +154,27 @@ fn getopts(args: &[str], opts: &[opt]) -> result { some(id) { optid = id; } none. { ret failure(unrecognized_option(name_str(nm))); } } - alt opts.(optid).hasarg { + alt opts[optid].hasarg { no. { if !option::is_none::<str>(i_arg) { ret failure(unexpected_argument(name_str(nm))); } - vals.(optid) += ~[given]; + vals[optid] += [given]; } maybe. { if !option::is_none::<str>(i_arg) { - vals.(optid) += ~[val(option::get(i_arg))]; - } else if (name_pos < vec::len::<name>(names) || - i + 1u == l || is_arg(args.(i + 1u))) { - vals.(optid) += ~[given]; - } else { i += 1u; vals.(optid) += ~[val(args.(i))]; } + vals[optid] += [val(option::get(i_arg))]; + } else if name_pos < vec::len::<name>(names) || + i + 1u == l || is_arg(args[i + 1u]) { + vals[optid] += [given]; + } else { i += 1u; vals[optid] += [val(args[i])]; } } yes. { if !option::is_none::<str>(i_arg) { - vals.(optid) += ~[val(option::get::<str>(i_arg))]; - } else if (i + 1u == l) { + vals[optid] += [val(option::get::<str>(i_arg))]; + } else if i + 1u == l { ret failure(argument_missing(name_str(nm))); - } else { i += 1u; vals.(optid) += ~[val(args.(i))]; } + } else { i += 1u; vals[optid] += [val(args[i])]; } } } } @@ -183,16 +183,16 @@ fn getopts(args: &[str], opts: &[opt]) -> result { } i = 0u; while i < n_opts { - let n = vec::len::<optval>(vals.(i)); - let occ = opts.(i).occur; + let n = vec::len::<optval>(vals[i]); + let occ = opts[i].occur; if occ == req { if n == 0u { - ret failure(option_missing(name_str(opts.(i).name))); + ret failure(option_missing(name_str(opts[i].name))); } } if occ != multi { if n > 1u { - ret failure(option_duplicated(name_str(opts.(i).name))); + ret failure(option_duplicated(name_str(opts[i].name))); } } i += 1u; @@ -202,12 +202,12 @@ fn getopts(args: &[str], opts: &[opt]) -> result { fn opt_vals(m: &match, nm: str) -> [optval] { ret alt find_opt(m.opts, mkname(nm)) { - some(id) { m.vals.(id) } + some(id) { m.vals[id] } none. { log_err "No option '" + nm + "' defined."; fail } }; } -fn opt_val(m: &match, nm: str) -> optval { ret opt_vals(m, nm).(0); } +fn opt_val(m: &match, nm: str) -> optval { ret opt_vals(m, nm)[0]; } fn opt_present(m: &match, nm: str) -> bool { ret vec::len::<optval>(opt_vals(m, nm)) > 0u; @@ -218,9 +218,9 @@ fn opt_str(m: &match, nm: str) -> str { } fn opt_strs(m: &match, nm: str) -> [str] { - let acc: [str] = ~[]; + let acc: [str] = []; for v: optval in opt_vals(m, nm) { - alt v { val(s) { acc += ~[s]; } _ { } } + alt v { val(s) { acc += [s]; } _ { } } } ret acc; } @@ -228,7 +228,7 @@ fn opt_strs(m: &match, nm: str) -> [str] { fn opt_maybe_str(m: &match, nm: str) -> option::t<str> { let vals = opt_vals(m, nm); if vec::len::<optval>(vals) == 0u { ret none::<str>; } - ret alt vals.(0) { val(s) { some::<str>(s) } _ { none::<str> } }; + ret alt vals[0] { val(s) { some::<str>(s) } _ { none::<str> } }; } @@ -238,7 +238,7 @@ fn opt_maybe_str(m: &match, nm: str) -> option::t<str> { fn opt_default(m: &match, nm: str, def: str) -> option::t<str> { let vals = opt_vals(m, nm); if vec::len::<optval>(vals) == 0u { ret none::<str>; } - ret alt vals.(0) { val(s) { some::<str>(s) } _ { some::<str>(def) } } + ret alt vals[0] { val(s) { some::<str>(s) } _ { some::<str>(def) } } } // Local Variables: // mode: rust; diff --git a/src/lib/int.rs b/src/lib/int.rs index 52a1cb1cd66..fd4cd09f279 100644 --- a/src/lib/int.rs +++ b/src/lib/int.rs @@ -52,7 +52,7 @@ fn str(i: int) -> str { ret to_str(i, 10u); } fn pow(base: int, exponent: uint) -> int { ret if exponent == 0u { 1 - } else if (base == 0) { + } else if base == 0 { 0 } else { let accum = base; diff --git a/src/lib/io.rs b/src/lib/io.rs index 10a59334f62..454cfd1041f 100644 --- a/src/lib/io.rs +++ b/src/lib/io.rs @@ -15,53 +15,49 @@ tag seek_style { seek_set; seek_end; seek_cur; } // The raw underlying reader class. All readers must implement this. type buf_reader = - // FIXME: Seekable really should be orthogonal. We will need // inheritance. obj { - fn read(uint) -> [u8] ; - fn read_byte() -> int ; - fn unread_byte(int) ; - fn eof() -> bool ; - fn seek(int, seek_style) ; - fn tell() -> uint ; + fn read(uint) -> [u8]; + fn read_byte() -> int; + fn unread_byte(int); + fn eof() -> bool; + fn seek(int, seek_style); + fn tell() -> uint; }; // Convenience methods for reading. type reader = - // FIXME: This should inherit from buf_reader. - // FIXME: eventually u64 + // FIXME: eventually u64 obj { - fn get_buf_reader() -> buf_reader ; - fn read_byte() -> int ; - fn unread_byte(int) ; - fn read_bytes(uint) -> [u8] ; - fn read_char() -> char ; - fn eof() -> bool ; - fn read_line() -> str ; - fn read_c_str() -> str ; - fn read_le_uint(uint) -> uint ; - fn read_le_int(uint) -> int ; - fn read_be_uint(uint) -> uint ; - fn read_whole_stream() -> [u8] ; - fn seek(int, seek_style) ; - fn tell() -> uint ; + fn get_buf_reader() -> buf_reader; + fn read_byte() -> int; + fn unread_byte(int); + fn read_bytes(uint) -> [u8]; + fn read_char() -> char; + fn eof() -> bool; + fn read_line() -> str; + fn read_c_str() -> str; + fn read_le_uint(uint) -> uint; + fn read_le_int(uint) -> int; + fn read_be_uint(uint) -> uint; + fn read_whole_stream() -> [u8]; + fn seek(int, seek_style); + fn tell() -> uint; }; fn convert_whence(whence: seek_style) -> int { ret alt whence { seek_set. { 0 } seek_cur. { 1 } seek_end. { 2 } }; } -resource FILE_res(f: os::libc::FILE) { - os::libc::fclose(f); -} +resource FILE_res(f: os::libc::FILE) { os::libc::fclose(f); } obj FILE_buf_reader(f: os::libc::FILE, res: option::t<@FILE_res>) { fn read(len: uint) -> [u8] { - let buf = ~[]; + let buf = []; vec::reserve::<u8>(buf, len); let read = os::libc::fread(vec::to_ptr::<u8>(buf), 1u, len, f); vec::unsafe::set_len::<u8>(buf, read); @@ -73,9 +69,7 @@ obj FILE_buf_reader(f: os::libc::FILE, res: option::t<@FILE_res>) { fn seek(offset: int, whence: seek_style) { assert (os::libc::fseek(f, offset, convert_whence(whence)) == 0); } - fn tell() -> uint { - ret os::libc::ftell(f) as uint; - } + fn tell() -> uint { ret os::libc::ftell(f) as uint; } } @@ -111,7 +105,7 @@ obj new_reader(rdr: buf_reader) { } fn eof() -> bool { ret rdr.eof(); } fn read_line() -> str { - let buf: [u8] = ~[]; + let buf: [u8] = []; // No break yet in rustc let go_on = true; @@ -119,16 +113,16 @@ obj new_reader(rdr: buf_reader) { let ch = rdr.read_byte(); if ch == -1 || ch == 10 { go_on = false; - } else { buf += ~[ch as u8]; } + } else { buf += [ch as u8]; } } ret str::unsafe_from_bytes(buf); } fn read_c_str() -> str { - let buf: [u8] = ~[]; + let buf: [u8] = []; let go_on = true; while go_on { let ch = rdr.read_byte(); - if ch < 1 { go_on = false; } else { buf += ~[ch as u8]; } + if ch < 1 { go_on = false; } else { buf += [ch as u8]; } } ret str::unsafe_from_bytes(buf); } @@ -166,7 +160,7 @@ obj new_reader(rdr: buf_reader) { ret val; } fn read_whole_stream() -> [u8] { - let buf: [u8] = ~[]; + let buf: [u8] = []; while !rdr.eof() { buf += rdr.read(2048u); } ret buf; } @@ -205,7 +199,7 @@ obj byte_buf_reader(bbuf: byte_buf) { } fn read_byte() -> int { if bbuf.pos == vec::len::<u8>(bbuf.buf) { ret -1; } - let b = bbuf.buf.(bbuf.pos); + let b = bbuf.buf[bbuf.pos]; bbuf.pos += 1u; ret b as int; } @@ -232,15 +226,14 @@ fn string_reader(s: &str) -> reader { tag fileflag { append; create; truncate; none; } type buf_writer = - // FIXME: Seekable really should be orthogonal. We will need // inheritance. - // FIXME: eventually u64 + // FIXME: eventually u64 obj { - fn write(&[u8]) ; - fn seek(int, seek_style) ; - fn tell() -> uint ; + fn write(&[u8]); + fn seek(int, seek_style); + fn tell() -> uint; }; obj FILE_writer(f: os::libc::FILE, res: option::t<@FILE_res>) { @@ -253,14 +246,10 @@ obj FILE_writer(f: os::libc::FILE, res: option::t<@FILE_res>) { fn seek(offset: int, whence: seek_style) { assert (os::libc::fseek(f, offset, convert_whence(whence)) == 0); } - fn tell() -> uint { - ret os::libc::ftell(f) as uint; - } + fn tell() -> uint { ret os::libc::ftell(f) as uint; } } -resource fd_res(fd: int) { - os::libc::close(fd); -} +resource fd_res(fd: int) { os::libc::close(fd); } obj fd_buf_writer(fd: int, res: option::t<@fd_res>) { fn write(v: &[u8]) { @@ -312,32 +301,31 @@ fn file_buf_writer(path: str, flags: &[fileflag]) -> buf_writer { } type writer = - // write_str will continue to do utf-8 output only. an alternative // function will be provided for general encoded string output obj { - fn get_buf_writer() -> buf_writer ; - fn write_str(str) ; - fn write_line(str) ; - fn write_char(char) ; - fn write_int(int) ; - fn write_uint(uint) ; - fn write_bytes(&[u8]) ; - fn write_le_uint(uint, uint) ; - fn write_le_int(int, uint) ; - fn write_be_uint(uint, uint) ; + fn get_buf_writer() -> buf_writer; + fn write_str(str); + fn write_line(str); + fn write_char(char); + fn write_int(int); + fn write_uint(uint); + fn write_bytes(&[u8]); + fn write_le_uint(uint, uint); + fn write_le_int(int, uint); + fn write_be_uint(uint, uint); }; fn uint_to_le_bytes(n: uint, size: uint) -> [u8] { - let bytes: [u8] = ~[]; - while size > 0u { bytes += ~[n & 255u as u8]; n >>= 8u; size -= 1u; } + let bytes: [u8] = []; + while size > 0u { bytes += [n & 255u as u8]; n >>= 8u; size -= 1u; } ret bytes; } fn uint_to_be_bytes(n: uint, size: uint) -> [u8] { - let bytes: [u8] = ~[]; + let bytes: [u8] = []; let i = size - 1u as int; - while i >= 0 { bytes += ~[n >> (i * 8 as uint) & 255u as u8]; i -= 1; } + while i >= 0 { bytes += [n >> (i * 8 as uint) & 255u as u8]; i -= 1; } ret bytes; } @@ -354,9 +342,7 @@ obj new_writer(out: buf_writer) { out.write(str::bytes(str::from_char(ch))); } fn write_int(n: int) { out.write(str::bytes(int::to_str(n, 10u))); } - fn write_uint(n: uint) { - out.write(str::bytes(uint::to_str(n, 10u))); - } + fn write_uint(n: uint) { out.write(str::bytes(uint::to_str(n, 10u))); } fn write_bytes(bytes: &[u8]) { out.write(bytes); } fn write_le_uint(n: uint, size: uint) { out.write(uint_to_le_bytes(n, size)); @@ -391,8 +377,8 @@ fn stdout() -> writer { ret new_writer(fd_buf_writer(1, option::none)); } type str_writer = obj { - fn get_writer() -> writer ; - fn get_str() -> str ; + fn get_writer() -> writer; + fn get_str() -> str; }; type mutable_byte_buf = @{mutable buf: [mutable u8], mutable pos: uint}; @@ -402,7 +388,7 @@ obj byte_buf_writer(buf: mutable_byte_buf) { // Fast path. if buf.pos == vec::len(buf.buf) { - for b: u8 in v { buf.buf += ~[mutable b]; } + for b: u8 in v { buf.buf += [mutable b]; } buf.pos += vec::len::<u8>(v); ret; } @@ -411,10 +397,10 @@ obj byte_buf_writer(buf: mutable_byte_buf) { let vlen = vec::len::<u8>(v); let vpos = 0u; while vpos < vlen { - let b = v.(vpos); + let b = v[vpos]; if buf.pos == vec::len(buf.buf) { - buf.buf += ~[mutable b]; - } else { buf.buf.(buf.pos) = b; } + buf.buf += [mutable b]; + } else { buf.buf[buf.pos] = b; } buf.pos += 1u; vpos += 1u; } @@ -430,7 +416,7 @@ obj byte_buf_writer(buf: mutable_byte_buf) { fn string_writer() -> str_writer { // FIXME: yikes, this is bad. Needs fixing of mutable syntax. - let b: [mutable u8] = ~[mutable 0u8]; + let b: [mutable u8] = [mutable 0u8]; vec::pop(b); let buf: mutable_byte_buf = @{mutable buf: b, mutable pos: 0u}; obj str_writer_wrap(wr: writer, buf: mutable_byte_buf) { @@ -451,7 +437,7 @@ fn seek_in_buf(offset: int, pos: uint, len: uint, whence: seek_style) -> seek_cur. { bpos += offset; } seek_end. { bpos = blen + offset; } } - if bpos < 0 { bpos = 0; } else if (bpos > blen) { bpos = blen; } + if bpos < 0 { bpos = 0; } else if bpos > blen { bpos = blen; } ret bpos as uint; } @@ -460,6 +446,7 @@ fn read_whole_file_str(file: &str) -> str { } fn read_whole_file(file: &str) -> [u8] { + // FIXME: There's a lot of copying here file_reader(file).read_whole_stream() } diff --git a/src/lib/list.rs b/src/lib/list.rs index ee25f7cd3bb..4f3cc5b2238 100644 --- a/src/lib/list.rs +++ b/src/lib/list.rs @@ -13,7 +13,7 @@ fn from_vec<@T>(v: &[T]) -> list<T> { ret l; } -fn foldl<@T, @U>(ls_: &list<T>, u: &U, f: &block(&T, &U) -> U ) -> U { +fn foldl<@T, @U>(ls_: &list<T>, u: &U, f: &block(&T, &U) -> U) -> U { let accum: U = u; let ls = ls_; while true { @@ -25,8 +25,8 @@ fn foldl<@T, @U>(ls_: &list<T>, u: &U, f: &block(&T, &U) -> U ) -> U { ret accum; } -fn find<@T, @U>(ls_: &list<T>, f: &block(&T) -> option::t<U>) - -> option::t<U> { +fn find<@T, @U>(ls_: &list<T>, f: &block(&T) -> option::t<U>) -> + option::t<U> { let ls = ls_; while true { alt ls { @@ -56,26 +56,17 @@ fn length<@T>(ls: &list<T>) -> uint { } fn cdr<@T>(ls: &list<T>) -> list<T> { - alt ls { - cons(_, tl) { ret *tl; } - nil. { fail "list empty" } - } + alt ls { cons(_, tl) { ret *tl; } nil. { fail "list empty" } } } fn car<@T>(ls: &list<T>) -> T { - alt ls { - cons(hd, _) { ret hd; } - nil. { fail "list empty" } - } + alt ls { cons(hd, _) { ret hd; } nil. { fail "list empty" } } } fn append<@T>(l: &list<T>, m: &list<T>) -> list<T> { alt l { nil. { ret m; } - cons(x, xs) { - let rest = append(*xs, m); - ret cons(x, @rest); - } + cons(x, xs) { let rest = append(*xs, m); ret cons(x, @rest); } } } diff --git a/src/lib/map.rs b/src/lib/map.rs index 4590fd8c63d..d54eae03d10 100644 --- a/src/lib/map.rs +++ b/src/lib/map.rs @@ -1,21 +1,21 @@ /** * Hashmap implementation. */ -type hashfn<K> = fn(&K) -> uint ; +type hashfn<K> = fn(&K) -> uint; -type eqfn<K> = fn(&K, &K) -> bool ; +type eqfn<K> = fn(&K, &K) -> bool; type hashmap<K, V> = obj { - fn size() -> uint ; - fn insert(&K, &V) -> bool ; - fn contains_key(&K) -> bool ; - fn get(&K) -> V ; - fn find(&K) -> option::t<V> ; - fn remove(&K) -> option::t<V> ; - fn rehash() ; - iter items() -> @{key: K, val: V} ; - iter keys() -> K ; + fn size() -> uint; + fn insert(&K, &V) -> bool; + fn contains_key(&K) -> bool; + fn get(&K) -> V; + fn find(&K) -> option::t<V>; + fn remove(&K) -> option::t<V>; + fn rehash(); + iter items() -> @{key: K, val: V}; + iter keys() -> K; }; type hashset<K> = hashmap<K, ()>; @@ -26,7 +26,7 @@ fn mk_hashmap<@K, @V>(hasher: &hashfn<K>, eqer: &eqfn<K>) -> hashmap<K, V> { let load_factor: util::rational = {num: 3, den: 4}; tag bucket<@K, @V> { nil; deleted; some(K, V); } - fn make_buckets<@K, @V>(nbkts: uint) -> [mutable (bucket<K, V>)] { + fn make_buckets<@K, @V>(nbkts: uint) -> [mutable bucket<K, V>] { ret vec::init_elt_mut::<bucket<K, V>>(nil::<K, V>, nbkts); } // Derive two hash functions from the one given by taking the upper @@ -53,37 +53,36 @@ fn mk_hashmap<@K, @V>(hasher: &hashfn<K>, eqer: &eqfn<K>) -> hashmap<K, V> { * will fail. */ - fn insert_common<@K, @V>(hasher: &hashfn<K>, eqer: &eqfn<K>, - bkts: &[mutable bucket<K, V>], nbkts: uint, - key: &K, val: &V) -> bool { + fn insert_common<@K, + @V>(hasher: &hashfn<K>, eqer: &eqfn<K>, + bkts: &[mutable bucket<K, V>], nbkts: uint, key: &K, + val: &V) -> bool { let i: uint = 0u; let h: uint = hasher(key); while i < nbkts { let j: uint = hash(h, nbkts, i); - alt bkts.(j) { + alt bkts[j] { some(k, _) { // Copy key to please alias analysis. let k_ = k; - if eqer(key, k_) { - bkts.(j) = some(k_, val); - ret false; - } + if eqer(key, k_) { bkts[j] = some(k_, val); ret false; } i += 1u; } - _ { bkts.(j) = some(key, val); ret true; } + _ { bkts[j] = some(key, val); ret true; } } } fail; // full table } - fn find_common<@K, @V>(hasher: &hashfn<K>, eqer: &eqfn<K>, - bkts: &[mutable bucket<K, V>], nbkts: uint, - key: &K) -> option::t<V> { + fn find_common<@K, + @V>(hasher: &hashfn<K>, eqer: &eqfn<K>, + bkts: &[mutable bucket<K, V>], nbkts: uint, key: &K) -> + option::t<V> { let i: uint = 0u; let h: uint = hasher(key); while i < nbkts { let j: uint = hash(h, nbkts, i); - alt bkts.(j) { + alt bkts[j] { some(k, v) { // Copy to please alias analysis. let k_ = k; @@ -97,9 +96,10 @@ fn mk_hashmap<@K, @V>(hasher: &hashfn<K>, eqer: &eqfn<K>) -> hashmap<K, V> { } ret option::none; } - fn rehash<@K, @V>(hasher: &hashfn<K>, eqer: &eqfn<K>, - oldbkts: &[mutable bucket<K, V>], _noldbkts: uint, - newbkts: &[mutable bucket<K, V>], nnewbkts: uint) { + fn rehash<@K, + @V>(hasher: &hashfn<K>, eqer: &eqfn<K>, + oldbkts: &[mutable bucket<K, V>], _noldbkts: uint, + newbkts: &[mutable bucket<K, V>], nnewbkts: uint) { for b: bucket<K, V> in oldbkts { alt b { some(k_, v_) { @@ -111,12 +111,13 @@ fn mk_hashmap<@K, @V>(hasher: &hashfn<K>, eqer: &eqfn<K>) -> hashmap<K, V> { } } } - obj hashmap<@K, @V>(hasher: hashfn<K>, - eqer: eqfn<K>, - mutable bkts: [mutable bucket<K, V>], - mutable nbkts: uint, - mutable nelts: uint, - lf: util::rational) { + obj hashmap<@K, + @V>(hasher: hashfn<K>, + eqer: eqfn<K>, + mutable bkts: [mutable bucket<K, V>], + mutable nbkts: uint, + mutable nelts: uint, + lf: util::rational) { fn size() -> uint { ret nelts; } fn insert(key: &K, val: &V) -> bool { let load: util::rational = @@ -154,12 +155,12 @@ fn mk_hashmap<@K, @V>(hasher: &hashfn<K>, eqer: &eqfn<K>) -> hashmap<K, V> { let h: uint = hasher(key); while i < nbkts { let j: uint = hash(h, nbkts, i); - alt bkts.(j) { + alt bkts[j] { some(k, v) { let k_ = k; let vo = option::some(v); if eqer(key, k_) { - bkts.(j) = deleted; + bkts[j] = deleted; nelts -= 1u; ret vo; } diff --git a/src/lib/net.rs b/src/lib/net.rs index 8b8b8113d8d..83923d6b987 100644 --- a/src/lib/net.rs +++ b/src/lib/net.rs @@ -2,29 +2,20 @@ import str; import vec; import uint; -tag ip_addr { - ipv4(u8, u8, u8, u8); -} +tag ip_addr { ipv4(u8, u8, u8, u8); } -fn format_addr(ip : ip_addr) -> str { - alt(ip) { +fn format_addr(ip: ip_addr) -> str { + alt ip { ipv4(a, b, c, d) { - #fmt("%u.%u.%u.%u", - a as uint, - b as uint, - c as uint, - d as uint) + #fmt["%u.%u.%u.%u", a as uint, b as uint, c as uint, d as uint] } _ { fail "Unsupported address type"; } } } -fn parse_addr(ip : str) -> ip_addr { - let parts = vec::map(uint::from_str, str::split(ip, ".".(0))); +fn parse_addr(ip: str) -> ip_addr { + let parts = vec::map(uint::from_str, str::split(ip, "."[0])); if vec::len(parts) != 4u { fail "Too many dots in IP address"; } for i in parts { if i > 255u { fail "Invalid IP Address part."; } } - ipv4(parts.(0) as u8, - parts.(1) as u8, - parts.(2) as u8, - parts.(3) as u8) + ipv4(parts[0] as u8, parts[1] as u8, parts[2] as u8, parts[3] as u8) } diff --git a/src/lib/option.rs b/src/lib/option.rs index e8656b0b77d..ea14ef2b867 100644 --- a/src/lib/option.rs +++ b/src/lib/option.rs @@ -3,10 +3,7 @@ tag t<@T> { none; some(T); } fn get<@T>(opt: &t<T>) -> T { - alt opt { - some(x) { x } - none. { fail "option none" } - } + alt opt { some(x) { x } none. { fail "option none" } } } fn map<@T, @U>(f: &block(&T) -> U, opt: &t<T>) -> t<U> { diff --git a/src/lib/rand.rs b/src/lib/rand.rs index 7b47311b04b..fee8bbe06ba 100644 --- a/src/lib/rand.rs +++ b/src/lib/rand.rs @@ -11,17 +11,16 @@ native "rust" mod rustrt { fn rand_free(c: rctx); } -type rng = obj { fn next() -> u32; }; +type rng = + obj { + fn next() -> u32; + }; -resource rand_res(c: rustrt::rctx) { - rustrt::rand_free(c); -} +resource rand_res(c: rustrt::rctx) { rustrt::rand_free(c); } fn mk_rng() -> rng { obj rt_rng(c: @rand_res) { - fn next() -> u32 { - ret rustrt::rand_next(**c); - } + fn next() -> u32 { ret rustrt::rand_next(**c); } } ret rt_rng(@rand_res(rustrt::rand_new())); } diff --git a/src/lib/run_program.rs b/src/lib/run_program.rs index 5c5f110816b..fb593bca58a 100644 --- a/src/lib/run_program.rs +++ b/src/lib/run_program.rs @@ -13,9 +13,9 @@ native "rust" mod rustrt { } fn arg_vec(prog: str, args: &[str]) -> [sbuf] { - let argptrs = ~[str::buf(prog)]; - for arg: str in args { argptrs += ~[str::buf(arg)]; } - argptrs += ~[0 as sbuf]; + let argptrs = [str::buf(prog)]; + for arg: str in args { argptrs += [str::buf(arg)]; } + argptrs += [0 as sbuf]; ret argptrs; } @@ -24,8 +24,8 @@ fn spawn_process(prog: str, args: &[str], in_fd: int, out_fd: int, // Note: we have to hold on to this vector reference while we hold a // pointer to its buffer let argv = arg_vec(prog, args); - let pid = rustrt::rust_run_program( - vec::to_ptr(argv), in_fd, out_fd, err_fd); + let pid = + rustrt::rust_run_program(vec::to_ptr(argv), in_fd, out_fd, err_fd); ret pid; } @@ -44,16 +44,15 @@ type program = fn destroy(); }; -resource program_res(p: program) { - p.destroy(); -} +resource program_res(p: program) { p.destroy(); } fn start_program(prog: str, args: &[str]) -> @program_res { let pipe_input = os::pipe(); let pipe_output = os::pipe(); let pipe_err = os::pipe(); - let pid = spawn_process(prog, args, pipe_input.in, pipe_output.out, - pipe_err.out); + let pid = + spawn_process(prog, args, pipe_input.in, pipe_output.out, + pipe_err.out); if pid == -1 { fail; } os::libc::close(pipe_input.in); @@ -66,16 +65,13 @@ fn start_program(prog: str, args: &[str]) -> @program_res { mutable finished: bool) { fn get_id() -> int { ret pid; } fn input() -> io::writer { - ret io::new_writer( - io::fd_buf_writer(in_fd, option::none)); + ret io::new_writer(io::fd_buf_writer(in_fd, option::none)); } fn output() -> io::reader { - ret io::new_reader( - io::FILE_buf_reader(out_file, option::none)); + ret io::new_reader(io::FILE_buf_reader(out_file, option::none)); } fn err() -> io::reader { - ret io::new_reader( - io::FILE_buf_reader(err_file, option::none)); + ret io::new_reader(io::FILE_buf_reader(err_file, option::none)); } fn close_input() { let invalid_fd = -1; @@ -96,11 +92,9 @@ fn start_program(prog: str, args: &[str]) -> @program_res { os::libc::fclose(err_file); } } - ret @program_res(new_program(pid, - pipe_input.out, + ret @program_res(new_program(pid, pipe_input.out, os::fd_FILE(pipe_output.in), - os::fd_FILE(pipe_err.in), - false)); + os::fd_FILE(pipe_err.in), false)); } fn read_all(rd: &io::reader) -> str { @@ -112,8 +106,8 @@ fn read_all(rd: &io::reader) -> str { ret buf; } -fn program_output(prog: str, args: [str]) - -> {status: int, out: str, err: str} { +fn program_output(prog: str, args: [str]) -> + {status: int, out: str, err: str} { let pr = start_program(prog, args); pr.close_input(); ret {status: pr.finish(), diff --git a/src/lib/sha1.rs b/src/lib/sha1.rs index 99c0f1276d1..db6fad15784 100644 --- a/src/lib/sha1.rs +++ b/src/lib/sha1.rs @@ -9,7 +9,6 @@ export sha1; export mk_sha1; type sha1 = - // Provide message input as bytes @@ -25,11 +24,11 @@ type sha1 = // Reset the sha1 state for reuse. This is called // automatically during construction obj { - fn input(&[u8]) ; - fn input_str(&str) ; - fn result() -> [u8] ; - fn result_str() -> str ; - fn reset() ; + fn input(&[u8]); + fn input_str(&str); + fn result() -> [u8]; + fn result_str() -> str; + fn reset(); }; @@ -65,7 +64,7 @@ fn mk_sha1() -> sha1 { assert (!st.computed); for element: u8 in msg { - st.msg_block.(st.msg_block_idx) = element; + st.msg_block[st.msg_block_idx] = element; st.msg_block_idx += 1u; st.len_low += 8u32; if st.len_low == 0u32 { @@ -92,30 +91,29 @@ fn mk_sha1() -> sha1 { t = 0; while t < 16 { let tmp; - tmp = (st.msg_block.(t * 4) as u32) << 24u32; - tmp = tmp | (st.msg_block.(t * 4 + 1) as u32) << 16u32; - tmp = tmp | (st.msg_block.(t * 4 + 2) as u32) << 8u32; - tmp = tmp | (st.msg_block.(t * 4 + 3) as u32); - w.(t) = tmp; + tmp = (st.msg_block[t * 4] as u32) << 24u32; + tmp = tmp | (st.msg_block[t * 4 + 1] as u32) << 16u32; + tmp = tmp | (st.msg_block[t * 4 + 2] as u32) << 8u32; + tmp = tmp | (st.msg_block[t * 4 + 3] as u32); + w[t] = tmp; t += 1; } // Initialize the rest of vector w while t < 80 { - let val = w.(t - 3) ^ w.(t - 8) ^ w.(t - 14) ^ w.(t - 16); - w.(t) = circular_shift(1u32, val); + let val = w[t - 3] ^ w[t - 8] ^ w[t - 14] ^ w[t - 16]; + w[t] = circular_shift(1u32, val); t += 1; } - let a = st.h.(0); - let b = st.h.(1); - let c = st.h.(2); - let d = st.h.(3); - let e = st.h.(4); + let a = st.h[0]; + let b = st.h[1]; + let c = st.h[2]; + let d = st.h[3]; + let e = st.h[4]; let temp: u32; t = 0; while t < 20 { - temp = - circular_shift(5u32, a) + (b & c | !b & d) + e + w.(t) + k0; + temp = circular_shift(5u32, a) + (b & c | !b & d) + e + w[t] + k0; e = d; d = c; c = circular_shift(30u32, b); @@ -124,7 +122,7 @@ fn mk_sha1() -> sha1 { t += 1; } while t < 40 { - temp = circular_shift(5u32, a) + (b ^ c ^ d) + e + w.(t) + k1; + temp = circular_shift(5u32, a) + (b ^ c ^ d) + e + w[t] + k1; e = d; d = c; c = circular_shift(30u32, b); @@ -134,8 +132,8 @@ fn mk_sha1() -> sha1 { } while t < 60 { temp = - circular_shift(5u32, a) + (b & c | b & d | c & d) + e + w.(t) - + k2; + circular_shift(5u32, a) + (b & c | b & d | c & d) + e + w[t] + + k2; e = d; d = c; c = circular_shift(30u32, b); @@ -144,7 +142,7 @@ fn mk_sha1() -> sha1 { t += 1; } while t < 80 { - temp = circular_shift(5u32, a) + (b ^ c ^ d) + e + w.(t) + k3; + temp = circular_shift(5u32, a) + (b ^ c ^ d) + e + w[t] + k3; e = d; d = c; c = circular_shift(30u32, b); @@ -152,11 +150,11 @@ fn mk_sha1() -> sha1 { a = temp; t += 1; } - st.h.(0) = st.h.(0) + a; - st.h.(1) = st.h.(1) + b; - st.h.(2) = st.h.(2) + c; - st.h.(3) = st.h.(3) + d; - st.h.(4) = st.h.(4) + e; + st.h[0] = st.h[0] + a; + st.h[1] = st.h[1] + b; + st.h[2] = st.h[2] + c; + st.h[3] = st.h[3] + d; + st.h[4] = st.h[4] + e; st.msg_block_idx = 0u; } fn circular_shift(bits: u32, word: u32) -> u32 { @@ -164,13 +162,13 @@ fn mk_sha1() -> sha1 { } fn mk_result(st: &sha1state) -> [u8] { if !st.computed { pad_msg(st); st.computed = true; } - let rs: [u8] = ~[]; + let rs: [u8] = []; for hpart: u32 in st.h { let a = hpart >> 24u32 & 0xFFu32 as u8; let b = hpart >> 16u32 & 0xFFu32 as u8; let c = hpart >> 8u32 & 0xFFu32 as u8; let d = hpart & 0xFFu32 as u8; - rs += ~[a, b, c, d]; + rs += [a, b, c, d]; } ret rs; } @@ -195,31 +193,31 @@ fn mk_sha1() -> sha1 { */ if st.msg_block_idx > 55u { - st.msg_block.(st.msg_block_idx) = 0x80u8; + st.msg_block[st.msg_block_idx] = 0x80u8; st.msg_block_idx += 1u; while st.msg_block_idx < msg_block_len { - st.msg_block.(st.msg_block_idx) = 0u8; + st.msg_block[st.msg_block_idx] = 0u8; st.msg_block_idx += 1u; } process_msg_block(st); } else { - st.msg_block.(st.msg_block_idx) = 0x80u8; + st.msg_block[st.msg_block_idx] = 0x80u8; st.msg_block_idx += 1u; } while st.msg_block_idx < 56u { - st.msg_block.(st.msg_block_idx) = 0u8; + st.msg_block[st.msg_block_idx] = 0u8; st.msg_block_idx += 1u; } // Store the message length as the last 8 octets - st.msg_block.(56) = st.len_high >> 24u32 & 0xFFu32 as u8; - st.msg_block.(57) = st.len_high >> 16u32 & 0xFFu32 as u8; - st.msg_block.(58) = st.len_high >> 8u32 & 0xFFu32 as u8; - st.msg_block.(59) = st.len_high & 0xFFu32 as u8; - st.msg_block.(60) = st.len_low >> 24u32 & 0xFFu32 as u8; - st.msg_block.(61) = st.len_low >> 16u32 & 0xFFu32 as u8; - st.msg_block.(62) = st.len_low >> 8u32 & 0xFFu32 as u8; - st.msg_block.(63) = st.len_low & 0xFFu32 as u8; + st.msg_block[56] = st.len_high >> 24u32 & 0xFFu32 as u8; + st.msg_block[57] = st.len_high >> 16u32 & 0xFFu32 as u8; + st.msg_block[58] = st.len_high >> 8u32 & 0xFFu32 as u8; + st.msg_block[59] = st.len_high & 0xFFu32 as u8; + st.msg_block[60] = st.len_low >> 24u32 & 0xFFu32 as u8; + st.msg_block[61] = st.len_low >> 16u32 & 0xFFu32 as u8; + st.msg_block[62] = st.len_low >> 8u32 & 0xFFu32 as u8; + st.msg_block[63] = st.len_low & 0xFFu32 as u8; process_msg_block(st); } obj sha1(st: sha1state) { @@ -230,11 +228,11 @@ fn mk_sha1() -> sha1 { st.len_low = 0u32; st.len_high = 0u32; st.msg_block_idx = 0u; - st.h.(0) = 0x67452301u32; - st.h.(1) = 0xEFCDAB89u32; - st.h.(2) = 0x98BADCFEu32; - st.h.(3) = 0x10325476u32; - st.h.(4) = 0xC3D2E1F0u32; + st.h[0] = 0x67452301u32; + st.h[1] = 0xEFCDAB89u32; + st.h[2] = 0x98BADCFEu32; + st.h[3] = 0x10325476u32; + st.h[4] = 0xC3D2E1F0u32; st.computed = false; } fn input(msg: &[u8]) { add_input(st, msg); } diff --git a/src/lib/sio.rs b/src/lib/sio.rs index bd75efa76a5..b506f92a97b 100644 --- a/src/lib/sio.rs +++ b/src/lib/sio.rs @@ -7,25 +7,17 @@ import str; import net; type ctx = aio::ctx; -type client = { ctx: ctx, client: aio::client, - evt: _port<aio::socket_event> }; -type server = { ctx: ctx, server: aio::server, - evt: _port<aio::server_event> }; +type client = {ctx: ctx, client: aio::client, evt: _port<aio::socket_event>}; +type server = {ctx: ctx, server: aio::server, evt: _port<aio::server_event>}; -fn new() -> ctx { - ret aio::new(); -} +fn new() -> ctx { ret aio::new(); } -fn destroy(ctx: ctx) { - send(ctx, aio::quit); -} +fn destroy(ctx: ctx) { send(ctx, aio::quit); } fn make_socket(ctx: ctx, p: _port<aio::socket_event>) -> client { let evt: aio::socket_event = p.recv(); alt evt { - aio::connected(client) { - ret { ctx: ctx, client: client, evt: p }; - } + aio::connected(client) { ret {ctx: ctx, client: client, evt: p}; } _ { fail "Could not connect to client"; } } } @@ -38,22 +30,17 @@ fn connect_to(ctx: ctx, ip: net::ip_addr, portnum: int) -> client { fn read(c: client) -> [u8] { alt c.evt.recv() { - aio::closed. { - ret ~[]; - } - aio::received(buf) { - ret buf; - } + aio::closed. { ret []; } + aio::received(buf) { ret buf; } } } fn create_server(ctx: ctx, ip: net::ip_addr, portnum: int) -> server { let evt: _port<aio::server_event> = mk_port(); let p: _port<aio::server> = mk_port(); - send(ctx, aio::serve(ip, portnum, - evt.mk_chan(), p.mk_chan())); + send(ctx, aio::serve(ip, portnum, evt.mk_chan(), p.mk_chan())); let srv: aio::server = p.recv(); - ret { ctx: ctx, server: srv, evt: evt }; + ret {ctx: ctx, server: srv, evt: evt}; } fn accept_from(server: server) -> client { @@ -85,15 +72,8 @@ fn close_server(server: server) { fn close_client(client: client) { send(client.ctx, aio::close_client(client.client)); let evt: aio::socket_event; - do { - evt = client.evt.recv(); - alt evt { - aio::closed. { - ret; - } - _ {} - } - } while (true); + do { evt = client.evt.recv(); alt evt { aio::closed. { ret; } _ { } } } + while true } // Local Variables: diff --git a/src/lib/smallintmap.rs b/src/lib/smallintmap.rs index 6eca2eccc23..d5dcd6bcc78 100644 --- a/src/lib/smallintmap.rs +++ b/src/lib/smallintmap.rs @@ -10,7 +10,7 @@ import option::some; type smallintmap<T> = @{mutable v: [mutable option::t<T>]}; fn mk<@T>() -> smallintmap<T> { - let v: [mutable option::t<T>] = ~[mutable]; + let v: [mutable option::t<T>] = [mutable]; ret @{mutable v: v}; } @@ -19,7 +19,7 @@ fn insert<@T>(m: &smallintmap<T>, key: uint, val: &T) { } fn find<@T>(m: &smallintmap<T>, key: uint) -> option::t<T> { - if key < vec::len::<option::t<T>>(m.v) { ret m.v.(key); } + if key < vec::len::<option::t<T>>(m.v) { ret m.v[key]; } ret none::<T>; } diff --git a/src/lib/sort.rs b/src/lib/sort.rs index 137cf0495ca..2a43bde42be 100644 --- a/src/lib/sort.rs +++ b/src/lib/sort.rs @@ -6,20 +6,20 @@ export merge_sort; export quick_sort; export quick_sort3; -type lteq<T> = block(&T, &T) -> bool ; +type lteq<T> = block(&T, &T) -> bool; fn merge_sort<@T>(le: <eq<T>, v: &[T]) -> [T] { fn merge<@T>(le: <eq<T>, a: &[T], b: &[T]) -> [T] { - let rs: [T] = ~[]; + let rs: [T] = []; let a_len: uint = len::<T>(a); let a_ix: uint = 0u; let b_len: uint = len::<T>(b); let b_ix: uint = 0u; while a_ix < a_len && b_ix < b_len { - if le(a.(a_ix), b.(b_ix)) { - rs += ~[a.(a_ix)]; + if le(a[a_ix], b[b_ix]) { + rs += [a[a_ix]]; a_ix += 1u; - } else { rs += ~[b.(b_ix)]; b_ix += 1u; } + } else { rs += [b[b_ix]]; b_ix += 1u; } } rs += slice::<T>(a, a_ix, a_len); rs += slice::<T>(b, b_ix, b_len); @@ -34,19 +34,19 @@ fn merge_sort<@T>(le: <eq<T>, v: &[T]) -> [T] { } fn swap<@T>(arr: &[mutable T], x: uint, y: uint) { - let a = arr.(x); - arr.(x) = arr.(y); - arr.(y) = a; + let a = arr[x]; + arr[x] = arr[y]; + arr[y] = a; } fn part<@T>(compare_func: <eq<T>, arr: &[mutable T], left: uint, right: uint, pivot: uint) -> uint { - let pivot_value = arr.(pivot); + let pivot_value = arr[pivot]; swap::<T>(arr, pivot, right); let storage_index: uint = left; let i: uint = left; while i < right { - if compare_func({ arr.(i) }, pivot_value) { + if compare_func({ arr[i] }, pivot_value) { swap::<T>(arr, i, storage_index); storage_index += 1u; } @@ -82,26 +82,26 @@ fn quick_sort<@T>(compare_func: <eq<T>, arr: &[mutable T]) { fn qsort3<@T>(compare_func_lt: <eq<T>, compare_func_eq: <eq<T>, arr: &[mutable T], left: int, right: int) { if right <= left { ret; } - let v: T = arr.(right); + let v: T = arr[right]; let i: int = left - 1; let j: int = right; let p: int = i; let q: int = j; while true { i += 1; - while compare_func_lt({ arr.(i) }, v) { i += 1; } + while compare_func_lt({ arr[i] }, v) { i += 1; } j -= 1; - while compare_func_lt(v, { arr.(j) }) { + while compare_func_lt(v, { arr[j] }) { if j == left { break; } j -= 1; } if i >= j { break; } swap::<T>(arr, i as uint, j as uint); - if compare_func_eq({ arr.(i) }, v) { + if compare_func_eq({ arr[i] }, v) { p += 1; swap::<T>(arr, p as uint, i as uint); } - if compare_func_eq(v, { arr.(j) }) { + if compare_func_eq(v, { arr[j] }) { q -= 1; swap::<T>(arr, j as uint, q as uint); } @@ -131,7 +131,7 @@ fn quick_sort3<@T>(compare_func_lt: <eq<T>, compare_func_eq: <eq<T>, arr: &[mutable T]) { if len::<T>(arr) == 0u { ret; } qsort3::<T>(compare_func_lt, compare_func_eq, arr, 0, - (len::<T>(arr) as int) - 1); + (len::<T>(arr) as int) - 1); } // Local Variables: diff --git a/src/lib/str.rs b/src/lib/str.rs index df9f6817343..98e59f70b4f 100644 --- a/src/lib/str.rs +++ b/src/lib/str.rs @@ -73,8 +73,8 @@ fn eq(a: &str, b: &str) -> bool { if byte_len(b) != i { ret false; } while i > 0u { i -= 1u; - let cha = a.(i); - let chb = b.(i); + let cha = a[i]; + let chb = b[i]; if cha != chb { ret false; } } ret true; @@ -87,9 +87,9 @@ fn lteq(a: &str, b: &str) -> bool { if j < n { n = j; } let x: uint = 0u; while x < n { - let cha = a.(x); - let chb = b.(x); - if cha < chb { ret true; } else if (cha > chb) { ret false; } + let cha = a[x]; + let chb = b[x]; + if cha < chb { ret true; } else if cha > chb { ret false; } x += 1u; } ret i <= j; @@ -134,12 +134,12 @@ fn is_utf8(v: &[u8]) -> bool { let i = 0u; let total = vec::len::<u8>(v); while i < total { - let chsize = utf8_char_width(v.(i)); + let chsize = utf8_char_width(v[i]); if chsize == 0u { ret false; } if i + chsize > total { ret false; } i += 1u; while chsize > 1u { - if v.(i) & 192u8 != tag_cont_u8 { ret false; } + if v[i] & 192u8 != tag_cont_u8 { ret false; } i += 1u; chsize -= 1u; } @@ -149,7 +149,7 @@ fn is_utf8(v: &[u8]) -> bool { fn is_ascii(s: str) -> bool { let i: uint = byte_len(s); - while i > 0u { i -= 1u; if s.(i) & 128u8 != 0u8 { ret false; } } + while i > 0u { i -= 1u; if s[i] & 128u8 != 0u8 { ret false; } } ret true; } @@ -165,9 +165,7 @@ fn is_whitespace(s: str) -> bool { let i = 0u; let len = char_len(s); while i < len { - if !char::is_whitespace(char_at(s, i)) { - ret false; - } + if !char::is_whitespace(char_at(s, i)) { ret false; } i += 1u } ret true; @@ -192,7 +190,7 @@ fn unsafe_from_bytes(v: &[mutable? u8]) -> str { ret rustrt::str_from_ivec(v); } -fn unsafe_from_byte(u: u8) -> str { ret rustrt::str_from_ivec(~[u]); } +fn unsafe_from_byte(u: u8) -> str { ret rustrt::str_from_ivec([u]); } fn str_from_cstr(cstr: sbuf) -> str { ret rustrt::str_from_cstr(cstr); } @@ -204,19 +202,19 @@ fn push_utf8_bytes(s: &mutable str, ch: char) { let code = ch as uint; if code < max_one_b { s = rustrt::str_push_byte(s, code); - } else if (code < max_two_b) { + } else if code < max_two_b { s = rustrt::str_push_byte(s, code >> 6u & 31u | tag_two_b); s = rustrt::str_push_byte(s, code & 63u | tag_cont); - } else if (code < max_three_b) { + } else if code < max_three_b { s = rustrt::str_push_byte(s, code >> 12u & 15u | tag_three_b); s = rustrt::str_push_byte(s, code >> 6u & 63u | tag_cont); s = rustrt::str_push_byte(s, code & 63u | tag_cont); - } else if (code < max_four_b) { + } else if code < max_four_b { s = rustrt::str_push_byte(s, code >> 18u & 7u | tag_four_b); s = rustrt::str_push_byte(s, code >> 12u & 63u | tag_cont); s = rustrt::str_push_byte(s, code >> 6u & 63u | tag_cont); s = rustrt::str_push_byte(s, code & 63u | tag_cont); - } else if (code < max_five_b) { + } else if code < max_five_b { s = rustrt::str_push_byte(s, code >> 24u & 3u | tag_five_b); s = rustrt::str_push_byte(s, code >> 18u & 63u | tag_cont); s = rustrt::str_push_byte(s, code >> 12u & 63u | tag_cont); @@ -259,7 +257,7 @@ fn utf8_char_width(b: u8) -> uint { } fn char_range_at(s: str, i: uint) -> {ch: char, next: uint} { - let b0 = s.(i); + let b0 = s[i]; let w = utf8_char_width(b0); assert (w != 0u); if w == 1u { ret {ch: b0 as char, next: i + 1u}; } @@ -267,7 +265,7 @@ fn char_range_at(s: str, i: uint) -> {ch: char, next: uint} { let end = i + w; i += 1u; while i < end { - let byte = s.(i); + let byte = s[i]; assert (byte & 192u8 == tag_cont_u8); val <<= 6u; val += byte & 63u8 as uint; @@ -288,7 +286,7 @@ fn char_len(s: str) -> uint { let len = 0u; let total = byte_len(s); while i < total { - let chsize = utf8_char_width(s.(i)); + let chsize = utf8_char_width(s[i]); assert (chsize > 0u); len += 1u; i += chsize; @@ -298,12 +296,12 @@ fn char_len(s: str) -> uint { } fn to_chars(s: str) -> [char] { - let buf: [char] = ~[]; + let buf: [char] = []; let i = 0u; let len = byte_len(s); while i < len { let cur = char_range_at(s, i); - buf += ~[cur.ch]; + buf += [cur.ch]; i = cur.next; } ret buf; @@ -313,7 +311,7 @@ fn push_char(s: &mutable str, ch: char) { s += from_char(ch); } fn pop_char(s: &mutable str) -> char { let end = byte_len(s); - while end > 0u && s.(end - 1u) & 192u8 == tag_cont_u8 { end -= 1u; } + while end > 0u && s[end - 1u] & 192u8 == tag_cont_u8 { end -= 1u; } assert (end > 0u); let ch = char_at(s, end - 1u); s = substr(s, 0u, end - 1u); @@ -343,7 +341,7 @@ fn index(s: str, c: u8) -> int { fn rindex(s: str, c: u8) -> int { let n: int = str::byte_len(s) as int; - while n >= 0 { if s.(n) == c { ret n; } n -= 1; } + while n >= 0 { if s[n] == c { ret n; } n -= 1; } ret n; } @@ -353,7 +351,7 @@ fn find(haystack: str, needle: str) -> int { if needle_len == 0 { ret 0; } fn match_at(haystack: &str, needle: &str, i: int) -> bool { let j: int = i; - for c: u8 in needle { if haystack.(j) != c { ret false; } j += 1; } + for c: u8 in needle { if haystack[j] != c { ret false; } j += 1; } ret true; } let i: int = 0; @@ -377,7 +375,7 @@ fn ends_with(haystack: str, needle: str) -> bool { let needle_len: uint = byte_len(needle); ret if needle_len == 0u { true - } else if (needle_len > haystack_len) { + } else if needle_len > haystack_len { false } else { eq(substr(haystack, haystack_len - needle_len, needle_len), @@ -397,18 +395,19 @@ fn slice(s: str, begin: uint, end: uint) -> str { ret rustrt::str_slice(s, begin, end); } -fn safe_slice(s: str, begin: uint, end: uint): le(begin, end) -> str { +fn safe_slice(s: str, begin: uint, end: uint) : le(begin, end) -> str { assert (end <= str::byte_len(s)); // would need some magic to // make this a precondition + ret rustrt::str_slice(s, begin, end); } fn shift_byte(s: &mutable str) -> u8 { let len = byte_len(s); assert (len > 0u); - let b = s.(0); + let b = s[0]; s = substr(s, 1u, len - 1u); ret b; } @@ -416,7 +415,7 @@ fn shift_byte(s: &mutable str) -> u8 { fn pop_byte(s: &mutable str) -> u8 { let len = byte_len(s); assert (len > 0u); - let b = s.(len - 1u); + let b = s[len - 1u]; s = substr(s, 0u, len - 1u); ret b; } @@ -433,17 +432,17 @@ fn unshift_byte(s: &mutable str, b: u8) { } fn split(s: str, sep: u8) -> [str] { - let v: [str] = ~[]; + let v: [str] = []; let accum: str = ""; let ends_with_sep: bool = false; for c: u8 in s { if c == sep { - v += ~[accum]; + v += [accum]; accum = ""; ends_with_sep = true; } else { accum += unsafe_from_byte(c); ends_with_sep = false; } } - if str::byte_len(accum) != 0u || ends_with_sep { v += ~[accum]; } + if str::byte_len(accum) != 0u || ends_with_sep { v += [accum]; } ret v; } @@ -486,10 +485,10 @@ fn replace(s: str, from: str, to: str) : is_not_empty(from) -> str { check (is_not_empty(from)); if byte_len(s) == 0u { ret ""; - } else if (starts_with(s, from)) { + } else if starts_with(s, from) { ret to + replace(slice(s, byte_len(from), byte_len(s)), from, to); } else { - ret unsafe_from_byte(s.(0)) + + ret unsafe_from_byte(s[0]) + replace(slice(s, 1u, byte_len(s)), from, to); } } @@ -503,9 +502,7 @@ fn trim_left(s: &str) -> str { fn count_whities(s: &[char]) -> uint { let i = 0u; while i < vec::len(s) { - if !char::is_whitespace(s.(i)) { - break; - } + if !char::is_whitespace(s[i]) { break; } i += 1u; } ret i; @@ -519,9 +516,7 @@ fn trim_right(s: &str) -> str { fn count_whities(s: &[char]) -> uint { let i = vec::len(s); while 0u < i { - if !char::is_whitespace(s.(i - 1u)) { - break; - } + if !char::is_whitespace(s[i - 1u]) { break; } i -= 1u; } ret i; @@ -531,9 +526,7 @@ fn trim_right(s: &str) -> str { ret from_chars(vec::slice(chars, 0u, whities)); } -fn trim(s: &str) -> str { - trim_left(trim_right(s)) -} +fn trim(s: &str) -> str { trim_left(trim_right(s)) } // Local Variables: // mode: rust; diff --git a/src/lib/task.rs b/src/lib/task.rs index 9ed8e8ecdf0..fd971b27477 100644 --- a/src/lib/task.rs +++ b/src/lib/task.rs @@ -19,43 +19,35 @@ native "rust" mod rustrt { fn set_min_stack(stack_size: uint); fn new_task() -> task_id; - fn drop_task(task : *rust_task); - fn get_task_pointer(id : task_id) -> *rust_task; - fn start_task(id : task_id); + fn drop_task(task: *rust_task); + fn get_task_pointer(id: task_id) -> *rust_task; + fn start_task(id: task_id); fn get_task_trampoline() -> u32; - fn migrate_alloc(alloc : *u8, target : task_id); + fn migrate_alloc(alloc: *u8, target: task_id); - fn leak<@T>(thing : -T); + fn leak<@T>(thing: -T); } -type rust_task = { - id : task, - mutable notify_enabled : u8, - mutable notify_chan : comm::chan_handle<task_notification>, - ctx : task_context, - stack_ptr : *u8 -}; - -type task_context = { - regs : x86_registers, - next : *u8 -}; - -resource rust_task_ptr(task : *rust_task) { - rustrt::drop_task(task); -} +type rust_task = + {id: task, + mutable notify_enabled: u8, + mutable notify_chan: comm::chan_handle<task_notification>, + ctx: task_context, + stack_ptr: *u8}; + +type task_context = {regs: x86_registers, next: *u8}; + +resource rust_task_ptr(task: *rust_task) { rustrt::drop_task(task); } -fn get_task_ptr(id : task) -> rust_task_ptr { +fn get_task_ptr(id: task) -> rust_task_ptr { ret rust_task_ptr(rustrt::get_task_pointer(id)); } type task = int; type task_id = task; -fn get_task_id() -> task_id { - rustrt::get_task_id() -} +fn get_task_id() -> task_id { rustrt::get_task_id() } /** * Hints the scheduler to yield this task for a specified ammount of time. @@ -68,22 +60,20 @@ fn yield() { ret rustrt::task_yield(); } tag task_result { tr_success; tr_failure; } -tag task_notification { - exit(task, task_result); -} +tag task_notification { exit(task, task_result); } -fn join(task_port : (task_id, comm::port<task_notification>)) - -> task_result { +fn join(task_port: (task_id, comm::port<task_notification>)) -> task_result { let (id, port) = task_port; alt comm::recv::<task_notification>(port) { exit(_id, res) { - if _id == id { ret res } - else { fail #fmt("join received id %d, expected %d", _id, id) } + if _id == id { + ret res + } else { fail #fmt["join received id %d, expected %d", _id, id] } } } } -fn join_id(t : task_id) -> task_result { +fn join_id(t: task_id) -> task_result { alt rustrt::task_join(t) { 0 { tr_success } _ { tr_failure } } } @@ -93,33 +83,25 @@ fn pin() { rustrt::pin_task(); } fn unpin() { rustrt::unpin_task(); } -fn set_min_stack(stack_size : uint) { - rustrt::set_min_stack(stack_size); -} +fn set_min_stack(stack_size: uint) { rustrt::set_min_stack(stack_size); } -fn _spawn(thunk : -fn() -> ()) -> task { - spawn(thunk) -} +fn _spawn(thunk: -fn()) -> task { spawn(thunk) } -fn spawn(thunk : -fn() -> ()) -> task { - spawn_inner(thunk, none) -} +fn spawn(thunk: -fn()) -> task { spawn_inner(thunk, none) } -fn spawn_notify(thunk : -fn() -> (), notify : comm::chan<task_notification>) - -> task { +fn spawn_notify(thunk: -fn(), notify: comm::chan<task_notification>) -> task { spawn_inner(thunk, some(notify)) } -fn spawn_joinable(thunk : -fn()) -> (task_id, comm::port<task_notification>) { +fn spawn_joinable(thunk: -fn()) -> (task_id, comm::port<task_notification>) { let p = comm::port::<task_notification>(); let id = spawn_notify(thunk, comm::chan::<task_notification>(p)); ret (id, p); } // FIXME: make this a fn~ once those are supported. -fn spawn_inner(thunk : -fn() -> (), - notify : option<comm::chan<task_notification>>) - -> task_id { +fn spawn_inner(thunk: -fn(), notify: option<comm::chan<task_notification>>) -> + task_id { let id = rustrt::new_task(); // the order of arguments are outptr, taskptr, envptr. @@ -129,21 +111,21 @@ fn spawn_inner(thunk : -fn() -> (), // set up the task pointer let task_ptr = get_task_ptr(id); let regs = ptr::addr_of((**task_ptr).ctx.regs); - (*regs).edx = cast(*task_ptr); + (*regs).edx = cast(*task_ptr);; (*regs).esp = cast((**task_ptr).stack_ptr); - assert ptr::null() != (**task_ptr).stack_ptr; + assert (ptr::null() != (**task_ptr).stack_ptr); - let raw_thunk : { code: u32, env: u32 } = cast(thunk); + let raw_thunk: {code: u32, env: u32} = cast(thunk); (*regs).eip = raw_thunk.code; // set up notifications if they are enabled. alt notify { some(c) { - (**task_ptr).notify_enabled = 1u8; + (**task_ptr).notify_enabled = 1u8;; (**task_ptr).notify_chan = *c; } - none {} + none { } }; // okay, now we align the stack and add the environment pointer and a fake @@ -153,15 +135,15 @@ fn spawn_inner(thunk : -fn() -> (), // -4 for the return address. (*regs).esp = align_down((*regs).esp - 12u32) - 4u32; - let ra : *mutable u32 = cast((*regs).esp); - let env : *mutable u32 = cast((*regs).esp+4u32); - let tptr : *mutable u32 = cast((*regs).esp+12u32); + let ra: *mutable u32 = cast((*regs).esp); + let env: *mutable u32 = cast((*regs).esp + 4u32); + let tptr: *mutable u32 = cast((*regs).esp + 12u32); // put the return pointer in ecx. - (*regs).ecx = (*regs).esp + 8u32; + (*regs).ecx = (*regs).esp + 8u32;; - *tptr = cast(*task_ptr); - *env = raw_thunk.env; + *tptr = cast(*task_ptr);; + *env = raw_thunk.env;; *ra = rustrt::get_task_trampoline(); rustrt::migrate_alloc(cast(raw_thunk.env), id); @@ -173,31 +155,31 @@ fn spawn_inner(thunk : -fn() -> (), } // Who says we can't write an operating system in Rust? -type x86_registers = { +type x86_registers = // This needs to match the structure in context.h - mutable eax : u32, - mutable ebx : u32, - mutable ecx : u32, - mutable edx : u32, - mutable ebp : u32, - mutable esi : u32, - mutable edi : u32, - mutable esp : u32, - - mutable cs : u16, - mutable ds : u16, - mutable ss : u16, - mutable es : u16, - mutable fs : u16, - mutable gs : u16, - - mutable eflags : u32, - mutable eip : u32 -}; - -fn align_down(x : u32) -> u32 { + + + {mutable eax: u32, + mutable ebx: u32, + mutable ecx: u32, + mutable edx: u32, + mutable ebp: u32, + mutable esi: u32, + mutable edi: u32, + mutable esp: u32, + mutable cs: u16, + mutable ds: u16, + mutable ss: u16, + mutable es: u16, + mutable fs: u16, + mutable gs: u16, + mutable eflags: u32, + mutable eip: u32}; + +fn align_down(x: u32) -> u32 { + // Aligns x down to 16 bytes - x & !(15u32) + x & !15u32 } // Local Variables: diff --git a/src/lib/term.rs b/src/lib/term.rs index 8891a9ddadd..cfec5ff6b8f 100644 --- a/src/lib/term.rs +++ b/src/lib/term.rs @@ -40,15 +40,15 @@ const color_bright_cyan: u8 = 14u8; const color_bright_white: u8 = 15u8; -fn esc(writer: io::buf_writer) { writer.write(~[0x1bu8, '[' as u8]); } +fn esc(writer: io::buf_writer) { writer.write([0x1bu8, '[' as u8]); } fn reset(writer: io::buf_writer) { esc(writer); - writer.write(~['0' as u8, 'm' as u8]); + writer.write(['0' as u8, 'm' as u8]); } fn color_supported() -> bool { - let supported_terms = ~["xterm-color", "xterm", "screen-bce"]; + let supported_terms = ["xterm-color", "xterm", "screen-bce"]; ret alt generic_os::getenv("TERM") { option::some(env) { for term: str in supported_terms { @@ -63,8 +63,8 @@ fn color_supported() -> bool { fn set_color(writer: io::buf_writer, first_char: u8, color: u8) { assert (color < 16u8); esc(writer); - if color >= 8u8 { writer.write(~['1' as u8, ';' as u8]); color -= 8u8; } - writer.write(~[first_char, ('0' as u8) + color, 'm' as u8]); + if color >= 8u8 { writer.write(['1' as u8, ';' as u8]); color -= 8u8; } + writer.write([first_char, ('0' as u8) + color, 'm' as u8]); } fn fg(writer: io::buf_writer, color: u8) { diff --git a/src/lib/test.rs b/src/lib/test.rs index b725507502b..72e5eb75597 100644 --- a/src/lib/test.rs +++ b/src/lib/test.rs @@ -41,7 +41,7 @@ type test_name = str; // the test succeeds; if the function fails then the test fails. We // may need to come up with a more clever definition of test in order // to support isolation of tests into tasks. -type test_fn = fn() ; +type test_fn = fn(); // The definition of a single test. A test runner will run a list of // these. @@ -69,7 +69,7 @@ fn parse_opts(args: &[str]) : vec::is_not_empty(args) -> opt_res { // FIXME (#649): Shouldn't have to check here check (vec::is_not_empty(args)); let args_ = vec::tail(args); - let opts = ~[getopts::optflag("ignored")]; + let opts = [getopts::optflag("ignored")]; let match = alt getopts::getopts(args_, opts) { getopts::success(m) { m } @@ -78,7 +78,7 @@ fn parse_opts(args: &[str]) : vec::is_not_empty(args) -> opt_res { let filter = if vec::len(match.free) > 0u { - option::some(match.free.(0)) + option::some(match.free[0]) } else { option::none }; let run_ignored = getopts::opt_present(match, "ignored"); @@ -106,25 +106,22 @@ fn run_tests_console(opts: &test_opts, tests: &[test_desc]) -> bool { fn run_tests_console_(opts: &test_opts, tests: &[test_desc], to_task: &test_to_task) -> bool { - type test_state = @{ - out: io::writer, - use_color: bool, - mutable total: uint, - mutable passed: uint, - mutable failed: uint, - mutable ignored: uint, - mutable failures: [test_desc] - }; + type test_state = + @{out: io::writer, + use_color: bool, + mutable total: uint, + mutable passed: uint, + mutable failed: uint, + mutable ignored: uint, + mutable failures: [test_desc]}; fn callback(event: testevent, st: test_state) { alt event { te_filtered(filtered_tests) { st.total = vec::len(filtered_tests); - st.out.write_line(#fmt("\nrunning %u tests", st.total)); - } - te_wait(test) { - st.out.write_str(#fmt("test %s ... ", test.name)); + st.out.write_line(#fmt["\nrunning %u tests", st.total]); } + te_wait(test) { st.out.write_str(#fmt["test %s ... ", test.name]); } te_result(test, result) { alt result { tr_ok. { @@ -136,7 +133,7 @@ fn run_tests_console_(opts: &test_opts, tests: &[test_desc], st.failed += 1u; write_failed(st.out, st.use_color); st.out.write_line(""); - st.failures += ~[test]; + st.failures += [test]; } tr_ignored. { st.ignored += 1u; @@ -148,37 +145,35 @@ fn run_tests_console_(opts: &test_opts, tests: &[test_desc], } } - let st = @{ - out: io::stdout(), - use_color: use_color(), - mutable total: 0u, - mutable passed: 0u, - mutable failed: 0u, - mutable ignored: 0u, - mutable failures: ~[] - }; + let st = + @{out: io::stdout(), + use_color: use_color(), + mutable total: 0u, + mutable passed: 0u, + mutable failed: 0u, + mutable ignored: 0u, + mutable failures: []}; - run_tests(opts, tests, to_task, - bind callback(_, st)); + run_tests(opts, tests, to_task, bind callback(_, st)); - assert st.passed + st.failed + st.ignored == st.total; + assert (st.passed + st.failed + st.ignored == st.total); let success = st.failed == 0u; if !success { st.out.write_line("\nfailures:"); for test: test_desc in st.failures { let testname = test.name; // Satisfy alias analysis - st.out.write_line(#fmt(" %s", testname)); + st.out.write_line(#fmt[" %s", testname]); } } - st.out.write_str(#fmt("\nresult: ")); + st.out.write_str(#fmt["\nresult: "]); if success { // There's no parallelism at this point so it's safe to use color write_ok(st.out, true); } else { write_failed(st.out, true); } - st.out.write_str(#fmt(". %u passed; %u failed; %u ignored\n\n", - st.passed, st.failed, st.ignored)); + st.out.write_str(#fmt[". %u passed; %u failed; %u ignored\n\n", st.passed, + st.failed, st.ignored]); ret success; @@ -206,9 +201,7 @@ fn run_tests_console_(opts: &test_opts, tests: &[test_desc], } } -fn use_color() -> bool { - ret get_concurrency() == 1u; -} +fn use_color() -> bool { ret get_concurrency() == 1u; } tag testevent { te_filtered([test_desc]); @@ -216,8 +209,8 @@ tag testevent { te_result(test_desc, test_result); } -fn run_tests(opts: &test_opts, tests: &[test_desc], - to_task: &test_to_task, callback: fn(testevent)) { +fn run_tests(opts: &test_opts, tests: &[test_desc], to_task: &test_to_task, + callback: fn(testevent)) { let filtered_tests = filter_tests(opts, tests); @@ -227,19 +220,19 @@ fn run_tests(opts: &test_opts, tests: &[test_desc], // provide a great user experience because you might sit waiting for the // result of a particular test for an unusually long amount of time. let concurrency = get_concurrency(); - log #fmt("using %u test tasks", concurrency); + log #fmt["using %u test tasks", concurrency]; let total = vec::len(filtered_tests); let run_idx = 0u; let wait_idx = 0u; - let futures = ~[]; + let futures = []; while wait_idx < total { while vec::len(futures) < concurrency && run_idx < total { - futures += ~[run_test(filtered_tests.(run_idx), to_task)]; + futures += [run_test(filtered_tests[run_idx], to_task)]; run_idx += 1u; } - let future = futures.(0); + let future = futures[0]; callback(te_wait(future.test)); let result = future.wait(); callback(te_result(future.test, result)); @@ -306,33 +299,26 @@ fn filter_tests(opts: &test_opts, tests: &[test_desc]) -> [test_desc] { ret filtered; } -type test_future = - {test: test_desc, wait: fn() -> test_result }; +type test_future = {test: test_desc, wait: fn() -> test_result}; fn run_test(test: &test_desc, to_task: &test_to_task) -> test_future { if !test.ignore { let test_task = to_task(test.fn); ret {test: test, wait: - bind fn (test_task: joinable)-> test_result { - alt task::join(test_task) { - task::tr_success. { tr_ok } - task::tr_failure. { tr_failed } - } - }(test_task)}; - } else { - ret {test: test, - wait: fn () -> test_result { tr_ignored }}; - } + bind fn (test_task: joinable) -> test_result { + alt task::join(test_task) { + task::tr_success. { tr_ok } + task::tr_failure. { tr_failed } + } + }(test_task)}; + } else { ret {test: test, wait: fn () -> test_result { tr_ignored }}; } } // We need to run our tests in another task in order to trap test failures. // This function only works with functions that don't contain closures. fn default_test_to_task(f: &fn()) -> joinable { - fn run_task(f: fn()) { - configure_test_task(); - f(); - } + fn run_task(f: fn()) { configure_test_task(); f(); } ret task::spawn_joinable(bind run_task(f)); } diff --git a/src/lib/time.rs b/src/lib/time.rs index cf8ddfc6323..c92bdb61f30 100644 --- a/src/lib/time.rs +++ b/src/lib/time.rs @@ -18,4 +18,4 @@ fn precise_time_ns() -> u64 { let ns = 0u64; rustrt::nano_time(ns); ret ns; } fn precise_time_s() -> float { ret (precise_time_ns() as float) / 1000000000.; -} \ No newline at end of file +} diff --git a/src/lib/u64.rs b/src/lib/u64.rs index 12197adaedf..8082f7aef0d 100644 --- a/src/lib/u64.rs +++ b/src/lib/u64.rs @@ -33,4 +33,4 @@ fn to_str(n: u64, radix: uint) -> str { ret s; } -fn str(n: u64) -> str { ret to_str(n, 10u); } \ No newline at end of file +fn str(n: u64) -> str { ret to_str(n, 10u); } diff --git a/src/lib/ufind.rs b/src/lib/ufind.rs index 12e85a98b7c..f225a2a7ca3 100644 --- a/src/lib/ufind.rs +++ b/src/lib/ufind.rs @@ -10,11 +10,11 @@ type node = option::t<uint>; type ufind = {mutable nodes: [mutable node]}; -fn make() -> ufind { ret {mutable nodes: ~[mutable]}; } +fn make() -> ufind { ret {mutable nodes: [mutable]}; } fn make_set(ufnd: &ufind) -> uint { let idx = vec::len(ufnd.nodes); - ufnd.nodes += ~[mutable none::<uint>]; + ufnd.nodes += [mutable none::<uint>]; ret idx; } @@ -26,7 +26,7 @@ fn grow(ufnd: &ufind, n: uint) { } fn find(ufnd: &ufind, n: uint) -> uint { - alt ufnd.nodes.(n) { + alt ufnd.nodes[n] { none. { ret n; } some(m) { let m_ = m; be find(ufnd, m_); } } @@ -36,10 +36,8 @@ fn union(ufnd: &ufind, m: uint, n: uint) { let m_root = find(ufnd, m); let n_root = find(ufnd, n); if m_root < n_root { - ufnd.nodes.(n_root) = some::<uint>(m_root); - } else if (m_root > n_root) { - ufnd.nodes.(m_root) = some::<uint>(n_root); - } + ufnd.nodes[n_root] = some::<uint>(m_root); + } else if m_root > n_root { ufnd.nodes[m_root] = some::<uint>(n_root); } } fn set_count(ufnd: &ufind) -> uint { ret vec::len::<node>(ufnd.nodes); } diff --git a/src/lib/uint.rs b/src/lib/uint.rs index 8be0df57fd7..94517b29f27 100644 --- a/src/lib/uint.rs +++ b/src/lib/uint.rs @@ -51,7 +51,7 @@ fn parse_buf(buf: &[u8], radix: uint) -> uint { let power = 1u; let n = 0u; while true { - n += (buf.(i) - ('0' as u8) as uint) * power; + n += (buf[i] - ('0' as u8) as uint) * power; power *= radix; if i == 0u { ret n; } i -= 1u; @@ -59,9 +59,7 @@ fn parse_buf(buf: &[u8], radix: uint) -> uint { fail; } -fn from_str(s : &str) -> uint { - parse_buf(str::bytes(s), 10u) -} +fn from_str(s: &str) -> uint { parse_buf(str::bytes(s), 10u) } fn to_str(num: uint, radix: uint) -> str { let n = num; @@ -95,7 +93,7 @@ fn to_str(num: uint, radix: uint) -> str { } let s1: str = ""; let len: uint = str::byte_len(s); - while len != 0u { len -= 1u; s1 += str::unsafe_from_byte(s.(len)); } + while len != 0u { len -= 1u; s1 += str::unsafe_from_byte(s[len]); } ret s1; } fn str(i: uint) -> str { ret to_str(i, 10u); } diff --git a/src/lib/vec.rs b/src/lib/vec.rs index a2222dfbf21..dde4fc7505b 100644 --- a/src/lib/vec.rs +++ b/src/lib/vec.rs @@ -28,51 +28,51 @@ fn to_ptr<T>(v: &[T]) -> *T { ret rustrt::ivec_to_ptr(v); } fn len<T>(v: &[mutable? T]) -> uint { ret rusti::ivec_len(v); } -type init_op<T> = fn(uint) -> T ; +type init_op<T> = fn(uint) -> T; fn init_fn<@T>(op: &init_op<T>, n_elts: uint) -> [T] { - let v = ~[]; + let v = []; reserve(v, n_elts); let i: uint = 0u; - while i < n_elts { v += ~[op(i)]; i += 1u; } + while i < n_elts { v += [op(i)]; i += 1u; } ret v; } // TODO: Remove me once we have slots. fn init_fn_mut<@T>(op: &init_op<T>, n_elts: uint) -> [mutable T] { - let v = ~[mutable]; + let v = [mutable]; reserve(v, n_elts); let i: uint = 0u; - while i < n_elts { v += ~[mutable op(i)]; i += 1u; } + while i < n_elts { v += [mutable op(i)]; i += 1u; } ret v; } fn init_elt<@T>(t: &T, n_elts: uint) -> [T] { - let v = ~[]; + let v = []; reserve(v, n_elts); let i: uint = 0u; - while i < n_elts { v += ~[t]; i += 1u; } + while i < n_elts { v += [t]; i += 1u; } ret v; } // TODO: Remove me once we have slots. fn init_elt_mut<@T>(t: &T, n_elts: uint) -> [mutable T] { - let v = ~[mutable]; + let v = [mutable]; reserve(v, n_elts); let i: uint = 0u; - while i < n_elts { v += ~[mutable t]; i += 1u; } + while i < n_elts { v += [mutable t]; i += 1u; } ret v; } fn to_mut<@T>(v: &[T]) -> [mutable T] { - let vres = ~[mutable]; - for t: T in v { vres += ~[mutable t]; } + let vres = [mutable]; + for t: T in v { vres += [mutable t]; } ret vres; } fn from_mut<@T>(v: &[mutable T]) -> [T] { - let vres = ~[]; - for t: T in v { vres += ~[t]; } + let vres = []; + for t: T in v { vres += [t]; } ret vres; } @@ -88,7 +88,7 @@ pred is_not_empty<T>(v: &[mutable? T]) -> bool { ret !is_empty(v); } // Accessors /// Returns the first element of a vector -fn head<@T>(v: &[mutable? T]) : is_not_empty(v) -> T { ret v.(0); } +fn head<@T>(v: &[mutable? T]) : is_not_empty(v) -> T { ret v[0]; } /// Returns all but the first element of a vector fn tail<@T>(v: &[mutable? T]) : is_not_empty(v) -> [mutable? T] { @@ -98,17 +98,17 @@ fn tail<@T>(v: &[mutable? T]) : is_not_empty(v) -> [mutable? T] { /// Returns the last element of `v`. fn last<@T>(v: &[mutable? T]) -> option::t<T> { if len(v) == 0u { ret none; } - ret some(v.(len(v) - 1u)); + ret some(v[len(v) - 1u]); } /// Returns a copy of the elements from [`start`..`end`) from `v`. fn slice<@T>(v: &[mutable? T], start: uint, end: uint) -> [T] { assert (start <= end); assert (end <= len(v)); - let result = ~[]; + let result = []; reserve(result, end - start); let i = start; - while i < end { result += ~[v.(i)]; i += 1u; } + while i < end { result += [v[i]]; i += 1u; } ret result; } @@ -116,10 +116,10 @@ fn slice<@T>(v: &[mutable? T], start: uint, end: uint) -> [T] { fn slice_mut<@T>(v: &[mutable? T], start: uint, end: uint) -> [mutable T] { assert (start <= end); assert (end <= len(v)); - let result = ~[mutable]; + let result = [mutable]; reserve(result, end - start); let i = start; - while i < end { result += ~[mutable v.(i)]; i += 1u; } + while i < end { result += [mutable v[i]]; i += 1u; } ret result; } @@ -129,7 +129,7 @@ fn slice_mut<@T>(v: &[mutable? T], start: uint, end: uint) -> [mutable T] { fn shift<@T>(v: &mutable [mutable? T]) -> T { let ln = len::<T>(v); assert (ln > 0u); - let e = v.(0); + let e = v[0]; v = slice::<T>(v, 1u, ln); ret e; } @@ -139,7 +139,7 @@ fn pop<@T>(v: &mutable [mutable? T]) -> T { let ln = len(v); assert (ln > 0u); ln -= 1u; - let e = v.(ln); + let e = v[ln]; v = slice(v, 0u, ln); ret e; } @@ -153,22 +153,22 @@ fn pop<@T>(v: &mutable [mutable? T]) -> T { fn grow<@T>(v: &mutable [T], n: uint, initval: &T) { reserve(v, next_power_of_two(len(v) + n)); let i: uint = 0u; - while i < n { v += ~[initval]; i += 1u; } + while i < n { v += [initval]; i += 1u; } } // TODO: Remove me once we have slots. fn grow_mut<@T>(v: &mutable [mutable T], n: uint, initval: &T) { reserve(v, next_power_of_two(len(v) + n)); let i: uint = 0u; - while i < n { v += ~[mutable initval]; i += 1u; } + while i < n { v += [mutable initval]; i += 1u; } } /// Calls `f` `n` times and appends the results of these calls to the given /// vector. -fn grow_fn<@T>(v: &mutable [T], n: uint, init_fn: fn(uint) -> T ) { +fn grow_fn<@T>(v: &mutable [T], n: uint, init_fn: fn(uint) -> T) { reserve(v, next_power_of_two(len(v) + n)); let i: uint = 0u; - while i < n { v += ~[init_fn(i)]; i += 1u; } + while i < n { v += [init_fn(i)]; i += 1u; } } /// Sets the element at position `index` to `val`. If `index` is past the end @@ -176,49 +176,48 @@ fn grow_fn<@T>(v: &mutable [T], n: uint, init_fn: fn(uint) -> T ) { /// intervening space. fn grow_set<@T>(v: &mutable [mutable T], index: uint, initval: &T, val: &T) { if index >= len(v) { grow_mut(v, index - len(v) + 1u, initval); } - v.(index) = val; + v[index] = val; } // Functional utilities -fn map<@T, @U>(f: &block(&T) -> U , v: &[mutable? T]) -> [U] { - let result = ~[]; +fn map<@T, @U>(f: &block(&T) -> U, v: &[mutable? T]) -> [U] { + let result = []; reserve(result, len(v)); for elem: T in v { let elem2 = elem; // satisfies alias checker - result += ~[f(elem2)]; + result += [f(elem2)]; } ret result; } -fn map2<@T, @U, @V>(f: &block(&T, &U) -> V, v0: &[T], v1: &[U]) - -> [V] { +fn map2<@T, @U, @V>(f: &block(&T, &U) -> V, v0: &[T], v1: &[U]) -> [V] { let v0_len = len::<T>(v0); if v0_len != len::<U>(v1) { fail; } - let u: [V] = ~[]; + let u: [V] = []; let i = 0u; - while i < v0_len { u += ~[f({ v0.(i) }, { v1.(i) })]; i += 1u; } + while i < v0_len { u += [f({ v0[i] }, { v1[i] })]; i += 1u; } ret u; } -fn filter_map<@T, @U>(f: &block(&T) -> option::t<U>, - v: &[mutable? T]) -> [U] { - let result = ~[]; +fn filter_map<@T, @U>(f: &block(&T) -> option::t<U>, v: &[mutable? T]) -> + [U] { + let result = []; for elem: T in v { let elem2 = elem; // satisfies alias checker alt f(elem2) { none. {/* no-op */ } - some(result_elem) { result += ~[result_elem]; } + some(result_elem) { result += [result_elem]; } } } ret result; } -fn foldl<@T, @U>(p: &block(&U, &T) -> U , z: &U, v: &[mutable? T]) -> U { +fn foldl<@T, @U>(p: &block(&U, &T) -> U, z: &U, v: &[mutable? T]) -> U { let sz = len(v); if sz == 0u { ret z; } - let first = v.(0); + let first = v[0]; let rest = slice(v, 1u, sz); ret p(foldl(p, z, rest), first); } @@ -251,42 +250,36 @@ fn find<@T>(f: &block(&T) -> bool, v: &[T]) -> option::t<T> { fn position<@T>(x: &T, v: &[T]) -> option::t<uint> { let i: uint = 0u; - while i < len(v) { if x == v.(i) { ret some::<uint>(i); } i += 1u; } + while i < len(v) { if x == v[i] { ret some::<uint>(i); } i += 1u; } ret none; } fn position_pred<T>(f: fn(&T) -> bool, v: &[T]) -> option::t<uint> { let i: uint = 0u; - while i < len(v) { if f(v.(i)) { ret some::<uint>(i); } i += 1u; } + while i < len(v) { if f(v[i]) { ret some::<uint>(i); } i += 1u; } ret none; } fn unzip<@T, @U>(v: &[(T, U)]) -> ([T], [U]) { - let as = ~[], bs = ~[]; - for (a, b) in v { - as += ~[a]; - bs += ~[b]; - } + let as = [], bs = []; + for (a, b) in v { as += [a]; bs += [b]; } ret (as, bs); } // FIXME make the lengths being equal a constraint fn zip<@T, @U>(v: &[T], u: &[U]) -> [(T, U)] { - let zipped = ~[]; + let zipped = []; let sz = len(v), i = 0u; 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; } // Swaps two elements in a vector fn swap<@T>(v: &[mutable T], a: uint, b: uint) { - let t: T = v.(a); - v.(a) = v.(b); - v.(b) = t; + let t: T = v[a]; + v[a] = v[b]; + v[b] = t; } // In place vector reversal @@ -299,11 +292,11 @@ fn reverse<@T>(v: &[mutable T]) { // Functional vector reversal. Returns a reversed copy of v. fn reversed<@T>(v: &[T]) -> [T] { - let rs: [T] = ~[]; + let rs: [T] = []; let i = len::<T>(v); if i == 0u { ret rs; } else { i -= 1u; } - while i != 0u { rs += ~[v.(i)]; i -= 1u; } - rs += ~[v.(0)]; + while i != 0u { rs += [v[i]]; i -= 1u; } + rs += [v[0]]; ret rs; } @@ -328,7 +321,7 @@ mod unsafe { } fn from_buf<T>(ptr: *T, bytes: uint) -> [T] { - let v = ~[]; + let v = []; copy_from_buf(v, ptr, bytes); ret v; } diff --git a/src/lib/win32_fs.rs b/src/lib/win32_fs.rs index 6a8fe063251..5890e1f4443 100644 --- a/src/lib/win32_fs.rs +++ b/src/lib/win32_fs.rs @@ -5,9 +5,7 @@ native "rust" mod rustrt { fn rust_file_is_dir(path: str) -> int; } -fn list_dir(path: str) -> [str] { - ret *rustrt::rust_list_files(path + "*"); -} +fn list_dir(path: str) -> [str] { ret *rustrt::rust_list_files(path + "*"); } fn path_is_absolute(p: str) -> bool { ret str::char_at(p, 0u) == '/' || diff --git a/src/lib/win32_os.rs b/src/lib/win32_os.rs index cfd03dfce85..57944b5ff53 100644 --- a/src/lib/win32_os.rs +++ b/src/lib/win32_os.rs @@ -30,7 +30,7 @@ mod libc_constants { fn O_TRUNC() -> int { ret 512; } fn O_TEXT() -> int { ret 16384; } fn O_BINARY() -> int { ret 32768; } - fn O_NOINHERIT() -> int { ret 0x0080; } + fn O_NOINHERIT() -> int { ret 128; } fn S_IRUSR() -> uint { ret 256u; // really _S_IREAD in win32 @@ -59,12 +59,13 @@ fn pipe() -> {in: int, out: int} { // which means to pass it to a subprocess they need to be duplicated // first, as in rust_run_program. let fds = {mutable in: 0, mutable out: 0}; - let res = os::libc::_pipe(ptr::addr_of(fds.in), 1024u, - libc_constants::O_BINARY() - | libc_constants::O_NOINHERIT()); - assert res == 0; - assert fds.in != -1 && fds.in != 0; - assert fds.out != -1 && fds.in != 0; + let res = + os::libc::_pipe(ptr::addr_of(fds.in), 1024u, + libc_constants::O_BINARY() | + libc_constants::O_NOINHERIT()); + assert (res == 0); + assert (fds.in != -1 && fds.in != 0); + assert (fds.out != -1 && fds.in != 0); ret {in: fds.in, out: fds.out}; } |
