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