00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #ifndef __AVR_CPP_BASE_STRING_H__
00029 #define __AVR_CPP_BASE_STRING_H__
00030
00031 #include "StrDefs.h"
00032
00033 namespace AVRCpp
00034 {
00035 namespace Collection
00036 {
00037
00038 class BaseString
00039 {
00040 protected:
00041
00043 str_t me;
00044
00046 uint16_t length;
00047
00049 uint16_t size;
00050
00056 virtual bool EnsureCapacity(uint16_t required) = 0;
00057
00058
00059 bool SubJoin(cstr_t joinable, uint16_t joinableLength);
00060 bool SubJoin(cstr_t joinable, uint16_t n, uint16_t joinableLength);
00061 bool SubCopy(cstr_t source, uint16_t sourceLength);
00062 bool SubReplace(uint16_t begin, uint16_t end, cstr_t replaceWith, uint16_t replaceWithLength);
00063 uint16_t SubFind(uint16_t index, cstr_t value, uint16_t valueLength) const;
00064 uint16_t SubIFind(uint16_t index, cstr_t value, uint16_t valueLength) const;
00065
00066 template <uint8_t radix, uint8_t requiredSpace> bool SubFromInt(int16_t i)
00067 {
00068 if (!EnsureCapacity(requiredSpace) )
00069 return false;
00070
00071 ToString(i, me, radix);
00072
00073 length = StrLength(me);
00074 return true;
00075
00076 }
00077
00078 public:
00079
00081 enum FindResult
00082 {
00083 NotFound = 0xFFFF
00084
00085 };
00086
00087 BaseString() : me(NULL), length(0), size(0) {}
00088 virtual ~BaseString() {}
00089
00093 inline uint16_t GetLength() const { return length; }
00094
00099 inline cstr_t Get() const { return me != NULL ? me : STR_NULL; }
00100
00105 inline cstr_t Get(uint16_t index) const { return (index < length) ? me + index : STR_NULL; }
00106
00111 bool SetLength(uint16_t newLength);
00112
00116 inline bool FromInt(int16_t i) { return SubFromInt<10, 7>(i); }
00117
00121 inline bool FromIntBin(int16_t i) { return SubFromInt<2, 18>(i); }
00122
00126 inline bool FromIntOct(int16_t i) { return SubFromInt<8, 8>(i); }
00127
00131 inline bool FromIntHex(int16_t i) { return SubFromInt<16, 6>(i); }
00132
00136 bool FromDouble(double d);
00137
00141 inline bool Copy(cstr_t source) { return SubCopy(source, StrLength(source) ); }
00142
00146 inline bool Copy(const BaseString &source) { return SubCopy(source.Get(), source.GetLength() ); }
00147
00151 bool Copy(cstr_t source, uint16_t n);
00152
00156 inline bool Copy(const BaseString &source, uint16_t n) { return Copy(source.Get(), n); }
00157
00161 inline bool Join(cstr_t value) { return SubJoin(value, StrLength(value) ); }
00162
00166 inline bool Join(const BaseString &value) { return SubJoin(value.Get(), value.GetLength() ); }
00167
00171 inline bool Join(cstr_t value, uint16_t n) { return SubJoin(value, n, StrLength(value) ); }
00172
00176 inline bool Join(const BaseString &value, uint16_t n) { return SubJoin(value.Get(), n, value.GetLength() ); }
00177
00184 inline uint16_t Find(uint16_t index, const BaseString &value) { return SubFind(index, value.Get(), value.GetLength() ); }
00185
00192 inline uint16_t Find(uint16_t index, cstr_t value) { return SubFind(index, value, StrLength(value) ); }
00193
00200 inline uint16_t IFind(uint16_t index, const BaseString &value) { return SubIFind(index, value.Get(), value.GetLength() ); }
00201
00208 inline uint16_t IFind(uint16_t index, cstr_t value) { return SubIFind(index, value, StrLength(value) ); }
00209
00214 inline bool Replace(uint16_t begin, uint16_t end, const BaseString &replaceWith) { return SubReplace(begin, end, replaceWith.Get(), replaceWith.GetLength() ); }
00215
00220 inline bool Replace(uint16_t begin, uint16_t end, cstr_t replaceWith) { return SubReplace(begin, end, replaceWith, StrLength(replaceWith) ); }
00221
00225 inline bool Insert(uint16_t index, const BaseString &value) { return Replace(index, index, value); }
00226
00230 inline bool Insert(uint16_t index, cstr_t value) { return Replace(index, index, value); }
00231
00236 inline char_t GetChar(uint16_t index) const { return (index < length) ? me[index] : '\0'; }
00237
00242 inline bool StartsWith(cstr_t value) const { return StrNCompare(Get(), value, StrLength(value) ) == 0; }
00243
00248 inline bool StartsWith(BaseString &value) const { return StrNCompare(Get(), value.Get(), value.GetLength() ) == 0; }
00249
00254 inline bool IStartsWith(cstr_t value) const { return StrNICompare(Get(), value, StrLength(value) ) == 0; }
00255
00260 inline bool IStartsWith(BaseString &value) const { return StrNICompare(Get(), value.Get(), value.GetLength() ) == 0; }
00261
00266 bool EndsWith(cstr_t value) const;
00267
00272 bool EndsWith(const BaseString &value) const;
00273
00278 bool IEndsWith(cstr_t value) const;
00279
00284 bool IEndsWith(const BaseString &value) const;
00285
00293 inline bool Reserve(uint16_t size) { return EnsureCapacity(size); }
00294
00298 inline void ToUpper() { if (me) StrToUpper(me); }
00299
00303 inline void ToLower() { if (me) StrToLower(me); }
00304
00308 inline double GetDouble() const { return ToDouble(Get() ); }
00309
00313 inline int GetInteger() const { return ToInteger(Get() ); }
00314
00319 inline int Compare(cstr_t comparable) const { return StrCompare(Get(), comparable); }
00320
00325 inline int Compare(BaseString &comparable) const { return StrCompare(Get(), comparable.Get() ); }
00326
00331 inline int ICompare(cstr_t comparable) const { return StrICompare(Get(), comparable); }
00332
00337 inline int ICompare(BaseString &comparable) const { return StrICompare(Get(), comparable.Get() ); }
00338
00343 inline cstr_t operator () () const { return Get(); }
00344
00349 inline cstr_t operator () (uint16_t index) const { return Get(index); }
00350
00355 inline char_t operator [] (uint16_t index) const { return GetChar(index); }
00356
00360 inline BaseString &operator += (BaseString &s) { Join(s.Get() ); return *this; }
00361
00365 inline BaseString &operator += (cstr_t s) { Join(s); return *this; }
00366
00370 inline bool operator < (BaseString &s) const { return Compare(s) < 0; }
00371
00375 inline bool operator > (BaseString &s) const { return Compare(s) > 0; }
00376
00380 inline bool operator <= (BaseString &s) const { return Compare(s) <= 0; }
00381
00385 inline bool operator >= (BaseString &s) const { return Compare(s) >= 0; }
00386
00390 inline bool operator == (BaseString &s) const { return Compare(s) == 0; }
00391
00395 inline bool operator != (BaseString &s) const { return Compare(s) != 0; }
00396
00400 inline bool operator < (cstr_t s) const { return Compare(s) < 0; }
00401
00405 inline bool operator > (cstr_t s) const { return Compare(s) > 0; }
00406
00410 inline bool operator <= (cstr_t s) const { return Compare(s) <= 0; }
00411
00415 inline bool operator >= (cstr_t s) const { return Compare(s) >= 0; }
00416
00420 inline bool operator == (cstr_t s) const { return Compare(s) == 0; }
00421
00425 inline bool operator != (cstr_t s) const { return Compare(s) != 0; }
00426
00427 };
00428
00429 }
00430
00431 }
00432
00433 #endif // ifndef __AVR_CPP_BASE_STRING_H__