Raw byte strings
diff --git a/src/lit.rs b/src/lit.rs
index 7c851ec..6b36315 100644
--- a/src/lit.rs
+++ b/src/lit.rs
@@ -6,7 +6,7 @@
/// A string literal (`"foo"`)
Str(String, StrStyle),
/// A byte string (`b"foo"`)
- ByteStr(Vec<u8>),
+ ByteStr(Vec<u8>, StrStyle),
/// A byte char (`b'f'`)
Byte(u8),
/// A character literal (`'a'`)
@@ -43,13 +43,13 @@
impl From<Vec<u8>> for Lit {
fn from(input: Vec<u8>) -> Lit {
- Lit::ByteStr(input)
+ Lit::ByteStr(input, StrStyle::Cooked)
}
}
impl<'a> From<&'a [u8]> for Lit {
fn from(input: &[u8]) -> Lit {
- Lit::ByteStr(input.into())
+ Lit::ByteStr(input.into(), StrStyle::Cooked)
}
}
@@ -168,12 +168,12 @@
punct!("b\""),
cooked_string,
tag!("\"")
- ) => { |s: String| Lit::ByteStr(s.into_bytes()) }
+ ) => { |s: String| Lit::ByteStr(s.into_bytes(), StrStyle::Cooked) }
|
preceded!(
punct!("br"),
raw_string
- ) => { |(s, _): (String, _)| Lit::ByteStr(s.into_bytes()) }
+ ) => { |(s, n): (String, _)| Lit::ByteStr(s.into_bytes(), StrStyle::Raw(n)) }
));
named!(byte -> Lit, do_parse!(
@@ -262,6 +262,7 @@
use quote::{Tokens, ToTokens};
use std::{ascii, iter};
use std::fmt::{self, Display};
+ use std::str;
impl ToTokens for Lit {
fn to_tokens(&self, tokens: &mut Tokens) {
@@ -272,7 +273,7 @@
delim = iter::repeat("#").take(n).collect::<String>(),
string = s));
}
- Lit::ByteStr(ref v) => {
+ Lit::ByteStr(ref v, StrStyle::Cooked) => {
let mut escaped = "b\"".to_string();
for &ch in v.iter() {
escaped.extend(ascii::escape_default(ch).map(|c| c as char));
@@ -280,6 +281,11 @@
escaped.push('"');
tokens.append(&escaped);
}
+ Lit::ByteStr(ref vec, StrStyle::Raw(n)) => {
+ tokens.append(&format!("br{delim}\"{string}\"{delim}",
+ delim = iter::repeat("#").take(n).collect::<String>(),
+ string = str::from_utf8(vec).unwrap()));
+ }
Lit::Byte(b) => tokens.append(&format!("b{:?}", b as char)),
Lit::Char(ch) => ch.to_tokens(tokens),
Lit::Int(value, ty) => tokens.append(&format!("{}{}", value, ty)),