3 * - By John Hodge (thePowersGang)
8 #ifndef _LIBCXX_STRING_
9 #define _LIBCXX_STRING_
15 template <class charT>
21 struct char_traits<char>
23 typedef char char_type;
25 //typedef streamoff off_type;
26 //typedef streampos pos_type;
27 //typedef mbstate_t state_type;
29 static bool eq(const char_type& c, const char_type& d) {
32 static bool lt(const char_type& c, const char_type& d) {
35 static size_t length(const char_type* s) {
43 struct char_traits<wchar_t>
45 typedef wchar_t char_type;
47 //typedef streamoff off_type;
48 //typedef streampos pos_type;
49 //typedef mbstate_t state_type;
51 static size_t length(const char_type* s) {
56 static bool eq(const char_type& c, const char_type& d) {
59 static bool lt(const char_type& c, const char_type& d) {
64 template < class charT, class traits=char_traits<charT>, class Alloc=allocator<charT> >
67 typedef basic_string<char> string;
75 template < class charT, class traits, class Alloc >
79 typedef traits traits_type;
80 typedef Alloc allocator_type;
81 typedef charT value_type;
82 typedef typename allocator_type::reference reference;
83 typedef typename allocator_type::const_reference const_reference;
84 typedef size_t size_type;
86 basic_string(const allocator_type& alloc = allocator_type()):
93 basic_string(const basic_string& str):
94 basic_string(str, allocator_type())
97 basic_string(const basic_string& str, const allocator_type& alloc):
98 basic_string(str, 0, str.length(), alloc)
101 basic_string(const basic_string& str, size_type pos, size_type len = npos, const allocator_type& alloc = allocator_type()):
104 if( pos < str.length() )
106 if( len > str.length() - pos )
107 len = str.length() - pos;
109 for( size_type i = 0; i < len; i ++ )
110 m_data[i] = str.m_data[pos+i];
114 basic_string(const charT *s, const allocator_type& alloc = allocator_type()):
115 basic_string(s, traits::length(s), alloc)
118 basic_string(const charT *s, size_type n, const allocator_type& alloc = allocator_type()):
124 for( size_type i = 0; i < n; i ++ )
129 basic_string(size_type n, charT c, const allocator_type& alloc = allocator_type()):
135 for( size_type i = 0; i < n; i ++ )
144 size_type size() const {
147 size_type length() const {
150 size_type max_size() const {
153 void resize(size_type size, charT c = 0) {
155 if( m_size < size ) {
156 for( size_type ofs = m_size; ofs < size; ofs ++ )
162 size_type capacity() const {
165 void reserve(size_type size) {
166 size = (size+1 + 31) & ~31;
167 if( size > m_capacity ) {
168 auto new_area = m_allocator.allocate(size);
169 for( size_type i = 0; i < m_length; i ++ )
170 new_area[i] = m_data[i];
171 m_allocator.deallocate(m_data, m_capacity);
180 return length() == 0;
184 reference operator[] (size_type pos) {
187 const_reference operator[] (size_type pos) const {
190 reference at(size_type pos) {
191 if(pos >= m_size) throw ::std::out_of_range("basic_string - at");
194 const_reference at(size_type pos) const {
195 if(pos >= m_size) throw ::std::out_of_range("basic_string - at");
200 basic_string& operator +=(const basic_string& str);
201 basic_string& operator +=(const charT* s);
202 basic_string& operator +=(charT c);
204 static const size_type npos = -1;
206 allocator_type m_allocator;
207 size_type m_capacity;
210 typename allocator_type::pointer m_data;