@@ -258,12 +258,26 @@ pub fn entry(args: TokenStream, input: TokenStream) -> TokenStream {
258
258
/// # Example
259
259
/// ```
260
260
/// # use uefi_macros::cstr8;
261
+ /// // Empty string
262
+ /// assert_eq!(cstr8!().to_u16_slice_with_nul(), [0]);
263
+ /// assert_eq!(cstr8!("").to_u16_slice_with_nul(), [0]);
264
+ /// // Non-empty string
261
265
/// assert_eq!(cstr8!("test").to_bytes_with_nul(), [116, 101, 115, 116, 0]);
262
266
/// ```
263
267
#[ proc_macro]
264
268
pub fn cstr8 ( input : proc_macro:: TokenStream ) -> proc_macro:: TokenStream {
269
+ // Accept empty input.
270
+ if input. is_empty ( ) {
271
+ return quote ! ( unsafe { :: uefi:: CStr16 :: from_u16_with_nul_unchecked( & [ 0 ] ) } ) . into ( ) ;
272
+ }
265
273
let input: LitStr = parse_macro_input ! ( input) ;
266
274
let input = input. value ( ) ;
275
+ // Accept "" input.
276
+ if input. is_empty ( ) {
277
+ return quote ! ( unsafe { :: uefi:: CStr16 :: from_u16_with_nul_unchecked( & [ 0 ] ) } ) . into ( ) ;
278
+ }
279
+
280
+ // Accept any non-empty string input.
267
281
match input
268
282
. chars ( )
269
283
. map ( u8:: try_from)
@@ -283,14 +297,28 @@ pub fn cstr8(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
283
297
/// This will throw a compile error if an invalid character is in the passed string.
284
298
///
285
299
/// # Example
286
- /// ```
300
+ /// ```rust
287
301
/// # use uefi_macros::cstr16;
302
+ /// // Empty string
303
+ /// assert_eq!(cstr16!().to_u16_slice_with_nul(), [0]);
304
+ /// assert_eq!(cstr16!("").to_u16_slice_with_nul(), [0]);
305
+ /// // Non-empty string
288
306
/// assert_eq!(cstr16!("test €").to_u16_slice_with_nul(), [116, 101, 115, 116, 32, 8364, 0]);
289
307
/// ```
290
308
#[ proc_macro]
291
309
pub fn cstr16 ( input : proc_macro:: TokenStream ) -> proc_macro:: TokenStream {
310
+ // Accept empty input.
311
+ if input. is_empty ( ) {
312
+ return quote ! ( unsafe { :: uefi:: CStr16 :: from_u16_with_nul_unchecked( & [ 0 ] ) } ) . into ( ) ;
313
+ }
292
314
let input: LitStr = parse_macro_input ! ( input) ;
293
315
let input = input. value ( ) ;
316
+ // Accept "" input.
317
+ if input. is_empty ( ) {
318
+ return quote ! ( unsafe { :: uefi:: CStr16 :: from_u16_with_nul_unchecked( & [ 0 ] ) } ) . into ( ) ;
319
+ }
320
+
321
+ // Accept any non-empty string input.
294
322
match input
295
323
. chars ( )
296
324
. map ( |c| u16:: try_from ( c as u32 ) )
@@ -299,8 +327,11 @@ pub fn cstr16(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
299
327
Ok ( c) => {
300
328
quote ! ( unsafe { :: uefi:: CStr16 :: from_u16_with_nul_unchecked( & [ #( #c) , * , 0 ] ) } ) . into ( )
301
329
}
302
- Err ( _) => syn:: Error :: new_spanned ( input, "invalid character in string" )
303
- . into_compile_error ( )
304
- . into ( ) ,
330
+ Err ( _) => syn:: Error :: new_spanned (
331
+ input,
332
+ "There are UTF-8 characters that can't be transformed to UCS-2 character" ,
333
+ )
334
+ . into_compile_error ( )
335
+ . into ( ) ,
305
336
}
306
337
}
0 commit comments