ALib C++ Library
Library Version: 2510 R0
Documentation generated by doxygen
Loading...
Searching...
No Matches
strings.inl
Go to the documentation of this file.
1//==================================================================================================
2/// \file
3/// This header-file is part of module \alib_expressions 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 expressions { namespace plugins {
9
10//==================================================================================================
11/// This built-in \alib{expressions;CompilerPlugin} of \alib_expressions_nl
12/// compiles identifiers, functions and operators with character string-type operands or return
13/// types.
14///
15/// By default, this plug-in is \alib{expressions;Compiler::CfgBuiltInPlugins;automatically created}
16/// and inserted into each instance of class \alib{expressions;Compiler} with the invocation of
17/// \alib{expressions::Compiler;SetupDefaults}.
18///
19/// <b>Constants:</b><br>
20///
21/// Type | Name | Min. Abbreviation | Description
22/// --------|-------------|-------------------|--------------
23/// String |\b NewLine | nl | System dependent new line character combination ( <c>"\n"</c> or <c>"\r\n"</c>).
24/// String |\b Tabulator | tab | String with single tabulator character (<c>"\t"</c>).
25///
26/// <br>
27/// <b>Functions:</b><br>
28///
29/// Note: All identifier and function names are defined case-insensitive.
30///
31/// Return Type | Name | Min. Abbreviation| Signature | Description
32/// ---------------|-----------|------------------|-----------|-------------
33/// String | \b String | str | ... | Creates and returns a string representation of the arguments, concatenated from left to right.
34/// String | \b ToUpper | tu | String | Converts all appropriate characters of a string to upper case letters. This function is aliased by unary operator <c>'+'</c>.
35/// String | \b ToLower | tl | String | Converts all appropriate characters of a string to lower case letters. This function is aliased by unary operator <c>'-'</c>.
36/// Boolean | \b Compare | comp | String, String | Returns \c 0 if strings are equal, a negative value if the first string is "smaller" than the second and a positive otherwise.
37/// Boolean | \b Compare | comp | String, String, Boolean | Same as Compare(String,String) if third parameter is \c false. If it is \c true letter case is ignored.
38/// Boolean | \b StartsWith | sw | String, String | Tests if the first given string starts with the second. Comparison is case-sensitive.
39/// Boolean | \b StartsWith | sw | String, String, Boolean | Tests if the first given string starts with the second. The third parameter is \c true, letter case is ignored.
40/// Boolean | \b EndsWith | ew | String, String | Tests if the first given string ends with the second. Comparison is case-sensitive.
41/// Boolean | \b EndsWith | ew | String, String, Boolean | Tests if the first given string ends with the second. The third parameter is \c true, letter case is ignored.
42/// String | \b Substring | subs | String, Integer | Returns the substring, starting at given position (2nd argument) to the end of the string..
43/// String | \b Substring | subs | String, Integer, Integer| Returns the substring, starting at given position (2nd argument) with the given length (3rd argument).
44/// Integer | \b IndexOf | indo | String, String | Searches the first occurrence of the second string in the first. Returns the position if found, otherwise \c -1.
45/// Integer | \b Count | count | String, String | Counts the number of occurrences of the second string in the first.
46/// String | \b Replace | repl | String, String, String | Returns a copy of the 1st argument, with occurrences of 2nd argument replaced by 3rd argument.
47/// String | \b Repeat | repeat | String, Integer | Returns 1st argument concatenated \e N times (2nd argument).
48/// String | \b Token | tok | String, String, Integer | Parses tokens separated by character (2nd argument) in string (1st argument) an returns the n-th (3rd argument).
49/// String | \b Trim | trim | String | Trims the string at both sides by removing the following whitespace characters: <c>' '</c>, <c>'\\n'</c>, <c>'\\r'</c> and <c>'\\t'</c>.
50/// String | \b Trim | trim | String, String | Trims the string at both sides by removing the whitespace characters listed in the second string.
51/// String | \b TrimStart | trims | String | Trims the string at the start by removing the following whitespace characters: <c>' '</c>, <c>'\\n'</c>, <c>'\\r'</c> and <c>'\\t'</c>.
52/// String | \b TrimStart | trims | String, String | Trims the string at the start by removing the whitespace characters listed in the second string.
53/// String | \b TrimEnd | trime | String | Trims the string at the end by removing the following whitespace characters: <c>' '</c>, <c>'\\n'</c>, <c>'\\r'</c> and <c>'\\t'</c>.
54/// String | \b TrimEnd | trime | String, String | Trims the string at the end by removing the whitespace characters listed in the second string.
55/// Integer | \b Integer | int | String | Parses an integral value from the string. Allows decimal, hexadecimal, octal and binary formats. Literal prefixes default to <c>'0x'</c>, <c>'0o'</c> and <c>'0b'</c>, which can be changed by configuring object \alib{format::Formatter;DefaultNumberFormat} of field \doxlinkproblem{structalib_1_1expressions_1_1Scope.html;aebf2cd4ff8a2611de06368fccfd3ef5b;Scope::Formatter;expressions::Scope::Formatter}.
56/// Float | \b Float | float | String | Parses a floating point value from the string. Allows scientific format and change of decimal point character by configuring object \alib{format::Formatter;DefaultNumberFormat} of field \doxlinkproblem{structalib_1_1expressions_1_1Scope.html;aebf2cd4ff8a2611de06368fccfd3ef5b;Scope::Formatter;expressions::Scope::Formatter}.
57/// String | \b Hexadecimal | hex | Integer | Converts an integral value to a hexadecimal string representation. Format options are available with object \alib{format::Formatter;DefaultNumberFormat} of field \doxlinkproblem{structalib_1_1expressions_1_1Scope.html;aebf2cd4ff8a2611de06368fccfd3ef5b;Scope::Formatter;expressions::Scope::Formatter}. The literal prefix (e.g., \c "0x") is not written.
58/// String | \b Hexadecimal | hex | Integer, Integer | Converts an integral value to a hexadecimal string representation of the given output width (2nd argument). Format options are available with object \alib{format::Formatter;DefaultNumberFormat} of field \doxlinkproblem{structalib_1_1expressions_1_1Scope.html;aebf2cd4ff8a2611de06368fccfd3ef5b;Scope::Formatter;expressions::Scope::Formatter}. The literal prefix (e.g., \c "0x") is not written.
59/// String | \b Octal | oct | Integer | Converts an integral value to a binary string representation. Format options are available with object \alib{format::Formatter;DefaultNumberFormat} of field \doxlinkproblem{structalib_1_1expressions_1_1Scope.html;aebf2cd4ff8a2611de06368fccfd3ef5b;Scope::Formatter;expressions::Scope::Formatter}. The literal prefix (e.g., \c "0x") is not written.
60/// String | \b Octal | oct | Integer, Integer | Converts an integral value to a binary string representation of the given output width (2nd argument). Format options are available with object \alib{format::Formatter;DefaultNumberFormat} of field \doxlinkproblem{structalib_1_1expressions_1_1Scope.html;aebf2cd4ff8a2611de06368fccfd3ef5b;Scope::Formatter;expressions::Scope::Formatter}. The literal prefix (e.g., \c "0x") is not written.
61/// String | \b Binary | bin | Integer | Converts an integral value to an octal string representation. Format options are available with object \alib{format::Formatter;DefaultNumberFormat} of field \doxlinkproblem{structalib_1_1expressions_1_1Scope.html;aebf2cd4ff8a2611de06368fccfd3ef5b;Scope::Formatter;expressions::Scope::Formatter}. The literal prefix (e.g., \c "0x") is not written.
62/// String | \b Binary | bin | Integer, Integer | Converts an integral value to an octal string representation of the given output width (2nd argument). Format options are available with object \alib{format::Formatter;DefaultNumberFormat} of field \doxlinkproblem{structalib_1_1expressions_1_1Scope.html;aebf2cd4ff8a2611de06368fccfd3ef5b;Scope::Formatter;expressions::Scope::Formatter}. The literal prefix (e.g., \c "0x") is not written.
63/// String | \b Format | format | String, ... | Formats the given variadic parameters according to the given format string. See notes below, for further information.
64/// Boolean | \b WildCardMatch | wcm | String, String | Matches a string against a wildcard pattern. See notes below, for further information.
65/// Boolean | \b RegExMatch | rem | String, String | Matches a string against a regex pattern. See notes below, for further information.
66///
67/// <br>
68/// <b>Unary Operators:</b><br>
69///
70/// | Return Type | Operator| Argument Type | Description|
71/// |-------------|---------|---------------|---------------------
72/// | String |<b>+</b> | String | Alias to function \b %ToUpper.
73/// | String |<b>-</b> | String | Alias to function \b %ToLower.
74/// | Boolean |<b>!</b> | String | Tests a string for emptiness. See notes below.
75///
76/// <br>
77/// <b>Binary Operators:</b><br>
78///
79/// | Return Type | Lhs Type | Operator | Rhs Type | Description
80/// |-------------|----------|------------|----------|--------------------
81/// |String | String |<b>+</b> | Integer | Concatenates an integral value to a string. The number conversion is performed using object \alib{format::Formatter;DefaultNumberFormat} of field \doxlinkproblem{structalib_1_1expressions_1_1Scope.html;aebf2cd4ff8a2611de06368fccfd3ef5b;Scope::Formatter;expressions::Scope::Formatter}.
82/// |String | String |<b>+</b> | Float | Concatenates a floating point value to a string. The number conversion is performed using object \alib{format::Formatter;DefaultNumberFormat} of field \doxlinkproblem{structalib_1_1expressions_1_1Scope.html;aebf2cd4ff8a2611de06368fccfd3ef5b;Scope::Formatter;expressions::Scope::Formatter}.
83/// |String | String |<b>+</b> | <any> | Converts a boxed value of an arbitrary type to a string and appends the result to another string. The conversion of the boxed value is performed by invoking box-function \alib{boxing;FAppend}.
84/// |String | Integer |<b>+</b> | String | Converts an integral value to a string and concatenates another string. The number conversion is performed using object \alib{format::Formatter;DefaultNumberFormat} of field \doxlinkproblem{structalib_1_1expressions_1_1Scope.html;aebf2cd4ff8a2611de06368fccfd3ef5b;Scope::Formatter;expressions::Scope::Formatter}.
85/// |String | Float |<b>+</b> | String | Converts a floating point value to a string and concatenates another string. The number conversion is performed using object \alib{format::Formatter;DefaultNumberFormat} of field \doxlinkproblem{structalib_1_1expressions_1_1Scope.html;aebf2cd4ff8a2611de06368fccfd3ef5b;Scope::Formatter;expressions::Scope::Formatter}.
86/// |String | <any> |<b>+</b> | String | Converts a boxed value of an arbitrary type to a string and appends another string. The conversion of the boxed value is performed by invoking box-function \alib{boxing;FAppend}.
87/// |String | String |<b>+</b> | String | Concatenates two strings.
88/// |Boolean | String |<b><</b> | String | Compares two strings using method \alib{strings;TString::CompareTo;String::CompareTo}.
89/// |Boolean | String |<b><=</b> | String | Compares two strings using method \alib{strings;TString::CompareTo;String::CompareTo}.
90/// |Boolean | String |<b>></b> | String | Compares two strings using method \alib{strings;TString::CompareTo;String::CompareTo}.
91/// |Boolean | String |<b>>=</b> | String | Compares two strings using method \alib{strings;TString::CompareTo;String::CompareTo}.
92/// |Boolean | String |<b>==</b> | String | Compares two strings using method \alib{strings;TString::Equals;String::Equals}.
93/// |Boolean | String |<b>!=</b> | String | Compares two strings using method \alib{strings;TString::Equals;String::Equals}.
94/// |Boolean | String |<b>*</b> | String | Alias to expression function \b %WildCardMatch. See notes below, for further information.
95/// |Boolean | String |<b>\%</b> | String | Alias to expression function \b %RegexMatch. See notes below, for further information.
96/// |Boolean | String |<b>[]</b> | Integer | Returns the substring of length \c 1 at index \e rhs in string \e lhs.
97///
98///
99///\I{##############################################################################################}
100/// # Notes And Hints #
101///
102/// \par Compile-Time Invokable:
103/// All callback functions are defined compile-time invokable. This means that redundancies in
104/// string expressions emerging from operations on constant strings are optimized (pruned) by the
105/// compiler.
106///
107/// \par Underlying %String %Types:
108/// While all string manipulation is based on \alib classes \alib{strings;TString;String},
109/// \alib{strings;TAString;AString} and \alib{strings;TSubstring;Substring}, due to the
110/// "harmonizing" way that string classes become boxed, none of these types appears as an expression
111/// result type. This means that custom expression functions can unbox strings that have been created
112/// by callback functions of this plug-in to their own custom string-type (e.g., \c std::string,
113/// \c QString, or whatever), and, the other way round, results of custom callback functions that
114/// returned custom strings objects, can seamlessly be used by functions and operators defined here.
115///
116/// \par
117/// Details of this are explained in chapters
118/// \ref alib_boxing_bijective "3. Non-Bijective Type Relationships" and
119/// \ref alib_boxing_strings "10. Boxing Character Strings" of the
120/// \ref alib_mod_boxing "Programmer's Manual" of module \alib_boxing_nl.
121///
122///
123/// \par Determine A String's Length
124/// The length of the string can be determined with function \b %Length, which is defined with
125/// compiler plugin \alib{expressions::plugins;Arithmetics} (because it works on all arrays).
126///
127/// \par Test For Empty %Strings:
128/// Unary operator \alib{expressions::DefaultUnaryOperators;BoolNot} (<c>'!'</c>) may be used to check if a
129/// string is not empty. The operator returns \c true if the string is empty and \c false otherwise.
130///
131/// \par
132/// Consequently, to test for non-empty strings, <c>'!!'</c> may be written. The outer
133/// <em>not-</em>operator is then a <em>not-</em>operator on the boolean result of the inner.<br>
134/// Alternatively, binary operators (see below) against an emtpy string can be used:
135///
136/// myIndentifier == ""
137/// myIndentifier != ""
138///
139/// \par
140/// As a result, this expressions is a tautology:
141///
142/// !myIdentifier == (myIndentifier == "")
143///
144/// \par
145/// Finally with strings, the built-in implementation of the
146/// \alib{expressions;plugins::ElvisOperator;elvis operator} is quite useful to avoid empty
147/// strings. The expression:
148///
149/// myIdentifier ?: "Default"
150///
151/// \par
152/// returns "Default" if \e myIdentifier evaluates to an empty string.
153///
154///
155/// \par Case conversion:
156/// Unary operators \alib{expressions::DefaultUnaryOperators;Positive} <c>'+'</c> and
157/// \alib{expressions::DefaultUnaryOperators;Negative} <c>'-'</c> are alias to functions \b %ToUpper and
158/// \b %ToLower.
159/// Hence, the expressions
160///
161/// ToUpper( "Hello " ) + ToLower( "World" )
162/// +"Hello " + -"World"
163/// are equivalent.
164///
165///
166/// \par Concatenation:
167/// Binary operator <c>'+'</c> is compiled by this plug-in if one of the arguments (or both) is of
168/// string-type. If one argument is not of string-type, it becomes converted. For the conversion,
169/// the following rules apply:
170///
171/// - Types \alib{expressions::Types;Integer} and \alib{expressions::Types;Float} are converted
172/// to string-types using object \alib{format::Formatter;DefaultNumberFormat} of
173/// field \doxlinkproblem{structalib_1_1expressions_1_1Scope.html;aebf2cd4ff8a2611de06368fccfd3ef5b;Scope::Formatter;expressions::Scope::Formatter}.
174/// With this, detailed options for the number output format is available.
175/// Even more options are available when using expression function \b %Format instead of
176/// the <c>'+'</c> operator.
177///
178/// <p>
179/// - Any other type is converted to a string representation by using box-function
180/// \alib{boxing;FAppend}. For information on how to implement this for custom
181/// types, consult the documentation of module \alib_boxing.
182/// The following provides a quick sample snippet, taken from the unit tests:
183///
184/// 1. The custom type:
185/// \snippet "ut_expr.cpp" DOX_EXPRESSIONS_STRINGOPS_IAPPEND_1
186///
187/// 2. Definition of functor \alib{strings;AppendableTraits} type <b>MyType</b>.
188/// Because the type will be boxed as a pointer, we also define the functor for the pointer type:
189/// \snippet "ut_expr.cpp" DOX_EXPRESSIONS_STRINGOPS_IAPPEND_2
190///
191/// 3. In the bootstrap section of an application, an implementation of the templated interface
192/// function has be registered with \alib_boxing_nl:
193/// \snippet "ut_expr.cpp" DOX_EXPRESSIONS_STRINGOPS_IAPPEND_3
194///
195/// With this short setup code, objects of type \e MyType, which might be returned by custom
196/// identifiers, can be added to strings in expressions!
197///
198/// <p>
199/// - With type \alib{expressions::Types;Boolean}, the \alib{resources;ResourcePool;resourced}
200/// string values generally used with box-function \alib{boxing;FAppend} defined for
201/// C++ type \c bool are used.
202/// These string resources default to \b "true" and \b "false". While resources can be customized,
203/// such customization has process-wide effect.<br>
204/// To express other values, \ref alib_expressions_builtin_ternary "conditional operator"
205/// (<c>Q ? T : F</c>) can be used by the end-user:
206///
207/// "Is true: " + {boolean expression} // Uses the global resourced strings
208/// "Is true: " + ( {boolean expression} ? "Yes" : "No" ) // Custom conversion per expression.
209///
210/// \par Comparison:
211/// Operators <c>'=='</c>, <c>'!='</c>, <c>'<'</c>, <c>'<='</c>, <c>'>'</c> and <c>'>='</c>
212/// perform comparisons between two string operands.
213/// Internally, to perform the comparison, methods \alib{strings;TString::Equals;String::Equals} and
214/// \alib{strings;TString::CompareTo;String::CompareTo} are used.
215///
216/// case-insensitive comparison can be performed by converting the operand(s) to upper case
217/// using expression function \b %ToUpper or its "alias operator", unary <c>'+'</c>.
218/// A more effective way is to use overloaded function \b %Compare that accepts an optional
219/// third operator of type \b %Boolean, which if \c true is given performs a case-insensitive
220/// search without performing string conversions.
221///
222/// \anchor alib_expressions_Strings_Formatting
223/// \par Formatting:
224/// Function <b>Format(String, ...)</b> offers full featured string formatting in expressions.
225/// Being based on classes of underlying module \alib_format, there is a choice between
226/// \alib{format;FormatterPythonStyle;python style} or
227/// \alib{format;FormatterJavaStyle;Java/printf style} format strings. It is even possible
228/// to allow both formats, of course not within the same string, but within different
229/// evaluations of the same expression!
230///
231/// \par
232/// Please consult the documentation of classes
233/// - \alib{format;Formatter},
234/// - \alib{format;FormatterPythonStyle;FormatterPythonStyle} and
235/// - \alib{format;FormatterJavaStyle;FormatterJavaStyle} for details.
236///
237/// \par Wildcard Match:
238/// Wildcard match is implemented with expression function \b %WildcardMatch, respectively its "alias
239/// operator" <c>'*'</c>.
240/// The first (left-hand side) argument is the
241/// string that is searched, the second (right-hand side) argument is the pattern string to be
242/// matched.
243///
244/// \par
245/// Wildcards characters are (<c>'*'</c>) and (<c>'?'</c>). For example, expressions
246///
247/// WildcardMatch("This is ALib Expressions", "*A?ib*")
248/// "This is ALib Expressions" * "*A?ib*"
249///
250/// are equivalent and return boolean \c true.
251///
252/// \par
253/// For case-insensitive search, the both strings are to be converted to upper case, as in
254/// expression:
255///
256/// +filename * "*.JPG"
257///
258/// \note
259/// A more performant alternative for the latest sample expression is:
260///
261/// EndsWith( filename, ".jpg", true )
262///
263/// \par Regular %Expression Match:
264/// Regular expression match is implemented with expression function \b %RegexMatch, respectively its
265/// "alias operator" <c>'\%'</c>.
266/// The first (left-hand side) argument is the
267/// string that is searched, the second (right-hand side) argument is the pattern string to be matched.
268///
269/// \par
270/// The regular expression syntax is compatible with
271/// \https{Perl Regular Expressions,perldoc.perl.org/perlretut.html}.
272/// The feature is implemented with using \https{boost::regex library,www.boost.org}.
273///
274/// \attention
275/// The regular expression features of \alib and therefore of this compiler plug-in, are only
276/// available with the definition of the compiler-symbol \ref ALIB_FEAT_BOOST_REGEX
277/// and if either \ref ALIB_CHARACTERS_WIDE is \c false or \ref ALIB_CHARACTERS_NATIVE_WCHAR
278/// equals \c true.
279///
280///\I{##############################################################################################}
281/// # Reference Documentation #
282//==================================================================================================
284{
285 //==============================================================================================
286 /// Constructor. Creates the hash map.
287 /// @param compiler The compiler we will get attached to.
288 //==============================================================================================
290
291 //==============================================================================================
292 /// Virtual destructor
293 //==============================================================================================
294 virtual ~Strings() override
295 {}
296
297 //==============================================================================================
298 /// Overwrites the implementation of class \b %Calculus. While this is usually not needed,
299 /// this class uses this for wildcard and regular expression matching functions.
300 /// These functions implement a special behavior in respect to allocate "matcher" classes
301 /// at compile-time in the case that the pattern string is constant (which usually is).
302 /// Parent helper-class \alib{expressions::plugins;Calculus} does provide mechanics for such
303 /// rather complicated special actions.
304 ///
305 /// Of course, the original method is called alternatively.
306 ///
307 /// @param[in,out] ciFunction The compilation info struct.
308 /// @return \c true if compilation information was given, \c false otherwise.
309 //==============================================================================================
311 virtual bool TryCompilation( CIFunction& ciFunction) override;
312
313 //==============================================================================================
314 /// Overwrites the implementation of class \b %Calculus. While this is usually not needed,
315 /// this class uses this to fetch arbitrary boxed types for certain operations because
316 /// the interface mechanics of module \alib_boxing allows us to convert any custom type
317 /// to a string.
318 ///
319 /// Of course, the original method is called alternatively.
320 ///
321 /// @param[in,out] ciBinaryOp The compilation info struct.
322 /// @return \c true if compilation information was given, \c false otherwise.
323 //==============================================================================================
325 virtual bool TryCompilation( CIBinaryOp& ciBinaryOp ) override;
326
327};
328
329//==================================================================================================
330/// This is the callback method for string function <b>%String(...)</b>, which converts an arbitrary
331/// number of arguments of arbitrary types to a concatenated string.
332///
333/// The function is compile-time invokable.
334///
335/// \note
336/// As an exception to the rule, this function is not defined in an anonymous namespace, but
337/// exposed through the C++ header-file of this struct. The rationale for this
338/// is that the function is also used for auto-casting custom types to strings, which is performed
339/// with compiler plug-in \alib{expressions;plugins::AutoCast}.
340///
341/// @param scope The scope.
342/// @param args The single argument.
343/// @return The boxed result string, allocated inside the scope's mono allocator.
344//==================================================================================================
347
348//==================================================================================================
349/// This is the callback method for string function <b>%Format(...)</b>, which formats an arbitrary
350/// number of arguments according to a given format string.
351///
352/// The function is compile-time invokable.
353///
354/// \note
355/// As an exception to the rule, this function is not defined in an anonymous namespace, but
356/// exposed through the C++ header-file of this struct. The rationale for this
357/// is that the function this way can be called by other plug-ins as well.
358///
359/// @param scope The scope.
360/// @param args The single argument.
361/// @return The boxed result string, allocated inside the scope's mono allocator.
362//==================================================================================================
365
366}}} // namespace [alib::expressions::plugins]
367
#define ALIB_DLL
Definition alib.inl:496
#define ALIB_EXPORT
Definition alib.inl:488
ALIB_DLL Box CBFormat(Scope &scope, ArgIterator args, ArgIterator)
ALIB_DLL Box CBToString(Scope &scope, ArgIterator args, ArgIterator)
StdVectorMono< Box >::iterator ArgIterator
boxing::Box Box
Type alias in namespace alib.
Definition box.inl:1216
virtual ALIB_DLL bool TryCompilation(CIFunction &ciFunction) override
virtual ALIB_DLL bool TryCompilation(CIBinaryOp &ciBinaryOp) override
ALIB_DLL Strings(Compiler &compiler)
virtual ~Strings() override
Virtual destructor.
Definition strings.inl:294