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 {
165
165
}
166
166
}
167
167
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
+
168
173
/// Writes each [`Char16`] as a [´char´] (4 bytes long in Rust language) into the buffer.
169
174
/// It is up the the implementer of [`core::fmt::Write`] to convert the char to a string
170
175
/// with proper encoding/charset. For example, in the case of [`core::alloc::string::String`]
@@ -234,3 +239,14 @@ impl fmt::Display for CStr16 {
234
239
Ok ( ( ) )
235
240
}
236
241
}
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
+ }
You can’t perform that action at this time.
0 commit comments