Skip to content

Commit 40e733a

Browse files
committed
auto merge of #5209 : luqmana/rust/reader, r=graydon
2 parents c639a78 + 162c816 commit 40e733a

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

src/libcore/io.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ pub trait ReaderUtil {
7878
/// Read len bytes into a new vec.
7979
fn read_bytes(&self, len: uint) -> ~[u8];
8080

81+
/// Read up until a specified character (which is not returned) or EOF.
82+
fn read_until(&self, c: char) -> ~str;
83+
8184
/// Read up until the first '\n' char (which is not returned), or EOF.
8285
fn read_line(&self) -> ~str;
8386

@@ -181,16 +184,22 @@ impl<T:Reader> ReaderUtil for T {
181184
bytes
182185
}
183186

184-
fn read_line(&self) -> ~str {
187+
fn read_until(&self, c: char) -> ~str {
185188
let mut bytes = ~[];
186189
loop {
187190
let ch = self.read_byte();
188-
if ch == -1 || ch == 10 { break; }
191+
if ch == -1 || ch == c as int {
192+
break;
193+
}
189194
bytes.push(ch as u8);
190195
}
191196
str::from_bytes(bytes)
192197
}
193198

199+
fn read_line(&self) -> ~str {
200+
self.read_until('\n')
201+
}
202+
194203
fn read_chars(&self, n: uint) -> ~[char] {
195204
// returns the (consumed offset, n_req), appends characters to &chars
196205
fn chars_from_bytes<T:Reader>(bytes: &~[u8], chars: &mut ~[char])
@@ -262,12 +271,7 @@ impl<T:Reader> ReaderUtil for T {
262271
}
263272

264273
fn read_c_str(&self) -> ~str {
265-
let mut bytes: ~[u8] = ~[];
266-
loop {
267-
let ch = self.read_byte();
268-
if ch < 1 { break; } else { bytes.push(ch as u8); }
269-
}
270-
str::from_bytes(bytes)
274+
self.read_until(0 as char)
271275
}
272276

273277
fn read_whole_stream(&self) -> ~[u8] {

0 commit comments

Comments
 (0)