| 1 |
use std::{fmt, ops::Range}; |
| 2 |
|
| 3 |
use crate::{ |
| 4 |
err::{perr, ParseErrorKind::*}, |
| 5 |
escape::{scan_raw_string, unescape_string}, |
| 6 |
Buffer, ParseError, |
| 7 |
}; |
| 8 |
|
| 9 |
|
| 10 |
/// A byte string or raw byte string literal, e.g. `b"hello"` or `br#"abc"def"#`. |
| 11 |
/// |
| 12 |
/// See [the reference][ref] for more information. |
| 13 |
/// |
| 14 |
/// [ref]: https://doc.rust-lang.org/reference/tokens.html#byte-string-literals |
| 15 |
#[derive(Debug, Clone, PartialEq, Eq)] |
| 16 |
pub struct ByteStringLit<B: Buffer> { |
| 17 |
/// The raw input. |
| 18 |
raw: B, |
| 19 |
|
| 20 |
/// The string value (with all escaped unescaped), or `None` if there were |
| 21 |
/// no escapes. In the latter case, `input` is the string value. |
| 22 |
value: Option<Vec<u8>>, |
| 23 |
|
| 24 |
/// The number of hash signs in case of a raw string literal, or `None` if |
| 25 |
/// it's not a raw string literal. |
| 26 |
num_hashes: Option<u8>, |
| 27 |
|
| 28 |
/// Start index of the suffix or `raw.len()` if there is no suffix. |
| 29 |
start_suffix: usize, |
| 30 |
} |
| 31 |
|
| 32 |
impl<B: Buffer> ByteStringLit<B> { |
| 33 |
/// Parses the input as a (raw) byte string literal. Returns an error if the |
| 34 |
/// input is invalid or represents a different kind of literal. |
| 35 |
pub fn parse(input: B) -> Result<Self, ParseError> { |
| 36 |
if input.is_empty() { |
| 37 |
return Err(perr(None, Empty)); |
| 38 |
} |
| 39 |
if !input.starts_with(r#"b""#) && !input.starts_with("br") { |
| 40 |
return Err(perr(None, InvalidByteStringLiteralStart)); |
| 41 |
} |
| 42 |
|
| 43 |
let (value, num_hashes, start_suffix) = parse_impl(&input)?; |
| 44 |
Ok(Self { raw: input, value, num_hashes, start_suffix }) |
| 45 |
} |
| 46 |
|
| 47 |
/// Returns the string value this literal represents (where all escapes have |
| 48 |
/// been turned into their respective values). |
| 49 |
pub fn value(&self) -> &[u8] { |
| 50 |
self.value |
| 51 |
.as_deref() |
| 52 |
.unwrap_or(&self.raw.as_bytes()[self.inner_range()]) |
| 53 |
} |
| 54 |
|
| 55 |
/// Like `value` but returns a potentially owned version of the value. |
| 56 |
/// |
| 57 |
/// The return value is either `Vec<u8>` if `B = String`, or |
| 58 |
/// `Cow<'a, [u8]>` if `B = &'a str`. |
| 59 |
pub fn into_value(self) -> B::ByteCow { |
| 60 |
let inner_range = self.inner_range(); |
| 61 |
let Self { raw, value, .. } = self; |
| 62 |
value |
| 63 |
.map(B::ByteCow::from) |
| 64 |
.unwrap_or_else(|| raw.cut(inner_range).into_byte_cow()) |
| 65 |
} |
| 66 |
|
| 67 |
/// The optional suffix. Returns `""` if the suffix is empty/does not exist. |
| 68 |
pub fn suffix(&self) -> &str { |
| 69 |
&(*self.raw)[self.start_suffix..] |
| 70 |
} |
| 71 |
|
| 72 |
/// Returns whether this literal is a raw string literal (starting with |
| 73 |
/// `r`). |
| 74 |
pub fn is_raw_byte_string(&self) -> bool { |
| 75 |
self.num_hashes.is_some() |
| 76 |
} |
| 77 |
|
| 78 |
/// Returns the raw input that was passed to `parse`. |
| 79 |
pub fn raw_input(&self) -> &str { |
| 80 |
&self.raw |
| 81 |
} |
| 82 |
|
| 83 |
/// Returns the raw input that was passed to `parse`, potentially owned. |
| 84 |
pub fn into_raw_input(self) -> B { |
| 85 |
self.raw |
| 86 |
} |
| 87 |
|
| 88 |
/// The range within `self.raw` that excludes the quotes and potential `r#`. |
| 89 |
fn inner_range(&self) -> Range<usize> { |
| 90 |
match self.num_hashes { |
| 91 |
None => 2..self.start_suffix - 1, |
| 92 |
Some(n) => 2 + n as usize + 1..self.start_suffix - n as usize - 1, |
| 93 |
} |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
impl ByteStringLit<&str> { |
| 98 |
/// Makes a copy of the underlying buffer and returns the owned version of |
| 99 |
/// `Self`. |
| 100 |
pub fn into_owned(self) -> ByteStringLit<String> { |
| 101 |
ByteStringLit { |
| 102 |
raw: self.raw.to_owned(), |
| 103 |
value: self.value, |
| 104 |
num_hashes: self.num_hashes, |
| 105 |
start_suffix: self.start_suffix, |
| 106 |
} |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
impl<B: Buffer> fmt::Display for ByteStringLit<B> { |
| 111 |
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 112 |
f.pad(&self.raw) |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
/// Precondition: input has to start with either `b"` or `br`. |
| 118 |
#[inline(never)] |
| 119 |
fn parse_impl(input: &str) -> Result<(Option<Vec<u8>>, Option<u8>, usize), ParseError> { |
| 120 |
if input.starts_with("br") { |
| 121 |
scan_raw_string(input, 2, false, true) |
| 122 |
.map(|(num, start_suffix)| (None, Some(num), start_suffix)) |
| 123 |
} else { |
| 124 |
unescape_string::<Vec<u8>>(input, 2, false, true, true) |
| 125 |
.map(|(v, start_suffix)| (v, None, start_suffix)) |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
#[cfg(test)] |
| 130 |
mod tests; |