| 1 | #ifndef CONST_STRING_HPP
|
|---|
| 2 | #define CONST_STRING_HPP
|
|---|
| 3 |
|
|---|
| 4 | #include <string>
|
|---|
| 5 |
|
|---|
| 6 | // ************************************************************************** //
|
|---|
| 7 | // ************** const_string ************** //
|
|---|
| 8 | // ************************************************************************** //
|
|---|
| 9 |
|
|---|
| 10 | class const_string {
|
|---|
| 11 | public:
|
|---|
| 12 | // Constructors
|
|---|
| 13 | const_string()
|
|---|
| 14 | : m_begin( "" ), m_end( m_begin ) {}
|
|---|
| 15 |
|
|---|
| 16 | const_string( const std::string& s )
|
|---|
| 17 | : m_begin( s.c_str() ),
|
|---|
| 18 | m_end( m_begin + s.length() ) {}
|
|---|
| 19 |
|
|---|
| 20 | const_string( char const* s )
|
|---|
| 21 | : m_begin(s), m_end(s + (s?strlen(s):0))
|
|---|
| 22 | { if( is_empty()) erase(); } // avoids problems with data() if s == NULL
|
|---|
| 23 |
|
|---|
| 24 | const_string( char const* s, size_t length )
|
|---|
| 25 | : m_begin( s ), m_end( m_begin + length )
|
|---|
| 26 | { if( length == 0 ) erase(); }
|
|---|
| 27 |
|
|---|
| 28 | const_string( char const* first, char const* last )
|
|---|
| 29 | : m_begin( first ), m_end( last ) {}
|
|---|
| 30 |
|
|---|
| 31 | // public members
|
|---|
| 32 | char const* data() const { return m_begin; }
|
|---|
| 33 | size_t length() const { return m_end - m_begin; }
|
|---|
| 34 | bool is_empty() const { return m_end == m_begin; }
|
|---|
| 35 | void erase() { m_begin = m_end = ""; }
|
|---|
| 36 |
|
|---|
| 37 | private:
|
|---|
| 38 |
|
|---|
| 39 | // Data members
|
|---|
| 40 | char const* m_begin;
|
|---|
| 41 | char const* m_end;
|
|---|
| 42 |
|
|---|
| 43 | };
|
|---|
| 44 |
|
|---|
| 45 | #endif
|
|---|