about summary refs log tree commit diff
diff options
context:
space:
mode:
authorGraydon Hoare <graydon@pobox.com>2011-12-28 13:05:02 -0800
committerGraydon Hoare <graydon@pobox.com>2011-12-28 13:05:02 -0800
commit51227c1e8abe1f6cf7957ca10e98f58a2e4fef5c (patch)
treee5cbaca869c51b7daea8a19b7bfb33a67a5201a6
parent9236fdf39f2cf38b9642768214c39ea1c9543628 (diff)
parentb6cecdd4de2824b3f07c2c7126300479037f0b3b (diff)
Merge pull request #1389 from erickt/master
misc standard library additions
-rw-r--r--src/comp/syntax/ext/fmt.rs2
-rw-r--r--src/libcore/u64.rs2
-rw-r--r--src/libstd/io.rs41
-rw-r--r--src/libstd/rand.rs17
4 files changed, 49 insertions, 13 deletions
diff --git a/src/comp/syntax/ext/fmt.rs b/src/comp/syntax/ext/fmt.rs
index 0530b743eef..3b17fdb6f7c 100644
--- a/src/comp/syntax/ext/fmt.rs
+++ b/src/comp/syntax/ext/fmt.rs
@@ -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);
diff --git a/src/libcore/u64.rs b/src/libcore/u64.rs
index 9483af9b634..a7cd9af35bc 100644
--- a/src/libcore/u64.rs
+++ b/src/libcore/u64.rs
@@ -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.
 */
diff --git a/src/libstd/io.rs b/src/libstd/io.rs
index deb31f59ba0..1928cd4b3d7 100644
--- a/src/libstd/io.rs
+++ b/src/libstd/io.rs
@@ -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)));
 }
@@ -256,7 +260,7 @@ 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;
@@ -264,7 +268,7 @@ type buf_writer =
     };
 
 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);
@@ -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;
@@ -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);
@@ -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));
     }
@@ -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;
@@ -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) {
@@ -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)
 }
 
 
diff --git a/src/libstd/rand.rs b/src/libstd/rand.rs
index be350baadbe..2d577a5fcf5 100644
--- a/src/libstd/rand.rs
+++ b/src/libstd/rand.rs
@@ -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); }
@@ -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()));
 }