StringUtilsImpl.h
Go to the documentation of this file.
1 #pragma once
2 
3 template<class T>
5 {
6 public:
7 
8  static int replace(std::basic_string<T>& s, const std::basic_string<T>& from, const std::basic_string<T>& to)
9  {
10  int ii = 0;
11  int result = 0;
12 
13  if (s.length() != 0 && from.length() != 0)
14  {
15  while ((ii = s.find(from, ii)) != s.npos)
16  {
17  s.replace(ii, from.length(), to);
18  ii += (abs((int)(to.length() - from.length())) + 1);
19  result ++;
20  }
21  }
22  return result;
23  }
24 
25  static void tokenize(const std::basic_string<T>& ss, std::vector<std::basic_string<T>>& tokens, const std::basic_string<T>& delim)
26  {
27  tokens.clear();
28  int begin = 0, end = 0;
29  std::basic_string<T> temp;
30 
31  temp = ss;
32 
33  while ( (end = temp.find(delim)) != std::basic_string<T>::npos )
34  {
35  tokens.push_back( temp.substr(0, end) );
36  begin = end + delim.length();
37  temp = temp.substr(begin, temp.length() - begin);
38  }
39 
40  tokens.push_back(temp.substr(0, temp.length()));
41  }
42 
43  static std::basic_string<T> join(const std::vector<std::basic_string<T>>& tokens, const std::basic_string<T>& delim)
44  {
45  std::basic_string<T> ss;
46 
47  for (unsigned int i = 0; i < tokens.size(); i++)
48  {
49  if (i > 0) ss.append(delim);
50  ss.append(tokens[i]);
51  }
52 
53  return ss;
54  }
55 
56  static std::basic_string<T> join(const std::list<std::basic_string<T>>& tokens, const std::basic_string<T>& delim)
57  {
58  std::basic_string<T> ss;
59 
60  int i = 0;
61  for each (const std::basic_string<T>& token in tokens)
62  {
63  if (i > 0) ss.append(delim);
64  ss.append(token);
65  i++;
66  }
67 
68  return ss;
69  }
70 
71  static void tokenizeWithSkip(const std::basic_string<T>& ss, std::vector<std::basic_string<T>>& tokens, const std::basic_string<T>& delims)
72  {
73  // Skip delims at beginning.
74  std::basic_string<T>::size_type lastpos = ss.find_first_not_of(delims, 0);
75  // Find first "non-delimiter".
76  std::basic_string<T>::size_type pos = ss.find_first_of(delims, lastpos);
77 
78  while (std::string::npos != pos || std::string::npos != lastpos)
79  {
80  // Found a token, add it to the vector.
81  tokens.push_back(ss.substr(lastpos, pos - lastpos));
82  // Skip delims. Note the "not_of"
83  lastpos = ss.find_first_not_of(delims, pos);
84  // Find next "non-delimiter"
85  pos = ss.find_first_of(delims, lastpos);
86  }
87  }
88 
89  // Each character in the delim string is treated as a delimeter.
90  static void tokenizeOnChar(const std::basic_string<T>& ss, std::vector<std::basic_string<T>>& tokens, const std::basic_string<T>& delims)
91  {
92  tokens.clear();
93  int begin = 0, end = 0;
94 
95  for( unsigned int i = 0; i < ss.length(); i++)
96  {
97  if ( delims.find(ss.substr(i, 1), 0) == std::basic_string<T>::npos )
98  {
99  end++;
100  }
101  else
102  {
103  tokens.push_back(ss.substr(begin, (end - begin)));
104  begin = ++end;
105  }
106  }
107  tokens.push_back(ss.substr(begin, (end - begin)));
108  }
109 
110  static void ltrim(std::basic_string<T>& ss, const std::basic_string<T>& whitespaces)
111  {
112  ss.erase(0, ss.find_first_not_of(whitespaces));
113  }
114 
115  static void rtrim(std::basic_string<T>& ss, const std::basic_string<T>& whitespaces)
116  {
117  ss.erase(ss.find_last_not_of(whitespaces) + 1);
118  }
119 
120  static void lrtrim(std::basic_string<T>& ss, const std::basic_string<T>& whitespaces)
121  {
122  ltrim(ss, whitespaces);
123  rtrim(ss, whitespaces);
124  }
125 
126  static void lrtrimcrlf(std::basic_string<T>& ss, const std::basic_string<T>& whitespaces)
127  {
128  ltrim(ss, whitespaces);
129  rtrim(ss, whitespaces);
130  }
131 
132  static void uppercase(std::basic_string<T>& ss)
133  {
134  std::transform(ss.begin(), ss.end(), ss.begin(), (int(*)(int)) toupper);
135  }
136 
137  static void lowercase(std::basic_string<T>& ss)
138  {
139  std::transform(ss.begin(), ss.end(), ss.begin(), (int(*)(int)) tolower);
140  }
141 
142  static bool comparei(const std::basic_string<T>& ss1, const std::basic_string<T>& ss2)
143  {
144  std::basic_string<T> s1, s2;
145  s1 = ss1; s2 = ss2;
146  lowercase(s1);
147  lowercase(s2);
148  return (s1 == s2);
149  }
150 
151  static bool isNumber(const std::basic_string<T>& ss)
152  {
153  unsigned int i = 0;
154 
155  if( ss.length() > 1 && ss[0] == '-') i = 1;
156  if( ss.length() == i ) return false;
157 
158  for( ; i < ss.length(); i++ )
159  {
160  if(! isdigit(ss[i]))
161  {
162  return false;
163  }
164  }
165 
166  return true;
167  }
168 
169  static bool startsWith(const std::basic_string<T>& what, const std::basic_string<T>& with)
170  {
171  if (what.length() < with.length())
172  return false;
173  if (what.substr(0, with.length()) == with)
174  return true;
175  return false;
176  }
177 
178  static bool endsWith(const std::basic_string<T>& what, const std::basic_string<T>& with)
179  {
180  if (what.length() < with.length())
181  return false;
182  if (what.substr(what.length() - with.length(), what.length()) == with)
183  return true;
184  return false;
185  }
186 };
static bool endsWith(const std::basic_string< T > &what, const std::basic_string< T > &with)
Definition: StringUtilsImpl.h:178
static void lrtrimcrlf(std::basic_string< T > &ss, const std::basic_string< T > &whitespaces)
Definition: StringUtilsImpl.h:126
static void ltrim(std::basic_string< T > &ss, const std::basic_string< T > &whitespaces)
Definition: StringUtilsImpl.h:110
static void tokenize(const std::basic_string< T > &ss, std::vector< std::basic_string< T >> &tokens, const std::basic_string< T > &delim)
Definition: StringUtilsImpl.h:25
static std::basic_string< T > join(const std::vector< std::basic_string< T >> &tokens, const std::basic_string< T > &delim)
Definition: StringUtilsImpl.h:43
Definition: StringUtilsImpl.h:4
static void lrtrim(std::basic_string< T > &ss, const std::basic_string< T > &whitespaces)
Definition: StringUtilsImpl.h:120
static int replace(std::basic_string< T > &s, const std::basic_string< T > &from, const std::basic_string< T > &to)
Definition: StringUtilsImpl.h:8
static void uppercase(std::basic_string< T > &ss)
Definition: StringUtilsImpl.h:132
static bool isNumber(const std::basic_string< T > &ss)
Definition: StringUtilsImpl.h:151
static bool startsWith(const std::basic_string< T > &what, const std::basic_string< T > &with)
Definition: StringUtilsImpl.h:169
static void lowercase(std::basic_string< T > &ss)
Definition: StringUtilsImpl.h:137
static void rtrim(std::basic_string< T > &ss, const std::basic_string< T > &whitespaces)
Definition: StringUtilsImpl.h:115
static void tokenizeOnChar(const std::basic_string< T > &ss, std::vector< std::basic_string< T >> &tokens, const std::basic_string< T > &delims)
Definition: StringUtilsImpl.h:90
static void tokenizeWithSkip(const std::basic_string< T > &ss, std::vector< std::basic_string< T >> &tokens, const std::basic_string< T > &delims)
Definition: StringUtilsImpl.h:71
static bool comparei(const std::basic_string< T > &ss1, const std::basic_string< T > &ss2)
Definition: StringUtilsImpl.h:142
static std::basic_string< T > join(const std::list< std::basic_string< T >> &tokens, const std::basic_string< T > &delim)
Definition: StringUtilsImpl.h:56


© Application Security Inc. - All Rights Reserved http://msiext.codeplex.com