| 1 | //! Utility functions for the UCS-2 character encoding. |
| 2 | |
| 3 | #![no_std] |
| 4 | #![deny(missing_docs)] |
| 5 | #![deny(clippy::all)] |
| 6 | |
| 7 | mod macrosrust-analyzer cargo ucs2 0.3.3 macros/; |
| 8 | |
| 9 | /// These need to be public for the `ucs2_cstr!` macro, but are not |
| 10 | /// intended to be called directly. |
| 11 | #[doc(hidden)] |
| 12 | pub use macrosrust-analyzer cargo ucs2 0.3.3 macros/::{str_num_ucs2_charsrust-analyzer cargo ucs2 0.3.3 macros/str_num_ucs2_chars()., str_to_ucs2rust-analyzer cargo ucs2 0.3.3 macros/str_to_ucs2().}; |
| 13 | |
| 14 | use bit_fieldrust-analyzer cargo bit_field 0.10.3 crate/::BitFieldrust-analyzer cargo bit_field 0.10.3 BitField#; |
| 15 | use corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::fmtrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/::{selfrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/, Displayrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Display#, Formatterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Formatter#}; |
| 16 | |
| 17 | /// Possible errors returned by the API. |
| 18 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, Copyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Copy:, Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:, Ordrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ord:, PartialOrdrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd:, Hashrust-analyzer cargo core https://github.com/rust-lang/rust/library/core hash/macros/Hash:)] |
| 19 | pub enum Errorrust-analyzer cargo ucs2 0.3.3 Error# { |
| 20 | /// Not enough space left in the output buffer. |
| 21 | BufferOverflowrust-analyzer cargo ucs2 0.3.3 Error#BufferOverflow#, |
| 22 | /// Input contained a character which cannot be represented in UCS-2. |
| 23 | MultiByterust-analyzer cargo ucs2 0.3.3 Error#MultiByte#, |
| 24 | } |
| 25 | |
| 26 | impl Displayrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Display# for Errorrust-analyzer cargo ucs2 0.3.3 Error# { |
| 27 | fn fmtrust-analyzer cargo ucs2 0.3.3 impl#[Error][Display]fmt().(&self, f: &mut Formatterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Formatter#<'_>) -> fmtrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/::Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Result# { |
| 28 | match self { |
| 29 | Selfrust-analyzer cargo ucs2 0.3.3 impl#[Error][Display]::BufferOverflowrust-analyzer cargo ucs2 0.3.3 Error#BufferOverflow# => f.write_strrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/impl#[`Formatter<'a>`]write_str().("output buffer is too small"), |
| 30 | Selfrust-analyzer cargo ucs2 0.3.3 impl#[Error][Display]::MultiByterust-analyzer cargo ucs2 0.3.3 Error#MultiByte# => { |
| 31 | f.write_strrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/impl#[`Formatter<'a>`]write_str().("input contains a character which cannot be represented in UCS-2") |
| 32 | } |
| 33 | } |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | type Resultrust-analyzer cargo ucs2 0.3.3 Result#<T> = corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/::Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<T, Errorrust-analyzer cargo ucs2 0.3.3 Error#>; |
| 38 | |
| 39 | /// Value returned by `ucs2_from_utf8_at_offset`. |
| 40 | struct Ucs2CharFromUtf8rust-analyzer cargo ucs2 0.3.3 Ucs2CharFromUtf8# { |
| 41 | /// UCS-2 character. |
| 42 | valrust-analyzer cargo ucs2 0.3.3 Ucs2CharFromUtf8#val.: u16, |
| 43 | /// Number of bytes needed to encode the character in UTF-8. |
| 44 | num_bytesrust-analyzer cargo ucs2 0.3.3 Ucs2CharFromUtf8#num_bytes.: u8, |
| 45 | } |
| 46 | |
| 47 | /// Get a UCS-2 character from a UTF-8 byte slice at the given offset. |
| 48 | /// |
| 49 | /// # Safety |
| 50 | /// |
| 51 | /// The input `bytes` must be valid UTF-8. |
| 52 | const unsafe fn ucs2_from_utf8_at_offsetrust-analyzer cargo ucs2 0.3.3 ucs2_from_utf8_at_offset().(bytes: &[u8], offset: usize) -> Resultrust-analyzer cargo ucs2 0.3.3 Result#<Ucs2CharFromUtf8rust-analyzer cargo ucs2 0.3.3 Ucs2CharFromUtf8#> { |
| 53 | let len = bytes.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().(); |
| 54 | let ch; |
| 55 | let ch_len; |
| 56 | |
| 57 | if bytes[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().offset]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u8][`BitAnd<Self>`]bitand().&rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u8][`BitAnd<Self>`]bitand(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u8][`BitAnd<Self>`]bitand().0b1000_0000 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]eq().0b0000_0000 { |
| 58 | ch = bytes[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().offset]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index(). as u16; |
| 59 | ch_len = 1; |
| 60 | } else if bytes[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().offset]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u8][`BitAnd<Self>`]bitand().&rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u8][`BitAnd<Self>`]bitand(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u8][`BitAnd<Self>`]bitand().0b1110_0000 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]eq().0b1100_0000 { |
| 61 | // 2 byte codepoint |
| 62 | if offset rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().1 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]ge().>=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]ge(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]ge().len { |
| 63 | // safe: len is the length of bytes, |
| 64 | // and bytes is a direct view into the |
| 65 | // buffer of input, which in order to be a valid |
| 66 | // utf-8 string _must_ contain `i + 1`. |
| 67 | unsafe { corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::hintrust-analyzer cargo core https://github.com/rust-lang/rust/library/core hint/::unreachable_uncheckedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core hint/unreachable_unchecked().() } |
| 68 | } |
| 69 | |
| 70 | let a = (bytes[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().offset]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u8][`BitAnd<Self>`]bitand().&rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u8][`BitAnd<Self>`]bitand(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u8][`BitAnd<Self>`]bitand().0b0001_1111) as u16; |
| 71 | let b = (bytes[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().offset rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().1]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u8][`BitAnd<Self>`]bitand().&rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u8][`BitAnd<Self>`]bitand(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u8][`BitAnd<Self>`]bitand().0b0011_1111) as u16; |
| 72 | ch = a rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u16][`Shl<i32>`]shl().<<rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u16][`Shl<i32>`]shl(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u16][`Shl<i32>`]shl().6 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u16][`BitOr<Self>`]bitor().|rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u16][`BitOr<Self>`]bitor(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u16][`BitOr<Self>`]bitor().b; |
| 73 | ch_len = 2; |
| 74 | } else if bytes[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().offset]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u8][`BitAnd<Self>`]bitand().&rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u8][`BitAnd<Self>`]bitand(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u8][`BitAnd<Self>`]bitand().0b1111_0000 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]eq().0b1110_0000 { |
| 75 | // 3 byte codepoint |
| 76 | if offset rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().2 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]ge().>=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]ge(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]ge().len || offset rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().1 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]ge().>=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]ge(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]ge().len { |
| 77 | // safe: impossible utf-8 string. |
| 78 | unsafe { corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::hintrust-analyzer cargo core https://github.com/rust-lang/rust/library/core hint/::unreachable_uncheckedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core hint/unreachable_unchecked().() } |
| 79 | } |
| 80 | |
| 81 | let a = (bytes[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().offset]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u8][`BitAnd<Self>`]bitand().&rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u8][`BitAnd<Self>`]bitand(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u8][`BitAnd<Self>`]bitand().0b0000_1111) as u16; |
| 82 | let b = (bytes[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().offset rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().1]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u8][`BitAnd<Self>`]bitand().&rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u8][`BitAnd<Self>`]bitand(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u8][`BitAnd<Self>`]bitand().0b0011_1111) as u16; |
| 83 | let c = (bytes[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().offset rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().2]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u8][`BitAnd<Self>`]bitand().&rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u8][`BitAnd<Self>`]bitand(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u8][`BitAnd<Self>`]bitand().0b0011_1111) as u16; |
| 84 | ch = a rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u16][`Shl<i32>`]shl().<<rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u16][`Shl<i32>`]shl(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u16][`Shl<i32>`]shl().12 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u16][`BitOr<Self>`]bitor().|rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u16][`BitOr<Self>`]bitor(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u16][`BitOr<Self>`]bitor().b rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u16][`Shl<i32>`]shl().<<rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u16][`Shl<i32>`]shl(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u16][`Shl<i32>`]shl().6 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u16][`BitOr<Self>`]bitor().|rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u16][`BitOr<Self>`]bitor(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u16][`BitOr<Self>`]bitor().c; |
| 85 | ch_len = 3; |
| 86 | } else if bytes[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().offset]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u8][`BitAnd<Self>`]bitand().&rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u8][`BitAnd<Self>`]bitand(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u8][`BitAnd<Self>`]bitand().0b1111_0000 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]eq().0b1111_0000 { |
| 87 | return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(Errorrust-analyzer cargo ucs2 0.3.3 Error#::MultiByterust-analyzer cargo ucs2 0.3.3 Error#MultiByte#); // UTF-16 |
| 88 | } else { |
| 89 | // safe: impossible utf-8 string. |
| 90 | unsafe { corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::hintrust-analyzer cargo core https://github.com/rust-lang/rust/library/core hint/::unreachable_uncheckedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core hint/unreachable_unchecked().() } |
| 91 | } |
| 92 | |
| 93 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Ucs2CharFromUtf8rust-analyzer cargo ucs2 0.3.3 Ucs2CharFromUtf8# { |
| 94 | valrust-analyzer cargo ucs2 0.3.3 Ucs2CharFromUtf8#val.: ch, |
| 95 | num_bytesrust-analyzer cargo ucs2 0.3.3 Ucs2CharFromUtf8#num_bytes.: ch_len, |
| 96 | }) |
| 97 | } |
| 98 | |
| 99 | /// Encodes an input UTF-8 string into a UCS-2 string. |
| 100 | /// |
| 101 | /// The returned `usize` represents the length of the returned buffer, |
| 102 | /// measured in 2-byte characters. |
| 103 | pub fn encoderust-analyzer cargo ucs2 0.3.3 encode().(input: &str, buffer: &mut [u16]) -> Resultrust-analyzer cargo ucs2 0.3.3 Result#<usize> { |
| 104 | let buffer_size = buffer.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().(); |
| 105 | let mut i = 0; |
| 106 | |
| 107 | encode_withrust-analyzer cargo ucs2 0.3.3 encode_with().(input, |ch| { |
| 108 | if i rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]ge().>=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]ge(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]ge().buffer_size { |
| 109 | Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(Errorrust-analyzer cargo ucs2 0.3.3 Error#::BufferOverflowrust-analyzer cargo ucs2 0.3.3 Error#BufferOverflow#) |
| 110 | } else { |
| 111 | buffer[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut().i]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut(). = ch; |
| 112 | i rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().+=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().1; |
| 113 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()) |
| 114 | } |
| 115 | })?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 116 | |
| 117 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(i) |
| 118 | } |
| 119 | |
| 120 | /// Encode UTF-8 string to UCS-2 with a custom callback function. |
| 121 | /// |
| 122 | /// `output` is a function which receives every encoded character. |
| 123 | pub fn encode_withrust-analyzer cargo ucs2 0.3.3 encode_with().<F>(input: &str, mut output: F) -> Resultrust-analyzer cargo ucs2 0.3.3 Result#<()> |
| 124 | where |
| 125 | F: FnMutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/FnMut#(u16) -> Resultrust-analyzer cargo ucs2 0.3.3 Result#<()>, |
| 126 | { |
| 127 | let bytes = input.as_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]as_bytes().(); |
| 128 | let len = bytes.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().(); |
| 129 | let mut i = 0; |
| 130 | |
| 131 | while i rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt().<rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt().len { |
| 132 | // SAFETY: `bytes` is valid UTF-8. |
| 133 | let ch = unsafe { ucs2_from_utf8_at_offsetrust-analyzer cargo ucs2 0.3.3 ucs2_from_utf8_at_offset().(bytes, i) }?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 134 | i rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().+=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().usize::fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/num/impl#[usize][`From<u8>`]from().(ch.num_bytesrust-analyzer cargo ucs2 0.3.3 Ucs2CharFromUtf8#num_bytes.); |
| 135 | output(ch.valrust-analyzer cargo ucs2 0.3.3 Ucs2CharFromUtf8#val.)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 136 | } |
| 137 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()) |
| 138 | } |
| 139 | |
| 140 | /// Decode UCS-2 string to UTF-8 with a custom callback function. |
| 141 | /// |
| 142 | /// `output` is a function which receives every decoded character. |
| 143 | /// Due to the nature of UCS-2, the function can receive an UTF-8 character |
| 144 | /// of up to three bytes, for every input character. |
| 145 | pub fn decode_withrust-analyzer cargo ucs2 0.3.3 decode_with().<F>(input: &[u16], mut output: F) -> Resultrust-analyzer cargo ucs2 0.3.3 Result#<usize> |
| 146 | where |
| 147 | F: FnMutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/FnMut#(&[u8]) -> Resultrust-analyzer cargo ucs2 0.3.3 Result#<()>, |
| 148 | { |
| 149 | let mut written = 0; |
| 150 | |
| 151 | for ch in input.iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().() { |
| 152 | /* |
| 153 | * We need to find how many bytes of UTF-8 this UCS-2 code-point needs. Because UCS-2 can only encode |
| 154 | * the Basic Multilingual Plane, a maximum of three bytes are needed. |
| 155 | */ |
| 156 | if (0x000..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#0x0080).containsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/impl#[`Range<Idx>`]contains().(ch) { |
| 157 | output(&[*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().ch as u8])?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 158 | |
| 159 | written rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().+=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().1; |
| 160 | } else if (0x0080..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#0x0800).containsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/impl#[`Range<Idx>`]contains().(ch) { |
| 161 | let first = 0b1100_0000 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u8][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u8][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u8][`Add<Self>`]add().ch.get_bitsrust-analyzer cargo bit_field 0.10.3 impl#[u16][BitField]get_bits().(6..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#11) as u8; |
| 162 | let last = 0b1000_0000 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u8][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u8][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u8][`Add<Self>`]add().ch.get_bitsrust-analyzer cargo bit_field 0.10.3 impl#[u16][BitField]get_bits().(0..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#6) as u8; |
| 163 | |
| 164 | output(&[first, last])?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 165 | |
| 166 | written rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().+=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().2; |
| 167 | } else { |
| 168 | let first = 0b1110_0000 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u8][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u8][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u8][`Add<Self>`]add().ch.get_bitsrust-analyzer cargo bit_field 0.10.3 impl#[u16][BitField]get_bits().(12..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#16) as u8; |
| 169 | let mid = 0b1000_0000 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u8][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u8][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u8][`Add<Self>`]add().ch.get_bitsrust-analyzer cargo bit_field 0.10.3 impl#[u16][BitField]get_bits().(6..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#12) as u8; |
| 170 | let last = 0b1000_0000 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u8][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u8][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u8][`Add<Self>`]add().ch.get_bitsrust-analyzer cargo bit_field 0.10.3 impl#[u16][BitField]get_bits().(0..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#6) as u8; |
| 171 | |
| 172 | output(&[first, mid, last])?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 173 | |
| 174 | written rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().+=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().3; |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(written) |
| 179 | } |
| 180 | |
| 181 | /// Decode an input UCS-2 string into a UTF-8 string. |
| 182 | /// |
| 183 | /// The returned `usize` represents the length of the returned buffer, |
| 184 | /// in bytes. Due to the nature of UCS-2, the output buffer could end up with |
| 185 | /// three bytes for every character in the input buffer. |
| 186 | pub fn decoderust-analyzer cargo ucs2 0.3.3 decode().(input: &[u16], output: &mut [u8]) -> Resultrust-analyzer cargo ucs2 0.3.3 Result#<usize> { |
| 187 | let buffer_size = output.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().(); |
| 188 | let mut i = 0; |
| 189 | |
| 190 | decode_withrust-analyzer cargo ucs2 0.3.3 decode_with().(input, |bytes| { |
| 191 | if bytes.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq().1 { |
| 192 | // Can be encoded in a single byte |
| 193 | if i rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]ge().>=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]ge(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]ge().buffer_size { |
| 194 | return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(Errorrust-analyzer cargo ucs2 0.3.3 Error#::BufferOverflowrust-analyzer cargo ucs2 0.3.3 Error#BufferOverflow#); |
| 195 | } |
| 196 | |
| 197 | output[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut().i]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut(). = bytes[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().0]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().; |
| 198 | |
| 199 | i rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().+=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().1; |
| 200 | } else if bytes.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq().2 { |
| 201 | // Can be encoded two bytes |
| 202 | if i rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().1 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]ge().>=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]ge(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]ge().buffer_size { |
| 203 | return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(Errorrust-analyzer cargo ucs2 0.3.3 Error#::BufferOverflowrust-analyzer cargo ucs2 0.3.3 Error#BufferOverflow#); |
| 204 | } |
| 205 | |
| 206 | output[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut().i]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut(). = bytes[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().0]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().; |
| 207 | output[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut().i rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().1]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut(). = bytes[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().1]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().; |
| 208 | |
| 209 | i rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().+=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().2; |
| 210 | } else if bytes.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq().3 { |
| 211 | // Can be encoded three bytes |
| 212 | if i rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().2 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]ge().>=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]ge(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]ge().buffer_size { |
| 213 | return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(Errorrust-analyzer cargo ucs2 0.3.3 Error#::BufferOverflowrust-analyzer cargo ucs2 0.3.3 Error#BufferOverflow#); |
| 214 | } |
| 215 | |
| 216 | output[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut().i]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut(). = bytes[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().0]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().; |
| 217 | output[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut().i rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().1]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut(). = bytes[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().1]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().; |
| 218 | output[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut().i rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().2]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut(). = bytes[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().2]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().; |
| 219 | |
| 220 | i rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().+=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().3; |
| 221 | } else { |
| 222 | unreachablerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/unreachable!!("More than three bytes per UCS-2 character."); |
| 223 | } |
| 224 | |
| 225 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()) |
| 226 | }) |
| 227 | } |