Usermode/libc++ - Starting on a hacky STL implementation
[tpg/acess2.git] / Usermode / Libraries / libc++.so_src / include_exp / string
1 /*
2  * Acess2 C++ Library
3  * - By John Hodge (thePowersGang)
4  *
5  * string (header)
6  * - C++'s String type
7  */
8 #ifndef _LIBCXX_STRING_
9 #define _LIBCXX_STRING_
10
11 #include <allocator>
12
13 namespace std {
14
15 template <class charT>
16 struct char_traits
17 {
18 };
19
20 template <>
21 struct char_traits<char>
22 {
23         typedef char    char_type;
24         typedef int     int_type;
25         //typedef streamoff     off_type;
26         //typedef streampos     pos_type;
27         //typedef mbstate_t     state_type;
28         
29         static bool eq(const char_type& c, const char_type& d) {
30                 return c == d;
31         }
32         static bool lt(const char_type& c, const char_type& d) {
33                 return c < d;
34         }
35         static size_t length(const char_type* s) {
36                 size_t  ret = 0;
37                 while(*s++)     ret ++;
38                 return ret;
39         }
40 };
41
42 template <>
43 struct char_traits<wchar_t>
44 {
45         typedef wchar_t char_type;
46         typedef int     int_type;
47         //typedef streamoff     off_type;
48         //typedef streampos     pos_type;
49         //typedef mbstate_t     state_type;
50         
51         static size_t length(const char_type* s) {
52                 size_t  ret = 0;
53                 while(*s++)     ret ++;
54                 return ret;
55         }
56         static bool eq(const char_type& c, const char_type& d) {
57                 return c == d;
58         }
59         static bool lt(const char_type& c, const char_type& d) {
60                 return c < d;
61         }
62 };
63
64 template < class charT, class traits=char_traits<charT>, class Alloc=allocator<charT> >
65 class basic_string;
66
67 typedef basic_string<char>      string;
68
69 };      // namespace std
70
71 #include <stdexcept>
72
73 namespace std {
74
75 template < class charT, class traits, class Alloc >
76 class basic_string
77 {
78 public:
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;
85
86         basic_string(const allocator_type& alloc = allocator_type()):
87                 m_allocator(alloc),
88                 m_capacity(0),
89                 m_length(0),
90                 m_data(0)
91         {
92         }
93         basic_string(const basic_string& str):
94                 basic_string(str, allocator_type())
95         {
96         }
97         basic_string(const basic_string& str, const allocator_type& alloc):
98                 basic_string(str, 0, str.length(), alloc)
99         {
100         }
101         basic_string(const basic_string& str, size_type pos, size_type len = npos, const allocator_type& alloc = allocator_type()):
102                 basic_string(alloc)
103         {
104                 if( pos < str.length() )
105                 {
106                         if( len > str.length() - pos )
107                                 len = str.length() - pos;
108                         reserve(len);
109                         for( size_type i = 0; i < len; i ++ )
110                                 m_data[i] = str.m_data[pos+i];
111                         m_length = len;
112                 }
113         }
114         basic_string(const charT *s, const allocator_type& alloc = allocator_type()):
115                 basic_string(s, traits::length(s), alloc)
116         {
117         }
118         basic_string(const charT *s, size_type n, const allocator_type& alloc = allocator_type()):
119                 basic_string(alloc)
120         {
121                 if( n > 0 )
122                 {
123                         reserve(n);
124                         for( size_type i = 0; i < n; i ++ )
125                                 m_data[i] = s[i];
126                         m_length = n;
127                 }
128         }
129         basic_string(size_type n, charT c, const allocator_type& alloc = allocator_type()):
130                 basic_string(alloc)
131         {
132                 if( n > 0 )
133                 {
134                         reserve(n);
135                         for( size_type i = 0; i < n; i ++ )
136                                 m_data[i] = c;
137                         m_length = n;
138                 }
139         }
140         
141         // iterators
142         
143         // capacity
144         size_type size() const {
145                 return m_size;
146         }
147         size_type length() const {
148                 return size();
149         }
150         size_type max_size() const {
151                 return -1;
152         }
153         void resize(size_type size, charT c = 0) {
154                 reserve(size);
155                 if( m_size < size ) {
156                         for( size_type ofs = m_size; ofs < size; ofs ++ )
157                                 m_data[ofs] = c;
158                 }
159                 m_size = size;
160                 m_data[m_size] = 0;
161         }
162         size_type capacity() const {
163                 return m_capacity;
164         }
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);
172                         m_data = new_area;
173                         m_capacity = size;
174                 }
175         }
176         void clear() {
177                 m_size = 0;
178         }
179         bool empty() const {
180                 return length() == 0;
181         }
182         
183         // Access
184         reference operator[] (size_type pos) {
185                 return m_data[pos];
186         }
187         const_reference operator[] (size_type pos) const {
188                 return m_data[pos];
189         }
190         reference at(size_type pos) {
191                 if(pos >= m_size)       throw ::std::out_of_range("basic_string - at");
192                 return m_data[pos];
193         }
194         const_reference at(size_type pos) const {
195                 if(pos >= m_size)       throw ::std::out_of_range("basic_string - at");
196                 return m_data[pos];
197         }
198         
199         // Modifiers
200         basic_string& operator +=(const basic_string& str);
201         basic_string& operator +=(const charT* s);
202         basic_string& operator +=(charT c);
203         
204         static const size_type npos = -1;
205 private:
206         allocator_type  m_allocator;
207         size_type       m_capacity;
208         size_type       m_size;
209         size_type       m_length;
210         typename allocator_type::pointer m_data;
211 };
212
213 };
214
215 #include <stdexcept>
216
217 #endif
218
219 // vim: ft=cpp

UCC git Repository :: git.ucc.asn.au