glVertex  5.5.2
glvertex_string.h
Go to the documentation of this file.
1 // (c) by Stefan Roettger, licensed under MIT license
2 
5 #ifndef GLVERTEX_STRING_H
6 #define GLVERTEX_STRING_H
7 
8 #include <string>
9 #include <algorithm>
10 
12 class lgl_string: public std::string
13 {
14 public:
15 
16  lgl_string(const std::string &text = "")
17  : std::string(text)
18  {}
19 
20  lgl_string(const char *text)
21  : std::string(text)
22  {}
23 
25  bool empty() const
26  {
27  return(size() == 0);
28  }
29 
31  std::string prefix(const std::string &match) const
32  {
33  size_t pos = find(match);
34  if (pos != std::string::npos) return(substr(0, pos));
35  return("");
36  }
37 
39  std::string suffix(const std::string &match) const
40  {
41  size_t pos = rfind(match);
42  if (pos != std::string::npos) return(substr(pos+match.size(), size()-pos-match.size()));
43  return("");
44  }
45 
47  std::string head(const std::string &match) const
48  {
49  size_t pos = rfind(match);
50  if (pos != std::string::npos) return(substr(0, pos));
51  return("");
52  }
53 
55  std::string tail(const std::string &match) const
56  {
57  size_t pos = find(match);
58  if (pos != std::string::npos) return(substr(pos+match.size(), size()-pos-match.size()));
59  return("");
60  }
61 
63  bool startsWith(const std::string &with) const
64  {
65  return(find(with) == 0);
66  }
67 
69  bool endsWith(const std::string &with) const
70  {
71  return(rfind(with) == size()-with.size());
72  }
73 
75  lgl_string strip(const std::string &match) const
76  {
77  if (startsWith(match)) return(substr(match.size(), size()-match.size()));
78  else return(*this);
79  }
80 
82  lgl_string chop(const std::string &match) const
83  {
84  if (endsWith(match)) return(substr(0, size()-match.size()));
85  else return(*this);
86  }
87 
89  lgl_string remove(const std::string &start, const std::string &stop)
90  {
91  size_t pos1 = find(start);
92  if (pos1 != std::string::npos)
93  {
94  size_t pos2 = find(stop, pos1+start.size());
95  if (pos2 != std::string::npos)
96  {
97  lgl_string sub = substr(pos1, pos2-pos1+stop.size());
98  replace(pos1, pos2-pos1+stop.size(), "");
99  return(sub);
100  }
101  }
102 
103  return("");
104  }
105 
107  lgl_string toLower(char separator = '\0') const
108  {
109  lgl_string s(*this);
110 
111  if (separator == '\0')
112  std::transform(s.begin(), s.end(), s.begin(), ::tolower);
113  else
114  for (size_t i=0; i<s.size(); i++)
115  if (::isupper(s[i]))
116  {
117  s[i] = ::tolower(s[i]);
118 
119  if (i > 0)
120  if (::islower(s[i-1]))
121  s.insert(i, 1, separator);
122  }
123 
124  return(s);
125  }
126 
129  {
130  lgl_string s(*this);
131  std::transform(s.begin(), s.end(), s.begin(), ::toupper);
132  return(s);
133  }
134 
137  {
138  lgl_string s(*this);
139  std::transform(s.begin(), s.end(), s.begin(), tononwhite);
140  return(s);
141  }
142 
145  {
146  lgl_string s(*this);
147  std::transform(s.begin(), s.end(), s.begin(), toprint);
148  return(s);
149  }
150 
153  {
154  lgl_string s(*this);
155  std::transform(s.begin(), s.end(), s.begin(), toalnum);
156  return(s);
157  }
158 
159 protected:
160 
161  static char tononwhite(char c)
162  {
163  if (::isspace(c)) return('_');
164  else return(c);
165  }
166 
167  static char toprint(char c)
168  {
169  if (::isprint(c)) return(c);
170  else return('_');
171  }
172 
173  static char toalnum(char c)
174  {
175  if (::isalnum(c)) return(c);
176  else return('_');
177  }
178 
179 public:
180 
182  void replaceAll(const std::string &search, const std::string &with)
183  {
184  size_t pos = 0;
185  while ((pos = find(search, pos)) != std::string::npos)
186  {
187  replace(pos, search.length(), with);
188  pos += with.length();
189  }
190  }
191 
193  static void stringReplaceAll(std::string &text, const std::string &search, const std::string &with)
194  {
195  size_t pos = 0;
196  while ((pos = text.find(search, pos)) != std::string::npos)
197  {
198  text.replace(pos, search.length(), with);
199  pos += with.length();
200  }
201  }
202 
203 };
204 
205 #endif
lgl_string::strip
lgl_string strip(const std::string &match) const
strip match from begin
Definition: glvertex_string.h:75
lgl_string::empty
bool empty() const
is the string empty?
Definition: glvertex_string.h:25
lgl_string::chop
lgl_string chop(const std::string &match) const
chop match from end
Definition: glvertex_string.h:82
lgl_string::toUpper
lgl_string toUpper() const
convert to upper case
Definition: glvertex_string.h:128
lgl_string::suffix
std::string suffix(const std::string &match) const
get suffix after last substring match
Definition: glvertex_string.h:39
lgl_string::prefix
std::string prefix(const std::string &match) const
get prefix before first substring match
Definition: glvertex_string.h:31
lgl_string::startsWith
bool startsWith(const std::string &with) const
match begin
Definition: glvertex_string.h:63
lgl_string::stringReplaceAll
static void stringReplaceAll(std::string &text, const std::string &search, const std::string &with)
replace all occurrences in std::string
Definition: glvertex_string.h:193
lgl_string::replaceNonAlphaNumeric
lgl_string replaceNonAlphaNumeric() const
replace non-alphanumeric characters
Definition: glvertex_string.h:152
lgl_string::endsWith
bool endsWith(const std::string &with) const
match end
Definition: glvertex_string.h:69
lgl_string
LGL string class that extends std::string.
Definition: glvertex_string.h:12
lgl_string::replaceWhiteSpace
lgl_string replaceWhiteSpace() const
replace white space
Definition: glvertex_string.h:136
lgl_string::head
std::string head(const std::string &match) const
get head before last substring match
Definition: glvertex_string.h:47
lgl_string::remove
lgl_string remove(const std::string &start, const std::string &stop)
remove fragment between start and stop delimiters
Definition: glvertex_string.h:89
lgl_string::replaceAll
void replaceAll(const std::string &search, const std::string &with)
replace all occurrences in place
Definition: glvertex_string.h:182
lgl_string::toLower
lgl_string toLower(char separator='\0') const
convert to lower case
Definition: glvertex_string.h:107
lgl_string::tail
std::string tail(const std::string &match) const
get tail after first substring match
Definition: glvertex_string.h:55
lgl_string::replaceNonPrintable
lgl_string replaceNonPrintable() const
replace non-printable characters
Definition: glvertex_string.h:144