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