tbx  0.7.6
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 
52 inline std::string to_string(const std::string &value)
53 {
54  return value;
55 }
56 
58 inline std::string to_string(const char *value)
59 {
60  return std::string(value);
61 }
62 
64 inline std::string to_string(char *value)
65 {
66  return std::string(value);
67 }
68 
72 template<class T> T from_string(const std::string &str)
73 {
74  T result;
75  std::istringstream ss(str);
76  ss >> result;
77  return result;
78 }
79 
83 inline std::string to_upper(const std::string &value)
84 {
85  std::string result;
86  for (std::string::const_iterator i = value.begin(); i != value.end(); ++i)
87  {
88  result += std::toupper(*i);
89  }
90  return result;
91 }
92 
96 inline std::string to_lower(const std::string &value)
97 {
98  std::string result;
99  for (std::string::const_iterator i = value.begin(); i != value.end(); ++i)
100  {
101  result += std::tolower(*i);
102  }
103  return result;
104 }
105 
109 inline bool equals_ignore_case(const std::string &s1, const std::string &s2)
110 {
111  if (s1.length() != s2.length()) return false;
112  std::string::const_iterator i1, i2 = s2.begin();
113  for (i1 = s1.begin(); i1 != s1.end(); ++i1, ++i2)
114  if (std::tolower(*i1) != std::tolower(*i2)) return false;
115  return true;
116 }
117 
121 inline bool equals_ignore_case(const std::string &s1, const char *cs2)
122 {
123  return (stricmp(s1.c_str(), cs2) == 0);
124 }
125 
129 inline bool equals_ignore_case(const char *cs1, const std::string &s2)
130 {
131  return equals_ignore_case(s2, cs1);
132 }
133 
137 inline bool equals_ignore_case(const char *cs1, const char *cs2)
138 {
139  return (stricmp(cs1, cs2) == 0);
140 }
141 
149 inline int compare_ignore_case(const char *cs1, const char *cs2)
150 {
151  return stricmp(cs1,cs2);
152 }
153 
161 inline int compare_ignore_case(const std::string &s1, const char *cs2)
162 {
163  return stricmp(s1.c_str(),cs2);
164 }
165 
173 inline int compare_ignore_case(const char *cs1, const std::string &s2)
174 {
175  return stricmp(cs1,s2.c_str());
176 }
184 inline int compare_ignore_case(const std::string &s1, const std::string &s2)
185 {
186  return stricmp(s1.c_str(),s2.c_str());
187 }
188 
198 inline std::string::size_type find_ignore_case(const std::string &s, std::string f, std::string::size_type start = 0)
199 {
200  if (f.empty()) return std::string::npos;
201  if (s.length() < f.length()) return std::string::npos;
202 
203  std::string::size_type pos;
204  std::string::size_type max_pos = s.length() - f.length();
205 
206  char fchar[2];
207  fchar[0] = std::tolower(f[0]);
208  fchar[1] = std::toupper(f[0]);
209 
210  while ((pos = s.find_first_of(fchar, start))!=std::string::npos)
211  {
212  if (pos > max_pos) return std::string::npos;
213  bool found = true;
214  for (std::string::size_type i = 1; i < f.length() && found; i++)
215  found = (std::tolower(s[pos+i]) == std::tolower(f[i]));
216  if (found) return pos;
217  start = pos + 1;
218  }
219 
220  return std::string::npos;
221 }
222 
223 }
224 
225 #endif /* TBX_STRINGUTILS_H_ */
tbx
A library for creating RISC OS toolbox applications.
Definition: abouttobeshownlistener.cc:35
tbx::equals_ignore_case
bool equals_ignore_case(const std::string &s1, const std::string &s2)
Compare two strings ignoring case.
Definition: stringutils.h:109
tbx::from_string
T from_string(const std::string &str)
Convert a string to another type.
Definition: stringutils.h:72
tbx::to_upper
std::string to_upper(const std::string &value)
Return string converted to upper case.
Definition: stringutils.h:83
tbx::to_string
std::string to_string(const T &value)
Convert a value to a string.
Definition: stringutils.h:44
tbx::find_ignore_case
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:198
tbx::compare_ignore_case
int compare_ignore_case(const char *cs1, const char *cs2)
Compares two strings ignoring case.
Definition: stringutils.h:149
tbx::to_lower
std::string to_lower(const std::string &value)
Return string converted to lower case.
Definition: stringutils.h:96