Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,27 @@ pub fn encode<T: AsRef<[u8]>>(data: T) -> String {
data.encode_hex()
}

/// Encodes `data` as hex string using lowercase characters, appending to target string.
///
/// This is otherwise the same as [`encode`]. One reason to use this function
/// is that if you are performing multiple encodings on distinct data in
/// a loop, this will allow reusing the allocation of a string.
///
/// Alternatively, this is also more efficient to use when you have an
/// existing string and just want to append to it.
///
/// # Example
///
/// ```
/// let mut s = "The hex encoding is: ".to_string();
/// hex::encode_to("Hello world!", &mut s);
/// assert_eq!(s, "The hex encoding is: 48656c6c6f20776f726c6421");
/// ```
#[cfg(feature = "alloc")]
pub fn encode_to<T: AsRef<[u8]>>(data: T, s: &mut String) {
s.extend(BytesToHexChars::new(data.as_ref(), HEX_CHARS_LOWER))
}

/// Encodes `data` as hex string using uppercase characters.
///
/// Apart from the characters' casing, this works exactly like `encode()`.
Expand All @@ -276,6 +297,22 @@ pub fn encode_upper<T: AsRef<[u8]>>(data: T) -> String {
data.encode_hex_upper()
}

/// Encodes `data` as hex string using uppercase characters, appending to target string.
///
/// This is the same as [`encode_to`], but uses uppercase characters.
///
/// # Example
///
/// ```
/// let mut s = "The hex encoding is: ".to_string();
/// hex::encode_upper_to("Hello world!", &mut s);
/// assert_eq!(s, "The hex encoding is: 48656C6C6F20776F726C6421");
/// ```
#[cfg(feature = "alloc")]
pub fn encode_upper_to<T: AsRef<[u8]>>(data: T, s: &mut String) {
s.extend(BytesToHexChars::new(data.as_ref(), HEX_CHARS_UPPER))
}

/// Decodes a hex string into raw bytes.
///
/// Both, upper and lower case characters are valid in the input string and can
Expand Down
2 changes: 1 addition & 1 deletion tests/version-number.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![allow(non_fmt_panic)]
#![allow(non_fmt_panics)]

#[test]
fn test_readme_deps() {
Expand Down