|
8 | 8 | // option. This file may not be copied, modified, or distributed
|
9 | 9 | // except according to those terms.
|
10 | 10 |
|
| 11 | +use std::mem::transmute; |
| 12 | + |
11 | 13 | pub fn char_at(s: &str, byte: usize) -> char {
|
12 | 14 | s[byte..].chars().next().unwrap()
|
13 | 15 | }
|
| 16 | + |
| 17 | +// FIXME: This was copied from core/char.rs because it is currenty unstable. |
| 18 | +pub fn escape_unicode(ch: char) -> EscapeUnicode { |
| 19 | + EscapeUnicode { c: ch, state: EscapeUnicodeState::Backslash } |
| 20 | +} |
| 21 | + |
| 22 | +// FIXME: This was copied from core/char.rs because it is currenty unstable. |
| 23 | +pub fn escape_default(ch: char) -> EscapeDefault { |
| 24 | + let init_state = match ch { |
| 25 | + '\t' => EscapeDefaultState::Backslash('t'), |
| 26 | + '\r' => EscapeDefaultState::Backslash('r'), |
| 27 | + '\n' => EscapeDefaultState::Backslash('n'), |
| 28 | + '\\' => EscapeDefaultState::Backslash('\\'), |
| 29 | + '\'' => EscapeDefaultState::Backslash('\''), |
| 30 | + '"' => EscapeDefaultState::Backslash('"'), |
| 31 | + '\x20' ... '\x7e' => EscapeDefaultState::Char(ch), |
| 32 | + _ => EscapeDefaultState::Unicode(escape_unicode(ch)) |
| 33 | + }; |
| 34 | + EscapeDefault { state: init_state } |
| 35 | +} |
| 36 | + |
| 37 | +// FIXME: This was copied from core/char.rs because it is currenty unstable. |
| 38 | +pub struct EscapeUnicode { |
| 39 | + c: char, |
| 40 | + state: EscapeUnicodeState |
| 41 | +} |
| 42 | + |
| 43 | +enum EscapeUnicodeState { |
| 44 | + Backslash, |
| 45 | + Type, |
| 46 | + LeftBrace, |
| 47 | + Value(usize), |
| 48 | + RightBrace, |
| 49 | + Done, |
| 50 | +} |
| 51 | + |
| 52 | +impl Iterator for EscapeUnicode { |
| 53 | + type Item = char; |
| 54 | + |
| 55 | + fn next(&mut self) -> Option<char> { |
| 56 | + match self.state { |
| 57 | + EscapeUnicodeState::Backslash => { |
| 58 | + self.state = EscapeUnicodeState::Type; |
| 59 | + Some('\\') |
| 60 | + } |
| 61 | + EscapeUnicodeState::Type => { |
| 62 | + self.state = EscapeUnicodeState::LeftBrace; |
| 63 | + Some('u') |
| 64 | + } |
| 65 | + EscapeUnicodeState::LeftBrace => { |
| 66 | + let mut n = 0; |
| 67 | + while (self.c as u32) >> (4 * (n + 1)) != 0 { |
| 68 | + n += 1; |
| 69 | + } |
| 70 | + self.state = EscapeUnicodeState::Value(n); |
| 71 | + Some('{') |
| 72 | + } |
| 73 | + EscapeUnicodeState::Value(offset) => { |
| 74 | + let v = match ((self.c as i32) >> (offset * 4)) & 0xf { |
| 75 | + i @ 0 ... 9 => '0' as i32 + i, |
| 76 | + i => 'a' as i32 + (i - 10) |
| 77 | + }; |
| 78 | + if offset == 0 { |
| 79 | + self.state = EscapeUnicodeState::RightBrace; |
| 80 | + } else { |
| 81 | + self.state = EscapeUnicodeState::Value(offset - 1); |
| 82 | + } |
| 83 | + Some(unsafe { transmute(v) }) |
| 84 | + } |
| 85 | + EscapeUnicodeState::RightBrace => { |
| 86 | + self.state = EscapeUnicodeState::Done; |
| 87 | + Some('}') |
| 88 | + } |
| 89 | + EscapeUnicodeState::Done => None, |
| 90 | + } |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +// FIXME: This was copied from core/char.rs because it is currenty unstable. |
| 95 | +pub struct EscapeDefault { |
| 96 | + state: EscapeDefaultState |
| 97 | +} |
| 98 | + |
| 99 | +enum EscapeDefaultState { |
| 100 | + Backslash(char), |
| 101 | + Char(char), |
| 102 | + Done, |
| 103 | + Unicode(EscapeUnicode), |
| 104 | +} |
| 105 | + |
| 106 | +impl Iterator for EscapeDefault { |
| 107 | + type Item = char; |
| 108 | + |
| 109 | + fn next(&mut self) -> Option<char> { |
| 110 | + match self.state { |
| 111 | + EscapeDefaultState::Backslash(c) => { |
| 112 | + self.state = EscapeDefaultState::Char(c); |
| 113 | + Some('\\') |
| 114 | + } |
| 115 | + EscapeDefaultState::Char(c) => { |
| 116 | + self.state = EscapeDefaultState::Done; |
| 117 | + Some(c) |
| 118 | + } |
| 119 | + EscapeDefaultState::Done => None, |
| 120 | + EscapeDefaultState::Unicode(ref mut iter) => iter.next() |
| 121 | + } |
| 122 | + } |
| 123 | +} |
0 commit comments