ARTS  2.2.66
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 <cassert>
31 #include <climits>
32 #include <string>
33 #include <algorithm>
34 #include "matpack.h"
35 #include "array.h"
36 
37 // String stream library. This is included with the ARTS source code
38 // for now, because it is missing in gcc <= 2.95.2
39 #ifdef HAVE_SSTREAM
40 #include <sstream>
41 #else
42 #include "sstream.h"
43 #endif
44 
45 
62 template<class charT>
63 class my_basic_string : public std::basic_string<charT>
64 {
65 public:
66  // Constructors:
68  explicit my_basic_string(Index n, char c=' ');
69  my_basic_string(const std::basic_string<charT>& A,
70  Index pos=0,
72  my_basic_string(const char A[]);
73 
74  // Assignment operators:
76  // my_basic_string& operator=(const char A[]);
77 
78  // Insert string before all occurrences of the substring.
79  void insert_substr(const my_basic_string<charT>& searchstr,
80  const my_basic_string<charT>& insstr);
81 
82  // Split string
84  const my_basic_string<charT> &delim) const;
85 
87  void toupper() { std::transform ( this->begin(), this->end(),
88  this->begin(), ::toupper); }
89 
91  void tolower() { std::transform ( this->begin(), this->end(),
92  this->begin(), ::tolower); }
93 
95  void trim();
96 
97  // Number of elements:
98  Index nelem() const;
99 
100  // Index operators:
101  char operator[](Index n) const;
102  char& operator[](Index n);
103 
105  static const Index npos = static_cast<Index>(std::basic_string<charT>::npos);
106 
107  typedef Index size_type;
108 };
109 
110 
111 // Member functions for my_basic_string:
112 
113 
114 // Constructors:
115 
117 template<class charT>
118 inline my_basic_string<charT>::my_basic_string() : std::basic_string<charT>()
119 { /* Nothing to do here. */ }
120 
121 
129 template<class charT>
131  std::basic_string<charT>(n,c)
132 { /* Nothing to do here. */ }
133 
134 
147 template<class charT>
148 inline my_basic_string<charT>::my_basic_string(const std::basic_string<charT>& A,
149  Index pos,
150  Index numpos)
151 {
152  // Range checks:
153  assert(0<=pos); // Start index must be 0 or greater 0.
154 
155  if (!A.size()) return;
156 
157 // cout << "A = " << A << "\n";
158 // cout << "pos = " << pos << "\n";
159 // cout << "size = " << A.size() << "\n";
160 
161  assert(static_cast<typename std::basic_string<charT>::size_type>(pos)<A.size());
162  // At most the last element of the original string.
163 
164  assert( numpos==my_basic_string<charT>::npos ||
165  ( (numpos >= 0) &&
166  (static_cast<typename std::basic_string<charT>::size_type>(numpos)<=(A.size()-pos))
167  )
168  ); // Number of characters to copy must be at the most the
169  // number left. -1 means all remaining characters.
170 
171  // The assertions look complicated, because we have to cast pos and
172  // npos to the unsigned size type of basic string to avoid warning
173  // messages from the compiler. Both casts are save, because previous
174  // assertions check that pos and npos are positive. (The allowed
175  // case npos -1 (=my_basic_string<charT>::npos) is also handled
176  // correctly.)
177 
178  std::basic_string<charT>::operator=(std::basic_string<charT>(A,pos,numpos));
179 
180 }
181 
183 template<class charT>
184 inline my_basic_string<charT>::my_basic_string(const char A[]) : std::basic_string<charT>(A)
185 { /* Nothing to do here. */ }
186 
187 
194 template<class charT>
196 {
197  std::basic_string<charT>::operator=(A);
198  return *this;
199 }
200 
201 
207 template<class charT>
209  const my_basic_string<charT>& insstr)
210 {
211  size_t searchstr_size = searchstr.size();
212  size_t insstr_size = insstr.size();
213  size_t start_pos = 0;
214 
215  while (start_pos != std::string::npos)
216  {
217  start_pos = this->find (searchstr, start_pos);
218  if (start_pos && start_pos != std::string::npos)
219  {
220  this->insert (start_pos, insstr);
221  start_pos += searchstr_size + insstr_size;
222  }
223  }
224 }
225 
226 
232 template<class charT>
234  const my_basic_string<charT> &delim) const
235 {
236  size_t pos, oldpos;
237  pos = oldpos = 0;
238  aos.resize(0);
239 
240  while (oldpos < (size_t)this->nelem() &&
241  (pos = this->find(delim, oldpos)) != (size_t)my_basic_string<charT>::npos)
242  {
243  if (pos && pos-oldpos) aos.push_back(this->substr(oldpos, pos-oldpos));
244  oldpos = pos+delim.nelem();
245  }
246 
247  if (oldpos < (size_t)this->nelem()) aos.push_back(this->substr(oldpos));
248 }
249 
250 
252 template<class charT>
254 {
255  // Create ref to self for readability
256  my_basic_string& this_string = *this;
257 
258  // Remove leading whitespace
259  while (0 != this_string.nelem()
260  && (' ' == this_string[0]
261  || '\t' == this_string[0]
262  || '\n' == this_string[0]
263  || '\r' == this_string[0]))
264  this_string.erase(0,1);
265 
266  // Remove trailing whitespace
267  while (0 != this_string.nelem()
268  && (' ' == this_string[this_string.nelem()-1]
269  || '\t' == this_string[this_string.nelem()-1]
270  || '\n' == this_string[this_string.nelem()-1]
271  || '\r' == this_string[this_string.nelem()-1]))
272  this_string.erase(this_string.nelem()-1);
273 }
274 
275 
277 template<class charT>
279 {
280  size_t s = this->size();
281  assert(s<LONG_MAX);
282  return static_cast<long>(s);
283 }
284 
285 
292 template<class charT>
294 {
295  assert(0<=n);
296  assert(n<nelem());
297  return std::basic_string<charT>::operator[](n);
298 }
299 
300 
307 template<class charT>
309 {
310  assert(0<=n);
311  assert(n<nelem());
312  return std::basic_string<charT>::operator[](n);
313 }
314 
315 
319 
322 
323 
332 template<class T>
333 void extract(T& x,
334  String& line,
335  Index n)
336 {
337  // Initialize output to zero! This is important, because otherwise
338  // the output variable could `remember' old values.
339  x = T(0);
340 
341  // This will contain the short subString with the item to extract.
342  // Make it a String stream, for easy parsing,
343  // extracting subString of width n from line:
344  std::istringstream item( line.substr(0,n) );
345 
346  // cout << "line = '" << line << "'\n";
347  // cout << "line.substr(0,n) = " << line.substr(0,n) << endl;
348  // cout << "item = " << item.str() << endl;
349 
350  // Shorten line by n:
351  line.erase(0,n);
352  // cout << "line = " << line << endl;
353 
354  // Convert with the aid of String stream item:
355  item >> x;
356 }
357 
358 #endif // mystring_h
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:1838
my_basic_string::operator[]
char operator[](Index n) const
Constant index operator.
Definition: mystring.h:293
matpack.h
my_basic_string::toupper
void toupper()
Convert to upper case.
Definition: mystring.h:87
my_basic_string::my_basic_string
my_basic_string()
Default constructor.
Definition: mystring.h:118
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:148
my_basic_string::my_basic_string
my_basic_string(Index n, char c=' ')
Constructor setting size.
Definition: mystring.h:130
array.h
This file contains the definition of Array.
extract
void extract(T &x, String &line, Index n)
Extract something from the beginning of a string.
Definition: mystring.h:333
my_basic_string::size_type
Index size_type
Definition: mystring.h:107
Array
This can be used to make arrays out of anything.
Definition: array.h:107
my_basic_string::operator=
my_basic_string & operator=(const my_basic_string< charT > &A)
Assignment from another my_basic_string.
Definition: mystring.h:195
my_basic_string
The implementation for String, the ARTS string class.
Definition: mystring.h:64
ArrayOfString
Array< String > ArrayOfString
An array of Strings.
Definition: mystring.h:321
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:208
my_basic_string::my_basic_string
my_basic_string(const char A[])
Constructor from a C-style char array.
Definition: mystring.h:184
my_basic_string::trim
void trim()
Trim leading and trailing whitespace.
Definition: mystring.h:253
my_basic_string::tolower
void tolower()
Convert to lower case.
Definition: mystring.h:91
my_basic_string::nelem
Index nelem() const
Number of elements.
Definition: mystring.h:278
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:233
String
my_basic_string< char > String
The String type for ARTS.
Definition: mystring.h:318
my_basic_string::operator[]
char & operator[](Index n)
Non-constant index operator.
Definition: mystring.h:308
Index
INDEX Index
The type to use for all integer numbers and indices.
Definition: matpack.h:35
my_basic_string::npos
static const Index npos
Define npos:
Definition: mystring.h:105