Skip to content

Commit ba9504f

Browse files
Add num_bytes method to CStr16 (#274)
1 parent 93c2991 commit ba9504f

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 [`alloc::string::String`]
@@ -236,3 +241,14 @@ impl fmt::Display for CStr16 {
236241
Ok(())
237242
}
238243
}
244+
245+
#[cfg(test)]
246+
mod tests {
247+
use super::*;
248+
249+
#[test]
250+
fn test_cstr16_num_bytes() {
251+
let s = CStr16::from_u16_with_nul(&[65, 66, 67, 0]).unwrap();
252+
assert_eq!(s.num_bytes(), 8);
253+
}
254+
}

0 commit comments

Comments
 (0)