Advertisement
thenuke321

Sstring.h

Nov 28th, 2014
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.67 KB | None | 0 0
  1. /*****************************************************************************
  2. *
  3. *  PROJECT:     Multi Theft Auto v1.0
  4. *  LICENSE:     See LICENSE in the top level directory
  5. *  FILE:        SString.h
  6. *  PURPOSE:
  7. *  DEVELOPERS:  ccw <chris@codewave.co.uk>
  8. *               Alberto Alonso <rydencillo@gmail.com>
  9. *
  10. *  Multi Theft Auto is available from http://www.multitheftauto.com/
  11. *
  12. *****************************************************************************/
  13.  
  14. #pragma once
  15.  
  16. //
  17. // class SString
  18. //
  19. // Adds some functionality to the std::string class
  20. //
  21. #include <stdarg.h>
  22.  
  23. #ifdef WIN32
  24. #ifndef va_copy
  25.     #define va_copy(dest, orig) (dest) = (orig)
  26. #endif
  27. #endif
  28.  
  29. class SString : public std::string
  30. {
  31. public:
  32.     // Constructors
  33.     SString ( )
  34.         : std::string ()
  35.     { }
  36.  
  37.     SString ( const char* szText )
  38.         : std::string ( szText ? szText : "" )
  39.     { }
  40.  
  41.     explicit SString ( const char* szFormat, ... )
  42.         : std::string ()
  43.     {
  44.         if ( szFormat )
  45.         {
  46.             va_list vl;
  47.  
  48.             va_start ( vl, szFormat );
  49.             vFormat ( szFormat, vl );
  50.             va_end ( vl );
  51.         }
  52.     }
  53.  
  54.     SString ( const std::string& strText )
  55.         : std::string ( strText )
  56.     { }
  57.  
  58.  
  59.     SString& Format ( const char* szFormat, ... )
  60.     {
  61.         va_list vl;
  62.  
  63.         va_start ( vl, szFormat );
  64.         SString& str = vFormat ( szFormat, vl );
  65.         va_end ( vl );
  66.  
  67.         return str;
  68.     }
  69.  
  70.     SString& vFormat ( const char* szFormat, va_list vl );
  71.     void OnFormatException ( const char* szFormat );
  72.  
  73.     // Access
  74.     char& operator[]( int iOffset )
  75.     {
  76.         return std::string::operator[]( iOffset );
  77.     }
  78.  
  79.     // Operators  
  80.     SString operator+( const char* other ) const
  81.     {
  82.         return std::string ( *this ) + other;
  83.     }
  84.     SString operator+( const std::string& other ) const
  85.     {
  86.         return std::string ( *this ) + other;
  87.     }
  88.     SString operator+( const SString& other ) const
  89.     {
  90.         return std::string ( *this ) + other;
  91.     }
  92.  
  93.     // Assignment  
  94.     operator const char*() const    { return c_str (); }        // Auto assign to const char* without using c_str()
  95.     const char* operator*( void ) const
  96.     {
  97.         return c_str ();
  98.     }
  99.  
  100.     // Functions
  101.     void            Split               ( const SString& strDelim, std::vector < SString >& outResult, unsigned int uiMaxAmount = 0, unsigned int uiMinAmount = 0 ) const;
  102.     bool            Split               ( const SString& strDelim, SString* pstrLeft, SString* pstrRight, int iIndex = 1 ) const;
  103.     SString         SplitLeft           ( const SString& strDelim, SString* pstrRight = NULL, int iIndex = 1 ) const;
  104.     SString         SplitRight          ( const SString& strDelim, SString* pstrLeft = NULL, int iIndex = 1 ) const;
  105.     SString         Replace             ( const char* szOld, const char* szNew, bool bSearchJustReplaced = false ) const;
  106.     SString         ReplaceI            ( const char* szOld, const char* szNew, bool bSearchJustReplaced = false ) const;
  107.     SString         TrimStart           ( const char* szOld ) const;
  108.     SString         TrimEnd             ( const char* szOld ) const;
  109.     SString         ToLower             ( void ) const;
  110.     SString         ToUpper             ( void ) const;
  111.     SString         ConformLineEndings  ( void ) const;
  112.     bool            Contains            ( const SString& strOther ) const;
  113.     bool            ContainsI           ( const SString& strOther ) const;
  114.     bool            CompareI            ( const SString& strOther ) const;
  115.     SString         SubStr              ( int iPos, int iCount = 0x3fffffff ) const;
  116.     SString         Left                ( int iCount ) const;
  117.     SString         Right               ( int iCount ) const;
  118.     bool            EndsWith            ( const SString& strOther ) const;
  119.     bool            EndsWithI           ( const SString& strOther ) const;
  120.     bool            BeginsWith          ( const SString& strOther ) const;
  121.     bool            BeginsWithI         ( const SString& strOther ) const;
  122.     static SString  Join                ( const SString& strDelim, const std::vector < SString >& parts, int iFirst = 0, int iCount = 0x3fffffff );
  123.     void            AssignLeft          ( const char* szOther, uint uiMaxLength );
  124. };
  125.  
  126.  
  127. class SStringX : public SString
  128. {
  129. public:
  130.     SStringX ( const char* szText )
  131.         : SString ( std::string ( szText ? szText : "" ) )
  132.     { }
  133.     SStringX ( const char* szText, uint uiLength )
  134.         : SString ( std::string ( szText ? szText : "", uiLength ) )
  135.     { }
  136. };
  137.  
  138.  
  139. //
  140. // SCharStringRef
  141. //
  142. // String reference - Used for direct access to Lua strings
  143. //
  144. struct SCharStringRef
  145. {
  146.     SCharStringRef ( void ) : pData ( NULL ),  uiSize ( 0 ) {}
  147.     char* pData;
  148.     size_t uiSize;
  149. };
  150.  
  151.  
  152. //
  153. // Faster type of SString::Split
  154. // Uses pointers to a big buffer rather than an array of strings
  155. //
  156. template < class STRING_TYPE, class CHAR_TYPE >
  157. class TSplitString : public std::vector < const CHAR_TYPE* >
  158. {
  159. public:
  160.     TSplitString ( void ) {}
  161.     TSplitString ( const STRING_TYPE& strInput, const STRING_TYPE& strDelim, unsigned int uiMaxAmount = 0, unsigned int uiMinAmount = 0 )
  162.     {
  163.         Split ( strInput, strDelim, uiMaxAmount, uiMinAmount );
  164.     }
  165.  
  166.     void Split ( const STRING_TYPE& strInput, const STRING_TYPE& strDelim, unsigned int uiMaxAmount = 0, unsigned int uiMinAmount = 0 )
  167.     {
  168.         // Copy string to buffer
  169.         uint iInputLength = strInput.length ();
  170.         buffer.resize ( iInputLength + 1 );
  171.         memcpy ( &buffer[0], &strInput[0], ( iInputLength + 1 ) * sizeof ( CHAR_TYPE ) );
  172.  
  173.         // Prime result list
  174.         this->clear ();
  175.         this->reserve ( 16U < uiMaxAmount ? 16U : uiMaxAmount );
  176.  
  177.         // Split into pointers
  178.         size_t ulCurrentPoint = 0;
  179.         while ( true )
  180.         {
  181.             size_t ulPos = strInput.find ( strDelim, ulCurrentPoint );
  182.             if ( ulPos == STRING_TYPE::npos || ( uiMaxAmount > 0 && uiMaxAmount <= this->size () + 1 ) )
  183.             {
  184.                 if ( ulCurrentPoint <= strInput.length () )
  185.                     push_back ( &buffer[ ulCurrentPoint ] );
  186.                 break;
  187.             }
  188.             push_back ( &buffer[ ulCurrentPoint ] );
  189.             buffer[ ulPos ] = 0;
  190.             ulCurrentPoint = ulPos + strDelim.length ();
  191.         }
  192.         while ( this->size () < uiMinAmount )
  193.             push_back ( &buffer[ iInputLength ] );        
  194.     }
  195.  
  196. protected:
  197.     std::vector < CHAR_TYPE > buffer;
  198. };
  199.  
  200.  
  201. typedef TSplitString < std::string, char >      CSplitString;
  202. typedef TSplitString < std::wstring, wchar_t >  CSplitStringW;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement