string8.h

Go to the documentation of this file.
00001 /*
00002 **  ClanLib SDK
00003 **  Copyright (c) 1997-2011 The ClanLib Team
00004 **
00005 **  This software is provided 'as-is', without any express or implied
00006 **  warranty.  In no event will the authors be held liable for any damages
00007 **  arising from the use of this software.
00008 **
00009 **  Permission is granted to anyone to use this software for any purpose,
00010 **  including commercial applications, and to alter it and redistribute it
00011 **  freely, subject to the following restrictions:
00012 **
00013 **  1. The origin of this software must not be misrepresented; you must not
00014 **     claim that you wrote the original software. If you use this software
00015 **     in a product, an acknowledgment in the product documentation would be
00016 **     appreciated but is not required.
00017 **  2. Altered source versions must be plainly marked as such, and must not be
00018 **     misrepresented as being the original software.
00019 **  3. This notice may not be removed or altered from any source distribution.
00020 **
00021 **  Note: Some of the libraries ClanLib may link to may have additional
00022 **  requirements or restrictions.
00023 **
00024 **  File Author(s):
00025 **
00026 **    Magnus Norddahl
00027 **    Harry Storbacka
00028 */
00029 
00032 
00033 #pragma once
00034 
00035 #include "../api_core.h"
00036 #include "string_data8.h"
00037 
00042 class CL_API_CORE CL_String8 : public CL_StringData8
00043 {
00044 public:
00045         CL_String8();
00046 
00050         CL_String8(const std::string &source);
00051 
00055         CL_String8(const CL_String8 &source);
00056 
00058         CL_String8(const CL_StringData8 &source);
00059 
00063         CL_String8(const char *c_str);
00064 
00069         CL_String8(const char *c_str, size_type length);
00070 
00074         CL_String8(const wchar_t *wc_str);
00075 
00080         CL_String8(const wchar_t *wc_str, size_type length);
00081 
00086         CL_String8(size_type n, char c);
00087         ~CL_String8();
00088 
00092         operator CL_StringRef8() const;
00093 
00094         const char *c_str() const;
00095 
00099         size_type max_size() const { return (size_type) -1; }
00100 
00104         size_type capacity() const { return data_capacity; }
00105 
00106 //      void swap(CL_String8 &other);
00107 //      static swap(CL_String8 &s1, CL_String8 &s2);
00108 
00110         void clear();
00111 
00115         void reserve(size_type size);
00116 
00120         void resize(size_type n);
00121 
00126         void resize(size_type n, char c);
00127 
00128 //      template <class InputIterator>
00129 //      CL_String8 &assign(InputIterator first, InputIterator last);
00130         CL_String8 &assign(const CL_StringData8 &s);
00131 
00139         CL_String8 &assign(const CL_StringData8 &s, size_type pos, size_type n);
00140 
00147         CL_String8 &assign(const char *s, size_type n);
00148 
00154         CL_String8 &assign(const char *s);
00155 
00162         CL_String8 &assign(size_type n, char c);
00163 
00170         iterator insert(iterator pos, const char &item)
00171         {
00172                 size_type insert_pos = (size_type) (pos - begin());
00173                 size_type n = length();
00174                 if (insert_pos > n)
00175                         insert_pos = n;
00176                 resize(n + 1);
00177                 char *d = data();
00178                 for (size_type i = n+1; i > insert_pos; i--)
00179                         d[i] = d[i-1];
00180                 d[insert_pos] = item;
00181 
00182                 return begin() + insert_pos;
00183         }
00184 
00190         void insert(iterator pos, size_type num_copies, const char &item);
00191 
00198         CL_String8 &insert(size_type pos, const CL_StringData8 &s);
00199 
00208         CL_String8 &insert(size_type pos, const CL_StringData8 &s, size_type pos1, size_type length);
00209 
00216         CL_String8 &insert(size_type pos, const char *s);
00217 
00225         CL_String8 &insert(size_type pos, const char *s, size_type s_length);
00226 
00234         CL_String8 &insert(size_type pos, size_type n, char c);
00235 
00241         CL_String8 &append(const CL_StringData8 &s);
00242 
00250         CL_String8 &append(const CL_StringData8 &s, size_type pos, size_type n);
00251 
00252         CL_String8 &append(const char *s);
00253 
00260         CL_String8 &append(const char *s, size_type n);
00261 
00267         CL_String8 &append(const wchar_t *s);
00268 
00275         CL_String8 &append(const wchar_t *s, size_type n);
00276 
00283         CL_String8 &append(size_type n, char c);
00284 
00288         void push_back(char c);
00289 
00295         iterator erase(iterator p)
00296         {
00297                 iterator e = end();
00298                 if (p == e)
00299                         return e;
00300                 return erase(p, p+1);
00301         }
00302 
00309         iterator erase(iterator first, iterator last)
00310         {
00311                 size_type pos = (size_type) (first - begin());
00312                 iterator e = end();
00313                 iterator p1 = first;
00314                 iterator p2 = last;
00315                 while (p2 != e)
00316                 {
00317                         *p1 = *p2;
00318                         p1++;
00319                         p2++;
00320                 }
00321                 size_type new_size = (size_type) (p1 - begin());
00322                 resize(new_size);
00323                 return begin() + pos;
00324         }
00325 
00332         CL_String8 &erase(size_type pos = 0, size_type n = CL_StringData8::npos);
00333 
00341         CL_String8 &replace(size_type pos, size_type n, const CL_StringData8 &s);
00342 
00352         CL_String8 &replace(size_type pos, size_type n, const CL_StringData8 &s, size_type pos1, size_type n1);
00353 
00362         CL_String8 &replace(size_type pos, size_type n, const char *s, size_type n1);
00363 
00371         CL_String8 &replace(size_type pos, size_type n, const char *s);
00372 
00381         CL_String8 &replace(size_type pos, size_type n, size_type n1, char c);
00382 
00390         CL_String8 &replace(iterator first, iterator last, const CL_StringData8 &s);
00391 
00400         CL_String8 &replace(iterator first, iterator last, const char *s, size_type n);
00401 
00409         CL_String8 &replace(iterator first, iterator last, const char *s);
00410 
00419         CL_String8 &replace(iterator first, iterator last, size_type n, char c);
00420 
00428         size_type copy(char *buf, size_type n, size_type pos = 0) const;
00429 
00430         CL_String8 &operator =(const CL_String8 &source);
00431         CL_String8 &operator =(const CL_StringData8 &source);
00432         CL_String8 &operator =(const char *c_str);
00433         CL_String8 &operator =(const wchar_t *c_str);
00434         CL_String8 &operator +=(const CL_StringData8 &s);
00435         CL_String8 &operator +=(const char *c_str);
00436         CL_String8 &operator +=(const wchar_t *c_str);
00437         CL_String8 &operator +=(char c);
00438 
00439 private:
00441         void init();
00442 
00443         size_type data_capacity;
00444         enum { local_string_length = 63 };
00445         char local_string[local_string_length + 1];
00446 };
00447 
00448 CL_API_CORE CL_String8 operator+(const CL_StringData8 &s1, const CL_StringData8 &s2);
00449 CL_API_CORE CL_String8 operator+(const char *s1, const CL_StringData8 &s2);
00450 CL_API_CORE CL_String8 operator+(const wchar_t *s1, const CL_StringData8 &s2);
00451 CL_API_CORE CL_String8 operator+(const CL_StringData8 &s1, const char *s2);
00452 CL_API_CORE CL_String8 operator+(const CL_StringData8 &s1, const wchar_t *s2);
00453 CL_API_CORE CL_String8 operator+(char c, const CL_StringData8 &s2);
00454 CL_API_CORE CL_String8 operator+(const CL_StringData8 &s1, char c);
00455