tbx  0.7.3
stringutils.h
1 /*
2  * tbx RISC OS toolbox library
3  *
4  * Copyright (C) 2010-2013 Alan Buckley All Rights Reserved.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  */
24 
25 /*
26  * stringutils.h
27  *
28  * Created on: 02-Apr-2009
29  * Author: alanb
30  */
31 
32 #ifndef TBX_STRINGUTILS_H_
33 #define TBX_STRINGUTILS_H_
34 
35 #include <sstream>
36 #include <cstring>
37 
38 namespace tbx
39 {
40 
44 template<class T> std::string to_string(const T &value)
45 {
46  std::ostringstream ss;
47  ss << value;
48  return ss.str();
49 }
50 
51 // Specialisations for string types
52 inline std::string to_string(const std::string &value)
53 {
54  return value;
55 }
56 
57 inline std::string to_string(const char *value)
58 {
59  return std::string(value);
60 }
61 
62 inline std::string to_string(char *value)
63 {
64  return std::string(value);
65 }
66 
70 template<class T> T from_string(const std::string &str)
71 {
72  T result;
73  std::istringstream ss(str);
74  ss >> result;
75  return result;
76 }
77 
81 inline std::string to_upper(const std::string &value)
82 {
83  std::string result;
84  for (std::string::const_iterator i = value.begin(); i != value.end(); ++i)
85  {
86  result += std::toupper(*i);
87  }
88  return result;
89 }
90 
94 inline std::string to_lower(const std::string &value)
95 {
96  std::string result;
97  for (std::string::const_iterator i = value.begin(); i != value.end(); ++i)
98  {
99  result += std::tolower(*i);
100  }
101  return result;
102 }
103 
107 inline bool equals_ignore_case(const std::string &s1, const std::string &s2)
108 {
109  if (s1.length() != s2.length()) return false;
110  std::string::const_iterator i1, i2 = s2.begin();
111  for (i1 = s1.begin(); i1 != s1.end(); ++i1, ++i2)
112  if (std::tolower(*i1) != std::tolower(*i2)) return false;
113  return true;
114 }
115 
119 inline bool equals_ignore_case(const std::string &s1, const char *cs2)
120 {
121  return (stricmp(s1.c_str(), cs2) == 0);
122 }
123 
127 inline bool equals_ignore_case(const char *cs1, const std::string &s2)
128 {
129  return equals_ignore_case(s2, cs1);
130 }
131 
135 inline bool equals_ignore_case(const char *cs1, const char *cs2)
136 {
137  return (stricmp(cs1, cs2) == 0);
138 }
139 
147 inline int compare_ignore_case(const char *cs1, const char *cs2)
148 {
149  return stricmp(cs1,cs2);
150 }
151 
159 inline int compare_ignore_case(const std::string &s1, const char *cs2)
160 {
161  return stricmp(s1.c_str(),cs2);
162 }
163 
171 inline int compare_ignore_case(const char *cs1, const std::string &s2)
172 {
173  return stricmp(cs1,s2.c_str());
174 }
182 inline int compare_ignore_case(const std::string &s1, const std::string &s2)
183 {
184  return stricmp(s1.c_str(),s2.c_str());
185 }
186 
196 inline std::string::size_type find_ignore_case(const std::string &s, std::string f, std::string::size_type start = 0)
197 {
198  if (f.empty()) return std::string::npos;
199  if (s.length() < f.length()) return std::string::npos;
200 
201  std::string::size_type pos;
202  std::string::size_type max_pos = s.length() - f.length();
203 
204  char fchar[2];
205  fchar[0] = std::tolower(f[0]);
206  fchar[1] = std::toupper(f[0]);
207 
208  while ((pos = s.find_first_of(fchar, start))!=std::string::npos)
209  {
210  if (pos > max_pos) return std::string::npos;
211  bool found = true;
212  for (std::string::size_type i = 1; i < f.length() && found; i++)
213  found = (std::tolower(s[pos+i]) == std::tolower(f[i]));
214  if (found) return pos;
215  start = pos + 1;
216  }
217 
218  return std::string::npos;
219 }
220 
221 }
222 
223 #endif /* TBX_STRINGUTILS_H_ */
std::string to_upper(const std::string &value)
Return string converted to upper case.
Definition: stringutils.h:81
bool equals_ignore_case(const std::string &s1, const std::string &s2)
Compare two strings ignoring case.
Definition: stringutils.h:107
T from_string(const std::string &str)
Convert a string to another type.
Definition: stringutils.h:70
int compare_ignore_case(const char *cs1, const char *cs2)
Compares two strings ignoring case.
Definition: stringutils.h:147
std::string to_string(const T &value)
Convert a value to a string.
Definition: stringutils.h:44
std::string::size_type find_ignore_case(const std::string &s, std::string f, std::string::size_type start=0)
Find a string in another ignoring case.
Definition: stringutils.h:196
std::string to_lower(const std::string &value)
Return string converted to lower case.
Definition: stringutils.h:94