aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorRoy Frostig <[email protected]>2010-08-04 23:09:25 -0700
committerRoy Frostig <[email protected]>2010-08-04 23:09:33 -0700
commit718c0b5963e6513337e4fee003b34423397c2d14 (patch)
treebfce28f0d0e3b523691c4b536af213330a0f0a96 /src
parentThread argument-types down to internal_check_outer_lval in type.ml, in prepar... (diff)
downloadrust-718c0b5963e6513337e4fee003b34423397c2d14.tar.xz
rust-718c0b5963e6513337e4fee003b34423397c2d14.zip
Add to std._io some formatter/type-specific-writer mechanism. Make a few type-specific buffered writers as wrappers of buf_writer.
Diffstat (limited to 'src')
-rw-r--r--src/lib/_io.rs51
-rw-r--r--src/lib/_str.rs19
-rw-r--r--src/test/run-pass/lib-map.rs6
3 files changed, 75 insertions, 1 deletions
diff --git a/src/lib/_io.rs b/src/lib/_io.rs
index 142f808a..b0b0c313 100644
--- a/src/lib/_io.rs
+++ b/src/lib/_io.rs
@@ -1,3 +1,7 @@
+import std.os;
+import std._str;
+import std._vec;
+
type buf_reader = unsafe obj {
fn read() -> vec[u8];
};
@@ -107,3 +111,50 @@ fn new_buf_writer(str path, vec[fileflag] flags) -> buf_writer {
}
ret fd_buf_writer(fd);
}
+
+type formatter[T] = fn(&T x) -> vec[u8];
+
+type writer[T] = unsafe obj { fn write(&T x); };
+
+fn mk_writer[T](str path,
+ vec[fileflag] flags,
+ &formatter[T] fmt)
+ -> writer[T]
+{
+ unsafe obj w[T](buf_writer out, formatter[T] fmt) {
+ fn write(&T x) {
+ out.write(fmt(x));
+ }
+ }
+ ret w[T](new_buf_writer(path, flags), fmt);
+}
+
+/* TODO: int_writer, uint_writer, ... */
+
+fn str_writer(str path, vec[fileflag] flags) -> writer[str] {
+ auto fmt = _str.bytes; // FIXME (issue #90)
+ ret mk_writer[str](path, flags, fmt);
+}
+
+fn vec_writer[T](str path,
+ vec[fileflag] flags,
+ &formatter[T] inner)
+ -> writer[vec[T]]
+{
+ fn fmt[T](&vec[T] v, &formatter[T] inner) -> vec[u8] {
+ let vec[u8] res = _str.bytes("vec(");
+ auto first = true;
+ for (T x in v) {
+ if (!first) {
+ res += _str.bytes(", ");
+ } else {
+ first = false;
+ }
+ res += inner(x);
+ }
+ res += _str.bytes(")\n");
+ ret res;
+ }
+
+ ret mk_writer[vec[T]](path, flags, bind fmt[T](_, inner));
+}
diff --git a/src/lib/_str.rs b/src/lib/_str.rs
index 062d8bf1..8eed9a38 100644
--- a/src/lib/_str.rs
+++ b/src/lib/_str.rs
@@ -12,6 +12,18 @@ fn is_utf8(vec[u8] v) -> bool {
fail; // FIXME
}
+fn is_ascii(str s) -> bool {
+ let uint i = len(s);
+ while (i > 0u) {
+ i -= 1u;
+ // FIXME (issue #94)
+ if ((s.(i as int) & 0x80u8) != 0u8) {
+ ret false;
+ }
+ }
+ ret true;
+}
+
fn alloc(uint n_bytes) -> str {
ret rustrt.str_alloc(n_bytes);
}
@@ -23,3 +35,10 @@ fn len(str s) -> uint {
fn buf(str s) -> sbuf {
ret rustrt.str_buf(s);
}
+
+fn bytes(&str s) -> vec[u8] {
+ fn ith(str s, uint i) -> u8 {
+ ret s.(i as int); // FIXME (issue #94)
+ }
+ ret _vec.init_fn[u8](bind ith(s, _), _str.len(s));
+}
diff --git a/src/test/run-pass/lib-map.rs b/src/test/run-pass/lib-map.rs
index 11101c84..058fb237 100644
--- a/src/test/run-pass/lib-map.rs
+++ b/src/test/run-pass/lib-map.rs
@@ -4,13 +4,17 @@ use std;
import std.map;
fn test_simple() {
+ log "*** starting test_simple";
+
fn eq(&uint x, &uint y) -> bool { ret x == y; }
let map.hashfn[uint] hasher = std.util.id[uint];
let map.eqfn[uint] eqer = eq;
let map.hashmap[uint, uint] hm = map.mk_hashmap[uint, uint](hasher, eqer);
+
+ log "*** finished test_simple";
}
fn main() {
test_simple();
-} \ No newline at end of file
+}