ClanLib  2.3.7
string8.h
Go to the documentation of this file.
1 /*
2 ** ClanLib SDK
3 ** Copyright (c) 1997-2011 The ClanLib Team
4 **
5 ** This software is provided 'as-is', without any express or implied
6 ** warranty. In no event will the authors be held liable for any damages
7 ** arising from the use of this software.
8 **
9 ** Permission is granted to anyone to use this software for any purpose,
10 ** including commercial applications, and to alter it and redistribute it
11 ** freely, subject to the following restrictions:
12 **
13 ** 1. The origin of this software must not be misrepresented; you must not
14 ** claim that you wrote the original software. If you use this software
15 ** in a product, an acknowledgment in the product documentation would be
16 ** appreciated but is not required.
17 ** 2. Altered source versions must be plainly marked as such, and must not be
18 ** misrepresented as being the original software.
19 ** 3. This notice may not be removed or altered from any source distribution.
20 **
21 ** Note: Some of the libraries ClanLib may link to may have additional
22 ** requirements or restrictions.
23 **
24 ** File Author(s):
25 **
26 ** Magnus Norddahl
27 ** Harry Storbacka
28 */
29 
32 
33 #pragma once
34 
35 #include "../api_core.h"
36 #include "string_data8.h"
37 
43 {
44 public:
45  CL_String8();
46 
50  CL_String8(const std::string &source);
51 
55  CL_String8(const CL_String8 &source);
56 
58  CL_String8(const CL_StringData8 &source);
59 
63  CL_String8(const char *c_str);
64 
69  CL_String8(const char *c_str, size_type length);
70 
74  CL_String8(const wchar_t *wc_str);
75 
80  CL_String8(const wchar_t *wc_str, size_type length);
81 
86  CL_String8(size_type n, char c);
87  ~CL_String8();
88 
92  operator CL_StringRef8() const;
93 
94  const char *c_str() const;
95 
99  size_type max_size() const { return (size_type) -1; }
100 
104  size_type capacity() const { return data_capacity; }
105 
106 // void swap(CL_String8 &other);
107 // static swap(CL_String8 &s1, CL_String8 &s2);
108 
110  void clear();
111 
115  void reserve(size_type size);
116 
120  void resize(size_type n);
121 
126  void resize(size_type n, char c);
127 
128 // template <class InputIterator>
129 // CL_String8 &assign(InputIterator first, InputIterator last);
130  CL_String8 &assign(const CL_StringData8 &s);
131 
139  CL_String8 &assign(const CL_StringData8 &s, size_type pos, size_type n);
140 
147  CL_String8 &assign(const char *s, size_type n);
148 
154  CL_String8 &assign(const char *s);
155 
162  CL_String8 &assign(size_type n, char c);
163 
170  iterator insert(iterator pos, const char &item)
171  {
172  size_type insert_pos = (size_type) (pos - begin());
173  size_type n = length();
174  if (insert_pos > n)
175  insert_pos = n;
176  resize(n + 1);
177  char *d = data();
178  for (size_type i = n+1; i > insert_pos; i--)
179  d[i] = d[i-1];
180  d[insert_pos] = item;
181 
182  return begin() + insert_pos;
183  }
184 
190  void insert(iterator pos, size_type num_copies, const char &item);
191 
198  CL_String8 &insert(size_type pos, const CL_StringData8 &s);
199 
208  CL_String8 &insert(size_type pos, const CL_StringData8 &s, size_type pos1, size_type length);
209 
216  CL_String8 &insert(size_type pos, const char *s);
217 
225  CL_String8 &insert(size_type pos, const char *s, size_type s_length);
226 
234  CL_String8 &insert(size_type pos, size_type n, char c);
235 
241  CL_String8 &append(const CL_StringData8 &s);
242 
250  CL_String8 &append(const CL_StringData8 &s, size_type pos, size_type n);
251 
252  CL_String8 &append(const char *s);
253 
260  CL_String8 &append(const char *s, size_type n);
261 
267  CL_String8 &append(const wchar_t *s);
268 
275  CL_String8 &append(const wchar_t *s, size_type n);
276 
283  CL_String8 &append(size_type n, char c);
284 
288  void push_back(char c);
289 
296  {
297  iterator e = end();
298  if (p == e)
299  return e;
300  return erase(p, p+1);
301  }
302 
310  {
311  size_type pos = (size_type) (first - begin());
312  iterator e = end();
313  iterator p1 = first;
314  iterator p2 = last;
315  while (p2 != e)
316  {
317  *p1 = *p2;
318  p1++;
319  p2++;
320  }
321  size_type new_size = (size_type) (p1 - begin());
322  resize(new_size);
323  return begin() + pos;
324  }
325 
332  CL_String8 &erase(size_type pos = 0, size_type n = CL_StringData8::npos);
333 
341  CL_String8 &replace(size_type pos, size_type n, const CL_StringData8 &s);
342 
352  CL_String8 &replace(size_type pos, size_type n, const CL_StringData8 &s, size_type pos1, size_type n1);
353 
362  CL_String8 &replace(size_type pos, size_type n, const char *s, size_type n1);
363 
371  CL_String8 &replace(size_type pos, size_type n, const char *s);
372 
381  CL_String8 &replace(size_type pos, size_type n, size_type n1, char c);
382 
390  CL_String8 &replace(iterator first, iterator last, const CL_StringData8 &s);
391 
400  CL_String8 &replace(iterator first, iterator last, const char *s, size_type n);
401 
409  CL_String8 &replace(iterator first, iterator last, const char *s);
410 
419  CL_String8 &replace(iterator first, iterator last, size_type n, char c);
420 
428  size_type copy(char *buf, size_type n, size_type pos = 0) const;
429 
430  CL_String8 &operator =(const CL_String8 &source);
431  CL_String8 &operator =(const CL_StringData8 &source);
432  CL_String8 &operator =(const char *c_str);
433  CL_String8 &operator =(const wchar_t *c_str);
434  CL_String8 &operator +=(const CL_StringData8 &s);
435  CL_String8 &operator +=(const char *c_str);
436  CL_String8 &operator +=(const wchar_t *c_str);
437  CL_String8 &operator +=(char c);
438 
439 private:
441  void init();
442 
443  size_type data_capacity;
444  enum { local_string_length = 63 };
445  char local_string[local_string_length + 1];
446 };
447 
449 CL_API_CORE CL_String8 operator+(const char *s1, const CL_StringData8 &s2);
450 CL_API_CORE CL_String8 operator+(const wchar_t *s1, const CL_StringData8 &s2);
451 CL_API_CORE CL_String8 operator+(const CL_StringData8 &s1, const char *s2);
452 CL_API_CORE CL_String8 operator+(const CL_StringData8 &s1, const wchar_t *s2);
455