MURE
Loading...
Searching...
No Matches
StringLine.hxx
Go to the documentation of this file.
1/*
2 This file is part of MURE,
3 Copyright (C) 2007-2021 MURE developers.
4
5 MURE is free software: you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 MURE is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License
16 along with MURE. If not, see <http://www.gnu.org/licenses/>.
17*/
18
19#ifndef _STRINGLINE_
20#define _STRINGLINE_
21
22#include <string>
23#include <sstream>
24#include <iostream>
25#include <algorithm>
26#include <cctype>
27#include <locale>
28
29using namespace std;
36
70{
71 public:
77
86 static string NextWord(string Line, int &start, char sep = ' ', char alt_sep = '\0');
88
97 static string PreviousWord(string Line, int &start, char sep = ' ', char alt_sep = '\0');
99
104 static int GetStartWord(string Line, int CurrentPosition, char sep = ' ');
106
111 static int GetEndWord(string Line, int CurrentPosition, char sep = ' ');
113
118 static string ReplaceAll(string InLine, string ToReplace, string By);
119
121
124 static void ltrim(std::string &s);
126
129 static void rtrim(std::string &s);
131
134 static void trim(std::string &s);
136
141
143
149 static int Find(const char *search, string Line);
151
157 static int rFind(const char *search, string Line);
159
164 static void ToLower(string &Line);
165 static void ToUpper(string &Line);
166
168
177 template < class out_T, class in_T > static out_T convert(const in_T &t);
179
195 template < class T > static bool ToNumber(T &t, const std::string &s, std::ios_base & (*f)(std::ios_base & ))
196 {
197 if(s.size() == 0) return true;
198 std::istringstream iss(s);
199 return ! (iss >> f >> t).fail();
200 }
201 static bool IsDouble(const std::string &s);
203};
204
205
206//_________________________________________________________________________________
207inline string StringLine::NextWord(string Line, int &start, char sep, char alt_sep)
208{
209 string Word = "";
210 if(start >= int(Line.size()))
211 {
212 return Word;
213 }
214 if(alt_sep != '\0') //replace all alt_sep by sep
215 {
216 string ToReplace = string(1, alt_sep); //cast char to string using constructor
217 string By = string(1, sep);
218 Line = ReplaceAll(Line, ToReplace, By);
219 }
220
221 start = GetStartWord(Line, start, sep);
222 int wordlength = GetEndWord(Line, start, sep) - start;
223
224 Word = Line.substr(start, wordlength);
225
226 start += wordlength;
227 return Word;
228}
229//_________________________________________________________________________________
230inline string StringLine::PreviousWord(string Line, int &start, char sep, char alt_sep)
231{
232 string Word = "";
233 if(start <= 0)
234 {
235 return Word;
236 }
237 if(start >= int(Line.size())) //first valid pos should be the 1st end char
238 start = Line.size() - 1;
239 if(alt_sep != '\0') //replace all alt_sep by sep
240 {
241 string ToReplace = string(1, alt_sep); //cast char to string using constructor
242 string By = string(1, sep);
243 Line = ReplaceAll(Line, ToReplace, By);
244 }
245 while(Line[start] == sep) //suppress end sep
246 start--;
247 int pos = Line.rfind(sep, start);
248 int wordlength = start - Line.rfind(sep, pos);
249
250 if(pos <= 0)
251 {
252 Word = Line.substr(0, start + 1);
253 start = 0;
254 return Word;
255 }
256 Word = Line.substr(pos + 1, wordlength);
257
258 start -= wordlength + 1;
259 return Word;
260}
261
262//_________________________________________________________________________________
263inline void StringLine::ltrim(std::string &s)
264{
265 s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](int ch)
266 {
267 return ! std::iswspace(ch);
268 }));
269}
270
271//_________________________________________________________________________________
272inline void StringLine::rtrim(std::string &s)
273{
274 s.erase(std::find_if(s.rbegin(), s.rend(), [](int ch)
275 {
276 return ! std::iswspace(ch);
277 }).base(), s.end());
278}
279
280//_________________________________________________________________________________
281inline void StringLine::trim(std::string &s)
282{
283 ltrim(s);
284 rtrim(s);
285}
286
287//_________________________________________________________________________________
288inline void StringLine::ToLower(string &Line)
289{
290 transform (Line.begin(), Line.end(), // source
291 Line.begin(), // destination
292 (int(*)(int))tolower); // operation
293}
294
295//_________________________________________________________________________________
296inline void StringLine::ToUpper(string &Line)
297{
298 transform (Line.begin(), Line.end(), // source
299 Line.begin(), // destination
300 (int(*)(int))toupper); // operation
301}
302
303//_________________________________________________________________________________
304inline int StringLine::GetStartWord(string Line, int CurrentPosition, char sep)
305{
306 int pos = Line.find(sep, CurrentPosition);
307 if(pos == int(string::npos)) return CurrentPosition;
308 while(CurrentPosition < int(Line.size()) && Line[CurrentPosition] == sep)
309 CurrentPosition++;
310 return CurrentPosition;
311}
312
313//_________________________________________________________________________________
314inline int StringLine::GetEndWord(string Line, int CurrentPosition, char sep)
315{
316 int pos = Line.find(sep, CurrentPosition);
317
318 if(pos == int(string::npos))
319 return Line.size();
320 return pos;
321}
322
323//_________________________________________________________________________________
324inline int StringLine::Find(const char *search, string Line)
325{
326 size_t Pos = Line.find(search);
327 if(Pos != string::npos ) return Pos;
328 return -1;
329}
330
331//_________________________________________________________________________________
332inline int StringLine::rFind(const char *search, string Line)
333{
334 size_t Pos = Line.rfind(search);
335 if(Pos != string::npos) return Pos;
336 return -1;
337}
338
339//_________________________________________________________________________________
340template < class out_T, class in_T >
341inline out_T StringLine::convert(const in_T &t)
342{
343 stringstream stream;
344 stream << t; // insert value to stream
345 out_T result; // store conversion's result here
346 stream >> result; // write value to result
347 return result;
348}
349
350//_________________________________________________________________________________
351inline string StringLine::ReplaceAll(string InLine, string ToReplace, string By)
352{
353 if(ToReplace == By) return InLine;
354 int start = 0;
355 int pos = InLine.find(ToReplace, start);
356 while(pos != int(string::npos))
357 {
358 InLine.replace(pos, ToReplace.size(), By);
359 start = 0;
360 pos = InLine.find(ToReplace, start);
361 }
362 return InLine;
363}
364//_________________________________________________________________________________
365inline bool StringLine::IsDouble(const std::string &s)
366{
367 std::istringstream i(s);
368 double temp;
369 return ( (i >> temp) ? true : false );
370}
371
372#endif
Class extracting fields from a string / line.
Definition StringLine.hxx:70
static string NextWord(string Line, int &start, char sep=' ', char alt_sep='\0')
Find the next word in a line.
Definition StringLine.hxx:207
static string ReplaceAll(string InLine, string ToReplace, string By)
Replace a sub-string by an other in a string.
Definition StringLine.hxx:351
static out_T convert(const in_T &t)
convert a input type (in_T) to another (out_T).
Definition StringLine.hxx:341
static void trim(std::string &s)
Remove spaces (trim) of the begining and the end of a string.
Definition StringLine.hxx:281
static bool ToNumber(T &t, const std::string &s, std::ios_base &(*f)(std::ios_base &))
try to convert a string to a number.
Definition StringLine.hxx:195
static void rtrim(std::string &s)
Remove ending spaces (trim) of a string.
Definition StringLine.hxx:272
static int Find(const char *search, string Line)
Find search in Line from the begining.
Definition StringLine.hxx:324
static void ToLower(string &Line)
convert a string to Lower case
Definition StringLine.hxx:288
static void ltrim(std::string &s)
Remove starting spaces (trim) a string.
Definition StringLine.hxx:263
static string PreviousWord(string Line, int &start, char sep=' ', char alt_sep='\0')
Find the previous word in a line.
Definition StringLine.hxx:230
static bool IsDouble(const std::string &s)
Definition StringLine.hxx:365
static void ToUpper(string &Line)
convert a string to Upper case
Definition StringLine.hxx:296
static int GetStartWord(string Line, int CurrentPosition, char sep=' ')
Find the start of a word in a line.
Definition StringLine.hxx:304
static int GetEndWord(string Line, int CurrentPosition, char sep=' ')
Find the end of a word in a line.
Definition StringLine.hxx:314
static int rFind(const char *search, string Line)
Find search in Line from the end.
Definition StringLine.hxx:332
the namespace of the Standard C++

MURE Project, documentation generated by Doxygen 1.9.7 - Fri Jan 19 2024