| 1 | use craterust-analyzer cargo ucs2 0.3.3 crate/::{ucs2_from_utf8_at_offsetrust-analyzer cargo ucs2 0.3.3 ucs2_from_utf8_at_offset()., Errorrust-analyzer cargo ucs2 0.3.3 Error#}; |
| 2 | |
| 3 | /// Count the number of UCS-2 characters in a string. Return an error if |
| 4 | /// the string cannot be encoded in UCS-2. |
| 5 | pub const fn str_num_ucs2_charsrust-analyzer cargo ucs2 0.3.3 macros/str_num_ucs2_chars().(s: &str) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<usize, Errorrust-analyzer cargo ucs2 0.3.3 Error#> { |
| 6 | let bytes = s.as_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]as_bytes().(); |
| 7 | let len = bytes.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().(); |
| 8 | |
| 9 | let mut offset = 0; |
| 10 | let mut num_ucs2_chars = 0; |
| 11 | |
| 12 | while offset 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 { |
| 13 | // SAFETY: `bytes` is valid UTF-8. |
| 14 | match unsafe { ucs2_from_utf8_at_offsetrust-analyzer cargo ucs2 0.3.3 ucs2_from_utf8_at_offset().(bytes, offset) } { |
| 15 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(ch) => { |
| 16 | offset 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().ch.num_bytesrust-analyzer cargo ucs2 0.3.3 Ucs2CharFromUtf8#num_bytes. as usize; |
| 17 | num_ucs2_chars 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; |
| 18 | } |
| 19 | Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(err) => { |
| 20 | return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(err); |
| 21 | } |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(num_ucs2_chars) |
| 26 | } |
| 27 | |
| 28 | /// Convert a `str` into a null-terminated UCS-2 character array. |
| 29 | pub const fn str_to_ucs2rust-analyzer cargo ucs2 0.3.3 macros/str_to_ucs2().<const N: usize>(s: &str) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<[u16; N], Errorrust-analyzer cargo ucs2 0.3.3 Error#> { |
| 30 | let bytes = s.as_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]as_bytes().(); |
| 31 | let len = bytes.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().(); |
| 32 | |
| 33 | let mut output = [0; N]; |
| 34 | |
| 35 | let mut output_offset = 0; |
| 36 | let mut input_offset = 0; |
| 37 | while input_offset 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 { |
| 38 | // SAFETY: `bytes` is valid UTF-8. |
| 39 | match unsafe { ucs2_from_utf8_at_offsetrust-analyzer cargo ucs2 0.3.3 ucs2_from_utf8_at_offset().(bytes, input_offset) } { |
| 40 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(ch) => { |
| 41 | if ch.valrust-analyzer cargo ucs2 0.3.3 Ucs2CharFromUtf8#val. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u16][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u16][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u16][`PartialEq<Self>`]eq().0 { |
| 42 | panicrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/panic!!("interior null character"); |
| 43 | } else { |
| 44 | output[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`IndexMut<I>`]index_mut().output_offset]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`IndexMut<I>`]index_mut(). = ch.valrust-analyzer cargo ucs2 0.3.3 Ucs2CharFromUtf8#val.; |
| 45 | output_offset 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; |
| 46 | input_offset 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().ch.num_bytesrust-analyzer cargo ucs2 0.3.3 Ucs2CharFromUtf8#num_bytes. as usize; |
| 47 | } |
| 48 | } |
| 49 | Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(err) => { |
| 50 | return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(err); |
| 51 | } |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | // The output array must be one bigger than the converted string, |
| 56 | // to leave room for the trailing null character. |
| 57 | if output_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][`PartialEq<Self>`]ne().!=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]ne(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]ne().N { |
| 58 | panicrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/panic!!("incorrect array length"); |
| 59 | } |
| 60 | |
| 61 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(output) |
| 62 | } |
| 63 | |
| 64 | /// Encode a string as UCS-2 with a trailing null character. |
| 65 | /// |
| 66 | /// The encoding is done at compile time, so the result can be used in a |
| 67 | /// `const` item. The type returned by the macro is a `[u16; N]` array; |
| 68 | /// to avoid having to specify what `N` is in a `const` item, take a |
| 69 | /// reference and store it as `&[u16]`. |
| 70 | /// |
| 71 | /// # Example |
| 72 | /// |
| 73 | /// ``` |
| 74 | /// use ucs2::ucs2_cstr; |
| 75 | /// |
| 76 | /// const S: &[u16] = &ucs2_cstr!("abc"); |
| 77 | /// assert_eq!(S, [97, 98, 99, 0]); |
| 78 | /// ``` |
| 79 | #[macro_export] |
| 80 | macro_rules! ucs2_cstrrust-analyzer cargo ucs2 0.3.3 macros/ucs2_cstr! { |
| 81 | ($s:literal) => {{ |
| 82 | // Use `const` values here to force errors to happen at compile |
| 83 | // time. |
| 84 | |
| 85 | const NUM_CHARS: usize = match $crate::str_num_ucs2_chars($s) { |
| 86 | // Add one for the null char. |
| 87 | Ok(num) => num + 1, |
| 88 | Err(_) => panic!("input contains a character which cannot be represented in UCS-2"), |
| 89 | }; |
| 90 | |
| 91 | const VAL: [u16; NUM_CHARS] = match $crate::str_to_ucs2($s) { |
| 92 | Ok(val) => val, |
| 93 | // The string was already checked by `str_num_ucs2_chars`, |
| 94 | // so this error is unreachable. |
| 95 | Err(_) => { |
| 96 | unreachable!(); |
| 97 | } |
| 98 | }; |
| 99 | VAL |
| 100 | }}; |
| 101 | } |
| 102 | |
| 103 | #[cfg(test)] |
| 104 | mod testsrust-analyzer cargo ucs2 0.3.3 macros/tests/ { |
| 105 | use superrust-analyzer cargo ucs2 0.3.3 macros/::*; |
| 106 | |
| 107 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 108 | fn test_str_num_charsrust-analyzer cargo ucs2 0.3.3 macros/tests/test_str_num_chars().() { |
| 109 | // Some of the strings here are from https://www.kermitproject.org/utf8.html. |
| 110 | |
| 111 | // One-byte chars. |
| 112 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(str_num_ucs2_charsrust-analyzer cargo ucs2 0.3.3 macros/str_num_ucs2_chars().("abc"), Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(3)); |
| 113 | // Two-byte chars. |
| 114 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(str_num_ucs2_charsrust-analyzer cargo ucs2 0.3.3 macros/str_num_ucs2_chars().("Τη γλώσσα μου έδωσαν ελληνική"), Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(29)); |
| 115 | // Three-byte chars. |
| 116 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(str_num_ucs2_charsrust-analyzer cargo ucs2 0.3.3 macros/str_num_ucs2_chars().("ვეპხის ტყაოსანი შოთა რუსთაველი"), Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(30)); |
| 117 | // Four-byte chars. |
| 118 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(str_num_ucs2_charsrust-analyzer cargo ucs2 0.3.3 macros/str_num_ucs2_chars().("😎🔥"), 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#)); |
| 119 | } |
| 120 | |
| 121 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 122 | fn test_ucs2_cstrrust-analyzer cargo ucs2 0.3.3 macros/tests/test_ucs2_cstr().() { |
| 123 | let s = ucs2_cstrrust-analyzer cargo ucs2 0.3.3 macros/ucs2_cstr!!("abc"); |
| 124 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(s, [97, 98, 99, 0]); |
| 125 | } |
| 126 | } |