NAWA 0.9
Web Application Framework for C++
nawa::encoding Namespace Reference

Functions

std::string htmlEncode (std::string input, bool encodeAll=false)
 
std::string htmlDecode (std::string input)
 
std::string urlEncode (std::string const &input)
 
std::string urlDecode (std::string input)
 
bool isBase64 (std::string const &input, bool allowWhitespaces=true)
 
std::string base64Encode (std::string const &input, size_t breakAfter=0, std::string const &breakSequence="")
 
std::string base64Decode (std::string const &input)
 
std::string quotedPrintableEncode (std::string const &input, std::string const &lineEnding="\r\n", bool replaceCrlf=false, bool qEncoding=false)
 
std::string quotedPrintableDecode (std::string input, bool qEncoding=false)
 
std::string makeEncodedWord (std::string const &input, bool base64=false, bool onlyIfNecessary=true)
 
std::string punycodeEncode (std::string const &input)
 
std::string punycodeDecode (std::string const &input)
 

Detailed Description

Namespace containing functions for text encoding and decoding

Function Documentation

◆ htmlEncode()

std::string nawa::encoding::htmlEncode ( std::string  input,
bool  encodeAll = false 
)

Escape special HTML characters in the input string.

Parameters
inputString with characters that need to be escaped for use in HTML.
encodeAllIf false (by default), only the characters &, ", <, and > will be escaped. If true, all characters that have an HTML entity will be encoded (less efficient and usually not necessary). Please note that if encodeAll is set to true, the input string must be utf-8 encoded for correct results.
Returns
String with escaped characters.

Definition at line 133 of file encoding.cpp.

◆ htmlDecode()

std::string nawa::encoding::htmlDecode ( std::string  input)

Decode a string with HTML entities by replacing them with the corresponding utf-8 characters. Only utf-8 (or ascii) encoded strings are supported by this function.
Only entities starting with & and ending in ; will be replaced, including the unicode notations. If the semicolon is missing, the entity will not be replaced.

Parameters
inputInput string containing HTML entities.
Returns
String in which the entities are decoded to utf-8 characters.

Definition at line 185 of file encoding.cpp.

◆ urlEncode()

std::string nawa::encoding::urlEncode ( std::string const &  input)

Percent-encode a string for inclusion in a URL. Works with any byte-wise encoding, but only utf-8 makes sense for use in URLs.

Parameters
inputString with characters that need to be escaped for use in a URL.
Returns
String with escaped characters.

Definition at line 238 of file encoding.cpp.

◆ urlDecode()

std::string nawa::encoding::urlDecode ( std::string  input)

Decode a percent-encoded string (byte-wise, in URLs always utf-8).

Parameters
inputPercent-encoded string.
Returns
Decoded string.

Definition at line 256 of file encoding.cpp.

◆ isBase64()

bool nawa::encoding::isBase64 ( std::string const &  input,
bool  allowWhitespaces = true 
)

Check if a string contains only valid base64 characters and could be valid base64.

Parameters
inputInput string.
allowWhitespacesIf true, the whitespace characters '\r', '\n', '\t', and ' ' will be allowed.
Returns
True if the string contains only valid base64 characters, false otherwise.

Definition at line 270 of file encoding.cpp.

◆ base64Encode()

std::string nawa::encoding::base64Encode ( std::string const &  input,
size_t  breakAfter = 0,
std::string const &  breakSequence = "" 
)

Encode a string (of bytes) as base64.

Parameters
inputInput string.
breakAfterMaximum line length. The breakSequence will be inserted after the given number of base64 characters. If breakAfter is 0 (default), no line breaks will be inserted by this function.
breakSequenceThe characters to use as a line break. (e.g., "\\n" or "\\r\\n").
Returns
Base64-encoded string.

Definition at line 281 of file encoding.cpp.

◆ base64Decode()

std::string nawa::encoding::base64Decode ( std::string const &  input)

Decode a base64-encoded string (of bytes). If the input string does not (only) contain valid base64, the return string may be undefined garbage. Exception: The characters '\r', '\n', '\t', and ' ' (whitespaces) will be ignored. You may use isBase64 to check it with a regex first, if the result matters.

Parameters
inputBase64 representation of a string.
Returns
The decoded string.

Definition at line 286 of file encoding.cpp.

◆ quotedPrintableEncode()

std::string nawa::encoding::quotedPrintableEncode ( std::string const &  input,
std::string const &  lineEnding = "\r\n",
bool  replaceCrlf = false,
bool  qEncoding = false 
)

Encode a string using quoted-printable encoding (especially useful for emails).

Parameters
inputString to be encoded.
lineEndingCharacter(s) to end the line. Default is "\\r\\n", as this is the default for emails.
replaceCrlfWhether to encode the '\r' and '\n' characters in the string. Defaults to false.
qEncodingUse the similar Q-encoding instead of quoted-printable. Line breaks will not be used, no limit on line length will be applied.
Returns
The encoded string.

Definition at line 290 of file encoding.cpp.

◆ quotedPrintableDecode()

std::string nawa::encoding::quotedPrintableDecode ( std::string  input,
bool  qEncoding = false 
)

Decode a quoted-printable encoded string. Please note that only the line endings "\\r\\n" and "\\n" are supported.

Parameters
inputEncoded string.
qEncodingWhether the string is Q-encoded.
Returns
Decoded string.

Definition at line 320 of file encoding.cpp.

◆ makeEncodedWord()

std::string nawa::encoding::makeEncodedWord ( std::string const &  input,
bool  base64 = false,
bool  onlyIfNecessary = true 
)

Convert a UTF8-encoded string to the encoded-word syntax for email headers.

Parameters
inputThe input string to encode.
base64Whether to use base64-encoding instead of Q-encoding (defaults to false).
onlyIfNecessaryOnly apply q-encoding if special characters, which would be encoded, are present in the input (does not apply to base64 encoding).
Returns
Encoded string.

Definition at line 339 of file encoding.cpp.

◆ punycodeEncode()

std::string nawa::encoding::punycodeEncode ( std::string const &  input)

Encode a domain in punycode, if it contains non-ASCII characters (otherwise it will be returned as-is). The return value of this function is undefined for invalid domain names.

Parameters
inputDomain to be encoded.
Returns
Encoded domain.

Definition at line 355 of file encoding.cpp.

◆ punycodeDecode()

std::string nawa::encoding::punycodeDecode ( std::string const &  input)

Decode a punycode-encoded domain. If it is not encoded, it will be returned as-is. The return value of this function is undefined for invalid domain names.

Parameters
inputEncoded domain.
Returns
Decoded domain.

Definition at line 380 of file encoding.cpp.