/* * This source file is part of libRocket, the HTML/CSS Interface Middleware * * For the latest information, see http://www.librocket.com * * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. * */ #ifndef ROCKETCORESTRINGBASE_H #define ROCKETCORESTRINGBASE_H #include namespace Rocket { namespace Core { /** Base String Implementation @author Lloyd Weehuizen */ template< typename T > class StringBase { public: typedef size_t size_type; static const size_type npos = (size_type)-1; StringBase(); StringBase(const StringBase& copy); StringBase(const T* string); StringBase(const T* string_start, const T* string_end); StringBase(size_type length, const T character); StringBase(size_type max_length, const T* fmt, ...); ~StringBase(); /// Is the string currently empty inline bool Empty() const; /// Clear the string to empty void Clear(); /// The length of the string inline size_type Length() const; /// Get the hash value of this string inline unsigned int Hash() const; /// Access the string as a standard C string inline const T* CString() const; /// Reserve space for at least this much data inline void Reserve(size_type size); /// Find the given string within this string /// @param find The string to search for /// @param offset Starting location of the search size_type Find(const T* find, size_type offset = 0) const; /// Find the given string within this string /// @param find The string to search for /// @param offset Starting location of the search size_type Find(const StringBase& find, size_type offset = 0) const; /// Reverse find the given string within this string /// @param find The string to search for /// @param offset Starting location of the search size_type RFind(const T* find, size_type offset = npos) const; /// Reverse find the given string within this string /// @param find The string to search for /// @param offset Starting location of the search size_type RFind(const StringBase& find, size_type offset = npos) const; /// Replace all occurances of the given string with another /// @param find The string to search for /// @param replace The string to replace it with StringBase Replace(const T* find, const T* replace) const; /// Replace all occurances of the given string with another /// @param find The string to search for /// @param replace The string to replace it with StringBase Replace(const StringBase& find, const StringBase& replace) const; /// Return a substring of this string /// @param start The starting position /// @param length The number of characters to copy inline StringBase Substring(size_type start, size_type length = StringBase::npos) const; /// Append the given string to this string /// @param append The string to appen /// @param count The number of characters to append inline StringBase& Append(const T* append, size_type count = StringBase::npos); /// Assign the given string to this string /// @param assign The string to assign /// @param count The number of characters to assign inline StringBase& Append(const StringBase& append, size_type count = StringBase::npos); /// Append a single character /// @param append The character to append inline StringBase& Append(const T& append); /// Assign the given string to this string /// @param assign The string to assign /// @param length The number of characters to assign inline StringBase& Assign(const T* assign, size_type count = StringBase::npos); /// Assign the given string to this string /// @param assign The string to assign /// @param count The number of characters to assign inline StringBase& Assign(const T* assign, const T* end); /// Assign the given string to this string /// @param assign The string to assign /// @param count The number of characters to assign inline StringBase& Assign(const StringBase& assign, size_type count = StringBase::npos); /// Insert a string into this string /// @param index Index to insert the characters /// @param insert String to insert /// @param count Number of characters to insert inline void Insert(size_type index, const T* insert, size_type count = StringBase::npos); /// Insert a string into this string /// @param index Index to insert the characters /// @param insert String to insert /// @param count Number of characters to insert inline void Insert(size_type index, const StringBase& insert, size_type count = StringBase::npos); /// Insert a character into this string /// @param index Index to insert the characters /// @param insert Character to insert inline void Insert(size_type index, const T& insert); /// Erase characters from this string /// @param index Index to erase the characters /// @param length Number of characters to erase inline void Erase(size_type index, size_type length = StringBase::npos); /// sprsize_typef style string formatting. /// NOTE: This is not implemented in the base layer and requires template /// specialisation of the specific string type /// @param max_length Maximum length of the result /// @param format The sprsize_typef style formatting int FormatString(size_type max_length, const T* format, ...); /// Resize the string to the given size, inserts space if the string is getting bigger /// @param size New size void Resize(size_type size); /// Create a lowercase version of the string /// @returns The lower case representation of the string StringBase ToLower() const; /// Create a lowercase version of the string /// @returns The lower case representation of the string StringBase ToUpper() const; inline bool operator==(const T* compare) const; inline bool operator==(const StringBase& compare) const; inline bool operator!=(const T* compare) const; inline bool operator!=(const StringBase& compare) const; inline bool operator<(const T* compare) const; inline bool operator<(const StringBase& compare) const; inline StringBase& operator=(const T* assign); inline StringBase& operator=(const StringBase& assign); inline StringBase operator+(const T* append) const; inline StringBase operator+(const StringBase& append) const; inline StringBase& operator+=(const T* append); inline StringBase& operator+=(const StringBase& append); inline StringBase& operator+=(const T& append); inline const T& operator[](size_type index) const; inline T& operator[](size_type index); protected: T* value; size_type buffer_size; size_type length; mutable unsigned int hash; static const size_type LOCAL_BUFFER_SIZE = 8; char local_buffer[LOCAL_BUFFER_SIZE]; size_type GetLength(const T* string) const; // Copies the source string to target string inline void Copy(T* target, const T* src, size_type length, bool terminate = false); // Internal implementations of the public interfaces, // all these functions take the length of the const T*'s they're // dealing with which *MUST* be accurate. // Its up to the external interfaces to provide valid values for these functions inline size_type _Find(const T* find, size_type find_length, size_type offset = 0) const; inline size_type _RFind(const T* find, size_type find_length, size_type offset = 0) const; inline StringBase _Replace(const T* find, size_type find_length, const T* replace, size_type replace_length) const; inline StringBase& _Append(const T* append, size_type append_length, size_type count = StringBase::npos); inline StringBase& _Assign(const T* assign, size_type assign_length, size_type count = StringBase::npos); inline void _Insert(size_type index, const T* insert, size_type insert_length, size_type count = StringBase::npos); }; #include } } #endif