libcore: Add read_until to ReaderUtil.
This commit is contained in:
parent
a7de81ac3e
commit
162c816e34
@ -78,6 +78,9 @@ pub trait ReaderUtil {
|
|||||||
/// Read len bytes into a new vec.
|
/// Read len bytes into a new vec.
|
||||||
fn read_bytes(&self, len: uint) -> ~[u8];
|
fn read_bytes(&self, len: uint) -> ~[u8];
|
||||||
|
|
||||||
|
/// Read up until a specified character (which is not returned) or EOF.
|
||||||
|
fn read_until(&self, c: char) -> ~str;
|
||||||
|
|
||||||
/// Read up until the first '\n' char (which is not returned), or EOF.
|
/// Read up until the first '\n' char (which is not returned), or EOF.
|
||||||
fn read_line(&self) -> ~str;
|
fn read_line(&self) -> ~str;
|
||||||
|
|
||||||
@ -181,16 +184,22 @@ impl<T:Reader> ReaderUtil for T {
|
|||||||
bytes
|
bytes
|
||||||
}
|
}
|
||||||
|
|
||||||
fn read_line(&self) -> ~str {
|
fn read_until(&self, c: char) -> ~str {
|
||||||
let mut bytes = ~[];
|
let mut bytes = ~[];
|
||||||
loop {
|
loop {
|
||||||
let ch = self.read_byte();
|
let ch = self.read_byte();
|
||||||
if ch == -1 || ch == 10 { break; }
|
if ch == -1 || ch == c as int {
|
||||||
|
break;
|
||||||
|
}
|
||||||
bytes.push(ch as u8);
|
bytes.push(ch as u8);
|
||||||
}
|
}
|
||||||
str::from_bytes(bytes)
|
str::from_bytes(bytes)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn read_line(&self) -> ~str {
|
||||||
|
self.read_until('\n')
|
||||||
|
}
|
||||||
|
|
||||||
fn read_chars(&self, n: uint) -> ~[char] {
|
fn read_chars(&self, n: uint) -> ~[char] {
|
||||||
// returns the (consumed offset, n_req), appends characters to &chars
|
// returns the (consumed offset, n_req), appends characters to &chars
|
||||||
fn chars_from_bytes<T:Reader>(bytes: &~[u8], chars: &mut ~[char])
|
fn chars_from_bytes<T:Reader>(bytes: &~[u8], chars: &mut ~[char])
|
||||||
@ -262,12 +271,7 @@ impl<T:Reader> ReaderUtil for T {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn read_c_str(&self) -> ~str {
|
fn read_c_str(&self) -> ~str {
|
||||||
let mut bytes: ~[u8] = ~[];
|
self.read_until(0 as char)
|
||||||
loop {
|
|
||||||
let ch = self.read_byte();
|
|
||||||
if ch < 1 { break; } else { bytes.push(ch as u8); }
|
|
||||||
}
|
|
||||||
str::from_bytes(bytes)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn read_whole_stream(&self) -> ~[u8] {
|
fn read_whole_stream(&self) -> ~[u8] {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user