File tree Expand file tree Collapse file tree 1 file changed +16
-0
lines changed Expand file tree Collapse file tree 1 file changed +16
-0
lines changed Original file line number Diff line number Diff 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+ }
You can’t perform that action at this time.
0 commit comments