File tree 5 files changed +60
-1
lines changed
5 files changed +60
-1
lines changed Original file line number Diff line number Diff line change @@ -63,6 +63,10 @@ fn main() {
63
63
println ! ( "cargo:rustc-cfg=no_source_text" ) ;
64
64
}
65
65
66
+ if rustc < 79 {
67
+ println ! ( "cargo:rustc-cfg=no_literal_byte_character" ) ;
68
+ }
69
+
66
70
if !cfg ! ( feature = "proc-macro" ) {
67
71
println ! ( "cargo:rerun-if-changed=build.rs" ) ;
68
72
return ;
Original file line number Diff line number Diff line change @@ -926,7 +926,7 @@ impl Debug for Ident {
926
926
927
927
#[ derive( Clone ) ]
928
928
pub ( crate ) struct Literal {
929
- repr : String ,
929
+ pub ( crate ) repr : String ,
930
930
span : Span ,
931
931
}
932
932
@@ -1048,6 +1048,25 @@ impl Literal {
1048
1048
Literal :: _new ( repr)
1049
1049
}
1050
1050
1051
+ pub fn byte_character ( byte : u8 ) -> Literal {
1052
+ let mut repr = "b'" . to_string ( ) ;
1053
+ #[ allow( clippy:: match_overlapping_arm) ]
1054
+ match byte {
1055
+ b'\0' => repr. push_str ( r"\0" ) ,
1056
+ b'\t' => repr. push_str ( r"\t" ) ,
1057
+ b'\n' => repr. push_str ( r"\n" ) ,
1058
+ b'\r' => repr. push_str ( r"\r" ) ,
1059
+ b'\'' => repr. push_str ( "\\ \' " ) ,
1060
+ b'\\' => repr. push_str ( r"\\" ) ,
1061
+ b'\x20' ..=b'\x7E' => repr. push ( byte as char ) ,
1062
+ _ => {
1063
+ let _ = write ! ( repr, r"\x{:02X}" , byte) ;
1064
+ }
1065
+ }
1066
+ repr. push ( '\'' ) ;
1067
+ Literal :: _new ( repr)
1068
+ }
1069
+
1051
1070
pub fn byte_string ( bytes : & [ u8 ] ) -> Literal {
1052
1071
let mut repr = "b\" " . to_string ( ) ;
1053
1072
let mut bytes = bytes. iter ( ) ;
Original file line number Diff line number Diff line change @@ -1234,6 +1234,11 @@ impl Literal {
1234
1234
Literal :: _new ( imp:: Literal :: character ( ch) )
1235
1235
}
1236
1236
1237
+ /// Byte character literal.
1238
+ pub fn byte_character ( byte : u8 ) -> Literal {
1239
+ Literal :: _new ( imp:: Literal :: byte_character ( byte) )
1240
+ }
1241
+
1237
1242
/// Byte string literal.
1238
1243
pub fn byte_string ( s : & [ u8 ] ) -> Literal {
1239
1244
Literal :: _new ( imp:: Literal :: byte_string ( s) )
Original file line number Diff line number Diff line change @@ -862,6 +862,25 @@ impl Literal {
862
862
}
863
863
}
864
864
865
+ pub fn byte_character ( byte : u8 ) -> Literal {
866
+ if inside_proc_macro ( ) {
867
+ Literal :: Compiler ( {
868
+ #[ cfg( not( no_literal_byte_character) ) ]
869
+ {
870
+ proc_macro:: Literal :: byte_character ( byte)
871
+ }
872
+
873
+ #[ cfg( no_literal_byte_character) ]
874
+ {
875
+ let fallback = fallback:: Literal :: byte_character ( byte) ;
876
+ fallback. repr . parse :: < proc_macro:: Literal > ( ) . unwrap ( )
877
+ }
878
+ } )
879
+ } else {
880
+ Literal :: Fallback ( fallback:: Literal :: byte_character ( byte) )
881
+ }
882
+ }
883
+
865
884
pub fn byte_string ( bytes : & [ u8 ] ) -> Literal {
866
885
if inside_proc_macro ( ) {
867
886
Literal :: Compiler ( proc_macro:: Literal :: byte_string ( bytes) )
Original file line number Diff line number Diff line change @@ -132,6 +132,18 @@ fn literal_raw_string() {
132
132
. unwrap_err ( ) ;
133
133
}
134
134
135
+ #[ test]
136
+ fn literal_byte_character ( ) {
137
+ assert_eq ! ( Literal :: byte_character( b'\0' ) . to_string( ) , r"b'\0'" ) ;
138
+ assert_eq ! ( Literal :: byte_character( b'\t' ) . to_string( ) , r"b'\t'" ) ;
139
+ assert_eq ! ( Literal :: byte_character( b'\n' ) . to_string( ) , r"b'\n'" ) ;
140
+ assert_eq ! ( Literal :: byte_character( b'\r' ) . to_string( ) , r"b'\r'" ) ;
141
+ assert_eq ! ( Literal :: byte_character( b'\'' ) . to_string( ) , r"b'\''" ) ;
142
+ assert_eq ! ( Literal :: byte_character( b'\\' ) . to_string( ) , r"b'\\'" ) ;
143
+ assert_eq ! ( Literal :: byte_character( b'\x1f' ) . to_string( ) , r"b'\x1F'" ) ;
144
+ assert_eq ! ( Literal :: byte_character( b'"' ) . to_string( ) , "b'\" '" ) ;
145
+ }
146
+
135
147
#[ test]
136
148
fn literal_byte_string ( ) {
137
149
assert_eq ! ( Literal :: byte_string( b"" ) . to_string( ) , "b\" \" " ) ;
You can’t perform that action at this time.
0 commit comments