C12Adapter Opensource C++ Interface
MAlgorithm Class Reference

Set of various, mostly string related algorithms. More...

Inheritance diagram for MAlgorithm:

Static Public Member Functions

static int Find (const MVariant &sequence, const MVariant &subSequence)
 Find first occurrence of string in another string. More...
 
static int FindReverse (const MVariant &sequence, const MVariant &subSequence)
 Find last occurrence of string in another string. More...
 
static MVariant Replace (const MVariant &source, const MVariant &from, const MVariant &to)
 Replaces all occurrences of 'from' in the source sequence with 'to'. More...
 
static void InplaceSort (MStdStringVector &collection, bool uniqueOnly=false, bool naturalSort=false)
 Sort the string vector given, possibly remove duplicates. More...
 
static MVariant Sort (const MVariant &sequence, bool uniqueOnly=false)
 Return the given sequence sorted, possibly remove duplicates. More...
 
static void InplaceTrimLeft (MStdString &str, MConstChars trimCharacters=NULL)
 Trim the leading characters from the string given, modify this string. More...
 
static void InplaceTrimRight (MStdString &str, MConstChars trimCharacters=NULL)
 Trim the trailing characters from the string given, modify this string. More...
 
static void InplaceTrim (MStdString &str, MConstChars trimCharacters=NULL)
 Trim the leading and trailing characters from the string given, modify this string. More...
 
static MVariant TrimLeft (const MVariant &str, const MVariant &trimCharacters)
 Trim the leading characters or bytes from the string given, return the result. More...
 
static MVariant TrimRight (const MVariant &str, const MVariant &trimCharacters)
 Trim the trailing characters or bytes from the string given, return the result. More...
 
static MVariant Trim (const MVariant &str, const MVariant &trimCharacters)
 Trim the leading and trailing characters or bytes from the string given, return the result. More...
 
static MStdString TrimString (const MStdString &str, MConstChars trimCharacters=NULL)
 Specialized faster version of Trim that works on string type rather than on MVariant. More...
 
static MVariant Split (const MVariant &str, const MVariant &delimiter, bool trimBlanks=false, bool allowEmpty=false)
 Split the string separated by delimiter into a collection of strings. More...
 
static MVariant Join (const MVariant &source, const MVariant &delimiter)
 Join the given array of items into a string or byte string using the given delimiter. More...
 
static void AddUnique (MStdStringVector &source, const MStdString &value)
 Add string value to string collection if it is not there yet. More...
 
static MStdStringVector SplitWithDelimiter (const MStdString &str, char delimiter, bool trimBlanks=false, bool allowEmpty=false)
 
static MStdStringVector SplitWithDelimiter (const MStdString &str, const MStdString &delimiter, bool trimBlanks=false, bool allowEmpty=false)
 
- Static Public Member Functions inherited from MObject
static const MClassGetStaticClass ()
 Get the declared class of this particular object. More...
 
static bool IsClassPresent (const MStdString &name)
 Tells if the given class name is available. More...
 

Additional Inherited Members

- Public Member Functions inherited from MObject
virtual ~MObject ()
 Object destructor.
 
virtual const MClassGetClass () const =0
 Get the final class of the object. More...
 
virtual unsigned GetEmbeddedSizeof () const
 For embedded object types, return the size of the class. More...
 
bool IsEmbeddedObject () const
 Tell if the object is of embedded kind. More...
 
SHOW_INTERNAL MVariant Call (const MStdString &name, const MVariant &params)
 Call the object service with parameters, given as variant. More...
 
MVariant Call0 (const MStdString &name)
 Call the object service with no parameters. More...
 
MVariant Call1 (const MStdString &name, const MVariant &p1)
 Call the object service with one parameter. More...
 
MVariant Call2 (const MStdString &name, const MVariant &p1, const MVariant &p2)
 Call the object service with two parameter. More...
 
MVariant Call3 (const MStdString &name, const MVariant &p1, const MVariant &p2, const MVariant &p3)
 Call the object service with three parameter. More...
 
MVariant Call4 (const MStdString &name, const MVariant &p1, const MVariant &p2, const MVariant &p3, const MVariant &p4)
 Call the object service with four parameter. More...
 
MVariant Call5 (const MStdString &name, const MVariant &p1, const MVariant &p2, const MVariant &p3, const MVariant &p4, const MVariant &p5)
 Call the object service with five parameter. More...
 
MVariant Call6 (const MStdString &name, const MVariant &p1, const MVariant &p2, const MVariant &p3, const MVariant &p4, const MVariant &p5, const MVariant &p6)
 Call the object service with six parameter. More...
 
virtual MVariant CallV (const MStdString &name, const MVariant::VariantVector &params)
 Call the object service with parameters, given as variant vector. More...
 
virtual bool IsPropertyPresent (const MStdString &name) const
 Tell if the property with the given name exists.
 
virtual bool IsServicePresent (const MStdString &name) const
 Tell if the service with the given name exists.
 
virtual MVariant GetProperty (const MStdString &name) const
 Get the property value using name of the property. More...
 
virtual void SetProperty (const MStdString &name, const MVariant &value)
 Set the property using name of the property, and value. More...
 
virtual MStdStringVector GetAllPropertyNames () const
 Return the list of publicly available properties, persistent or not. More...
 
virtual MStdStringVector GetAllPersistentPropertyNames () const
 Return the list of persistent properties. More...
 
virtual void SetPersistentPropertiesToDefault ()
 Set the persistent properties of the object to their default values. More...
 
virtual MVariant GetPersistentPropertyDefaultValue (const MStdString &name) const
 Get the default value of persistent property with the name given. More...
 
virtual void SetPersistentPropertyToDefault (const MStdString &name)
 Set the persistent property with the name given to default value. More...
 
virtual const char * GetType () const
 Get the name of the type for the object (could be the same as class name).
 
virtual void SetType (const MStdString &)
 Intentionally, it will set the name of the type for the object, but the service will not allow setting the name to anything other than the current name. More...
 
virtual void Validate ()
 Validate internal structures of the object. More...
 
- Static Public Attributes inherited from MObject
static const MClass s_class
 Class of MObject.
 
- Protected Member Functions inherited from MObject
 MObject ()
 Object constructor, protected as the class is abstract.
 
void DoSetPersistentPropertiesToDefault (const MClass *staticClass)
 Set the persistent properties to their default values for one object provided the class for that object. More...
 

Detailed Description

Set of various, mostly string related algorithms.

This includes:

  • Searching in strings and collections
  • Replacing subsequences in strings and collections
  • Sorting of collections
  • Removing (trimming) blanks or other characters from the beginning or the end of the sequence
  • Splitting strings into collections and joining them back using delimiters

Member Function Documentation

static void MAlgorithm::AddUnique ( MStdStringVector source,
const MStdString value 
)
static

Add string value to string collection if it is not there yet.

This is a C++ only method.

Parameters
sourceSource string collection into which the value will be attempted to be added
valueValue to be present in collection after the call succeeds
static int MAlgorithm::Find ( const MVariant sequence,
const MVariant subSequence 
)
static

Find first occurrence of string in another string.

Can work with strings, byte strings, or collections of items. Searches the given sequence in forward direction for the first occurrence of a subsequence.

Parameters
sequenceSource string or collection, the one where to find a subSequence Can be of types string, byte string, or collection of items, and the type should be the same as for subSequence, or an exception is thrown.
subSequenceString or collection to find in sequence Can be of types string, byte string, or collection of items, and the type should be the same as for sequence, or an exception is thrown.
Returns
When successful, returns zero based index of first character that matches the first substring of the given string. When there is no substring found, returns -1.
static int MAlgorithm::FindReverse ( const MVariant sequence,
const MVariant subSequence 
)
static

Find last occurrence of string in another string.

Can work with strings, byte strings, or collections of items. Searches the given sequence in backward direction for the last occurrence of a subsequence. Return the index of the first element of subSequence.

Parameters
sequenceSource string or collection, the one where to find a subSequence. Can be of types string, byte string, or collection of items, and the type should be the same as for subSequence, or an exception is thrown.
subSequenceString or collection to find in sequence Can be of types string, byte string, or collection of items, and the type should be the same as for sequence, or an exception is thrown.
Returns
When successful, returns zero based index of first character that matches the last substring of the given string. When there is no substring found, return -1.
static void MAlgorithm::InplaceSort ( MStdStringVector collection,
bool  uniqueOnly = false,
bool  naturalSort = false 
)
static

Sort the string vector given, possibly remove duplicates.

Different from Sort, this method changes the given collection and does not return a new collection. This is a C++ only method, not available through Reflection interface.

Parameters
collectionSequence of type MStdStringVector to sort.
uniqueOnlyIf true, return only unique items. Default value is false, return all items sorted, with possible duplicates.
naturalSortNatural sort is case insensitive, and multidigit numbers are compared as numbers. This way, the sorted sequence will be such as COM1 COM2 COM11, instead of COM1 COM11 COM2 as in the case of lexicographical comparison.
See also
Sort(const MVariant&, bool)
static void MAlgorithm::InplaceTrim ( MStdString str,
MConstChars  trimCharacters = NULL 
)
static

Trim the leading and trailing characters from the string given, modify this string.

This is a non-reflective C++ version of Trim.

Parameters
strString to remove characters from, the given string will be modified.
trimCharactersSet of characters to remove. If NULL is given, or if the parameter is not present, the control characters and blanks are removed. If given, this should be a zero terminated string of what to remove, such as "\n\t ".
See also
Trim - reflection-enabled version that returns a new sequence rather than modifies the current
InplaceTrimRight(MStdString&, MConstChars)
InplaceTrimLeft(MStdString&, MConstChars)
static void MAlgorithm::InplaceTrimLeft ( MStdString str,
MConstChars  trimCharacters = NULL 
)
static

Trim the leading characters from the string given, modify this string.

This is a non-reflective C++ version of TrimLeft.

Parameters
strString to remove characters from, the given string will be modified.
trimCharactersSet of characters to remove. If NULL is given, or if the parameter is not present, control characters and blanks are removed. If given, this should be a zero terminated string of what to remove, such as "\n\t ".
See also
TrimLeft - reflection-enabled version that returns a new sequence rather than modifies the current
InplaceTrim(MStdString&, MConstChars)
InplaceTrimRight(MStdString&, MConstChars)
static void MAlgorithm::InplaceTrimRight ( MStdString str,
MConstChars  trimCharacters = NULL 
)
static

Trim the trailing characters from the string given, modify this string.

This is a non-reflective C++ version of TrimRight.

Parameters
strString to remove characters from, the given string will be modified.
trimCharactersSet of characters to remove. If NULL is given, or if the parameter is not present, control characters and blanks are removed. If given, this should be a zero terminated string of what to remove, such as "\n\t ".
See also
TrimRight - reflection-enabled version that returns a new sequence rather than modifies the current
InplaceTrim(MStdString&, MConstChars)
InplaceTrimLeft(MStdString&, MConstChars)
static MVariant MAlgorithm::Join ( const MVariant source,
const MVariant delimiter 
)
static

Join the given array of items into a string or byte string using the given delimiter.

If delimiter is of byte, or byte string type, the result string will be a byte string. Otherwise the result will be a string. All items in source should be convertible into the result type.

Parameters
sourceSequence of items to join together. This should be a collection of items convertible into either string or to byte string.
delimiterDelimiter to place in between source strings.
Returns
String or byte string is returned, depending on the type of delimiter.
See also
Split - reverse function of Join.
static MVariant MAlgorithm::Replace ( const MVariant source,
const MVariant from,
const MVariant to 
)
static

Replaces all occurrences of 'from' in the source sequence with 'to'.

Can work with strings, byte strings, or collections of items. The sequences are not necessarily of the same length, but they should be assignable one to another.

Parameters
sourceSequence where to replace the items. Can be of types string, byte string, or collection of items, or an exception is thrown.
fromSequence to replace. Should be of the same type as source parameter, or an exception is thrown.
toReplacement sequence. Should be of the same type as source parameter, or an exception is thrown.
Returns
Result string is returned. If there nothing is replaced, the string is returned unchanged.
static MVariant MAlgorithm::Sort ( const MVariant sequence,
bool  uniqueOnly = false 
)
static

Return the given sequence sorted, possibly remove duplicates.

Parameters
sequenceWhat to sort. The method does not change the parameter, but rather returns it. Shall be of type string, byte string, or collection, or an exception is thrown.
uniqueOnlyIf true, return only unique items. Default value is false, return all items sorted, with possible duplicates.
Returns
Sorted sequence is returned.
static MVariant MAlgorithm::Split ( const MVariant str,
const MVariant delimiter,
bool  trimBlanks = false,
bool  allowEmpty = false 
)
static

Split the string separated by delimiter into a collection of strings.

Empty items are not put into collection, like if the list has a separator at the end, it will not be part of the output vector. If the string has no separators, only one item will be returned.

Parameters
strString or byte string to split
delimiterString or byte string separator, can be one character such as ";", or multiple characters like ", " (there is a blank at the end). The type of delimiter shall be the same as the type of source, or an exception is thrown.
trimBlanksBoolean value, whether to remove blanks from each result string in the collection. If false, or if the parameter is not given, the blanks will be retained in the result string.
allowEmptyBoolean value, whether to retain empty strings in the result collection. For example, if allowEmpty is false, three empty strings will result from string ",," if separator is ",". When allowEmpty is true, the result will be an empty collection. If not given, this parameter defaults to false, and the result collection can have empty strings.
Returns
The vector of items is returned.
See also
Join - reverse function of Split.
static MStdStringVector MAlgorithm::SplitWithDelimiter ( const MStdString str,
char  delimiter,
bool  trimBlanks = false,
bool  allowEmpty = false 
)
static

Split the string into a vector of strings using the given delimiter sequence.

This is a fast C++ variation of split for strings, not reflected. Empty items are not put into collection, like if the list has a separator at the end, it will not be part of the output vector. If the string has no separators, only one item is returned.

Parameters
strString to split
delimiterString separator, can be one character such as ";", or multiple characters like ", " (there is a blank at the end in this example)
trimBlanksBoolean value, whether to remove blanks from each result string in the collection. If false, or if the parameter is not given, the blanks will be retained in the result string.
allowEmptyBoolean value, whether to retain empty strings in the result collection. For example, if allowEmpty is false, three empty strings will result from string ",," if separator is ",". When allowEmpty is true, the result will be an empty collection. If not given, this parameter defaults to false, and the result collection can have empty strings.
Returns
The vector of items is returned.
See also
Split - reflection enabled universal function.
Join - Reverse function.
static MStdStringVector MAlgorithm::SplitWithDelimiter ( const MStdString str,
const MStdString delimiter,
bool  trimBlanks = false,
bool  allowEmpty = false 
)
static

Split the string into a vector of strings using the given delimiter sequence.

This is a fast C++ variation of split for strings, not reflected. Empty items are not put into collection, like if the list has a separator at the end, it will not be part of the output vector. If the string has no separators, only one item is returned.

Parameters
strString to split
delimiterString separator, can be one character such as ";", or multiple characters like ", " (there is a blank at the end in this example)
trimBlanksBoolean value, whether to remove blanks from each result string in the collection. If false, or if the parameter is not given, the blanks will be retained in the result string.
allowEmptyBoolean value, whether to retain empty strings in the result collection. For example, if allowEmpty is false, three empty strings will result from string ",," if separator is ",". When allowEmpty is true, the result will be an empty collection. If not given, this parameter defaults to false, and the result collection can have empty strings.
Returns
The vector of items is returned.
See also
Split - reflection enabled universal function.
Join - Reverse function.
static MVariant MAlgorithm::Trim ( const MVariant str,
const MVariant trimCharacters 
)
static

Trim the leading and trailing characters or bytes from the string given, return the result.

Either string or byte string can be given as parameter.

Parameters
strString or byte string to remove characters from, the given string will not be modified.
trimCharactersSet of characters to remove. If the set is empty, for example the variant is empty, the control characters and blanks are removed. If given, this should be a zero terminated string of what to remove, such as "\n\t ".
Returns
The result trimmed string
See also
Trim
TrimLeft
static MVariant MAlgorithm::TrimLeft ( const MVariant str,
const MVariant trimCharacters 
)
static

Trim the leading characters or bytes from the string given, return the result.

Either string or byte string can be given as parameter.

Parameters
strString or byte string to remove characters from, the given string will not be modified.
trimCharactersSet of characters to remove. If the set is empty, for example the variant is empty, the control characters and blanks are removed. If given, this should be a zero terminated string of what to remove, such as "\n\t ".
Returns
The result trimmed string.
See also
Trim
TrimRight
static MVariant MAlgorithm::TrimRight ( const MVariant str,
const MVariant trimCharacters 
)
static

Trim the trailing characters or bytes from the string given, return the result.

Either string or byte string can be given as parameter.

Parameters
strString or byte string to remove characters from, the given string will not be modified.
trimCharactersSet of characters to remove. If the set is empty, for example the variant is empty, the control characters and blanks are removed. If given, this should be a zero terminated string of what to remove, such as "\n\t ".
Returns
The result trimmed string.
See also
Trim
TrimLeft
static MStdString MAlgorithm::TrimString ( const MStdString str,
MConstChars  trimCharacters = NULL 
)
static

Specialized faster version of Trim that works on string type rather than on MVariant.

See also
Trim