home *** CD-ROM | disk | FTP | other *** search
/ Game.EXE 2002 May / Game.EXE_05_2002.iso / Alawar / Lib / String.hpp < prev   
Encoding:
C/C++ Source or Header  |  2002-04-04  |  2.5 KB  |  137 lines

  1. #ifndef STRING_H
  2. #define STRING_H
  3.  
  4. #include <string>
  5.  
  6. class String
  7. {
  8.     std::string data;
  9. public:
  10.     String()
  11.     {}
  12.     String(int n, char c)
  13.     :    data( n, c )
  14.     {}
  15.     String(const char * str)
  16.     :    data( str )
  17.     {}
  18.     String(const char * begin, const char * end)
  19.     :    data( begin, end )
  20.     {}
  21.     String(const String & str)
  22.     :    data( str.data )
  23.     {}
  24.     const String & operator=(const char * str)
  25.     {
  26.         data = str;
  27.         return *this;
  28.     }
  29.     const String & operator=(const String & str)
  30.     {
  31.         data = str.data;
  32.         return *this;
  33.     }
  34. /*    operator const char *()const
  35.     {
  36.         return data.c_str();
  37.     }*/
  38.     const char * c_str()const
  39.     {
  40.         return data.c_str();
  41.     }
  42.     unsigned size()const
  43.     {
  44.         return data.size();
  45.     }
  46.     void resize(unsigned new_size)
  47.     {
  48.         data.resize( new_size );
  49.     }
  50.     char operator[]( unsigned pos )const
  51.     {
  52.         if( pos >= data.size() ) // Åα«óÑα¬á «Γ¡¿¼áÑΓ óαѼ∩, ¡« ñѽáÑΓ ¬½áßß ¡áñѪ¡δ¼
  53.             return 0;
  54.         return data[ pos ];
  55.     }
  56.     bool empty()const
  57.     {
  58.         return data.size() == 0;
  59.     }
  60.     const String & operator+=(char c)
  61.     {
  62.         data += c;
  63.         return *this;
  64.     }
  65.     const String & operator+=(const char * str)
  66.     {
  67.         data += str;
  68.         return *this;
  69.     }
  70.     const String & operator+=(const String & str)
  71.     {
  72.         data += str.data;
  73.         return *this;
  74.     }
  75.     int compare(const char * str)const
  76.     {
  77.         return data.compare( str );
  78.     }
  79.     int compare(const String & str)const
  80.     {
  81.         return data.compare( str.data );
  82.     }
  83. };
  84.  
  85. inline bool operator==(const String & a, const String & b)
  86. {
  87.     return a.compare( b ) == 0;
  88. }
  89. inline bool operator==(const char * a, const String & b)
  90. {
  91.     return b.compare( a ) == 0;
  92. }
  93. inline bool operator==(const String & a, const char * b)
  94. {
  95.     return a.compare( b ) == 0;
  96. }
  97.  
  98. inline bool operator!=(const String & a, const String & b)
  99. {
  100.     return !operator==( a, b );
  101. }
  102. inline bool operator!=(const char * a, const String & b)
  103. {
  104.     return !operator==( a, b );
  105. }
  106. inline bool operator!=(const String & a, const char * b)
  107. {
  108.     return !operator==( a, b );
  109. }
  110.  
  111. inline bool operator<(const String & a, const String & b)
  112. {
  113.     return a.compare( b ) < 0;
  114. }
  115. inline bool operator<(const char * a, const String & b)
  116. {
  117.     return b.compare( a ) > 0;
  118. }
  119. inline bool operator<(const String & a, const char * b)
  120. {
  121.     return a.compare( b ) < 0;
  122. }
  123.  
  124. inline String operator+(const String & a, const String & b)
  125. {
  126.     return String(a) += b;
  127. }
  128. inline String operator+(const String & a, const char * b)
  129. {
  130.     return String(a) += b;
  131. }
  132. inline String operator+(const String & a, char b)
  133. {
  134.     return String(a) += b;
  135. }
  136.  
  137. #endif //STRING_H