xmscore  1.0
StringUtil.h
Go to the documentation of this file.
1 #pragma once
2 //------------------------------------------------------------------------------
7 //------------------------------------------------------------------------------
8 
9 //----- Included files ---------------------------------------------------------
10 
11 // 3. Standard Library Headers
12 #pragma warning(push)
13 #pragma warning(disable : 4996) // Windows code: unsafe
14 #include <iomanip>
15 #pragma warning(pop)
16 #include <set>
17 #include <string>
18 #include <type_traits>
19 #include <vector>
20 
21 // 4. External Library Headers
22 
23 // 5. Shared Headers
24 #include <xmscore/stl/vector.h>
25 
26 // 6. Non-shared Headers
27 
28 namespace xms
29 {
31 enum PrecFlags {
32  STR_FLOAT = 0x01
33  ,
35  ,
37  ,
38  STR_FULLWIDTH = 0x08
39  ,
41 };
42 
45 {
46 public:
49 
50 private:
52 };
53 
54 static const char* ST_WHITESPACE = " \t\n\f\r\v";
55 
56 // Convenience functions
57 
58 bool stEqualNoCase(const std::string&, const std::string&);
59 bool stFindNoCase(const std::string&, const std::string&);
60 
61 VecStr stSplit(const std::string& source,
62  const std::string& a_delimiterList = ST_WHITESPACE,
63  bool a_delimiterCompressOn = true);
64 VecStr stExplode(const std::string& source, const std::string& a_delimiterString);
65 std::string stImplode(const std::vector<std::string>& source, const std::string& delim);
66 int stIndexOfElem(const VecStr& a_container, const std::string& str);
67 
68 std::string& stLeft(std::string& a_source, size_t const a_length);
69 std::string stLeftCopy(const std::string& a_source, size_t const a_length);
70 
71 std::string& stRemove(std::string& str, char source);
72 std::string stRemoveCopy(const std::string& str, char source);
73 
74 std::string& stReplace(std::string& str, char source, char dest);
75 std::string& stReplace(std::string& str, const std::string& source, const std::string& dest);
76 std::string stReplaceCopy(const std::string& str, char source, char dest);
77 std::string stReplaceCopy(const std::string& str,
78  const std::string& source,
79  const std::string& dest);
80 
81 std::string& stRight(std::string& a_source, size_t const a_length);
82 std::string stRightCopy(const std::string& a_source, size_t const a_length);
83 
84 std::string stSimplified(const std::string& str);
85 bool stContains(const std::string& a_container, const std::string& a_substr);
86 bool stVectorContainsString(const VecStr& a_container, const std::string& str);
87 
88 std::string& stToLower(std::string& str);
89 std::string stToLowerCopy(const std::string& str);
90 std::string& stToUpper(std::string& str);
91 std::string stToUpperCopy(const std::string& str);
92 
93 std::string& stTrim(std::string& str, const std::string& delim = ST_WHITESPACE);
94 std::string stTrimCopy(const std::string& str, const std::string& delim = ST_WHITESPACE);
95 std::string& stTrimLeft(std::string& str, const std::string& delim = ST_WHITESPACE);
96 std::string& stTrimRight(std::string& str, const std::string& delim = ST_WHITESPACE);
97 
98 // Misc
99 
100 unsigned int stCountChar(const std::string& str, char c);
101 bool stNumeric(const std::string& str);
102 bool stScientificNotation(const std::string& str, bool check_numeric = true);
103 void stChangeExtendedAscii(std::string& str, bool to_extended);
104 bool stMakeUnique(const std::set<std::string>& set_str, std::string& str);
105 bool stStringToInt(const std::string&, int& i, int base = 0);
106 bool stStringToDouble(const std::string&, double& d);
107 
108 int stPrecision(double value, int& flags, int length = 15);
109 
110 // Number to string
111 
112 std::string STRstd(double a_value, int a_n = -1, int width = 15, int flags = 0);
113 std::string STRstd(float a_value, int a_n = -1, int width = 15, int flags = 0);
114 
115 //----- OVERLOAD ---------------------------------------------------------------
126 template <typename T>
127 inline std::string STRstd(T a_value, int a_n = 0, int width = 0, int flags = 0)
128 {
129  static_assert(std::is_integral<T>::value, "Not an integral (integer) type");
130  a_n = ((flags & STR_SCIENTIFIC) ? -1 : 0);
131  return STRstd((double)a_value, a_n, width, flags);
132 }
133 
134 std::string STRstd(std::string a_value);
135 
137 class StCommaNumpunct : public std::numpunct<char>
138 {
139 protected:
140  virtual char do_thousands_sep() const;
141  virtual std::string do_grouping() const;
142 }; // class StCommaNumpunct
143 
144 } // end namespace xms
int m_oldOutputFormat
Saved output format to restore to orignal value.
Definition: StringUtil.h:51
PrecFlags
Bitwise flags used by stPrecision(), and STR()
Definition: StringUtil.h:31
std::string stRemoveCopy(const std::string &str, char source)
Returns a copy of str with all instances of char source removed.
Definition: StringUtil.cpp:392
std::string & stTrimLeft(std::string &str, const std::string &delim)
Trims the leading delim characters from a string.
Definition: StringUtil.cpp:270
std::string & stLeft(std::string &a_source, size_t const a_length)
Modifies a_source to contain the first (leftmost) a_length characters.
Definition: StringUtil.cpp:466
std::string & stToUpper(std::string &str)
Modifies str to be all uppercase.
Definition: StringUtil.cpp:441
int 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]...
Definition: StringUtil.cpp:673
std::string STRstd(double a_value, int a_n, int width, int flags)
Get a properly formatted string from a double.
Definition: StringUtil.cpp:995
Use full width.
Definition: StringUtil.h:38
std::string & stTrim(std::string &str, const std::string &delim)
Trim the leading and trailing delim characters from a string.
Definition: StringUtil.cpp:306
bool stScientificNotation(const std::string &str, bool check_numeric)
Determines whether the given string is a number in scientific notation.
Definition: StringUtil.cpp:103
std::string stReplaceCopy(const std::string &str, char source, char dest)
Returns a copy of str with every instance of source replaced with dest.
Definition: StringUtil.cpp:318
std::string stToLowerCopy(const std::string &str)
Returns a new string that is str with all characters lowercase.
Definition: StringUtil.cpp:412
bool stVectorContainsString(const VecStr &a_container, const std::string &str)
Checks if a vec of strings contains a string. Case sensitive.
Definition: StringUtil.cpp:527
std::vector< std::string > VecStr
short rename
Definition: vector.h:30
std::string stToUpperCopy(const std::string &str)
Returns a new string that is str with all characters uppercase.
Definition: StringUtil.cpp:432
std::string & stRight(std::string &a_source, size_t const a_length)
Modifies a_source to contain the last (rightmost) a_length characters.
Definition: StringUtil.cpp:537
bool stStringToDouble(const std::string &s, double &d)
Convert a string to an double.
Definition: StringUtil.cpp:646
StTemp2DigitExponents()
Temporarily output 2-digit exponents for floating point numbers to match C++ standard. Should be able to remove all instances of this class upon moving to Visual Studio 2015.
Definition: StringUtil.cpp:47
std::string stLeftCopy(const std::string &a_source, size_t const a_length)
Extracts first (leftmost) a_length characters from a_source returning a copy.
Definition: StringUtil.cpp:452
std::string stSimplified(const std::string &str)
Removes all white space from the passed string.
Definition: StringUtil.cpp:497
Used to format numbers with comma separators.
Definition: StringUtil.h:137
int stIndexOfElem(const VecStr &a_container, const std::string &str)
Iterates through a vec of strings and looks for the string. Case sensitive.
Definition: StringUtil.cpp:239
When constructed std::cout will temporarily output 2-digit exponents for floating point numbers...
Definition: StringUtil.h:44
bool stFindNoCase(const std::string &a, const std::string &b)
Returns true if a contains b while ignoring capitalization.
Definition: StringUtil.cpp:563
std::string & stReplace(std::string &str, char source, char dest)
Returns a reference to str, and replaces every instance of source replaced with dest.
Definition: StringUtil.cpp:345
std::string stImplode(const std::vector< std::string > &source, const std::string &delim)
Joins the vector, inserting delim between each item.
Definition: StringUtil.cpp:216
Use maximum precision.
Definition: StringUtil.h:36
std::string & stToLower(std::string &str)
Modifies str to be all lowercase.
Definition: StringUtil.cpp:421
Float, not double.
Definition: StringUtil.h:32
std::string 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)...
Definition: StringUtil.cpp:258
~StTemp2DigitExponents()
Revert back to 3-digit exponents for pre-Visual Studio 2015.
Definition: StringUtil.cpp:58
static const char * ST_WHITESPACE
Whitespace characters.
Definition: StringUtil.h:54
std::string stRightCopy(const std::string &a_source, size_t const a_length)
Extracts last (rightmost) a_length characters from a_source returning a copy.
Definition: StringUtil.cpp:484
std::string & stTrimRight(std::string &str, const std::string &delim)
Trims the trailing delim characters from a string.
Definition: StringUtil.cpp:288
bool 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.
Definition: StringUtil.cpp:572
VecStr stExplode(const std::string &source, const std::string &a_delimiter)
Breaks the string into a vector of strings based on the delimiter.
Definition: StringUtil.cpp:144
bool stNumeric(const std::string &str)
Determines whether the given string is a valid number.
Definition: StringUtil.cpp:87
VecStr 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.
Definition: StringUtil.cpp:182
bool stEqualNoCase(const std::string &a, const std::string &b)
Returns true if a and b are equal while ignoring capitalization.
Definition: StringUtil.cpp:554
bool stContains(const std::string &a_container, const std::string &a_substr)
Checks if first string contains second. Case insensitive.
Definition: StringUtil.cpp:509
Vector types for convenience.
void stChangeExtendedAscii(std::string &str, bool to_extended)
Replaces the Windows Latin-1 extended ASCII characters with standard ASCII and vice-versa.
Definition: StringUtil.cpp:117
Use scientific notation.
Definition: StringUtil.h:34
unsigned int stCountChar(const std::string &str, char c)
Counts the number of a given character in a string.
Definition: StringUtil.cpp:70
std::string & stRemove(std::string &str, char source)
Removes all instances of char source from str and returns a reference to str.
Definition: StringUtil.cpp:403
bool stStringToInt(const std::string &s, int &i, int base)
Convert a string to an int.
Definition: StringUtil.cpp:625
Use commas in string.
Definition: StringUtil.h:40