diff options
| author | Huon Wilson <dbau.pp+github@gmail.com> | 2014-02-18 22:25:32 +1100 |
|---|---|---|
| committer | Huon Wilson <dbau.pp+github@gmail.com> | 2014-02-19 14:09:16 +1100 |
| commit | c9b4538babbc85b971b19bbeff16bd12a4f4db54 (patch) | |
| tree | 8efb0c748d1330d1e5bfcb5c9b0c1a14ff92655b /src/libstd/str.rs | |
| parent | 4f841ee1509fafdf688a3898e01560ae29ee7836 (diff) | |
str: add a function for truncating a vector of u16 at NUL.
Many of the functions interacting with Windows APIs allocate a vector of 0's and do not retrieve a length directly from the API call, and so need to be sure to remove the unmodified junk at the end of the vector.
Diffstat (limited to 'src/libstd/str.rs')
| -rw-r--r-- | src/libstd/str.rs | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/src/libstd/str.rs b/src/libstd/str.rs index 20321dad600..1f94aaaa7c4 100644 --- a/src/libstd/str.rs +++ b/src/libstd/str.rs @@ -920,6 +920,32 @@ pub fn utf16_items<'a>(v: &'a [u16]) -> UTF16Items<'a> { UTF16Items { iter : v.iter() } } +/// Return a slice of `v` ending at (and not including) the first NUL +/// (0). +/// +/// # Example +/// +/// ```rust +/// use std::str; +/// +/// // "abcd" +/// let mut v = ['a' as u16, 'b' as u16, 'c' as u16, 'd' as u16]; +/// // no NULs so no change +/// assert_eq!(str::truncate_utf16_at_nul(v), v.as_slice()); +/// +/// // "ab\0d" +/// v[2] = 0; +/// assert_eq!(str::truncate_utf16_at_nul(v), +/// &['a' as u16, 'b' as u16]); +/// ``` +pub fn truncate_utf16_at_nul<'a>(v: &'a [u16]) -> &'a [u16] { + match v.iter().position(|c| *c == 0) { + // don't include the 0 + Some(i) => v.slice_to(i), + None => v + } +} + /// Decode a UTF-16 encoded vector `v` into a string, returning `None` /// if `v` contains any invalid data. /// @@ -3876,6 +3902,24 @@ mod tests { } #[test] + fn test_truncate_utf16_at_nul() { + let v = []; + assert_eq!(truncate_utf16_at_nul(v), &[]); + + let v = [0, 2, 3]; + assert_eq!(truncate_utf16_at_nul(v), &[]); + + let v = [1, 0, 3]; + assert_eq!(truncate_utf16_at_nul(v), &[1]); + + let v = [1, 2, 0]; + assert_eq!(truncate_utf16_at_nul(v), &[1, 2]); + + let v = [1, 2, 3]; + assert_eq!(truncate_utf16_at_nul(v), &[1, 2, 3]); + } + + #[test] fn test_char_at() { let s = ~"ศไทย中华Việt Nam"; let v = ~['ศ','ไ','ท','ย','中','华','V','i','ệ','t',' ','N','a','m']; |
