Skip to content

add a copyless with_str_reader, and remove_file #1914

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/comp/driver/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,8 +272,10 @@ fn pretty_print_input(sess: session, cfg: ast::crate_cfg, input: str,
ppm_expanded | ppm_normal {}
}
let src = codemap::get_filemap(sess.codemap, input).src;
pprust::print_crate(sess.codemap, sess.span_diagnostic, crate, input,
io::string_reader(*src), io::stdout(), ann);
io::with_str_reader(*src) { |rdr|
pprust::print_crate(sess.codemap, sess.span_diagnostic, crate, input,
rdr, io::stdout(), ann);
}
}

fn get_os(triple: str) -> option<session::os> {
Expand Down
26 changes: 15 additions & 11 deletions src/fuzzer/fuzzer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ fn check_variants_T<T: copy>(
diagnostic::mk_span_handler(handler, codemap),
crate2,
filename,
io::string_reader(""), _,
io::str_reader(""), _,
pprust::no_ann()));
alt cx.mode {
tm_converge {
Expand Down Expand Up @@ -412,12 +412,14 @@ fn parse_and_print(code: @str) -> str {
write_file(filename, *code);
let crate = parser::parse_crate_from_source_str(
filename, code, [], sess);
ret as_str(bind pprust::print_crate(sess.cm,
io::with_str_reader(*code) { |rdr|
as_str(bind pprust::print_crate(sess.cm,
sess.span_diagnostic,
crate,
filename,
io::string_reader(*code), _,
pprust::no_ann()));
rdr, _,
pprust::no_ann()))
}
}

fn has_raw_pointers(c: ast::crate) -> bool {
Expand Down Expand Up @@ -557,13 +559,15 @@ fn check_variants(files: [str], cx: context) {
parser::parse_crate_from_source_str(
file,
s, [], sess);
#error("%s",
as_str(bind pprust::print_crate(sess.cm,
sess.span_diagnostic,
crate,
file,
io::string_reader(*s), _,
pprust::no_ann())));
io::with_str_reader(*s) { |rdr|
#error("%s",
as_str(bind pprust::print_crate(sess.cm,
sess.span_diagnostic,
crate,
file,
rdr, _,
pprust::no_ann())));
}
check_variants_of_ast(*crate, sess.cm, file, cx);
}
}
Expand Down
1 change: 1 addition & 0 deletions src/libstd/freebsd_os.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ native mod libc {
fn mkdir(path: str::sbuf, mode: c_int) -> c_int;
fn rmdir(path: str::sbuf) -> c_int;
fn chdir(path: str::sbuf) -> c_int;
fn unlink(path: str::sbuf) -> c_int;

fn sysctl(name: *c_int, namelen: c_uint,
oldp: *u8, &oldlenp: size_t,
Expand Down
21 changes: 21 additions & 0 deletions src/libstd/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,27 @@ fn homedir() -> option<path> {
}
}

/*
Function: remove_file

Deletes an existing file.
*/
fn remove_file(p: path) -> bool {
ret unlink(p);

#[cfg(target_os = "win32")]
fn unlink(p: path) -> bool {
ret str::as_buf(p, {|buf| os::kernel32::DeleteFile(buf)});
}

#[cfg(target_os = "linux")]
#[cfg(target_os = "macos")]
#[cfg(target_os = "freebsd")]
fn unlink(_p: path) -> bool {
ret str::as_buf(_p, {|buf| os::libc::unlink(buf) == 0i32 });
}
}

#[cfg(test)]
mod tests {
#[test]
Expand Down
41 changes: 29 additions & 12 deletions src/libstd/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,41 +227,58 @@ fn file_reader(path: str) -> result::t<reader, str> {
// Byte buffer readers

// TODO: const u8, but this fails with rustboot.
type byte_buf = {buf: [u8], mutable pos: uint};
type byte_buf = {buf: [u8], mutable pos: uint, len: uint};

impl of reader for byte_buf {
fn read_bytes(len: uint) -> [u8] {
let rest = vec::len(self.buf) - self.pos;
let rest = self.len - self.pos;
let to_read = len;
if rest < to_read { to_read = rest; }
let range = vec::slice(self.buf, self.pos, self.pos + to_read);
self.pos += to_read;
ret range;
}
fn read_byte() -> int {
if self.pos == vec::len(self.buf) { ret -1; }
if self.pos == self.len { ret -1; }
let b = self.buf[self.pos];
self.pos += 1u;
ret b as int;
}
fn unread_byte(_byte: int) { #error("TODO: unread_byte"); fail; }
fn eof() -> bool { self.pos == vec::len(self.buf) }
fn eof() -> bool { self.pos == self.len }
fn seek(offset: int, whence: seek_style) {
let pos = self.pos;
let len = vec::len(self.buf);
self.pos = seek_in_buf(offset, pos, len, whence);
self.pos = seek_in_buf(offset, pos, self.len, whence);
}
fn tell() -> uint { self.pos }
}

fn bytes_reader(bytes: [u8]) -> reader {
{buf: bytes, mutable pos: 0u} as reader
bytes_reader_between(bytes, 0u, vec::len(bytes))
}

fn bytes_reader_between(bytes: [u8], start: uint, end: uint) -> reader {
{buf: bytes, mutable pos: start, len: end} as reader
}

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 string_reader(s: str) -> reader {
fn str_reader(s: str) -> reader {
bytes_reader(str::bytes(s))
}

fn with_str_reader<T>(s: str, f: fn(reader) -> T) -> T {
str::as_bytes(s) { |bytes|
with_bytes_reader_between(bytes, 0u, str::len(s), f)
}
}

// Writing
enum fileflag { append, create, truncate, no_flag, }
Expand Down Expand Up @@ -645,7 +662,7 @@ mod tests {

#[test]
fn test_readchars_empty() {
let inp : io::reader = io::string_reader("");
let inp : io::reader = io::str_reader("");
let res : [char] = inp.read_chars(128u);
assert(vec::len(res) == 0u);
}
Expand All @@ -660,7 +677,7 @@ mod tests {
29983, 38152, 30340, 27748,
21273, 20999, 32905, 27748];
fn check_read_ln(len : uint, s: str, ivals: [int]) {
let inp : io::reader = io::string_reader(s);
let inp : io::reader = io::str_reader(s);
let res : [char] = inp.read_chars(len);
if (len <= vec::len(ivals)) {
assert(vec::len(res) == len);
Expand All @@ -679,14 +696,14 @@ mod tests {

#[test]
fn test_readchar() {
let inp : io::reader = io::string_reader("生");
let inp : io::reader = io::str_reader("生");
let res : char = inp.read_char();
assert(res as int == 29983);
}

#[test]
fn test_readchar_empty() {
let inp : io::reader = io::string_reader("");
let inp : io::reader = io::str_reader("");
let res : char = inp.read_char();
assert(res as int == -1);
}
Expand Down
2 changes: 1 addition & 1 deletion src/libstd/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,7 @@ Function: from_str
Deserializes a json value from a string.
*/
fn from_str(s: str) -> result::t<json, error> {
from_reader(io::string_reader(s))
io::with_str_reader(s, from_reader)
}

/*
Expand Down
1 change: 1 addition & 0 deletions src/libstd/linux_os.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ native mod libc {
fn mkdir(path: str::sbuf, mode: c_int) -> c_int;
fn rmdir(path: str::sbuf) -> c_int;
fn chdir(path: str::sbuf) -> c_int;
fn unlink(path: str::sbuf) -> c_int;
}

mod libc_constants {
Expand Down
1 change: 1 addition & 0 deletions src/libstd/macos_os.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ native mod libc {
fn mkdir(s: str::sbuf, mode: c_int) -> c_int;
fn rmdir(s: str::sbuf) -> c_int;
fn chdir(s: str::sbuf) -> c_int;
fn unlink(path: str::sbuf) -> c_int;

// FIXME: Needs varags
fn fcntl(fd: fd_t, cmd: c_int) -> c_int;
Expand Down
1 change: 1 addition & 0 deletions src/libstd/win32_os.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ native mod kernel32 {
lpSecurityAttributes: LPSECURITY_ATTRIBUTES) -> bool;
fn RemoveDirectoryA(lpPathName: LPCTSTR) -> bool;
fn SetCurrentDirectoryA(lpPathName: LPCTSTR) -> bool;
fn DeleteFile(lpFileName: LPCTSTR) -> bool;
}

// FIXME turn into constants
Expand Down
4 changes: 2 additions & 2 deletions src/test/bench/task-perf-word-count.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import comm::recv;
import comm::send;

fn map(input: str, emit: map_reduce::putter) {
let f = io::string_reader(input);
let f = io::str_reader(input);


while true {
Expand Down Expand Up @@ -451,4 +451,4 @@ convallis. Integer non tellus ante. Nulla hendrerit lobortis augue sit
amet vulputate. Donec cursus hendrerit diam convallis
luctus. Curabitur ipsum mauris, fermentum quis tincidunt ac, laoreet
sollicitudin sapien. Fusce velit urna, gravida non pulvinar eu, tempor
id nunc. " }
id nunc. " }