aboutsummaryrefslogtreecommitdiff
path: root/src/lib/_str.rs
diff options
context:
space:
mode:
authorOr Brostovski <[email protected]>2010-08-07 16:43:08 +0300
committerOr Brostovski <[email protected]>2010-08-07 16:43:08 +0300
commit4467d7683dae87d6d4c55e446910f7a5b85abd13 (patch)
treee2578dbe8e2350eb4e82ae2941fc2efb7478253b /src/lib/_str.rs
parentAdded AST pretty printing for communication alt statement, closes issue 19. (diff)
parentAdd Or to the AUTHORS file. (diff)
downloadrust-4467d7683dae87d6d4c55e446910f7a5b85abd13.tar.xz
rust-4467d7683dae87d6d4c55e446910f7a5b85abd13.zip
Merge branch 'master' of git://github.com/graydon/rust
Conflicts: src/boot/fe/ast.ml
Diffstat (limited to 'src/lib/_str.rs')
-rw-r--r--src/lib/_str.rs29
1 files changed, 26 insertions, 3 deletions
diff --git a/src/lib/_str.rs b/src/lib/_str.rs
index 062d8bf1..a607c7d5 100644
--- a/src/lib/_str.rs
+++ b/src/lib/_str.rs
@@ -3,7 +3,7 @@ import rustrt.sbuf;
native "rust" mod rustrt {
type sbuf;
fn str_buf(str s) -> sbuf;
- fn str_len(str s) -> uint;
+ fn str_byte_len(str s) -> uint;
fn str_alloc(uint n_bytes) -> str;
fn refcount[T](str s) -> uint;
}
@@ -12,14 +12,37 @@ fn is_utf8(vec[u8] v) -> bool {
fail; // FIXME
}
+fn is_ascii(str s) -> bool {
+ let uint i = byte_len(s);
+ while (i > 0u) {
+ i -= 1u;
+ if ((s.(i) & 0x80u8) != 0u8) {
+ ret false;
+ }
+ }
+ ret true;
+}
+
fn alloc(uint n_bytes) -> str {
ret rustrt.str_alloc(n_bytes);
}
-fn len(str s) -> uint {
- ret rustrt.str_len(s);
+// 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);
}
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);
+ }
+ ret _vec.init_fn[u8](bind ith(s, _), _str.byte_len(s));
+}