#include <mstring.h>
Public Member Functions | |
| MString () | |
| MString (const char *str) | |
| MString (const MString &str) | |
| MString (char c, uint len) | |
| MString (const char *str, uint n) | |
| virtual | ~MString () |
| MString & | operator= (const char *str) |
| MString & | operator= (const MString &str) |
| bool | operator< (const char *str) const |
| bool | operator<= (const char *str) const |
| bool | operator== (const char *str) const |
| bool | operator!= (const char *str) const |
| bool | operator>= (const char *str) const |
| bool | operator> (const char *str) const |
| MString | operator+ (const char *str) const |
| MString | operator+ (const MString &str) const |
| MString & | operator+= (const char *str) |
| MString & | operator+= (const MString &str) |
| MString | operator * (uint n) const |
| MString & | operator *= (uint n) |
| operator const char * () const | |
| const char & | operator[] (int pos) const |
| char & | operator[] (int pos) |
| MString | operator() (int start, int end) const |
| void | clear () |
| const char * | get () const |
| uint | len () const |
| bool | isEmpty () const |
| MString | left (uint len) const |
| MString | right (uint len) const |
| MString | mid (uint start, uint len=0xffffffff) const |
| MString | trimLeft (const char c= '\0') const |
| MString | trimRight (const char c= '\0') const |
| MString | trimAll (const char c= '\0') const |
| MString | padLeft (uint len, char c= ' ') const |
| MString | padRight (uint len, char c= ' ') const |
| MString | padCenter (uint len, char c= ' ') const |
| MString | caseUpper () const |
| MString | caseLower () const |
| bool | startsWith (const char *str) const |
| bool | endsWith (const char *str) const |
| MString & | toString (int val, int width=-1, char fillChar= ' ', NumericFormat format=DefaultFormat, NumericBase base=Dec) |
| MString & | toString (uint val, int width=-1, char fillChar= ' ', NumericFormat format=DefaultFormat, NumericBase base=Dec) |
| MString & | toString (long val, int width=-1, char fillChar= ' ', NumericFormat format=DefaultFormat, NumericBase base=Dec) |
| MString & | toString (ulong val, int width=-1, char fillChar= ' ', NumericFormat format=DefaultFormat, NumericBase base=Dec) |
| MString & | toString (float val, int precision=-1, int width=-1, char fillChar= ' ', NumericFormat format=DefaultFormat) |
| MString & | toString (double val, int precision=-1, int width=-1, char fillChar= ' ', NumericFormat format=DefaultFormat) |
| MString & | boolToString (bool val, bool yesNo=false, const MBoolStrings *format=NULL) |
| MString & | timeToString (time_t val, TimeFormat which=Date_Time, const MTimeStrings *format=NULL) |
| int | toInt (NumericBase base=Dec) const |
| uint | toUInt (NumericBase base=Dec) const |
| long | toLong (NumericBase base=Dec) const |
| ulong | toULong (NumericBase base=Dec) const |
| double | toDouble () const |
| int | toBool (const MBoolStrings *format=NULL) const |
| time_t | toTime (TimeFormat which=Date_Time, const MTimeStrings *format=NULL) const |
| uint | occurs (const char *str) const |
| int | find (const char *str, int pos=0) const |
| int | at (const char *str, int n=1) const |
| MString | section (const char *sep, int n) const |
| MStringList | split (const char *sep, bool ignoreEmpty=false) const |
| MString & | replace (const char *str, const char *byStr) |
| MString | wrap (uint width, uint indent=0, bool hanging=false) const |
| MString & | append (const char *str, const char *sep=MString::NL) |
Static Public Member Functions | |
| static MString | version (bool verbose=true) |
| static void | setNumLocale (bool on=true) |
| static void | setTimeLocale (bool on=true) |
| static MString | number (int val, int width=-1, char fillChar= ' ', NumericFormat format=DefaultFormat, NumericBase base=Dec) |
| static MString | number (uint val, int width=-1, char fillChar= ' ', NumericFormat format=DefaultFormat, NumericBase base=Dec) |
| static MString | number (long val, int width=-1, char fillChar= ' ', NumericFormat format=DefaultFormat, NumericBase base=Dec) |
| static MString | number (ulong val, int width=-1, char fillChar= ' ', NumericFormat format=DefaultFormat, NumericBase base=Dec) |
| static MString | number (float val, int precision=-1, int width=-1, char fillChar= ' ', NumericFormat format=DefaultFormat) |
| static MString | number (double val, int precision=-1, int width=-1, char fillChar= ' ', NumericFormat format=DefaultFormat) |
| static MString | boolean (bool val, bool yesNo=false, const MBoolStrings *format=NULL) |
| static MString | time (time_t val, TimeFormat which=Date_Time, const MTimeStrings *format=NULL) |
Static Public Attributes | |
| static const MString | NL |
Protected Member Functions | |
| void | initClass () |
| void | newString (uint len) |
| void | delString () |
| virtual uint | calcWidth () const |
| virtual MString | constructIndent (uint width) const |
Protected Attributes | |
| char * | _string |
| uint | stringLen |
Static Protected Attributes | |
| static const MBoolStrings | BOOLLOCALES [] |
| static const MTimeStrings | TIMELOCALES [] |
Objects of this class can be used as input for all of its methods that expect a const char* parameter; unwrapping will be done implicitely by operator const char*().
| mstring::MString::MString | ( | ) |
Constructs an empty MString object.
| mstring::MString::MString | ( | const char * | str | ) |
Constructs a MString object containing the given null-terminated string.
| mstring::MString::MString | ( | char | c, | |
| uint | len | |||
| ) |
Constructs a MString of length len filled with character c.
| mstring::MString::MString | ( | const char * | str, | |
| uint | n | |||
| ) |
Constructs a MString of n times the length of str and fills it with str.
| virtual mstring::MString::~MString | ( | ) | [virtual] |
Destroys the MString object.
| MString& mstring::MString::operator= | ( | const char * | str | ) |
Assignes the string from a char array.
Assignes the string from another MString object.
| bool mstring::MString::operator< | ( | const char * | str | ) | const |
Lexicographically compares the given to the contained string and returns true if contained string is less than the given.
| bool mstring::MString::operator<= | ( | const char * | str | ) | const |
Lexicographically compares the given to the contained string and returns true if contained string is less or equal the given.
| bool mstring::MString::operator== | ( | const char * | str | ) | const |
Compares the contained string with the given one, returns true if they're equal.
| bool mstring::MString::operator!= | ( | const char * | str | ) | const |
Compares the contained string with the given one, returns true if they aren't equal.
| bool mstring::MString::operator>= | ( | const char * | str | ) | const |
Lexicographically compares the given to the contained string and returns true if contained string is greater or equal the given.
| bool mstring::MString::operator> | ( | const char * | str | ) | const |
Lexicographically compares the given to the contained string and returns true if contained string is greater than the given.
| MString mstring::MString::operator+ | ( | const char * | str | ) | const |
Returns a MString which is n times the contained string concatenated.
Assignes n times the contained string to this MString and returns a reference to it.
| mstring::MString::operator const char * | ( | ) | const |
Returns a readonly pointer to the contained string.
| const char& mstring::MString::operator[] | ( | int | pos | ) | const |
Returns a const reference to the character at position pos in the contained string (begin of string: pos == 0), or the last character if pos points to a position behind the last character of the contained string, or the first character if pos points to a position before the first character of the contained string, or a reference to a NULL character if the string is empty.
If pos is negative then characters are counted backwards from the end of the contained string; -1 is the last character etc.
Watch operator precedence on pointers to MString instances! Correct invocation:
(*mstrobj)[x]
| char& mstring::MString::operator[] | ( | int | pos | ) |
As above, but returns a mutable reference to the character; can be used to assign single characters to a position in the string.
| MString mstring::MString::operator() | ( | int | start, | |
| int | end | |||
| ) | const |
Returns a slice of the string from position start until (and not including) position end. If start is out of range or end isn't larger than start an empty string will be returned.
From begin of the string: start = 0.
Until the end of the string: end = 0.
If a position is negative it will be counted from the end of the string; -1 is the last character, -2 the second last and so on.
Compare the slicing facility of Python <http://www.python.org>.
| void mstring::MString::clear | ( | ) |
Discards the content of the MString object.
| const char* mstring::MString::get | ( | ) | const |
Returns a readonly pointer to the contained string.
| uint mstring::MString::len | ( | ) | const |
Returns the length of the contained string.
| bool mstring::MString::isEmpty | ( | ) | const |
Returns true if the MString is empty.
Returns the len left characters of the contained string.
Returns the len right characters of the contained string.
Returns len characters of the contained string, starting at position start (begin of string = pos. 0); if len is omitted then the rest of the string will be returned.
| MString mstring::MString::trimLeft | ( | const char | c = '\0' |
) | const |
Returns the contained string without leading chars c; if c is empty it defaults to whitespaces.
| MString mstring::MString::trimRight | ( | const char | c = '\0' |
) | const |
Returns the contained string without trailing chars c; if c is empty it defaults to whitespaces.
| MString mstring::MString::trimAll | ( | const char | c = '\0' |
) | const |
Returns the contained string without leading and trailing chars c; if c is empty it defaults to whitespaces.
Returns the contained string padded on the left to len characters with c, if too long the string will be cut off on the right. c defaults to space.
Returns the contained string padded on the right to len characters with c, if too long the string will be cut off on the left. c defaults to space.
Returns the contained string centered by padding it on both sides to len characters with c, if too long the string will be cut off on the right. c defaults to space.
| MString mstring::MString::caseUpper | ( | ) | const |
Returns the contained string converted to uppercase.
| MString mstring::MString::caseLower | ( | ) | const |
Returns the contained string converted to lowercase.
| bool mstring::MString::startsWith | ( | const char * | str | ) | const |
Checks if the contained string starts with str. Always true for empty str.
| bool mstring::MString::endsWith | ( | const char * | str | ) | const |
Checks if the contained string ends with str. Always true for empty str.
| MString& mstring::MString::toString | ( | int | val, | |
| int | width = -1, |
|||
| char | fillChar = ' ', |
|||
| NumericFormat | format = DefaultFormat, |
|||
| NumericBase | base = Dec | |||
| ) |
Converts the given integer value to a string, assignes it to this MString and returns a reference to this MString.
If a width > -1 is given the output will be right justified within this width by padding it on the left with fillChar. Format flags and a numeric base may be given for the conversion (currently only octal, decimal and hexadecimal are supported).
| MString& mstring::MString::toString | ( | uint | val, | |
| int | width = -1, |
|||
| char | fillChar = ' ', |
|||
| NumericFormat | format = DefaultFormat, |
|||
| NumericBase | base = Dec | |||
| ) |
Converts the given unsigned integer value to a string, assignes it to this MString and returns a reference to this MString.
If a width > -1 is given the output will be right justified within this width by padding it on the left with fillChar. Format flags and a numeric base may be given for the conversion (currently only octal, decimal and hexadecimal are supported).
| MString& mstring::MString::toString | ( | long | val, | |
| int | width = -1, |
|||
| char | fillChar = ' ', |
|||
| NumericFormat | format = DefaultFormat, |
|||
| NumericBase | base = Dec | |||
| ) |
Converts the given long integer value to a string, assignes it to this MString and returns a reference to this MString.
If a width > -1 is given the output will be right justified within this width by padding it on the left with fillChar. Format flags and a numeric base may be given for the conversion (currently only octal, decimal and hexadecimal are supported).
| MString& mstring::MString::toString | ( | ulong | val, | |
| int | width = -1, |
|||
| char | fillChar = ' ', |
|||
| NumericFormat | format = DefaultFormat, |
|||
| NumericBase | base = Dec | |||
| ) |
Converts the given unsigned long integer value to a string, assignes it to this MString and returns a reference to this MString.
If a width > -1 is given the output will be right justified within this width by padding it on the left with fillChar. Format flags and a numeric base may be given for the conversion (currently only octal, decimal and hexadecimal are supported).
| MString& mstring::MString::toString | ( | float | val, | |
| int | precision = -1, |
|||
| int | width = -1, |
|||
| char | fillChar = ' ', |
|||
| NumericFormat | format = DefaultFormat | |||
| ) |
Converts the given float value to a string, assignes it to this MString and returns a reference to this MString. Representation of the decimal point depends on the current locale environment.
Provide a precision if you want a fixed-point string. If a width > -1 is given the output will be right justified within this width by padding it on the left with fillChar.
| MString& mstring::MString::toString | ( | double | val, | |
| int | precision = -1, |
|||
| int | width = -1, |
|||
| char | fillChar = ' ', |
|||
| NumericFormat | format = DefaultFormat | |||
| ) |
Converts the given double value to a string, assignes it to this MString and returns a reference to this MString. Representation of the decimal point depends on the current locale environment.
Provide a precision if you want a fixed-point string. If a width > -1 is given the output will be right justified within this width by padding it on the left with fillChar.
| MString& mstring::MString::boolToString | ( | bool | val, | |
| bool | yesNo = false, |
|||
| const MBoolStrings * | format = NULL | |||
| ) |
Converts the given bool value to a string, assignes it to this MString and returns a reference to this MString. If no userdefined MBoolStrings are given conversion is performed according to the current locale environment if a fitting entry is available; else the default entry of MString::BOOLLOCALES will be used.
If yesNo=true the result will be the equivalent to "Yes" or "No" instead of "True" or "False". If no match is found the contained string keeps its current content; should occur only if there's no default entry.
| MString& mstring::MString::timeToString | ( | time_t | val, | |
| TimeFormat | which = Date_Time, |
|||
| const MTimeStrings * | format = NULL | |||
| ) |
Converts the given time value into its string representation, using the specified time format (results either in a string containing a date, or a time of day, or both separated by a space, which is the default), assignes it to this MString and returns a reference to it. If no userdefined MTimeStrings are given conversion is performed according to the current locale environment; if no fitting entry in MString::TIMELOCALES is available the default entry is used. If MTimeStrings are provided by the user the locale setting is irrelevant.
| int mstring::MString::toInt | ( | NumericBase | base = Dec |
) | const |
Extracts an integer value from the beginning of the contained string and calculates its value to the given base; base defaults to decimal numbers. Extraction terminates at the first character that isn't part of the numeric base, counting from 0 to 9 and then from A to Z, and is not case sensitive.
| uint mstring::MString::toUInt | ( | NumericBase | base = Dec |
) | const |
Extracts an unsigned integer from the beginning of the contained string and calculates its value to the given base; base defaults to decimal numbers. Extraction terminates at the first character that isn't part of the numeric base, counting from 0 to 9 and then from A to Z, and is not case sensitive. Negative numbers create an overflow.
| long mstring::MString::toLong | ( | NumericBase | base = Dec |
) | const |
Extracts a long integer value from the beginning of the contained string and calculates its value to the given base; base defaults to decimal numbers. Extraction terminates at the first character that isn't part of the numeric base, counting from 0 to 9 and then from A to Z, and is not case sensitive.
| ulong mstring::MString::toULong | ( | NumericBase | base = Dec |
) | const |
Extracts an unsigned long integer from the beginning of the contained string and calculates its value to the given base; base defaults to decimal numbers. Extraction terminates at the first character that isn't part of the numeric base, counting from 0 to 9 and then from A to Z, and is not case sensitive. Negative numbers create an overflow.
| double mstring::MString::toDouble | ( | ) | const |
Extracts a floating point value from the beginning of the contained string if it starts with a number. Recognition of the decimal point depends on the current locale environment.
| int mstring::MString::toBool | ( | const MBoolStrings * | format = NULL |
) | const |
Extracts a boolean value from the beginning of the contained string by comparing it either to the given userdefined MBoolStrings or searching MString::BOOLLOCALES for a match applicable to the current locale environment or the default entry. The comparison is case-insensitive and includes both the True/False and Yes/No string pairs.
Returns 1 if true, 0 if false or -1 if no matching string can be found.
ATTENTION: -1 casted to bool is interpreted as true! Workaround for to interpret an unknown string as false:
bool b = (MString("?").toBool() > 0);
| time_t mstring::MString::toTime | ( | TimeFormat | which = Date_Time, |
|
| const MTimeStrings * | format = NULL | |||
| ) | const |
Extracts a time_t value from the beginning of the contained string by comparing it either to the given userdefine MTimeStrings or searching MString::TIMELOCALES for a match applicable to the current locale environment or the default entry. Returns -1 if no matching format string can be found or the given string is an invalid time.
When converting from a date the time will be set to 00:00:00 (midnight), from a time the date it will be set to the current day.
This method recognizes the following strftime() commands in the format string: %%, %Y, %y, %m, %d, %H, %M, %S, %p. Short years (%y) will be interpreted as the current century. All other characters will be compared to the contained string; the string will be treated as invalid if they don't match.
| uint mstring::MString::occurs | ( | const char * | str | ) | const |
| int mstring::MString::find | ( | const char * | str, | |
| int | pos = 0 | |||
| ) | const |
Searches the given string str in the contained string, starting at position pos, and returns the position of the first occurance or -1 if not found. The first position in a string is 0. If pos is negative the search is performed backwards from the end of the contained string; last character is -1, second last -2 and so on.
| int mstring::MString::at | ( | const char * | str, | |
| int | n = 1 | |||
| ) | const |
| MString mstring::MString::section | ( | const char * | sep, | |
| int | n | |||
| ) | const |
Returns the content of the nth section, starting at n = 0, of the contained string as parted by the separator string sep, or an empty string if the section was not found. If n is negative counting starts at the end of the string.
| MStringList mstring::MString::split | ( | const char * | sep, | |
| bool | ignoreEmpty = false | |||
| ) | const |
Returns a list with the content of all sections parted by the separator string sep. If ingoreEmpty == true then empty sections won't be part of the list. If the separator is empty the list will contain the whole string split into single characters.
| MString& mstring::MString::replace | ( | const char * | str, | |
| const char * | byStr | |||
| ) |
Replaces every occurance of string str with string byStr and returns a reference to this MString object.
Wraps the contained string using MString::NL and returns a MString with the result. The lines will be wrapped at whitespaces or in mid-word if necessary, so that no line exceeds the given width. If an indent is passed each line will be indented by the given value, while the first line spans the whole width if hanging == true. Existing linebreaks are taken into account if they equal MString::NL, tabulators are not!
The metrics for width and indent is characters by default; you can reimplement the protected members calcWidth() and constructIndent() if you would like to use other algorithms to calculate string widths (e. g. pixels for proportional fonts).
| MString& mstring::MString::append | ( | const char * | str, | |
| const char * | sep = MString::NL | |||
| ) |
Appends str to the contained string with sep as separator and returns a reference to this MString object. If sep is already terminating the contained string, or the contained string is empty, then first str and then sep will be appended, else sep separates contained string and appended str. sep defaults to the platform-dependent newline character.
| static MString mstring::MString::version | ( | bool | verbose = true |
) | [static] |
| static void mstring::MString::setNumLocale | ( | bool | on = true |
) | [static] |
Sets the decimal point character and other preferences to the local settings as extracted from the system, or to the default if on=false (concerns LC_NUMERIC only).
ATTENTION: Setting this to on=true changes the behaviour of your whole application, e. g. printf(), not only that of the MString object!
| static void mstring::MString::setTimeLocale | ( | bool | on = true |
) | [static] |
Sets the weekday names and other preferences to the local settings as extracted from the system, or to the default if on=false (concerns LC_TIME only).
ATTENTION: Setting this to on=true changes the behaviour of your whole application, e. g. strftime(), not only that of the MString object!
| static MString mstring::MString::number | ( | int | val, | |
| int | width = -1, |
|||
| char | fillChar = ' ', |
|||
| NumericFormat | format = DefaultFormat, |
|||
| NumericBase | base = Dec | |||
| ) | [static] |
Converts the given integer value to a MString.
Provided for convenience.
| static MString mstring::MString::number | ( | uint | val, | |
| int | width = -1, |
|||
| char | fillChar = ' ', |
|||
| NumericFormat | format = DefaultFormat, |
|||
| NumericBase | base = Dec | |||
| ) | [static] |
Converts the given unsigned integer value to a MString.
Provided for convenience.
| static MString mstring::MString::number | ( | long | val, | |
| int | width = -1, |
|||
| char | fillChar = ' ', |
|||
| NumericFormat | format = DefaultFormat, |
|||
| NumericBase | base = Dec | |||
| ) | [static] |
Converts the given long integer value to a MString.
Provided for convenience.
| static MString mstring::MString::number | ( | ulong | val, | |
| int | width = -1, |
|||
| char | fillChar = ' ', |
|||
| NumericFormat | format = DefaultFormat, |
|||
| NumericBase | base = Dec | |||
| ) | [static] |
Converts the given unsigned long integer value to a MString.
Provided for convenience.
| static MString mstring::MString::number | ( | float | val, | |
| int | precision = -1, |
|||
| int | width = -1, |
|||
| char | fillChar = ' ', |
|||
| NumericFormat | format = DefaultFormat | |||
| ) | [static] |
Converts the given float value to a MString.
Provided for convenience.
| static MString mstring::MString::number | ( | double | val, | |
| int | precision = -1, |
|||
| int | width = -1, |
|||
| char | fillChar = ' ', |
|||
| NumericFormat | format = DefaultFormat | |||
| ) | [static] |
Converts the given double value to a MString.
Provided for convenience.
| static MString mstring::MString::boolean | ( | bool | val, | |
| bool | yesNo = false, |
|||
| const MBoolStrings * | format = NULL | |||
| ) | [static] |
Converts the given bool value to a MString.
Provided for convenience.
| static MString mstring::MString::time | ( | time_t | val, | |
| TimeFormat | which = Date_Time, |
|||
| const MTimeStrings * | format = NULL | |||
| ) | [static] |
Converts the given time_t value to a MString.
Provided for convenience.
| void mstring::MString::initClass | ( | ) | [protected] |
Initialize the class and its environment.
To be called from constructors only!
| void mstring::MString::newString | ( | uint | len | ) | [protected] |
Reserves memory for a string of the given length including the terminator. Call delString() before to delete the old content.
| void mstring::MString::delString | ( | ) | [protected] |
Deletes the contained string from memory and resets the buffered length.
| virtual uint mstring::MString::calcWidth | ( | ) | const [protected, virtual] |
const MString mstring::MString::NL [static] |
Provides the newline character(s) for the current platform: CR + LF on Windows systems, else LF.
Initialization in file mstring.cpp.
char* mstring::MString::_string [protected] |
Pointer to the contained string; NULL if no string is assigned.
uint mstring::MString::stringLen [mutable, protected] |
The buffered length of the contained string; if 0 it has to be calculated. This attribute can be changed from const members, too.
const MBoolStrings mstring::MString::BOOLLOCALES[] [static, protected] |
Contains a list of definitions for the conversion of bool values to strings and vice versa in different locales. The relevant locale setting for this behaviour is LC_NUMERIC.
This list will be processed in order, which means a preceding entry with locale="de" overrides one with locale="de_CH" in a subsequent line. Therefore the default set of strings (locale="") has to be the last entry in this list.
Initialization in file mstring.cpp. Defaults to the english terms "True", "False", "Yes", "No".
const MTimeStrings mstring::MString::TIMELOCALES[] [static, protected] |
Contains a list of definitions for the conversion of time values to strings and vice versa in different locales. The relevant locale setting for this behaviour is LC_TIME.
This list will be processed in order, which means a preceding entry with locale="de" overrides one with locale="de_CH" in a subsequent line. Therefore the default set of strings (locale="") has to be the last entry in this list.
Initialization in file mstring.cpp. Defaults to the ISO 8601 date and time format "YYYY-MM-DD", "HH:MM:SS".
1.5.2