use super::super::*;
use sqlparser::tokenizer::{Location, Whitespace};
pub(super) fn has_block_comment(tokens: &[TokenWithSpan], start: Location, end: Location) -> bool {
let at = tokens.partition_point(|token| token.span.start < start);
tokens[at..]
.iter()
.take_while(|token| token.span.start < end)
.any(|token| {
matches!(
token.token,
Token::Whitespace(Whitespace::MultiLineComment(_))
)
})
}
pub(super) fn unicode_source_bytes(
encoded: &str,
source_bytes: &[Origin],
escape: char,
) -> Option<(String, Vec<Origin>)> {
let mut decoded_bytes = Vec::new();
let decoded = crate::parse::unicode::decode_unicode_escapes(encoded, escape, |at, scalar| {
decoded_bytes.extend(std::iter::repeat_n(source_bytes[at], scalar.len_utf8()));
})?;
Some((decoded, decoded_bytes))
}
#[derive(Clone, Copy)]
pub(super) enum LiteralEscape {
Raw,
Plain,
Escaped,
Unicode(char),
}
impl LiteralEscape {
pub(super) fn from_token(token: &Token, unicode_escape: Option<char>) -> Option<Self> {
match token {
Token::DollarQuotedString(_) => Some(Self::Raw),
Token::SingleQuotedString(_) => Some(Self::Plain),
Token::EscapedStringLiteral(_) => Some(Self::Escaped),
Token::UnicodeStringLiteral(_) => Some(Self::Unicode(unicode_escape.unwrap_or('\\'))),
_ => None,
}
}
}
pub(super) fn append_literal(
out: &mut Vec<u8>,
source_bytes: &mut Vec<Origin>,
source: Option<&str>,
token: &TokenWithSpan,
value: &str,
escape: LiteralEscape,
) -> Option<()> {
let fallback = Origin {
line: token.span.start.line as usize,
offset: source
.and_then(|sql| location_offset(sql, token.span.start.line, token.span.start.column))
.unwrap_or_default(),
};
let origins = source.and_then(|source| literal_source_bytes(source, token, value, escape));
if matches!(escape, LiteralEscape::Escaped) {
crate::source::adjacent_strings::escape::append(value, out, |at, width| {
let origin = origins.as_ref().map_or(fallback, |lines| lines[at]);
source_bytes.extend(std::iter::repeat_n(origin, width));
})?;
} else {
out.extend_from_slice(value.as_bytes());
if let Some(origins) = origins {
source_bytes.extend(origins);
} else {
source_bytes.extend(std::iter::repeat_n(fallback, value.len()));
}
}
Some(())
}
fn literal_source_bytes(
source: &str,
token: &TokenWithSpan,
decoded: &str,
escape: LiteralEscape,
) -> Option<Vec<Origin>> {
let start = location_offset(source, token.span.start.line, token.span.start.column)?;
let end = location_offset(source, token.span.end.line, token.span.end.column)?;
let raw = source.get(start..end)?;
let (leading, trailing) = match &token.token {
Token::SingleQuotedString(_) => (1, 1),
Token::EscapedStringLiteral(_) => (2, 1),
Token::UnicodeStringLiteral(_) => (3, 1),
Token::DollarQuotedString(value) => {
let delimiter_len = value.tag.as_ref().map_or(2, |tag| tag.len() + 2);
(delimiter_len, delimiter_len)
}
_ => return None,
};
let content = raw.get(leading..raw.len().checked_sub(trailing)?)?;
let origin = Origin {
offset: start + leading,
line: token.span.start.line as usize,
};
map_decoded_source_bytes(content, decoded, escape, origin)
}
fn map_decoded_source_bytes(
raw: &str,
decoded: &str,
escape: LiteralEscape,
mut origin: Origin,
) -> Option<Vec<Origin>> {
let raw = raw.chars().collect::<Vec<_>>();
let mut at = 0usize;
let mut escaped = false;
let mut source_bytes = Vec::with_capacity(decoded.len());
for character in decoded.chars() {
let doubled_quote =
!escaped && raw.get(at) == Some(&'\'') && raw.get(at + 1) == Some(&'\'');
let width = usize::from(doubled_quote && !matches!(escape, LiteralEscape::Raw)) + 1;
escaped =
matches!(escape, LiteralEscape::Escaped) && !escaped && raw.get(at) == Some(&'\\');
let consumed = raw.get(at..at.checked_add(width)?)?;
source_bytes.extend(std::iter::repeat_n(origin, character.len_utf8()));
origin.offset += consumed
.iter()
.map(|character| character.len_utf8())
.sum::<usize>();
origin.line += consumed
.iter()
.filter(|character| **character == '\n')
.count();
at += width;
}
(at == raw.len()).then_some(source_bytes)
}
#[cfg(test)]
mod tests;