absl::CUnescape

Unescapes a source string and copies it into dest, rewriting C-style escape sequences (https://en.cppreference.com/w/cpp/language/escape) into their proper code point equivalents, returning true if successful.

Synopsis

Declared in <absl/strings/escaping.h>

bool
CUnescape(
    std::string_view source,
    std::string* dest,
    std::string* error);

Description

The following unescape sequences can be handled:

* ASCII escape sequences (' ','r','', etc.) to their ASCII equivalents * Octal escape sequences ('nnn') to byte nnn. The unescaped value must resolve to a single byte or an error will occur. E.g. values greater than 0xff will produce an error. * Hexadecimal escape sequences ('xnn') to byte nn. While an arbitrary number of following digits are allowed, the unescaped value must resolve to a single byte or an error will occur. E.g. 'x0045' is equivalent to 'x45', but 'x1234' will produce an error. * Unicode escape sequences ('unnnn' for exactly four hex digits or 'Unnnnnnnn' for exactly eight hex digits, which will be encoded in UTF-8. (E.g., u2019 unescapes to the three bytes 0xE2, 0x80, and 0x99).

If any errors are encountered, this function returns false, leaving the dest output parameter in an unspecified state, and stores the first encountered error in error. To disable error reporting, set error to nullptr or use the overload with no error reporting below.

Example:

std::string s = "foo\rbar\nbaz\t"; std::string unescaped_s; if (!absl::CUnescape(s, &unescaped_s)) { ... } EXPECT_EQ(unescaped_s, "foorbarnbazt");

Return Value

true on success, false if an error was encountered.

Parameters

NameDescription
sourceThe escaped string to unescape.
destThe output string that receives the unescaped result.
errorReceives the first error encountered, or nullptr to disable error reporting.