00001
00002 #ifndef SUBSTRING_H
00003 #define SUBSTRING_H
00004
00005 #include <stdexcept>
00006
00031 #include "RCObject.h"
00032 #include "RCPtr.h"
00033
00034 class String;
00035
00160 class SubString
00161 {
00162 private:
00163
00164 class SharedData : public RCObject
00165 {
00166 private:
00167 String *pStr_;
00168 size_t start_;
00169 size_t subStrLen_;
00170
00171 private:
00172 void copy(const SharedData &rhs);
00173
00174 public:
00175 SharedData();
00176 SharedData(const SharedData &rhs) { copy( rhs ); }
00177 ~SharedData() {}
00178
00179 void pStr( String *strRef ) { pStr_ = strRef; }
00180 String *pStr() { return pStr_; }
00181
00182 void start(int start) { start_ = static_cast<size_t>(start); }
00183 size_t start() { return start_; }
00184
00185 void subStrLen(int val) { subStrLen_ = static_cast<size_t>(val); }
00186 size_t subStrLen() { return subStrLen_; }
00187
00188 size_t getRefCnt() { return refCnt(); }
00189 };
00190
00191 RCPtr<SharedData> value;
00192
00193 void rangeCheck(String &s, const size_t start, const size_t len)
00194 throw(std::out_of_range);
00195 int compareTo( const char *CStr );
00196 int compareTo( String &s );
00197 int compareTo( SubString &subStr );
00198 void createCopySpace(size_t start, size_t charsToCopy);
00199 String string() const;
00200
00201 public:
00202
00203 SubString(String &s, const size_t start, const size_t len)
00204 throw(std::out_of_range);
00205
00206 operator String() const;
00207
00208 size_t getRefCnt() { return value->getRefCnt(); }
00209
00210 size_t length() { return value->subStrLen(); }
00211
00212 String operator +(const char *Cstr);
00213 String operator +(const SubString& sub);
00214 String operator +(const String &str);
00215
00216 String operator =(const char *CStr );
00217 String operator =(String& str );
00218 String operator =(const SubString& sub );
00219
00220
00221 bool operator ==(const char *Cstr ) { return (compareTo( Cstr ) == 0); }
00222 bool operator ==(String &s ) { return (compareTo( s ) == 0); }
00223 bool operator ==(SubString subStr) { return (compareTo(subStr) == 0); }
00224
00225 bool operator !=(const char *Cstr ) { return (compareTo( Cstr ) != 0); }
00226 bool operator !=(String &s ) { return (compareTo( s ) != 0); }
00227 bool operator !=(SubString subStr) { return (compareTo(subStr) != 0); }
00228
00229 bool operator <=(const char *Cstr ) { return (compareTo( Cstr ) <= 0); }
00230 bool operator <=(String &s ) { return (compareTo( s ) <= 0); }
00231 bool operator <=(SubString subStr) { return (compareTo(subStr) <= 0); }
00232
00233 bool operator >=(const char *Cstr ) { return (compareTo( Cstr ) >= 0); }
00234 bool operator >=(String &s ) { return (compareTo( s ) >= 0); }
00235 bool operator >=(SubString subStr) { return (compareTo(subStr) >= 0); }
00236
00237 bool operator <(const char *Cstr ) { return (compareTo( Cstr ) < 0); }
00238 bool operator <(String &s ) { return (compareTo( s ) < 0); }
00239 bool operator <(SubString subStr) { return (compareTo(subStr) < 0); }
00240
00241 bool operator >(const char *Cstr ) { return (compareTo( Cstr ) > 0); }
00242 bool operator >(String &s ) { return (compareTo( s ) > 0); }
00243 bool operator >(SubString subStr) { return (compareTo(subStr) > 0); }
00244 };
00245
00246
00247
00248
00249 bool operator ==(const char *Cstr, SubString s);
00250 bool operator !=(const char *Cstr, SubString s);
00251 bool operator <=(const char *Cstr, SubString s);
00252 bool operator >=(const char *Cstr, SubString s);
00253 bool operator <(const char *Cstr, SubString s);
00254 bool operator >(const char *Cstr, SubString s);
00255
00256
00260 inline
00261 SubString::SharedData::SharedData()
00262 {
00263 pStr_ = 0;
00264 start_ = 0;
00265 subStrLen_ = 0;
00266 }
00267
00268 inline
00269 void SubString::SharedData::copy(const SharedData &rhs)
00270 {
00271 pStr_ = rhs.pStr_;
00272 start_ = rhs.start_;
00273 subStrLen_ = subStrLen_;
00274 }
00275
00276
00277 #include "String.h"
00278
00279 #endif
00280