Skip to content

Commit 2402649

Browse files
Add num_bytes method to CStr16
1 parent d289e0c commit 2402649

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

src/data_types/strs.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,11 @@ impl CStr16 {
165165
}
166166
}
167167

168+
/// Get the number of bytes in the string (including the trailing null character).
169+
pub fn num_bytes(&self) -> usize {
170+
self.0.len() * 2
171+
}
172+
168173
/// Writes each [`Char16`] as a [´char´] (4 bytes long in Rust language) into the buffer.
169174
/// It is up the the implementer of [`core::fmt::Write`] to convert the char to a string
170175
/// with proper encoding/charset. For example, in the case of [`core::alloc::string::String`]
@@ -234,3 +239,14 @@ impl fmt::Display for CStr16 {
234239
Ok(())
235240
}
236241
}
242+
243+
#[cfg(test)]
244+
mod tests {
245+
use super::*;
246+
247+
#[test]
248+
fn test_cstr16_num_bytes() {
249+
let s = CStr16::from_u16_with_nul(&[65, 66, 67, 0]).unwrap();
250+
assert_eq!(s.num_bytes(), 8);
251+
}
252+
}

0 commit comments

Comments
 (0)