Last active
August 29, 2015 14:02
-
-
Save schmee/a9e269fb47b35ea64afd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extern crate debug; | |
use std::str::CharRange; | |
use std::str::Chars; | |
use std::str::raw::slice_unchecked; | |
fn main() { | |
let test = "¨återbesök på färöarna"; | |
let v: Vec<u16> = to_utf16(test).collect(); | |
println!("{}", v); | |
println!("{}", test.to_utf16()); | |
assert_eq!(v, test.to_utf16()); | |
} | |
struct Utf16Codeunits<'a> { | |
chars: Chars<'a>, | |
extra: u16 | |
} | |
impl<'a> Iterator<u16> for Utf16Codeunits<'a> { | |
fn next(&mut self) -> Option<u16> { | |
if self.extra != 0 { | |
let tmp = self.extra; | |
self.extra = 0; | |
return Some(tmp); | |
} | |
let mut buf = [0u16, ..2]; | |
self.chars.next().map(|ch| { | |
let n = ch.encode_utf16(buf /* as mut slice! */); | |
if n == 2 { self.extra = buf[1]; } | |
buf[0] | |
}) | |
} | |
} | |
/// Converts to a vector of `u16` encoded as UTF-16. | |
fn to_utf16<'a>(s: &'a str) -> Utf16Codeunits<'a> { | |
Utf16Codeunits{ chars: s.chars(), extra: 0} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment