Advertisement
Tyler_Elric

SmartString.cpp

Feb 9th, 2012
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.91 KB | None | 0 0
  1. #include <cstring>
  2. #include <cctype>
  3. #include <iostream>
  4. #include <stdexcept>
  5.  
  6. #include "SmartString.h"
  7. #include "SmartArray.h"
  8.  
  9. //SmartString::SplitPoint::NoSplit.
  10. SmartString::SplitPoint::NoSplit::NoSplit(unsigned int len):mLen(len){}
  11. SmartString::SplitPoint::NoSplit::operator unsigned int()
  12. {
  13.     return mLen;
  14. }
  15. //SmartString::SplitPoint::Split.
  16. SmartString::SplitPoint::Split::Split(unsigned int len,char* ptr):SmartString::SplitPoint::NoSplit(len)
  17. {
  18.     newString = new SmartString(ptr,mLen);
  19. }
  20. SmartString* SmartString::SplitPoint::Split::operator () ()
  21. {
  22.     return newString;
  23. }
  24. SmartString::SplitPoint::Split::~Split()
  25. {
  26.     if ( newString->UnReference() )
  27.         delete newString;
  28. }
  29. //SmartString::SplitPoint.
  30. SmartString::SplitPoint::SplitPoint():mPoints(new SmartString("")),mKeys(){}
  31. SmartString::SplitPoint& SmartString::SplitPoint::AddPoint( const char* src)
  32. {
  33.     mKeys.Append(new SmartString( src) );
  34.     return *this;
  35. }
  36. SmartString::SplitPoint& SmartString::SplitPoint::AddPoint( const char src )
  37. {
  38.     mPoints().Append( src );
  39.     return *this;
  40. }
  41. SmartString::SplitPoint::~SplitPoint()
  42. {
  43. }
  44. unsigned int SmartString::SplitPoint::operator()(char* ptr) const
  45. {
  46.     if(strchr(mPoints(),*ptr))
  47.     {
  48.             char* o = ptr;
  49.             while(strchr(mPoints(),*ptr)) ptr++;
  50.             std::cout << "Not including : '" << SmartString(o,((unsigned int)(ptr-o))) << "'" << std::endl;
  51.             throw SplitPoint::NoSplit(((unsigned int)(ptr-o)));
  52.     }
  53.     else
  54.     {
  55.             char* o = ptr;
  56.             while(strchr(mPoints(),*ptr)==NULL) ptr++;
  57.             std::cout << "Including : '" << SmartString(o,((unsigned int)(ptr-o))) << "'" << std::endl;
  58.             throw SplitPoint::Split(((unsigned int)(ptr-o)),o);
  59.     }
  60.     return 1;
  61. }
  62.  
  63. //SmartString.
  64. unsigned int SmartString::gBufferSize = 10;
  65.  
  66. void SmartString::Allocate( unsigned int len ) throw (Iterable::UnneededAllocation)
  67. {
  68.     char* nmStr = static_cast<char*>(memcpy( new char[ mAlloc = newSize(len) ], mStr, mUsed ));
  69.     delete mStr;
  70.     mStr = nmStr;
  71. }
  72.  
  73. SmartString SmartString::operator [] ( const Iterable::Slice& aSlice ){
  74.     try{
  75.         unsigned int lStart = aSlice.start(mUsed), lEnd = aSlice.end(mUsed);
  76.         return SmartString(mStr + lStart,lEnd - lStart);
  77.     } catch ( std::range_error ){
  78.         return SmartString("Invalid slice was provided.");
  79.     }
  80. }
  81.  
  82. int SmartString::operator [] ( char search )
  83. {
  84.     return ((int)strchr( mStr, search))-((int)search);
  85. }
  86.  
  87. char SmartString::operator [] ( int aIndice){
  88.     return mStr[SmartString::Slice(0,mUsed).start(aIndice)];
  89. }
  90.  
  91. SmartString::operator const char *() const
  92. {
  93.     return mStr;
  94. }
  95.  
  96. SmartString& SmartString::Empty()
  97. {
  98.     delete[] mStr;
  99.     mStr = new char[mAlloc = mUsed = 1];
  100.     mStr[0] = '\0';
  101.     return *this;
  102. }
  103.  
  104. const SmartArray<SmartString> SmartString::Explode( const SmartString::SplitPoint& sp )
  105. {
  106.     SmartArray<SmartString> ret;
  107.     for( char* ptr=mStr,*end=mStr+mUsed; ptr<end; ptr++)
  108.     {
  109.         try{ sp(ptr); }
  110.         catch( SmartString::SplitPoint::Split& lSplit )
  111.         {
  112.             ret.Append(lSplit());
  113.             ptr += static_cast<unsigned int>(lSplit);
  114.         }
  115.         catch( SmartString::SplitPoint::NoSplit& noSplit )
  116.         {
  117.             ptr += static_cast<unsigned int>(noSplit);
  118.         }
  119.     }
  120.     return ret;
  121. }
  122.  
  123. SmartString& SmartString::Append(const char* src)
  124. {
  125.     return Append(src,strlen(src));
  126. }
  127.  
  128. SmartString& SmartString::Append(const char* src, unsigned int aLen)
  129. {
  130.     try{
  131.         Allocate(aLen);
  132.     } catch ( SmartString::UnneededAllocation ){}
  133.     memcpy(mStr+mUsed-1,src,aLen);
  134.     mUsed += aLen;
  135.     mStr[mUsed-1] = '\0';
  136.     return *this;
  137. }
  138.  
  139. SmartString& SmartString::Append( const char src )
  140. {
  141.     return Append( &src, 1 );
  142. }
  143.  
  144. SmartString& SmartString::Insert(const char* src, int aWhere )
  145. {
  146.     return Insert(src,strlen(src),aWhere);
  147. }
  148.  
  149. SmartString& SmartString::Insert(const char* src, unsigned int len, int aWhere)
  150. {
  151.     try{ Allocate(len); }
  152.     catch( Iterable::UnneededAllocation ){}
  153.     aWhere = Iterable::Slice(0,aWhere).end(mUsed);
  154.     char* temp = static_cast<char*>(memcpy(new char[mUsed-aWhere],mStr + aWhere,mUsed-aWhere));
  155.     memcpy(mStr + aWhere,src,len);
  156.     memcpy(mStr + aWhere + len,temp,mUsed - aWhere);
  157.     mUsed += len;
  158.     delete [] temp;
  159.     return *this;
  160. }
  161.  
  162. SmartString& SmartString::Prepend( const char* src )
  163. {
  164.     return Prepend( src, strlen(src) );
  165. }
  166.  
  167. SmartString& SmartString::Prepend( const char* src, unsigned int len )
  168. {
  169.     SmartString temp(src);
  170.     temp.Append(mStr);
  171.     char* stemp = mStr;
  172.     mStr = temp.mStr;
  173.     temp.mStr = stemp;
  174.     mUsed = temp.mUsed;
  175.     mAlloc = temp.mAlloc;
  176.     return *this;
  177. }
  178.  
  179. SmartString::SmartString( const SmartString&& src )
  180. {
  181.     mStr = src.mStr;
  182.     mUsed = src.mUsed;
  183.     mAlloc = src.mAlloc;
  184. }
  185.  
  186. SmartString::SmartString( const SmartString& src):mStr(NULL)
  187. {
  188.     Empty().Append(src.mStr);
  189. }
  190.  
  191. SmartString::SmartString(const char* src):mStr(NULL)
  192. {
  193.     Empty().Append(src);
  194. }
  195.  
  196. SmartString::SmartString(const char* src,unsigned int aLen):mStr(NULL)
  197. {
  198.     Empty().Append(src,aLen);
  199. }
  200.  
  201. SmartString::SmartString(void):mStr(NULL)
  202. {
  203.     Empty();
  204. }
  205.  
  206. SmartString::~SmartString()
  207. {
  208.     delete [] mStr;
  209. }
  210.  
  211. int SmartString::Similar ( const char* src )
  212. {
  213.     char* ptr = mStr;
  214.     do
  215.     {
  216.         while(isspace(*ptr))ptr++;
  217.         while(isspace(*src))src++;
  218.         if( toupper(*ptr) ^ toupper(*src) )
  219.             return -1 * ( ptr - mStr );
  220.     } while(*++ptr&&*++src);
  221.     return 1;
  222. }
  223.  
  224. unsigned int SmartString::Contains( const char* src, bool aSingle ) const
  225. {
  226.     unsigned int ret = 0;
  227.     //if(aSingle){
  228.     for(unsigned int i = 0; i<mUsed; i++){
  229.         for( char* ptr = ((char*)src);*ptr;ptr++){
  230.             if( toupper(*ptr) ^ toupper(mStr[i]) ) continue;
  231.             ret++;
  232.             break;
  233.         }
  234.     }
  235.     //} else if{
  236.  
  237.     //}
  238.     return ret;
  239. }
  240.  
  241. bool SmartString::operator==( const char* src ) const
  242. {
  243.     for( char* ptr = mStr; *ptr&&*src; ptr++,src++ )
  244.         if(*ptr^*src) return false;
  245.     return true;
  246. }
  247.  
  248. void SmartString::ImportFromFile( std::istream& input )
  249. {
  250.     Empty();
  251.     unsigned int len;
  252.     input.read((char*)&len,4);
  253.     Allocate(len);
  254.     input.read(mStr,mUsed=len);
  255. }
  256.  
  257. void SmartString::ExportToFile( std::ostream& output )
  258. {
  259.     output.write( (char*)&mUsed, 4);
  260.     output.write( mStr, mUsed);
  261. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement