ALib C++ Library
Library Version: 2412 R0
Documentation generated by doxygen
Loading...
Searching...
No Matches
StringEscaperStandard Struct Reference

Description:

This struct implements the interface given with StringEscaper. Its purpose is to convert string data to external string representations and vice versa. Such conversion is needed when C++ strings contain non-readable characters like 'new line', 'carriage return' or 'tabulator' and such strings should be stored in ASCII or unicode files. The common way of conversion is to add "escape sequences", for example "\n" for the 'new line' character.

Furthermore if a C++ string starts or ends with spaces, the string has to be quoted and in this case occurences of the quote character (") inside the string have to be escaped. Quotation is also needed, if a series of strings, separated by possible delimiter characters needs to be escaped, and a string contains one of the delimiters itself. Likewise, when such string is imported back to C++ representation, delimiters must only be recognized when outside of quoted string tokens.

With this approach, this implementation should be compatible with INI-files, JSon files and such. If a different approach is needed, the virtual functions may be overridden by custom descendents.


This default implementation proceeds as follows:

  • "Externalizing" a value:
    • Value is surrounded by quotes if it starts or ends with spaces or if it includes one of the delimiter tokens.
    • A few characters are escaped using '\'. Those are \n, \r, \t , \a, \b, \v, \f, \e and also the double quotation marks \" and the backslash itself (\c \\\\‍). - "Internalizing" a value: - If (non-escaped) quote \c " characters are found, those are removed and whitespaces within such quotes are kept.
    • Escaped characters are converted to their original value.
  • Loading variables from external strings:
    • If the provided variable has a valid delimiter, this character is used to tokenize the external string.
    • Values are trimmed, unless quoted. Quoted characters themselves are removed.
    • Delimiters found within a pair of quotes are ignored.
    • Each value found is internalized separately.

Definition at line 130 of file escaper.hpp.

#include <escaper.hpp>

Inheritance diagram for StringEscaperStandard:
[legend]
Collaboration diagram for StringEscaperStandard:
[legend]

Public Method Index:

virtual ~StringEscaperStandard () override
 Virtual destructor.
 
virtual ALIB_API AStringEscape (const String &src, AString &dest, const String &delimiters) const override
 
virtual ALIB_API int EscapeTokens (StringVectorMA &result, const String &src, const String &delimiters) const override
 
virtual ALIB_API AStringUnescape (const String &src, AString &dest) const override
 
virtual ALIB_API int UnescapeTokens (StringVectorMA &result, const String &src, const String &delimiters) const override
 
- Public Method Index: inherited from StringEscaper
virtual ~StringEscaper ()
 Virtual destructor.
 

Constructor(s) / Destructor Details:

◆ ~StringEscaperStandard()

virtual ~StringEscaperStandard ( )
inlineoverridevirtual

Virtual destructor.

Definition at line 133 of file escaper.hpp.

Method Details:

◆ Escape()

AString & Escape ( const String & src,
AString & dest,
const String & delimiters ) const
overridevirtual

Converts the given src string to an external representation. The escape symbol is backslash '\' and the following characters are escaped: \n, \r, \t , \a, \b, \v, \f, \e. Furthermore the double quotation marks \" and the backslash itself (\c \\\\‍).<br> Besides that, the value is surrounded by double quotes \c " if it starts or ends with spaces or if it includes one of the delimiter characters. The rationale of the latter is understood when it comes to method UnescapeTokens: Here, delimiters are ignored if they reside in a quoted string. If neither trailing nor leading spaces nor a delimiter is found, the string is not quoted. In situations, a quoted string is always needed, the caller has to test if a string was quoted and add the quote after the invocation of this method.

Parameters
srcThe source string to convert to external representation.
destThe destination string buffer.
delimitersIf one of these characters is found in the string, the value is quoted
Returns
dest to allow concatenated operations.

Reimplemented from StringEscaper.

Definition at line 81 of file escaper.cpp.

Here is the call graph for this function:

◆ EscapeTokens()

int EscapeTokens ( StringVectorMA & result,
const String & src,
const String & delimiters ) const
overridevirtual

Parses a list of tokens separated by the given delimiter and calls method Escape for each of them. The results are copied to string list result.

Availability
This method is defined only if module ALib Monomem is included in the ALib Distribution.
Parameters
resultThe destination list of strings.
srcThe source string.
delimitersA set of characters defining the delimiters.
Returns
The number of tokens found.

Reimplemented from StringEscaper.

Definition at line 128 of file escaper.cpp.

Here is the call graph for this function:

◆ Unescape()

AString & Unescape ( const String & src,
AString & dest ) const
overridevirtual

Trims src, removes surrounding quotes and, un-escapes characters as defined with method Escape.

Parameters
srcThe source string.
destThe destination string.
Returns
dest to allow concatenated operations.

Reimplemented from StringEscaper.

Definition at line 35 of file escaper.cpp.

Here is the call graph for this function:

◆ UnescapeTokens()

int UnescapeTokens ( StringVectorMA & result,
const String & src,
const String & delimiters ) const
overridevirtual

Parses a list of tokens separated by the given delimiter and calls method Unescape for each of them. The results are copied to string list result. Delimiters found within quoted strings are rightfully ignored.

Availability
This method is defined only if module ALib Monomem is included in the ALib Distribution.
Parameters
resultThe destination list of strings.
srcThe source string.
delimitersA set of characters accepted as delimiters.
Returns
The number of tokens found.

Reimplemented from StringEscaper.

Definition at line 142 of file escaper.cpp.

Here is the call graph for this function:

The documentation for this struct was generated from the following files: