ARTS  1.0.222
mystring.h
Go to the documentation of this file.
1 /* Copyright (C) 2001 Stefan Buehler <sbuehler@uni-bremen.de>
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 string_h
28 #define string_h
29 
30 #include <string>
31 #include <climits>
32 #include <iostream>
33 #include "arts.h"
34 
35 // String stream library. This is included with the ARTS source code
36 // for now, because it is missing in gcc <= 2.95.2
37 #ifdef HAVE_SSTREAM
38 #include <sstream>
39 #else
40 #include "sstream.h"
41 #endif
42 
59 template<class charT>
60 class my_basic_string : public std::basic_string<charT>
61 {
62 public:
63  // Constructors:
65  explicit my_basic_string(Index n, char c=' ');
66  my_basic_string(const basic_string<charT>& A,
67  Index pos=0,
69  my_basic_string(const char A[]);
70 
71  // Assignment operators:
73  // my_basic_string& operator=(const char A[]);
74 
75  // Number of elements:
76  Index nelem() const;
77 
78  // Find functions:
79 // Index find(char c);
80 // Index find(const my_basic_string<charT>& c);
81 
82  // Index operators:
83  char operator[](Index n) const;
84  char& operator[](Index n);
85 
87  static const Index npos = static_cast<Index>(std::basic_string<charT>::npos);
88 };
89 
90 
91 // Member functions for my_basic_string:
92 
93 
94 // Constructors:
95 
97 template<class charT>
98 inline my_basic_string<charT>::my_basic_string() : std::basic_string<charT>()
99 { /* Nothing to do here. */ };
100 
108 template<class charT>
110  std::basic_string<charT>(n,c)
111 { /* Nothing to do here. */ };
112 
114 // template<class charT>
115 // inline my_basic_string<charT>::my_basic_string(const my_basic_string& A) : std::basic_string<charT>(A)
116 // { /* Nothing to do here. */ };
117 
130 template<class charT>
131 inline my_basic_string<charT>::my_basic_string(const basic_string<charT>& A,
132  Index pos,
133  Index npos)
134 {
135  // Range checks:
136  assert(0<=pos); // Start index must be 0 or greater 0.
137 
138 // cout << "A = " << A << "\n";
139 // cout << "pos = " << pos << "\n";
140 // cout << "size = " << A.size() << "\n";
141 
142  assert(static_cast<typename std::basic_string<charT>::size_type>(pos)<A.size());
143  // At most the last element of the original string.
144 
145  assert( npos==my_basic_string<charT>::npos ||
146  ( (npos >= 0) &&
147  (static_cast<typename std::basic_string<charT>::size_type>(npos)<=(A.size()-pos))
148  )
149  ); // Number of characters to copy must be at the most the
150  // number left. -1 means all remaining characters.
151 
152  // The assertions look complicated, because we have to cast pos and
153  // npos to the unsigned size type of basic string to avoid warning
154  // messages from the compiler. Both casts are save, because previous
155  // assertions check that pos and npos are positive. (The allowed
156  // case npos -1 (=my_basic_string<charT>::npos) is also handled
157  // correctly.)
158 
159  std::basic_string<charT>::operator=(std::basic_string<charT>(A,pos,npos));
160 
161 };
162 
164 template<class charT>
165 inline my_basic_string<charT>::my_basic_string(const char A[]) : std::basic_string<charT>(A)
166 { /* Nothing to do here. */ };
167 
168 
176 template<class charT>
178 {
179  std::basic_string<charT>::operator=(A);
180  return *this;
181 }
182 
184 template<class charT>
186 {
187  size_t s = this->size();
188  assert(s<LONG_MAX);
189  return static_cast<INDEX>(s);
190 }
191 
192 // /** Find function for char.
193 
194 // \param c What character to find.
195 // \return Position of c, or npos if not found.
196 
197 // Unfortunately, the std::basid_string.find() functions returns npos
198 // when the character is not found. This is -1, but assigned to a
199 // positive type! Gives a very high number. If you add 1 to this
200 // number you get zero again, so it does indeed bahave like -1 in a
201 // way. Yuck! With Index this does not work. */
202 // template<class charT>
203 // inline Index my_basic_string<charT>::find(char c)
204 // {
205 // std::basic_string<charT>::size_type i = std::basic_string<charT>::find(c);
206 // if ( i == std::basic_string<charT>::npos )
207 // return npos;
208 // else
209 // return static_cast<Index>(i);
210 // }
211 
212 // /** Find function for string.
213 
214 // \param c What string to find.
215 // \return Position of c, or npos if not found.
216 
217 // Unfortunately, the std::basid_string.find() functions returns npos
218 // when the character is not found. This is -1, but assigned to a
219 // positive type! Gives a very high number. If you add 1 to this
220 // number you get zero again, so it does indeed bahave like -1 in a
221 // way. Yuck! With Index this does not work. */
222 // template<class charT>
223 // inline Index my_basic_string<charT>::find(const my_basic_string<charT>& c)
224 // {
225 // std::basic_string<charT>::size_type i = std::basic_string<charT>::find(c);
226 // if ( i == std::basic_string<charT>::npos )
227 // return npos;
228 // else
229 // return static_cast<Index>(i);
230 // }
231 
232 
235 template<class charT>
237 {
238  assert(0<=n);
239  assert(n<nelem());
240  return std::basic_string<charT>::operator[](n);
241 }
242 
245 template<class charT>
247 {
248  assert(0<=n);
249  assert(n<nelem());
250  return std::basic_string<charT>::operator[](n);
251 }
252 
253 // Non-member functions:
254 
255 // /** Output operator. */
256 // inline std::ostream& operator<<(std::ostream& os, const my_basic_string& v)
257 // {
258 // my_basic_string<base>::const_iterator i = v.begin();
259 // const my_basic_string<base>::const_iterator end = v.end();
260 
261 // if ( i!=end )
262 // {
263 // os << *i;
264 // ++i;
265 // }
266 
267 // for ( ; i!=end; ++i )
268 // {
269 // os << "\n" << setw(3) << *i;
270 // }
271 
272 
273 // // Just use the operator of std::string.
274 // operator<<(os,v);
275 // return os;
276 // }
277 
278 
282 
283 // Declare the existance of class Array:
284 template<class base>
285 class Array;
286 
289 
290 
291 #endif // string_h
my_basic_string::operator[]
char operator[](Index n) const
Constant index operator.
Definition: mystring.h:236
my_basic_string::my_basic_string
my_basic_string()
Default constructor.
Definition: mystring.h:98
my_basic_string::my_basic_string
my_basic_string(Index n, char c=' ')
Constructor setting size.
Definition: mystring.h:109
Array
This can be used to make arrays out of anything.
Definition: array.h:48
my_basic_string::operator=
my_basic_string & operator=(const my_basic_string< charT > &A)
Assignment from another my_basic_string.
Definition: mystring.h:177
my_basic_string
The implementation for String, the ARTS string class.
Definition: mystring.h:61
ArrayOfString
Array< String > ArrayOfString
An array of Strings.
Definition: mystring.h:285
my_basic_string::my_basic_string
my_basic_string(const char A[])
Constructor from a C-style char array.
Definition: mystring.h:165
my_basic_string::nelem
Index nelem() const
Number of elements.
Definition: mystring.h:185
Index
INDEX Index
The type to use for all integer numbers and indices.
Definition: arts.h:153
String
my_basic_string< char > String
The String type for ARTS.
Definition: mystring.h:281
my_basic_string::operator[]
char & operator[](Index n)
Non-constant index operator.
Definition: mystring.h:246
my_basic_string::my_basic_string
my_basic_string(const basic_string< charT > &A, Index pos=0, Index npos=my_basic_string< charT >::npos)
Construnctor from another my_basic_string.
Definition: mystring.h:131
my_basic_string::npos
static const Index npos
Define npos:
Definition: mystring.h:87
arts.h
The global header file for ARTS.