mxlib
c++ tools for analyzing astronomical data and other tasks by Jared R. Males. [git repo]
Loading...
Searching...
No Matches
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
30
31namespace mx
32{
33namespace ioutils
34{
35
36// Specialization of convertToString to avoid converting a string to a string
37template <>
38std::string convertToString<std::string>( const std::string &value, int precision )
39{
40 static_cast<void>( precision );
41 return value;
42}
43
44// Template specialization of convertFromString for char
45template <>
46char convertFromString<char>( const std::string &str )
47{
48 return (char)atoi( str.c_str() );
49}
50
51// Template specialization of convertFromString for char16_t
52template <>
53char16_t convertFromString<char16_t>( const std::string &str )
54{
55 return (char16_t)atoi( str.c_str() );
56}
57
58// Template specialization of convertFromString for char32_t
59template <>
60char32_t convertFromString<char32_t>( const std::string &str )
61{
62 return (char32_t)atoi( str.c_str() );
63}
64
65// Template specialization of convertFromString for char32_t
66template <>
67wchar_t convertFromString<wchar_t>( const std::string &str )
68{
69 return (wchar_t)atoi( str.c_str() );
70}
71
72// Template specialization of convertFromString for unsigned char
73template <>
74signed char convertFromString<signed char>( const std::string &str )
75{
76 return (signed char)atoi( str.c_str() );
77}
78
79// Template specialization of convertFromString for unsigned char
80template <>
81unsigned char convertFromString<unsigned char>( const std::string &str )
82{
83 return (unsigned char)atoi( str.c_str() );
84}
85
86// Template specialization of convertFromString for short
87template <>
88short convertFromString<short>( const std::string &str )
89{
90 return (short)atoi( str.c_str() );
91}
92
93// Template specialization of convertFromString for unsigned short
94template <>
95unsigned short convertFromString<unsigned short>( const std::string &str )
96{
97 return (unsigned short)atoi( str.c_str() );
98}
99
100// Template specialization of convertFromString for int
101template <>
102int convertFromString<int>( const std::string &str )
103{
104 //return static_cast<int>(std::stod( str ));
105 return std::stoi( str );
106}
107
108// Template specialization of convertFromString for unsigned int
109template <>
110unsigned int convertFromString<unsigned int>( const std::string &str )
111{
112 return (unsigned int)strtoul( str.c_str(), 0, 0 );
113}
114
115// Template specialization of convertFromString for long
116template <>
117long convertFromString<long>( const std::string &str )
118{
119 return strtol( str.c_str(), 0, 0 );
120}
121
122// Template specialization of convertFromString for unsigned long
123template <>
124unsigned long convertFromString<unsigned long>( const std::string &str )
125{
126 return strtoul( str.c_str(), 0, 0 );
127}
128
129// Template specialization of convertFromString for long long
130template <>
131long long convertFromString<long long>( const std::string &str )
132{
133 return strtoll( str.c_str(), 0, 0 );
134}
135
136// Template specialization of convertFromString for unsigned long long
137template <>
138unsigned long long convertFromString<unsigned long long>( const std::string &str )
139{
140 return strtoull( str.c_str(), 0, 0 );
141}
142
143// Template specialization of convertFromString for float
144template <>
145float convertFromString<float>( const std::string &str )
146{
147 return strtof( str.c_str(), 0 );
148}
149
150// Template specialization of convertFromString for double
151template <>
152double convertFromString<double>( const std::string &str )
153{
154 return strtod( str.c_str(), 0 );
155}
156
157// Template specialization of convertFromString for long double
158template <>
159long double convertFromString<long double>( const std::string &str )
160{
161 return strtold( str.c_str(), 0 );
162}
163
164// Template specialization of convertFromString for bool
165template <>
166bool convertFromString<bool>( const std::string &str )
167{
168 char c = str[0];
169 size_t i = 0;
170 while( isspace( c ) && i < str.length() )
171 c = str[i++];
172
173 if( c == '0' || c == 'f' || c == 'F' )
174 return false;
175 if( c == '1' || c == 't' || c == 'T' )
176 return true;
177
178 return (bool)convertFromString<int>( str );
179}
180
181// Convert a string to all lower case.
182void toLower( std::string &outstr, const std::string &instr )
183{
184 outstr.resize( instr.size() );
185
186 for( size_t i = 0; i < instr.size(); ++i )
187 outstr[i] = tolower( instr[i] );
188}
189
190// Convert a string to all lower case.
191std::string toLower( const std::string &instr )
192{
193 std::string outstr;
194
195 toLower( outstr, instr );
196
197 return outstr;
198}
199
200// vConvert a string to all upper case.
201void toUpper( std::string &outstr, const std::string &instr )
202{
203 outstr.resize( instr.size() );
204
205 for( size_t i = 0; i < instr.size(); ++i )
206 outstr[i] = toupper( instr[i] );
207}
208
209// Convert a string to all upper case.
210std::string toUpper( const std::string &instr )
211{
212 std::string outstr;
213
214 toUpper( outstr, instr );
215
216 return outstr;
217}
218
219// Remove all white space from a string.
220void removeWhiteSpace( std::string &outstr, const std::string &instr )
221{
222 outstr = instr;
223
224 outstr.erase( std::remove_if( outstr.begin(), outstr.end(), ::isspace ), outstr.end() );
225}
226
227// Remove all white space from a string.
228std::string removeWhiteSpace( const std::string &instr )
229{
230 std::string outstr;
231
232 removeWhiteSpace( outstr, instr );
233
234 return outstr;
235}
236
237// Wrap a string by breaking it into smaller sized portions of a desired width
238int stringWrap( std::vector<std::string> &lines, const std::string &str, int width )
239{
240 int L = str.length();
241
242 if( L == 0 )
243 return 0;
244 int startPos, tmpPos, endPos;
245
246 bool done = false;
247
248 startPos = 0;
249
250 while( !done )
251 {
252 if( startPos == L )
253 --startPos; // Just to make sure
254
255 endPos = startPos + width;
256
257 if( endPos >= L )
258 {
259 lines.push_back( str.substr( startPos, L - startPos ) );
260 done = true;
261 }
262 else
263 {
264 // Backup to nearest space
265 tmpPos = endPos;
266 while( !isspace( str[tmpPos] ) && tmpPos >= startPos )
267 --tmpPos;
268
269 // If we aren't at the beginning (i.e. splitting consecutive characters) we use new end position
270 if( tmpPos > startPos )
271 endPos = tmpPos;
272
273 lines.push_back( str.substr( startPos, endPos - startPos ) );
274
275 startPos = endPos;
276
277 // Clear 1 space
278 if( str[startPos] == ' ' )
279 ++startPos;
280 }
281 }
282
283 return 0;
284}
285
286} // namespace ioutils
287} // namespace mx
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:40
Utilities for working with strings.