ALib C++ Framework
by
Library Version: 2605 R0
Documentation generated by doxygen
Loading...
Searching...
No Matches
escaper.hpp
Go to the documentation of this file.
1//==================================================================================================
2/// \file
3/// This header-file is part of module \alib_strings of the \aliblong.
4///
5/// Copyright 2013-2026 A-Worx GmbH, Germany.
6/// Published under #"mainpage_license".
7//==================================================================================================
8ALIB_EXPORT namespace alib { namespace strings::util {
9
10
11/// This is a virtual base type defining the interface for derived struct
12/// #"util::StringEscaperStandard" and possible custom derivates.<br>
13/// However, this class is not abstract but instead implements the interface by just copying
14/// the strings to the given target buffers. The rationale for this is that it can be used
15/// in situations, where no externalization and import of externalized strings are needed.
16/// In other words: In situations where a function expects a #"%StringEscaper" where none is necessary
17/// a local instance of this type can be created and just passed.
18///
19/// @see
20/// Any further explanation is found with the documentation of derived type
21/// #"util::StringEscaperStandard".
23 /// Virtual destructor.
24 virtual ~StringEscaper() {}
25
26 /// Just copies the given \p{src} string to \p{dest}.
27 ///
28 /// @param src The source string.
29 /// @param dest The destination string.
30 /// @param delimiters Ignored
31 /// @return \p{dest} to allow concatenated operations.
32 virtual inline
33 AString& Escape( const String& src, AString& dest, const String& delimiters ) const
34 { (void) delimiters; return dest << src; }
35
36 /// Just copies the given \p{src} string to \p{dest}.
37 ///
38 /// @param src The source string.
39 /// @param dest The destination string.
40 /// @return \p{dest} to allow concatenated operations.
41 virtual inline
42 AString& Unescape( const String& src, AString& dest ) const { return dest << src; }
43
44#if ALIB_MONOMEM
45 /// Simply tokenizes \p{src} and feeds the tokens into \p{result}.
46 ///
47 /// \par Availability
48 /// This method is defined only if module \alib_monomem is included in the \alibbuild.
49 /// @param result The destination list of strings.
50 /// @param src The source string.
51 /// @param delimiters A set of characters accepted as delimiters.
52 /// @return The number of tokens found.
53 ALIB_DLL virtual
54 int EscapeTokens( StringVectorMA& result, const String& src, const String& delimiters) const;
55
56 /// Simply tokenizes \p{src} and feeds the tokens into \p{result}.
57 /// (In fact, #".EscapeTokens" is called.)
58 ///
59 /// \par Availability
60 /// This method is defined only if module \alib_monomem is included in the \alibbuild.
61 /// @param result The destination list of strings.
62 /// @param src The source string.
63 /// @param delimiters A set of characters accepted as delimiters.
64 /// @return The number of tokens found.
65 virtual inline
66 int UnescapeTokens(StringVectorMA& result, const String& src, const String& delimiters) const
67 { return EscapeTokens( result, src, delimiters); }
68
69#endif // #if ALIB_MONOMEM
70
71}; // StringEscaper
72
73//==================================================================================================
74/// This struct implements the interface given with #"util::StringEscaper".
75/// Its purpose is to convert string data to external string representations and vice versa.
76/// Such conversion is needed when C++ strings contain non-readable characters like
77/// 'new line', 'carriage return' or 'tabulator' and such strings should be stored in ASCII or
78/// unicode files. The common way of conversion is to add "escape sequences", for example,
79/// <c>"\n"</c> for the 'new line' character.
80///
81/// Furthermore if a C++ string starts or ends with spaces, the string has to be quoted and in this
82/// case occurences of the quote character <c>(\")</c> inside the string have to be escaped.
83/// Quotation is also needed, if a series of strings, separated by possible delimiter characters
84/// needs to be escaped, and a string contains one of the delimiters itself. Likewise, when such
85/// string is imported back to C++ representation, delimiters must only be recognized when outside
86/// of quoted string tokens.
87///
88/// With this approach, this implementation should be compatible with INI-files, JSon files
89/// and such. If a different approach is needed, the virtual functions may be overridden by
90/// custom descendents.
91///
92/// ---------------------------
93/// This default implementation proceeds as follows:
94/// - "Externalizing" a value:
95/// - Value is surrounded by quotes if it starts or ends with spaces or if it includes one of
96/// the delimiter tokens.
97/// - A few characters are escaped using \c '\\'. Those are
98/// \c \\n, \c \\r, \c \\t , \c \\a, \c \\b, \c \\v, \c \\f, \c \\e and also
99/// the double quotation marks \c \\" and the backslash itself (\c \\\\‍).
100///
101/// - "Internalizing" a value:
102/// - If (non-escaped) quote \c " characters are found, those are removed and whitespaces
103/// within such quotes are kept.
104/// - Escaped characters are converted to their original value.
105///
106/// - Loading variables from external strings:
107/// - If the provided variable has a valid delimiter, this character is used to tokenize
108/// the external string.
109/// - Values are trimmed, unless quoted. Quoted characters themselves are removed.
110/// - Delimiters found within a pair of quotes are ignored.
111/// - Each value found is internalized separately.
112//==================================================================================================
114 /// Virtual destructor.
115 virtual ~StringEscaperStandard() override {}
116
117 /// Converts the given \p{src} string to an external representation.
118 /// The escape symbol is backslash \c '\\' and the following characters are escaped:
119 /// \c \\n, \c \\r, \c \\t , \c \\a, \c \\b, \c \\v, \c \\f, \c \\e. Furthermore
120 /// the double quotation marks \c \\" and the backslash itself (\c \\\\‍).<br>
121 ///
122 /// Besides that, the value is surrounded by double quotes \c " if it starts or ends with spaces
123 /// or if it includes one of the delimiter characters. The rationale of the latter is understood
124 /// when it comes to method #".UnescapeTokens:" Here, delimiters are ignored if they reside in a
125 /// quoted string. If neither trailing nor leading spaces nor a delimiter is found, the string
126 /// is \b not quoted. In situations, a quoted string is always needed, the caller has to test
127 /// if a string was quoted and add the quote after the invocation of this method.
128 ///
129 /// @param src The source string to convert to external representation.
130 /// @param dest The destination string buffer.
131 /// @param delimiters If one of these characters is found in the string, the value is quoted
132 /// @return \p{dest} to allow concatenated operations.
133 ALIB_DLL virtual
134 AString& Escape( const String& src, AString& dest, const String& delimiters ) const override;
135
136 /// Trims \p{src}, removes surrounding quotes and, un-escapes characters as defined with
137 /// method #".Escape".
138 ///
139 /// @param src The source string.
140 /// @param dest The destination string.
141 /// @return \p{dest} to allow concatenated operations.
142 ALIB_DLL virtual
143 AString& Unescape( const String& src, AString& dest ) const override;
144
145#if ALIB_MONOMEM
146 /// Parses a list of tokens separated by the given \p{delimiter} and calls method
147 /// #".Escape" for each of them. The results are copied to string list \p{result}.
148 ///
149 /// \par Availability
150 /// This method is defined only if module \alib_monomem is included in the \alibbuild.
151 ///
152 /// @param result The destination list of strings.
153 /// @param src The source string.
154 /// @param delimiters A set of characters defining the delimiters.
155 /// @return The number of tokens found.
156 ALIB_DLL virtual
157 int EscapeTokens( StringVectorMA& result, const String& src,
158 const String& delimiters ) const override;
159
160 /// Parses a list of tokens separated by the given \p{delimiter} and calls method
161 /// #".Unescape" for each of them. The results are copied to string list \p{result}.
162 /// Delimiters found within quoted strings are rightfully ignored.
163 ///
164 /// \par Availability
165 /// This method is defined only if module \alib_monomem is included in the \alibbuild.
166 /// @param result The destination list of strings.
167 /// @param src The source string.
168 /// @param delimiters A set of characters accepted as delimiters.
169 /// @return The number of tokens found.
170 ALIB_DLL virtual
171 int UnescapeTokens(StringVectorMA& result,
172 const String& src , const String& delimiters) const override;
173#endif //#if ALIB_MONOMEM
174
175}; // struct StringEscaperStandard
176
177
178} // namespace alib::[strings::util]
179
180/// Type alias in namespace #"%alib".
182
183/// Type alias in namespace #"%alib".
185
186
187} // namespace [alib]
#define ALIB_DLL
#define ALIB_EXPORT
Definition alox.cpp:14
strings::util::StringEscaperStandard StringEscaperStandard
Type alias in namespace #"%alib".
Definition escaper.hpp:184
strings::util::TStringVector< character, MonoAllocator > StringVectorMA
Type alias in namespace #"%alib".
Definition vector.hpp:107
strings::TEscape< character > Escape
Type alias in namespace #"%alib".
Definition format.hpp:531
strings::util::StringEscaper StringEscaper
Type alias in namespace #"%alib".
Definition escaper.hpp:181
strings::TString< character > String
Type alias in namespace #"%alib".
Definition string.hpp:2165
strings::TAString< character, lang::HeapAllocator > AString
Type alias in namespace #"%alib".
virtual AString & Unescape(const String &src, AString &dest) const override
Definition escaper.cpp:21
virtual int UnescapeTokens(StringVectorMA &result, const String &src, const String &delimiters) const override
Definition escaper.cpp:117
virtual ~StringEscaperStandard() override
Virtual destructor.
Definition escaper.hpp:115
virtual int EscapeTokens(StringVectorMA &result, const String &src, const String &delimiters) const override
Definition escaper.cpp:105
virtual ~StringEscaper()
Virtual destructor.
Definition escaper.hpp:24
virtual int EscapeTokens(StringVectorMA &result, const String &src, const String &delimiters) const
Definition escaper.cpp:7
virtual int UnescapeTokens(StringVectorMA &result, const String &src, const String &delimiters) const
Definition escaper.hpp:66
virtual AString & Unescape(const String &src, AString &dest) const
Definition escaper.hpp:42
virtual AString & Escape(const String &src, AString &dest, const String &delimiters) const
Definition escaper.hpp:33