xmscore
1.0
|
#include <xmscore/misc/StringUtil.h>
#include <cctype>
#include <cstdio>
#include <float.h>
#include <boost/algorithm/string.hpp>
#include <boost/algorithm/string/trim.hpp>
#include <boost/format.hpp>
#include <boost/lexical_cast.hpp>
#include <regex>
#include <xmscore/math/math.h>
#include <xmscore/misc/XmError.h>
#include <xmscore/misc/StringUtil.t.h>
#include <xmscore/stl/vector.h>
#include <xmscore/testing/TestTools.h>
Go to the source code of this file.
Functions | |
unsigned int | xms::stCountChar (const std::string &str, char c) |
Counts the number of a given character in a string. More... | |
bool | xms::stNumeric (const std::string &str) |
Determines whether the given string is a valid number. More... | |
bool | xms::stScientificNotation (const std::string &str, bool check_numeric) |
Determines whether the given string is a number in scientific notation. More... | |
void | xms::stChangeExtendedAscii (std::string &str, bool to_extended) |
Replaces the Windows Latin-1 extended ASCII characters with standard ASCII and vice-versa. More... | |
VecStr | xms::stExplode (const std::string &source, const std::string &a_delimiter) |
Breaks the string into a vector of strings based on the delimiter. More... | |
VecStr | xms::stSplit (const std::string &a_source, const std::string &a_delimiterList, bool a_delimiterCompressOn) |
Breaks string into vector of strings based on one or more delimiters. More... | |
std::string | xms::stImplode (const std::vector< std::string > &source, const std::string &delim) |
Joins the vector, inserting delim between each item. More... | |
int | xms::stIndexOfElem (const VecStr &a_container, const std::string &str) |
Iterates through a vec of strings and looks for the string. Case sensitive. More... | |
std::string | xms::stTrimCopy (const std::string &str, const std::string &delim) |
Trims the white space from a string. This involves creating a copy of the string (twice I believe), so don't call this unless you really don't want the original string modified. More... | |
std::string & | xms::stTrimLeft (std::string &str, const std::string &delim) |
Trims the leading delim characters from a string. More... | |
std::string & | xms::stTrimRight (std::string &str, const std::string &delim) |
Trims the trailing delim characters from a string. More... | |
std::string & | xms::stTrim (std::string &str, const std::string &delim) |
Trim the leading and trailing delim characters from a string. More... | |
std::string | xms::stReplaceCopy (const std::string &str, char source, char dest) |
Returns a copy of str with every instance of source replaced with dest. More... | |
std::string | xms::stReplaceCopy (const std::string &str, const std::string &source, const std::string &dest) |
Returns a copy of str with every instance of source replaced with dest. More... | |
std::string & | xms::stReplace (std::string &str, char source, char dest) |
Returns a reference to str, and replaces every instance of source replaced with dest. More... | |
std::string & | xms::stReplace (std::string &str, const std::string &source, const std::string &dest) |
Returns a reference to str, and replaces every instance of source replaced with dest. More... | |
std::string | xms::stRemoveCopy (const std::string &str, char source) |
Returns a copy of str with all instances of char source removed. More... | |
std::string & | xms::stRemove (std::string &str, char source) |
Removes all instances of char source from str and returns a reference to str. More... | |
std::string | xms::stToLowerCopy (const std::string &str) |
Returns a new string that is str with all characters lowercase. More... | |
std::string & | xms::stToLower (std::string &str) |
Modifies str to be all lowercase. More... | |
std::string | xms::stToUpperCopy (const std::string &str) |
Returns a new string that is str with all characters uppercase. More... | |
std::string & | xms::stToUpper (std::string &str) |
Modifies str to be all uppercase. More... | |
std::string | xms::stLeftCopy (const std::string &a_source, size_t const a_length) |
Extracts first (leftmost) a_length characters from a_source returning a copy. More... | |
std::string & | xms::stLeft (std::string &a_source, size_t const a_length) |
Modifies a_source to contain the first (leftmost) a_length characters. More... | |
std::string | xms::stRightCopy (const std::string &a_source, size_t const a_length) |
Extracts last (rightmost) a_length characters from a_source returning a copy. More... | |
std::string | xms::stSimplified (const std::string &str) |
Removes all white space from the passed string. More... | |
bool | xms::stContains (const std::string &a_container, const std::string &a_substr) |
Checks if first string contains second. Case insensitive. More... | |
bool | xms::stVectorContainsString (const VecStr &a_container, const std::string &str) |
Checks if a vec of strings contains a string. Case sensitive. More... | |
std::string & | xms::stRight (std::string &a_source, size_t const a_length) |
Modifies a_source to contain the last (rightmost) a_length characters. More... | |
bool | xms::stEqualNoCase (const std::string &a, const std::string &b) |
Returns true if a and b are equal while ignoring capitalization. More... | |
bool | xms::stFindNoCase (const std::string &a, const std::string &b) |
Returns true if a contains b while ignoring capitalization. More... | |
bool | xms::stMakeUnique (const std::set< std::string > &set_str, std::string &str) |
Changes str to "str (2)" etc. if it is in set of set_str. More... | |
bool | xms::stStringToInt (const std::string &s, int &i, int base) |
Convert a string to an int. More... | |
bool | xms::stStringToDouble (const std::string &s, double &d) |
Convert a string to an double. More... | |
int | xms::stPrecision (double value, int &flags, int length) |
Returns precision, or the number of digits to the right of the decimal needed to display the [value]. This is used in conjunction with FloatToString() or STR() to get the string. The precision is calculated such that the total string length (including the minus sign, decimal, E etc.) will never end up longer than length (by default, 15 characters) so all edit fields can be about 60 dialog units wide on PC. More... | |
std::string | xms::STRstd (double a_value, int a_n, int width, int flags) |
Get a properly formatted string from a double. More... | |
std::string | xms::STRstd (float value, int n, int width, int flags) |
Get a properly formatted string from a float. More... | |
std::string | xms::STRstd (std::string value) |
This version is needed because the template parameter could be a string. More... | |
Definition in file StringUtil.cpp.
void xms::stChangeExtendedAscii | ( | std::string & | str, |
bool | to_extended | ||
) |
Replaces the Windows Latin-1 extended ASCII characters with standard ASCII and vice-versa.
[in,out] | str | The string to modify. |
[in] | to_extended | true to add extended ASCII, false to remove it |
Definition at line 117 of file StringUtil.cpp.
References xms::stChangeExtendedAscii(), and xms::stReplace().
Referenced by xms::stChangeExtendedAscii().
bool xms::stContains | ( | const std::string & | a_container, |
const std::string & | a_substr | ||
) |
Checks if first string contains second. Case insensitive.
[in] | a_container | Container to search if it includes a substr. |
[in] | a_substr | Substring to search for in the container. |
Definition at line 509 of file StringUtil.cpp.
References xms::stContains().
Referenced by xms::stContains().
unsigned int xms::stCountChar | ( | const std::string & | str, |
char | c | ||
) |
Counts the number of a given character in a string.
[in] | str | The string. |
[in] | c | The char. |
Definition at line 70 of file StringUtil.cpp.
References xms::stCountChar().
Referenced by xms::stCountChar().
bool xms::stEqualNoCase | ( | const std::string & | a, |
const std::string & | b | ||
) |
Returns true if a and b are equal while ignoring capitalization.
[in] | a | First string. |
[in] | b | Second string. |
Definition at line 554 of file StringUtil.cpp.
References xms::stEqualNoCase().
Referenced by xms::stEqualNoCase().
VecStr xms::stExplode | ( | const std::string & | source, |
const std::string & | a_delimiter | ||
) |
Breaks the string into a vector of strings based on the delimiter.
[in] | source | The string. |
[in] | a_delimiter | The delimiter. The whole string is THE delimiter. |
a_delimiter is not a list of delimiters, it is THE delimiter. Use stSplit method if you need to specify multiple different delimiter characters or need to handle multiple adjacent delimiters (ie. "1 2 3")
Definition at line 144 of file StringUtil.cpp.
References xms::stExplode().
Referenced by xms::stExplode().
bool xms::stFindNoCase | ( | const std::string & | a, |
const std::string & | b | ||
) |
Returns true if a contains b while ignoring capitalization.
[in] | a | String to search. |
[in] | b | Substring searching for. |
Definition at line 563 of file StringUtil.cpp.
References xms::stFindNoCase(), and xms::stToLowerCopy().
Referenced by xms::stFindNoCase().
std::string xms::stImplode | ( | const std::vector< std::string > & | source, |
const std::string & | delim | ||
) |
Joins the vector, inserting delim between each item.
[in] | source | A vector of strings. |
[in] | delim | The delimiter to be inserted between each string. |
Definition at line 216 of file StringUtil.cpp.
References xms::stImplode().
Referenced by xms::stImplode().
int xms::stIndexOfElem | ( | const VecStr & | a_container, |
const std::string & | str | ||
) |
Iterates through a vec of strings and looks for the string. Case sensitive.
[in] | a_container | Vec of strings to iterate through. |
[in] | str | Element we're searching for. |
Definition at line 239 of file StringUtil.cpp.
References xms::stIndexOfElem().
Referenced by xms::stIndexOfElem().
std::string & xms::stLeft | ( | std::string & | a_source, |
size_t const | a_length | ||
) |
Modifies a_source to contain the first (leftmost) a_length characters.
[in,out] | a_source | The string that is modified. |
[in] | a_length | Number of leftmost characters to keep. |
Definition at line 466 of file StringUtil.cpp.
References xms::stLeft(), and XM_ASSERT.
Referenced by xms::stLeft(), and xms::stLeftCopy().
std::string xms::stLeftCopy | ( | const std::string & | a_source, |
size_t const | a_length | ||
) |
Extracts first (leftmost) a_length characters from a_source returning a copy.
[in] | a_source | The string. |
[in] | a_length | Number of leftmost characters to keep. |
Definition at line 452 of file StringUtil.cpp.
References xms::stLeft(), xms::stLeftCopy(), and XM_ASSERT.
Referenced by xms::stLeftCopy().
bool xms::stMakeUnique | ( | const std::set< std::string > & | set_str, |
std::string & | str | ||
) |
Changes str to "str (2)" etc. if it is in set of set_str.
[in] | set_str | A set of strings. |
[in,out] | str | A string that's made unique if it is in set_str. |
Definition at line 572 of file StringUtil.cpp.
References xms::stMakeUnique().
Referenced by xms::stMakeUnique().
bool xms::stNumeric | ( | const std::string & | str | ) |
Determines whether the given string is a valid number.
[in] | str | The string. |
Definition at line 87 of file StringUtil.cpp.
References xms::stNumeric(), and xms::stTrimCopy().
Referenced by xms::stNumeric(), and xms::stScientificNotation().
int xms::stPrecision | ( | double | value, |
int & | flags, | ||
int | length | ||
) |
Returns precision, or the number of digits to the right of the decimal needed to display the [value]. This is used in conjunction with FloatToString() or STR() to get the string. The precision is calculated such that the total string length (including the minus sign, decimal, E etc.) will never end up longer than length (by default, 15 characters) so all edit fields can be about 60 dialog units wide on PC.
[in] | value | The value. |
[out] | flags | Can be either 0, or STR_FLOAT, STR_SCIENTIFIC, or STR_FLOAT | STR_SCIENTIFIC. So, if your number is a float, or you want it in scientific notation, you can specify that with the flags. |
[in] | length | Number characters available to display the number |
Definition at line 673 of file StringUtil.cpp.
References xms::EQ_EPS(), xms::GT_EPS(), xms::LT_EPS(), xms::Miabs(), xms::Round(), xms::stPrecision(), xms::STR_FLOAT, xms::STR_SCIENTIFIC, and xms::stRemove().
Referenced by xms::StCommaNumpunct::do_grouping(), xms::stPrecision(), and xms::STRstd().
std::string & xms::stRemove | ( | std::string & | str, |
char | source | ||
) |
Removes all instances of char source from str and returns a reference to str.
[in,out] | str | The string to be modified. |
[in] | source | The character to be removed. |
Definition at line 403 of file StringUtil.cpp.
References xms::stRemove().
Referenced by xms::stPrecision(), xms::stRemove(), and xms::stRemoveCopy().
std::string xms::stRemoveCopy | ( | const std::string & | str, |
char | source | ||
) |
Returns a copy of str with all instances of char source removed.
[in,out] | str | The string. |
[in] | source | The character to be removed. |
Definition at line 392 of file StringUtil.cpp.
References xms::stRemove(), and xms::stRemoveCopy().
Referenced by xms::stRemoveCopy().
std::string & xms::stReplace | ( | std::string & | str, |
char | source, | ||
char | dest | ||
) |
Returns a reference to str, and replaces every instance of source replaced with dest.
[in,out] | str | The string to be modified. |
[in] | source | The character to be replaced. |
[in] | dest | The character replacing source. |
Definition at line 345 of file StringUtil.cpp.
References xms::stReplace().
Referenced by xms::stChangeExtendedAscii(), xms::stReplace(), and xms::stReplaceCopy().
std::string & xms::stReplace | ( | std::string & | str, |
const std::string & | source, | ||
const std::string & | dest | ||
) |
Returns a reference to str, and replaces every instance of source replaced with dest.
[in,out] | str | The string to be modified. |
[in] | source | The sub string to be replaced. |
[in] | dest | The sub string replacing source. |
Definition at line 367 of file StringUtil.cpp.
References xms::stReplace().
std::string xms::stReplaceCopy | ( | const std::string & | str, |
char | source, | ||
char | dest | ||
) |
Returns a copy of str with every instance of source replaced with dest.
[in,out] | str | The string. |
[in] | source | The character to be replaced. |
[in] | dest | The character replacing source. |
Definition at line 318 of file StringUtil.cpp.
References xms::stReplace(), and xms::stReplaceCopy().
Referenced by xms::stReplaceCopy().
std::string xms::stReplaceCopy | ( | const std::string & | str, |
const std::string & | source, | ||
const std::string & | dest | ||
) |
Returns a copy of str with every instance of source replaced with dest.
[in,out] | str | The string. |
[in] | source | The sub string to be replaced. |
[in] | dest | The sub string replacing source. |
Definition at line 331 of file StringUtil.cpp.
References xms::stReplace(), and xms::stReplaceCopy().
std::string & xms::stRight | ( | std::string & | a_source, |
size_t const | a_length | ||
) |
Modifies a_source to contain the last (rightmost) a_length characters.
[in,out] | a_source | The string that is modified. |
[in] | a_length | Number of rightmost characters to keep. |
Definition at line 537 of file StringUtil.cpp.
References xms::stRight(), and XM_ASSERT.
Referenced by xms::stRight(), and xms::stRightCopy().
std::string xms::stRightCopy | ( | const std::string & | a_source, |
size_t const | a_length | ||
) |
Extracts last (rightmost) a_length characters from a_source returning a copy.
[in] | a_source | The string. |
[in] | a_length | Number of rightmost characters to keep. |
Definition at line 484 of file StringUtil.cpp.
References xms::stRight(), xms::stRightCopy(), and XM_ASSERT.
Referenced by xms::stRightCopy().
std::string xms::STRstd | ( | double | a_value, |
int | a_n, | ||
int | width, | ||
int | flags | ||
) |
Get a properly formatted string from a double.
The reason for the short name is so that we can easily replace code like this: fprintf(fp, "%g %g", value1, value2); with code like this: fprintf(fp, "%s %s", STR(value1), STR(value2));
a_value | = the floating point number to be formatted as a string |
a_n | = the MAX precision, -1 (default) is auto compute |
width | = the MAX num characters (15 by default) |
flags | = bitwise flags: STR_FLOAT = 1 Treat value as floating point value (i.e. only 7 digits of precision are available) STR_SCIENTIFIC = 2 Force use of scientific notation STR_USEMAXPREC = 4 This only applies when a_n is not -1. a_n value is the desired precision. When this flag is set, the returned string may have less precision because trailing zeros are removed. When this flag is not set, they are not removed. Example: a_n = 3 and a_value = 5.0003 With this flag, returned string is "5.0". Without this flag, the returned string is "5.000". STR_FULLWIDTH = 8 Forces string to be 'width' in length. If the number is less than this width, it is padded with spaces on the right side. STR_WITHCOMMAS = 16 Includes commas in the number (345,000.0). Does account for different locales. |
Definition at line 995 of file StringUtil.cpp.
References xms::LT_TOL(), xms::Miabs(), xms::stPrecision(), xms::STR_FULLWIDTH, xms::STR_SCIENTIFIC, xms::STR_USEMAXPREC, xms::STR_WITHCOMMAS, xms::STRstd(), xms::stTrimRight(), and XM_ASSERT.
Referenced by xms::daWrite3DoubleLine(), xms::daWriteDoubleLine(), xms::daWriteVecDbl(), xms::daWriteVecPt3d(), and xms::STRstd().
std::string xms::STRstd | ( | float | value, |
int | n, | ||
int | width, | ||
int | flags | ||
) |
Get a properly formatted string from a float.
The reason for the short name is so that we can easily replace code like this: fprintf(fp, "%g %g", value1, value2); with code like this: fprintf(fp, "%s %s", STR(value1), STR(value2));
value | = the floating point number to be formatted as a string |
n | = the MAX precision, -1 (default) is auto compute |
width | = the MAX num characters (15 by default) |
flags | = bitwise flags: STR_FLOAT = 1 Treat value as floating point value (i.e. only 7 digits of precision are available) STR_SCIENTIFIC = 2 Force use of scientific notation STR_USEMAXPREC = 4 This only applies when a_n is not -1. a_n value is the desired precision. When this flag is set, the returned string may have less precision because trailing zeros are removed. When this flag is not set, they are not removed. Example: a_n = 3 and a_value = 5.0003 With this flag, returned string is "5.0". Without this flag, the returned string is "5.000". STR_FULLWIDTH = 8 Forces string to be 'width' in length. If the number is less than this width, it is padded with spaces on the right side. STR_WITHCOMMAS = 16 Includes commas in the number (345,000.0). Does account for different locales. |
Definition at line 1168 of file StringUtil.cpp.
References xms::STRstd().
std::string xms::STRstd | ( | std::string | value | ) |
This version is needed because the template parameter could be a string.
value | = the value. |
Definition at line 1178 of file StringUtil.cpp.
References xms::STRstd().
bool xms::stScientificNotation | ( | const std::string & | str, |
bool | check_numeric | ||
) |
Determines whether the given string is a number in scientific notation.
[in] | str | The string to modify. |
check_numeric | Pass false if you are sure your string is a number. |
Definition at line 103 of file StringUtil.cpp.
References xms::stNumeric(), and xms::stScientificNotation().
Referenced by xms::stScientificNotation().
std::string xms::stSimplified | ( | const std::string & | str | ) |
Removes all white space from the passed string.
[in] | str | The string to be modified. |
Definition at line 497 of file StringUtil.cpp.
References xms::stSimplified().
Referenced by xms::stSimplified().
VecStr xms::stSplit | ( | const std::string & | a_source, |
const std::string & | a_delimiterList, | ||
bool | a_delimiterCompressOn | ||
) |
Breaks string into vector of strings based on one or more delimiters.
[in] | a_source | The string to be split. |
[in] | a_delimiterList | String where each character is a delimeter. |
[in] | a_delimiterCompressOn | If true, consecutive delimeters are treated as one. |
Definition at line 182 of file StringUtil.cpp.
References xms::stSplit(), and xms::stTrimCopy().
Referenced by xms::stSplit().
bool xms::stStringToDouble | ( | const std::string & | s, |
double & | d | ||
) |
Convert a string to an double.
Better than stringstream or boost::lexical_cast. See: http://stackoverflow.com/questions/194465/how-to-parse-a-string-to-an-int-in-c
[in] | s | The string. |
[out] | d | The double. |
Definition at line 646 of file StringUtil.cpp.
References xms::stStringToDouble().
Referenced by xms::stStringToDouble().
bool xms::stStringToInt | ( | const std::string & | s, |
int & | i, | ||
int | base | ||
) |
Convert a string to an int.
Better than stringstream or boost::lexical_cast. See: http://stackoverflow.com/questions/194465/how-to-parse-a-string-to-an-int-in-c
[in] | s | The string. |
[out] | i | The double. |
[in] | base | Numeric base. |
Definition at line 625 of file StringUtil.cpp.
References xms::stStringToInt().
Referenced by xms::stStringToInt().
std::string & xms::stToLower | ( | std::string & | str | ) |
Modifies str to be all lowercase.
[in,out] | str | String to be modified. |
Definition at line 421 of file StringUtil.cpp.
References xms::stToLower().
Referenced by xms::stToLower(), and xms::stToLowerCopy().
std::string xms::stToLowerCopy | ( | const std::string & | str | ) |
Returns a new string that is str with all characters lowercase.
str | The string. |
Definition at line 412 of file StringUtil.cpp.
References xms::stToLower(), and xms::stToLowerCopy().
Referenced by xms::stFindNoCase(), and xms::stToLowerCopy().
std::string & xms::stToUpper | ( | std::string & | str | ) |
Modifies str to be all uppercase.
[in,out] | str | String to be modified. |
Definition at line 441 of file StringUtil.cpp.
References xms::stToUpper().
Referenced by xms::stToUpper(), and xms::stToUpperCopy().
std::string xms::stToUpperCopy | ( | const std::string & | str | ) |
Returns a new string that is str with all characters uppercase.
str | The string. |
Definition at line 432 of file StringUtil.cpp.
References xms::stToUpper(), and xms::stToUpperCopy().
Referenced by xms::stToUpperCopy().
std::string & xms::stTrim | ( | std::string & | str, |
const std::string & | delim | ||
) |
Trim the leading and trailing delim characters from a string.
[in,out] | str | The string to be modified. |
[in] | delim | All the characters you want to be trimmed. |
Definition at line 306 of file StringUtil.cpp.
References xms::stTrim(), xms::stTrimLeft(), and xms::stTrimRight().
Referenced by xms::stTrim(), and xms::stTrimCopy().
std::string xms::stTrimCopy | ( | const std::string & | str, |
const std::string & | delim | ||
) |
Trims the white space from a string. This involves creating a copy of the string (twice I believe), so don't call this unless you really don't want the original string modified.
[in,out] | str | The string. |
[in] | delim | All the characters you want to be trimmed. |
Definition at line 258 of file StringUtil.cpp.
References xms::stTrim(), and xms::stTrimCopy().
Referenced by xms::stNumeric(), xms::stSplit(), and xms::stTrimCopy().
std::string & xms::stTrimLeft | ( | std::string & | str, |
const std::string & | delim | ||
) |
Trims the leading delim characters from a string.
[in,out] | str | The string to be modified. |
[in] | delim | All the characters you want to be trimmed. |
Definition at line 270 of file StringUtil.cpp.
References xms::stTrimLeft().
Referenced by xms::stTrim(), and xms::stTrimLeft().
std::string & xms::stTrimRight | ( | std::string & | str, |
const std::string & | delim | ||
) |
Trims the trailing delim characters from a string.
[in,out] | str | The string to be modified. |
[in] | delim | All the characters you want to be trimmed. |
Definition at line 288 of file StringUtil.cpp.
References xms::stTrimRight().
Referenced by xms::STRstd(), xms::stTrim(), and xms::stTrimRight().
bool xms::stVectorContainsString | ( | const VecStr & | a_container, |
const std::string & | str | ||
) |
Checks if a vec of strings contains a string. Case sensitive.
[in] | a_container | Vec of strings to iterate through. |
[in] | str | String to search for in the container. |
Definition at line 527 of file StringUtil.cpp.
References xms::stVectorContainsString().
Referenced by xms::stVectorContainsString().