ARTS  2.4.0(git:4fb77825)
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 <cassert>
32 #include <climits>
33 #include <sstream>
34 #include <string>
35 #include "array.h"
36 #include "matpack.h"
37 
54 template <class charT>
55 class 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 
65  // Insert string before all occurrences of the substring.
66  void insert_substr(const my_basic_string<charT>& searchstr,
67  const my_basic_string<charT>& insstr);
68 
69  // Split string
71  const my_basic_string<charT>& delim) const;
72 
74  void toupper() {
75  std::transform(this->begin(), this->end(), this->begin(), ::toupper);
76  }
77 
79  my_basic_string s = *this;
80  s.toupper();
81  return s;
82  }
83 
85  void tolower() {
86  std::transform(this->begin(), this->end(), this->begin(), ::tolower);
87  }
88 
90  my_basic_string s = *this;
91  s.tolower();
92  return s;
93  }
94 
96  void trim();
97 
98  // Number of elements:
99  Index nelem() const;
100 
101  // Index operators:
102  char operator[](Index n) const;
103  char& operator[](Index n);
104 
106  static const Index npos = static_cast<Index>(std::basic_string<charT>::npos);
107 
108  typedef Index size_type;
109 };
110 
111 // Member functions for my_basic_string:
112 
113 // Constructors:
114 
122 template <class charT>
124  : std::basic_string<charT>(n, c) { /* Nothing to do here. */
125 }
126 
139 template <class charT>
141  const std::basic_string<charT>& A, Index pos, Index numpos) {
142  // Range checks:
143  assert(0 <= pos); // Start index must be 0 or greater 0.
144 
145  if (!A.size()) return;
146 
147  // cout << "A = " << A << "\n";
148  // cout << "pos = " << pos << "\n";
149  // cout << "size = " << A.size() << "\n";
150 
151  assert(static_cast<typename std::basic_string<charT>::size_type>(pos) <
152  A.size());
153  // At most the last element of the original string.
154 
155  assert(numpos == my_basic_string<charT>::npos ||
156  ((numpos >= 0) &&
157  (static_cast<typename std::basic_string<charT>::size_type>(numpos) <=
158  (A.size() -
159  pos)))); // Number of characters to copy must be at the most the
160  // number left. -1 means all remaining characters.
161 
162  // The assertions look complicated, because we have to cast pos and
163  // npos to the unsigned size type of basic string to avoid warning
164  // messages from the compiler. Both casts are save, because previous
165  // assertions check that pos and npos are positive. (The allowed
166  // case npos -1 (=my_basic_string<charT>::npos) is also handled
167  // correctly.)
168 
169  std::basic_string<charT>::operator=(std::basic_string<charT>(A, pos, numpos));
170 }
171 
173 template <class charT>
175  : std::basic_string<charT>(A) { /* Nothing to do here. */
176 }
177 
183 template <class charT>
185  const my_basic_string<charT>& searchstr,
186  const my_basic_string<charT>& insstr) {
187  size_t searchstr_size = searchstr.size();
188  size_t insstr_size = insstr.size();
189  size_t start_pos = 0;
190 
191  while (start_pos != std::string::npos) {
192  start_pos = this->find(searchstr, start_pos);
193  if (start_pos && start_pos != std::string::npos) {
194  this->insert(start_pos, insstr);
195  start_pos += searchstr_size + insstr_size;
196  }
197  }
198 }
199 
205 template <class charT>
208  const my_basic_string<charT>& delim) const {
209  size_t pos, oldpos;
210  pos = oldpos = 0;
211  aos.resize(0);
212 
213  while (oldpos < (size_t)this->nelem() &&
214  (pos = this->find(delim, oldpos)) !=
216  if (pos && pos - oldpos) aos.push_back(this->substr(oldpos, pos - oldpos));
217  oldpos = pos + delim.nelem();
218  }
219 
220  if (oldpos < (size_t)this->nelem()) aos.push_back(this->substr(oldpos));
221 }
222 
224 template <class charT>
226  // Create ref to self for readability
227  my_basic_string& this_string = *this;
228 
229  // Remove leading whitespace
230  while (0 != this_string.nelem() &&
231  (' ' == this_string[0] || '\t' == this_string[0] ||
232  '\n' == this_string[0] || '\r' == this_string[0]))
233  this_string.erase(0, 1);
234 
235  // Remove trailing whitespace
236  while (0 != this_string.nelem() &&
237  (' ' == this_string[this_string.nelem() - 1] ||
238  '\t' == this_string[this_string.nelem() - 1] ||
239  '\n' == this_string[this_string.nelem() - 1] ||
240  '\r' == this_string[this_string.nelem() - 1]))
241  this_string.erase(this_string.nelem() - 1);
242 }
243 
245 template <class charT>
247  size_t s = this->size();
248  assert(s < LONG_MAX);
249  return static_cast<long>(s);
250 }
251 
258 template <class charT>
260  assert(0 <= n);
261  assert(n < nelem());
262  return std::basic_string<charT>::operator[](n);
263 }
264 
271 template <class charT>
273  assert(0 <= n);
274  assert(n < nelem());
275  return std::basic_string<charT>::operator[](n);
276 }
277 
281 
284 
287 
296 template <class T>
297 void extract(T& x, String& line, Index n) {
298  // Initialize output to zero! This is important, because otherwise
299  // the output variable could `remember' old values.
300  x = T(0);
301 
302  // This will contain the short subString with the item to extract.
303  // Make it a String stream, for easy parsing,
304  // extracting subString of width n from line:
305  std::istringstream item(line.substr(0, n));
306 
307  // cout << "line = '" << line << "'\n";
308  // cout << "line.substr(0,n) = " << line.substr(0,n) << endl;
309  // cout << "item = " << item.str() << endl;
310 
311  // Shorten line by n:
312  line.erase(0, n);
313  // cout << "line = " << line << endl;
314 
315  // Convert with the aid of String stream item:
316  item >> x;
317 }
318 
319 #endif // mystring_h
my_basic_string::my_basic_string
my_basic_string()=default
transform
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:1476
my_basic_string::operator[]
char operator[](Index n) const
Constant index operator.
Definition: mystring.h:259
matpack.h
my_basic_string::toupper
void toupper()
Convert to upper case.
Definition: mystring.h:74
my_basic_string::my_basic_string
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:140
my_basic_string::my_basic_string
my_basic_string(Index n, char c=' ')
Constructor setting size.
Definition: mystring.h:123
linalg::std
void std(VectorView std, const Vector &y, const ArrayOfVector &ys, const Index start=0, const Index end=-1)
Compute the standard deviation of the ranged ys.
Definition: raw.cc:66
ArrayOfArrayOfString
Array< Array< String > > ArrayOfArrayOfString
An array of Strings.
Definition: mystring.h:286
array.h
This file contains the definition of Array.
my_basic_string::toupper
my_basic_string toupper() const
Definition: mystring.h:78
extract
void extract(T &x, String &line, Index n)
Extract something from the beginning of a string.
Definition: mystring.h:297
my_basic_string::size_type
Index size_type
Definition: mystring.h:108
Array
This can be used to make arrays out of anything.
Definition: array.h:108
Absorption::nelem
Index nelem(const Lines &l)
Number of lines.
Definition: absorptionlines.h:1820
my_basic_string
The implementation for String, the ARTS string class.
Definition: mystring.h:55
ArrayOfString
Array< String > ArrayOfString
An array of Strings.
Definition: mystring.h:283
my_basic_string::insert_substr
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:184
my_basic_string::my_basic_string
my_basic_string(const char A[])
Constructor from a C-style char array.
Definition: mystring.h:174
my_basic_string::trim
void trim()
Trim leading and trailing whitespace.
Definition: mystring.h:225
my_basic_string::tolower
void tolower()
Convert to lower case.
Definition: mystring.h:85
Zeeman::end
constexpr Rational end(Rational Ju, Rational Jl, Polarization type) noexcept
Gives the largest M for a polarization type of this transition.
Definition: zeemandata.h:108
my_basic_string::nelem
Index nelem() const
Number of elements.
Definition: mystring.h:246
my_basic_string::split
void split(Array< my_basic_string< charT > > &aos, const my_basic_string< charT > &delim) const
Split string into substrings.
Definition: mystring.h:206
String
my_basic_string< char > String
The String type for ARTS.
Definition: mystring.h:280
my_basic_string::operator[]
char & operator[](Index n)
Non-constant index operator.
Definition: mystring.h:272
my_basic_string::tolower
my_basic_string tolower() const
Definition: mystring.h:89
ARTS::Var::x
Vector x(Workspace &ws) noexcept
Definition: autoarts.h:7346
Index
INDEX Index
The type to use for all integer numbers and indices.
Definition: matpack.h:39
my_basic_string::npos
static const Index npos
Define npos:
Definition: mystring.h:106