ARTS  2.0.49
mystring.h
Go to the documentation of this file.
1 /* Copyright (C) 2001-2008 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 "matpack.h"
34 #include "array.h"
35 
36 // String stream library. This is included with the ARTS source code
37 // for now, because it is missing in gcc <= 2.95.2
38 #ifdef HAVE_SSTREAM
39 #include <sstream>
40 #else
41 #include "sstream.h"
42 #endif
43 
60 template<class charT>
61 class my_basic_string : public basic_string<charT>
62 {
63 public:
64  // Constructors:
66  explicit my_basic_string(Index n, char c=' ');
67  my_basic_string(const basic_string<charT>& A,
68  Index pos=0,
70  my_basic_string(const char A[]);
71 
72  // Assignment operators:
74  // my_basic_string& operator=(const char A[]);
75 
76  // Insert string before all occurrences of the substring.
77  void insert_substr(const my_basic_string<charT>& searchstr,
78  const my_basic_string<charT>& insstr);
79 
80  // Split string
82  const my_basic_string<charT> &delim) const;
83 
84  // Number of elements:
85  Index nelem() const;
86 
87  // Find functions:
88 // Index find(char c);
89 // Index find(const my_basic_string<charT>& c);
90 
91  // Index operators:
92  char operator[](Index n) const;
93  char& operator[](Index n);
94 
96  static const Index npos = static_cast<Index>(basic_string<charT>::npos);
97 
98  typedef Index size_type;
99 };
100 
101 
102 // Member functions for my_basic_string:
103 
104 
105 // Constructors:
106 
108 template<class charT>
109 inline my_basic_string<charT>::my_basic_string() : basic_string<charT>()
110 { /* Nothing to do here. */ }
111 
119 template<class charT>
121  basic_string<charT>(n,c)
122 { /* Nothing to do here. */ }
123 
125 // template<class charT>
126 // inline my_basic_string<charT>::my_basic_string(const my_basic_string& A) : basic_string<charT>(A)
127 // { /* Nothing to do here. */ };
128 
141 template<class charT>
142 inline my_basic_string<charT>::my_basic_string(const basic_string<charT>& A,
143  Index pos,
144  Index numpos)
145 {
146  // Range checks:
147  assert(0<=pos); // Start index must be 0 or greater 0.
148 
149 // cout << "A = " << A << "\n";
150 // cout << "pos = " << pos << "\n";
151 // cout << "size = " << A.size() << "\n";
152 
153  assert(static_cast<typename basic_string<charT>::size_type>(pos)<A.size());
154  // At most the last element of the original string.
155 
156  assert( numpos==my_basic_string<charT>::npos ||
157  ( (numpos >= 0) &&
158  (static_cast<typename basic_string<charT>::size_type>(numpos)<=(A.size()-pos))
159  )
160  ); // 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  basic_string<charT>::operator=(basic_string<charT>(A,pos,numpos));
171 
172 }
173 
175 template<class charT>
176 inline my_basic_string<charT>::my_basic_string(const char A[]) : basic_string<charT>(A)
177 { /* Nothing to do here. */ }
178 
179 
186 template<class charT>
188 {
189  basic_string<charT>::operator=(A);
190  return *this;
191 }
192 
198 template<class charT>
200  const my_basic_string<charT>& insstr)
201 {
202  size_t searchstr_size = searchstr.size();
203  size_t insstr_size = insstr.size();
204  size_t start_pos = 0;
205 
206  while (start_pos != string::npos)
207  {
208  start_pos = this->find (searchstr, start_pos);
209  if (start_pos && start_pos != string::npos)
210  {
211  this->insert (start_pos, insstr);
212  start_pos += searchstr_size + insstr_size;
213  }
214  }
215 }
216 
222 template<class charT>
224  const my_basic_string<charT> &delim) const
225 {
226  size_t pos, oldpos;
227  pos = oldpos = 0;
228  aos.resize(0);
229 
230  while (oldpos < (size_t)this->nelem() &&
231  (pos = this->find(delim, oldpos)) != (size_t)my_basic_string<charT>::npos)
232  {
233  if (pos) aos.push_back(this->substr(oldpos, pos-oldpos));
234  oldpos = pos+delim.nelem();
235  }
236 
237  if (oldpos < (size_t)this->nelem()) aos.push_back(this->substr(oldpos));
238 }
239 
241 template<class charT>
243 {
244  size_t s = this->size();
245  assert(s<LONG_MAX);
246  return static_cast<long>(s);
247 }
248 
255 template<class charT>
257 {
258  assert(0<=n);
259  assert(n<nelem());
260  return basic_string<charT>::operator[](n);
261 }
262 
269 template<class charT>
271 {
272  assert(0<=n);
273  assert(n<nelem());
274  return basic_string<charT>::operator[](n);
275 }
276 
277 // Non-member functions:
278 
279 // /** Output operator. */
280 // inline ostream& operator<<(ostream& os, const my_basic_string& v)
281 // {
282 // my_basic_string<base>::const_iterator i = v.begin();
283 // const my_basic_string<base>::const_iterator end = v.end();
284 
285 // if ( i!=end )
286 // {
287 // os << *i;
288 // ++i;
289 // }
290 
291 // for ( ; i!=end; ++i )
292 // {
293 // os << "\n" << setw(3) << *i;
294 // }
295 
296 
297 // // Just use the operator of string.
298 // operator<<(os,v);
299 // return os;
300 // }
301 
302 
306 
307 // Declare the existance of class Array:
308 /*template<class base>
309 class Array;*/
310 
313 
314 //
315 // We don't use this function, we use string.find() instead.
316 //
317 // //! Find first occurance.
318 // /*!
319 // This returns the index of the first occurance of w in
320 // string x.
321 //
322 // A return value of -1 indicates that no matching element was found.
323 //
324 // \return The index of the thing we looked for.
325 // \param x The string to search.
326 // \param w The character to look for.
327 //
328 // \author Stefan Buehler
329 // \date 2002-12-06
330 // */
331 // template <class base>
332 // Index find_first( const my_basic_string<base>& x,
333 // const base& w )
334 // {
335 // for ( Index i=0; i<x.nelem(); ++i )
336 // if ( w == x[i] )
337 // return i;
338 //
339 // return -1;
340 // }
341 
342 
343 #endif // mystring_h
my_basic_string::operator[]
char operator[](Index n) const
Constant index operator.
Definition: mystring.h:256
matpack.h
my_basic_string::my_basic_string
my_basic_string()
Default constructor.
Definition: mystring.h:109
my_basic_string::my_basic_string
my_basic_string(Index n, char c=' ')
Constructor setting size.
Definition: mystring.h:120
array.h
This file contains the definition of Array.
my_basic_string::size_type
Index size_type
Definition: mystring.h:98
Array
This can be used to make arrays out of anything.
Definition: array.h:103
my_basic_string::operator=
my_basic_string & operator=(const my_basic_string< charT > &A)
Assignment from another my_basic_string.
Definition: mystring.h:187
my_basic_string
The implementation for String, the ARTS string class.
Definition: mystring.h:62
ArrayOfString
Array< String > ArrayOfString
An array of Strings.
Definition: mystring.h:312
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:199
my_basic_string::my_basic_string
my_basic_string(const char A[])
Constructor from a C-style char array.
Definition: mystring.h:176
my_basic_string::nelem
Index nelem() const
Number of elements.
Definition: mystring.h:242
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:223
String
my_basic_string< char > String
The String type for ARTS.
Definition: mystring.h:305
my_basic_string::operator[]
char & operator[](Index n)
Non-constant index operator.
Definition: mystring.h:270
Index
INDEX Index
The type to use for all integer numbers and indices.
Definition: matpack.h:39
my_basic_string::my_basic_string
my_basic_string(const basic_string< charT > &A, Index pos=0, Index numpos=my_basic_string< charT >::npos)
Construnctor from another my_basic_string.
Definition: mystring.h:142
my_basic_string::npos
static const Index npos
Define npos:
Definition: mystring.h:96