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 #include "CppString.h"
00029
00030 using namespace AVRCpp::Collection;
00031
00032 bool DynamicString<1>::EnsureCapacity(uint16_t required)
00033 {
00034 if (me)
00035 {
00036 if (size < required)
00037 me = StrRealloc(me, required);
00038 else
00039 return true;
00040 }
00041 else
00042 me = StrAlloc(required);
00043
00044 if (me)
00045 {
00046 size = required;
00047 return true;
00048 }
00049
00050 size = 0;
00051 length = 0;
00052 return false;
00053
00054 }
00055
00056
00057 bool BaseString::SetLength(uint16_t newLength)
00058 {
00059 if (newLength < length)
00060 {
00061 me[newLength] = '\0';
00062 length = newLength;
00063 return true;
00064 }
00065 return false;
00066
00067 }
00068
00069
00070 bool BaseString::FromDouble(double d)
00071 {
00072 if (!EnsureCapacity(128) )
00073 return false;
00074
00075 int result = DoubleToString(me, d);
00076
00077 if (result == EOF) return false;
00078
00079 length = (uint16_t)result;
00080 return true;
00081
00082 }
00083
00084
00085 bool BaseString::SubJoin(cstr_t value, uint16_t valueLength)
00086 {
00087 uint16_t newLength = valueLength + length;
00088
00089 if (newLength > length)
00090 {
00091 if (!EnsureCapacity(newLength + 1) )
00092 return false;
00093
00094 if (length)
00095 StrJoin(me, value);
00096 else
00097 StrCopy(me, value);
00098
00099 length = newLength;
00100 }
00101 return true;
00102
00103 }
00104
00105
00106 bool BaseString::SubJoin(cstr_t value, uint16_t n, uint16_t valueLength)
00107 {
00108 uint16_t minLength = valueLength > n ? n : valueLength;
00109 uint16_t newLength = minLength + length;
00110
00111 if (newLength > length)
00112 {
00113 if (!EnsureCapacity(newLength + 1) )
00114 return false;
00115
00116 StrNCopy(me + length, value, minLength);
00117 me[newLength] = '\0';
00118
00119 length = newLength;
00120 }
00121 return true;
00122
00123 }
00124
00125
00126 bool BaseString::SubCopy(cstr_t source, uint16_t sourceLength)
00127 {
00128 if (sourceLength == 0 && !me)
00129 return true;
00130
00131 if (!EnsureCapacity(sourceLength + 1) )
00132 return false;
00133
00134 StrCopy(me, source);
00135 length = sourceLength;
00136 return true;
00137
00138 }
00139
00140
00141 bool BaseString::Copy(cstr_t source, uint16_t n)
00142 {
00143 if (n == 0 && !me)
00144 return true;
00145
00146 if (!EnsureCapacity(n + 1) )
00147 return false;
00148
00149 StrNCopy(me, source, n);
00150 me[n] = '\0';
00151
00152 length = n;
00153 return true;
00154
00155 }
00156
00157
00158 uint16_t BaseString::SubFind(uint16_t index, cstr_t value, uint16_t valueLength) const
00159 {
00160
00161 if (valueLength)
00162 {
00163 str_t pointer = StrFind(Get(index), value);
00164
00165 if (pointer)
00166 return (uint16_t)(pointer - me);
00167
00168 }
00169
00170 return NotFound;
00171
00172 }
00173
00174 uint16_t BaseString::SubIFind(uint16_t index, cstr_t value, uint16_t valueLength) const
00175 {
00176
00177 if (valueLength)
00178 {
00179 str_t pointer = StrFind(Get(index), value);
00180
00181 if (pointer)
00182 return (uint16_t)(pointer - me);
00183
00184 }
00185
00186 return NotFound;
00187
00188 }
00189
00190
00191 bool BaseString::EndsWith(cstr_t value) const
00192 {
00193 uint16_t valueLength = StrLength(value);
00194 if (valueLength > length)
00195 return false;
00196
00197 return StrCompare(Get(length - valueLength), value) == 0;
00198
00199 }
00200
00201
00202 bool BaseString::EndsWith(const BaseString &value) const
00203 {
00204 if (value.GetLength() > length)
00205 return false;
00206
00207 return StrCompare(Get(length - value.GetLength() ), value.Get() ) == 0;
00208
00209 }
00210
00211
00212 bool BaseString::IEndsWith(cstr_t value) const
00213 {
00214 uint16_t valueLength = StrLength(value);
00215
00216 if (valueLength > length)
00217 return false;
00218
00219 return StrICompare(Get(length - valueLength), value) == 0;
00220
00221 }
00222
00223
00224 bool BaseString::IEndsWith(const BaseString &value) const
00225 {
00226 if (value.GetLength() > length)
00227 return false;
00228
00229 return StrICompare(Get(length - value.GetLength() ), value.Get() ) == 0;
00230
00231 }
00232
00233
00234 bool BaseString::SubReplace(uint16_t begin, uint16_t end, cstr_t replaceWith, uint16_t replaceWithLength)
00235 {
00236 if (begin > length || end > length || begin > end)
00237 return false;
00238
00239 if (replaceWithLength)
00240 {
00241 if (end == length)
00242 {
00243 uint16_t length1 = begin + replaceWithLength;
00244
00245 if (!EnsureCapacity(length1 + 1) )
00246 return false;
00247
00248 StrCopy(me + begin, replaceWith);
00249 length = length1;
00250 }
00251 else
00252 {
00253 String strEnd;
00254
00255 strEnd.Copy(me + end);
00256 uint16_t length1 = begin + replaceWithLength;
00257 uint16_t length2 = length1 + strEnd.GetLength();
00258
00259 if (!EnsureCapacity(length2 + 1) )
00260 return false;
00261
00262 StrCopy(me + begin, replaceWith);
00263 StrCopy(me + length1, strEnd.Get() );
00264 length = length2;
00265 }
00266 }
00267 else if (begin != end)
00268 {
00269 if (end == length)
00270 {
00271 me[begin] = '\0';
00272 length = begin;
00273 }
00274 else
00275 {
00276 StrCopy(me + begin, me + end);
00277 length -= end - begin;
00278 }
00279 }
00280
00281 return true;
00282
00283 }
00284
00285