mxlib
c++ tools for analyzing astronomical data and other tasks by Jared R. Males. [git repo]
stringUtils.cpp
Go to the documentation of this file.
1 /** \file stringUtils.cpp
2  * \brief Implementation of utilities for working with strings
3  *
4  * \author Jared R. Males (jaredmales@gmail.com)
5  *
6  * \ingroup stringutils
7  *
8  */
9 
10 //***********************************************************************//
11 // Copyright 2020 Jared R. Males (jaredmales@gmail.com)
12 //
13 // This file is part of mxlib.
14 //
15 // mxlib is free software: you can redistribute it and/or modify
16 // it under the terms of the GNU General Public License as published by
17 // the Free Software Foundation, either version 3 of the License, or
18 // (at your option) any later version.
19 //
20 // mxlib is distributed in the hope that it will be useful,
21 // but WITHOUT ANY WARRANTY; without even the implied warranty of
22 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 // GNU General Public License for more details.
24 //
25 // You should have received a copy of the GNU General Public License
26 // along with mxlib. If not, see <http://www.gnu.org/licenses/>.
27 //***********************************************************************//
28 
29 #include "ioutils/stringUtils.hpp"
30 
31 namespace mx
32 {
33 namespace ioutils
34 {
35 
36 
37 // Specialization of convertToString to avoid converting a string to a string
38 template<>
39 std::string convertToString<std::string>( const std::string & value,
40  int precision
41  )
42 {
43  static_cast<void>(precision);
44  return value;
45 }
46 
47 // Template specialization of convertFromString for char
48 template<>
49 char convertFromString<char>(const std::string & str )
50 {
51  return (char) atoi(str.c_str());
52 }
53 
54 // Template specialization of convertFromString for char16_t
55 template<>
56 char16_t convertFromString<char16_t>(const std::string & str )
57 {
58  return (char16_t) atoi(str.c_str());
59 }
60 
61 // Template specialization of convertFromString for char32_t
62 template<>
63 char32_t convertFromString<char32_t>(const std::string & str )
64 {
65  return (char32_t) atoi(str.c_str());
66 }
67 
68 // Template specialization of convertFromString for char32_t
69 template<>
70 wchar_t convertFromString<wchar_t>(const std::string & str )
71 {
72  return (wchar_t) atoi(str.c_str());
73 }
74 
75 // Template specialization of convertFromString for unsigned char
76 template<>
77 signed char convertFromString<signed char>(const std::string & str )
78 {
79  return (signed char) atoi(str.c_str());
80 }
81 
82 // Template specialization of convertFromString for unsigned char
83 template<>
84 unsigned char convertFromString<unsigned char>(const std::string & str )
85 {
86  return (unsigned char) atoi(str.c_str());
87 }
88 
89 
90 // Template specialization of convertFromString for short
91 template<>
92 short convertFromString<short>(const std::string & str )
93 {
94  return (short) atoi(str.c_str());
95 }
96 
97 // Template specialization of convertFromString for unsigned short
98 template<>
99 unsigned short convertFromString<unsigned short>(const std::string & str )
100 {
101  return (unsigned short) atoi(str.c_str());
102 }
103 
104 // Template specialization of convertFromString for int
105 template<>
106 int convertFromString<int>(const std::string & str )
107 {
108  return atoi(str.c_str());
109 }
110 
111 // Template specialization of convertFromString for unsigned int
112 template<>
113 unsigned int convertFromString<unsigned int>(const std::string & str )
114 {
115  return (unsigned int) strtoul(str.c_str(),0,0);
116 }
117 
118 // Template specialization of convertFromString for long
119 template<>
120 long convertFromString<long>(const std::string & str )
121 {
122  return strtol(str.c_str(), 0, 0);
123 }
124 
125 // Template specialization of convertFromString for unsigned long
126 template<>
127 unsigned long convertFromString<unsigned long>(const std::string & str)
128 {
129  return strtoul(str.c_str(), 0, 0);
130 }
131 
132 // Template specialization of convertFromString for long long
133 template<>
134 long long convertFromString<long long>(const std::string & str )
135 {
136  return strtoll(str.c_str(), 0, 0);
137 }
138 
139 // Template specialization of convertFromString for unsigned long long
140 template<>
141 unsigned long long convertFromString<unsigned long long>(const std::string & str )
142 {
143  return strtoull(str.c_str(), 0, 0);
144 }
145 
146 // Template specialization of convertFromString for float
147 template<>
148 float convertFromString<float>(const std::string & str )
149 {
150  return strtof(str.c_str(), 0);
151 }
152 
153 // Template specialization of convertFromString for double
154 template<>
155 double convertFromString<double>(const std::string & str )
156 {
157  return strtod(str.c_str(), 0);
158 }
159 
160 // Template specialization of convertFromString for long double
161 template<>
162 long double convertFromString<long double>(const std::string & str )
163 {
164  return strtold(str.c_str(), 0);
165 }
166 
167 // Template specialization of convertFromString for bool
168 template<>
169 bool convertFromString<bool>(const std::string & str )
170 {
171  char c = str[0];
172  size_t i=0;
173  while(isspace(c) && i < str.length()) c = str[i++];
174 
175  if(c == '0' || c == 'f' || c == 'F') return false;
176  if(c == '1' || c == 't' || c == 'T') return true;
177 
178  return (bool) convertFromString<int>(str);
179 }
180 
181 // Convert a string to all lower case.
182 void toLower( std::string &outstr,
183  const std::string & instr
184  )
185 {
186  outstr.resize(instr.size());
187 
188  for(size_t i=0; i < instr.size(); ++i) outstr[i] = tolower(instr[i]);
189 
190 }
191 
192 // Convert a string to all lower case.
193 std::string toLower(const std::string & instr )
194 {
195  std::string outstr;
196 
197  toLower(outstr, instr);
198 
199  return outstr;
200 }
201 
202 //vConvert a string to all upper case.
203 void toUpper( std::string &outstr,
204  const std::string & instr
205  )
206 {
207  outstr.resize(instr.size());
208 
209  for(size_t i=0; i < instr.size(); ++i) outstr[i] = toupper(instr[i]);
210 
211 }
212 
213 // Convert a string to all upper case.
214 std::string toUpper(const std::string & instr )
215 {
216  std::string outstr;
217 
218  toUpper(outstr, instr);
219 
220  return outstr;
221 }
222 
223 // Remove all white space from a string.
224 void removeWhiteSpace( std::string & outstr,
225  const std::string & instr
226  )
227 {
228  outstr = instr;
229 
230  outstr.erase(std::remove_if(outstr.begin(), outstr.end(), ::isspace), outstr.end());
231 }
232 
233 // Remove all white space from a string.
234 std::string removeWhiteSpace( const std::string & instr )
235 {
236  std::string outstr;
237 
238  removeWhiteSpace(outstr, instr);
239 
240  return outstr;
241 }
242 
243 // Wrap a string by breaking it into smaller sized portions of a desired width
244 int stringWrap( std::vector<std::string> & lines,
245  const std::string & str,
246  int width
247  )
248 {
249  int L = str.length();
250 
251  if(L == 0) return 0;
252  int startPos, tmpPos, endPos;
253 
254  bool done = false;
255 
256  startPos = 0;
257 
258  while( !done )
259  {
260  if(startPos == L) --startPos; //Just to make sure
261 
262  endPos = startPos + width;
263 
264  if(endPos >= L)
265  {
266  lines.push_back( str.substr( startPos, L-startPos ));
267  done = true;
268  }
269  else
270  {
271  //Backup to nearest space
272  tmpPos = endPos;
273  while( !isspace(str[tmpPos]) && tmpPos >= startPos ) --tmpPos;
274 
275  //If we aren't at the beginning (i.e. splitting consecutive characters) we use new end position
276  if(tmpPos > startPos) endPos = tmpPos;
277 
278 
279  lines.push_back( str.substr( startPos, endPos-startPos) );
280 
281 
282  startPos = endPos;
283 
284  //Clear 1 space
285  if( str[startPos] == ' ') ++startPos;
286  }
287  }
288 
289  return 0;
290 }
291 
292 
293 } //namespace ioutils
294 } //namespace mx
constexpr units::realT c()
The speed of light.
Definition: constants.hpp:60
void toUpper(std::string &outstr, const std::string &instr)
Convert a string to all upper case.
int stringWrap(std::vector< std::string > &lines, const std::string &str, int width)
Wrap a string by breaking it into smaller sized portions of a desired width.
void toLower(std::string &outstr, const std::string &instr)
Convert a string to all lower case.
bool convertFromString< bool >(const std::string &str)
Template specialization of convertFromString for bool.
void removeWhiteSpace(std::string &outstr, const std::string &instr)
Remove all white space from a string.
The mxlib c++ namespace.
Definition: mxError.hpp:107
Utilities for working with strings.