1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*!
UCS-2 encoding and decoding.
# Encoding
A unicode code point is represented using [two bytes]() in UCS-2, using always this fixed size.
# Decoding
A UCS-2 code point is decoded into a unicode code point using the the first [two bytes]().
## Representation
**Note**:
* UCS-2 is a subset of UTF-16.
* UCS-2 is capable of ending 65,536 code points. This is the same as the first 65,536 code points of UTF-16.
### Two bytes
**Encoding**: If the unicode code point is less than 0xFFFF, the unicode code point is represented in UTF-16 using only the 16 least significant bits.
**Decoding**: If the UTF-16 code point is less than 0xD800 or greater than 0xDBFF and less than 0xFFFF, the unicode code point is represented using only the 16 least significant bits.
* Unicode code point: `nnnnnnnn|nnnnnnnn|xxxxxxxx|xxxxxxxx`
* UTF-16 code point: `xxxxxxxx|xxxxxxxx`
*/
/// Pretty print the UCS-2 code points in hexadecimal, (binary) and decimal.
///
/// # Parameters
/// * `ucs2_cp`: [`Vec<u16>`] - A vector of UCS-2 code points.
/// * `binary_flag`: [`bool`] - A flag to print the binary representation of the UCS-2 code points.
///
/// # Note
/// The bytes printed in hexadecimal are code points in UCS-2.
// ============================================================================
// ================================ Public API ================================
// ============================================================================
/// Pretty print the UCS-2 encoding in hexadecimal and decimal of a vector of UCS-2 code points.
///
/// # Parameters
/// * `ucs2_cp`: [`Vec<u16>`] - A vector of UCS-2 code points.
///
/// # Note
/// The bytes printed in hexadecimal are code points in UCS-2.
///
/// # Example
/// ```rust
/// use ende::prelude::*;
/// let v: Vec<u16> = vec![0xFFEE];
/// print_ucs2(&v);
/// ```
/// **Output**
/// ```text
/// --------------- UTF-16 encoding of "𐀁" ---------------
/// Hex: [0xFFEE]
/// Dec: [65518]
/// ------------------------------------------------------
/// Pretty print the UCS-2 encoding in hexadecimal and decimal of a vector of UCS-2 code points.
///
/// # Parameters
/// * `ucs2_cp`: [`Vec<u16>`] - A vector of UCS-2 code points.
///
/// # Note
/// The bytes printed in hexadecimal are code points in UCS-2.
///
/// # Example
/// ```rust
/// use ende::prelude::*;
/// let v: Vec<u16> = vec![0xFFEE];
/// print_ucs2_b(&v);
/// ```
/// **Output**
/// ```text
/// --------------- UTF-16 encoding of "𐀀" ---------------
/// Hex: [0xFFEE]
/// Bin: ["1111111111101110"]
/// Dec: [65518]
/// ------------------------------------------------------
/// Encode a vector of unicode code points into a vector of UCS-2 code points.
///
/// # Parameters
/// * `unicode_cp`: [`Vec<u32>`] - A vector of unicode code points.
///
/// # Returns
/// A [`Vec<u16>`] containing the UCS-2 code points.
///
/// # Panics
/// * If the input vector (`unicode_cp`) of unicode code points contains invalid unicode code points.
///
/// # Example
/// ```rust
/// use ende::prelude::*;
/// let v: Vec<u32> = vec![0xFFEE]; // Array of code points in unicode
/// let enc: Vec<u16> = encode_in_ucs2(&v);
/// assert_eq!(enc, vec![0xFFEE]);
/// ```
/// Decode a vector of UCS-2 code points into a vector of unicode code points.
///
/// # Parameters
/// * `ucs2_cp`: [`Vec<u16>`] - A vector of UCS-2 code points.
///
/// # Returns
/// A [`Vec<u32>`] containing the unicode code points.
///
/// # Panics
/// * If the input vector (`ucs2_cp`) of UCS-2 code points contains invalid UCS-2 code points.
///
/// # Example
/// ```rust
/// use ende::prelude::*;
/// let v: Vec<u16> = vec![0xFFEE]; // Array of code points in UCS-2
/// let dec: Vec<u32> = decode_from_ucs2(&v);
/// assert_eq!(dec, vec![0xFFEE]);
/// ```