Skip to content

Commit 5409349

Browse files
committed
Demode some of core::io
1 parent d38b97a commit 5409349

File tree

1 file changed

+44
-42
lines changed

1 file changed

+44
-42
lines changed

src/libcore/io.rs

Lines changed: 44 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -74,69 +74,71 @@ impl<T: Reader> T : ReaderUtil {
7474
impl Reader {
7575
fn read_chars(n: uint) -> ~[char] {
7676
// returns the (consumed offset, n_req), appends characters to &chars
77-
fn chars_from_bytes(buf: ~[u8], &chars: ~[char]) -> (uint, uint) {
78-
let mut i = 0u;
79-
while i < vec::len(buf) {
77+
fn chars_from_bytes(buf: &~[u8], chars: &mut ~[char])
78+
-> (uint, uint) {
79+
let mut i = 0;
80+
let buf_len = buf.len();
81+
while i < buf_len {
8082
let b0 = buf[i];
8183
let w = str::utf8_char_width(b0);
8284
let end = i + w;
83-
i += 1u;
84-
assert (w > 0u);
85-
if w == 1u {
86-
vec::push(chars, b0 as char );
85+
i += 1;
86+
assert (w > 0);
87+
if w == 1 {
88+
vec::push(*chars, b0 as char);
8789
loop;
8890
}
8991
// can't satisfy this char with the existing data
90-
if end > vec::len(buf) {
91-
return (i - 1u, end - vec::len(buf));
92+
if end > buf_len {
93+
return (i - 1, end - buf_len);
9294
}
93-
let mut val = 0u;
95+
let mut val = 0;
9496
while i < end {
9597
let next = buf[i] as int;
96-
i += 1u;
98+
i += 1;
9799
assert (next > -1);
98100
assert (next & 192 == 128);
99-
val <<= 6u;
101+
val <<= 6;
100102
val += (next & 63) as uint;
101103
}
102104
// See str::char_at
103-
val += ((b0 << ((w + 1u) as u8)) as uint)
104-
<< (w - 1u) * 6u - w - 1u;
105-
vec::push(chars, val as char );
105+
val += ((b0 << ((w + 1) as u8)) as uint)
106+
<< (w - 1) * 6 - w - 1u;
107+
vec::push(*chars, val as char);
106108
}
107-
return (i, 0u);
109+
return (i, 0);
108110
}
109111
let mut buf: ~[u8] = ~[];
110112
let mut chars: ~[char] = ~[];
111113
// might need more bytes, but reading n will never over-read
112114
let mut nbread = n;
113-
while nbread > 0u {
115+
while nbread > 0 {
114116
let data = self.read_bytes(nbread);
115-
if vec::len(data) == 0u {
117+
if data.is_empty() {
116118
// eof - FIXME (#2004): should we do something if
117119
// we're split in a unicode char?
118120
break;
119121
}
120122
vec::push_all(buf, data);
121-
let (offset, nbreq) = chars_from_bytes(buf, chars);
122-
let ncreq = n - vec::len(chars);
123+
let (offset, nbreq) = chars_from_bytes(&buf, &mut chars);
124+
let ncreq = n - chars.len();
123125
// again we either know we need a certain number of bytes
124126
// to complete a character, or we make sure we don't
125127
// over-read by reading 1-byte per char needed
126128
nbread = if ncreq > nbreq { ncreq } else { nbreq };
127-
if nbread > 0u {
128-
buf = vec::slice(buf, offset, vec::len(buf));
129+
if nbread > 0 {
130+
buf = vec::slice(buf, offset, buf.len());
129131
}
130132
}
131133
move chars
132134
}
133135

134136
fn read_char() -> char {
135-
let c = self.read_chars(1u);
136-
if vec::len(c) == 0u {
137+
let c = self.read_chars(1);
138+
if vec::len(c) == 0 {
137139
return -1 as char; // FIXME will this stay valid? // #2004
138140
}
139-
assert(vec::len(c) == 1u);
141+
assert(vec::len(c) == 1);
140142
return c[0];
141143
}
142144

@@ -195,7 +197,7 @@ impl Reader {
195197
}
196198
}
197199

198-
fn each_line(it: fn(~str) -> bool) {
200+
fn each_line(it: fn(s: &str) -> bool) {
199201
while !self.eof() {
200202
if !it(self.read_line()) { break; }
201203
}
@@ -440,7 +442,7 @@ fn fd_writer(fd: fd_t, cleanup: bool) -> Writer {
440442
}
441443

442444

443-
fn mk_file_writer(path: &Path, flags: ~[FileFlag])
445+
fn mk_file_writer(path: &Path, flags: &[FileFlag])
444446
-> Result<Writer, ~str> {
445447

446448
#[cfg(windows)]
@@ -644,7 +646,7 @@ impl<T: Writer> T : WriterUtil {
644646
}
645647

646648
#[allow(non_implicitly_copyable_typarams)]
647-
fn file_writer(path: &Path, flags: ~[FileFlag]) -> Result<Writer, ~str> {
649+
fn file_writer(path: &Path, flags: &[FileFlag]) -> Result<Writer, ~str> {
648650
mk_file_writer(path, flags).chain(|w| result::Ok(w))
649651
}
650652

@@ -786,7 +788,7 @@ mod fsync {
786788

787789

788790
// Artifacts that need to fsync on destruction
789-
struct Res<t> {
791+
struct Res<t: Copy> {
790792
arg: Arg<t>,
791793
drop {
792794
match self.arg.opt_level {
@@ -799,7 +801,7 @@ mod fsync {
799801
}
800802
}
801803

802-
fn Res<t>(-arg: Arg<t>) -> Res<t>{
804+
fn Res<t: Copy>(+arg: Arg<t>) -> Res<t>{
803805
Res {
804806
arg: move arg
805807
}
@@ -808,28 +810,28 @@ mod fsync {
808810
type Arg<t> = {
809811
val: t,
810812
opt_level: Option<Level>,
811-
fsync_fn: fn@(t, Level) -> int
813+
fsync_fn: fn@(+f: t, Level) -> int
812814
};
813815

814816
// fsync file after executing blk
815817
// FIXME (#2004) find better way to create resources within lifetime of
816818
// outer res
817-
fn FILE_res_sync(&&file: FILERes, opt_level: Option<Level>,
818-
blk: fn(&&v: Res<*libc::FILE>)) {
819-
blk(Res({
819+
fn FILE_res_sync(file: &FILERes, opt_level: Option<Level>,
820+
blk: fn(+v: Res<*libc::FILE>)) {
821+
blk(move Res({
820822
val: file.f, opt_level: opt_level,
821-
fsync_fn: fn@(&&file: *libc::FILE, l: Level) -> int {
823+
fsync_fn: fn@(+file: *libc::FILE, l: Level) -> int {
822824
return os::fsync_fd(libc::fileno(file), l) as int;
823825
}
824826
}));
825827
}
826828

827829
// fsync fd after executing blk
828-
fn fd_res_sync(&&fd: FdRes, opt_level: Option<Level>,
829-
blk: fn(&&v: Res<fd_t>)) {
830-
blk(Res({
830+
fn fd_res_sync(fd: &FdRes, opt_level: Option<Level>,
831+
blk: fn(+v: Res<fd_t>)) {
832+
blk(move Res({
831833
val: fd.fd, opt_level: opt_level,
832-
fsync_fn: fn@(&&fd: fd_t, l: Level) -> int {
834+
fsync_fn: fn@(+fd: fd_t, l: Level) -> int {
833835
return os::fsync_fd(fd, l) as int;
834836
}
835837
}));
@@ -839,11 +841,11 @@ mod fsync {
839841
trait FSyncable { fn fsync(l: Level) -> int; }
840842

841843
// Call o.fsync after executing blk
842-
fn obj_sync(&&o: FSyncable, opt_level: Option<Level>,
843-
blk: fn(&&v: Res<FSyncable>)) {
844+
fn obj_sync(+o: FSyncable, opt_level: Option<Level>,
845+
blk: fn(+v: Res<FSyncable>)) {
844846
blk(Res({
845847
val: o, opt_level: opt_level,
846-
fsync_fn: fn@(&&o: FSyncable, l: Level) -> int {
848+
fsync_fn: fn@(+o: FSyncable, l: Level) -> int {
847849
return o.fsync(l);
848850
}
849851
}));

0 commit comments

Comments
 (0)