ARTS 2.5.4 (git: 31ce4f0e)
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 <fast_float/fast_float.h>
31
32#include <algorithm>
33#include <charconv>
34#include <climits>
35#include <sstream>
36#include <string>
37#include <string_view>
38#include <type_traits>
39
40#include "array.h"
41#include "debug.h"
42#include "matpack.h"
43
60template <class charT>
61class my_basic_string : public std::basic_string<charT> {
62 public:
63 // Constructors:
64 my_basic_string() = default;
65
66 explicit my_basic_string(Index n, char c = ' ')
67 : std::basic_string<charT>(n, c) {}
68
69 my_basic_string(const std::basic_string<charT>& A,
70 Index pos = 0,
72 // Range checks:
73 ARTS_ASSERT(0 <= pos); // Start index must be 0 or greater 0.
74
75 if (!A.size()) return;
76
77 // cout << "A = " << A << "\n";
78 // cout << "pos = " << pos << "\n";
79 // cout << "size = " << A.size() << "\n";
80
81 ARTS_ASSERT(static_cast<typename std::basic_string<charT>::size_type>(pos) <
82 A.size());
83 // At most the last element of the original string.
84
87 ((numpos >= 0) &&
88 (static_cast<typename std::basic_string<charT>::size_type>(numpos) <=
89 (A.size() -
90 pos)))); // Number of characters to copy must be at the most the
91 // number left. -1 means all remaining characters.
92
93 // The assertions look complicated, because we have to cast pos and
94 // npos to the unsigned size type of basic string to avoid warning
95 // messages from the compiler. Both casts are save, because previous
96 // assertions check that pos and npos are positive. (The allowed
97 // case npos -1 (=my_basic_string<charT>::npos) is also handled
98 // correctly.)
99
100 std::basic_string<charT>::operator=(
101 std::basic_string<charT>(A, pos, numpos));
102 }
103
104 my_basic_string(const char* A)
105 : std::basic_string<charT>(A) { /* Nothing to do here. */
106 }
107
108 my_basic_string(const std::string_view& sv)
109 : std::basic_string<charT>(std::string(sv)) { /* Nothing to do here. */
110 }
111
112 // Insert string before all occurrences of the substring.
114 const my_basic_string<charT>& insstr) {
115 size_t searchstr_size = searchstr.size();
116 size_t insstr_size = insstr.size();
117 size_t start_pos = 0;
118
119 while (start_pos != std::string::npos) {
120 start_pos = this->find(searchstr, start_pos);
121 if (start_pos && start_pos != std::string::npos) {
122 this->insert(start_pos, insstr);
123 start_pos += searchstr_size + insstr_size;
124 }
125 }
126 }
127
128 // Split string
130 const my_basic_string<charT>& delim) const {
131 size_t pos, oldpos;
132 pos = oldpos = 0;
133 aos.resize(0);
134
135 while (oldpos < (size_t)this->nelem() &&
136 (pos = this->find(delim, oldpos)) !=
138 if (pos && pos - oldpos)
139 aos.push_back(this->substr(oldpos, pos - oldpos));
140 oldpos = pos + delim.nelem();
141 }
142
143 if (oldpos < (size_t)this->nelem()) aos.push_back(this->substr(oldpos));
144 }
145
147 void toupper() {
148 std::transform(this->begin(), this->end(), this->begin(), ::toupper);
149 }
150
151 [[nodiscard]] my_basic_string toupper() const {
152 my_basic_string s = *this;
153 s.toupper();
154 return s;
155 }
156
158 void tolower() {
159 std::transform(this->begin(), this->end(), this->begin(), ::tolower);
160 }
161
162 [[nodiscard]] my_basic_string tolower() const {
163 my_basic_string s = *this;
164 s.tolower();
165 return s;
166 }
167
169 void trim() {
170 // Create ref to self for readability
171 my_basic_string& this_string = *this;
172
173 // Remove leading whitespace
174 while (0 != this_string.nelem() &&
175 (' ' == this_string[0] || '\t' == this_string[0] ||
176 '\n' == this_string[0] || '\r' == this_string[0]))
177 this_string.erase(0, 1);
178
179 // Remove trailing whitespace
180 while (0 != this_string.nelem() &&
181 (' ' == this_string[this_string.nelem() - 1] ||
182 '\t' == this_string[this_string.nelem() - 1] ||
183 '\n' == this_string[this_string.nelem() - 1] ||
184 '\r' == this_string[this_string.nelem() - 1]))
185 this_string.erase(this_string.nelem() - 1);
186 }
187
188 // Number of elements:
189 [[nodiscard]] Index nelem() const {
190 size_t s = this->size();
191 ARTS_ASSERT(s < LONG_MAX);
192 return static_cast<long>(s);
193 }
194
195 // Index operators:
196 char operator[](Index n) const {
197 ARTS_ASSERT(0 <= n);
198 ARTS_ASSERT(n < nelem());
199 return std::basic_string<charT>::operator[](n);
200 }
201
202 char& operator[](Index n) {
203 ARTS_ASSERT(0 <= n);
204 ARTS_ASSERT(n < nelem());
205 return std::basic_string<charT>::operator[](n);
206 }
207
209 static const Index npos = static_cast<Index>(std::basic_string<charT>::npos);
210
212};
213
217
220
223
232template <class T>
233void extract(T& x, String& line, std::size_t n) {
234 // Initialize output to zero! This is important, because otherwise
235 // the output variable could `remember' old values.
236 x = T(0);
237
238 const std::size_t N = n;
239 std::size_t i = 0;
240 while (i < N and i < line.size() and isspace(line[i])) ++i;
241 while (n > i and (n-1) < line.size() and isspace(line[n-1])) --n;
242
243 if constexpr (std::is_same_v<double, T> or std::is_same_v<float, T>) {
244 fast_float::from_chars(line.data() + i, line.data() + n, x);
245 } else if constexpr (std::is_same_v<long long, T> or
246 std::is_same_v<long, T> or std::is_same_v<int, T>) {
247 std::from_chars(line.data() + i, line.data() + n, x);
248 } else {
249 // This will contain the short subString with the item to extract.
250 // Make it a String stream, for easy parsing,
251 // extracting subString of width n from line:
252 std::istringstream item(line.substr(i, n));
253
254 // Convert with the aid of String stream item:
255 item >> x;
256 }
257
258 // Shorten line by n:
259 line.erase(0, N);
260}
261
262#endif // mystring_h
This file contains the definition of Array.
This can be used to make arrays out of anything.
Definition: array.h:48
The implementation for String, the ARTS string class.
Definition: mystring.h:61
char & operator[](Index n)
Definition: mystring.h:202
my_basic_string(Index n, char c=' ')
Definition: mystring.h:66
char operator[](Index n) const
Definition: mystring.h:196
void insert_substr(const my_basic_string< charT > &searchstr, const my_basic_string< charT > &insstr)
Definition: mystring.h:113
Index nelem() const
Definition: mystring.h:189
void split(Array< my_basic_string< charT > > &aos, const my_basic_string< charT > &delim) const
Definition: mystring.h:129
my_basic_string()=default
my_basic_string tolower() const
Definition: mystring.h:162
Index size_type
Definition: mystring.h:211
void trim()
Trim leading and trailing whitespace.
Definition: mystring.h:169
static const Index npos
Define npos:
Definition: mystring.h:209
my_basic_string toupper() const
Definition: mystring.h:151
void toupper()
Convert to upper case.
Definition: mystring.h:147
my_basic_string(const std::basic_string< charT > &A, Index pos=0, Index numpos=my_basic_string< charT >::npos)
Definition: mystring.h:69
my_basic_string(const char *A)
Definition: mystring.h:104
my_basic_string(const std::string_view &sv)
Definition: mystring.h:108
void tolower()
Convert to lower case.
Definition: mystring.h:158
Helper macros for debugging.
#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:1431
INDEX Index
The type to use for all integer numbers and indices.
Definition: matpack.h:39
void extract(T &x, String &line, std::size_t n)
Extract something from the beginning of a string.
Definition: mystring.h:233
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:113
constexpr int isspace(int ch) noexcept
Returns 1 if x is a standard space-character.
Definition: nonstd.h:39
#define N
Definition: rng.cc:164
#define c