diff options
| author | Lindsey Kuper <[email protected]> | 2011-03-18 12:32:54 -0700 |
|---|---|---|
| committer | Lindsey Kuper <[email protected]> | 2011-03-18 12:32:54 -0700 |
| commit | 6dee1ac161a22a4bc1e49c5dac3c7bbba1ff97f0 (patch) | |
| tree | f423d576e977e7a258f09e50e5a51702525782e2 /src/lib/_str.rs | |
| parent | Ignore emacs autosave files. (diff) | |
| parent | rustc: Add a span_unimpl() for debugging (diff) | |
| download | rust-6dee1ac161a22a4bc1e49c5dac3c7bbba1ff97f0.tar.xz rust-6dee1ac161a22a4bc1e49c5dac3c7bbba1ff97f0.zip | |
Merge branch 'master' of git://github.com/graydon/rust
Diffstat (limited to 'src/lib/_str.rs')
| -rw-r--r-- | src/lib/_str.rs | 52 |
1 files changed, 46 insertions, 6 deletions
diff --git a/src/lib/_str.rs b/src/lib/_str.rs index 526dac8f..93d0a9f2 100644 --- a/src/lib/_str.rs +++ b/src/lib/_str.rs @@ -8,6 +8,8 @@ native "rust" mod rustrt { fn str_byte_len(str s) -> uint; fn str_alloc(uint n_bytes) -> str; fn str_from_vec(vec[u8] b) -> str; + fn str_from_cstr(sbuf cstr) -> str; + fn str_from_buf(sbuf buf, uint len) -> str; fn refcount[T](str s) -> uint; } @@ -111,6 +113,19 @@ fn unsafe_from_bytes(vec[u8] v) -> str { ret rustrt.str_from_vec(v); } +fn unsafe_from_byte(u8 u) -> str { + ret rustrt.str_from_vec(vec(u)); +} + +unsafe fn str_from_cstr(sbuf cstr) -> str { + ret rustrt.str_from_cstr(cstr); +} + +unsafe fn str_from_buf(sbuf buf, uint len) -> str { + ret rustrt.str_from_buf(buf, len); +} + + fn refcount(str s) -> uint { auto r = rustrt.refcount[u8](s); if (r == dbg.const_refcount) { @@ -190,7 +205,6 @@ fn starts_with(str haystack, str needle) -> bool { ret eq(substr(haystack, 0u, needle_len), needle); } - fn ends_with(str haystack, str needle) -> bool { let uint haystack_len = byte_len(haystack); let uint needle_len = byte_len(needle); @@ -206,34 +220,60 @@ fn ends_with(str haystack, str needle) -> bool { needle); } - fn substr(str s, uint begin, uint len) -> str { let str accum = ""; let uint i = begin; while (i < begin+len) { - accum += s.(i); + accum += unsafe_from_byte(s.(i)); i += 1u; } ret accum; } +fn shift_byte(&mutable str s) -> u8 { + auto len = byte_len(s); + check(len > 0u); + auto b = s.(0); + s = substr(s, 1u, len - 1u); + ret b; +} + +fn pop_byte(&mutable str s) -> u8 { + auto len = byte_len(s); + check(len > 0u); + auto b = s.(len - 1u); + s = substr(s, 0u, len - 1u); + ret b; +} + +fn push_byte(&mutable str s, u8 b) { + s += unsafe_from_byte(b); +} + +fn unshift_byte(&mutable str s, u8 b) { + auto res = alloc(byte_len(s) + 1u); + res += unsafe_from_byte(b); + res += s; + s = res; +} + fn split(str s, u8 sep) -> vec[str] { let vec[str] v = vec(); let str accum = ""; let bool ends_with_sep = false; for (u8 c in s) { if (c == sep) { - v += accum; + v += vec(accum); accum = ""; ends_with_sep = true; } else { - accum += c; + accum += unsafe_from_byte(c); ends_with_sep = false; } } if (_str.byte_len(accum) != 0u || ends_with_sep) { - v += accum; + v += vec(accum); } ret v; } |