1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
// This file is part of yash, an extended POSIX shell.
// Copyright (C) 2021 WATANABE Yuki
//! This crate provides a function that quotes a string according to the POSIX
//! shell quoting rules.
//!
//! When used in a POSIX shell script, the resultant string will expand to a
//! single field having the same value as the original string.
//!
//! POSIX specifies several types of quoting mechanisms we can use. This crate
//! picks one according to the following decision rules:
//!
//! - If the string is not empty and contains no characters that need quoting,
//! the string is returned intact.
//! - Otherwise, if the string contains no single quote, the whole string is
//! single-quoted.
//! - Otherwise, the whole string is double-quoted, and all occurrences of `"`,
//! `` ` ``, `$`, and `\` are backslash-escaped.
//!
//! The following characters need quoting:
//!
//! - `;`, `&`, `|`, `(`, `)`, `<`, and `>`
//! - A space, tab, newline, or any other whitespace character
//! - `$`, `` ` ``, `\`, `"`, and `'`
//! - `=`, `*`, and `?`
//! - `#` or `~` occurring at the beginning of the string
//! - `:` immediately followed by `~`
//! - `{` preceding `}`
//! - `[` preceding `]`
//!
//!
//! The [`quoted`] function wraps a string in [`Quoted`], which implements
//! `Display` to produce the quoted version of the string with a formatter. The
//! [`quote`] function returns a `Cow<str>`, avoiding unnecessary clone of the
//! string if it requires no quoting.
//!
//! # Examples
//!
//! ```
//! # use yash_quote::quoted;
//! assert_eq!(format!("value={}", quoted("foo")), "value=foo");
//! assert_eq!(format!("value={}", quoted("")), "value=''");
//! assert_eq!(format!("value={}", quoted("$foo")), "value='$foo'");
//! assert_eq!(format!("value={}", quoted("'$foo'")), r#"value="'\$foo'""#);
//! ```
//!
//! ```
//! # use yash_quote::quote;
//! assert_eq!(quote("foo"), "foo");
//! assert_eq!(quote(""), "''");
//! assert_eq!(quote("$foo"), "'$foo'");
//! assert_eq!(quote("'$foo'"), r#""'\$foo'""#);
//! ```
use std::borrow::Cow::{self, Borrowed, Owned};
#[must_use]
fn char_needs_quoting(c: char) -> bool {
match c {
';' | '&' | '|' | '(' | ')' | '<' | '>' | ' ' | '\t' | '\n' => true,
'$' | '`' | '\\' | '"' | '\'' | '=' | '*' | '?' => true,
_ => c.is_whitespace(),
}
}
#[must_use]
fn str_needs_quoting(s: &str) -> bool {
if s.is_empty() {
return true;
}
// `#` or `~` occurring at the beginning of the string
if let Some(c) = s.chars().next() {
if c == '#' || c == '~' {
return true;
}
}
// characters that require quoting regardless of the position
if s.chars().any(char_needs_quoting) {
return true;
}
// `:` immediately followed by `~`
if s.contains(":~") {
return true;
}
// `{` preceding `}`
if let Some(i) = s.find('{') {
if s[i + 1..].contains('}') {
return true;
}
}
// `[` preceding `]`
if let Some(i) = s.find('[') {
if s[i + 1..].contains(']') {
return true;
}
}
false
}
/// Wrapper for quoting a string.
///
/// `Quoted` wraps a `&str` and implements `Display` to produce a quoted version
/// of the string. The implementation prints the same result as [`quote`] but
/// may be more efficient if the result is to be part of a larger string built
/// with a formatter.
#[derive(Clone, Copy, Debug)]
#[must_use = "`Quoted` does nothing unless printed"]
pub struct Quoted<'a> {
raw: &'a str,
needs_quoting: bool,
}
impl<'a> Quoted<'a> {
/// Returns the original string.
#[inline]
#[must_use]
pub fn as_raw(&self) -> &'a str {
self.raw
}
/// Tests whether the contained string requires quoting.
#[inline]
#[must_use]
pub fn needs_quoting(&self) -> bool {
self.needs_quoting
}
}
/// Quotes the contained string.
impl std::fmt::Display for Quoted<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
use std::fmt::Write;
if !self.needs_quoting {
f.write_str(self.raw)
} else if !self.raw.contains('\'') {
write!(f, "'{}'", self.raw)
} else {
f.write_char('"')?;
for c in self.raw.chars() {
if matches!(c, '"' | '`' | '$' | '\\') {
f.write_char('\\')?;
}
f.write_char(c)?;
}
f.write_char('"')
}
}
}
/// Wraps a string in [`Quoted`].
///
/// This function scans the string to cache the value for
/// [`Quoted::needs_quoting`], so this is an _O_(_n_) operation.
impl<'a> From<&'a str> for Quoted<'a> {
#[inline]
fn from(raw: &'a str) -> Self {
let needs_quoting = str_needs_quoting(raw);
Quoted { raw, needs_quoting }
}
}
/// Constructs a quoted string.
impl<'a> From<Quoted<'a>> for Cow<'a, str> {
#[must_use]
fn from(q: Quoted<'a>) -> Self {
if q.needs_quoting() {
Owned(q.to_string())
} else {
Borrowed(q.as_raw())
}
}
}
/// Wraps a string in [`Quoted`].
///
/// This function scans the string to cache the value for
/// [`Quoted::needs_quoting`], so this is an _O_(_n_) operation.
#[inline]
pub fn quoted(raw: &str) -> Quoted {
Quoted::from(raw)
}
/// Quotes the argument.
///
/// If the argument needs no quoting, the return value is `Borrowed(raw)`.
/// Otherwise, it is `Owned(new_quoted_string)`.
///
/// See the [module doc](self) for more details.
#[inline]
#[must_use]
pub fn quote(raw: &str) -> Cow<'_, str> {
quoted(raw).into()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn no_quoting() {
fn test(s: &str) {
assert_eq!(quote(s), Borrowed(s));
}
test("a");
test("z");
test("_");
test("!#%+,-./:@^~");
test("{");
test("{x");
test("}");
test("x}");
test("}{");
test("[");
test("[x");
test("]");
test("x]");
test("][");
}
#[test]
fn single_quoted() {
fn test(s: &str) {
assert_eq!(quote(s), Owned::<str>(format!("'{}'", s)));
}
test("");
for c in ";&|()<> \t\n\u{3000}$`\\\"=*?#~".chars() {
test(&c.to_string());
}
test("{}");
test("{a}");
test("[]");
test("[a]");
test("foo:~bar");
}
#[test]
fn double_quoted() {
fn test(input: &str, output: &str) {
assert_eq!(quote(input), Owned::<str>(output.to_string()));
}
test("'", r#""'""#);
test(r#"'"'"#, r#""'\"'""#);
test("'$", r#""'\$""#);
test("'foo'", r#""'foo'""#);
test(r"'\'\\''", r#""'\\'\\\\''""#);
test("'{\n}'", "\"'{\n}'\"");
}
}