|
|
| @@ -3,7 +3,7 @@ |
| 3 | //! # Examples |
3 | //! # Examples |
| 4 | //!``` |
4 | //!``` |
| 5 | //!let hex_string = "1a2b3c"; |
5 | //!let hex_string = "1a2b3c"; |
| 6 | //!let data = hex::decode_hex(&hex_string).unwrap(); |
6 | //!let data = hex::decode(&hex_string).unwrap(); |
| 7 | //!``` |
7 | //!``` |
| 8 | |
8 | |
| 9 | use std::fmt::Write; |
9 | use std::fmt::Write; |
| @@ -121,11 +121,11 @@ fn append_byte_hex_to_string(byte: u8, string: &mut String) { |
| 121 | /// |
121 | /// |
| 122 | ///``` |
122 | ///``` |
| 123 | ///let hex_string = "12ffbc"; |
123 | ///let hex_string = "12ffbc"; |
| 124 | ///for byte in hex::decode_hex_iter(&hex_string) { |
124 | ///for byte in hex::decode_iter(&hex_string) { |
| 125 | /// //do something |
125 | /// //do something |
| 126 | ///} |
126 | ///} |
| 127 | ///``` |
127 | ///``` |
| 128 | pub fn decode_hex_iter<T>(string: &T) -> impl Iterator<Item = u8> + '_ |
128 | pub fn decode_iter<T>(string: &T) -> impl Iterator<Item = u8> + '_ |
| 129 | where |
129 | where |
| 130 | T: AsRef<[u8]> + ?Sized, |
130 | T: AsRef<[u8]> + ?Sized, |
| 131 | { |
131 | { |
| @@ -137,11 +137,11 @@ where |
| 137 | /// |
137 | /// |
| 138 | ///``` |
138 | ///``` |
| 139 | ///let data = &[0x11, 0xbc, 0x22]; |
139 | ///let data = &[0x11, 0xbc, 0x22]; |
| 140 | ///for c in hex::encode_hex_iter(&data) { |
140 | ///for c in hex::encode_iter(&data) { |
| 141 | /// //do something with c |
141 | /// //do something with c |
| 142 | ///} |
142 | ///} |
| 143 | ///``` |
143 | ///``` |
| 144 | pub fn encode_hex_iter<T>(data: &T) -> impl Iterator<Item = char> + '_ |
144 | pub fn encode_iter<T>(data: &T) -> impl Iterator<Item = char> + '_ |
| 145 | where |
145 | where |
| 146 | T: AsRef<[u8]> + ?Sized, |
146 | T: AsRef<[u8]> + ?Sized, |
| 147 | { |
147 | { |
| @@ -152,9 +152,9 @@ where |
| 152 | /// |
152 | /// |
| 153 | ///``` |
153 | ///``` |
| 154 | ///let hex_string = "ffaa11"; |
154 | ///let hex_string = "ffaa11"; |
| 155 | ///let data = hex::decode_hex(&hex_string).unwrap(); |
155 | ///let data = hex::decode(&hex_string).unwrap(); |
| 156 | ///``` |
156 | ///``` |
| 157 | pub fn decode_hex<T: AsRef<[u8]>>(string: T) -> Result<Vec<u8>, HexDecodeError> { |
157 | pub fn decode<T: AsRef<[u8]>>(string: T) -> Result<Vec<u8>, HexDecodeError> { |
| 158 | let chars = string.as_ref(); |
158 | let chars = string.as_ref(); |
| 159 | if chars.len() % 2 != 0 { |
159 | if chars.len() % 2 != 0 { |
| 160 | return Err(HexDecodeError::InvalidByteCount); |
160 | return Err(HexDecodeError::InvalidByteCount); |
| @@ -177,10 +177,10 @@ pub fn decode_hex<T: AsRef<[u8]>>(string: T) -> Result<Vec<u8>, HexDecodeError> |
| 177 | /// |
177 | /// |
| 178 | ///``` |
178 | ///``` |
| 179 | ///let data = &[0x11, 0x12, 0x13]; |
179 | ///let data = &[0x11, 0x12, 0x13]; |
| 180 | ///let string = hex::encode_hex(&data); |
180 | ///let string = hex::encode(&data); |
| 181 | ///assert_eq!(string, "111213"); |
181 | ///assert_eq!(string, "111213"); |
| 182 | ///``` |
182 | ///``` |
| 183 | pub fn encode_hex<T: AsRef<[u8]>>(data: T) -> String { |
183 | pub fn encode<T: AsRef<[u8]>>(data: T) -> String { |
| 184 | let mut string = String::new(); |
184 | let mut string = String::new(); |
| 185 | let data_ref = data.as_ref(); |
185 | let data_ref = data.as_ref(); |
| 186 | for b in data_ref.iter() { |
186 | for b in data_ref.iter() { |
| @@ -196,7 +196,7 @@ where |
| 196 | T: AsRef<[u8]>, |
196 | T: AsRef<[u8]>, |
| 197 | { |
197 | { |
| 198 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
198 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 199 | for c in encode_hex_iter(self.0.as_ref()) { |
199 | for c in encode_iter(self.0.as_ref()) { |
| 200 | f.write_char(c)?; |
200 | f.write_char(c)?; |
| 201 | } |
201 | } |
| 202 | Ok(()) |
202 | Ok(()) |
| @@ -214,14 +214,14 @@ mod tests { |
| 214 | #[test] |
214 | #[test] |
| 215 | fn decode_zero_bytes_test() { |
215 | fn decode_zero_bytes_test() { |
| 216 | let string = ""; |
216 | let string = ""; |
| 217 | let data = decode_hex(&string).unwrap(); |
217 | let data = decode(&string).unwrap(); |
| 218 | assert_eq!(0, data.len()); |
218 | assert_eq!(0, data.len()); |
| 219 | } |
219 | } |
| 220 | |
220 | |
| 221 | #[test] |
221 | #[test] |
| 222 | fn decode_one_byte_test() { |
222 | fn decode_one_byte_test() { |
| 223 | let string = "ff"; |
223 | let string = "ff"; |
| 224 | let data = decode_hex(&string).unwrap(); |
224 | let data = decode(&string).unwrap(); |
| 225 | assert_eq!(1, data.len()); |
225 | assert_eq!(1, data.len()); |
| 226 | assert_eq!(0xff, data[0]); |
226 | assert_eq!(0xff, data[0]); |
| 227 | } |
227 | } |
| @@ -229,7 +229,7 @@ mod tests { |
| 229 | #[test] |
229 | #[test] |
| 230 | fn decode_multiple_bytes_test() { |
230 | fn decode_multiple_bytes_test() { |
| 231 | let string = "ffaabbccdd1122"; |
231 | let string = "ffaabbccdd1122"; |
| 232 | let data = decode_hex(&string).unwrap(); |
232 | let data = decode(&string).unwrap(); |
| 233 | assert_eq!(7, data.len()); |
233 | assert_eq!(7, data.len()); |
| 234 | assert_eq!(&[0xff, 0xaa, 0xbb, 0xcc, 0xdd, 0x11, 0x22], data.as_slice()); |
234 | assert_eq!(&[0xff, 0xaa, 0xbb, 0xcc, 0xdd, 0x11, 0x22], data.as_slice()); |
| 235 | } |
235 | } |
| @@ -237,7 +237,7 @@ mod tests { |
| 237 | #[test] |
237 | #[test] |
| 238 | fn decode_invalid_character_test() { |
238 | fn decode_invalid_character_test() { |
| 239 | let string = "ffaabz"; |
239 | let string = "ffaabz"; |
| 240 | match decode_hex(&string) { |
240 | match decode(&string) { |
| 241 | Err(HexDecodeError::InvalidHexCharacter) => {} |
241 | Err(HexDecodeError::InvalidHexCharacter) => {} |
| 242 | _ => panic!("Invalid result returned"), |
242 | _ => panic!("Invalid result returned"), |
| 243 | } |
243 | } |
| @@ -246,7 +246,7 @@ mod tests { |
| 246 | #[test] |
246 | #[test] |
| 247 | fn decode_invalid_character_length() { |
247 | fn decode_invalid_character_length() { |
| 248 | let string = "ffaab"; |
248 | let string = "ffaab"; |
| 249 | match decode_hex(&string) { |
249 | match decode(&string) { |
| 250 | Err(HexDecodeError::InvalidByteCount) => {} |
250 | Err(HexDecodeError::InvalidByteCount) => {} |
| 251 | _ => panic!("Invalid result returned"), |
251 | _ => panic!("Invalid result returned"), |
| 252 | } |
252 | } |
| @@ -255,21 +255,21 @@ mod tests { |
| 255 | #[test] |
255 | #[test] |
| 256 | fn encode_zero_bytes() { |
256 | fn encode_zero_bytes() { |
| 257 | let data: &[u8; 0] = &[]; |
257 | let data: &[u8; 0] = &[]; |
| 258 | let string = encode_hex(&data); |
258 | let string = encode(&data); |
| 259 | assert_eq!(string, ""); |
259 | assert_eq!(string, ""); |
| 260 | } |
260 | } |
| 261 | |
261 | |
| 262 | #[test] |
262 | #[test] |
| 263 | fn encode_one_byte() { |
263 | fn encode_one_byte() { |
| 264 | let data: &[u8; 1] = &[0xa1]; |
264 | let data: &[u8; 1] = &[0xa1]; |
| 265 | let string = encode_hex(&data); |
265 | let string = encode(&data); |
| 266 | assert_eq!(string, "a1"); |
266 | assert_eq!(string, "a1"); |
| 267 | } |
267 | } |
| 268 | |
268 | |
| 269 | #[test] |
269 | #[test] |
| 270 | fn encode_multiple_bytes() { |
270 | fn encode_multiple_bytes() { |
| 271 | let data: &[u8; 5] = &[0xaa, 0xbb, 0xcc, 0xdd, 0xee]; |
271 | let data: &[u8; 5] = &[0xaa, 0xbb, 0xcc, 0xdd, 0xee]; |
| 272 | let string = encode_hex(&data); |
272 | let string = encode(&data); |
| 273 | assert_eq!(string, "aabbccddee"); |
273 | assert_eq!(string, "aabbccddee"); |
| 274 | } |
274 | } |
| 275 | |
275 | |
| @@ -277,21 +277,21 @@ mod tests { |
| 277 | fn owned_string_decode() { |
277 | fn owned_string_decode() { |
| 278 | //this test just needs to compile |
278 | //this test just needs to compile |
| 279 | let string = String::from("aabbccddee"); |
279 | let string = String::from("aabbccddee"); |
| 280 | let _ = decode_hex(&string); |
280 | let _ = decode(&string); |
| 281 | let _ = decode_hex(string); |
281 | let _ = decode(string); |
| 282 | } |
282 | } |
| 283 | |
283 | |
| 284 | #[test] |
284 | #[test] |
| 285 | fn iter_decode_test() { |
285 | fn iter_decode_test() { |
| 286 | let string = "aabbcc"; |
286 | let string = "aabbcc"; |
| 287 | let data: Vec<u8> = decode_hex_iter(&string).collect(); |
287 | let data: Vec<u8> = decode_iter(&string).collect(); |
| 288 | assert_eq!(&[0xaa, 0xbb, 0xcc], data.as_slice()); |
288 | assert_eq!(&[0xaa, 0xbb, 0xcc], data.as_slice()); |
| 289 | } |
289 | } |
| 290 | |
290 | |
| 291 | #[test] |
291 | #[test] |
| 292 | fn iter_encode_test() { |
292 | fn iter_encode_test() { |
| 293 | let data = &[0xff, 0xaa, 0x12, 0x45]; |
293 | let data = &[0xff, 0xaa, 0x12, 0x45]; |
| 294 | let string: String = encode_hex_iter(&data).collect(); |
294 | let string: String = encode_iter(&data).collect(); |
| 295 | assert_eq!(string, "ffaa1245"); |
295 | assert_eq!(string, "ffaa1245"); |
| 296 | } |
296 | } |
| 297 | |
297 | |
|