2010-06-23 23:03:09 -05:00
|
|
|
import rustrt.sbuf;
|
|
|
|
|
2010-08-11 18:06:45 -05:00
|
|
|
import std._vec.rustrt.vbuf;
|
|
|
|
|
2010-06-23 23:03:09 -05:00
|
|
|
native "rust" mod rustrt {
|
|
|
|
type sbuf;
|
|
|
|
fn str_buf(str s) -> sbuf;
|
2010-07-25 02:36:03 -05:00
|
|
|
fn str_byte_len(str s) -> uint;
|
2010-07-05 16:42:12 -05:00
|
|
|
fn str_alloc(uint n_bytes) -> str;
|
2010-08-11 18:06:45 -05:00
|
|
|
fn str_from_vec(vec[u8] b) -> str;
|
2010-07-13 16:23:49 -05:00
|
|
|
fn refcount[T](str s) -> uint;
|
2010-06-23 23:03:09 -05:00
|
|
|
}
|
|
|
|
|
2010-08-20 14:57:38 -05:00
|
|
|
fn eq(str a, str b) -> bool {
|
|
|
|
let uint i = byte_len(a);
|
|
|
|
if (byte_len(b) != i) {
|
|
|
|
ret false;
|
|
|
|
}
|
|
|
|
while (i > 0u) {
|
|
|
|
i -= 1u;
|
|
|
|
auto cha = a.(i);
|
|
|
|
auto chb = b.(i);
|
|
|
|
if (cha != chb) {
|
|
|
|
ret false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ret true;
|
|
|
|
}
|
|
|
|
|
2010-06-23 23:03:09 -05:00
|
|
|
fn is_utf8(vec[u8] v) -> bool {
|
2010-07-16 17:33:30 -05:00
|
|
|
fail; // FIXME
|
2010-06-23 23:03:09 -05:00
|
|
|
}
|
|
|
|
|
2010-08-05 01:09:25 -05:00
|
|
|
fn is_ascii(str s) -> bool {
|
2010-07-25 02:36:03 -05:00
|
|
|
let uint i = byte_len(s);
|
2010-08-05 01:09:25 -05:00
|
|
|
while (i > 0u) {
|
|
|
|
i -= 1u;
|
2010-08-05 12:10:39 -05:00
|
|
|
if ((s.(i) & 0x80u8) != 0u8) {
|
2010-08-05 01:09:25 -05:00
|
|
|
ret false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ret true;
|
|
|
|
}
|
|
|
|
|
2010-07-05 16:42:12 -05:00
|
|
|
fn alloc(uint n_bytes) -> str {
|
2010-06-23 23:03:09 -05:00
|
|
|
ret rustrt.str_alloc(n_bytes);
|
|
|
|
}
|
|
|
|
|
2010-07-25 02:36:03 -05:00
|
|
|
// Returns the number of bytes (a.k.a. UTF-8 code units) in s.
|
|
|
|
// Contrast with a function that would return the number of code
|
|
|
|
// points (char's), combining character sequences, words, etc. See
|
|
|
|
// http://icu-project.org/apiref/icu4c/classBreakIterator.html for a
|
|
|
|
// way to implement those.
|
|
|
|
fn byte_len(str s) -> uint {
|
|
|
|
ret rustrt.str_byte_len(s);
|
2010-06-23 23:03:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn buf(str s) -> sbuf {
|
|
|
|
ret rustrt.str_buf(s);
|
|
|
|
}
|
2010-08-05 01:09:25 -05:00
|
|
|
|
2010-08-11 18:06:45 -05:00
|
|
|
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;
|
2010-08-05 01:09:25 -05:00
|
|
|
}
|
2010-08-11 18:06:45 -05:00
|
|
|
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;
|
2010-08-05 01:09:25 -05:00
|
|
|
}
|