home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1998 / MacHack 1998.toast / Sessions / Completions / Completions Source / Data / Data.h < prev   
Encoding:
Text File  |  1998-06-18  |  2.3 KB  |  73 lines  |  [TEXT/CWIE]

  1. // Data.h
  2.  
  3. #ifndef Data_h
  4. #define Data_h
  5.  
  6. #ifndef ConstData_h
  7. #include "ConstData.h"
  8. #endif
  9.  
  10. /*
  11.    A Data is a range of bytes, starting at Start() and
  12.    not including End().
  13. */
  14.  
  15. class Data
  16.   {
  17.     private:
  18.         uint8 *start;
  19.         uint8 *end;
  20.     
  21.     public:
  22.         Data()        : start(0), end(0)    {}
  23.  
  24.         Data( void *theStart, void *theEnd )
  25.           : start( static_cast<uint8 *>( theStart ) ),
  26.              end( static_cast<uint8 *>( theEnd ) )
  27.           {
  28.             Assert( theEnd >= theStart );
  29.             Assert( theStart != 0 || theEnd == 0 );
  30.           }
  31.         
  32.         Data( void *theStart, uint32 theLength )
  33.           : start( static_cast<uint8 *>( theStart ) ),
  34.              end( static_cast<uint8 *>( theStart ) + theLength )
  35.           {
  36.             Assert( theStart != 0 || theLength == 0 );
  37.           }
  38.  
  39.         uint8 *Start() const                                        { return start; }
  40.         uint8 *End() const                                        { return end; }
  41.  
  42.         uint32 Length() const                                    { return end - start; }
  43.                 
  44.         uint32 BoundedLength( uint32 bound ) const        { return ( Length() <= bound ) ? Length() : bound; }
  45.  
  46.         bool IsEmpty() const                                        { return start == end; }
  47.         bool Null() const                                            { return start == 0; }
  48.         
  49.         uint8& operator[]( uint32 i ) const                    { Assert( i < Length() ); return start[i]; }
  50.         
  51.         Data Head( uint32 position ) const                    { Assert( position <= Length() ); return Data( start, start+position ); }
  52.         Data Tail( uint32 position ) const                    { Assert( position <= Length() ); return Data( start+position, end ); }
  53.         Data Middle( uint32 s, uint32 e ) const            { Assert( s <= e );  Assert( e <= Length() ); return Data( start+s, start+e ); }
  54.         
  55.         void Truncate( uint32 position )                        { Assert( position <= Length() ); end = start + position; }
  56.         void LimitLength( uint32 limit )                        { if ( limit < Length() )  end = start + limit; }
  57.         
  58.         void Shorten( uint32 amount )                            { Assert( amount <= Length() ); start += amount; }
  59.         void Lengthen( uint32 amount )                        { end += amount; }
  60.         
  61.         operator ConstData() const                                { return ConstData( start, end ); }
  62.         
  63.         bool StartsWith( ConstData d ) const                { return ConstData( start, end ).StartsWith( d ); }
  64.         bool EndsWith( ConstData d ) const                    { return ConstData( start, end ).EndsWith( d ); }
  65.         bool Contains( ConstData d ) const                    { return ConstData( start, end ).Contains( d ); }
  66.         
  67.         bool Contains( const void *p ) const                { return start <= p && p < end; }
  68.         
  69.         Data operator<<( ConstData r ) const;                // returns unused space, so chaining works
  70.   };
  71.  
  72. #endif
  73.