Main Page Modules Namespace List Class Hierarchy Alphabetical List Compound List File List Namespace Members Compound Members File Members Related Pages

# deString.hpp

Go to the documentation of this file.

    00001 ///////////////////////////////////////////////////////////////////////////////
    00002 /// @file deString.hpp
    00003 ///
    00004 /// @brief object-oriented string manipulation class
    00005 ///
    00006 /// @author Assassin
    00007 ///
    00008 /// This file is the intellectual property of Novus Delta, LLC.. Usage of the
    00009 /// contents of this file is subject to the Destiny3D Member License which
    00010 /// can be found at http://www.destiny3d.com.  Any other usage is prohibited.
    00011 ///
    00012 /// This file is distributed "AS IS" without warranty of any kind.  Novus
    00013 /// Delta, LLC. does not guarantee the fitness of the contents of this file
    00014 /// for any particular purpose.
    00015 ///
    00016 /// Copyright (C) 2001-2003 Novus Delta, LLC. All Rights Reserved.
    00017 ///
    00018 /// <hr>
    00019 ///                                 Change History
    00020 /// <hr>
    00021 ///
    00022 /// @date Mar 2002
    00023 /// @author Assassin
    00024 /// @remarks Creation
    00025 ///
    00026 ///////////////////////////////////////////////////////////////////////////////
    00027
    00028 #ifndef DESTRING_HPP
    00029 #define DESTRING_HPP
    00030
    00031 #include "deGlobalTypes.hpp"
    00032 #include "deArray.hpp"
    00033
    00034 #if defined(DEUTIL_DLL_EXPORTS) || defined(DESTINY3D_EXPORT_ALL)
    00035 #   define DESTRING_API DEDLL_EXPORT
    00036 #elif defined(DESTINY3D_STATIC_LINK)
    00037 #   define DESTRING_API
    00038 #else
    00039 #   define DESTRING_API DEDLL_IMPORT
    00040 #endif
    00041
    00042 #ifdef USING_DESTINY3D
    00043 #ifdef _DEBUG
    00044 #   ifdef DESTINY3D_STATIC_LINK
    00045 #       pragma comment(lib, "deUtil_sd")
    00046 #   else
    00047 #       pragma comment(lib, "deUtild")
    00048 #   endif //DESTINY3D_STATIC_LINK
    00049 #else
    00050 #   ifdef DESTINY3D_STATIC_LINK
    00051 #       pragma comment(lib, "deUtil_s")
    00052 #   else
    00053 #       pragma comment(lib, "deUtil")
    00054 #   endif //DESTINY3D_STATIC_LINK
    00055 #endif //_DEBUG
    00056 #endif //USING_DESTINY3D
    00057
    00058 /// string utility class.
    00059 /// deString2 offers better performance for string-intensive operations involving copies.
    00060 class DESTRING_API deString
    00061 {
    00062 public:
    00063     deString();
    00064     deString(const char * rhs);
    00065     deString(const deString &rhs );
    00066     ~deString();
    00067
    00068     long inline Length() const { return m_Length; }
    00069     long inline size() const { return m_Length; }
    00070
    00071     deString operator+(const deString & rhs) const;
    00072     deString operator+(const char * rhs) const;
    00073
    00074     const deString& operator+=(const deString & rhs);
    00075     const deString& operator+=(const char * rhs);
    00076     const deString& operator+=(const char rhs);
    00077
    00078     const deString & operator=(const deString & rhs);
    00079     const deString & operator=(const char * rhs);
    00080
    00081     void SetCaseSensitivity(deBoolean Sensitive);
    00082     deBoolean operator==(const deString & rhs) const;
    00083     deBoolean operator==(const char * rhs) const;
    00084     deBoolean operator!=(const deString & rhs) const;
    00085     deBoolean operator!=(const char * rhs) const;
    00086
    00087     char operator[](long index) const;
    00088     char& operator[](long index);
    00089
    00090     void operator << (char c);              ///< concatenate a single character (signed)
    00091     void operator << (long l);              ///< concatenate an integer (signed)
    00092     void operator << (unsigned long ul);    ///< concatenate an integer (unsigned)
    00093     void operator << (int i) {operator<<((long)i);}
    00094     void operator << (unsigned int ui) {operator<<((unsigned long)ui);}
    00095     void operator << (s64 i);
    00096     void operator << (u64 ui);
    00097     void operator << (float f);             ///< concatenate a fp number (float)
    00098     void operator << (double d);            ///< concatenate a fp number (double)
    00099     void operator << (const char * str);    ///< concatenate a string (char*)
    00100     void operator << (const deString& str); ///< concatenate a string (deString)
    00101
    00102     int Format(const char * format, ...);
    00103
    00104     int Find(char ToFind, deBoolean FindLast);
    00105     void Replace(char Find, char Replace);
    00106     void Split(char Splitter, deTArray <deString> &ResultStrings);
    00107     deBoolean Strip(const deString &Stripped, deString &Result);
    00108     deString SubString(int start, int stop); // half-closed range: [start,stop)
    00109
    00110     const char * const_str() const { return m_String; }
    00111     char * c_str() { return m_String; }
    00112     char * Reserve(int Length);
    00113     deBoolean Trim(int Length = -1);
    00114
    00115 #ifndef _CRTDBG_MAP_ALLOC
    00116     void *operator new (size_t Size);
    00117     void operator delete (void *p);
    00118     void *operator new (size_t Size, void* buffer);
    00119     void operator delete (void *p, void* buffer);
    00120     void *operator new[] (size_t Size);
    00121     void *operator new[] (size_t Size, int, char *, int);
    00122     void operator delete[] (void *p);
    00123     void operator delete[] (void *p, int, char *, int);
    00124 #endif
    00125
    00126 private:
    00127     long m_Length;
    00128     deBoolean m_CaseSensitive;
    00129 #ifndef DESTRING2
    00130     char * m_String;
    00131     long m_Alloc;
    00132     char * Copy() const;
    00133     void Resize(long newsize);
    00134 #else
    00135     deTArray<char> m_StringArr;
    00136 #endif
    00137 };
    00138
    00139 /// A string class that uses a reference-counted inner class to manage the actual data.
    00140 /// Making unmodified copies of this class will have negligible impact on memory usage,
    00141 /// since they will all point at the same data. A copy-on-write mechanism allocates
    00142 /// space only when needed. Methods marked with [write] will cause a copy to be made
    00143 /// if more than one reference is held on the data.
    00144 class DESTRING_API deString2
    00145 {
    00146 public:
    00147     enum string_flag
    00148     {
    00149         search_forwards = 0,
    00150         search_backwards,
    00151         flag_32bit = 0x7fffffff
    00152     };
    00153 #ifdef _UNICODE
    00154     typedef wchar_t deString_char;
    00155 #else
    00156     typedef char deString_char;
    00157 #endif
    00158 private:
    00159     class StringData
    00160     {
    00161     protected:
    00162         ~StringData()
    00163         {
    00164             delete mString;
    00165         }
    00166     public:
    00167         long mRefs;
    00168         deString_char * mString;
    00169         long mLength;
    00170         long mAlloc;
    00171     public:
    00172         inline StringData() : mRefs(1), mString(0), mLength(0), mAlloc(0) {}
    00173         StringData(const StringData & ref);
    00174         StringData(const deString_char* str);
    00175         StringData(long max_len);
    00176         inline long Claim()
    00177         {
    00178             ++mRefs;
    00179             return mRefs;
    00180         }
    00181         inline long Release()
    00182         {
    00183             --mRefs;
    00184             if (mRefs == 0){
    00185                 delete this;
    00186                 return 0;
    00187             }
    00188             return mRefs;
    00189         }
    00190         StringData* getModString();
    00191     };
    00192     // member data
    00193     StringData* mData;
    00194     deBoolean mCaseSensitive;
    00195     // private methods
    00196     void resizeData(long newlen);
    00197     void OwnBuffer();
    00198     deString_char * c_str(); // temporarily private, until it's needed
    00199     deString2(StringData* ref);
    00200 public:
    00201     deString2();
    00202     deString2(const deString_char * ref);
    00203     deString2(const deString2 &ref);
    00204     ~deString2();
    00205
    00206     void reserve(long max_size);
    00207     void resize(long size);
    00208     /// get the length of the string
    00209     inline long size() const { return mData->mLength; }
    00210     inline void SetCaseSensitivity(deBoolean Sensitive) { mCaseSensitive = Sensitive; }
    00211     /// get a const C-string without creating a copy of the data
    00212     inline const deString_char * const_str() const { return mData->mString; }
    00213
    00214     /// get a character at an offset from the start of the string
    00215     deString_char get_at(long index) const;
    00216     /// [write] set a character at an offset from the start of the string
    00217     void set_at(long index, deString_char ch);
    00218
    00219     /// create a new string containing this string and another string appended
    00220     deString2 operator+(const deString2 & rhs) const;
    00221
    00222     /// [write] append another string onto this one
    00223     void operator+=(const deString2 & rhs);
    00224     /// [write] append another string onto this one
    00225     void operator+=(const deString_char * str);
    00226
    00227     /// [write] assign another string to this one
    00228     void operator=(const deString2 & rhs);
    00229
    00230     /// test equality, using current case-sensitivity setting.
    00231     bool operator==(const deString2 & rhs) const;
    00232     /// test equality, using current case-sensitivity setting.
    00233     bool operator!=(const deString2 & rhs) const;
    00234     /// test equality, using current case-sensitivity setting.
    00235     bool operator==(const deString_char * rhs) const;
    00236     /// test equality, using current case-sensitivity setting.
    00237     bool operator!=(const deString_char * rhs) const;
    00238     /// test equality, using current case-sensitivity setting.
    00239     bool operator<(const deString2 & rhs) const;
    00240     /// test equality, using current case-sensitivity setting.
    00241     bool operator<(const deString_char * rhs) const;
    00242
    00243     // special string operations
    00244     /// [write] replace all instances of 'find' with 'replace'
    00245     void Replace(deString_char find, deString_char replace);
    00246     /// linearly searches for the first instance of 'find', starting at offset 'start_at'
    00247     /// and searching in direction according to 'search_order'
    00248     long Find(deString_char find, long start_at = 0, string_flag search_order = search_forwards) const;
    00249     /// [write] cut the string down to at most length 'newsize', discarding any characters past that size.
    00250     void Trim(long newsize);
    00251     deString2 DropChars(const deString2 &Chars) const;
    00252     /// retrieve a sub-string over the half-open intervace [start,stop)
    00253     deString2 SubString(int start, int stop) const;
    00254     /// [write] convert this string to lowercase
    00255     void ToLower();
    00256     /// [write] convert this string to uppercase
    00257     void ToUpper();
    00258
    00259     u32 Hash() const;
    00260 };
    00261
    00262 #endif