StringBase.h

00001 /*
00002  * This source file is part of libRocket, the HTML/CSS Interface Middleware
00003  *
00004  * For the latest information, see http://www.librocket.com
00005  *
00006  * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
00007  *
00008  * Permission is hereby granted, free of charge, to any person obtaining a copy
00009  * of this software and associated documentation files (the "Software"), to deal
00010  * in the Software without restriction, including without limitation the rights
00011  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00012  * copies of the Software, and to permit persons to whom the Software is
00013  * furnished to do so, subject to the following conditions:
00014  *
00015  * The above copyright notice and this permission notice shall be included in
00016  * all copies or substantial portions of the Software.
00017  * 
00018  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00019  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00020  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00021  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00022  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00023  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00024  * THE SOFTWARE.
00025  *
00026  */
00027 
00028 #ifndef ROCKETCORESTRINGBASE_H
00029 #define ROCKETCORESTRINGBASE_H
00030 
00031 #include <Rocket/Core/Debug.h>
00032 #include <Rocket/Core/StringStorage.h>
00033 //#include <Rocket/Core/Types.h>
00034 
00035 namespace Rocket {
00036 namespace Core {
00037 
00042 template< typename T >
00043 class StringBase
00044 {
00045 public:
00046         typedef size_t size_type;
00047         static const size_type npos = (size_type)-1;
00048 
00049         StringBase();
00050         StringBase(const StringBase& copy);
00051         StringBase(const T* string);
00052         StringBase(const T* string_start, const T* string_end);
00053         StringBase(size_type length, const T character);
00054         StringBase(size_type max_length, const T* fmt, ...);
00055 
00056         ~StringBase();
00057 
00059         inline bool Empty() const;
00061         void Clear();
00062 
00064         inline size_type Length() const;
00066         inline const T* CString() const;
00067 
00071         size_type Find(const T* find, size_type offset = 0) const;
00075         size_type Find(const StringBase<T>& find, size_type offset = 0) const;
00079         size_type RFind(const T* find, size_type offset = npos) const;
00083         size_type RFind(const StringBase<T>& find, size_type offset = npos) const;
00084 
00088         StringBase<T> Replace(const T* find, const T* replace) const;
00092         StringBase<T> Replace(const StringBase<T>& find, const StringBase<T>& replace) const;
00093 
00097         inline StringBase<T> Substring(size_type start, size_type length = StringBase<T>::npos) const;
00098 
00102         inline StringBase<T>& Append(const T* append, size_type count = StringBase<T>::npos);
00106         inline StringBase<T>& Append(const StringBase<T>& append, size_type count = StringBase<T>::npos);
00109         inline StringBase<T>& Append(const T& append);
00110 
00114         inline StringBase<T>& Assign(const T* assign, size_type count = StringBase<T>::npos);
00118         inline StringBase<T>& Assign(const T* assign, const T* end);
00122         inline StringBase<T>& Assign(const StringBase<T>& assign, size_type count = StringBase<T>::npos);
00123 
00128         inline void Insert(size_type index, const T* insert, size_type count = StringBase<T>::npos);
00133         inline void Insert(size_type index, const StringBase<T>& insert, size_type count = StringBase<T>::npos);
00137         inline void Insert(size_type index, const T& insert);
00138 
00142         inline void Erase(size_type index, size_type length = StringBase<T>::npos);
00143 
00149         int FormatString(size_type max_length, const T* format, ...);
00150 
00153         void Resize(size_type size);
00154 
00157         StringBase<T> ToLower() const;
00160         StringBase<T> ToUpper() const;
00161 
00162         inline bool operator==(const T* compare) const;
00163         inline bool operator==(const StringBase<T>& compare) const;
00164 
00165         inline bool operator!=(const T* compare) const;
00166         inline bool operator!=(const StringBase<T>& compare) const;
00167 
00168         inline bool operator<(const T* compare) const;
00169         inline bool operator<(const StringBase<T>& compare) const;
00170 
00171         inline StringBase<T>& operator=(const T* assign);
00172         inline StringBase<T>& operator=(const StringBase<T>& assign);
00173 
00174         inline StringBase<T> operator+(const T* append) const;
00175         inline StringBase<T> operator+(const StringBase<T>& append) const;
00176 
00177         inline StringBase<T>& operator+=(const T* append);
00178         inline StringBase<T>& operator+=(const StringBase<T>& append);
00179         inline StringBase<T>& operator+=(const T& append);
00180 
00181         inline const T& operator[](size_type index) const;
00182         inline T& operator[](size_type index);
00183 
00184 protected:      
00185 
00186         mutable T* value;
00187         size_type length;
00188 
00189         mutable StringStorage::StringID string_id;
00190 
00191         size_type GetLength(const T* string) const;
00192 
00193         // Ensure the string is now in the storage
00194         inline void AddStorage() const;
00195         // Modify this string, moving it out of storage if necessary
00196         // By default, never shrink the memory allocation
00197         inline void Modify(size_type new_size, bool shrink = false);
00198         // Copies the source string to target string
00199         inline void Copy(T* target, const T* src, size_type length, bool terminate = false) const;
00200         // Release the string
00201         inline void Release() const;
00202 
00203         // Internal implementations of the public interfaces,
00204         // all these functions take the length of the const T*'s they're
00205         // dealing with which *MUST* be accurate.
00206         // Its up to the external interfaces to provide valid values for these functions
00207         inline size_type _Find(const T* find, size_type find_length, size_type offset = 0) const;
00208         inline size_type _RFind(const T* find, size_type find_length, size_type offset = 0) const;
00209         inline StringBase<T> _Replace(const T* find, size_type find_length, const T* replace, size_type replace_length) const;
00210         inline StringBase<T>& _Append(const T* append, size_type append_length, size_type count = StringBase<T>::npos);
00211         inline StringBase<T>& _Assign(const T* assign, size_type assign_length, size_type count = StringBase<T>::npos);
00212         inline void _Insert(size_type index, const T* insert, size_type insert_length, size_type count = StringBase<T>::npos);
00213 };
00214 
00215 #include <Rocket/Core/StringBase.inl>
00216 
00217 }
00218 }
00219 
00220 #endif