@@ -78,6 +78,9 @@ pub trait ReaderUtil {
78
78
/// Read len bytes into a new vec.
79
79
fn read_bytes ( & self , len : uint ) -> ~[ u8 ] ;
80
80
81
+ /// Read up until a specified character (which is not returned) or EOF.
82
+ fn read_until ( & self , c : char ) -> ~str ;
83
+
81
84
/// Read up until the first '\n' char (which is not returned), or EOF.
82
85
fn read_line ( & self ) -> ~str ;
83
86
@@ -181,16 +184,22 @@ impl<T:Reader> ReaderUtil for T {
181
184
bytes
182
185
}
183
186
184
- fn read_line ( & self ) -> ~str {
187
+ fn read_until ( & self , c : char ) -> ~str {
185
188
let mut bytes = ~[ ] ;
186
189
loop {
187
190
let ch = self . read_byte ( ) ;
188
- if ch == -1 || ch == 10 { break ; }
191
+ if ch == -1 || ch == c as int {
192
+ break ;
193
+ }
189
194
bytes. push ( ch as u8 ) ;
190
195
}
191
196
str:: from_bytes ( bytes)
192
197
}
193
198
199
+ fn read_line ( & self ) -> ~str {
200
+ self . read_until ( '\n' )
201
+ }
202
+
194
203
fn read_chars ( & self , n : uint ) -> ~[ char ] {
195
204
// returns the (consumed offset, n_req), appends characters to &chars
196
205
fn chars_from_bytes < T : Reader > ( bytes : & ~[ u8 ] , chars : & mut ~[ char ] )
@@ -262,12 +271,7 @@ impl<T:Reader> ReaderUtil for T {
262
271
}
263
272
264
273
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 )
271
275
}
272
276
273
277
fn read_whole_stream ( & self ) -> ~[ u8 ] {
0 commit comments