Skip to content

Add num_bytes method to CStr16 #274

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 23, 2021
Merged
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
16 changes: 16 additions & 0 deletions src/data_types/strs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,11 @@ impl CStr16 {
}
}

/// Get the number of bytes in the string (including the trailing null character).
pub fn num_bytes(&self) -> usize {
self.0.len() * 2
}

/// Writes each [`Char16`] as a [´char´] (4 bytes long in Rust language) into the buffer.
/// It is up the the implementer of [`core::fmt::Write`] to convert the char to a string
/// with proper encoding/charset. For example, in the case of [`alloc::string::String`]
Expand Down Expand Up @@ -236,3 +241,14 @@ impl fmt::Display for CStr16 {
Ok(())
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_cstr16_num_bytes() {
let s = CStr16::from_u16_with_nul(&[65, 66, 67, 0]).unwrap();
assert_eq!(s.num_bytes(), 8);
}
}