This built-in CompilerPlugin of ALib Expressions compiles identifiers, functions and operators with character string-type operands or return types.
By default, this plug-in is automatically created and inserted into each instance of class Compiler with the invocation of SetupDefaults.
Constants:
Type | Name | Min. Abbreviation | Description |
---|---|---|---|
String | NewLine | nl | System dependent new line character combination ( "\n" or "\r\n" ). |
String | Tabulator | tab | String with single tabulator character ("\t" ). |
Functions:
Note: All identifier and function names are defined case insensitive.
Return Type | Name | Min. Abbreviation | Signature | Description |
---|---|---|---|---|
String | String | str | ... | Creates and returns a string representation of the arguments, concatenated from left to right. |
String | ToUpper | tu | String | Converts all appropriate characters of a string to upper case letters. This function is aliased by unary operator '+' . |
String | ToLower | tl | String | Converts all appropriate characters of a string to lower case letters. This function is aliased by unary operator '-' . |
Boolean | Compare | comp | String, String | Returns 0 if strings are equal, a negative value if the first string is "smaller" than the second and a positive otherwise. |
Boolean | Compare | comp | String, String, Boolean | Same as Compare(String,String) if third parameter is false . If it is true letter case is ignored. |
Boolean | StartsWith | sw | String, String | Tests if the first given string starts with the second. Comparison is case-sensitive. |
Boolean | StartsWith | sw | String, String, Boolean | Tests if the first given string starts with the second. The third parameter is true , letter case is ignored. |
Boolean | EndsWith | ew | String, String | Tests if the first given string ends with the second. Comparison is case-sensitive. |
Boolean | EndsWith | ew | String, String, Boolean | Tests if the first given string ends with the second. The third parameter is true , letter case is ignored. |
String | Substring | subs | String, Integer | Returns the substring, starting at given position (2nd argument) to the end of the string.. |
String | Substring | subs | String, Integer, Integer | Returns the substring, starting at given position (2nd argument) with the given length (3rd argument). |
Integer | IndexOf | indo | String, String | Searches the first occurrence of the second string in the first. Returns the position if found, otherwise -1 . |
Integer | Count | count | String, String | Counts the number of occurrences of the second string in the first. |
String | Replace | repl | String, String, String | Returns a copy of the 1st argument, with occurrences of 2nd argument replaced by 3rd argument. |
String | Repeat | repeat | String, Integer | Returns 1st argument concatenated N times (2nd argument). |
String | Token | tok | String, String, Integer | Parses tokens separated by character (2nd argument) in string (1st argument) an returns the n-th (3rd argument). |
String | Trim | trim | String | Trims the string at both sides by removing the following whitespace characters: ' ' , '\n' , '\r' and '\t' . |
String | Trim | trim | String, String | Trims the string at both sides by removing the whitespace characters listed in the second string. |
String | TrimStart | trims | String | Trims the string at the start by removing the following whitespace characters: ' ' , '\n' , '\r' and '\t' . |
String | TrimStart | trims | String, String | Trims the string at the start by removing the whitespace characters listed in the second string. |
String | TrimEnd | trime | String | Trims the string at the end by removing the following whitespace characters: ' ' , '\n' , '\r' and '\t' . |
String | TrimEnd | trime | String, String | Trims the string at the end by removing the whitespace characters listed in the second string. |
Integer | Integer | int | String | Parses an integral value from the string. Allows decimal, hexadecimal, octal and binary formats. Literal prefixes default to '0x' , '0o' and '0b' , which can be changed by configuring object DefaultNumberFormat of field Scope::Formatter. |
Float | Float | float | String | Parses a floating point value from the string. Allows scientific format and change of decimal point character by configuring object DefaultNumberFormat of field Scope::Formatter. |
String | Hexadecimal | hex | Integer | Converts an integral value to a hexadecimal string representation. Format options are available with object DefaultNumberFormat of field Scope::Formatter. The literal prefix (e.g., "0x" ) is not written. |
String | 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 DefaultNumberFormat of field Scope::Formatter. The literal prefix (e.g., "0x" ) is not written. |
String | Octal | oct | Integer | Converts an integral value to a binary string representation. Format options are available with object DefaultNumberFormat of field Scope::Formatter. The literal prefix (e.g., "0x" ) is not written. |
String | 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 DefaultNumberFormat of field Scope::Formatter. The literal prefix (e.g., "0x" ) is not written. |
String | Binary | bin | Integer | Converts an integral value to an octal string representation. Format options are available with object DefaultNumberFormat of field Scope::Formatter. The literal prefix (e.g., "0x" ) is not written. |
String | 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 DefaultNumberFormat of field Scope::Formatter. The literal prefix (e.g., "0x" ) is not written. |
String | Format | format | String, ... | Formats the given variadic parameters according to the given format string. See notes below, for further information. |
Boolean | WildCardMatch | wcm | String, String | Matches a string against a wildcard pattern. See notes below, for further information. |
Boolean | RegExMatch | rem | String, String | Matches a string against a regex pattern. See notes below, for further information. |
Unary Operators:
Return Type | Operator | Argument Type | Description |
---|---|---|---|
String | + | String | Alias to function ToUpper. |
String | - | String | Alias to function ToLower. |
Boolean | ! | String | Tests a string for emptiness. See notes below. |
Binary Operators:
Return Type | Lhs Type | Operator | Rhs Type | Description |
---|---|---|---|---|
String | String | + | Integer | Concatenates an integral value to a string. The number conversion is performed using object DefaultNumberFormat of field Scope::Formatter. |
String | String | + | Float | Concatenates a floating point value to a string. The number conversion is performed using object DefaultNumberFormat of field Scope::Formatter. |
String | String | + | <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 FAppend. |
String | Integer | + | String | Converts an integral value to a string and concatenates another string. The number conversion is performed using object DefaultNumberFormat of field Scope::Formatter. |
String | Float | + | String | Converts a floating point value to a string and concatenates another string. The number conversion is performed using object DefaultNumberFormat of field Scope::Formatter. |
String | <any> | + | 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 FAppend. |
String | String | + | String | Concatenates two strings. |
Boolean | String | < | String | Compares two strings using method String::CompareTo. |
Boolean | String | <= | String | Compares two strings using method String::CompareTo. |
Boolean | String | > | String | Compares two strings using method String::CompareTo. |
Boolean | String | >= | String | Compares two strings using method String::CompareTo. |
Boolean | String | == | String | Compares two strings using method String::Equals. |
Boolean | String | != | String | Compares two strings using method String::Equals. |
Boolean | String | * | String | Alias to expression function WildCardMatch. See notes below, for further information. |
Boolean | String | % | String | Alias to expression function RegexMatch. See notes below, for further information. |
Boolean | String | [] | Integer | Returns the substring of length 1 at index rhs in string lhs. |
std::string
, QString
, or whatever), and, the other way round, results of custom callback functions that returned custom strings objects, can seamlessly be used by functions and operators defined here.'!'
) may be used to check if a string is not empty. The operator returns true
if the string is empty and false
otherwise.'!!'
may be written. The outer not-operator is then a not-operator on the boolean result of the inner.myIndentifier == "" myIndentifier != ""
!myIdentifier == (myIndentifier == "")
myIdentifier ?: "Default"
'+'
and Negative '-'
are alias to functions ToUpper and ToLower. Hence, the expressions ToUpper( "Hello " ) + ToLower( "World" ) +"Hello " + -"World"are equivalent.
'+'
is compiled by this plug-in if one of the arguments (or both) is of string-type. If one argument is not of string-type, it becomes converted. For the conversion, the following rules apply:'+'
operator.Any other type is converted to a string representation by using box-function FAppend. For information on how to implement this for custom types, consult the documentation of module ALib Boxing. The following provides a quick sample snippet, taken from the unit tests:
With this short setup code, objects of type MyType, which might be returned by custom identifiers, can be added to strings in expressions!
bool
are used. These string resources default to "true" and "false". While resources can be customized, such customization has process-wide effect.Q ? T : F
) can be used by the end-user: "Is true: " + {boolean expression} // Uses the global resourced strings "Is true: " + ( {boolean expression} ? "Yes" : "No" ) // Custom conversion per expression.
'=='
, '!='
, '<'
, '<='
, '>'
and '>='
perform comparisons between two string operands. Internally, to perform the comparison, methods String::Equals and String::CompareTo are used.Case insensitive comparison can be performed by converting the operand(s) to upper case using expression function ToUpper or its "alias operator", unary '+'
. A more effective way is to use overloaded function Compare that accepts an optional third operator of type Boolean, which if true
is given performs a case insensitive search without performing string conversions.
'*'
. The first (left-hand side) argument is the string that is searched, the second (right-hand side) argument is the pattern string to be matched.'*'
) and ('?'
). For example, expressions WildcardMatch("This is ALib Expressions", "*A?ib*") "This is ALib Expressions" * "*A?ib*"
are equivalent and return boolean true
.
+filename * "*.JPG"
EndsWith( filename, ".jpg", true )
'%'
. The first (left-hand side) argument is the string that is searched, the second (right-hand side) argument is the pattern string to be matched.false
or ALIB_CHARACTERS_NATIVE_WCHAR equals true
. Definition at line 289 of file strings.hpp.
#include <strings.hpp>
Public Method Index: | |
ALIB_API | Strings (Compiler &compiler) |
virtual | ~Strings () override |
Virtual destructor. | |
virtual ALIB_API bool | TryCompilation (CIBinaryOp &ciBinaryOp) override |
virtual ALIB_API bool | TryCompilation (CIFunction &ciFunction) override |
Public Method Index: inherited from Calculus | |
Calculus (const NString &name, Compiler &compiler, CompilePriorities pPriority) | |
virtual | ~Calculus () override |
Virtual destructor. | |
ALIB_API void | AddBinaryOpOptimizations (BinaryOpOptimizationsTableEntry *table, size_t length) |
template<size_t TCapacity> | |
void | AddBinaryOpOptimizations (BinaryOpOptimizationsTableEntry(&table)[TCapacity]) |
void | AddOperator (const String &op, Type lhsType, Type rhsType, CallbackDecl callback, const char *dbgCallbackName, Type resultType, CTInvokable cti) |
void | AddOperatorAlias (const String &alias, Type lhsType, Type rhsType, const String &op) |
void | AddOperatorAliases (OperatorAliasTableEntry *table, size_t length) |
template<size_t TCapacity> | |
void | AddOperatorAliases (OperatorAliasTableEntry(&table)[TCapacity]) |
ALIB_API void | AddOperators (OperatorTableEntry *table, size_t length) |
template<size_t TCapacity> | |
void | AddOperators (OperatorTableEntry(&table)[TCapacity]) |
virtual ALIB_API bool | TryCompilation (CIAutoCast &autoCast) override |
virtual ALIB_API bool | TryCompilation (CIUnaryOp &ciUnaryOp) override |
Public Method Index: inherited from CompilerPlugin | |
CompilerPlugin (const NString &name, Compiler &compiler, CompilePriorities pPriority) | |
virtual | ~CompilerPlugin () |
Virtual destructor. | |
Public Method Index: inherited from Plugin< Compiler, CompilePriorities > | |
PrioritiesType | GetPriority () const |
Additional Inherited Members | |
Public Type Index: inherited from Calculus | |
using | BinaryOpOptimizationsTableEntry = const std::tuple<String, lang::Side, Type, const Box&, const Box&> |
using | CTInvokable = bool |
using | OperatorAliasTableEntry = const std::tuple<String, Type, Type, String> |
using | OperatorTableEntry |
Public Type Index: inherited from Plugin< Compiler, CompilePriorities > | |
using | PluginType |
This exposes the template parameter TPlugin to the outer world. | |
using | PrioritiesType |
This exposes the template parameter pTPlugin to the outer world. | |
Public Static Field Index: inherited from Calculus | |
static constexpr CTInvokable | CTI = true |
static constexpr CTInvokable | ETI = false |
Public Field Index: inherited from Calculus | |
MonoAllocator | allocator |
std::vector< AutoCastEntry > | AutoCasts |
List of auto-casts to be compiled by this plug-in. | |
HashMap< MonoAllocator, BinOpOptKey, Box, BinOpOptKey::Hash, BinOpOptKey::EqualTo > | BinaryOperatorOptimizations |
std::vector< ConstantIdentifierEntry > | ConstantIdentifiers |
List of identifiers that return constant values to be compiled by this plug-in. | |
std::vector< FunctionEntry > | Functions |
List of functions to be compiled by this plug-in. | |
HashMap< MonoAllocator, OperatorKey, String, OperatorKey::Hash, OperatorKey::EqualTo > | OperatorAliases |
HashMap< MonoAllocator, OperatorKey, std::tuple< CallbackDecl, Box, CTInvokable ALIB_DBG(, const char *) >, OperatorKey::Hash, OperatorKey::EqualTo > | Operators |
Public Field Index: inherited from CompilerPlugin | |
Compiler & | Cmplr |
The compiler that this plug-in is attached to. | |
const NString | Name |
Protected Field Index: inherited from Plugin< Compiler, CompilePriorities > | |
PrioritiesType | priority |
The priority of this plug-in. | |
Protected Method Index: inherited from Plugin< Compiler, CompilePriorities > | |
Plugin (PrioritiesType pPriority) | |
Constructor. Creates the hash map.
compiler | The compiler we will get attached to. |
|
inlineoverridevirtual |
Virtual destructor.
Definition at line 300 of file strings.hpp.
|
overridevirtual |
Overwrites the implementation of class Calculus. While this is usually not needed, this class uses this to fetch arbitrary boxed types for certain operations because the interface mechanics of module ALib Boxing allows us to convert any custom type to a string.
Of course, the original method is called alternatively.
[in,out] | ciBinaryOp | The compilation info struct. |
true
if compilation information was given, false
otherwise. Reimplemented from Calculus.
|
overridevirtual |
Overwrites the implementation of class Calculus. While this is usually not needed, this class uses this for wildcard and regular expression matching functions. These functions implement a special behavior in respect to allocate "matcher" classes at compile-time in the case that the pattern string is constant (which usually is). Parent helper-class Calculus does provide mechanics for such rather complicated special actions.
Of course, the original method is called alternatively.
[in,out] | ciFunction | The compilation info struct. |
true
if compilation information was given, false
otherwise. Reimplemented from Calculus.