aboutsummaryrefslogtreecommitdiff
path: root/src/lib
diff options
context:
space:
mode:
authorRoy Frostig <[email protected]>2010-08-11 16:06:45 -0700
committerRoy Frostig <[email protected]>2010-08-11 16:06:45 -0700
commitf307688bf44404b371b91b3b2a67048088695fe1 (patch)
treecfd89b43e614588ea7ee35eebd8f83eec3f5fedf /src/lib
parentFix reverse-indexing bug in _vec.init_fn. (diff)
downloadrust-f307688bf44404b371b91b3b2a67048088695fe1.tar.xz
rust-f307688bf44404b371b91b3b2a67048088695fe1.zip
Add native vec[u8] to str converter. Put in workaround for leak in str to vec[u8] converter. Add testcase exercising both. Drive-by fix a potential array-out-of-bounds write on rust_str buffers.
Diffstat (limited to 'src/lib')
-rw-r--r--src/lib/_str.rs35
1 files changed, 31 insertions, 4 deletions
diff --git a/src/lib/_str.rs b/src/lib/_str.rs
index a607c7d5..807edf31 100644
--- a/src/lib/_str.rs
+++ b/src/lib/_str.rs
@@ -1,10 +1,13 @@
import rustrt.sbuf;
+import std._vec.rustrt.vbuf;
+
native "rust" mod rustrt {
type sbuf;
fn str_buf(str s) -> sbuf;
fn str_byte_len(str s) -> uint;
fn str_alloc(uint n_bytes) -> str;
+ fn str_from_vec(vec[u8] b) -> str;
fn refcount[T](str s) -> uint;
}
@@ -40,9 +43,33 @@ 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);
+fn bytes(str s) -> vec[u8] {
+ /* FIXME (issue #58):
+ * Should be...
+ *
+ * fn ith(str s, uint i) -> u8 {
+ * ret s.(i);
+ * }
+ * ret _vec.init_fn[u8](bind ith(s, _), byte_len(s));
+ *
+ * but we do not correctly decrement refcount of s when
+ * the binding dies, so we have to do this manually.
+ */
+ let uint n = _str.byte_len(s);
+ let vec[u8] v = _vec.alloc[u8](n);
+ let uint i = 0u;
+ while (i < n) {
+ v += vec(s.(i));
+ i += 1u;
}
- ret _vec.init_fn[u8](bind ith(s, _), _str.byte_len(s));
+ ret v;
+}
+
+fn from_bytes(vec[u8] v) : is_utf8(v) -> str {
+ ret rustrt.str_from_vec(v);
+}
+
+fn refcount(str s) -> uint {
+ // -1 because calling this function incremented the refcount.
+ ret rustrt.refcount[u8](s) - 1u;
}