Skip to content

Commit 3a4a8d9

Browse files
committed
Add Literal::byte_character constructor
1 parent 5bdf9be commit 3a4a8d9

File tree

5 files changed

+60
-1
lines changed

5 files changed

+60
-1
lines changed

build.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ fn main() {
6363
println!("cargo:rustc-cfg=no_source_text");
6464
}
6565

66+
if rustc < 79 {
67+
println!("cargo:rustc-cfg=no_literal_byte_character");
68+
}
69+
6670
if !cfg!(feature = "proc-macro") {
6771
println!("cargo:rerun-if-changed=build.rs");
6872
return;

src/fallback.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -926,7 +926,7 @@ impl Debug for Ident {
926926

927927
#[derive(Clone)]
928928
pub(crate) struct Literal {
929-
repr: String,
929+
pub(crate) repr: String,
930930
span: Span,
931931
}
932932

@@ -1048,6 +1048,25 @@ impl Literal {
10481048
Literal::_new(repr)
10491049
}
10501050

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+
10511070
pub fn byte_string(bytes: &[u8]) -> Literal {
10521071
let mut repr = "b\"".to_string();
10531072
let mut bytes = bytes.iter();

src/lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1234,6 +1234,11 @@ impl Literal {
12341234
Literal::_new(imp::Literal::character(ch))
12351235
}
12361236

1237+
/// Byte character literal.
1238+
pub fn byte_character(byte: u8) -> Literal {
1239+
Literal::_new(imp::Literal::byte_character(byte))
1240+
}
1241+
12371242
/// Byte string literal.
12381243
pub fn byte_string(s: &[u8]) -> Literal {
12391244
Literal::_new(imp::Literal::byte_string(s))

src/wrapper.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -862,6 +862,25 @@ impl Literal {
862862
}
863863
}
864864

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+
865884
pub fn byte_string(bytes: &[u8]) -> Literal {
866885
if inside_proc_macro() {
867886
Literal::Compiler(proc_macro::Literal::byte_string(bytes))

tests/test.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,18 @@ fn literal_raw_string() {
132132
.unwrap_err();
133133
}
134134

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+
135147
#[test]
136148
fn literal_byte_string() {
137149
assert_eq!(Literal::byte_string(b"").to_string(), "b\"\"");

0 commit comments

Comments
 (0)