home *** CD-ROM | disk | FTP | other *** search
Text File | 1998-06-18 | 2.3 KB | 73 lines | [TEXT/CWIE] |
- // Data.h
-
- #ifndef Data_h
- #define Data_h
-
- #ifndef ConstData_h
- #include "ConstData.h"
- #endif
-
- /*
- A Data is a range of bytes, starting at Start() and
- not including End().
- */
-
- class Data
- {
- private:
- uint8 *start;
- uint8 *end;
-
- public:
- Data() : start(0), end(0) {}
-
- Data( void *theStart, void *theEnd )
- : start( static_cast<uint8 *>( theStart ) ),
- end( static_cast<uint8 *>( theEnd ) )
- {
- Assert( theEnd >= theStart );
- Assert( theStart != 0 || theEnd == 0 );
- }
-
- Data( void *theStart, uint32 theLength )
- : start( static_cast<uint8 *>( theStart ) ),
- end( static_cast<uint8 *>( theStart ) + theLength )
- {
- Assert( theStart != 0 || theLength == 0 );
- }
-
- uint8 *Start() const { return start; }
- uint8 *End() const { return end; }
-
- uint32 Length() const { return end - start; }
-
- uint32 BoundedLength( uint32 bound ) const { return ( Length() <= bound ) ? Length() : bound; }
-
- bool IsEmpty() const { return start == end; }
- bool Null() const { return start == 0; }
-
- uint8& operator[]( uint32 i ) const { Assert( i < Length() ); return start[i]; }
-
- Data Head( uint32 position ) const { Assert( position <= Length() ); return Data( start, start+position ); }
- Data Tail( uint32 position ) const { Assert( position <= Length() ); return Data( start+position, end ); }
- Data Middle( uint32 s, uint32 e ) const { Assert( s <= e ); Assert( e <= Length() ); return Data( start+s, start+e ); }
-
- void Truncate( uint32 position ) { Assert( position <= Length() ); end = start + position; }
- void LimitLength( uint32 limit ) { if ( limit < Length() ) end = start + limit; }
-
- void Shorten( uint32 amount ) { Assert( amount <= Length() ); start += amount; }
- void Lengthen( uint32 amount ) { end += amount; }
-
- operator ConstData() const { return ConstData( start, end ); }
-
- bool StartsWith( ConstData d ) const { return ConstData( start, end ).StartsWith( d ); }
- bool EndsWith( ConstData d ) const { return ConstData( start, end ).EndsWith( d ); }
- bool Contains( ConstData d ) const { return ConstData( start, end ).Contains( d ); }
-
- bool Contains( const void *p ) const { return start <= p && p < end; }
-
- Data operator<<( ConstData r ) const; // returns unused space, so chaining works
- };
-
- #endif
-