#escaping #quote #shell-escape

snailquote

Escape and unescape strings with shell-inspired quoting

5 unstable releases

0.3.1 Oct 28, 2021
0.3.0 Jan 29, 2020
0.2.0 Jan 3, 2019
0.1.1 Dec 30, 2018
0.1.0 Jul 13, 2018

#556 in Text processing

Download history 5663/week @ 2024-07-21 5095/week @ 2024-07-28 4997/week @ 2024-08-04 5312/week @ 2024-08-11 5213/week @ 2024-08-18 4048/week @ 2024-08-25 4833/week @ 2024-09-01 4810/week @ 2024-09-08 4401/week @ 2024-09-15 4447/week @ 2024-09-22 3650/week @ 2024-09-29 4091/week @ 2024-10-06 4114/week @ 2024-10-13 4906/week @ 2024-10-20 5169/week @ 2024-10-27 5415/week @ 2024-11-03

19,950 downloads per month
Used in 128 crates (58 directly)

GPL-3.0-only

22KB
354 lines

snailquote

Build Status

Docs

This library provides functions to escape and unescape strings.

It escapes them in a roughly 'sh' compatible way (e.g. double quotes supporting backslash escapes, single quotes supporting no escapes).

In addition, it provides support for common c-like ascii escapes (like \n for newline, \v for vertical tab, etc) and rust-string-like unicode (via \u{12ff} style escapes).

More importantly, this library also provides the ability to un-escape a given escaped text to recover the original string.

For more information on usage and what escape sequences will work, see the docs.

Compatibility

snailquote intends to explicitly be compatible with the following use-cases:

  1. Readable encoding of arbitrary strings for user's editing; re-parsing of said strings after being edited.
  2. Reading and writing the values of os-release files.

Other files may have a similar shell-inspired format that snailescape works with, but it's up to you to verify it really is similar enough to be correct.

snailquote is inspired by, but not compatible with, the following:

  1. gnulib quotearg, as used for 'ls' output -- snailquote handles unicode escapes differently.
  2. ANSI-c string literal quoting -- snailquote handles unicode differently and supports shell-related escapes, like '$'.
  3. sh string quoting -- snailquote handles unicode differently and is more lax about shell special characters.

Why not use <other library>

Other libraries in rust I've found have one or more of the following problems:

  1. The escaped text is not as easily human-editable
  2. There is no way to un-escape text
  3. NIH

Dependencies

~0.6–1MB
~20K SLoC