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  class 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& other, size_type pos = 0, size_type n = npos());
62  String(const char* s);
63  String(const char* s, size_type n);
64  String(size_type n, char c);
65  String(iterator beg, iterator end);
69  ~String();
70  ///@}
71 
72  /// \name assignment, swapping
73  ///@{
74  String& operator=(const String& other);
75  String& operator=(const char* other);
76  String& operator=(char c);
77  String& assign(const String&);
78  String& assign(const String& s, size_type pos, size_type n);
79  String& assign(const char* s, size_type n);
80  String& assign(const char* s);
81  String& assign(size_type n, char c);
82  String& assign(iterator beg, iterator end);
83  String& assign(const_iterator beg, const_iterator end);
84  String& assign(reverse_iterator beg, reverse_iterator end);
86  void swap(String& other);
87  ///@}
88 
89  /// \name iterator traversal
90  /// forward/reverse, const/non-const
91  ///@{
92  iterator begin();
93  const_iterator begin() const;
94  const_iterator cbegin() const;
95 
96  iterator end();
97  const_iterator end() const;
98  const_iterator cend() const;
99 
100  reverse_iterator rbegin();
101  const_reverse_iterator rbegin() const;
102  const_reverse_iterator crbegin() const;
103 
104  reverse_iterator rend();
105  const_reverse_iterator rend() const;
106  const_reverse_iterator crend() const;
107  ///@}
108 
109  /// \name element access
110  ///@{
111  reference at(size_type n);
112  const_reference at(size_type n) const;
113  reference operator[](size_type n);
114  const_reference operator[](size_type n) const;
115  reference front();
116  const_reference front() const;
117  reference back();
118  const_reference back() const;
119  const char* c_str() const;
120  const char* data() const;
121  ///@}
122 
123  /// \name size
124  ///@{
125  size_type size() const;
126  size_type length() const;
127  size_type max_size() const;
128  size_type capacity() const;
129  bool empty() const;
130  void clear();
131  void resize(size_type n, char c = char());
132  ///@}
133 
134  /// \name append
135  ///@{
136  String& append(const String& s);
137  String& append(const String& s, size_type pos, size_type n);
138  String& append(const char* s);
139  String& append(const char* s, size_type n);
140  String& append(size_type n, char c);
141  String& append(iterator beg, iterator end);
142  String& append(const_iterator beg, const_iterator end);
143  String& append(reverse_iterator beg, reverse_iterator end);
145 
146  void push_back(char c);
147 
148  String& operator+=(const String& s);
149  String& operator+=(const char* s);
150  String& operator+=(char c);
151  ///@}
152 
153  /// \name insert
154  ///@{
155  iterator insert(iterator pos, const char& s);
156  void insert(iterator pos, size_type n, const char& s);
157  String& insert(size_type n, const String& s);
158  String& insert(size_type pos, const String& s, size_type pos1, size_type n);
159  String& insert(size_type pos, const char* s);
160  String& insert(size_type pos, const char* s, size_type n);
161  String& insert(size_type pos, size_type n, char c);
162  void insert(iterator pos, iterator beg, iterator end);
163  void insert(iterator pos, const_iterator beg, const_iterator end);
164  void insert(iterator pos, reverse_iterator beg, reverse_iterator end);
165  void insert(iterator pos, const_reverse_iterator beg, const_reverse_iterator end);
166  ///@}
167 
168  /// \name erase
169  ///@{
170  iterator erase(iterator p);
171  iterator erase(iterator first, iterator last);
172  String& erase(size_type pos = 0, size_type n = npos());
173  ///@}
174 
175  /// \name replace
176  ///@{
177  String& replace(size_type pos, size_type n, const String& s);
178  String& replace(size_type pos, size_type n, const String& s, size_type pos1, size_type n1);
179  String& replace(size_type pos, size_type n, const char* s, size_type n1);
180  String& replace(size_type pos, size_type n, const char* s);
181  String& replace(size_type pos, size_type n, size_type n1, char c);
182  String& replace(iterator first, iterator last, const String& s);
183  String& replace(iterator first, iterator last, const char* s, size_type n);
184  String& replace(iterator first, iterator last, const char* s);
185  String& replace(iterator first, iterator last, size_type n, char c);
186  String& replace(iterator beg1, iterator end1, iterator beg2, iterator end2);
187  String& replace(iterator beg1, iterator end1, const_iterator beg2, const_iterator end2);
188  String& replace(iterator beg1, iterator end1, reverse_iterator beg2, reverse_iterator end2);
189  String& replace(iterator beg1, iterator end1, const_reverse_iterator beg2, const_reverse_iterator end2);
190  ///@}
191 
192  /// \name find
193  /// forward/reverse
194  ///@{
195  size_type find(const String& s, size_type pos = 0) const;
196  size_type find(const char* s, size_type pos, size_type n) const;
197  size_type find(const char* s, size_type pos = 0) const;
198  size_type find(char c, size_type pos = 0) const;
199  size_type rfind(const String& s, size_type pos = npos()) const;
200  size_type rfind(const char* s, size_type pos, size_type n) const;
201  size_type rfind(const char* s, size_type pos = npos()) const;
202  size_type rfind(char c, size_type pos = npos()) const;
203  size_type find_first_of(const String& s, size_type pos = 0) const;
204  size_type find_first_of(const char* s, size_type pos, size_type n) const;
205  size_type find_first_of(const char* s, size_type pos = 0) const;
206  size_type find_first_of(char c, size_type pos = 0) const;
207  size_type find_first_not_of(const String& s, size_type pos = 0) const;
208  size_type find_first_not_of(const char* s, size_type pos, size_type n) const;
209  size_type find_first_not_of(const char* s, size_type pos = 0) const;
210  size_type find_first_not_of(char c, size_type pos = 0) const;
211  size_type find_last_of(const String& s, size_type pos = npos()) const;
212  size_type find_last_of(const char* s, size_type pos, size_type n) const;
213  size_type find_last_of(const char* s, size_type pos = npos()) const;
214  size_type find_last_of(char c, size_type pos = npos()) const;
215  size_type find_last_not_of(const String& s, size_type pos = npos()) const;
216  size_type find_last_not_of(const char* s, size_type pos, size_type n) const;
217  size_type find_last_not_of(const char* s, size_type pos = npos()) const;
218  size_type find_last_not_of(char c, size_type pos = npos()) const;
219  ///@}
220 
221  /// \name substring
222  ///@{
223  String substr(size_type pos = 0, size_type n = npos()) const;
224  ///@}
225 
226  /// \name copy to buffer
227  ///@{
228  size_type copy(char* buf, size_type n, size_type pos = 0) const;
229  ///@}
230 
231  /// \name 3-way comparison
232  ///@{
233  int compare(const String& s) const;
234  int compare(size_type pos, size_type n, const String& s) const;
235  int compare(size_type pos, size_type n, const String& s, size_type pos1, size_type n1) const;
236  int compare(const char* s) const;
237  int compare(size_type pos, size_type n, const char* s, size_type len = npos()) const;
238  ///@}
239 
240  /**
241  \class Iterator
242  \tparam Const constant-ness
243  \tparam Forward forward/reverse traversal
244 
245  \brief A random access iterator class for String
246 
247  \author wk
248  \date 02.02.2011
249  */
250  template<bool Const, bool Forward>
251  class Iterator
252  {
253  using Char = typename std::conditional<Const, const char, char>::type;
254  class Impl;
255  Impl *pimpl_;
256 
257  public:
258  using iterator_category = std::random_access_iterator_tag;
259  using value_type = char;
260  using difference_type = String::difference_type;
261  using reference = Char&;
262  using pointer = Char*;
264 
265  Iterator();
266  Iterator(const Iterator& other);
267  operator ConstIterator() const;
268  ~Iterator();
269  Iterator &operator= (const Iterator& that);
270  reference operator* () const;
271  pointer operator->() const;
272  Iterator& operator++();
273  Iterator operator++(int);
274  Iterator& operator--();
275  Iterator operator--(int);
276  Iterator& operator+=(difference_type offset);
277  Iterator operator+ (difference_type offset) const;
278  Iterator& operator-=(difference_type offset);
279  Iterator operator- (difference_type offset) const;
280  difference_type operator-(const Iterator<true, Forward>& that) const;
281  difference_type operator-(const Iterator<false, Forward>& that) const;
282  reference operator[](difference_type offset) const;
283 
284  bool operator==(const Iterator<true, Forward>& that) const;
285  bool operator==(const Iterator<false, Forward>& that) const;
286  bool operator!=(const Iterator<true, Forward>& that) const;
287  bool operator!=(const Iterator<false, Forward>& that) const;
288  bool operator< (const Iterator<true, Forward>& that) const;
289  bool operator< (const Iterator<false, Forward>& that) const;
290  bool operator> (const Iterator<true, Forward>& that) const;
291  bool operator> (const Iterator<false, Forward>& that) const;
292  bool operator<=(const Iterator<true, Forward>& that) const;
293  bool operator<=(const Iterator<false, Forward>& that) const;
294  bool operator>=(const Iterator<true, Forward>& that) const;
295  bool operator>=(const Iterator<false, Forward>& that) const;
296  };
297 
298  };
299 
300  /// \relatesalso String
301  OPALS_API String operator+(const String& s1, const String& s2);
302  /// \relatesalso String
303  OPALS_API String operator+(const char* s1, const String& s2);
304  /// \relatesalso String
305  OPALS_API String operator+(const String& s1, const char* s2);
306  /// \relatesalso String
307  OPALS_API String operator+(char c, const String& s2);
308  /// \relatesalso String
309  OPALS_API String operator+(const String& s1, char c);
310 
311  /// \relatesalso String
312  OPALS_API bool operator==(const String& s1, const String& s2);
313  /// \relatesalso String
314  OPALS_API bool operator==(const char* s1, const String& s2);
315  /// \relatesalso String
316  OPALS_API bool operator==(const String& s1, const char* s2);
317 
318  /// \relatesalso String
319  OPALS_API bool operator!=(const String& s1, const String& s2);
320  /// \relatesalso String
321  OPALS_API bool operator!=(const char* s1, const String& s2);
322  /// \relatesalso String
323  OPALS_API bool operator!=(const String& s1, const char* s2);
324 
325  /// \relatesalso String
326  OPALS_API bool operator<(const String& s1, const String& s2);
327  /// \relatesalso String
328  OPALS_API bool operator<(const char* s1, const String& s2);
329  /// \relatesalso String
330  OPALS_API bool operator<(const String& s1, const char* s2);
331 
332  /// \relatesalso String
333  OPALS_API void swap(String& s1, String& s2);
334 
335 }
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