| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
#if !defined( INCLUDED_STREAM_STRINGSTREAM_H ) |
| 23 |
#define INCLUDED_STREAM_STRINGSTREAM_H |
| 24 |
|
| 25 |
#include "itextstream.h" |
| 26 |
#include "string/string.h" |
| 27 |
#include <vector> |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
class StringBuffer |
| 34 |
{ |
| 35 |
std::vector<char> m_string; |
| 36 |
public: |
| 37 |
StringBuffer(){ |
| 38 |
m_string.push_back( '\0' ); |
| 39 |
} |
| 40 |
explicit StringBuffer( std::size_t capacity ){ |
| 41 |
m_string.reserve( capacity ); |
| 42 |
m_string.push_back( '\0' ); |
| 43 |
} |
| 44 |
explicit StringBuffer( const char* string ) : m_string( string, string + string_length( string ) + 1 ){ |
| 45 |
} |
| 46 |
|
| 47 |
typedef std::vector<char>::iterator iterator; |
| 48 |
typedef std::vector<char>::const_iterator const_iterator; |
| 49 |
|
| 50 |
iterator begin(){ |
| 51 |
return m_string.begin(); |
| 52 |
} |
| 53 |
const_iterator begin() const { |
| 54 |
return m_string.begin(); |
| 55 |
} |
| 56 |
iterator end(){ |
| 57 |
return m_string.end() - 1; |
| 58 |
} |
| 59 |
const_iterator end() const { |
| 60 |
return m_string.end() - 1; |
| 61 |
} |
| 62 |
|
| 63 |
void push_back( char c ){ |
| 64 |
m_string.insert( end(), c ); |
| 65 |
} |
| 66 |
void pop_back(){ |
| 67 |
m_string.erase( end() - 1 ); |
| 68 |
} |
| 69 |
void push_range( const char* first, const char* last ){ |
| 70 |
m_string.insert( end(), first, last ); |
| 71 |
} |
| 72 |
void push_string( const char* string ){ |
| 73 |
push_range( string, string + string_length( string ) ); |
| 74 |
} |
| 75 |
char* c_str(){ |
| 76 |
return &( *m_string.begin() ); |
| 77 |
} |
| 78 |
const char* c_str() const { |
| 79 |
return &( *m_string.begin() ); |
| 80 |
} |
| 81 |
|
| 82 |
char& back(){ |
| 83 |
return *( end() - 1 ); |
| 84 |
} |
| 85 |
const char& back() const { |
| 86 |
return *( end() - 1 ); |
| 87 |
} |
| 88 |
bool empty() const { |
| 89 |
return m_string.size() == 1; |
| 90 |
} |
| 91 |
void clear(){ |
| 92 |
m_string.clear(); |
| 93 |
m_string.push_back( '\0' ); |
| 94 |
} |
| 95 |
}; |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
class StringOutputStream : public TextOutputStream |
| 100 |
{ |
| 101 |
StringBuffer m_string; |
| 102 |
public: |
| 103 |
typedef StringBuffer::iterator iterator; |
| 104 |
typedef StringBuffer::const_iterator const_iterator; |
| 105 |
|
| 106 |
StringOutputStream(){ |
| 107 |
} |
| 108 |
explicit StringOutputStream( std::size_t capacity ) : m_string( capacity ){ |
| 109 |
} |
| 110 |
std::size_t write( const char* buffer, std::size_t length ){ |
| 111 |
m_string.push_range( buffer, buffer + length ); |
| 112 |
return length; |
| 113 |
} |
| 114 |
|
| 115 |
iterator begin(){ |
| 116 |
return m_string.begin(); |
| 117 |
} |
| 118 |
const_iterator begin() const { |
| 119 |
return m_string.begin(); |
| 120 |
} |
| 121 |
iterator end(){ |
| 122 |
return m_string.end(); |
| 123 |
} |
| 124 |
const_iterator end() const { |
| 125 |
return m_string.end(); |
| 126 |
} |
| 127 |
|
| 128 |
bool empty() const { |
| 129 |
return m_string.empty(); |
| 130 |
} |
| 131 |
char* c_str(){ |
| 132 |
return m_string.c_str(); |
| 133 |
} |
| 134 |
const char* c_str() const { |
| 135 |
return m_string.c_str(); |
| 136 |
} |
| 137 |
void clear(){ |
| 138 |
m_string.clear(); |
| 139 |
} |
| 140 |
}; |
| 141 |
|
| 142 |
template<typename T> |
| 143 |
inline StringOutputStream& operator<<( StringOutputStream& ostream, const T& t ){ |
| 144 |
return ostream_write( ostream, t ); |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
#endif |