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
00029
00030
00031
00038 template<class T, u8 N> class pgStr
00039 {
00040 public:
00041 pgDefineException(ExceptionOutOfRange);
00042
00046 pgStr()
00047 {
00048 m_str[0] = 0;
00049 m_length = 0;
00050 }
00051
00056 pgStr(T ch)
00057 {
00058 *this = ch;
00059 }
00060
00065 pgStr(const T* str)
00066 {
00067 *this = str;
00068 }
00069
00074 pgStr(const pgStr<T, N>& str)
00075 {
00076 *this = str;
00077 }
00078
00084 template<u8 N2> pgStr(const pgStr<T, N2>& str)
00085 {
00086 *this = str;
00087 }
00088
00094 pgStr<T, N>& operator=(T ch)
00095 {
00096 m_str[0] = ch;
00097 m_str[1] = 0;
00098
00099 m_length = 1;
00100
00101 return *this;
00102 }
00103
00109 pgStr<T, N>& operator=(const T* str)
00110 {
00111 for (m_length = 0; m_length < N; m_length++)
00112 {
00113 T t = str[m_length];
00114
00115 if (t == 0)
00116 {
00117 break;
00118 }
00119
00120 m_str[m_length] = t;
00121 }
00122
00123 m_str[m_length] = 0;
00124
00125 return *this;
00126 }
00127
00133 pgStr<T, N>& operator=(const pgStr<T, N>& str)
00134 {
00135 m_length = str.m_length;
00136
00137 pgMemMgr::memcpy(m_str, str.m_str, sizeof(T) * (m_length + 1));
00138
00139 return *this;
00140 }
00141
00148 template<u8 N2> pgStr<T, N>& operator=(const pgStr<T, N2>& str)
00149 {
00150 m_length = (str.getLength() < N) ? str.getLength() : N;
00151
00152 pgMemMgr::memcpy(m_str, str.getString(), sizeof(T) * (m_length + 1));
00153
00154 m_str[m_length] = 0;
00155
00156 return *this;
00157 }
00158
00164 bool operator==(T ch) const
00165 {
00166 return (*this == pgStr<T, N>(ch));
00167 }
00168
00174 bool operator==(const T* str) const
00175 {
00176 for (s32 i = 0; ; i++)
00177 {
00178 T t = m_str[i];
00179
00180 if (str[i] != t)
00181 {
00182 return false;
00183 }
00184
00185 if (t == 0)
00186 {
00187 return true;
00188 }
00189 }
00190
00191 return false;
00192 }
00193
00200 template<u8 N2> bool operator==(const pgStr<T, N2>& str) const
00201 {
00202 if (m_length == str.getLength())
00203 {
00204 const T* str2 = str.getString();
00205
00206 for (s32 i = 0; ; i++)
00207 {
00208 T t = m_str[i];
00209
00210 if (str2[i] != t)
00211 {
00212 return false;
00213 }
00214
00215 if (t == 0)
00216 {
00217 return true;
00218 }
00219 }
00220 }
00221
00222 return false;
00223 }
00224
00230 bool operator!=(T ch) const
00231 {
00232 return !(*this == ch);
00233 }
00234
00240 bool operator!=(const T* str) const
00241 {
00242 return !(*this == str);
00243 }
00244
00251 template<u8 N2> bool operator!=(const pgStr<T, N2>& str) const
00252 {
00253 return !(*this == str);
00254 }
00255
00261 T& operator[](u8 index)
00262 {
00263 if (index >= m_length)
00264 {
00265 pgThrow(ExceptionOutOfRange);
00266 }
00267
00268 return m_str[index];
00269 }
00270
00276 pgStr<T, N> operator+(T ch) const
00277 {
00278 pgStr<T, N> res = *this;
00279
00280 res += ch;
00281
00282 return res;
00283 }
00284
00290 pgStr<T, N> operator+(const T* str) const
00291 {
00292 pgStr<T, N> res = *this;
00293
00294 res += str;
00295
00296 return res;
00297 }
00298
00305 template<u8 N2> pgStr<T, N> operator+(const pgStr<T, N2>& str) const
00306 {
00307 pgStr<T, N> res = *this;
00308
00309 res += str;
00310
00311 return res;
00312 }
00313
00318 void operator+=(T ch)
00319 {
00320 *this += pgStr<T, N>(ch);
00321 }
00322
00327 void operator+=(const T* str)
00328 {
00329 for ( ; m_length < N; m_length++)
00330 {
00331 if (*str == 0)
00332 {
00333 break;
00334 }
00335
00336 m_str[m_length] = *str;
00337
00338 str++;
00339 }
00340
00341 m_str[m_length] = 0;
00342 }
00343
00349 template<u8 N2> void operator+=(const pgStr<T, N2>& str)
00350 {
00351 const T* str2 = str.getString();
00352
00353 for ( ; m_length < N; m_length++)
00354 {
00355 if (*str2 == 0)
00356 {
00357 break;
00358 }
00359
00360 m_str[m_length] = *str2;
00361
00362 str2++;
00363 }
00364
00365 m_str[m_length] = 0;
00366 }
00367
00374 u32 operator%(u32 n) const
00375 {
00376 u32 acc = 0;
00377
00378 for (s32 i = 0; i < m_length; i++)
00379 {
00380 acc += m_str[i];
00381 }
00382
00383 return acc % n;
00384 }
00385
00390 const T* getString() const
00391 {
00392 return m_str;
00393 }
00394
00399 u8 getLength() const
00400 {
00401 return m_length;
00402 }
00403
00410 pgStr<T, N> getSubStr(u8 index, u8 length = 0) const
00411 {
00412 pgStr<T, N> res;
00413
00414 if (length == 0)
00415 {
00416 length = N;
00417 }
00418
00419 for ( ; res.m_length < length; res.m_length++)
00420 {
00421 if (index >= m_length || index >= N)
00422 {
00423 break;
00424 }
00425
00426 res.m_str[res.m_length] = m_str[index];
00427
00428 index++;
00429 }
00430
00431 res.m_str[res.m_length] = 0;
00432
00433 return res;
00434 }
00435
00436 private:
00437 T m_str[N + 1];
00438 u8 m_length;
00439 };
00440
00441
00442 template<class T> class pgStr<T, 0>
00443 {
00444 private:
00445 pgStr() {}
00446 };