From: <bl...@us...> - 2003-04-26 12:09:47
|
Update of /cvsroot/cpptool/rfta/include/xtl In directory sc8-pr-cvs1:/tmp/cvs-serv12839/include/xtl Added Files: CStringView.h Log Message: * added const char * string wrapper and iterator (still need adjustment). --- NEW FILE: CStringView.h --- #ifndef XTL_CSTRINGVIEW_H_INCLUDED #define XTL_CSTRINGVIEW_H_INCLUDED #include <stdexcept> #include <string> #include <cstring> #include <boost/operators.hpp> namespace Xtl { class CStringView; class CStringEnumerator; class CStringView { public: CStringView() : start_( 0 ) , end_( 0 ) { } CStringView( const char *start, const char *end ) : start_( end ? start : 0 ) , end_( start ? end : 0 ) { } CStringView getSlice( int startIndex, int endIndex = -1 ) const { if ( startIndex > end_ - start_ || startIndex < 0 ) throw std::out_of_range( "CStringView::getSlice(), startIndex" ); if ( endIndex == -1 ) endIndex = getLength(); if ( endIndex > end_ - start_ ) throw std::out_of_range( "CStringView::getSlice(), endIndex" ); return CStringView( start_ + startIndex, start_ + endIndex ); } CStringEnumerator enumerate() const; CStringEnumerator enumerate( int startIndex, int endIndex ) const; const char *getStart() const { return start_; } const char *getEnd() const { return end_; } int getLength() const { return end_ - start_; } bool isEmpty() const { return getLength() == 0; } bool operator ==( const CStringView &other ) const { return getLength() == other.getLength() && ( ( start_ == other.start_ && end_ == other.end_ ) || std::memcmp( start_, other.start_, getLength() ) == 0 ); } bool operator !=( const CStringView &other ) const { return !( *this == other ); } bool operator ==( const std::string &other ) const { return (start_ && other == std::string( start_, getLength() ) ) || other.empty(); } bool operator !=( const std::string &other ) const { return !( *this == other ); } private: const char *start_; const char *end_; }; class CStringEnumerator : public boost::random_access_iterator_helper<CStringEnumerator , char , std::ptrdiff_t , const char * , char> { public: typedef CStringEnumerator self; typedef char Reference; typedef std::ptrdiff_t Distance; CStringEnumerator() : current_( 0 ) { } CStringEnumerator( const char *current, const char *end ) : string_( current, end ) , current_( string_.isEmpty() ? 0 : current ) { } CStringEnumerator( const CStringView &string ) : string_( string ) , current_( string_.getStart() ) { } int getRemainingLength() const { return string_.getEnd() - current_; } char operator*() const { return getCurrent(); } self &operator ++() { ++current_; return *this; } self &operator --() { --current_; return *this; } self &operator +=( Distance n ) { current_ += n; return *this; } self &operator -=( Distance n ) { current_ -= n; return *this; } bool operator ==( const self &other ) const { return current_ == other.current_; } bool operator <( const self &other ) const { return current_ < other.current_; } char getNext() { if ( current_ < string_.getEnd() ) return *current_++; return 0; } char getCurrent() const { if ( current_ < string_.getEnd() ) return *current_; return 0; } bool hasNext() const { return current_ < string_.getEnd(); } const char *getCurrentPos() const { return current_; } const CStringView &getString() const { return string_; } private: CStringView string_; const char *current_; }; inline CStringEnumerator::Distance operator -( const CStringEnumerator &x, const CStringEnumerator &y ) { return x.getCurrentPos() - y.getCurrentPos(); } /* class CStringBackEnumerator { public: CStringBackEnumerator() : current_( 0 ) , end_( 0 ) { } CStringBackEnumerator( const char *current, const char *start ) : current_( start ? current : 0 ) , start_( start ? start : current ) { } CStringBackEnumerator( const CStringView &cstringView ); int getRemainingLength() const { return current_ - start_; } char getNext() { if ( current_ >= start_ ) return *current_--; return 0; } char getCurrent() const { if ( current_ >= start_ ) return *current_; return 0; } bool hasNext() const { return current_ >= start_; } const char *getCurrentPos() const { return current_; } const char *getStartPos() const { return start_; } private: const char *current_; const char *start_; }; */ inline bool operator ==( const std::string &s1, const CStringView &s2 ) { return s2 == s1; } inline bool operator !=( const std::string &s1, const CStringView &s2 ) { return s2 != s1; } inline CStringEnumerator CStringView::enumerate() const { return CStringEnumerator( *this ); } inline CStringEnumerator CStringView::enumerate( int startIndex, int endIndex ) const { if ( start_ ) return CStringEnumerator( start_ + startIndex, start_ + endIndex ); return CStringEnumerator(); } } // namespace Xtl #endif // XTL_CSTRINGVIEW_H_INCLUDED |