@@ -37,7 +37,7 @@ pub trait Reader {
37
37
// FIXME (#2004): Seekable really should be orthogonal.
38
38
39
39
// FIXME (#2982): This should probably return an error.
40
- fn read ( buf : & [ mut u8] , len : uint ) -> uint ;
40
+ fn read ( bytes : & [ mut u8] , len : uint ) -> uint ;
41
41
fn read_byte ( ) -> int ;
42
42
fn unread_byte ( int ) ;
43
43
fn eof ( ) -> bool ;
@@ -65,32 +65,32 @@ pub trait ReaderUtil {
65
65
66
66
impl < T : Reader > T : ReaderUtil {
67
67
fn read_bytes ( len : uint ) -> ~[ u8 ] {
68
- let mut buf = vec:: with_capacity ( len) ;
69
- unsafe { vec:: raw:: set_len ( & mut buf , len) ; }
68
+ let mut bytes = vec:: with_capacity ( len) ;
69
+ unsafe { vec:: raw:: set_len ( & mut bytes , len) ; }
70
70
71
- let count = self . read ( buf , len) ;
71
+ let count = self . read ( bytes , len) ;
72
72
73
- unsafe { vec:: raw:: set_len ( & mut buf , count) ; }
74
- move buf
73
+ unsafe { vec:: raw:: set_len ( & mut bytes , count) ; }
74
+ move bytes
75
75
}
76
76
fn read_line ( ) -> ~str {
77
- let mut buf = ~[ ] ;
77
+ let mut bytes = ~[ ] ;
78
78
loop {
79
79
let ch = self . read_byte ( ) ;
80
80
if ch == -1 || ch == 10 { break ; }
81
- buf . push ( ch as u8 ) ;
81
+ bytes . push ( ch as u8 ) ;
82
82
}
83
- str:: from_bytes ( buf )
83
+ str:: from_bytes ( bytes )
84
84
}
85
85
86
86
fn read_chars ( n : uint ) -> ~[ char ] {
87
87
// returns the (consumed offset, n_req), appends characters to &chars
88
- fn chars_from_bytes < T : Reader > ( buf : & ~[ u8 ] , chars : & mut ~[ char ] )
88
+ fn chars_from_bytes < T : Reader > ( bytes : & ~[ u8 ] , chars : & mut ~[ char ] )
89
89
-> ( uint , uint ) {
90
90
let mut i = 0 ;
91
- let buf_len = buf . len ( ) ;
92
- while i < buf_len {
93
- let b0 = buf [ i] ;
91
+ let bytes_len = bytes . len ( ) ;
92
+ while i < bytes_len {
93
+ let b0 = bytes [ i] ;
94
94
let w = str:: utf8_char_width ( b0) ;
95
95
let end = i + w;
96
96
i += 1 ;
@@ -100,12 +100,12 @@ impl<T: Reader> T : ReaderUtil {
100
100
loop ;
101
101
}
102
102
// can't satisfy this char with the existing data
103
- if end > buf_len {
104
- return ( i - 1 , end - buf_len ) ;
103
+ if end > bytes_len {
104
+ return ( i - 1 , end - bytes_len ) ;
105
105
}
106
106
let mut val = 0 ;
107
107
while i < end {
108
- let next = buf [ i] as int ;
108
+ let next = bytes [ i] as int ;
109
109
i += 1 ;
110
110
assert ( next > -1 ) ;
111
111
assert ( next & 192 == 128 ) ;
@@ -119,8 +119,8 @@ impl<T: Reader> T : ReaderUtil {
119
119
}
120
120
return ( i, 0 ) ;
121
121
}
122
- let mut buf : ~ [ u8 ] = ~[ ] ;
123
- let mut chars: ~ [ char ] = ~[ ] ;
122
+ let mut bytes = ~[ ] ;
123
+ let mut chars = ~[ ] ;
124
124
// might need more bytes, but reading n will never over-read
125
125
let mut nbread = n;
126
126
while nbread > 0 {
@@ -130,15 +130,15 @@ impl<T: Reader> T : ReaderUtil {
130
130
// we're split in a unicode char?
131
131
break ;
132
132
}
133
- buf . push_all ( data) ;
134
- let ( offset, nbreq) = chars_from_bytes :: < T > ( & buf , & mut chars) ;
133
+ bytes . push_all ( data) ;
134
+ let ( offset, nbreq) = chars_from_bytes :: < T > ( & bytes , & mut chars) ;
135
135
let ncreq = n - chars. len ( ) ;
136
136
// again we either know we need a certain number of bytes
137
137
// to complete a character, or we make sure we don't
138
138
// over-read by reading 1-byte per char needed
139
139
nbread = if ncreq > nbreq { ncreq } else { nbreq } ;
140
140
if nbread > 0 {
141
- buf = vec:: slice ( buf , offset, buf . len ( ) ) ;
141
+ bytes = vec:: slice ( bytes , offset, bytes . len ( ) ) ;
142
142
}
143
143
}
144
144
move chars
@@ -154,12 +154,12 @@ impl<T: Reader> T : ReaderUtil {
154
154
}
155
155
156
156
fn read_c_str ( ) -> ~str {
157
- let mut buf : ~[ u8 ] = ~[ ] ;
157
+ let mut bytes : ~[ u8 ] = ~[ ] ;
158
158
loop {
159
159
let ch = self . read_byte ( ) ;
160
- if ch < 1 { break ; } else { buf . push ( ch as u8 ) ; }
160
+ if ch < 1 { break ; } else { bytes . push ( ch as u8 ) ; }
161
161
}
162
- str:: from_bytes ( buf )
162
+ str:: from_bytes ( bytes )
163
163
}
164
164
165
165
// FIXME deal with eof? // #2004
@@ -191,9 +191,9 @@ impl<T: Reader> T : ReaderUtil {
191
191
}
192
192
193
193
fn read_whole_stream ( ) -> ~[ u8 ] {
194
- let mut buf : ~[ u8 ] = ~[ ] ;
195
- while !self . eof ( ) { buf . push_all ( self . read_bytes ( 2048 u) ) ; }
196
- move buf
194
+ let mut bytes : ~[ u8 ] = ~[ ] ;
195
+ while !self . eof ( ) { bytes . push_all ( self . read_bytes ( 2048 u) ) ; }
196
+ move bytes
197
197
}
198
198
199
199
fn each_byte ( it : fn ( int ) -> bool ) {
@@ -226,8 +226,8 @@ fn convert_whence(whence: SeekStyle) -> i32 {
226
226
}
227
227
228
228
impl * libc:: FILE : Reader {
229
- fn read ( buf : & [ mut u8] , len : uint ) -> uint {
230
- do vec:: as_mut_buf ( buf ) |buf_p, buf_len| {
229
+ fn read ( bytes : & [ mut u8] , len : uint ) -> uint {
230
+ do vec:: as_mut_buf ( bytes ) |buf_p, buf_len| {
231
231
assert buf_len <= len;
232
232
233
233
let count = libc:: fread ( buf_p as * mut c_void , 1 u as size_t ,
@@ -250,7 +250,9 @@ impl *libc::FILE: Reader {
250
250
// duration of its lifetime.
251
251
// FIXME there really should be a better way to do this // #2004
252
252
impl < T : Reader , C > { base: T , cleanup: C } : Reader {
253
- fn read ( buf : & [ mut u8] , len : uint ) -> uint { self . base . read ( buf, len) }
253
+ fn read ( bytes : & [ mut u8] , len : uint ) -> uint {
254
+ self . base . read ( bytes, len)
255
+ }
254
256
fn read_byte ( ) -> int { self . base . read_byte ( ) }
255
257
fn unread_byte ( byte : int ) { self . base . unread_byte ( byte) ; }
256
258
fn eof ( ) -> bool { self . base . eof ( ) }
@@ -297,39 +299,41 @@ pub fn file_reader(path: &Path) -> Result<Reader, ~str> {
297
299
}
298
300
299
301
300
- // Byte buffer readers
301
-
302
- pub type ByteBuf = {buf: &[const u8], mut pos: uint};
302
+ // Byte readers
303
+ pub struct BytesReader {
304
+ bytes: &[u8],
305
+ mut pos: uint
306
+ }
303
307
304
- impl ByteBuf : Reader {
305
- fn read(buf : &[mut u8], len: uint) -> uint {
306
- let count = uint::min(len, self.buf .len() - self.pos);
308
+ impl BytesReader : Reader {
309
+ fn read(bytes : &[mut u8], len: uint) -> uint {
310
+ let count = uint::min(len, self.bytes .len() - self.pos);
307
311
308
- let view = vec::const_view (self.buf , self.pos, self.buf .len());
309
- vec::bytes::memcpy(buf , view, count);
312
+ let view = vec::view (self.bytes , self.pos, self.bytes .len());
313
+ vec::bytes::memcpy(bytes , view, count);
310
314
311
315
self.pos += count;
312
316
313
317
count
314
318
}
315
319
fn read_byte() -> int {
316
- if self.pos == self.buf .len() { return -1; }
317
- let b = self.buf [self.pos];
320
+ if self.pos == self.bytes .len() { return -1; }
321
+ let b = self.bytes [self.pos];
318
322
self.pos += 1u;
319
323
return b as int;
320
324
}
321
325
// FIXME (#2738): implement this
322
326
fn unread_byte(_byte: int) { error!(" Unimplemented : unread_byte") ; fail; }
323
- fn eof ( ) -> bool { self . pos == self . buf . len ( ) }
327
+ fn eof ( ) -> bool { self . pos == self . bytes . len ( ) }
324
328
fn seek ( offset : int , whence : SeekStyle ) {
325
329
let pos = self . pos ;
326
- self . pos = seek_in_buf ( offset, pos, self . buf . len ( ) , whence) ;
330
+ self . pos = seek_in_buf ( offset, pos, self . bytes . len ( ) , whence) ;
327
331
}
328
332
fn tell ( ) -> uint { self . pos }
329
333
}
330
334
331
- pub fn with_bytes_reader < t > ( bytes : & [ u8 ] , f : fn ( Reader ) -> t ) -> t {
332
- f ( { buf : bytes, mut pos: 0 u} as Reader )
335
+ pub pure fn with_bytes_reader < t > ( bytes : & [ u8 ] , f : fn ( Reader ) -> t ) -> t {
336
+ f ( BytesReader { bytes : bytes, pos : 0 u } as Reader )
333
337
}
334
338
335
339
pub fn with_str_reader < T > ( s : & str , f : fn ( Reader ) -> T ) -> T {
@@ -602,10 +606,10 @@ impl<T: Writer> T : WriterUtil {
602
606
self . write_str ( & "\n " ) ;
603
607
}
604
608
fn write_int ( n : int ) {
605
- int:: to_str_bytes ( n, 10 u, |buf | self . write ( buf ) )
609
+ int:: to_str_bytes ( n, 10 u, |bytes | self . write ( bytes ) )
606
610
}
607
611
fn write_uint ( n : uint ) {
608
- uint:: to_str_bytes ( false , n, 10 u, |buf | self . write ( buf ) )
612
+ uint:: to_str_bytes ( false , n, 10 u, |bytes | self . write ( bytes ) )
609
613
}
610
614
fn write_le_uint ( n : uint ) {
611
615
u64_to_le_bytes ( n as u64 , uint:: bytes, |v| self . write ( v) )
@@ -687,34 +691,34 @@ pub fn print(s: &str) { stdout().write_str(s); }
687
691
pub fn println(s: &str) { stdout().write_line(s); }
688
692
689
693
pub struct BytesWriter {
690
- buf : DVec<u8>,
694
+ bytes : DVec<u8>,
691
695
mut pos: uint,
692
696
}
693
697
694
698
impl BytesWriter: Writer {
695
699
fn write(v: &[const u8]) {
696
- do self.buf .swap |buf | {
697
- let mut buf <- buf ;
700
+ do self.bytes .swap |bytes | {
701
+ let mut bytes <- bytes ;
698
702
let v_len = v.len();
699
- let buf_len = buf .len();
703
+ let bytes_len = bytes .len();
700
704
701
- let count = uint::max(buf_len , self.pos + v_len);
702
- vec::reserve(&mut buf , count);
703
- unsafe { vec::raw::set_len(&mut buf , count); }
705
+ let count = uint::max(bytes_len , self.pos + v_len);
706
+ vec::reserve(&mut bytes , count);
707
+ unsafe { vec::raw::set_len(&mut bytes , count); }
704
708
705
709
{
706
- let view = vec::mut_view(buf , self.pos, count);
710
+ let view = vec::mut_view(bytes , self.pos, count);
707
711
vec::bytes::memcpy(view, v, v_len);
708
712
}
709
713
710
714
self.pos += v_len;
711
715
712
- move buf
716
+ move bytes
713
717
}
714
718
}
715
719
fn seek(offset: int, whence: SeekStyle) {
716
720
let pos = self.pos;
717
- let len = self.buf .len();
721
+ let len = self.bytes .len();
718
722
self.pos = seek_in_buf(offset, pos, len, whence);
719
723
}
720
724
fn tell() -> uint { self.pos }
@@ -730,21 +734,25 @@ impl @BytesWriter : Writer {
730
734
fn get_type() -> WriterType { (*self).get_type() }
731
735
}
732
736
733
- pub fn BytesWriter() -> BytesWriter {
734
- BytesWriter { buf : DVec(), mut pos: 0u }
737
+ pub pure fn BytesWriter() -> BytesWriter {
738
+ BytesWriter { bytes : DVec(), mut pos: 0u }
735
739
}
736
740
737
- pub fn with_bytes_writer(f: fn(Writer)) -> ~[u8] {
741
+ pub pure fn with_bytes_writer(f: fn(Writer)) -> ~[u8] {
738
742
let wr = @BytesWriter();
739
743
f(wr as Writer);
740
- wr.buf.check_out(|buf| move buf)
744
+ // FIXME (#3758): This should not be needed.
745
+ unsafe { wr.bytes.check_out(|bytes| move bytes) }
741
746
}
742
747
743
- pub fn with_str_writer(f: fn(Writer)) -> ~str {
748
+ pub pure fn with_str_writer(f: fn(Writer)) -> ~str {
744
749
let mut v = with_bytes_writer(f);
745
750
746
- // Make sure the vector has a trailing null and is proper utf8.
747
- v.push(0);
751
+ // FIXME (#3758): This should not be needed.
752
+ unsafe {
753
+ // Make sure the vector has a trailing null and is proper utf8.
754
+ v.push(0);
755
+ }
748
756
assert str::is_utf8(v);
749
757
750
758
unsafe { move ::cast::transmute(move v) }
@@ -975,15 +983,17 @@ mod tests {
975
983
fn bytes_buffer_overwrite ( ) {
976
984
let wr = BytesWriter ( ) ;
977
985
wr. write ( ~[ 0u8 , 1u8 , 2u8 , 3u8 ] ) ;
978
- assert wr. buf . borrow ( |buf| buf == ~[ 0u8 , 1u8 , 2u8 , 3u8 ] ) ;
986
+ assert wr. bytes . borrow ( |bytes| bytes == ~[ 0u8 , 1u8 , 2u8 , 3u8 ] ) ;
979
987
wr. seek ( -2 , SeekCur ) ;
980
988
wr. write ( ~[ 4u8 , 5u8 , 6u8 , 7u8 ] ) ;
981
- assert wr. buf . borrow ( |buf| buf == ~[ 0u8 , 1u8 , 4u8 , 5u8 , 6u8 , 7u8 ] ) ;
989
+ assert wr. bytes . borrow ( |bytes| bytes ==
990
+ ~[ 0u8 , 1u8 , 4u8 , 5u8 , 6u8 , 7u8 ] ) ;
982
991
wr. seek ( -2 , SeekEnd ) ;
983
992
wr. write ( ~[ 8u8 ] ) ;
984
993
wr. seek ( 1 , SeekSet ) ;
985
994
wr. write ( ~[ 9u8 ] ) ;
986
- assert wr. buf . borrow ( |buf| buf == ~[ 0u8 , 9u8 , 4u8 , 5u8 , 8u8 , 7u8 ] ) ;
995
+ assert wr. bytes . borrow ( |bytes| bytes ==
996
+ ~[ 0u8 , 9u8 , 4u8 , 5u8 , 8u8 , 7u8 ] ) ;
987
997
}
988
998
}
989
999
0 commit comments