General Utility Library for C++14  2.11
Functions
gul14/escape.h

Detailed Description

Escaping and unescaping special characters in strings.

Functions

GUL_EXPORT std::string gul14::escape (string_view in)
 Create a new string that looks like an ASCII-only C string literal of the input string. More...
 
GUL_EXPORT std::string gul14::unescape (string_view in)
 Evaluate a string with escaped characters to get the original string back. More...
 

Function Documentation

◆ escape()

GUL_EXPORT std::string gul14::escape ( string_view  in)

Create a new string that looks like an ASCII-only C string literal of the input string.

This is achieved by replacing all non-printable and non-ASCII characters with a hex code escape in the form \x01.

A few special cases are implemented to give more readable representations for very common control characters, and of course backslash and double quotes are escaped as well:

CR   ->  \r
NL   ->  \n
TAB  ->  \t
\    ->  \\
"    ->  \"

Example

std::cout << escape("Zwei\tFlüsse\nfließen ins Meer.") << "\n";
GUL_EXPORT std::string escape(string_view in)
Create a new string that looks like an ASCII-only C string literal of the input string.

Output (assuming that the string literal was in Latin-1 encoding):

Zwei\tFl\xfcsse\nflie\xdfen ins Meer.
Note
The hexadecimal escape always uses two digits. This is different from the C/C++ standard, where it can be an arbitrary number of digits. The standard's way makes it impossible to have any hex digit after a hex escape, e.g. "\x200" is invalid and not equal to " 0" from the standard's point of view.
Parameters
inThe input string.
Returns
a new string that contains only ASCII characters.
Since
GUL version 1.4 parameter in is a string_view (was std::string before)

◆ unescape()

GUL_EXPORT std::string gul14::unescape ( string_view  in)

Evaluate a string with escaped characters to get the original string back.

Does only know the escape sequences used by gul14::escape() and can be used as in inverse function.

Parameters
inThe string with escape sequences
Returns
A new string where the sequences have been evaluated
Since
GUL version 1.4 parameter in is a string_view (was std::string before)