From 718c0b5963e6513337e4fee003b34423397c2d14 Mon Sep 17 00:00:00 2001 From: Roy Frostig Date: Wed, 4 Aug 2010 23:09:25 -0700 Subject: Add to std._io some formatter/type-specific-writer mechanism. Make a few type-specific buffered writers as wrappers of buf_writer. --- src/lib/_str.rs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'src/lib/_str.rs') 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)); +} -- cgit v1.2.3 From 935b4347e286b0022ae6f38b2875df6f05c55fa3 Mon Sep 17 00:00:00 2001 From: Graydon Hoare Date: Thu, 5 Aug 2010 10:10:39 -0700 Subject: Mop up workarounds in stdlib no longer required as issue #93 is closed. --- src/lib/_str.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) (limited to 'src/lib/_str.rs') diff --git a/src/lib/_str.rs b/src/lib/_str.rs index 8eed9a38..7d1a2dbd 100644 --- a/src/lib/_str.rs +++ b/src/lib/_str.rs @@ -16,8 +16,7 @@ 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) { + if ((s.(i) & 0x80u8) != 0u8) { ret false; } } @@ -38,7 +37,7 @@ fn buf(str s) -> sbuf { fn bytes(&str s) -> vec[u8] { fn ith(str s, uint i) -> u8 { - ret s.(i as int); // FIXME (issue #94) + ret s.(i); } ret _vec.init_fn[u8](bind ith(s, _), _str.len(s)); } -- cgit v1.2.3 From 3f6e8ffe64b57b0eaba6812208e94500422ca40c Mon Sep 17 00:00:00 2001 From: Jeffrey Yasskin Date: Sun, 25 Jul 2010 00:36:03 -0700 Subject: Implement _str.len() to return the number of bytes, rename it to byte_len(), and add a test. --- src/lib/_str.rs | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) (limited to 'src/lib/_str.rs') diff --git a/src/lib/_str.rs b/src/lib/_str.rs index 7d1a2dbd..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; } @@ -13,7 +13,7 @@ fn is_utf8(vec[u8] v) -> bool { } fn is_ascii(str s) -> bool { - let uint i = len(s); + let uint i = byte_len(s); while (i > 0u) { i -= 1u; if ((s.(i) & 0x80u8) != 0u8) { @@ -27,8 +27,13 @@ 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 { @@ -39,5 +44,5 @@ 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.len(s)); + ret _vec.init_fn[u8](bind ith(s, _), _str.byte_len(s)); } -- cgit v1.2.3