nux-1.16.0
TextString.cpp
00001 /*
00002  * Copyright 2010 Inalogic® Inc.
00003  *
00004  * This program is free software: you can redistribute it and/or modify it
00005  * under the terms of the GNU Lesser General Public License, as
00006  * published by the  Free Software Foundation; either version 2.1 or 3.0
00007  * of the License.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranties of
00011  * MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR
00012  * PURPOSE.  See the applicable version of the GNU Lesser General Public
00013  * License for more details.
00014  *
00015  * You should have received a copy of both the GNU Lesser General Public
00016  * License along with this program. If not, see <http://www.gnu.org/licenses/>
00017  *
00018  * Authored by: Jay Taoko <jaytaoko@inalogic.com>
00019  *
00020  */
00021 
00022 
00023 #include "NuxCore.h"
00024 
00025 namespace nux
00026 {
00027 //
00028 // Copy a string with length checking.
00029 //warning: Behavior differs from strncpy; last character is zeroed.
00030 //
00031   TCHAR *Strncpy (TCHAR *Dest, t_size Size, const TCHAR *Src, t_size MaxLen)
00032   {
00033     nuxAssert (MaxLen >= 0);
00034     STRNCPY_S (Dest, Size, Src, MaxLen);
00035     Dest[MaxLen] = 0;
00036     return Dest;
00037   }
00038 
00039 //
00040 // Concatenate a string with length checking
00041 //
00042   TCHAR *Strncat (TCHAR *Dest, t_size Size, const TCHAR *Src, t_size MaxLen)
00043   {
00044     t_size Len = StringLength (Dest);
00045     nuxAssert (Size >= Len);
00046     TCHAR *NewDest = Dest + Len;
00047 
00048     if ( (MaxLen -= Len) > 0)
00049     {
00050       Strncpy (NewDest, Size - Len, Src, MaxLen);
00051       NewDest[MaxLen-1] = 0;
00052     }
00053 
00054     return Dest;
00055   }
00056 
00057 
00058 // Search a string inside a string. Return a pointer to the beginning of the searched string if it is found. Else, return NULL;
00059 // The shearched string must be preceded by a non alpha numeric character in Str.
00060 
00061   const TCHAR *Strfind (const TCHAR *Str, const TCHAR *Find)
00062   {
00063     nuxAssert (Find != NULL);
00064     nuxAssert (Str != NULL);
00065 
00066     if (Find == NULL || Str == NULL)
00067     {
00068       return NULL;
00069     }
00070 
00071     bool AlphaNum = 0;
00072     TCHAR First = ( (*Find < TEXT ('a') ) || (*Find > TEXT ('z') ) ) ? (*Find) : (*Find + TEXT ('A') - TEXT ('a') );
00073     t_size Len = StringLength (Find++) - 1;
00074     TCHAR chr = *Str++;
00075 
00076     while (chr)
00077     {
00078       if ( (chr >= TEXT ('a') ) && (chr <= TEXT ('z') ) )
00079       {
00080         chr += TEXT ('A') - TEXT ('a');
00081       }
00082 
00083       if (!AlphaNum && (chr == First) && !TCharStringNICompare (Str, Find, Len) )
00084       {
00085         return Str - 1;
00086       }
00087 
00088       AlphaNum = ( (chr >= TEXT ('A') ) && (chr <= TEXT ('Z') ) ) || ( (chr >= TEXT ('0') ) && (chr <= TEXT ('9') ) );
00089       chr = *Str++;
00090     }
00091 
00092     return NULL;
00093   }
00094 
00095   bool IsLastChar (const TCHAR *CharString, const TCHAR Chr)
00096   {
00097     nuxAssert (CharString != 0);
00098 
00099     if (CharString == 0)
00100       return false;
00101 
00102     t_size Size = StringLength (CharString);
00103 
00104     if (Size == 0)
00105       return false;
00106 
00107     if (CharString[Size-1] == Chr)
00108       return true;
00109 
00110     return false;
00111   }
00112 
00113   NString Itoa (int InNum)
00114   {
00115     SQWORD      Num                                     = InNum; // This avoids having to deal with negating -MaxS32 - 1
00116     NString NumberString;
00117     const TCHAR *NumberChar[10]         = { TEXT ("0"), TEXT ("1"), TEXT ("2"), TEXT ("3"), TEXT ("4"), TEXT ("5"), TEXT ("6"), TEXT ("7"), TEXT ("8"), TEXT ("9") };
00118     bool        bIsNumberNegative       = false;
00119 
00120     // Correctly handle negative numbers and convert to positive integer.
00121     if (Num < 0)
00122     {
00123       bIsNumberNegative = true;
00124       Num = -Num;
00125     }
00126 
00127     // Convert to string assuming base ten and a positive integer.
00128     do
00129     {
00130       NumberString += NumberChar[Num % 10];
00131       Num /= 10;
00132     }
00133     while (Num);
00134 
00135     // Append sign as we're going to reverse string afterwards.
00136     if (bIsNumberNegative)
00137     {
00138       NumberString += TEXT ("-");
00139     }
00140 
00141     NumberString.Reverse();
00142     return NumberString;
00143   }
00144 
00146   TCHAR *Strdup (const TCHAR *str)
00147   {
00148     if (str == 0)
00149       return 0;
00150 
00151     t_size len = StringLength (str);
00152 
00153     if (len >= 0)
00154     {
00155       TCHAR *res = new TCHAR[len+1];
00156       Strncpy (res, len + 1, str, len);
00157       return res;
00158     }
00159 
00160     return 0;
00161   }
00162 
00164   ANSICHAR *StrdupA ( const ANSICHAR *str)
00165   {
00166     if (str == 0)
00167       return 0;
00168 
00169     int len = (int) strlen (str);
00170 
00171     if (len >= 0)
00172     {
00173       ANSICHAR *res = new ANSICHAR[len+1];
00174       STRNCPY_S ( (char *) res, len + 1, (const char *) str, len);
00175       return res;
00176     }
00177 
00178     return 0;
00179   }
00180 
00182   UNICHAR *StrdupU ( const UNICHAR *str)
00183   {
00184     if (str == 0)
00185       return 0;
00186 
00187     int len = (int) wcslen ( (const wchar_t *) str);
00188 
00189     if (len >= 0)
00190     {
00191       UNICHAR *res = new UNICHAR[len+1];
00192       WCSNCPY_S ( (wchar_t *) res, len + 1, (const wchar_t *) str, len);
00193       return res;
00194     }
00195 
00196     return 0;
00197   }
00198 // /*
00199 // * Standard string formatted print.
00200 // */
00201 // VARARG_BODY(int, inlSprintf, const TCHAR*, VARARG_EXTRA(TCHAR* Dest))
00202 // {
00203 //     int      Result = -1;
00204 //     va_list ap;
00205 //     va_start(ap, Fmt);
00206 //     //@warning: make sure code using inlSprintf allocates enough memory if the below 1024 is ever changed.
00207 //     GET_VARARGS_RESULT(Dest,1024/*!!*/,Fmt,Result);
00208 //     return Result;
00209 // }
00210 
00211 
00212   t_size ValueToLiteralString (char *buffer, t_u32 len, t_u16     value)
00213   {
00214     return ToCharString (buffer, len, "%hu",     value);
00215   }
00216   t_size ValueToLiteralString (char *buffer, t_u32 len, t_s16     value)
00217   {
00218     return ToCharString (buffer, len, "%hi",     value);
00219   }
00220   t_size ValueToLiteralString (char *buffer, t_u32 len, t_u32     value)
00221   {
00222     return ToCharString (buffer, len, "%lu",     value);
00223   }
00224   t_size ValueToLiteralString (char *buffer, t_u32 len, t_s32     value)
00225   {
00226     return ToCharString (buffer, len, "%li",     value);
00227   }
00228   t_size ValueToLiteralString (char *buffer, t_u32 len, t_ulong   value)
00229   {
00230     return ToCharString (buffer, len, "%lu",     value);
00231   }
00232   t_size ValueToLiteralString (char *buffer, t_u32 len, t_long    value)
00233   {
00234     return ToCharString (buffer, len, "%li",     value);
00235   }
00236   t_size ValueToLiteralString (char *buffer, t_u32 len, t_u64     value)
00237   {
00238     return ToCharString (buffer, len, "%I64u",   value);
00239   }
00240   t_size ValueToLiteralString (char *buffer, t_u32 len, t_s64     value)
00241   {
00242     return ToCharString (buffer, len, "%I64i",   value);
00243   }
00244   t_size ValueToLiteralString (char *buffer, t_u32 len, t_float   value)
00245   {
00246     return ToCharString (buffer, len, "%.10g",   value);
00247   }
00248   t_size ValueToLiteralString (char *buffer, t_u32 len, t_double  value)
00249   {
00250     return ToCharString (buffer, len, "%.20lg",  value);
00251   }
00252   t_size ValueToLiteralString (char *buffer, t_u32 len, t_u8      value)
00253   {
00254     return ValueToLiteralString (buffer, len,    (t_u32) value);
00255   }
00256   t_size ValueToLiteralString (char *buffer, t_u32 len, t_char    value)
00257   {
00258     return ValueToLiteralString (buffer, len,    (t_s32) value);
00259   }
00260   t_size ValueToLiteralString (char *buffer, t_u32 len, t_s8      value)
00261   {
00262     return ValueToLiteralString (buffer, len,    (t_s32) value);
00263   }
00264 
00265   bool ValueFromLiteralString (const char *buffer, t_u32 len, t_u16    &value)
00266   {
00267     return FromCharString (buffer, len, "%hu", value );
00268   }
00269   bool ValueFromLiteralString (const char *buffer, t_u32 len, t_s16    &value)
00270   {
00271     return FromCharString (buffer, len, "%hi", value );
00272   }
00273   bool ValueFromLiteralString (const char *buffer, t_u32 len, t_u32    &value)
00274   {
00275     return FromCharString (buffer, len, "%lu", value );
00276   }
00277   bool ValueFromLiteralString (const char *buffer, t_u32 len, t_s32    &value)
00278   {
00279     return FromCharString (buffer, len, "%li", value );
00280   }
00281   bool ValueFromLiteralString (const char *buffer, t_u32 len, t_ulong  &value)
00282   {
00283     return FromCharString (buffer, len, "%lu", value );
00284   }
00285   bool ValueFromLiteralString (const char *buffer, t_u32 len, t_long   &value)
00286   {
00287     return FromCharString (buffer, len, "%li", value );
00288   }
00289   bool ValueFromLiteralString (const char *buffer, t_u32 len, t_u64    &value)
00290   {
00291     return FromCharString (buffer, len, "%I64u", value );
00292   }
00293   bool ValueFromLiteralString (const char *buffer, t_u32 len, t_s64    &value)
00294   {
00295     return FromCharString (buffer, len, "%I64i", value );
00296   }
00297   bool ValueFromLiteralString (const char *buffer, t_u32 len, t_float  &value)
00298   {
00299     return FromCharString (buffer, len, "%g", value );
00300   }
00301   bool ValueFromLiteralString (const char *buffer, t_u32 len, t_double &value)
00302   {
00303     return FromCharString (buffer, len, "%lg", value );
00304   }
00305   bool ValueFromLiteralString (const char *buffer, t_u32 len, t_u8     &value)
00306   {
00307     t_u32 tmp = 0;
00308     bool result;
00309     result = ValueFromLiteralString (buffer, len, tmp);
00310     value = (t_u8) tmp;
00311     return result;
00312   }
00313 
00314   bool ValueFromLiteralString (const char *buffer, t_u32 len, t_char &value)
00315   {
00316     t_s32 tmp = 0;
00317     bool result;
00318     result = ValueFromLiteralString (buffer, len, tmp);
00319     value = (t_char) tmp;
00320     return result;
00321   }
00322 
00323   bool ValueFromLiteralString (const char *buffer, t_u32 len, t_s8 &value)
00324   {
00325     t_s32 tmp = 0;
00326     bool result;
00327     result = ValueFromLiteralString (buffer, len, tmp);
00328     value = (t_s8) tmp;
00329     return result;
00330   }
00331 
00332   VARARG_BODY (int, Snprintf, const TCHAR *, VARARG_EXTRA (TCHAR *Dest) VARARG_EXTRA (int Size) VARARG_EXTRA (int Count) )
00333   {
00334     int Result = -1;
00335     GET_VARARGS_RESULT (Dest, Size, Count, Fmt, Result);
00336     return Result;
00337   }
00338 
00339   NString::NString()
00340   {
00341     m_string = TEXT("");
00342   }
00343 
00344   NString::NString (const NString &s)
00345   {
00346     m_string = s.m_string;
00347   }
00348 
00349   NString &NString::operator= (const NString &s)
00350   {
00351     m_string = s.m_string;
00352     return *this;
00353   }
00354 
00355   NString::NString (const tstring &s)
00356   {
00357     m_string = s;
00358   }
00359 
00360   NString::NString (const TCHAR &s)
00361   {
00362     m_string = s;
00363   }
00364 
00365   NString::NString (const ANSICHAR *s)
00366   {
00367 #ifdef UNICODE
00368 
00369     if (s == 0)
00370       m_string = TEXT ("");
00371     else
00372       m_string = ANSICHAR_TO_UNICHAR (s);
00373 
00374 #else
00375 
00376     if (s == 0)
00377       m_string = TEXT ("");
00378     else
00379       m_string = s;
00380 
00381 #endif
00382   }
00383 
00384   NString::NString (const UNICHAR *s)
00385   {
00386 #ifdef UNICODE
00387 
00388     if (s == 0)
00389       m_string = TEXT ("");
00390     else
00391       m_string = s;
00392 
00393 #else
00394 
00395     if (s == 0)
00396       m_string = TEXT ("");
00397     else
00398       m_string = UNICHAR_TO_ANSICHAR (s);
00399 
00400 #endif
00401   }
00402 
00403 
00404   NString::~NString()
00405   {
00406   }
00407 
00408   const tstring &NString::GetTStringRef() const
00409   {
00410     return m_string;
00411   }
00412 
00413 //const TCHAR* NString::GetTChar() const
00414 //{
00415 //    return m_string.c_str();
00416 //}
00417 
00418   const TCHAR *NString::GetTCharPtr() const
00419   {
00420     return m_string.c_str();
00421   }
00422 
00423   t_size NString::Length() const
00424   {
00425     return m_string.length();
00426   }
00427 
00428   t_size NString::Size() const
00429   {
00430     return m_string.size();
00431   }
00432 
00433   void NString::Clear()
00434   {
00435     m_string.clear();
00436   }
00437 
00438   bool NString::IsEmpty() const
00439   {
00440     return m_string.empty();
00441   }
00442 
00443   void NString::Erase (t_size Pos, t_size Count)
00444   {
00445     m_string.erase (Pos, Count);
00446   }
00447 
00448   NString &NString::Insert (t_size Pos, const TCHAR *Ptr)
00449   {
00450     m_string.insert (Pos, Ptr);
00451     return *this;
00452   }
00453 
00454   NString &NString::Insert (t_size Pos, const TCHAR *Ptr, t_size Count)
00455   {
00456     m_string.insert (Pos, Ptr, Count);
00457     return *this;
00458   }
00459 
00460   NString &NString::Insert (t_size Pos, const tstring &Str)
00461   {
00462     m_string.insert (Pos, Str);
00463     return *this;
00464   }
00465 
00466   NString &NString::Insert (t_size Pos, const tstring &Str, t_size Offset, t_size Count)
00467   {
00468     m_string.insert (Pos, Str, Offset, Count);
00469     return *this;
00470   }
00471 
00472   NString &NString::Insert (t_size Pos, const NString &Str)
00473   {
00474     m_string.insert (Pos, Str.m_string);
00475     return *this;
00476   }
00477 
00478   NString &NString::Insert (t_size Pos, const NString &Str, t_size Offset, t_size Count)
00479   {
00480     m_string.insert (Pos, Str.m_string, Offset, Count);
00481     return *this;
00482   }
00483 
00484   NString &NString::Insert (t_size Pos, int Count, const TCHAR &Ch)
00485   {
00486     m_string.insert (Pos, Count, Ch);
00487     return *this;
00488   }
00489 
00490   const TCHAR &NString::operator[] (t_size ChPos) const
00491   {
00492     return m_string[ChPos];
00493   }
00494 
00495   TCHAR &NString::operator[] (t_size ChPos)
00496   {
00497     return m_string[ChPos];
00498   }
00499 
00500   NString &NString::Replace (t_size Pos1, t_size Num1, const TCHAR *Ptr)
00501   {
00502     m_string.replace (Pos1, Num1, Ptr);
00503     return *this;
00504   }
00505 
00506   NString &NString::Replace (t_size Pos1, t_size Num1, const TCHAR *Ptr, t_size Num2)
00507   {
00508     m_string.replace (Pos1, Num1, Ptr, Num2);
00509     return *this;
00510   }
00511 
00512   NString &NString::Replace (t_size Pos1, t_size Num1, const tstring &Str)
00513   {
00514     m_string.replace (Pos1, Num1, Str);
00515     return *this;
00516   }
00517 
00518   NString &NString::Replace (t_size Pos1, t_size Num1, const tstring &Str, t_size Pos2, t_size Num2)
00519   {
00520     m_string.replace (Pos1, Num1, Str, Pos2, Num2);
00521     return *this;
00522   }
00523 
00524   NString &NString::Replace (t_size Pos1, t_size Num1, const NString &Str)
00525   {
00526     m_string.replace (Pos1, Num1, Str.m_string);
00527     return *this;
00528   }
00529 
00530   NString &NString::Replace (t_size Pos1, t_size Num1, const NString &Str, t_size Pos2, t_size Num2)
00531   {
00532     m_string.replace (Pos1, Num1, Str.m_string, Pos2, Num2);
00533     return *this;
00534   }
00535 
00536   NString &NString::Replace (t_size Pos1, t_size Num1, t_size Count, TCHAR Ch)
00537   {
00538     m_string.replace (Pos1, Num1, Count, Ch);
00539     return *this;
00540   }
00541 
00542   void NString::Reverse()
00543   {
00544     NString rev;
00545     t_size l = Length();
00546 
00547     for (t_size i = l - 1; i >= 0; i--)
00548     {
00549       rev += m_string[i];
00550     }
00551 
00552     (*this) = rev;
00553   }
00554 
00555   NString &NString::SearchAndReplace (TCHAR ChOut, TCHAR ChIn)
00556   {
00557     for (t_size i = 0; i < Length(); i++)
00558       if (m_string[i] == ChOut)
00559         m_string[i] = ChIn;
00560 
00561     return *this;
00562   }
00563 
00565   t_size NString::FindLastOccurence (const TCHAR &suffix) const
00566   {
00567     t_size start = 0;
00568     t_size pos = 0;
00569 
00570     do
00571     {
00572       pos = m_string.find (suffix, start);
00573 
00574       if (pos != tstring::npos)
00575         start = pos + 1;
00576     }
00577     while (pos != tstring::npos);
00578 
00579     return start - 1;
00580   }
00581 
00583   t_size NString::FindLastOccurence (const TCHAR *suffix) const
00584   {
00585     t_size start = 0;
00586     t_size pos = 0;
00587 
00588     do
00589     {
00590       pos = m_string.find (suffix, start);
00591 
00592       if (pos != tstring::npos)
00593         start = pos + 1;
00594     }
00595     while (pos != tstring::npos);
00596 
00597     return start - 1;
00598   }
00599 
00601   t_size NString::FindLastOccurence (const tstring &suffix) const
00602   {
00603     t_size start = 0;
00604     t_size pos = 0;
00605 
00606     do
00607     {
00608       pos = m_string.find (suffix, start);
00609 
00610       if (pos != tstring::npos)
00611         start = pos + 1;
00612     }
00613     while (pos != tstring::npos);
00614 
00615     return start - 1;
00616   }
00617 
00619   t_size NString::FindLastOccurence (const NString &suffix) const
00620   {
00621     t_size start = 0;
00622     t_size pos = 0;
00623 
00624     do
00625     {
00626       pos = m_string.find (suffix.m_string, start);
00627 
00628       if (pos != tstring::npos)
00629         start = pos + 1;
00630     }
00631     while (pos != tstring::npos);
00632 
00633     return start - 1;
00634   }
00635 
00636 
00638   t_size NString::FindFirstOccurence (const TCHAR &suffix)  const
00639   {
00640     t_size pos = 0;
00641     pos = m_string.find (suffix, pos);
00642     return (pos != tstring::npos) ? pos : -1;
00643   }
00644 
00646   t_size NString::FindFirstOccurence (const TCHAR *suffix)  const
00647   {
00648     t_size pos = 0;
00649     pos = m_string.find (suffix, pos);
00650     return (pos != tstring::npos) ? pos : -1;
00651   }
00652 
00654   t_size NString::FindFirstOccurence (const tstring &suffix)  const
00655   {
00656     t_size pos = 0;
00657     pos = m_string.find (suffix, pos);
00658     return (pos != tstring::npos) ? pos : -1;
00659   }
00660 
00662   t_size NString::FindFirstOccurence (const NString &suffix) const
00663   {
00664     t_size pos = 0;
00665     pos = m_string.find (suffix.m_string, pos);
00666     return (pos != tstring::npos) ? pos : -1;
00667   }
00668 
00670   t_size NString::FindNextOccurence (const TCHAR &suffix, t_size start) const
00671   {
00672     t_size pos = 0;
00673     pos = m_string.find (suffix, start);
00674     return (pos != tstring::npos) ? pos : -1;
00675   }
00676 
00678   t_size NString::FindNextOccurence (const TCHAR *suffix, t_size start) const
00679   {
00680     t_size pos = 0;
00681     pos = m_string.find (suffix, start);
00682     return (pos != tstring::npos) ? pos : -1;
00683   }
00684 
00686   t_size NString::FindNextOccurence (const tstring &suffix, t_size start) const
00687   {
00688     t_size pos = 0;
00689     pos = m_string.find (suffix, start);
00690     return (pos != tstring::npos) ? pos : -1;
00691   }
00692 
00694   t_size NString::FindNextOccurence (const NString &suffix, t_size start) const
00695   {
00696     t_size pos = 0;
00697     pos = m_string.find (suffix.m_string, start);
00698     return (pos != tstring::npos) ? pos : -1;
00699   }
00700 
00702   t_size NString::FindFirstOccurenceOf (const TCHAR &str) const
00703   {
00704     t_size pos = 0;
00705     pos = m_string.find_first_of (str, pos);
00706     return (pos != tstring::npos) ? pos : -1;
00707   }
00709   t_size NString::FindFirstOccurenceOf (const TCHAR *str) const
00710   {
00711     t_size pos = 0;
00712     pos = m_string.find_first_of (str, pos);
00713     return (pos != tstring::npos) ? pos : -1;
00714   }
00716   t_size NString::FindFirstOccurenceOf (const tstring &str) const
00717   {
00718     t_size pos = 0;
00719     pos = m_string.find_first_of (str, pos);
00720     return (pos != tstring::npos) ? pos : -1;
00721   }
00723   t_size NString::FindFirstOccurenceOf (const NString &str) const
00724   {
00725     t_size pos = 0;
00726     pos = m_string.find_first_of (str.m_string, pos);
00727     return (pos != tstring::npos) ? pos : -1;
00728   }
00729 
00731   t_size NString::FindLastOccurenceOf (const TCHAR &str) const
00732   {
00733     t_size pos = 0;
00734     pos = m_string.find_last_of (str, pos);
00735     return (pos != tstring::npos) ? pos : -1;
00736   }
00738   t_size NString::FindLastOccurenceOf (const TCHAR *str) const
00739   {
00740     t_size pos = 0;
00741     pos = m_string.find_last_of (str, pos);
00742     return (pos != tstring::npos) ? pos : -1;
00743   }
00745   t_size NString::FindLastOccurenceOf (const tstring &str) const
00746   {
00747     t_size pos = 0;
00748     pos = m_string.find_last_of (str, pos);
00749     return (pos != tstring::npos) ? pos : -1;
00750   }
00752   t_size NString::FindLastOccurenceOf (const NString &str) const
00753   {
00754     t_size pos = 0;
00755     pos = m_string.find_last_of (str.m_string, pos);
00756     return (pos != tstring::npos) ? pos : -1;
00757   }
00758 
00759   t_size NString::Find (NString str, int start)
00760   {
00761     t_size pos = m_string.find (str.m_string, start);
00762     return (pos != tstring::npos) ? pos : -1;
00763   }
00764 
00765   t_size NString::Find (TCHAR c, int start)
00766   {
00767     t_size pos = m_string.find (c, start);
00768     return (pos != tstring::npos) ? pos : -1;
00769   }
00770 
00771   bool NString::IsSuffix (const TCHAR &suffix)
00772   {
00773     t_size l = m_string.length() - 1;
00774 
00775     if (l < 0)
00776       return false;
00777 
00778     t_size pos = FindLastOccurence (suffix);
00779 
00780     if (pos == tstring::npos)
00781       return false;
00782 
00783     return (pos == l);
00784   }
00785 
00786   bool NString::IsSuffix (const TCHAR *suffix)
00787   {
00788     t_size sl = StringLength (suffix);
00789 
00790     if (sl == 0)
00791       return false;
00792 
00793     t_size l = m_string.length() - sl;
00794 
00795     if (l < 0)
00796       return false;
00797 
00798     t_size pos = FindLastOccurence (suffix);
00799 
00800     if (pos == tstring::npos)
00801       return false;
00802 
00803     return (pos == l);
00804   }
00805 
00807   bool NString::IsSuffix (const tstring &suffix)
00808   {
00809     t_size sl = suffix.length();
00810 
00811     if (sl == 0)
00812       return false;
00813 
00814     t_size l = m_string.length() - sl;
00815 
00816     if (l < 0)
00817       return false;
00818 
00819     t_size pos = FindLastOccurence (suffix);
00820 
00821     if (pos == tstring::npos)
00822       return false;
00823 
00824     return (pos == l);
00825   }
00826 
00828   bool NString::IsSuffix (const NString &suffix)
00829   {
00830     t_size sl = suffix.Length();
00831 
00832     if (sl == 0)
00833       return false;
00834 
00835     t_size l = m_string.length() - sl;
00836 
00837     if (l < 0)
00838       return false;
00839 
00840     t_size pos = FindLastOccurence (suffix);
00841 
00842     if (pos == tstring::npos)
00843       return false;
00844 
00845     return (pos == l);
00846   }
00847 
00849   bool NString::IsPrefix (const TCHAR &prefix)
00850   {
00851     t_size l = m_string.length() - 1;
00852 
00853     if (l < 0)
00854       return false;
00855 
00856     t_size pos = FindFirstOccurence (prefix);
00857 
00858     if (pos == tstring::npos)
00859       return false;
00860 
00861     return (pos == 0);
00862   }
00863 
00865   bool NString::IsPrefix (const TCHAR *prefix)
00866   {
00867     t_size sl = StringLength (prefix);
00868 
00869     if (sl == 0)
00870       return false;
00871 
00872     t_size pos = FindFirstOccurence (prefix);
00873 
00874     if (pos == tstring::npos)
00875       return false;
00876 
00877     return (pos == 0);
00878   }
00879 
00881   bool NString::IsPrefix (const tstring &prefix)
00882   {
00883     t_size sl = prefix.length();
00884 
00885     if (sl == 0)
00886       return false;
00887 
00888     t_size pos = FindFirstOccurence (prefix);
00889 
00890     if (pos == tstring::npos)
00891       return false;
00892 
00893     return (pos == 0);
00894   }
00896   bool NString::IsPrefix (const NString &prefix)
00897   {
00898     t_size sl = prefix.Length();
00899 
00900     if (sl == 0)
00901       return false;
00902 
00903     t_size pos = FindFirstOccurence (prefix);
00904 
00905     if (pos == tstring::npos)
00906       return false;
00907 
00908     return (pos == 0);
00909   }
00910 
00912   void NString::RemoveSuffix (const TCHAR &suffix)
00913   {
00914     if (IsSuffix (suffix) )
00915     {
00916       t_size pos = FindLastOccurence (suffix);
00917       *this = NString (m_string.substr (0, pos) );
00918     }
00919   }
00920 
00922   void NString::RemoveSuffix (const TCHAR *suffix)
00923   {
00924     if (IsSuffix (suffix) )
00925     {
00926       t_size pos = FindLastOccurence (suffix);
00927       *this = NString (m_string.substr (0, pos) );
00928     }
00929   }
00930 
00932   void NString::RemoveSuffix (const tstring &suffix)
00933   {
00934     if (IsSuffix (suffix) )
00935     {
00936       t_size pos = FindLastOccurence (suffix);
00937       *this = NString (m_string.substr (0, pos) );
00938     }
00939   }
00940 
00942   void NString::RemoveSuffix (const NString &suffix)
00943   {
00944     if (IsSuffix (suffix) )
00945     {
00946       t_size pos = FindLastOccurence (suffix);
00947       *this = NString (m_string.substr (0, pos) );
00948     }
00949   }
00950 
00952   void NString::RemovePrefix (const TCHAR &prefix)
00953   {
00954     if (IsPrefix (prefix) )
00955     {
00956       *this = NString (m_string.substr (1) );
00957     }
00958   }
00959 
00961   void NString::RemovePrefix (const TCHAR *prefix)
00962   {
00963     if (IsPrefix (prefix) )
00964     {
00965       t_size l = StringLength (prefix);
00966       *this = NString (m_string.substr (l) );
00967     }
00968   }
00969 
00971   void NString::RemovePrefix (const tstring &prefix)
00972   {
00973     if (IsPrefix (prefix) )
00974     {
00975       t_size l = prefix.length();
00976       *this = NString (m_string.substr (l) );
00977     }
00978   }
00979 
00981   void NString::RemovePrefix (const NString &prefix)
00982   {
00983     if (IsPrefix (prefix) )
00984     {
00985       t_size l = prefix.Length();
00986       *this = NString (m_string.substr (l) );
00987     }
00988   }
00989 
00990   NString NString::GetSubString (t_size count) const
00991   {
00992     nuxAssert (count >= 0);
00993     return NString (m_string.substr (0, count) );
00994   }
00995 
00996   NString NString::GetSubString (t_size start, t_size count) const
00997   {
00998     nuxAssert (start >= 0);
00999     nuxAssert (count >= 0);
01000     return NString (m_string.substr (start, count) );
01001   }
01002 
01003   NString NString::Mid (t_size count) const
01004   {
01005     return GetSubString (count);
01006   }
01007 
01008   NString NString::Mid (t_size start, t_size count) const
01009   {
01010     return GetSubString (start, count);
01011   }
01012 
01013   NString NString::Left (t_size N) const
01014   {
01015     if (N >= Length() )
01016       return *this;
01017 
01018     return GetSubString (0, N);
01019   }
01020 
01021   NString NString::Right (t_size N) const
01022   {
01023     if (N >= Length() )
01024       return *this;
01025 
01026     return GetSubString (Length() - N, N);
01027   }
01028 
01029   NString NString::Trim() const
01030   {
01031     return TrimLeft().TrimRight();
01032   }
01033 
01034   NString NString::TrimLeft() const
01035   {
01036     t_size n = 0;
01037     t_size L = Length() - 1;
01038 
01039     while (n <= L)
01040     {
01041       if (IsWhitespaceChar (m_string[n]) )
01042       {
01043         n++;
01044       }
01045       else
01046       {
01047         break;
01048       }
01049     }
01050 
01051     return GetSubString (n, Length() - n);
01052   }
01053 
01054   NString NString::TrimRight() const
01055   {
01056     t_size L = Length() - 1;
01057 
01058     while (0 <= L)
01059     {
01060       if (IsWhitespaceChar (m_string[L]) )
01061       {
01062         L--;
01063       }
01064       else
01065       {
01066         break;
01067       }
01068     }
01069 
01070     return GetSubString (0, Length() + 1);
01071   }
01072 
01073   NString NString::TrimLeft (NString str) const
01074   {
01075     t_size n = 0;
01076     t_size L = Length();
01077 
01078     if (L == 0)
01079       return *this;
01080 
01081     while (n < L)
01082     {
01083       bool trim = false;
01084       t_size sz = str.Length();
01085 
01086       for (t_size i = 0; i < sz; i++)
01087       {
01088         if (m_string[n] == str[i])
01089         {
01090           trim = true;
01091           break;
01092         }
01093       }
01094 
01095       if (trim)
01096       {
01097         n++;
01098       }
01099       else
01100       {
01101         break;
01102       }
01103     }
01104 
01105     return GetSubString (n, Length() - n);
01106   }
01107 
01108   NString NString::TrimRight (NString str) const
01109   {
01110     t_size L = Length();
01111 
01112     if (L == 0)
01113       return *this;
01114 
01115     L = L - 1;
01116 
01117     while (0 <= L)
01118     {
01119       bool trim = false;
01120       t_size sz = str.Length();
01121 
01122       for (t_size i = 0; i < sz; i++)
01123       {
01124         if (m_string[L] == str[i])
01125         {
01126           trim = true;
01127           break;
01128         }
01129       }
01130 
01131       if (trim)
01132       {
01133         L--;
01134       }
01135       else
01136       {
01137         break;
01138       }
01139     }
01140 
01141     return GetSubString (0, L + 1);
01142   }
01143 
01144   const TCHAR *NString::operator () () const
01145   {
01146     if (Size() == 0)
01147       return 0;
01148 
01149     return GetTCharPtr();
01150   }
01151 
01152   const TCHAR *NString::operator * () const
01153   {
01154     if (Size() == 0)
01155       return 0;
01156 
01157     return GetTCharPtr();
01158   }
01159 
01160 // NString::operator const TCHAR*() const
01161 // {
01162 //     return (const TCHAR*)GetTCharPtr();
01163 // }
01164 
01165   NString &NString::operator += (const TCHAR &sufix)
01166   {
01167     m_string += sufix;
01168     return *this;
01169   }
01170 
01171   NString &NString::operator += (const TCHAR *sufix)
01172   {
01173     m_string += sufix;
01174     return *this;
01175   }
01176 
01177   NString &NString::operator += (const tstring sufix)
01178   {
01179     m_string += sufix;
01180     return *this;
01181   }
01182 
01183   NString &NString::operator += (const NString sufix)
01184   {
01185     m_string += sufix.m_string;
01186     return *this;
01187   }
01188 
01189   void NString::SplitAtFirstOccurenceOf (const TCHAR *SplitString, NString &Left, NString &Right)
01190   {
01191     t_size start = FindFirstOccurence (SplitString);
01192 
01193     if (start != tstring::npos)
01194     {
01195       t_size size = StringLength (SplitString);
01196       Left = GetSubString (0, start);
01197       Right = GetSubString (start + size, Length() - (start + size) );
01198     }
01199     else
01200     {
01201       Left = *this;
01202       Right = "";
01203     }
01204   }
01205 
01206   void NString::SplitAtFirstOccurenceOf (const TCHAR &SplitChar, NString &Left, NString &Right)
01207   {
01208     SplitAtFirstOccurenceOf (NString (SplitChar), Left, Right);
01209   }
01210 
01211   void NString::SplitAtFirstOccurenceOf (const NString &SplitString, NString &Left, NString &Right)
01212   {
01213     SplitAtFirstOccurenceOf (SplitString.GetTCharPtr(), Left, Right);
01214   }
01215 
01216   void NString::SplitAtLastOccurenceOf (const TCHAR *SplitString, NString &Left, NString &Right)
01217   {
01218     t_size start = FindLastOccurence (SplitString);
01219 
01220     if (start != tstring::npos)
01221     {
01222       t_size size = StringLength (SplitString);
01223       Left = GetSubString (0, start);
01224       Right = GetSubString (start + size, Length() - (start + size) );
01225     }
01226     else
01227     {
01228       Left = *this;
01229       Right = "";
01230     }
01231   }
01232 
01233   void NString::SplitAtLastOccurenceOf (const TCHAR &SplitChar, NString &Left, NString &Right)
01234   {
01235     SplitAtLastOccurenceOf (NString (SplitChar), Left, Right);
01236   }
01237 
01238   void NString::SplitAtLastOccurenceOf (const NString &SplitString, NString &Left, NString &Right)
01239   {
01240     SplitAtLastOccurenceOf (SplitString.GetTCharPtr(), Left, Right);
01241   }
01242 
01243   void NString::ParseToArray (std::vector<NString>& StringArray, const NString &delimiter)
01244   {
01245     NString Left;
01246     NString Right;
01247     NString Temp;
01248     SplitAtFirstOccurenceOf (delimiter, Left, Temp);
01249 
01250     if (Left.Size() )
01251     {
01252       Left = Left.Trim();
01253       StringArray.push_back (Left);
01254     }
01255 
01256     Right = Temp;
01257 
01258     while (Right.Size() )
01259     {
01260       Right.SplitAtFirstOccurenceOf (delimiter, Left, Temp);
01261 
01262       if (Left.Size() )
01263       {
01264         Left = Left.Trim();
01265         StringArray.push_back (Left);
01266       }
01267 
01268       Right = Temp;
01269     }
01270   }
01271 
01272   TCHAR NString::GetFirstChar() const
01273   {
01274     if (IsEmpty() )
01275       return 0;
01276     return m_string[0];
01277   }
01278 
01279   TCHAR NString::GetLastChar() const
01280   {
01281     if (IsEmpty() )
01282       return 0;
01283     return m_string[Size()-1];
01284   }
01285 
01286   bool operator != (const NString &left, const NString &right)
01287   {
01288     return left.m_string != right.m_string;
01289   }
01290   bool operator == (const NString &left, const NString &right)
01291   {
01292     return left.m_string == right.m_string;
01293   }
01294   bool operator <  (const NString &left, const NString &right)
01295   {
01296     return left.m_string < right.m_string;
01297   }
01298   bool operator <= (const NString &left, const NString &right)
01299   {
01300     return left.m_string <= right.m_string;
01301   }
01302   bool operator >  (const NString &left, const NString &right)
01303   {
01304     return left.m_string > right.m_string;
01305   }
01306   bool operator >= (const NString &left, const NString &right)
01307   {
01308     return left.m_string >= right.m_string;
01309   }
01310 
01311   NString operator+ (const NString &left, const NString &right)
01312   {
01313     return NString (left.m_string + right.m_string);
01314   }
01315 
01316   NString operator+ (const NString &left, const TCHAR *right)
01317   {
01318     return NString (left.m_string + right);
01319   }
01320 
01321   NString operator+ (const NString &left, const TCHAR right)
01322   {
01323     return NString (left.m_string + right);
01324   }
01325 
01326   NString operator+ (const TCHAR *left, const NString &right)
01327   {
01328     return NString (left + right.m_string);
01329   }
01330 
01331   NString operator+ (const TCHAR left, const NString &right)
01332   {
01333     return NString (left + right.m_string);
01334   }
01335 
01336   tostream &operator << (tostream &o, const NString &s)
01337   {
01338     return o << s.m_string;
01339   }
01340 
01346   VARARG_BODY (NString, NString::Printf, const TCHAR *, VARARG_NONE)
01347   {
01348     t_u32  BufferSize  = 1024;
01349     TCHAR *Buffer      = NULL;
01350     t_size Result      = tstring::npos;
01351 
01352     while (Result == tstring::npos)
01353     {
01354       Buffer = (TCHAR *) Realloc (Buffer, BufferSize * sizeof (TCHAR) );
01355       GET_VARARGS_RESULT (Buffer, BufferSize, BufferSize - 1, Fmt, Result);
01356       BufferSize *= 2;
01357     };
01358 
01359     Buffer[Result] = 0;
01360 
01361     NString ResultString (Buffer);
01362 
01363     free (Buffer);
01364 
01365     return ResultString;
01366   }
01367 
01368 }
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends