@@ -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,7 +119,7 @@ impl<T: Reader> T : ReaderUtil {
119
119
}
120
120
return ( i, 0 ) ;
121
121
}
122
- let mut buf : ~[ u8 ] = ~[ ] ;
122
+ let mut bytes : ~[ u8 ] = ~[ ] ;
123
123
let mut chars: ~[ char ] = ~[ ] ;
124
124
// might need more bytes, but reading n will never over-read
125
125
let mut nbread = n;
@@ -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 ( ) }
@@ -604,10 +606,10 @@ impl<T: Writer> T : WriterUtil {
604
606
self . write_str ( & "\n " ) ;
605
607
}
606
608
fn write_int ( n : int ) {
607
- int:: to_str_bytes ( n, 10 u, |buf | self . write ( buf ) )
609
+ int:: to_str_bytes ( n, 10 u, |bytes | self . write ( bytes ) )
608
610
}
609
611
fn write_uint ( n : uint ) {
610
- 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 ) )
611
613
}
612
614
fn write_le_uint ( n : uint ) {
613
615
u64_to_le_bytes ( n as u64 , uint:: bytes, |v| self . write ( v) )
@@ -689,34 +691,34 @@ pub fn print(s: &str) { stdout().write_str(s); }
689
691
pub fn println(s: &str) { stdout().write_line(s); }
690
692
691
693
pub struct BytesWriter {
692
- buf : DVec<u8>,
694
+ bytes : DVec<u8>,
693
695
mut pos: uint,
694
696
}
695
697
696
698
impl BytesWriter: Writer {
697
699
fn write(v: &[const u8]) {
698
- do self.buf .swap |buf | {
699
- let mut buf <- buf ;
700
+ do self.bytes .swap |bytes | {
701
+ let mut bytes <- bytes ;
700
702
let v_len = v.len();
701
- let buf_len = buf .len();
703
+ let bytes_len = bytes .len();
702
704
703
- let count = uint::max(buf_len , self.pos + v_len);
704
- vec::reserve(&mut buf , count);
705
- 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); }
706
708
707
709
{
708
- let view = vec::mut_view(buf , self.pos, count);
710
+ let view = vec::mut_view(bytes , self.pos, count);
709
711
vec::bytes::memcpy(view, v, v_len);
710
712
}
711
713
712
714
self.pos += v_len;
713
715
714
- move buf
716
+ move bytes
715
717
}
716
718
}
717
719
fn seek(offset: int, whence: SeekStyle) {
718
720
let pos = self.pos;
719
- let len = self.buf .len();
721
+ let len = self.bytes .len();
720
722
self.pos = seek_in_buf(offset, pos, len, whence);
721
723
}
722
724
fn tell() -> uint { self.pos }
@@ -733,14 +735,14 @@ impl @BytesWriter : Writer {
733
735
}
734
736
735
737
pub pure fn BytesWriter() -> BytesWriter {
736
- BytesWriter { buf : DVec(), mut pos: 0u }
738
+ BytesWriter { bytes : DVec(), mut pos: 0u }
737
739
}
738
740
739
741
pub pure fn with_bytes_writer(f: fn(Writer)) -> ~[u8] {
740
742
let wr = @BytesWriter();
741
743
f(wr as Writer);
742
744
// FIXME (#3758): This should not be needed.
743
- unsafe { wr.buf .check_out(|buf | move buf ) }
745
+ unsafe { wr.bytes .check_out(|bytes | move bytes ) }
744
746
}
745
747
746
748
pub pure fn with_str_writer(f: fn(Writer)) -> ~str {
@@ -981,15 +983,17 @@ mod tests {
981
983
fn bytes_buffer_overwrite ( ) {
982
984
let wr = BytesWriter ( ) ;
983
985
wr. write ( ~[ 0u8 , 1u8 , 2u8 , 3u8 ] ) ;
984
- assert wr. buf . borrow ( |buf| buf == ~[ 0u8 , 1u8 , 2u8 , 3u8 ] ) ;
986
+ assert wr. bytes . borrow ( |bytes| bytes == ~[ 0u8 , 1u8 , 2u8 , 3u8 ] ) ;
985
987
wr. seek ( -2 , SeekCur ) ;
986
988
wr. write ( ~[ 4u8 , 5u8 , 6u8 , 7u8 ] ) ;
987
- 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 ] ) ;
988
991
wr. seek ( -2 , SeekEnd ) ;
989
992
wr. write ( ~[ 8u8 ] ) ;
990
993
wr. seek ( 1 , SeekSet ) ;
991
994
wr. write ( ~[ 9u8 ] ) ;
992
- 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 ] ) ;
993
997
}
994
998
}
995
999
0 commit comments