@@ -57,6 +57,13 @@ impl InnerWidthMapping {
57
57
}
58
58
}
59
59
60
+ /// Whether the input string is a literal. If yes, it contains the inner width mappings.
61
+ #[ derive( Clone , PartialEq , Eq ) ]
62
+ enum InputStringKind {
63
+ NotALiteral ,
64
+ Literal { width_mappings : Vec < InnerWidthMapping > } ,
65
+ }
66
+
60
67
/// The type of format string that we are parsing.
61
68
#[ derive( Copy , Clone , Debug , Eq , PartialEq ) ]
62
69
pub enum ParseMode {
@@ -307,7 +314,11 @@ impl<'a> Parser<'a> {
307
314
append_newline : bool ,
308
315
mode : ParseMode ,
309
316
) -> Parser < ' a > {
310
- let ( width_map, is_literal) = find_width_map_from_snippet ( s, snippet, style) ;
317
+ let input_string_kind = find_width_map_from_snippet ( s, snippet, style) ;
318
+ let ( width_map, is_literal) = match input_string_kind {
319
+ InputStringKind :: Literal { width_mappings } => ( width_mappings, true ) ,
320
+ InputStringKind :: NotALiteral => ( Vec :: new ( ) , false ) ,
321
+ } ;
311
322
Parser {
312
323
mode,
313
324
input : s,
@@ -848,14 +859,14 @@ fn find_width_map_from_snippet(
848
859
input : & str ,
849
860
snippet : Option < string:: String > ,
850
861
str_style : Option < usize > ,
851
- ) -> ( Vec < InnerWidthMapping > , bool ) {
862
+ ) -> InputStringKind {
852
863
let snippet = match snippet {
853
864
Some ( ref s) if s. starts_with ( '"' ) || s. starts_with ( "r\" " ) || s. starts_with ( "r#" ) => s,
854
- _ => return ( vec ! [ ] , false ) ,
865
+ _ => return InputStringKind :: NotALiteral ,
855
866
} ;
856
867
857
868
if str_style. is_some ( ) {
858
- return ( vec ! [ ] , true ) ;
869
+ return InputStringKind :: Literal { width_mappings : Vec :: new ( ) } ;
859
870
}
860
871
861
872
// Strip quotes.
@@ -868,15 +879,15 @@ fn find_width_map_from_snippet(
868
879
// Alternatively, we could just count the trailing newlines and only trim one from the input if they don't match up.
869
880
let input_no_nl = input. trim_end_matches ( '\n' ) ;
870
881
let Ok ( unescaped) = unescape_string ( snippet) else {
871
- return ( vec ! [ ] , false ) ;
882
+ return InputStringKind :: NotALiteral ;
872
883
} ;
873
884
874
885
let unescaped_no_nl = unescaped. trim_end_matches ( '\n' ) ;
875
886
876
887
if unescaped_no_nl != input_no_nl {
877
888
// The source string that we're pointing at isn't our input, so spans pointing at it will be incorrect.
878
889
// This can for example happen with proc macros that respan generated literals.
879
- return ( vec ! [ ] , false ) ;
890
+ return InputStringKind :: NotALiteral ;
880
891
}
881
892
882
893
let mut s = snippet. char_indices ( ) ;
@@ -958,7 +969,7 @@ fn find_width_map_from_snippet(
958
969
}
959
970
}
960
971
961
- ( width_mappings, true )
972
+ InputStringKind :: Literal { width_mappings }
962
973
}
963
974
964
975
fn unescape_string ( string : & str ) -> Result < string:: String , unescape:: EscapeError > {
0 commit comments