Skip to content

misc standard library additions #1389

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

Merged
merged 5 commits into from
Dec 28, 2011
Merged
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
2 changes: 1 addition & 1 deletion src/comp/syntax/ext/fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ fn expand_syntax_ext(cx: ext_ctxt, sp: span, arg: @ast::expr,
}
let fmt =
expr_to_str(cx, args[0],
"first argument to #fmt must be a " + "string literal.");
"first argument to #fmt must be a string literal.");
let fmtspan = args[0].span;
#debug("Format string:");
log(debug, fmt);
Expand Down
2 changes: 1 addition & 1 deletion src/libcore/u64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ Convert to a string
fn str(n: u64) -> str { ret to_str(n, 10u); }

/*
Function: parse_buf
Function: from_str

Parse a string as an unsigned integer.
*/
Expand Down
41 changes: 30 additions & 11 deletions src/libstd/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,10 @@ fn new_byte_buf_reader(buf: [u8]) -> buf_reader {
ret byte_buf_reader(@{buf: buf, mutable pos: 0u});
}

fn bytes_reader(bytes: [u8]) -> reader {
ret new_reader(new_byte_buf_reader(bytes));
}

fn string_reader(s: str) -> reader {
ret new_reader(new_byte_buf_reader(str::bytes(s)));
}
Expand All @@ -256,15 +260,15 @@ type buf_writer =
// FIXME: eventually u64

obj {
fn write([u8]);
fn write([const u8]);
fn seek(int, seek_style);
fn tell() -> uint;
fn flush() -> int;
fn fsync(level: fsync::level) -> int;
};

obj FILE_writer(f: os::libc::FILE, res: option::t<@FILE_res>) {
fn write(v: [u8]) unsafe {
fn write(v: [const u8]) unsafe {
let len = vec::len::<u8>(v);
let vbuf = vec::unsafe::to_ptr::<u8>(v);
let nout = os::libc::fwrite(vbuf, len, 1u, f);
Expand All @@ -283,7 +287,7 @@ obj FILE_writer(f: os::libc::FILE, res: option::t<@FILE_res>) {
resource fd_res(fd: fd_t) { os::libc::close(fd); }

obj fd_buf_writer(fd: fd_t, res: option::t<@fd_res>) {
fn write(v: [u8]) unsafe {
fn write(v: [const u8]) unsafe {
let len = vec::len::<u8>(v);
let count = 0u;
let vbuf;
Expand Down Expand Up @@ -351,7 +355,7 @@ type writer =
fn write_char(char);
fn write_int(int);
fn write_uint(uint);
fn write_bytes([u8]);
fn write_bytes([const u8]);
fn write_le_uint(uint, uint);
fn write_le_int(int, uint);
fn write_be_uint(uint, uint);
Expand Down Expand Up @@ -384,7 +388,7 @@ obj new_writer(out: buf_writer) {
}
fn write_int(n: int) { out.write(str::bytes(int::to_str(n, 10u))); }
fn write_uint(n: uint) { out.write(str::bytes(uint::to_str(n, 10u))); }
fn write_bytes(bytes: [u8]) { out.write(bytes); }
fn write_bytes(bytes: [const u8]) { out.write(bytes); }
fn write_le_uint(n: uint, size: uint) {
out.write(uint_to_le_bytes(n, size));
}
Expand Down Expand Up @@ -426,6 +430,12 @@ fn stderr() -> writer { ret new_writer(fd_buf_writer(2i32, option::none)); }
fn print(s: str) { stdout().write_str(s); }
fn println(s: str) { stdout().write_str(s + "\n"); }

type bytes_writer =
obj {
fn get_writer() -> writer;
fn get_bytes() -> [mutable u8];
};

type str_writer =
obj {
fn get_writer() -> writer;
Expand All @@ -435,7 +445,7 @@ type str_writer =
type mutable_byte_buf = @{mutable buf: [mutable u8], mutable pos: uint};

obj byte_buf_writer(buf: mutable_byte_buf) {
fn write(v: [u8]) {
fn write(v: [const u8]) {
// Fast path.

if buf.pos == vec::len(buf.buf) {
Expand Down Expand Up @@ -466,17 +476,26 @@ obj byte_buf_writer(buf: mutable_byte_buf) {
fn fsync(_level: fsync::level) -> int { ret 0; }
}

fn string_writer() -> str_writer {
fn bytes_writer() -> bytes_writer {
// FIXME: yikes, this is bad. Needs fixing of mutable syntax.

let b: [mutable u8] = [mutable 0u8];
vec::pop(b);
let buf: mutable_byte_buf = @{mutable buf: b, mutable pos: 0u};
obj str_writer_wrap(wr: writer, buf: mutable_byte_buf) {
let buf = @{mutable buf: b, mutable pos: 0u};
obj byte_buf_writer_wrap(wr: writer, buf: mutable_byte_buf) {
fn get_writer() -> writer { ret wr; }
fn get_str() -> str { ret str::unsafe_from_bytes(buf.buf); }
fn get_bytes() -> [mutable u8] { ret buf.buf; }
}
ret byte_buf_writer_wrap(new_writer(byte_buf_writer(buf)), buf);
}

fn string_writer() -> str_writer {
let writer = bytes_writer();
obj str_writer_wrap(wr: bytes_writer) {
fn get_writer() -> writer { wr.get_writer() }
fn get_str() -> str { str::unsafe_from_bytes(wr.get_bytes()) }
}
ret str_writer_wrap(new_writer(byte_buf_writer(buf)), buf);
str_writer_wrap(writer)
}


Expand Down
17 changes: 17 additions & 0 deletions src/libstd/rand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ type rng = obj {
Return a random string composed of A-Z, a-z, 0-9.
*/
fn gen_str(len: uint) -> str;

/*
Method: gen_bytes

Return a random byte string.
*/
fn gen_bytes(len: uint) -> [u8];
};

resource rand_res(c: rustrt::rctx) { rustrt::rand_free(c); }
Expand Down Expand Up @@ -74,6 +81,16 @@ fn mk_rng() -> rng {
}
s
}
fn gen_bytes(len: uint) -> [u8] {
let v = [];
let i = 0u;
while i < len {
let n = rustrt::rand_next(**c) as uint;
v += [(n % (u8::max_value as uint)) as u8];
i += 1u;
}
v
}
}
ret rt_rng(@rand_res(rustrt::rand_new()));
}
Expand Down