ARTS 2.5.0 (git: 9ee3ac6c)
mystring.h
Go to the documentation of this file.
1/* Copyright (C) 2001-2012 Stefan Buehler <sbuehler@ltu.se>
2
3 This program is free software; you can redistribute it and/or modify it
4 under the terms of the GNU General Public License as published by the
5 Free Software Foundation; either version 2, or (at your option) any
6 later version.
7
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12
13 You should have received a copy of the GNU General Public License
14 along with this program; if not, write to the Free Software
15 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
16 USA. */
17
27#ifndef mystring_h
28#define mystring_h
29
30#include <algorithm>
31#include <climits>
32#include <sstream>
33#include <string>
34#include <string_view>
35#include "array.h"
36#include "matpack.h"
37
54template <class charT>
55class my_basic_string : public std::basic_string<charT> {
56 public:
57 // Constructors:
58 my_basic_string() = default;
59 explicit my_basic_string(Index n, char c = ' ');
60 my_basic_string(const std::basic_string<charT>& A,
61 Index pos = 0,
63 my_basic_string(const char A[]);
64 my_basic_string(const std::string_view& sv);
65
66 // Insert string before all occurrences of the substring.
68 const my_basic_string<charT>& insstr);
69
70 // Split string
72 const my_basic_string<charT>& delim) const;
73
75 void toupper() {
76 std::transform(this->begin(), this->end(), this->begin(), ::toupper);
77 }
78
80 my_basic_string s = *this;
81 s.toupper();
82 return s;
83 }
84
86 void tolower() {
87 std::transform(this->begin(), this->end(), this->begin(), ::tolower);
88 }
89
91 my_basic_string s = *this;
92 s.tolower();
93 return s;
94 }
95
97 void trim();
98
99 // Number of elements:
100 Index nelem() const;
101
102 // Index operators:
103 char operator[](Index n) const;
105
107 static const Index npos = static_cast<Index>(std::basic_string<charT>::npos);
108
110};
111
112// Member functions for my_basic_string:
113
114// Constructors:
115
123template <class charT>
125 : std::basic_string<charT>(n, c) { /* Nothing to do here. */
126}
127
140template <class charT>
142 const std::basic_string<charT>& A, Index pos, Index numpos) {
143 // Range checks:
144 ARTS_ASSERT(0 <= pos); // Start index must be 0 or greater 0.
145
146 if (!A.size()) return;
147
148 // cout << "A = " << A << "\n";
149 // cout << "pos = " << pos << "\n";
150 // cout << "size = " << A.size() << "\n";
151
152 ARTS_ASSERT(static_cast<typename std::basic_string<charT>::size_type>(pos) <
153 A.size());
154 // At most the last element of the original string.
155
157 ((numpos >= 0) &&
158 (static_cast<typename std::basic_string<charT>::size_type>(numpos) <=
159 (A.size() -
160 pos)))); // Number of characters to copy must be at the most the
161 // number left. -1 means all remaining characters.
162
163 // The assertions look complicated, because we have to cast pos and
164 // npos to the unsigned size type of basic string to avoid warning
165 // messages from the compiler. Both casts are save, because previous
166 // assertions check that pos and npos are positive. (The allowed
167 // case npos -1 (=my_basic_string<charT>::npos) is also handled
168 // correctly.)
169
170 std::basic_string<charT>::operator=(std::basic_string<charT>(A, pos, numpos));
171}
172
174template <class charT>
176 : std::basic_string<charT>(A) { /* Nothing to do here. */
177}
178
180template <class charT>
181inline my_basic_string<charT>::my_basic_string(const std::string_view& sv)
182 : std::basic_string<charT>(std::string(sv)) { /* Nothing to do here. */
183}
184
190template <class charT>
192 const my_basic_string<charT>& searchstr,
193 const my_basic_string<charT>& insstr) {
194 size_t searchstr_size = searchstr.size();
195 size_t insstr_size = insstr.size();
196 size_t start_pos = 0;
197
198 while (start_pos != std::string::npos) {
199 start_pos = this->find(searchstr, start_pos);
200 if (start_pos && start_pos != std::string::npos) {
201 this->insert(start_pos, insstr);
202 start_pos += searchstr_size + insstr_size;
203 }
204 }
205}
206
212template <class charT>
215 const my_basic_string<charT>& delim) const {
216 size_t pos, oldpos;
217 pos = oldpos = 0;
218 aos.resize(0);
219
220 while (oldpos < (size_t)this->nelem() &&
221 (pos = this->find(delim, oldpos)) !=
223 if (pos && pos - oldpos) aos.push_back(this->substr(oldpos, pos - oldpos));
224 oldpos = pos + delim.nelem();
225 }
226
227 if (oldpos < (size_t)this->nelem()) aos.push_back(this->substr(oldpos));
228}
229
231template <class charT>
233 // Create ref to self for readability
234 my_basic_string& this_string = *this;
235
236 // Remove leading whitespace
237 while (0 != this_string.nelem() &&
238 (' ' == this_string[0] || '\t' == this_string[0] ||
239 '\n' == this_string[0] || '\r' == this_string[0]))
240 this_string.erase(0, 1);
241
242 // Remove trailing whitespace
243 while (0 != this_string.nelem() &&
244 (' ' == this_string[this_string.nelem() - 1] ||
245 '\t' == this_string[this_string.nelem() - 1] ||
246 '\n' == this_string[this_string.nelem() - 1] ||
247 '\r' == this_string[this_string.nelem() - 1]))
248 this_string.erase(this_string.nelem() - 1);
249}
250
252template <class charT>
254 size_t s = this->size();
255 ARTS_ASSERT(s < LONG_MAX);
256 return static_cast<long>(s);
257}
258
265template <class charT>
267 ARTS_ASSERT(0 <= n);
268 ARTS_ASSERT(n < nelem());
269 return std::basic_string<charT>::operator[](n);
270}
271
278template <class charT>
280 ARTS_ASSERT(0 <= n);
281 ARTS_ASSERT(n < nelem());
282 return std::basic_string<charT>::operator[](n);
283}
284
288
291
294
303template <class T>
304void extract(T& x, String& line, Index n) {
305 // Initialize output to zero! This is important, because otherwise
306 // the output variable could `remember' old values.
307 x = T(0);
308
309 // This will contain the short subString with the item to extract.
310 // Make it a String stream, for easy parsing,
311 // extracting subString of width n from line:
312 std::istringstream item(line.substr(0, n));
313
314 // cout << "line = '" << line << "'\n";
315 // cout << "line.substr(0,n) = " << line.substr(0,n) << endl;
316 // cout << "item = " << item.str() << endl;
317
318 // Shorten line by n:
319 line.erase(0, n);
320 // cout << "line = " << line << endl;
321
322 // Convert with the aid of String stream item:
323 item >> x;
324}
325
326#endif // mystring_h
This file contains the definition of Array.
This can be used to make arrays out of anything.
Definition: array.h:107
The implementation for String, the ARTS string class.
Definition: mystring.h:55
char & operator[](Index n)
Non-constant index operator.
Definition: mystring.h:279
my_basic_string(Index n, char c=' ')
Constructor setting size.
Definition: mystring.h:124
char operator[](Index n) const
Constant index operator.
Definition: mystring.h:266
void insert_substr(const my_basic_string< charT > &searchstr, const my_basic_string< charT > &insstr)
Insert string before all occurrences of the substring.
Definition: mystring.h:191
Index nelem() const
Number of elements.
Definition: mystring.h:253
void split(Array< my_basic_string< charT > > &aos, const my_basic_string< charT > &delim) const
Split string into substrings.
Definition: mystring.h:213
my_basic_string()=default
my_basic_string tolower() const
Definition: mystring.h:90
void trim()
Trim leading and trailing whitespace.
Definition: mystring.h:232
my_basic_string(const char A[])
Constructor from a C-style char array.
Definition: mystring.h:175
static const Index npos
Define npos:
Definition: mystring.h:107
my_basic_string toupper() const
Definition: mystring.h:79
void toupper()
Convert to upper case.
Definition: mystring.h:75
my_basic_string(const std::basic_string< charT > &A, Index pos=0, Index numpos=my_basic_string< charT >::npos)
Construnctor from a basic_string.
Definition: mystring.h:141
Index size_type
Definition: mystring.h:109
my_basic_string(const std::string_view &sv)
Constructor from a std::string_view.
Definition: mystring.h:181
void tolower()
Convert to lower case.
Definition: mystring.h:86
#define ARTS_ASSERT(condition,...)
Definition: debug.h:83
void transform(VectorView y, double(&my_func)(double), ConstVectorView x)
A generic transform function for vectors, which can be used to implement mathematical functions opera...
Definition: matpackI.cc:1472
INDEX Index
The type to use for all integer numbers and indices.
Definition: matpack.h:39
Array< String > ArrayOfString
An array of Strings.
Definition: mystring.h:290
my_basic_string< char > String
The String type for ARTS.
Definition: mystring.h:287
Array< Array< String > > ArrayOfArrayOfString
An array of Strings.
Definition: mystring.h:293
void extract(T &x, String &line, Index n)
Extract something from the beginning of a string.
Definition: mystring.h:304
Index nelem(const Lines &l)
Number of lines.
VectorView std(VectorView std, const Vector &y, const ArrayOfVector &ys, const Index start, const Index end_tmp)
Compute the standard deviation of the ranged ys.
Definition: raw.cc:205
constexpr Rational end(Rational Ju, Rational Jl, Polarization type) noexcept
Gives the largest M for a polarization type of this transition.
Definition: zeemandata.h:109
#define c