Loading [MathJax]/extensions/tex2jax.js
String.hpp
1 #pragma once
2 
3 #include <opals/fwd.hpp>
4 
5 #include <cstddef>
6 #include <type_traits>
7 
8 namespace std
9 {
10  struct random_access_iterator_tag;
11 }
12 
13 namespace opals
14 {
15 
16  /**
17  \class String
18 
19  \brief A dynamic character string whose interface conforms to STL's std::string
20 
21  \author wk
22  \date 02.02.2011
23 
24  \internal
25  opals::String is based on STL's std::string, which it however hides from the
26  interface in order to avoid problems when using the interface / opals-DLLs
27  with different STL-implementations.
28  The interface of opals::String differs slightly from the one of std::string:
29  - no template functions 'insert'
30  -> these functions are defined as overloaded non-template functions
31  for all String-iterator types
32  Conversion operators from/to std::string and its iterators, and
33  operator<< and operator>> for basic_istream and basic_ostream are provided for internal use only.
34  */
35  class String
36  {
37  struct Impl;
38  Impl *pimpl_;
39 
40  public:
41  template<bool Const, bool Forward>
42  class OPALS_API Iterator;
43 
48 
49  using value_type = char;
50  using pointer = char*;
51  using reference = char&;
52  using const_reference = char const&;
53  using size_type = std::size_t;
54  using difference_type = std::ptrdiff_t;
55 
56  static const size_type& npos();
57 
58  /// \name construction / destruction
59  ///@{
60  String();
61  String(const String& that);
62  String(const String& that, size_type pos, size_type n = npos());
63  String(String&& that);
64  String(const char* s);
65  String(const char* s, size_type n);
66  String(size_type n, char c);
67  String(iterator beg, iterator end);
71  ~String();
72  ///@}
73 
74  /// \name assignment, swapping
75  ///@{
76  String& operator=(const String& that);
77  String& operator=(String&& that);
78  String& operator=(const char* that);
79  String& operator=(char c);
80  String& assign(const String&);
81  String& assign(const String& s, size_type pos, size_type n);
82  String& assign(const char* s, size_type n);
83  String& assign(const char* s);
84  String& assign(size_type n, char c);
85  String& assign(iterator beg, iterator end);
86  String& assign(const_iterator beg, const_iterator end);
87  String& assign(reverse_iterator beg, reverse_iterator end);
89  void swap(String& that);
90  ///@}
91 
92  /// \name iterator traversal
93  /// forward/reverse, const/non-const
94  ///@{
95  iterator begin();
96  const_iterator begin() const;
97  const_iterator cbegin() const;
98 
99  iterator end();
100  const_iterator end() const;
101  const_iterator cend() const;
102 
103  reverse_iterator rbegin();
104  const_reverse_iterator rbegin() const;
105  const_reverse_iterator crbegin() const;
106 
107  reverse_iterator rend();
108  const_reverse_iterator rend() const;
109  const_reverse_iterator crend() const;
110  ///@}
111 
112  /// \name element access
113  ///@{
114  reference at(size_type n);
115  const_reference at(size_type n) const;
116  reference operator[](size_type n);
117  const_reference operator[](size_type n) const;
118  reference front();
119  const_reference front() const;
120  reference back();
121  const_reference back() const;
122  const char* c_str() const;
123  const char* data() const;
124  ///@}
125 
126  /// \name size
127  ///@{
128  size_type size() const;
129  size_type length() const;
130  size_type max_size() const;
131  size_type capacity() const;
132  bool empty() const;
133  void clear();
134  void resize(size_type n, char c = char());
135  ///@}
136 
137  /// \name append
138  ///@{
139  String& append(const String& s);
140  String& append(const String& s, size_type pos, size_type n);
141  String& append(const char* s);
142  String& append(const char* s, size_type n);
143  String& append(size_type n, char c);
144  String& append(iterator beg, iterator end);
145  String& append(const_iterator beg, const_iterator end);
146  String& append(reverse_iterator beg, reverse_iterator end);
148 
149  void push_back(char c);
150 
151  String& operator+=(const String& s);
152  String& operator+=(const char* s);
153  String& operator+=(char c);
154  ///@}
155 
156  /// \name insert
157  ///@{
158  iterator insert(iterator pos, const char& s);
159  void insert(iterator pos, size_type n, const char& s);
160  String& insert(size_type n, const String& s);
161  String& insert(size_type pos, const String& s, size_type pos1, size_type n);
162  String& insert(size_type pos, const char* s);
163  String& insert(size_type pos, const char* s, size_type n);
164  String& insert(size_type pos, size_type n, char c);
165  void insert(iterator pos, iterator beg, iterator end);
166  void insert(iterator pos, const_iterator beg, const_iterator end);
167  void insert(iterator pos, reverse_iterator beg, reverse_iterator end);
168  void insert(iterator pos, const_reverse_iterator beg, const_reverse_iterator end);
169  ///@}
170 
171  /// \name erase
172  ///@{
173  iterator erase(iterator p);
174  iterator erase(iterator first, iterator last);
175  String& erase(size_type pos = 0, size_type n = npos());
176  ///@}
177 
178  /// \name replace
179  ///@{
180  String& replace(size_type pos, size_type n, const String& s);
181  String& replace(size_type pos, size_type n, const String& s, size_type pos1, size_type n1);
182  String& replace(size_type pos, size_type n, const char* s, size_type n1);
183  String& replace(size_type pos, size_type n, const char* s);
184  String& replace(size_type pos, size_type n, size_type n1, char c);
185  String& replace(iterator first, iterator last, const String& s);
186  String& replace(iterator first, iterator last, const char* s, size_type n);
187  String& replace(iterator first, iterator last, const char* s);
188  String& replace(iterator first, iterator last, size_type n, char c);
189  String& replace(iterator beg1, iterator end1, iterator beg2, iterator end2);
190  String& replace(iterator beg1, iterator end1, const_iterator beg2, const_iterator end2);
191  String& replace(iterator beg1, iterator end1, reverse_iterator beg2, reverse_iterator end2);
192  String& replace(iterator beg1, iterator end1, const_reverse_iterator beg2, const_reverse_iterator end2);
193  ///@}
194 
195  /// \name find
196  /// forward/reverse
197  ///@{
198  size_type find(const String& s, size_type pos = 0) const;
199  size_type find(const char* s, size_type pos, size_type n) const;
200  size_type find(const char* s, size_type pos = 0) const;
201  size_type find(char c, size_type pos = 0) const;
202  size_type rfind(const String& s, size_type pos = npos()) const;
203  size_type rfind(const char* s, size_type pos, size_type n) const;
204  size_type rfind(const char* s, size_type pos = npos()) const;
205  size_type rfind(char c, size_type pos = npos()) const;
206  size_type find_first_of(const String& s, size_type pos = 0) const;
207  size_type find_first_of(const char* s, size_type pos, size_type n) const;
208  size_type find_first_of(const char* s, size_type pos = 0) const;
209  size_type find_first_of(char c, size_type pos = 0) const;
210  size_type find_first_not_of(const String& s, size_type pos = 0) const;
211  size_type find_first_not_of(const char* s, size_type pos, size_type n) const;
212  size_type find_first_not_of(const char* s, size_type pos = 0) const;
213  size_type find_first_not_of(char c, size_type pos = 0) const;
214  size_type find_last_of(const String& s, size_type pos = npos()) const;
215  size_type find_last_of(const char* s, size_type pos, size_type n) const;
216  size_type find_last_of(const char* s, size_type pos = npos()) const;
217  size_type find_last_of(char c, size_type pos = npos()) const;
218  size_type find_last_not_of(const String& s, size_type pos = npos()) const;
219  size_type find_last_not_of(const char* s, size_type pos, size_type n) const;
220  size_type find_last_not_of(const char* s, size_type pos = npos()) const;
221  size_type find_last_not_of(char c, size_type pos = npos()) const;
222  ///@}
223 
224  /// \name substring
225  ///@{
226  String substr(size_type pos = 0, size_type n = npos()) const;
227  ///@}
228 
229  /// \name copy to buffer
230  ///@{
231  size_type copy(char* buf, size_type n, size_type pos = 0) const;
232  ///@}
233 
234  /// \name 3-way comparison
235  ///@{
236  int compare(const String& s) const;
237  int compare(size_type pos, size_type n, const String& s) const;
238  int compare(size_type pos, size_type n, const String& s, size_type pos1, size_type n1) const;
239  int compare(const char* s) const;
240  int compare(size_type pos, size_type n, const char* s, size_type len = npos()) const;
241  ///@}
242 
243  /**
244  \class Iterator
245  \tparam Const constant-ness
246  \tparam Forward forward/reverse traversal
247 
248  \brief A random access iterator class for String
249 
250  \author wk
251  \date 02.02.2011
252  */
253  template<bool Const, bool Forward>
254  class Iterator
255  {
256  using Char = typename std::conditional<Const, const char, char>::type;
257  struct Impl;
258  Impl *pimpl_;
259 
260  public:
261  using iterator_category = std::random_access_iterator_tag;
262  using value_type = char;
263  using difference_type = String::difference_type;
264  using reference = Char&;
265  using pointer = Char*;
267 
268  Iterator();
269  Iterator(const Iterator& that);
270  operator ConstIterator() const;
271  ~Iterator();
272  Iterator &operator= (const Iterator& that);
273  reference operator* () const;
274  pointer operator->() const;
275  Iterator& operator++();
276  Iterator operator++(int);
277  Iterator& operator--();
278  Iterator operator--(int);
279  Iterator& operator+=(difference_type offset);
280  Iterator operator+ (difference_type offset) const;
281  Iterator& operator-=(difference_type offset);
282  Iterator operator- (difference_type offset) const;
283  difference_type operator-(const Iterator<true, Forward>& that) const;
284  difference_type operator-(const Iterator<false, Forward>& that) const;
285  reference operator[](difference_type offset) const;
286 
287  bool operator==(const Iterator<true, Forward>& that) const;
288  bool operator==(const Iterator<false, Forward>& that) const;
289  bool operator!=(const Iterator<true, Forward>& that) const;
290  bool operator!=(const Iterator<false, Forward>& that) const;
291  bool operator< (const Iterator<true, Forward>& that) const;
292  bool operator< (const Iterator<false, Forward>& that) const;
293  bool operator> (const Iterator<true, Forward>& that) const;
294  bool operator> (const Iterator<false, Forward>& that) const;
295  bool operator<=(const Iterator<true, Forward>& that) const;
296  bool operator<=(const Iterator<false, Forward>& that) const;
297  bool operator>=(const Iterator<true, Forward>& that) const;
298  bool operator>=(const Iterator<false, Forward>& that) const;
299  };
300 
301  };
302 
303  /// \relatesalso String
304  OPALS_API String operator+(const String& s1, const String& s2);
305  /// \relatesalso String
306  OPALS_API String operator+(const char* s1, const String& s2);
307  /// \relatesalso String
308  OPALS_API String operator+(const String& s1, const char* s2);
309  /// \relatesalso String
310  OPALS_API String operator+(char c, const String& s2);
311  /// \relatesalso String
312  OPALS_API String operator+(const String& s1, char c);
313 
314  /// \relatesalso String
315  OPALS_API bool operator==(const String& s1, const String& s2);
316  /// \relatesalso String
317  OPALS_API bool operator==(const char* s1, const String& s2);
318  /// \relatesalso String
319  OPALS_API bool operator==(const String& s1, const char* s2);
320 
321  /// \relatesalso String
322  OPALS_API bool operator!=(const String& s1, const String& s2);
323  /// \relatesalso String
324  OPALS_API bool operator!=(const char* s1, const String& s2);
325  /// \relatesalso String
326  OPALS_API bool operator!=(const String& s1, const char* s2);
327 
328  /// \relatesalso String
329  OPALS_API bool operator<(const String& s1, const String& s2);
330  /// \relatesalso String
331  OPALS_API bool operator<(const char* s1, const String& s2);
332  /// \relatesalso String
333  OPALS_API bool operator<(const String& s1, const char* s2);
334 
335  /// \relatesalso String
336  OPALS_API void swap(String& s1, String& s2);
337 
338 }
A random access iterator class for String.
Definition: String.hpp:42
Contains the public interface of OPALS.
Definition: AbsValueOrQuantile.hpp:8
A dynamic character string whose interface conforms to STL's std::string.
Definition: String.hpp:35