@@ -283,14 +283,28 @@ pub fn cstr8(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
283
283
/// This will throw a compile error if an invalid character is in the passed string.
284
284
///
285
285
/// # Example
286
- /// ```
286
+ /// ```rust
287
287
/// # use uefi_macros::cstr16;
288
+ /// // Empty string
289
+ /// assert_eq!(cstr16!().to_u16_slice_with_nul(), [0]);
290
+ /// assert_eq!(cstr16!("").to_u16_slice_with_nul(), [0]);
291
+ /// // Non-empty string
288
292
/// assert_eq!(cstr16!("test €").to_u16_slice_with_nul(), [116, 101, 115, 116, 32, 8364, 0]);
289
293
/// ```
290
294
#[ proc_macro]
291
295
pub fn cstr16 ( input : proc_macro:: TokenStream ) -> proc_macro:: TokenStream {
296
+ // Accept empty input.
297
+ if input. is_empty ( ) {
298
+ return quote ! ( unsafe { :: uefi:: CStr16 :: from_u16_with_nul_unchecked( & [ 0 ] ) } ) . into ( ) ;
299
+ }
292
300
let input: LitStr = parse_macro_input ! ( input) ;
293
301
let input = input. value ( ) ;
302
+ // Accept "" input.
303
+ if input. is_empty ( ) {
304
+ return quote ! ( unsafe { :: uefi:: CStr16 :: from_u16_with_nul_unchecked( & [ 0 ] ) } ) . into ( ) ;
305
+ }
306
+
307
+ // Accept any non-empty string input.
294
308
match input
295
309
. chars ( )
296
310
. map ( |c| u16:: try_from ( c as u32 ) )
@@ -299,8 +313,11 @@ pub fn cstr16(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
299
313
Ok ( c) => {
300
314
quote ! ( unsafe { :: uefi:: CStr16 :: from_u16_with_nul_unchecked( & [ #( #c) , * , 0 ] ) } ) . into ( )
301
315
}
302
- Err ( _) => syn:: Error :: new_spanned ( input, "invalid character in string" )
303
- . into_compile_error ( )
304
- . into ( ) ,
316
+ Err ( _) => syn:: Error :: new_spanned (
317
+ input,
318
+ "There are UTF-8 characters that can't be transformed to UCS-2 character" ,
319
+ )
320
+ . into_compile_error ( )
321
+ . into ( ) ,
305
322
}
306
323
}
0 commit comments