Simple Application Framework  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
Saf/Text/String.h
Go to the documentation of this file.
00001 /*
00002     This file is part of Simple Application Framework (Saf) library.
00003     Copyright (C) 2010 - 2012 Ondrej Danek <ondrej.danek@gmail.com>
00004 
00005     This library is free software: you can redistribute it and/or modify
00006     it under the terms of the GNU General Public License as published 
00007     by the Free Software Foundation, either version 3 of the License, or
00008     (at your option) any later version.
00009 
00010     Saf is distributed in the hope that it will be useful,
00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013     GNU General Public License for more details.
00014 
00015     You should have received a copy of the GNU General Public License
00016     along with Simple Application Framework library. If not, 
00017     see <http://www.gnu.org/licenses/>.
00018 */
00019 
00028 #ifndef SAF_TEXT_STRING_H
00029 #define SAF_TEXT_STRING_H
00030 
00031 #include "../Core.h"
00032 #include "../Mem/Alloc.h"
00033 #include "Char.h"
00034 #include "../Algo/Swap.h"
00035 
00036 namespace Saf
00037 {
00038     namespace Text
00039     {
00046         class SAF_DLL_EXPORT String
00047         {
00048         private:
00050             Char *m_data;
00052             Size *m_refCounter;
00054             Size m_codePoints;
00055 
00056         protected:
00058             void Resize(Size sz);
00060             void Free();
00061 
00062         public:
00064             String()
00065                 : m_data(nullptr), m_refCounter(nullptr), m_codePoints(0)
00066             {}
00067 
00069             String(Size count, const Char &c);
00070 
00072             String(const char *str);
00073 
00075             String(const String &str) 
00076                 : m_data(nullptr), m_refCounter(nullptr), m_codePoints(0)
00077             {
00078                 *this = str;
00079             }
00080 
00082             ~String()
00083             {
00084                 Free();
00085             }
00086 
00088             String& operator=(const String &str);
00089 
00095             void Swap(String& s)
00096             {
00097                 if (this != &s)
00098                 {
00099                     Algo::Swap(m_data, s.m_data);
00100                     Algo::Swap(m_refCounter, s.m_refCounter);
00101                     Algo::Swap(m_codePoints, s.m_codePoints);
00102                 }
00103             }
00104 
00106             Size Elements() const 
00107             { 
00108                 return CodePoints();
00109             }
00110 
00112             Size CodePoints() const
00113             {
00114                 return m_codePoints;
00115             }
00116 
00118             String Upper() const;
00119 
00121             String Lower() const;
00122 
00124             String Trim(bool left, bool right) const;
00125 
00127             String Pad(const Char &ch, Size left, Size right) const;
00128 
00130             String Mid(Size pos, Size sz) const;
00131 
00136             String Left(Size sz) const
00137             {
00138                 return Mid(0, sz);
00139             }
00140 
00145             String Right(Size sz) const
00146             {
00147                 return Mid(Elements() - sz, sz);
00148             }
00149 
00151             String Reverse() const;
00152 
00154             bool IsEmpty() const 
00155             { 
00156                 return (Elements() == 0);
00157             }
00158 
00160             String operator+(const String &str) const;
00161 
00163             bool operator<(const String &str) const;
00164 
00166             bool operator==(const String &str) const;
00167 
00169             Char operator[](Size n) const 
00170             { 
00171                 return m_data[n]; 
00172             }
00173         };
00174     }
00175 
00177     namespace Algo
00178     {
00179         // Swap specialization for strings.
00180         template <>
00181         struct SwapFunc<Text::String>
00182         {
00183             void operator()(Text::String& x, Text::String& y)
00184             {
00185                 x.Swap(y);
00186             }
00187         };
00188     }
00190 }
00191 
00192 #endif