1
Fork 0

Merge pull request #3246 from erickt/incoming

libcore: update bytes/str reader to work with regions.
This commit is contained in:
Tim Chevalier 2012-08-23 17:50:07 -07:00
commit feca839b9b
3 changed files with 37 additions and 50 deletions

View file

@ -259,16 +259,17 @@ fn check_variants_T<T: copy>(
// It would be best to test the *crate* for stability, but // It would be best to test the *crate* for stability, but
// testing the string for stability is easier and ok for now. // testing the string for stability is easier and ok for now.
let handler = diagnostic::mk_handler(none); let handler = diagnostic::mk_handler(none);
let str3 = let str3 = do io::with_str_reader("") |rdr| {
@as_str(|a|pprust::print_crate( @as_str(|a|pprust::print_crate(
codemap, codemap,
intr, intr,
diagnostic::mk_span_handler(handler, codemap), diagnostic::mk_span_handler(handler, codemap),
crate2, crate2,
filename, filename,
io::str_reader(~""), a, rdr, a,
pprust::no_ann(), pprust::no_ann(),
false)); false))
};
match cx.mode { match cx.mode {
tm_converge => { tm_converge => {
check_roundtrip_convergence(str3, 1u); check_roundtrip_convergence(str3, 1u);
@ -419,7 +420,7 @@ fn parse_and_print(code: @~str) -> ~str {
write_file(filename, *code); write_file(filename, *code);
let crate = parse::parse_crate_from_source_str( let crate = parse::parse_crate_from_source_str(
filename, code, ~[], sess); filename, code, ~[], sess);
io::with_str_reader(*code, |rdr| { do io::with_str_reader(*code) |rdr| {
as_str(|a| as_str(|a|
pprust::print_crate( pprust::print_crate(
sess.cm, sess.cm,
@ -431,7 +432,7 @@ fn parse_and_print(code: @~str) -> ~str {
rdr, a, rdr, a,
pprust::no_ann(), pprust::no_ann(),
false) ) false) )
}) }
} }
fn has_raw_pointers(c: ast::crate) -> bool { fn has_raw_pointers(c: ast::crate) -> bool {

View file

@ -272,13 +272,14 @@ fn file_reader(path: ~str) -> result<Reader, ~str> {
// Byte buffer readers // Byte buffer readers
type ByteBuf = {buf: ~[const u8], mut pos: uint, len: uint}; type ByteBuf = {buf: &[const u8], mut pos: uint};
impl ByteBuf: Reader { impl ByteBuf: Reader {
fn read(buf: &[mut u8], len: uint) -> uint { fn read(buf: &[mut u8], len: uint) -> uint {
let count = uint::min(len, self.len - self.pos); let count = uint::min(len, self.buf.len() - self.pos);
vec::u8::memcpy(buf, vec::const_view(self.buf, self.pos, self.len), vec::u8::memcpy(buf,
vec::const_view(self.buf, self.pos, self.buf.len()),
count); count);
self.pos += count; self.pos += count;
@ -286,46 +287,27 @@ impl ByteBuf: Reader {
count count
} }
fn read_byte() -> int { fn read_byte() -> int {
if self.pos == self.len { return -1; } if self.pos == self.buf.len() { return -1; }
let b = self.buf[self.pos]; let b = self.buf[self.pos];
self.pos += 1u; self.pos += 1u;
return b as int; return b as int;
} }
// FIXME (#2738): implement this // FIXME (#2738): implement this
fn unread_byte(_byte: int) { error!("Unimplemented: unread_byte"); fail; } fn unread_byte(_byte: int) { error!("Unimplemented: unread_byte"); fail; }
fn eof() -> bool { self.pos == self.len } fn eof() -> bool { self.pos == self.buf.len() }
fn seek(offset: int, whence: SeekStyle) { fn seek(offset: int, whence: SeekStyle) {
let pos = self.pos; let pos = self.pos;
self.pos = seek_in_buf(offset, pos, self.len, whence); self.pos = seek_in_buf(offset, pos, self.buf.len(), whence);
} }
fn tell() -> uint { self.pos } fn tell() -> uint { self.pos }
} }
fn bytes_reader(bytes: ~[u8]) -> Reader { fn with_bytes_reader<t>(bytes: &[u8], f: fn(Reader) -> t) -> t {
bytes_reader_between(bytes, 0u, vec::len(bytes)) f({buf: bytes, mut pos: 0u} as Reader)
} }
fn bytes_reader_between(bytes: ~[u8], start: uint, end: uint) -> Reader { fn with_str_reader<T>(s: &str, f: fn(Reader) -> T) -> T {
{buf: bytes, mut pos: start, len: end} as Reader str::byte_slice(s, |bytes| with_bytes_reader(bytes, f))
}
fn with_bytes_reader<t>(bytes: ~[u8], f: fn(Reader) -> t) -> t {
f(bytes_reader(bytes))
}
fn with_bytes_reader_between<t>(bytes: ~[u8], start: uint, end: uint,
f: fn(Reader) -> t) -> t {
f(bytes_reader_between(bytes, start, end))
}
fn str_reader(s: ~str) -> Reader {
bytes_reader(str::to_bytes(s))
}
fn with_str_reader<T>(s: ~str, f: fn(Reader) -> T) -> T {
do str::as_bytes(s) |bytes| {
with_bytes_reader_between(bytes, 0u, str::len(s), f)
}
} }
// Writing // Writing
@ -847,9 +829,10 @@ mod tests {
#[test] #[test]
fn test_readchars_empty() { fn test_readchars_empty() {
let inp : io::Reader = io::str_reader(~""); do io::with_str_reader(~"") |inp| {
let res : ~[char] = inp.read_chars(128u); let res : ~[char] = inp.read_chars(128u);
assert(vec::len(res) == 0u); assert(vec::len(res) == 0u);
}
} }
#[test] #[test]
@ -862,13 +845,14 @@ mod tests {
29983, 38152, 30340, 27748, 29983, 38152, 30340, 27748,
21273, 20999, 32905, 27748]; 21273, 20999, 32905, 27748];
fn check_read_ln(len : uint, s: ~str, ivals: ~[int]) { fn check_read_ln(len : uint, s: ~str, ivals: ~[int]) {
let inp : io::Reader = io::str_reader(s); do io::with_str_reader(s) |inp| {
let res : ~[char] = inp.read_chars(len); let res : ~[char] = inp.read_chars(len);
if (len <= vec::len(ivals)) { if (len <= vec::len(ivals)) {
assert(vec::len(res) == len); assert(vec::len(res) == len);
}
assert(vec::slice(ivals, 0u, vec::len(res)) ==
vec::map(res, |x| x as int));
} }
assert(vec::slice(ivals, 0u, vec::len(res)) ==
vec::map(res, |x| x as int));
} }
let mut i = 0u; let mut i = 0u;
while i < 8u { while i < 8u {
@ -881,16 +865,18 @@ mod tests {
#[test] #[test]
fn test_readchar() { fn test_readchar() {
let inp : io::Reader = io::str_reader(~""); do io::with_str_reader(~"") |inp| {
let res : char = inp.read_char(); let res : char = inp.read_char();
assert(res as int == 29983); assert(res as int == 29983);
}
} }
#[test] #[test]
fn test_readchar_empty() { fn test_readchar_empty() {
let inp : io::Reader = io::str_reader(~""); do io::with_str_reader(~"") |inp| {
let res : char = inp.read_char(); let res : char = inp.read_char();
assert(res as int == -1); assert(res as int == -1);
}
} }
#[test] #[test]

View file

@ -69,7 +69,7 @@ mod test {
#[test] #[test]
fn read_simple_board() { fn read_simple_board() {
let s = #include_str("./maps/contest1.map"); let s = #include_str("./maps/contest1.map");
read_board_grid(io::str_reader(s)); io::with_str_reader(s, read_board_grid)
} }
} }