Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1 | // -*- C++ -*- |
| 2 | //===--------------------------- string -----------------------------------===// |
| 3 | // |
Howard Hinnant | f5256e1 | 2010-05-11 21:36:01 +0000 | [diff] [blame] | 4 | // The LLVM Compiler Infrastructure |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 5 | // |
| 6 | // This file is distributed under the University of Illinois Open Source |
| 7 | // License. See LICENSE.TXT for details. |
| 8 | // |
| 9 | //===----------------------------------------------------------------------===// |
| 10 | |
| 11 | #ifndef _LIBCPP_STRING |
| 12 | #define _LIBCPP_STRING |
| 13 | |
| 14 | /* |
| 15 | string synopsis |
| 16 | |
| 17 | namespace std |
| 18 | { |
| 19 | |
| 20 | template <class stateT> |
| 21 | class fpos |
| 22 | { |
| 23 | private: |
| 24 | stateT st; |
| 25 | public: |
| 26 | fpos(streamoff = streamoff()); |
| 27 | |
| 28 | operator streamoff() const; |
| 29 | |
| 30 | stateT state() const; |
| 31 | void state(stateT); |
| 32 | |
| 33 | fpos& operator+=(streamoff); |
| 34 | fpos operator+ (streamoff) const; |
| 35 | fpos& operator-=(streamoff); |
| 36 | fpos operator- (streamoff) const; |
| 37 | }; |
| 38 | |
| 39 | template <class stateT> streamoff operator-(const fpos<stateT>& x, const fpos<stateT>& y); |
| 40 | |
| 41 | template <class stateT> bool operator==(const fpos<stateT>& x, const fpos<stateT>& y); |
| 42 | template <class stateT> bool operator!=(const fpos<stateT>& x, const fpos<stateT>& y); |
| 43 | |
| 44 | template <class charT> |
| 45 | struct char_traits |
| 46 | { |
| 47 | typedef charT char_type; |
| 48 | typedef ... int_type; |
| 49 | typedef streamoff off_type; |
| 50 | typedef streampos pos_type; |
| 51 | typedef mbstate_t state_type; |
| 52 | |
| 53 | static void assign(char_type& c1, const char_type& c2); |
| 54 | static bool eq(char_type c1, char_type c2); |
| 55 | static bool lt(char_type c1, char_type c2); |
| 56 | |
| 57 | static int compare(const char_type* s1, const char_type* s2, size_t n); |
| 58 | static size_t length(const char_type* s); |
| 59 | static const char_type* find(const char_type* s, size_t n, const char_type& a); |
| 60 | static char_type* move(char_type* s1, const char_type* s2, size_t n); |
| 61 | static char_type* copy(char_type* s1, const char_type* s2, size_t n); |
| 62 | static char_type* assign(char_type* s, size_t n, char_type a); |
| 63 | |
| 64 | static int_type not_eof(int_type c); |
| 65 | static char_type to_char_type(int_type c); |
| 66 | static int_type to_int_type(char_type c); |
| 67 | static bool eq_int_type(int_type c1, int_type c2); |
| 68 | static int_type eof(); |
| 69 | }; |
| 70 | |
| 71 | template <> struct char_traits<char>; |
| 72 | template <> struct char_traits<wchar_t>; |
| 73 | |
| 74 | template<class charT, class traits = char_traits<charT>, class Allocator = allocator<charT> > |
| 75 | class basic_string |
| 76 | { |
| 77 | public: |
| 78 | // types: |
| 79 | typedef traits traits_type; |
| 80 | typedef typename traits_type::char_type value_type; |
| 81 | typedef Allocator allocator_type; |
| 82 | typedef typename allocator_type::size_type size_type; |
| 83 | typedef typename allocator_type::difference_type difference_type; |
| 84 | typedef typename allocator_type::reference reference; |
| 85 | typedef typename allocator_type::const_reference const_reference; |
| 86 | typedef typename allocator_type::pointer pointer; |
| 87 | typedef typename allocator_type::const_pointer const_pointer; |
| 88 | typedef implementation-defined iterator; |
| 89 | typedef implementation-defined const_iterator; |
| 90 | typedef std::reverse_iterator<iterator> reverse_iterator; |
| 91 | typedef std::reverse_iterator<const_iterator> const_reverse_iterator; |
| 92 | |
| 93 | static const size_type npos = -1; |
| 94 | |
| 95 | explicit basic_string(const allocator_type& a = allocator_type()); |
| 96 | basic_string(const basic_string& str); |
| 97 | basic_string(basic_string&& str); |
Howard Hinnant | a6a062d | 2010-06-02 18:20:39 +0000 | [diff] [blame] | 98 | basic_string(const basic_string& str, size_type pos, size_type n = npos, |
| 99 | const allocator_type& a = allocator_type()); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 100 | basic_string(const_pointer s, const allocator_type& a = allocator_type()); |
| 101 | basic_string(const_pointer s, size_type n, const allocator_type& a = allocator_type()); |
| 102 | basic_string(size_type n, value_type c, const allocator_type& a = allocator_type()); |
| 103 | template<class InputIterator> |
Howard Hinnant | a6a062d | 2010-06-02 18:20:39 +0000 | [diff] [blame] | 104 | basic_string(InputIterator begin, InputIterator end, |
| 105 | const allocator_type& a = allocator_type()); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 106 | basic_string(initializer_list<value_type>, const Allocator& = Allocator()); |
| 107 | basic_string(const basic_string&, const Allocator&); |
| 108 | basic_string(basic_string&&, const Allocator&); |
| 109 | |
| 110 | ~basic_string(); |
| 111 | |
| 112 | basic_string& operator=(const basic_string& str); |
| 113 | basic_string& operator=(const_pointer s); |
| 114 | basic_string& operator=(value_type c); |
| 115 | basic_string& operator=(initializer_list<value_type>); |
| 116 | |
| 117 | iterator begin(); |
| 118 | const_iterator begin() const; |
| 119 | iterator end(); |
| 120 | const_iterator end() const; |
| 121 | |
| 122 | reverse_iterator rbegin(); |
| 123 | const_reverse_iterator rbegin() const; |
| 124 | reverse_iterator rend(); |
| 125 | const_reverse_iterator rend() const; |
| 126 | |
| 127 | const_iterator cbegin() const; |
| 128 | const_iterator cend() const; |
| 129 | const_reverse_iterator crbegin() const; |
| 130 | const_reverse_iterator crend() const; |
| 131 | |
| 132 | size_type size() const; |
| 133 | size_type length() const; |
| 134 | size_type max_size() const; |
| 135 | size_type capacity() const; |
| 136 | |
| 137 | void resize(size_type n, value_type c); |
| 138 | void resize(size_type n); |
| 139 | |
| 140 | void reserve(size_type res_arg = 0); |
| 141 | void shrink_to_fit(); |
| 142 | void clear(); |
| 143 | bool empty() const; |
| 144 | |
| 145 | const_reference operator[](size_type pos) const; |
| 146 | reference operator[](size_type pos); |
| 147 | |
| 148 | const_reference at(size_type n) const; |
| 149 | reference at(size_type n); |
| 150 | |
| 151 | basic_string& operator+=(const basic_string& str); |
| 152 | basic_string& operator+=(const_pointer s); |
| 153 | basic_string& operator+=(value_type c); |
| 154 | basic_string& operator+=(initializer_list<value_type>); |
| 155 | |
| 156 | basic_string& append(const basic_string& str); |
| 157 | basic_string& append(const basic_string& str, size_type pos, size_type n); |
| 158 | basic_string& append(const_pointer s, size_type n); |
| 159 | basic_string& append(const_pointer s); |
| 160 | basic_string& append(size_type n, value_type c); |
Howard Hinnant | a6a062d | 2010-06-02 18:20:39 +0000 | [diff] [blame] | 161 | template<class InputIterator> |
| 162 | basic_string& append(InputIterator first, InputIterator last); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 163 | basic_string& append(initializer_list<value_type>); |
| 164 | |
| 165 | void push_back(value_type c); |
| 166 | void pop_back(); |
| 167 | reference front(); |
| 168 | const_reference front() const; |
| 169 | reference back(); |
| 170 | const_reference back() const; |
| 171 | |
| 172 | basic_string& assign(const basic_string& str); |
| 173 | basic_string& assign(const basic_string& str, size_type pos, size_type n); |
| 174 | basic_string& assign(const_pointer s, size_type n); |
| 175 | basic_string& assign(const_pointer s); |
| 176 | basic_string& assign(size_type n, value_type c); |
Howard Hinnant | a6a062d | 2010-06-02 18:20:39 +0000 | [diff] [blame] | 177 | template<class InputIterator> |
| 178 | basic_string& assign(InputIterator first, InputIterator last); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 179 | basic_string& assign(initializer_list<value_type>); |
| 180 | |
| 181 | basic_string& insert(size_type pos1, const basic_string& str); |
Howard Hinnant | a6a062d | 2010-06-02 18:20:39 +0000 | [diff] [blame] | 182 | basic_string& insert(size_type pos1, const basic_string& str, |
| 183 | size_type pos2, size_type n); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 184 | basic_string& insert(size_type pos, const_pointer s, size_type n); |
| 185 | basic_string& insert(size_type pos, const_pointer s); |
| 186 | basic_string& insert(size_type pos, size_type n, value_type c); |
| 187 | iterator insert(const_iterator p, value_type c); |
| 188 | iterator insert(const_iterator p, size_type n, value_type c); |
Howard Hinnant | a6a062d | 2010-06-02 18:20:39 +0000 | [diff] [blame] | 189 | template<class InputIterator> |
| 190 | iterator insert(const_iterator p, InputIterator first, InputIterator last); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 191 | iterator insert(const_iterator p, initializer_list<value_type>); |
| 192 | |
| 193 | basic_string& erase(size_type pos = 0, size_type n = npos); |
| 194 | iterator erase(const_iterator position); |
| 195 | iterator erase(const_iterator first, const_iterator last); |
| 196 | |
| 197 | basic_string& replace(size_type pos1, size_type n1, const basic_string& str); |
Howard Hinnant | a6a062d | 2010-06-02 18:20:39 +0000 | [diff] [blame] | 198 | basic_string& replace(size_type pos1, size_type n1, const basic_string& str, |
| 199 | size_type pos2, size_type n2); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 200 | basic_string& replace(size_type pos, size_type n1, const_pointer s, size_type n2); |
| 201 | basic_string& replace(size_type pos, size_type n1, const_pointer s); |
| 202 | basic_string& replace(size_type pos, size_type n1, size_type n2, value_type c); |
| 203 | basic_string& replace(iterator i1, iterator i2, const basic_string& str); |
| 204 | basic_string& replace(iterator i1, iterator i2, const_pointer s, size_type n); |
| 205 | basic_string& replace(iterator i1, iterator i2, const_pointer s); |
| 206 | basic_string& replace(iterator i1, iterator i2, size_type n, value_type c); |
Howard Hinnant | a6a062d | 2010-06-02 18:20:39 +0000 | [diff] [blame] | 207 | template<class InputIterator> |
| 208 | basic_string& replace(iterator i1, iterator i2, InputIterator j1, InputIterator j2); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 209 | basic_string& replace(iterator i1, iterator i2, initializer_list<value_type>); |
| 210 | |
| 211 | size_type copy(pointer s, size_type n, size_type pos = 0) const; |
| 212 | basic_string substr(size_type pos = 0, size_type n = npos) const; |
| 213 | |
| 214 | void swap(basic_string& str); |
| 215 | |
| 216 | const_pointer c_str() const; |
| 217 | const_pointer data() const; |
| 218 | |
| 219 | allocator_type get_allocator() const; |
| 220 | |
| 221 | size_type find(const basic_string& str, size_type pos = 0) const; |
| 222 | size_type find(const_pointer s, size_type pos, size_type n) const; |
| 223 | size_type find(const_pointer s, size_type pos = 0) const; |
| 224 | size_type find(value_type c, size_type pos = 0) const; |
| 225 | |
| 226 | size_type rfind(const basic_string& str, size_type pos = npos) const; |
| 227 | size_type rfind(const_pointer s, size_type pos, size_type n) const; |
| 228 | size_type rfind(const_pointer s, size_type pos = npos) const; |
| 229 | size_type rfind(value_type c, size_type pos = npos) const; |
| 230 | |
| 231 | size_type find_first_of(const basic_string& str, size_type pos = 0) const; |
| 232 | size_type find_first_of(const_pointer s, size_type pos, size_type n) const; |
| 233 | size_type find_first_of(const_pointer s, size_type pos = 0) const; |
| 234 | size_type find_first_of(value_type c, size_type pos = 0) const; |
| 235 | |
| 236 | size_type find_last_of(const basic_string& str, size_type pos = npos) const; |
| 237 | size_type find_last_of(const_pointer s, size_type pos, size_type n) const; |
| 238 | size_type find_last_of(const_pointer s, size_type pos = npos) const; |
| 239 | size_type find_last_of(value_type c, size_type pos = npos) const; |
| 240 | |
| 241 | size_type find_first_not_of(const basic_string& str, size_type pos = 0) const; |
| 242 | size_type find_first_not_of(const_pointer s, size_type pos, size_type n) const; |
| 243 | size_type find_first_not_of(const_pointer s, size_type pos = 0) const; |
| 244 | size_type find_first_not_of(value_type c, size_type pos = 0) const; |
| 245 | |
| 246 | size_type find_last_not_of(const basic_string& str, size_type pos = npos) const; |
| 247 | size_type find_last_not_of(const_pointer s, size_type pos, size_type n) const; |
| 248 | size_type find_last_not_of(const_pointer s, size_type pos = npos) const; |
| 249 | size_type find_last_not_of(value_type c, size_type pos = npos) const; |
| 250 | |
| 251 | int compare(const basic_string& str) const; |
| 252 | int compare(size_type pos1, size_type n1, const basic_string& str) const; |
Howard Hinnant | a6a062d | 2010-06-02 18:20:39 +0000 | [diff] [blame] | 253 | int compare(size_type pos1, size_type n1, const basic_string& str, |
| 254 | size_type pos2, size_type n2) const; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 255 | int compare(const_pointer s) const; |
| 256 | int compare(size_type pos1, size_type n1, const_pointer s) const; |
| 257 | int compare(size_type pos1, size_type n1, const_pointer s, size_type n2) const; |
| 258 | |
| 259 | bool __invariants() const; |
| 260 | }; |
| 261 | |
| 262 | template<class charT, class traits, class Allocator> |
| 263 | basic_string<charT, traits, Allocator> |
Howard Hinnant | a6a062d | 2010-06-02 18:20:39 +0000 | [diff] [blame] | 264 | operator+(const basic_string<charT, traits, Allocator>& lhs, |
| 265 | const basic_string<charT, traits, Allocator>& rhs); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 266 | |
| 267 | template<class charT, class traits, class Allocator> |
| 268 | basic_string<charT, traits, Allocator> |
| 269 | operator+(const charT* lhs , const basic_string<charT,traits,Allocator>&rhs); |
| 270 | |
| 271 | template<class charT, class traits, class Allocator> |
| 272 | basic_string<charT, traits, Allocator> |
| 273 | operator+(charT lhs, const basic_string<charT,traits,Allocator>& rhs); |
| 274 | |
| 275 | template<class charT, class traits, class Allocator> |
| 276 | basic_string<charT, traits, Allocator> |
| 277 | operator+(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs); |
| 278 | |
| 279 | template<class charT, class traits, class Allocator> |
| 280 | basic_string<charT, traits, Allocator> |
| 281 | operator+(const basic_string<charT, traits, Allocator>& lhs, charT rhs); |
| 282 | |
| 283 | template<class charT, class traits, class Allocator> |
Howard Hinnant | a6a062d | 2010-06-02 18:20:39 +0000 | [diff] [blame] | 284 | bool operator==(const basic_string<charT, traits, Allocator>& lhs, |
| 285 | const basic_string<charT, traits, Allocator>& rhs); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 286 | |
| 287 | template<class charT, class traits, class Allocator> |
| 288 | bool operator==(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs); |
| 289 | |
| 290 | template<class charT, class traits, class Allocator> |
| 291 | bool operator==(const basic_string<charT,traits,Allocator>& lhs, const charT* rhs); |
| 292 | |
| 293 | template<class charT, class traits, class Allocator> |
Howard Hinnant | a6a062d | 2010-06-02 18:20:39 +0000 | [diff] [blame] | 294 | bool operator!=(const basic_string<charT,traits,Allocator>& lhs, |
| 295 | const basic_string<charT, traits, Allocator>& rhs); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 296 | |
| 297 | template<class charT, class traits, class Allocator> |
| 298 | bool operator!=(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs); |
| 299 | |
| 300 | template<class charT, class traits, class Allocator> |
| 301 | bool operator!=(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs); |
| 302 | |
| 303 | template<class charT, class traits, class Allocator> |
Howard Hinnant | a6a062d | 2010-06-02 18:20:39 +0000 | [diff] [blame] | 304 | bool operator< (const basic_string<charT, traits, Allocator>& lhs, |
| 305 | const basic_string<charT, traits, Allocator>& rhs); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 306 | |
| 307 | template<class charT, class traits, class Allocator> |
| 308 | bool operator< (const basic_string<charT, traits, Allocator>& lhs, const charT* rhs); |
| 309 | |
| 310 | template<class charT, class traits, class Allocator> |
| 311 | bool operator< (const charT* lhs, const basic_string<charT, traits, Allocator>& rhs); |
| 312 | |
| 313 | template<class charT, class traits, class Allocator> |
Howard Hinnant | a6a062d | 2010-06-02 18:20:39 +0000 | [diff] [blame] | 314 | bool operator> (const basic_string<charT, traits, Allocator>& lhs, |
| 315 | const basic_string<charT, traits, Allocator>& rhs); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 316 | |
| 317 | template<class charT, class traits, class Allocator> |
| 318 | bool operator> (const basic_string<charT, traits, Allocator>& lhs, const charT* rhs); |
| 319 | |
| 320 | template<class charT, class traits, class Allocator> |
| 321 | bool operator> (const charT* lhs, const basic_string<charT, traits, Allocator>& rhs); |
| 322 | |
| 323 | template<class charT, class traits, class Allocator> |
Howard Hinnant | a6a062d | 2010-06-02 18:20:39 +0000 | [diff] [blame] | 324 | bool operator<=(const basic_string<charT, traits, Allocator>& lhs, |
| 325 | const basic_string<charT, traits, Allocator>& rhs); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 326 | |
| 327 | template<class charT, class traits, class Allocator> |
| 328 | bool operator<=(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs); |
| 329 | |
| 330 | template<class charT, class traits, class Allocator> |
| 331 | bool operator<=(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs); |
| 332 | |
| 333 | template<class charT, class traits, class Allocator> |
Howard Hinnant | a6a062d | 2010-06-02 18:20:39 +0000 | [diff] [blame] | 334 | bool operator>=(const basic_string<charT, traits, Allocator>& lhs, |
| 335 | const basic_string<charT, traits, Allocator>& rhs); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 336 | |
| 337 | template<class charT, class traits, class Allocator> |
| 338 | bool operator>=(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs); |
| 339 | |
| 340 | template<class charT, class traits, class Allocator> |
| 341 | bool operator>=(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs); |
| 342 | |
| 343 | template<class charT, class traits, class Allocator> |
Howard Hinnant | a6a062d | 2010-06-02 18:20:39 +0000 | [diff] [blame] | 344 | void swap(basic_string<charT, traits, Allocator>& lhs, |
| 345 | basic_string<charT, traits, Allocator>& rhs); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 346 | |
| 347 | template<class charT, class traits, class Allocator> |
| 348 | basic_istream<charT, traits>& |
| 349 | operator>>(basic_istream<charT, traits>& is, basic_string<charT, traits, Allocator>& str); |
| 350 | |
| 351 | template<class charT, class traits, class Allocator> |
| 352 | basic_ostream<charT, traits>& |
| 353 | operator<<(basic_ostream<charT, traits>& os, const basic_string<charT, traits, Allocator>& str); |
| 354 | |
| 355 | template<class charT, class traits, class Allocator> |
| 356 | basic_istream<charT, traits>& |
Howard Hinnant | a6a062d | 2010-06-02 18:20:39 +0000 | [diff] [blame] | 357 | getline(basic_istream<charT, traits>& is, basic_string<charT, traits, Allocator>& str, |
| 358 | charT delim); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 359 | |
| 360 | template<class charT, class traits, class Allocator> |
| 361 | basic_istream<charT, traits>& |
| 362 | getline(basic_istream<charT, traits>& is, basic_string<charT, traits, Allocator>& str); |
| 363 | |
| 364 | typedef basic_string<char> string; |
| 365 | typedef basic_string<wchar_t> wstring; |
Howard Hinnant | a6a062d | 2010-06-02 18:20:39 +0000 | [diff] [blame] | 366 | typedef basic_string<char16_t> u16string; |
| 367 | typedef basic_string<char32_t> u32string; |
| 368 | |
| 369 | int stoi (const string& str, size_t* idx = 0, int base = 10); |
| 370 | long stol (const string& str, size_t* idx = 0, int base = 10); |
| 371 | unsigned long stoul (const string& str, size_t* idx = 0, int base = 10); |
| 372 | long long stoll (const string& str, size_t* idx = 0, int base = 10); |
| 373 | unsigned long long stoull(const string& str, size_t* idx = 0, int base = 10); |
| 374 | |
| 375 | float stof (const string& str, size_t* idx = 0); |
| 376 | double stod (const string& str, size_t* idx = 0); |
| 377 | long double stold(const string& str, size_t* idx = 0); |
| 378 | |
| 379 | string to_string(int val); |
| 380 | string to_string(unsigned val); |
| 381 | string to_string(long val); |
| 382 | string to_string(unsigned long val); |
| 383 | string to_string(long long val); |
| 384 | string to_string(unsigned long long val); |
| 385 | string to_string(float val); |
| 386 | string to_string(double val); |
| 387 | string to_string(long double val); |
| 388 | |
| 389 | int stoi (const wstring& str, size_t* idx = 0, int base = 10); |
| 390 | long stol (const wstring& str, size_t* idx = 0, int base = 10); |
| 391 | unsigned long stoul (const wstring& str, size_t* idx = 0, int base = 10); |
| 392 | long long stoll (const wstring& str, size_t* idx = 0, int base = 10); |
| 393 | unsigned long long stoull(const wstring& str, size_t* idx = 0, int base = 10); |
| 394 | |
| 395 | float stof (const wstring& str, size_t* idx = 0); |
| 396 | double stod (const wstring& str, size_t* idx = 0); |
| 397 | long double stold(const wstring& str, size_t* idx = 0); |
| 398 | |
| 399 | wstring to_wstring(int val); |
| 400 | wstring to_wstring(unsigned val); |
| 401 | wstring to_wstring(long val); |
| 402 | wstring to_wstring(unsigned long val); |
| 403 | wstring to_wstring(long long val); |
| 404 | wstring to_wstring(unsigned long long val); |
| 405 | wstring to_wstring(float val); |
| 406 | wstring to_wstring(double val); |
| 407 | wstring to_wstring(long double val); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 408 | |
| 409 | template <> struct hash<string>; |
| 410 | template <> struct hash<u16string>; |
| 411 | template <> struct hash<u32string>; |
| 412 | template <> struct hash<wstring>; |
| 413 | |
| 414 | } // std |
| 415 | |
| 416 | */ |
| 417 | |
| 418 | #include <__config> |
| 419 | #include <iosfwd> |
| 420 | #include <cstring> |
Howard Hinnant | adff489 | 2010-05-24 17:49:41 +0000 | [diff] [blame] | 421 | #include <cstdio> // For EOF. |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 422 | #include <cwchar> |
| 423 | #include <algorithm> |
| 424 | #include <iterator> |
| 425 | #include <utility> |
| 426 | #include <memory> |
| 427 | #include <stdexcept> |
| 428 | #include <type_traits> |
| 429 | #include <initializer_list> |
| 430 | #include <__functional_base> |
| 431 | #ifndef _LIBCPP_HAS_NO_UNICODE_CHARS |
| 432 | #include <cstdint> |
| 433 | #endif |
| 434 | #if defined(_LIBCPP_NO_EXCEPTIONS) || defined(_LIBCPP_DEBUG) |
| 435 | #include <cassert> |
| 436 | #endif |
| 437 | |
| 438 | #pragma GCC system_header |
| 439 | |
| 440 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 441 | |
| 442 | // fpos |
| 443 | |
| 444 | template <class _StateT> |
| 445 | class fpos |
| 446 | { |
| 447 | private: |
| 448 | _StateT __st_; |
| 449 | streamoff __off_; |
| 450 | public: |
| 451 | _LIBCPP_INLINE_VISIBILITY fpos(streamoff __off = streamoff()) : __st_(), __off_(__off) {} |
| 452 | |
| 453 | _LIBCPP_INLINE_VISIBILITY operator streamoff() const {return __off_;} |
| 454 | |
| 455 | _LIBCPP_INLINE_VISIBILITY _StateT state() const {return __st_;} |
| 456 | _LIBCPP_INLINE_VISIBILITY void state(_StateT __st) {__st_ = __st;} |
| 457 | |
| 458 | _LIBCPP_INLINE_VISIBILITY fpos& operator+=(streamoff __off) {__off_ += __off; return *this;} |
| 459 | _LIBCPP_INLINE_VISIBILITY fpos operator+ (streamoff __off) const {fpos __t(*this); __t += __off; return __t;} |
| 460 | _LIBCPP_INLINE_VISIBILITY fpos& operator-=(streamoff __off) {__off_ -= __off; return *this;} |
| 461 | _LIBCPP_INLINE_VISIBILITY fpos operator- (streamoff __off) const {fpos __t(*this); __t -= __off; return __t;} |
| 462 | }; |
| 463 | |
| 464 | template <class _StateT> |
| 465 | inline _LIBCPP_INLINE_VISIBILITY |
| 466 | streamoff operator-(const fpos<_StateT>& __x, const fpos<_StateT>& __y) |
| 467 | {return streamoff(__x) - streamoff(__y);} |
| 468 | |
| 469 | template <class _StateT> |
| 470 | inline _LIBCPP_INLINE_VISIBILITY |
| 471 | bool operator==(const fpos<_StateT>& __x, const fpos<_StateT>& __y) |
| 472 | {return streamoff(__x) == streamoff(__y);} |
| 473 | |
| 474 | template <class _StateT> |
| 475 | inline _LIBCPP_INLINE_VISIBILITY |
| 476 | bool operator!=(const fpos<_StateT>& __x, const fpos<_StateT>& __y) |
| 477 | {return streamoff(__x) != streamoff(__y);} |
| 478 | |
| 479 | // char_traits |
| 480 | |
| 481 | template <class _CharT> |
| 482 | struct char_traits |
| 483 | { |
| 484 | typedef _CharT char_type; |
| 485 | typedef int int_type; |
| 486 | typedef streamoff off_type; |
| 487 | typedef streampos pos_type; |
| 488 | typedef mbstate_t state_type; |
| 489 | |
| 490 | _LIBCPP_INLINE_VISIBILITY static void assign(char_type& __c1, const char_type& __c2) {__c1 = __c2;} |
| 491 | _LIBCPP_INLINE_VISIBILITY static bool eq(char_type __c1, char_type __c2) {return __c1 == __c2;} |
| 492 | _LIBCPP_INLINE_VISIBILITY static bool lt(char_type __c1, char_type __c2) {return __c1 < __c2;} |
| 493 | |
| 494 | static int compare(const char_type* __s1, const char_type* __s2, size_t __n); |
| 495 | static size_t length(const char_type* __s); |
| 496 | static const char_type* find(const char_type* __s, size_t __n, const char_type& __a); |
| 497 | static char_type* move(char_type* __s1, const char_type* __s2, size_t __n); |
| 498 | static char_type* copy(char_type* __s1, const char_type* __s2, size_t __n); |
| 499 | static char_type* assign(char_type* __s, size_t __n, char_type __a); |
| 500 | |
| 501 | _LIBCPP_INLINE_VISIBILITY static int_type not_eof(int_type __c) |
| 502 | {return eq_int_type(__c, eof()) ? ~eof() : __c;} |
| 503 | _LIBCPP_INLINE_VISIBILITY static char_type to_char_type(int_type __c) {return char_type(__c);} |
| 504 | _LIBCPP_INLINE_VISIBILITY static int_type to_int_type(char_type __c) {return int_type(__c);} |
| 505 | _LIBCPP_INLINE_VISIBILITY static bool eq_int_type(int_type __c1, int_type __c2) |
| 506 | {return __c1 == __c2;} |
| 507 | _LIBCPP_INLINE_VISIBILITY static int_type eof() {return int_type(EOF);} |
| 508 | }; |
| 509 | |
| 510 | template <class _CharT> |
| 511 | int |
| 512 | char_traits<_CharT>::compare(const char_type* __s1, const char_type* __s2, size_t __n) |
| 513 | { |
| 514 | for (; __n; --__n, ++__s1, ++__s2) |
| 515 | { |
| 516 | if (lt(*__s1, *__s2)) |
| 517 | return -1; |
| 518 | if (lt(*__s2, *__s1)) |
| 519 | return 1; |
| 520 | } |
| 521 | return 0; |
| 522 | } |
| 523 | |
| 524 | template <class _CharT> |
| 525 | inline _LIBCPP_INLINE_VISIBILITY |
| 526 | size_t |
| 527 | char_traits<_CharT>::length(const char_type* __s) |
| 528 | { |
| 529 | size_t __len = 0; |
| 530 | for (; !eq(*__s, char_type(0)); ++__s) |
| 531 | ++__len; |
| 532 | return __len; |
| 533 | } |
| 534 | |
| 535 | template <class _CharT> |
| 536 | inline _LIBCPP_INLINE_VISIBILITY |
| 537 | const _CharT* |
| 538 | char_traits<_CharT>::find(const char_type* __s, size_t __n, const char_type& __a) |
| 539 | { |
| 540 | for (; __n; --__n) |
| 541 | { |
| 542 | if (eq(*__s, __a)) |
| 543 | return __s; |
| 544 | ++__s; |
| 545 | } |
| 546 | return 0; |
| 547 | } |
| 548 | |
| 549 | template <class _CharT> |
| 550 | _CharT* |
| 551 | char_traits<_CharT>::move(char_type* __s1, const char_type* __s2, size_t __n) |
| 552 | { |
| 553 | char_type* __r = __s1; |
| 554 | if (__s1 < __s2) |
| 555 | { |
| 556 | for (; __n; --__n, ++__s1, ++__s2) |
| 557 | assign(*__s1, *__s2); |
| 558 | } |
| 559 | else if (__s2 < __s1) |
| 560 | { |
| 561 | __s1 += __n; |
| 562 | __s2 += __n; |
| 563 | for (; __n; --__n) |
| 564 | assign(*--__s1, *--__s2); |
| 565 | } |
| 566 | return __r; |
| 567 | } |
| 568 | |
| 569 | template <class _CharT> |
| 570 | inline _LIBCPP_INLINE_VISIBILITY |
| 571 | _CharT* |
| 572 | char_traits<_CharT>::copy(char_type* __s1, const char_type* __s2, size_t __n) |
| 573 | { |
| 574 | char_type* __r = __s1; |
| 575 | for (; __n; --__n, ++__s1, ++__s2) |
| 576 | assign(*__s1, *__s2); |
| 577 | return __r; |
| 578 | } |
| 579 | |
| 580 | template <class _CharT> |
| 581 | inline _LIBCPP_INLINE_VISIBILITY |
| 582 | _CharT* |
| 583 | char_traits<_CharT>::assign(char_type* __s, size_t __n, char_type __a) |
| 584 | { |
| 585 | char_type* __r = __s; |
| 586 | for (; __n; --__n, ++__s) |
| 587 | assign(*__s, __a); |
| 588 | return __r; |
| 589 | } |
| 590 | |
| 591 | // char_traits<char> |
| 592 | |
| 593 | template <> |
| 594 | struct char_traits<char> |
| 595 | { |
| 596 | typedef char char_type; |
| 597 | typedef int int_type; |
| 598 | typedef streamoff off_type; |
| 599 | typedef streampos pos_type; |
| 600 | typedef mbstate_t state_type; |
| 601 | |
| 602 | _LIBCPP_INLINE_VISIBILITY static void assign(char_type& __c1, const char_type& __c2) {__c1 = __c2;} |
| 603 | _LIBCPP_INLINE_VISIBILITY static bool eq(char_type __c1, char_type __c2) {return __c1 == __c2;} |
| 604 | _LIBCPP_INLINE_VISIBILITY static bool lt(char_type __c1, char_type __c2) |
| 605 | {return (unsigned char)__c1 < (unsigned char)__c2;} |
| 606 | |
| 607 | _LIBCPP_INLINE_VISIBILITY static int compare(const char_type* __s1, const char_type* __s2, size_t __n) |
| 608 | {return memcmp(__s1, __s2, __n);} |
| 609 | _LIBCPP_INLINE_VISIBILITY static size_t length(const char_type* __s) {return strlen(__s);} |
| 610 | _LIBCPP_INLINE_VISIBILITY static const char_type* find(const char_type* __s, size_t __n, const char_type& __a) |
| 611 | {return (const char_type*)memchr(__s, to_int_type(__a), __n);} |
| 612 | _LIBCPP_INLINE_VISIBILITY static char_type* move(char_type* __s1, const char_type* __s2, size_t __n) |
| 613 | {return (char_type*)memmove(__s1, __s2, __n);} |
| 614 | _LIBCPP_INLINE_VISIBILITY static char_type* copy(char_type* __s1, const char_type* __s2, size_t __n) |
| 615 | {return (char_type*)memcpy(__s1, __s2, __n);} |
| 616 | _LIBCPP_INLINE_VISIBILITY static char_type* assign(char_type* __s, size_t __n, char_type __a) |
| 617 | {return (char_type*)memset(__s, to_int_type(__a), __n);} |
| 618 | |
| 619 | _LIBCPP_INLINE_VISIBILITY static int_type not_eof(int_type __c) |
| 620 | {return eq_int_type(__c, eof()) ? ~eof() : __c;} |
| 621 | _LIBCPP_INLINE_VISIBILITY static char_type to_char_type(int_type __c) {return char_type(__c);} |
| 622 | _LIBCPP_INLINE_VISIBILITY static int_type to_int_type(char_type __c) {return int_type((unsigned char)__c);} |
| 623 | _LIBCPP_INLINE_VISIBILITY static bool eq_int_type(int_type __c1, int_type __c2) |
| 624 | {return __c1 == __c2;} |
| 625 | _LIBCPP_INLINE_VISIBILITY static int_type eof() {return int_type(EOF);} |
| 626 | }; |
| 627 | |
| 628 | // char_traits<wchar_t> |
| 629 | |
| 630 | template <> |
| 631 | struct char_traits<wchar_t> |
| 632 | { |
| 633 | typedef wchar_t char_type; |
| 634 | typedef wint_t int_type; |
| 635 | typedef streamoff off_type; |
| 636 | typedef streampos pos_type; |
| 637 | typedef mbstate_t state_type; |
| 638 | |
| 639 | _LIBCPP_INLINE_VISIBILITY static void assign(char_type& __c1, const char_type& __c2) {__c1 = __c2;} |
| 640 | _LIBCPP_INLINE_VISIBILITY static bool eq(char_type __c1, char_type __c2) {return __c1 == __c2;} |
| 641 | _LIBCPP_INLINE_VISIBILITY static bool lt(char_type __c1, char_type __c2) |
| 642 | {return __c1 < __c2;} |
| 643 | |
| 644 | _LIBCPP_INLINE_VISIBILITY static int compare(const char_type* __s1, const char_type* __s2, size_t __n) |
| 645 | {return wmemcmp(__s1, __s2, __n);} |
| 646 | _LIBCPP_INLINE_VISIBILITY static size_t length(const char_type* __s) {return wcslen(__s);} |
| 647 | _LIBCPP_INLINE_VISIBILITY static const char_type* find(const char_type* __s, size_t __n, const char_type& __a) |
| 648 | {return (const char_type*)wmemchr(__s, __a, __n);} |
| 649 | _LIBCPP_INLINE_VISIBILITY static char_type* move(char_type* __s1, const char_type* __s2, size_t __n) |
| 650 | {return (char_type*)wmemmove(__s1, __s2, __n);} |
| 651 | _LIBCPP_INLINE_VISIBILITY static char_type* copy(char_type* __s1, const char_type* __s2, size_t __n) |
| 652 | {return (char_type*)wmemcpy(__s1, __s2, __n);} |
| 653 | _LIBCPP_INLINE_VISIBILITY static char_type* assign(char_type* __s, size_t __n, char_type __a) |
| 654 | {return (char_type*)wmemset(__s, __a, __n);} |
| 655 | |
| 656 | _LIBCPP_INLINE_VISIBILITY static int_type not_eof(int_type __c) |
| 657 | {return eq_int_type(__c, eof()) ? ~eof() : __c;} |
| 658 | _LIBCPP_INLINE_VISIBILITY static char_type to_char_type(int_type __c) {return char_type(__c);} |
| 659 | _LIBCPP_INLINE_VISIBILITY static int_type to_int_type(char_type __c) {return int_type(__c);} |
| 660 | _LIBCPP_INLINE_VISIBILITY static bool eq_int_type(int_type __c1, int_type __c2) |
| 661 | {return __c1 == __c2;} |
| 662 | _LIBCPP_INLINE_VISIBILITY static int_type eof() {return int_type(WEOF);} |
| 663 | }; |
| 664 | |
| 665 | #ifndef _LIBCPP_HAS_NO_UNICODE_CHARS |
| 666 | |
| 667 | template <> |
| 668 | struct char_traits<char16_t> |
| 669 | { |
| 670 | typedef char16_t char_type; |
| 671 | typedef uint_least16_t int_type; |
| 672 | typedef streamoff off_type; |
| 673 | typedef u16streampos pos_type; |
| 674 | typedef mbstate_t state_type; |
| 675 | |
| 676 | _LIBCPP_INLINE_VISIBILITY static void assign(char_type& __c1, const char_type& __c2) {__c1 = __c2;} |
| 677 | _LIBCPP_INLINE_VISIBILITY static bool eq(char_type __c1, char_type __c2) {return __c1 == __c2;} |
| 678 | _LIBCPP_INLINE_VISIBILITY static bool lt(char_type __c1, char_type __c2) {return __c1 < __c2;} |
| 679 | |
| 680 | static int compare(const char_type* __s1, const char_type* __s2, size_t __n); |
| 681 | static size_t length(const char_type* __s); |
| 682 | static const char_type* find(const char_type* __s, size_t __n, const char_type& __a); |
| 683 | static char_type* move(char_type* __s1, const char_type* __s2, size_t __n); |
| 684 | static char_type* copy(char_type* __s1, const char_type* __s2, size_t __n); |
| 685 | static char_type* assign(char_type* __s, size_t __n, char_type __a); |
| 686 | |
| 687 | _LIBCPP_INLINE_VISIBILITY static int_type not_eof(int_type __c) |
| 688 | {return eq_int_type(__c, eof()) ? ~eof() : __c;} |
| 689 | _LIBCPP_INLINE_VISIBILITY static char_type to_char_type(int_type __c) {return char_type(__c);} |
| 690 | _LIBCPP_INLINE_VISIBILITY static int_type to_int_type(char_type __c) {return int_type(__c);} |
| 691 | _LIBCPP_INLINE_VISIBILITY static bool eq_int_type(int_type __c1, int_type __c2) |
| 692 | {return __c1 == __c2;} |
| 693 | _LIBCPP_INLINE_VISIBILITY static int_type eof() {return int_type(0xDFFF);} |
| 694 | }; |
| 695 | |
| 696 | inline _LIBCPP_INLINE_VISIBILITY |
| 697 | int |
| 698 | char_traits<char16_t>::compare(const char_type* __s1, const char_type* __s2, size_t __n) |
| 699 | { |
| 700 | for (; __n; --__n, ++__s1, ++__s2) |
| 701 | { |
| 702 | if (lt(*__s1, *__s2)) |
| 703 | return -1; |
| 704 | if (lt(*__s2, *__s1)) |
| 705 | return 1; |
| 706 | } |
| 707 | return 0; |
| 708 | } |
| 709 | |
| 710 | inline _LIBCPP_INLINE_VISIBILITY |
| 711 | size_t |
| 712 | char_traits<char16_t>::length(const char_type* __s) |
| 713 | { |
| 714 | size_t __len = 0; |
| 715 | for (; !eq(*__s, char_type(0)); ++__s) |
| 716 | ++__len; |
| 717 | return __len; |
| 718 | } |
| 719 | |
| 720 | inline _LIBCPP_INLINE_VISIBILITY |
| 721 | const char16_t* |
| 722 | char_traits<char16_t>::find(const char_type* __s, size_t __n, const char_type& __a) |
| 723 | { |
| 724 | for (; __n; --__n) |
| 725 | { |
| 726 | if (eq(*__s, __a)) |
| 727 | return __s; |
| 728 | ++__s; |
| 729 | } |
| 730 | return 0; |
| 731 | } |
| 732 | |
| 733 | inline _LIBCPP_INLINE_VISIBILITY |
| 734 | char16_t* |
| 735 | char_traits<char16_t>::move(char_type* __s1, const char_type* __s2, size_t __n) |
| 736 | { |
| 737 | char_type* __r = __s1; |
| 738 | if (__s1 < __s2) |
| 739 | { |
| 740 | for (; __n; --__n, ++__s1, ++__s2) |
| 741 | assign(*__s1, *__s2); |
| 742 | } |
| 743 | else if (__s2 < __s1) |
| 744 | { |
| 745 | __s1 += __n; |
| 746 | __s2 += __n; |
| 747 | for (; __n; --__n) |
| 748 | assign(*--__s1, *--__s2); |
| 749 | } |
| 750 | return __r; |
| 751 | } |
| 752 | |
| 753 | inline _LIBCPP_INLINE_VISIBILITY |
| 754 | char16_t* |
| 755 | char_traits<char16_t>::copy(char_type* __s1, const char_type* __s2, size_t __n) |
| 756 | { |
| 757 | char_type* __r = __s1; |
| 758 | for (; __n; --__n, ++__s1, ++__s2) |
| 759 | assign(*__s1, *__s2); |
| 760 | return __r; |
| 761 | } |
| 762 | |
| 763 | inline _LIBCPP_INLINE_VISIBILITY |
| 764 | char16_t* |
| 765 | char_traits<char16_t>::assign(char_type* __s, size_t __n, char_type __a) |
| 766 | { |
| 767 | char_type* __r = __s; |
| 768 | for (; __n; --__n, ++__s) |
| 769 | assign(*__s, __a); |
| 770 | return __r; |
| 771 | } |
| 772 | |
| 773 | template <> |
| 774 | struct char_traits<char32_t> |
| 775 | { |
| 776 | typedef char32_t char_type; |
| 777 | typedef uint_least32_t int_type; |
| 778 | typedef streamoff off_type; |
| 779 | typedef u32streampos pos_type; |
| 780 | typedef mbstate_t state_type; |
| 781 | |
| 782 | _LIBCPP_INLINE_VISIBILITY static void assign(char_type& __c1, const char_type& __c2) {__c1 = __c2;} |
| 783 | _LIBCPP_INLINE_VISIBILITY static bool eq(char_type __c1, char_type __c2) {return __c1 == __c2;} |
| 784 | _LIBCPP_INLINE_VISIBILITY static bool lt(char_type __c1, char_type __c2) {return __c1 < __c2;} |
| 785 | |
| 786 | static int compare(const char_type* __s1, const char_type* __s2, size_t __n); |
| 787 | static size_t length(const char_type* __s); |
| 788 | static const char_type* find(const char_type* __s, size_t __n, const char_type& __a); |
| 789 | static char_type* move(char_type* __s1, const char_type* __s2, size_t __n); |
| 790 | static char_type* copy(char_type* __s1, const char_type* __s2, size_t __n); |
| 791 | static char_type* assign(char_type* __s, size_t __n, char_type __a); |
| 792 | |
| 793 | _LIBCPP_INLINE_VISIBILITY static int_type not_eof(int_type __c) |
| 794 | {return eq_int_type(__c, eof()) ? ~eof() : __c;} |
| 795 | _LIBCPP_INLINE_VISIBILITY static char_type to_char_type(int_type __c) {return char_type(__c);} |
| 796 | _LIBCPP_INLINE_VISIBILITY static int_type to_int_type(char_type __c) {return int_type(__c);} |
| 797 | _LIBCPP_INLINE_VISIBILITY static bool eq_int_type(int_type __c1, int_type __c2) |
| 798 | {return __c1 == __c2;} |
| 799 | _LIBCPP_INLINE_VISIBILITY static int_type eof() {return int_type(0xFFFFFFFF);} |
| 800 | }; |
| 801 | |
| 802 | inline _LIBCPP_INLINE_VISIBILITY |
| 803 | int |
| 804 | char_traits<char32_t>::compare(const char_type* __s1, const char_type* __s2, size_t __n) |
| 805 | { |
| 806 | for (; __n; --__n, ++__s1, ++__s2) |
| 807 | { |
| 808 | if (lt(*__s1, *__s2)) |
| 809 | return -1; |
| 810 | if (lt(*__s2, *__s1)) |
| 811 | return 1; |
| 812 | } |
| 813 | return 0; |
| 814 | } |
| 815 | |
| 816 | inline _LIBCPP_INLINE_VISIBILITY |
| 817 | size_t |
| 818 | char_traits<char32_t>::length(const char_type* __s) |
| 819 | { |
| 820 | size_t __len = 0; |
| 821 | for (; !eq(*__s, char_type(0)); ++__s) |
| 822 | ++__len; |
| 823 | return __len; |
| 824 | } |
| 825 | |
| 826 | inline _LIBCPP_INLINE_VISIBILITY |
| 827 | const char32_t* |
| 828 | char_traits<char32_t>::find(const char_type* __s, size_t __n, const char_type& __a) |
| 829 | { |
| 830 | for (; __n; --__n) |
| 831 | { |
| 832 | if (eq(*__s, __a)) |
| 833 | return __s; |
| 834 | ++__s; |
| 835 | } |
| 836 | return 0; |
| 837 | } |
| 838 | |
| 839 | inline _LIBCPP_INLINE_VISIBILITY |
| 840 | char32_t* |
| 841 | char_traits<char32_t>::move(char_type* __s1, const char_type* __s2, size_t __n) |
| 842 | { |
| 843 | char_type* __r = __s1; |
| 844 | if (__s1 < __s2) |
| 845 | { |
| 846 | for (; __n; --__n, ++__s1, ++__s2) |
| 847 | assign(*__s1, *__s2); |
| 848 | } |
| 849 | else if (__s2 < __s1) |
| 850 | { |
| 851 | __s1 += __n; |
| 852 | __s2 += __n; |
| 853 | for (; __n; --__n) |
| 854 | assign(*--__s1, *--__s2); |
| 855 | } |
| 856 | return __r; |
| 857 | } |
| 858 | |
| 859 | inline _LIBCPP_INLINE_VISIBILITY |
| 860 | char32_t* |
| 861 | char_traits<char32_t>::copy(char_type* __s1, const char_type* __s2, size_t __n) |
| 862 | { |
| 863 | char_type* __r = __s1; |
| 864 | for (; __n; --__n, ++__s1, ++__s2) |
| 865 | assign(*__s1, *__s2); |
| 866 | return __r; |
| 867 | } |
| 868 | |
| 869 | inline _LIBCPP_INLINE_VISIBILITY |
| 870 | char32_t* |
| 871 | char_traits<char32_t>::assign(char_type* __s, size_t __n, char_type __a) |
| 872 | { |
| 873 | char_type* __r = __s; |
| 874 | for (; __n; --__n, ++__s) |
| 875 | assign(*__s, __a); |
| 876 | return __r; |
| 877 | } |
| 878 | |
| 879 | #endif |
| 880 | |
| 881 | // basic_string |
| 882 | |
| 883 | template<class _CharT, class _Traits, class _Allocator> |
| 884 | basic_string<_CharT, _Traits, _Allocator> |
| 885 | operator+(const basic_string<_CharT, _Traits, _Allocator>&, const basic_string<_CharT, _Traits, _Allocator>&); |
| 886 | |
| 887 | template<class _CharT, class _Traits, class _Allocator> |
| 888 | basic_string<_CharT, _Traits, _Allocator> |
| 889 | operator+(const _CharT*, const basic_string<_CharT,_Traits,_Allocator>&); |
| 890 | |
| 891 | template<class _CharT, class _Traits, class _Allocator> |
| 892 | basic_string<_CharT, _Traits, _Allocator> |
| 893 | operator+(_CharT, const basic_string<_CharT,_Traits,_Allocator>&); |
| 894 | |
| 895 | template<class _CharT, class _Traits, class _Allocator> |
| 896 | basic_string<_CharT, _Traits, _Allocator> |
| 897 | operator+(const basic_string<_CharT, _Traits, _Allocator>&, const _CharT*); |
| 898 | |
| 899 | template<class _CharT, class _Traits, class _Allocator> |
| 900 | basic_string<_CharT, _Traits, _Allocator> |
| 901 | operator+(const basic_string<_CharT, _Traits, _Allocator>&, _CharT); |
| 902 | |
| 903 | template <bool> |
| 904 | class __basic_string_common |
| 905 | { |
| 906 | protected: |
| 907 | void __throw_length_error() const; |
| 908 | void __throw_out_of_range() const; |
| 909 | }; |
| 910 | |
| 911 | template <bool __b> |
| 912 | void |
| 913 | __basic_string_common<__b>::__throw_length_error() const |
| 914 | { |
| 915 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 916 | throw length_error("basic_string"); |
| 917 | #else |
| 918 | assert(!"basic_string length_error"); |
| 919 | #endif |
| 920 | } |
| 921 | |
| 922 | template <bool __b> |
| 923 | void |
| 924 | __basic_string_common<__b>::__throw_out_of_range() const |
| 925 | { |
| 926 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 927 | throw out_of_range("basic_string"); |
| 928 | #else |
| 929 | assert(!"basic_string out_of_range"); |
| 930 | #endif |
| 931 | } |
| 932 | |
| 933 | extern template class __basic_string_common<true>; |
| 934 | |
| 935 | template<class _CharT, class _Traits, class _Allocator> |
| 936 | class basic_string |
| 937 | : private __basic_string_common<true> |
| 938 | { |
| 939 | public: |
| 940 | typedef basic_string __self; |
| 941 | typedef _Traits traits_type; |
| 942 | typedef typename traits_type::char_type value_type; |
| 943 | typedef _Allocator allocator_type; |
| 944 | typedef typename allocator_type::size_type size_type; |
| 945 | typedef typename allocator_type::difference_type difference_type; |
| 946 | typedef typename allocator_type::reference reference; |
| 947 | typedef typename allocator_type::const_reference const_reference; |
| 948 | typedef typename allocator_type::pointer pointer; |
| 949 | typedef typename allocator_type::const_pointer const_pointer; |
| 950 | #ifdef _LIBCPP_DEBUG |
| 951 | typedef __debug_iter<basic_string, pointer> iterator; |
| 952 | typedef __debug_iter<basic_string, const_pointer> const_iterator; |
| 953 | |
| 954 | friend class __debug_iter<basic_string, pointer>; |
| 955 | friend class __debug_iter<basic_string, const_pointer>; |
| 956 | #elif defined(_LIBCPP_RAW_ITERATORS) |
| 957 | typedef pointer iterator; |
| 958 | typedef const_pointer const_iterator; |
| 959 | #else |
| 960 | typedef __wrap_iter<pointer> iterator; |
| 961 | typedef __wrap_iter<const_pointer> const_iterator; |
| 962 | #endif |
| 963 | typedef _STD::reverse_iterator<iterator> reverse_iterator; |
| 964 | typedef _STD::reverse_iterator<const_iterator> const_reverse_iterator; |
| 965 | |
| 966 | private: |
| 967 | struct __long |
| 968 | { |
| 969 | size_type __cap_; |
| 970 | size_type __size_; |
| 971 | pointer __data_; |
| 972 | }; |
| 973 | |
| 974 | #if _LIBCPP_BIG_ENDIAN |
| 975 | enum {__short_mask = 0x80}; |
| 976 | enum {__long_mask = ~(size_type(~0) >> 1)}; |
| 977 | #else |
| 978 | enum {__short_mask = 0x01}; |
| 979 | enum {__long_mask = 0x1}; |
| 980 | #endif |
| 981 | |
| 982 | enum {__mask = size_type(~0) >> 1}; |
| 983 | |
| 984 | enum {__min_cap = (sizeof(__long) - 1)/sizeof(value_type) > 2 ? |
| 985 | (sizeof(__long) - 1)/sizeof(value_type) : 2}; |
| 986 | |
| 987 | struct __short |
| 988 | { |
| 989 | union |
| 990 | { |
| 991 | unsigned char __size_; |
| 992 | value_type _; |
| 993 | }; |
| 994 | value_type __data_[__min_cap]; |
| 995 | }; |
| 996 | |
| 997 | union _{__long _; __short __;}; |
| 998 | |
| 999 | enum {__n_words = sizeof(_) / sizeof(size_type)}; |
| 1000 | |
| 1001 | struct __raw |
| 1002 | { |
| 1003 | size_type __words[__n_words]; |
| 1004 | }; |
| 1005 | |
| 1006 | struct __rep |
| 1007 | { |
| 1008 | union |
| 1009 | { |
| 1010 | __long __l; |
| 1011 | __short __s; |
| 1012 | __raw __r; |
| 1013 | }; |
| 1014 | }; |
| 1015 | |
| 1016 | __compressed_pair<__rep, allocator_type> __r_; |
| 1017 | |
| 1018 | #ifdef _LIBCPP_DEBUG |
| 1019 | |
| 1020 | pair<iterator*, const_iterator*> __iterator_list_; |
| 1021 | |
| 1022 | _LIBCPP_INLINE_VISIBILITY iterator*& __get_iterator_list(iterator*) {return __iterator_list_.first;} |
| 1023 | _LIBCPP_INLINE_VISIBILITY const_iterator*& __get_iterator_list(const_iterator*) {return __iterator_list_.second;} |
| 1024 | |
| 1025 | #endif // _LIBCPP_DEBUG |
| 1026 | |
| 1027 | public: |
| 1028 | static const size_type npos = -1; |
| 1029 | |
| 1030 | basic_string(); |
| 1031 | explicit basic_string(const allocator_type& __a); |
| 1032 | basic_string(const basic_string& __str); |
| 1033 | basic_string(const basic_string& __str, const allocator_type& __a); |
| 1034 | #ifdef _LIBCPP_MOVE |
| 1035 | basic_string(basic_string&& __str); |
| 1036 | basic_string(basic_string&& __str, const allocator_type& __a); |
| 1037 | #endif |
| 1038 | basic_string(const_pointer __s); |
| 1039 | basic_string(const_pointer __s, const allocator_type& __a); |
| 1040 | basic_string(const_pointer __s, size_type __n); |
| 1041 | basic_string(const_pointer __s, size_type __n, const allocator_type& __a); |
| 1042 | basic_string(size_type __n, value_type __c); |
| 1043 | basic_string(size_type __n, value_type __c, const allocator_type& __a); |
| 1044 | basic_string(const basic_string& __str, size_type __pos, size_type __n = npos, |
| 1045 | const allocator_type& __a = allocator_type()); |
| 1046 | template<class _InputIterator> |
| 1047 | basic_string(_InputIterator __first, _InputIterator __last); |
| 1048 | template<class _InputIterator> |
| 1049 | basic_string(_InputIterator __first, _InputIterator __last, const allocator_type& __a); |
| 1050 | basic_string(initializer_list<value_type> __il); |
| 1051 | basic_string(initializer_list<value_type> __il, const allocator_type& __a); |
| 1052 | |
| 1053 | ~basic_string(); |
| 1054 | |
| 1055 | _LIBCPP_INLINE_VISIBILITY basic_string& operator=(const basic_string& __str) {return assign(__str);} |
| 1056 | #ifdef _LIBCPP_MOVE |
| 1057 | _LIBCPP_INLINE_VISIBILITY basic_string& operator=(basic_string&& __str) {swap(__str); return *this;} |
| 1058 | #endif |
| 1059 | _LIBCPP_INLINE_VISIBILITY basic_string& operator=(const_pointer __s) {return assign(__s);} |
| 1060 | basic_string& operator=(value_type __c); |
| 1061 | basic_string& operator=(initializer_list<value_type> __il) {return assign(__il.begin(), __il.size());} |
| 1062 | |
| 1063 | #ifndef _LIBCPP_DEBUG |
| 1064 | _LIBCPP_INLINE_VISIBILITY iterator begin() {return iterator(__get_pointer());} |
| 1065 | _LIBCPP_INLINE_VISIBILITY const_iterator begin() const {return const_iterator(data());} |
| 1066 | _LIBCPP_INLINE_VISIBILITY iterator end() {return iterator(__get_pointer() + size());} |
| 1067 | _LIBCPP_INLINE_VISIBILITY const_iterator end() const {return const_iterator(data() + size());} |
| 1068 | #else // _LIBCPP_DEBUG |
| 1069 | _LIBCPP_INLINE_VISIBILITY iterator begin() {return iterator(this, __get_pointer());} |
| 1070 | _LIBCPP_INLINE_VISIBILITY const_iterator begin() const {return const_iterator(this, data());} |
| 1071 | _LIBCPP_INLINE_VISIBILITY iterator end() {return iterator(this, __get_pointer() + size());} |
| 1072 | _LIBCPP_INLINE_VISIBILITY const_iterator end() const {return const_iterator(this, data() + size());} |
| 1073 | #endif // _LIBCPP_DEBUG |
| 1074 | _LIBCPP_INLINE_VISIBILITY reverse_iterator rbegin() {return reverse_iterator(end());} |
| 1075 | _LIBCPP_INLINE_VISIBILITY const_reverse_iterator rbegin() const {return const_reverse_iterator(end());} |
| 1076 | _LIBCPP_INLINE_VISIBILITY reverse_iterator rend() {return reverse_iterator(begin());} |
| 1077 | _LIBCPP_INLINE_VISIBILITY const_reverse_iterator rend() const {return const_reverse_iterator(begin());} |
| 1078 | |
| 1079 | _LIBCPP_INLINE_VISIBILITY const_iterator cbegin() const {return begin();} |
| 1080 | _LIBCPP_INLINE_VISIBILITY const_iterator cend() const {return end();} |
| 1081 | _LIBCPP_INLINE_VISIBILITY const_reverse_iterator crbegin() const {return rbegin();} |
| 1082 | _LIBCPP_INLINE_VISIBILITY const_reverse_iterator crend() const {return rend();} |
| 1083 | |
| 1084 | _LIBCPP_INLINE_VISIBILITY size_type size() const |
| 1085 | {return __is_long() ? __get_long_size() : __get_short_size();} |
| 1086 | _LIBCPP_INLINE_VISIBILITY size_type length() const {return size();} |
| 1087 | size_type max_size() const; |
| 1088 | _LIBCPP_INLINE_VISIBILITY size_type capacity() const |
| 1089 | {return (__is_long() ? __get_long_cap() : __min_cap) - 1;} |
| 1090 | |
| 1091 | void resize(size_type __n, value_type __c); |
| 1092 | _LIBCPP_INLINE_VISIBILITY void resize(size_type __n) {resize(__n, value_type());} |
| 1093 | |
| 1094 | void reserve(size_type res_arg = 0); |
| 1095 | void shrink_to_fit() {reserve();} |
| 1096 | void clear(); |
| 1097 | _LIBCPP_INLINE_VISIBILITY bool empty() const {return size() == 0;} |
| 1098 | |
| 1099 | _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_type __pos) const; |
| 1100 | _LIBCPP_INLINE_VISIBILITY reference operator[](size_type __pos); |
| 1101 | |
| 1102 | const_reference at(size_type __n) const; |
| 1103 | reference at(size_type __n); |
| 1104 | |
| 1105 | _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(const basic_string& __str) {return append(__str);} |
| 1106 | _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(const_pointer __s) {return append(__s);} |
| 1107 | _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(value_type __c) {push_back(__c); return *this;} |
| 1108 | _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(initializer_list<value_type> __il) {return append(__il);} |
| 1109 | |
| 1110 | basic_string& append(const basic_string& __str); |
| 1111 | basic_string& append(const basic_string& __str, size_type __pos, size_type __n); |
| 1112 | basic_string& append(const_pointer __s, size_type __n); |
| 1113 | basic_string& append(const_pointer __s); |
| 1114 | basic_string& append(size_type __n, value_type __c); |
| 1115 | template<class _InputIterator> |
| 1116 | typename enable_if |
| 1117 | < |
| 1118 | __is_input_iterator <_InputIterator>::value && |
| 1119 | !__is_forward_iterator<_InputIterator>::value, |
| 1120 | basic_string& |
| 1121 | >::type |
| 1122 | append(_InputIterator __first, _InputIterator __last); |
| 1123 | template<class _ForwardIterator> |
| 1124 | typename enable_if |
| 1125 | < |
| 1126 | __is_forward_iterator<_ForwardIterator>::value, |
| 1127 | basic_string& |
| 1128 | >::type |
| 1129 | append(_ForwardIterator __first, _ForwardIterator __last); |
| 1130 | basic_string& append(initializer_list<value_type> __il) {return append(__il.begin(), __il.size());} |
| 1131 | |
| 1132 | void push_back(value_type __c); |
| 1133 | void pop_back(); |
| 1134 | reference front(); |
| 1135 | const_reference front() const; |
| 1136 | reference back(); |
| 1137 | const_reference back() const; |
| 1138 | |
| 1139 | basic_string& assign(const basic_string& __str); |
| 1140 | basic_string& assign(const basic_string& __str, size_type __pos, size_type __n); |
| 1141 | basic_string& assign(const_pointer __s, size_type __n); |
| 1142 | basic_string& assign(const_pointer __s); |
| 1143 | basic_string& assign(size_type __n, value_type __c); |
| 1144 | template<class _InputIterator> |
| 1145 | typename enable_if |
| 1146 | < |
| 1147 | __is_input_iterator <_InputIterator>::value && |
| 1148 | !__is_forward_iterator<_InputIterator>::value, |
| 1149 | basic_string& |
| 1150 | >::type |
| 1151 | assign(_InputIterator __first, _InputIterator __last); |
| 1152 | template<class _ForwardIterator> |
| 1153 | typename enable_if |
| 1154 | < |
| 1155 | __is_forward_iterator<_ForwardIterator>::value, |
| 1156 | basic_string& |
| 1157 | >::type |
| 1158 | assign(_ForwardIterator __first, _ForwardIterator __last); |
| 1159 | basic_string& assign(initializer_list<value_type> __il) {return assign(__il.begin(), __il.size());} |
| 1160 | |
| 1161 | basic_string& insert(size_type __pos1, const basic_string& __str); |
| 1162 | basic_string& insert(size_type __pos1, const basic_string& __str, size_type __pos2, size_type __n); |
| 1163 | basic_string& insert(size_type __pos, const_pointer __s, size_type __n); |
| 1164 | basic_string& insert(size_type __pos, const_pointer __s); |
| 1165 | basic_string& insert(size_type __pos, size_type __n, value_type __c); |
| 1166 | iterator insert(const_iterator __pos, value_type __c); |
| 1167 | iterator insert(const_iterator __pos, size_type __n, value_type __c); |
| 1168 | template<class _InputIterator> |
| 1169 | typename enable_if |
| 1170 | < |
| 1171 | __is_input_iterator <_InputIterator>::value && |
| 1172 | !__is_forward_iterator<_InputIterator>::value, |
| 1173 | iterator |
| 1174 | >::type |
| 1175 | insert(const_iterator __pos, _InputIterator __first, _InputIterator __last); |
| 1176 | template<class _ForwardIterator> |
| 1177 | typename enable_if |
| 1178 | < |
| 1179 | __is_forward_iterator<_ForwardIterator>::value, |
| 1180 | iterator |
| 1181 | >::type |
| 1182 | insert(const_iterator __pos, _ForwardIterator __first, _ForwardIterator __last); |
| 1183 | iterator insert(const_iterator __pos, initializer_list<value_type> __il) |
| 1184 | {return insert(__pos, __il.begin(), __il.end());} |
| 1185 | |
| 1186 | basic_string& erase(size_type __pos = 0, size_type __n = npos); |
| 1187 | iterator erase(const_iterator __pos); |
| 1188 | iterator erase(const_iterator __first, const_iterator __last); |
| 1189 | |
| 1190 | basic_string& replace(size_type __pos1, size_type __n1, const basic_string& __str); |
| 1191 | basic_string& replace(size_type __pos1, size_type __n1, const basic_string& __str, size_type __pos2, size_type __n2); |
| 1192 | basic_string& replace(size_type __pos, size_type __n1, const_pointer __s, size_type __n2); |
| 1193 | basic_string& replace(size_type __pos, size_type __n1, const_pointer __s); |
| 1194 | basic_string& replace(size_type __pos, size_type __n1, size_type __n2, value_type __c); |
| 1195 | basic_string& replace(iterator __i1, iterator __i2, const basic_string& __str); |
| 1196 | basic_string& replace(iterator __i1, iterator __i2, const_pointer __s, size_type __n); |
| 1197 | basic_string& replace(iterator __i1, iterator __i2, const_pointer __s); |
| 1198 | basic_string& replace(iterator __i1, iterator __i2, size_type __n, value_type __c); |
| 1199 | template<class _InputIterator> |
| 1200 | typename enable_if |
| 1201 | < |
| 1202 | __is_input_iterator<_InputIterator>::value, |
| 1203 | basic_string& |
| 1204 | >::type |
| 1205 | replace(iterator __i1, iterator __i2, _InputIterator __j1, _InputIterator __j2); |
| 1206 | basic_string& replace(iterator __i1, iterator __i2, initializer_list<value_type> __il) |
| 1207 | {return replace(__i1, __i2, __il.begin(), __il.end());} |
| 1208 | |
| 1209 | size_type copy(pointer __s, size_type __n, size_type __pos = 0) const; |
| 1210 | basic_string substr(size_type __pos = 0, size_type __n = npos) const; |
| 1211 | |
| 1212 | void swap(basic_string& __str); |
| 1213 | |
| 1214 | _LIBCPP_INLINE_VISIBILITY const_pointer c_str() const {return data();} |
| 1215 | _LIBCPP_INLINE_VISIBILITY const_pointer data() const {return __get_pointer();} |
| 1216 | |
| 1217 | _LIBCPP_INLINE_VISIBILITY allocator_type get_allocator() const {return __alloc();} |
| 1218 | |
| 1219 | size_type find(const basic_string& __str, size_type __pos = 0) const; |
| 1220 | size_type find(const_pointer __s, size_type __pos, size_type __n) const; |
| 1221 | size_type find(const_pointer __s, size_type __pos = 0) const; |
| 1222 | size_type find(value_type __c, size_type __pos = 0) const; |
| 1223 | |
| 1224 | size_type rfind(const basic_string& __str, size_type __pos = npos) const; |
| 1225 | size_type rfind(const_pointer __s, size_type __pos, size_type __n) const; |
| 1226 | size_type rfind(const_pointer __s, size_type __pos = npos) const; |
| 1227 | size_type rfind(value_type __c, size_type __pos = npos) const; |
| 1228 | |
| 1229 | size_type find_first_of(const basic_string& __str, size_type __pos = 0) const; |
| 1230 | size_type find_first_of(const_pointer __s, size_type __pos, size_type __n) const; |
| 1231 | size_type find_first_of(const_pointer __s, size_type __pos = 0) const; |
| 1232 | size_type find_first_of(value_type __c, size_type __pos = 0) const; |
| 1233 | |
| 1234 | size_type find_last_of(const basic_string& __str, size_type __pos = npos) const; |
| 1235 | size_type find_last_of(const_pointer __s, size_type __pos, size_type __n) const; |
| 1236 | size_type find_last_of(const_pointer __s, size_type __pos = npos) const; |
| 1237 | size_type find_last_of(value_type __c, size_type __pos = npos) const; |
| 1238 | |
| 1239 | size_type find_first_not_of(const basic_string& __str, size_type __pos = 0) const; |
| 1240 | size_type find_first_not_of(const_pointer __s, size_type __pos, size_type __n) const; |
| 1241 | size_type find_first_not_of(const_pointer __s, size_type __pos = 0) const; |
| 1242 | size_type find_first_not_of(value_type __c, size_type __pos = 0) const; |
| 1243 | |
| 1244 | size_type find_last_not_of(const basic_string& __str, size_type __pos = npos) const; |
| 1245 | size_type find_last_not_of(const_pointer __s, size_type __pos, size_type __n) const; |
| 1246 | size_type find_last_not_of(const_pointer __s, size_type __pos = npos) const; |
| 1247 | size_type find_last_not_of(value_type __c, size_type __pos = npos) const; |
| 1248 | |
| 1249 | int compare(const basic_string& __str) const; |
| 1250 | int compare(size_type __pos1, size_type __n1, const basic_string& __str) const; |
| 1251 | int compare(size_type __pos1, size_type __n1, const basic_string& __str, size_type __pos2, size_type __n2) const; |
| 1252 | int compare(const_pointer __s) const; |
| 1253 | int compare(size_type __pos1, size_type __n1, const_pointer __s) const; |
| 1254 | int compare(size_type __pos1, size_type __n1, const_pointer __s, size_type __n2) const; |
| 1255 | |
| 1256 | bool __invariants() const; |
| 1257 | private: |
| 1258 | _LIBCPP_INLINE_VISIBILITY allocator_type& __alloc() {return __r_.second();} |
| 1259 | _LIBCPP_INLINE_VISIBILITY const allocator_type& __alloc() const {return __r_.second();} |
| 1260 | |
| 1261 | _LIBCPP_INLINE_VISIBILITY bool __is_long() const {return bool(__r_.first().__s.__size_ & __short_mask);} |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1262 | |
| 1263 | _LIBCPP_INLINE_VISIBILITY void __set_short_size(size_type __s) |
| 1264 | #if _LIBCPP_BIG_ENDIAN |
| 1265 | {__r_.first().__s.__size_ = (unsigned char)(__s);} |
| 1266 | #else |
| 1267 | {__r_.first().__s.__size_ = (unsigned char)(__s << 1);} |
| 1268 | #endif |
| 1269 | _LIBCPP_INLINE_VISIBILITY size_type __get_short_size() const |
| 1270 | #if _LIBCPP_BIG_ENDIAN |
| 1271 | {return __r_.first().__s.__size_;} |
| 1272 | #else |
| 1273 | {return __r_.first().__s.__size_ >> 1;} |
| 1274 | #endif |
| 1275 | _LIBCPP_INLINE_VISIBILITY void __set_long_size(size_type __s) {__r_.first().__l.__size_ = __s;} |
| 1276 | _LIBCPP_INLINE_VISIBILITY size_type __get_long_size() const {return __r_.first().__l.__size_;} |
| 1277 | _LIBCPP_INLINE_VISIBILITY void __set_size(size_type __s) |
| 1278 | {if (__is_long()) __set_long_size(__s); else __set_short_size(__s);} |
| 1279 | |
| 1280 | _LIBCPP_INLINE_VISIBILITY void __set_long_cap(size_type __s) {__r_.first().__l.__cap_ = __long_mask | __s;} |
| 1281 | _LIBCPP_INLINE_VISIBILITY size_type __get_long_cap() const {return __r_.first().__l.__cap_ & ~__long_mask;} |
| 1282 | |
| 1283 | _LIBCPP_INLINE_VISIBILITY void __set_long_pointer(pointer __p) {__r_.first().__l.__data_ = __p;} |
| 1284 | _LIBCPP_INLINE_VISIBILITY pointer __get_long_pointer() {return __r_.first().__l.__data_;} |
| 1285 | _LIBCPP_INLINE_VISIBILITY const_pointer __get_long_pointer() const {return __r_.first().__l.__data_;} |
| 1286 | _LIBCPP_INLINE_VISIBILITY pointer __get_short_pointer() {return __r_.first().__s.__data_;} |
| 1287 | _LIBCPP_INLINE_VISIBILITY const_pointer __get_short_pointer() const {return __r_.first().__s.__data_;} |
| 1288 | _LIBCPP_INLINE_VISIBILITY pointer __get_pointer() |
| 1289 | {return __is_long() ? __get_long_pointer() : __get_short_pointer();} |
| 1290 | _LIBCPP_INLINE_VISIBILITY const_pointer __get_pointer() const |
| 1291 | {return __is_long() ? __get_long_pointer() : __get_short_pointer();} |
| 1292 | |
| 1293 | _LIBCPP_INLINE_VISIBILITY void __zero() |
| 1294 | { |
| 1295 | size_type (&__a)[__n_words] = __r_.first().__r.__words; |
| 1296 | for (unsigned __i = 0; __i < __n_words; ++__i) |
| 1297 | __a[__i] = 0; |
| 1298 | } |
| 1299 | |
| 1300 | template <size_type __a> static |
| 1301 | _LIBCPP_INLINE_VISIBILITY size_type __align(size_type __s) {return __s + (__a-1) & ~(__a-1);} |
| 1302 | enum {__alignment = 16}; |
| 1303 | static _LIBCPP_INLINE_VISIBILITY size_type __recommend(size_type __s) |
| 1304 | {return (__s < __min_cap ? __min_cap : |
| 1305 | __align<sizeof(value_type) < __alignment ? __alignment/sizeof(value_type) : 1>(__s+1)) - 1;} |
| 1306 | |
| 1307 | void __init(const_pointer __s, size_type __sz, size_type __reserve); |
| 1308 | void __init(const_pointer __s, size_type __sz); |
| 1309 | void __init(size_type __n, value_type __c); |
| 1310 | |
| 1311 | template <class _InputIterator> |
| 1312 | typename enable_if |
| 1313 | < |
| 1314 | __is_input_iterator <_InputIterator>::value && |
| 1315 | !__is_forward_iterator<_InputIterator>::value, |
| 1316 | void |
| 1317 | >::type |
| 1318 | __init(_InputIterator __first, _InputIterator __last); |
| 1319 | |
| 1320 | template <class _ForwardIterator> |
| 1321 | typename enable_if |
| 1322 | < |
| 1323 | __is_forward_iterator<_ForwardIterator>::value, |
| 1324 | void |
| 1325 | >::type |
| 1326 | __init(_ForwardIterator __first, _ForwardIterator __last); |
| 1327 | |
| 1328 | void __grow_by(size_type __old_cap, size_type __delta_cap, size_type __old_sz, |
| 1329 | size_type __n_copy, size_type __n_del, size_type __n_add = 0); |
| 1330 | void __grow_by_and_replace(size_type __old_cap, size_type __delta_cap, size_type __old_sz, |
| 1331 | size_type __n_copy, size_type __n_del, |
| 1332 | size_type __n_add, const_pointer __p_new_stuff); |
| 1333 | |
| 1334 | void __erase_to_end(size_type __pos); |
| 1335 | |
| 1336 | void __invalidate_all_iterators(); |
| 1337 | void __invalidate_iterators_past(size_type); |
| 1338 | |
| 1339 | friend basic_string operator+<>(const basic_string&, const basic_string&); |
| 1340 | friend basic_string operator+<>(const value_type*, const basic_string&); |
| 1341 | friend basic_string operator+<>(value_type, const basic_string&); |
| 1342 | friend basic_string operator+<>(const basic_string&, const value_type*); |
| 1343 | friend basic_string operator+<>(const basic_string&, value_type); |
| 1344 | }; |
| 1345 | |
| 1346 | template <class _CharT, class _Traits, class _Allocator> |
| 1347 | #ifndef _LIBCPP_DEBUG |
| 1348 | _LIBCPP_INLINE_VISIBILITY inline |
| 1349 | #endif |
| 1350 | void |
| 1351 | basic_string<_CharT, _Traits, _Allocator>::__invalidate_all_iterators() |
| 1352 | { |
| 1353 | #ifdef _LIBCPP_DEBUG |
| 1354 | iterator::__remove_all(this); |
| 1355 | const_iterator::__remove_all(this); |
| 1356 | #endif |
| 1357 | } |
| 1358 | |
| 1359 | template <class _CharT, class _Traits, class _Allocator> |
| 1360 | #ifndef _LIBCPP_DEBUG |
| 1361 | _LIBCPP_INLINE_VISIBILITY inline |
| 1362 | #endif |
| 1363 | void |
| 1364 | basic_string<_CharT, _Traits, _Allocator>::__invalidate_iterators_past(size_type __pos) |
| 1365 | { |
| 1366 | #ifdef _LIBCPP_DEBUG |
| 1367 | const_iterator __beg = begin(); |
| 1368 | if (__iterator_list_.first) |
| 1369 | { |
| 1370 | for (iterator* __p = __iterator_list_.first; __p;) |
| 1371 | { |
| 1372 | if (*__p - __beg > static_cast<difference_type>(__pos)) |
| 1373 | { |
| 1374 | iterator* __n = __p; |
| 1375 | __p = __p->__next; |
| 1376 | __n->__remove_owner(); |
| 1377 | } |
| 1378 | else |
| 1379 | __p = __p->__next; |
| 1380 | } |
| 1381 | } |
| 1382 | if (__iterator_list_.second) |
| 1383 | { |
| 1384 | for (const_iterator* __p = __iterator_list_.second; __p;) |
| 1385 | { |
| 1386 | if (*__p - __beg > static_cast<difference_type>(__pos)) |
| 1387 | { |
| 1388 | const_iterator* __n = __p; |
| 1389 | __p = __p->__next; |
| 1390 | __n->__remove_owner(); |
| 1391 | } |
| 1392 | else |
| 1393 | __p = __p->__next; |
| 1394 | } |
| 1395 | } |
| 1396 | #endif |
| 1397 | } |
| 1398 | |
| 1399 | template <class _CharT, class _Traits, class _Allocator> |
| 1400 | _LIBCPP_INLINE_VISIBILITY inline |
| 1401 | basic_string<_CharT, _Traits, _Allocator>::basic_string() |
| 1402 | { |
| 1403 | __zero(); |
| 1404 | } |
| 1405 | |
| 1406 | template <class _CharT, class _Traits, class _Allocator> |
| 1407 | _LIBCPP_INLINE_VISIBILITY inline |
| 1408 | basic_string<_CharT, _Traits, _Allocator>::basic_string(const allocator_type& __a) |
| 1409 | : __r_(__a) |
| 1410 | { |
| 1411 | __zero(); |
| 1412 | } |
| 1413 | |
| 1414 | template <class _CharT, class _Traits, class _Allocator> |
| 1415 | void |
| 1416 | basic_string<_CharT, _Traits, _Allocator>::__init(const_pointer __s, size_type __sz, size_type __reserve) |
| 1417 | { |
| 1418 | if (__reserve > max_size()) |
| 1419 | this->__throw_length_error(); |
| 1420 | pointer __p; |
| 1421 | if (__reserve < __min_cap) |
| 1422 | { |
| 1423 | __set_short_size(__sz); |
| 1424 | __p = __get_short_pointer(); |
| 1425 | } |
| 1426 | else |
| 1427 | { |
| 1428 | size_type __cap = __recommend(__reserve); |
| 1429 | __p = __alloc().allocate(__cap+1); |
| 1430 | __set_long_pointer(__p); |
| 1431 | __set_long_cap(__cap+1); |
| 1432 | __set_long_size(__sz); |
| 1433 | } |
| 1434 | traits_type::copy(__p, __s, __sz); |
| 1435 | traits_type::assign(__p[__sz], value_type()); |
| 1436 | } |
| 1437 | |
| 1438 | template <class _CharT, class _Traits, class _Allocator> |
| 1439 | void |
| 1440 | basic_string<_CharT, _Traits, _Allocator>::__init(const_pointer __s, size_type __sz) |
| 1441 | { |
| 1442 | if (__sz > max_size()) |
| 1443 | this->__throw_length_error(); |
| 1444 | pointer __p; |
| 1445 | if (__sz < __min_cap) |
| 1446 | { |
| 1447 | __set_short_size(__sz); |
| 1448 | __p = __get_short_pointer(); |
| 1449 | } |
| 1450 | else |
| 1451 | { |
| 1452 | size_type __cap = __recommend(__sz); |
| 1453 | __p = __alloc().allocate(__cap+1); |
| 1454 | __set_long_pointer(__p); |
| 1455 | __set_long_cap(__cap+1); |
| 1456 | __set_long_size(__sz); |
| 1457 | } |
| 1458 | traits_type::copy(__p, __s, __sz); |
| 1459 | traits_type::assign(__p[__sz], value_type()); |
| 1460 | } |
| 1461 | |
| 1462 | template <class _CharT, class _Traits, class _Allocator> |
| 1463 | _LIBCPP_INLINE_VISIBILITY inline |
| 1464 | basic_string<_CharT, _Traits, _Allocator>::basic_string(const_pointer __s) |
| 1465 | { |
| 1466 | #ifdef _LIBCPP_DEBUG |
| 1467 | assert(__s != 0); |
| 1468 | #endif |
| 1469 | __init(__s, traits_type::length(__s)); |
| 1470 | } |
| 1471 | |
| 1472 | template <class _CharT, class _Traits, class _Allocator> |
| 1473 | _LIBCPP_INLINE_VISIBILITY inline |
| 1474 | basic_string<_CharT, _Traits, _Allocator>::basic_string(const_pointer __s, const allocator_type& __a) |
| 1475 | : __r_(__a) |
| 1476 | { |
| 1477 | #ifdef _LIBCPP_DEBUG |
| 1478 | assert(__s != 0); |
| 1479 | #endif |
| 1480 | __init(__s, traits_type::length(__s)); |
| 1481 | } |
| 1482 | |
| 1483 | template <class _CharT, class _Traits, class _Allocator> |
| 1484 | _LIBCPP_INLINE_VISIBILITY inline |
| 1485 | basic_string<_CharT, _Traits, _Allocator>::basic_string(const_pointer __s, size_type __n) |
| 1486 | { |
| 1487 | #ifdef _LIBCPP_DEBUG |
| 1488 | assert(__s != 0); |
| 1489 | #endif |
| 1490 | __init(__s, __n); |
| 1491 | } |
| 1492 | |
| 1493 | template <class _CharT, class _Traits, class _Allocator> |
| 1494 | _LIBCPP_INLINE_VISIBILITY inline |
| 1495 | basic_string<_CharT, _Traits, _Allocator>::basic_string(const_pointer __s, size_type __n, const allocator_type& __a) |
| 1496 | : __r_(__a) |
| 1497 | { |
| 1498 | #ifdef _LIBCPP_DEBUG |
| 1499 | assert(__s != 0); |
| 1500 | #endif |
| 1501 | __init(__s, __n); |
| 1502 | } |
| 1503 | |
| 1504 | template <class _CharT, class _Traits, class _Allocator> |
| 1505 | basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str) |
| 1506 | : __r_(__str.__alloc()) |
| 1507 | { |
| 1508 | if (!__str.__is_long()) |
| 1509 | __r_.first().__r = __str.__r_.first().__r; |
| 1510 | else |
| 1511 | __init(__str.__get_long_pointer(), __str.__get_long_size()); |
| 1512 | } |
| 1513 | |
| 1514 | template <class _CharT, class _Traits, class _Allocator> |
| 1515 | basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str, const allocator_type& __a) |
| 1516 | : __r_(__a) |
| 1517 | { |
| 1518 | if (!__str.__is_long()) |
| 1519 | __r_.first().__r = __str.__r_.first().__r; |
| 1520 | else |
| 1521 | __init(__str.__get_long_pointer(), __str.__get_long_size()); |
| 1522 | } |
| 1523 | |
| 1524 | #ifdef _LIBCPP_MOVE |
| 1525 | |
| 1526 | template <class _CharT, class _Traits, class _Allocator> |
| 1527 | _LIBCPP_INLINE_VISIBILITY inline |
| 1528 | basic_string<_CharT, _Traits, _Allocator>::basic_string(basic_string&& __str) |
| 1529 | : __r_(_STD::move(__str.__r_)) |
| 1530 | { |
| 1531 | __str.__zero(); |
| 1532 | #ifdef _LIBCPP_DEBUG |
| 1533 | __str.__invalidate_all_iterators(); |
| 1534 | #endif |
| 1535 | } |
| 1536 | |
| 1537 | template <class _CharT, class _Traits, class _Allocator> |
| 1538 | _LIBCPP_INLINE_VISIBILITY inline |
| 1539 | basic_string<_CharT, _Traits, _Allocator>::basic_string(basic_string&& __str, const allocator_type& __a) |
| 1540 | : __r_(__str.__r_.first(), __a) |
| 1541 | { |
| 1542 | __str.__zero(); |
| 1543 | #ifdef _LIBCPP_DEBUG |
| 1544 | __str.__invalidate_all_iterators(); |
| 1545 | #endif |
| 1546 | } |
| 1547 | |
| 1548 | #endif // _LIBCPP_MOVE |
| 1549 | |
| 1550 | template <class _CharT, class _Traits, class _Allocator> |
| 1551 | void |
| 1552 | basic_string<_CharT, _Traits, _Allocator>::__init(size_type __n, value_type __c) |
| 1553 | { |
| 1554 | if (__n > max_size()) |
| 1555 | this->__throw_length_error(); |
| 1556 | pointer __p; |
| 1557 | if (__n < __min_cap) |
| 1558 | { |
| 1559 | __set_short_size(__n); |
| 1560 | __p = __get_short_pointer(); |
| 1561 | } |
| 1562 | else |
| 1563 | { |
| 1564 | size_type __cap = __recommend(__n); |
| 1565 | __p = __alloc().allocate(__cap+1); |
| 1566 | __set_long_pointer(__p); |
| 1567 | __set_long_cap(__cap+1); |
| 1568 | __set_long_size(__n); |
| 1569 | } |
| 1570 | traits_type::assign(__p, __n, __c); |
| 1571 | traits_type::assign(__p[__n], value_type()); |
| 1572 | } |
| 1573 | |
| 1574 | template <class _CharT, class _Traits, class _Allocator> |
| 1575 | _LIBCPP_INLINE_VISIBILITY inline |
| 1576 | basic_string<_CharT, _Traits, _Allocator>::basic_string(size_type __n, value_type __c) |
| 1577 | { |
| 1578 | __init(__n, __c); |
| 1579 | } |
| 1580 | |
| 1581 | template <class _CharT, class _Traits, class _Allocator> |
| 1582 | _LIBCPP_INLINE_VISIBILITY inline |
| 1583 | basic_string<_CharT, _Traits, _Allocator>::basic_string(size_type __n, value_type __c, const allocator_type& __a) |
| 1584 | : __r_(__a) |
| 1585 | { |
| 1586 | __init(__n, __c); |
| 1587 | } |
| 1588 | |
| 1589 | |
| 1590 | template <class _CharT, class _Traits, class _Allocator> |
| 1591 | basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str, size_type __pos, size_type __n, |
| 1592 | const allocator_type& __a) |
| 1593 | : __r_(__a) |
| 1594 | { |
| 1595 | size_type __str_sz = __str.size(); |
| 1596 | if (__pos > __str_sz) |
| 1597 | this->__throw_out_of_range(); |
| 1598 | __init(__str.data() + __pos, _STD::min(__n, __str_sz - __pos)); |
| 1599 | } |
| 1600 | |
| 1601 | template <class _CharT, class _Traits, class _Allocator> |
| 1602 | template <class _InputIterator> |
| 1603 | typename enable_if |
| 1604 | < |
| 1605 | __is_input_iterator <_InputIterator>::value && |
| 1606 | !__is_forward_iterator<_InputIterator>::value, |
| 1607 | void |
| 1608 | >::type |
| 1609 | basic_string<_CharT, _Traits, _Allocator>::__init(_InputIterator __first, _InputIterator __last) |
| 1610 | { |
| 1611 | __zero(); |
| 1612 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 1613 | try |
| 1614 | { |
| 1615 | #endif |
| 1616 | for (; __first != __last; ++__first) |
| 1617 | push_back(*__first); |
| 1618 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 1619 | } |
| 1620 | catch (...) |
| 1621 | { |
| 1622 | if (__is_long()) |
| 1623 | __alloc().deallocate(__get_long_pointer(), __get_long_cap()); |
| 1624 | throw; |
| 1625 | } |
| 1626 | #endif |
| 1627 | } |
| 1628 | |
| 1629 | template <class _CharT, class _Traits, class _Allocator> |
| 1630 | template <class _ForwardIterator> |
| 1631 | typename enable_if |
| 1632 | < |
| 1633 | __is_forward_iterator<_ForwardIterator>::value, |
| 1634 | void |
| 1635 | >::type |
| 1636 | basic_string<_CharT, _Traits, _Allocator>::__init(_ForwardIterator __first, _ForwardIterator __last) |
| 1637 | { |
| 1638 | size_type __sz = static_cast<size_type>(_STD::distance(__first, __last)); |
| 1639 | if (__sz > max_size()) |
| 1640 | this->__throw_length_error(); |
| 1641 | pointer __p; |
| 1642 | if (__sz < __min_cap) |
| 1643 | { |
| 1644 | __set_short_size(__sz); |
| 1645 | __p = __get_short_pointer(); |
| 1646 | } |
| 1647 | else |
| 1648 | { |
| 1649 | size_type __cap = __recommend(__sz); |
| 1650 | __p = __alloc().allocate(__cap+1); |
| 1651 | __set_long_pointer(__p); |
| 1652 | __set_long_cap(__cap+1); |
| 1653 | __set_long_size(__sz); |
| 1654 | } |
| 1655 | for (; __first != __last; ++__first, ++__p) |
| 1656 | traits_type::assign(*__p, *__first); |
| 1657 | traits_type::assign(*__p, value_type()); |
| 1658 | } |
| 1659 | |
| 1660 | template <class _CharT, class _Traits, class _Allocator> |
| 1661 | template<class _InputIterator> |
| 1662 | _LIBCPP_INLINE_VISIBILITY inline |
| 1663 | basic_string<_CharT, _Traits, _Allocator>::basic_string(_InputIterator __first, _InputIterator __last) |
| 1664 | { |
| 1665 | __init(__first, __last); |
| 1666 | } |
| 1667 | |
| 1668 | template <class _CharT, class _Traits, class _Allocator> |
| 1669 | template<class _InputIterator> |
| 1670 | _LIBCPP_INLINE_VISIBILITY inline |
| 1671 | basic_string<_CharT, _Traits, _Allocator>::basic_string(_InputIterator __first, _InputIterator __last, |
| 1672 | const allocator_type& __a) |
| 1673 | : __r_(__a) |
| 1674 | { |
| 1675 | __init(__first, __last); |
| 1676 | } |
| 1677 | |
| 1678 | template <class _CharT, class _Traits, class _Allocator> |
| 1679 | _LIBCPP_INLINE_VISIBILITY inline |
| 1680 | basic_string<_CharT, _Traits, _Allocator>::basic_string(initializer_list<value_type> __il) |
| 1681 | { |
| 1682 | __init(__il.begin(), __il.end()); |
| 1683 | } |
| 1684 | |
| 1685 | template <class _CharT, class _Traits, class _Allocator> |
| 1686 | _LIBCPP_INLINE_VISIBILITY inline |
| 1687 | basic_string<_CharT, _Traits, _Allocator>::basic_string(initializer_list<value_type> __il, const allocator_type& __a) |
| 1688 | : __r_(__a) |
| 1689 | { |
| 1690 | __init(__il.begin(), __il.end()); |
| 1691 | } |
| 1692 | |
| 1693 | template <class _CharT, class _Traits, class _Allocator> |
| 1694 | _LIBCPP_INLINE_VISIBILITY inline |
| 1695 | basic_string<_CharT, _Traits, _Allocator>::~basic_string() |
| 1696 | { |
| 1697 | __invalidate_all_iterators(); |
| 1698 | if (__is_long()) |
| 1699 | __alloc().deallocate(__get_long_pointer(), __get_long_cap()); |
| 1700 | } |
| 1701 | |
| 1702 | template <class _CharT, class _Traits, class _Allocator> |
| 1703 | void |
| 1704 | basic_string<_CharT, _Traits, _Allocator>::__grow_by_and_replace |
| 1705 | (size_type __old_cap, size_type __delta_cap, size_type __old_sz, |
| 1706 | size_type __n_copy, size_type __n_del, size_type __n_add, const_pointer __p_new_stuff) |
| 1707 | { |
| 1708 | size_type __ms = max_size(); |
| 1709 | if (__delta_cap > __ms - __old_cap - 1) |
| 1710 | this->__throw_length_error(); |
| 1711 | pointer __old_p = __get_pointer(); |
| 1712 | size_type __cap = __old_cap < __ms / 2 - __alignment ? |
| 1713 | __recommend(_STD::max(__old_cap + __delta_cap, 2 * __old_cap)) : |
| 1714 | __ms - 1; |
| 1715 | pointer __p = __alloc().allocate(__cap+1); |
| 1716 | __invalidate_all_iterators(); |
| 1717 | if (__n_copy != 0) |
| 1718 | traits_type::copy(__p, __old_p, __n_copy); |
| 1719 | if (__n_add != 0) |
| 1720 | traits_type::copy(__p + __n_copy, __p_new_stuff, __n_add); |
| 1721 | size_type __sec_cp_sz = __old_sz - __n_del - __n_copy; |
| 1722 | if (__sec_cp_sz != 0) |
| 1723 | traits_type::copy(__p + __n_copy + __n_add, __old_p + __n_copy + __n_del, __sec_cp_sz); |
| 1724 | if (__old_cap+1 != __min_cap) |
| 1725 | __alloc().deallocate(__old_p, __old_cap+1); |
| 1726 | __set_long_pointer(__p); |
| 1727 | __set_long_cap(__cap+1); |
| 1728 | __old_sz = __n_copy + __n_add + __sec_cp_sz; |
| 1729 | __set_long_size(__old_sz); |
| 1730 | traits_type::assign(__p[__old_sz], value_type()); |
| 1731 | } |
| 1732 | |
| 1733 | template <class _CharT, class _Traits, class _Allocator> |
| 1734 | void |
| 1735 | basic_string<_CharT, _Traits, _Allocator>::__grow_by(size_type __old_cap, size_type __delta_cap, size_type __old_sz, |
| 1736 | size_type __n_copy, size_type __n_del, size_type __n_add) |
| 1737 | { |
| 1738 | size_type __ms = max_size(); |
| 1739 | if (__delta_cap > __ms - __old_cap - 1) |
| 1740 | this->__throw_length_error(); |
| 1741 | pointer __old_p = __get_pointer(); |
| 1742 | size_type __cap = __old_cap < __ms / 2 - __alignment ? |
| 1743 | __recommend(_STD::max(__old_cap + __delta_cap, 2 * __old_cap)) : |
| 1744 | __ms - 1; |
| 1745 | pointer __p = __alloc().allocate(__cap+1); |
| 1746 | __invalidate_all_iterators(); |
| 1747 | if (__n_copy != 0) |
| 1748 | traits_type::copy(__p, __old_p, __n_copy); |
| 1749 | size_type __sec_cp_sz = __old_sz - __n_del - __n_copy; |
| 1750 | if (__sec_cp_sz != 0) |
| 1751 | traits_type::copy(__p + __n_copy + __n_add, __old_p + __n_copy + __n_del, __sec_cp_sz); |
| 1752 | if (__old_cap+1 != __min_cap) |
| 1753 | __alloc().deallocate(__old_p, __old_cap+1); |
| 1754 | __set_long_pointer(__p); |
| 1755 | __set_long_cap(__cap+1); |
| 1756 | } |
| 1757 | |
| 1758 | // assign |
| 1759 | |
| 1760 | template <class _CharT, class _Traits, class _Allocator> |
| 1761 | basic_string<_CharT, _Traits, _Allocator>& |
| 1762 | basic_string<_CharT, _Traits, _Allocator>::assign(const_pointer __s, size_type __n) |
| 1763 | { |
| 1764 | #ifdef _LIBCPP_DEBUG |
| 1765 | assert(__s != 0); |
| 1766 | #endif |
| 1767 | size_type __cap = capacity(); |
| 1768 | if (__cap >= __n) |
| 1769 | { |
| 1770 | pointer __p = __get_pointer(); |
| 1771 | traits_type::move(__p, __s, __n); |
| 1772 | traits_type::assign(__p[__n], value_type()); |
| 1773 | __set_size(__n); |
| 1774 | __invalidate_iterators_past(__n); |
| 1775 | } |
| 1776 | else |
| 1777 | { |
| 1778 | size_type __sz = size(); |
| 1779 | __grow_by_and_replace(__cap, __n - __cap, __sz, 0, __sz, __n, __s); |
| 1780 | } |
| 1781 | return *this; |
| 1782 | } |
| 1783 | |
| 1784 | template <class _CharT, class _Traits, class _Allocator> |
| 1785 | basic_string<_CharT, _Traits, _Allocator>& |
| 1786 | basic_string<_CharT, _Traits, _Allocator>::assign(size_type __n, value_type __c) |
| 1787 | { |
| 1788 | size_type __cap = capacity(); |
| 1789 | if (__cap < __n) |
| 1790 | { |
| 1791 | size_type __sz = size(); |
| 1792 | __grow_by(__cap, __n - __cap, __sz, 0, __sz); |
| 1793 | } |
| 1794 | else |
| 1795 | __invalidate_iterators_past(__n); |
| 1796 | pointer __p = __get_pointer(); |
| 1797 | traits_type::assign(__p, __n, __c); |
| 1798 | traits_type::assign(__p[__n], value_type()); |
| 1799 | __set_size(__n); |
| 1800 | return *this; |
| 1801 | } |
| 1802 | |
| 1803 | template <class _CharT, class _Traits, class _Allocator> |
| 1804 | basic_string<_CharT, _Traits, _Allocator>& |
| 1805 | basic_string<_CharT, _Traits, _Allocator>::operator=(value_type __c) |
| 1806 | { |
| 1807 | pointer __p; |
| 1808 | if (__is_long()) |
| 1809 | { |
| 1810 | __p = __get_long_pointer(); |
| 1811 | __set_long_size(1); |
| 1812 | } |
| 1813 | else |
| 1814 | { |
| 1815 | __p = __get_short_pointer(); |
| 1816 | __set_short_size(1); |
| 1817 | } |
| 1818 | traits_type::assign(*__p, __c); |
| 1819 | traits_type::assign(*++__p, value_type()); |
| 1820 | __invalidate_iterators_past(1); |
| 1821 | return *this; |
| 1822 | } |
| 1823 | |
| 1824 | template <class _CharT, class _Traits, class _Allocator> |
| 1825 | template<class _InputIterator> |
| 1826 | typename enable_if |
| 1827 | < |
| 1828 | __is_input_iterator <_InputIterator>::value && |
| 1829 | !__is_forward_iterator<_InputIterator>::value, |
| 1830 | basic_string<_CharT, _Traits, _Allocator>& |
| 1831 | >::type |
| 1832 | basic_string<_CharT, _Traits, _Allocator>::assign(_InputIterator __first, _InputIterator __last) |
| 1833 | { |
| 1834 | clear(); |
| 1835 | for (; __first != __last; ++__first) |
| 1836 | push_back(*__first); |
| 1837 | } |
| 1838 | |
| 1839 | template <class _CharT, class _Traits, class _Allocator> |
| 1840 | template<class _ForwardIterator> |
| 1841 | typename enable_if |
| 1842 | < |
| 1843 | __is_forward_iterator<_ForwardIterator>::value, |
| 1844 | basic_string<_CharT, _Traits, _Allocator>& |
| 1845 | >::type |
| 1846 | basic_string<_CharT, _Traits, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last) |
| 1847 | { |
| 1848 | size_type __n = static_cast<size_type>(_STD::distance(__first, __last)); |
| 1849 | size_type __cap = capacity(); |
| 1850 | if (__cap < __n) |
| 1851 | { |
| 1852 | size_type __sz = size(); |
| 1853 | __grow_by(__cap, __n - __cap, __sz, 0, __sz); |
| 1854 | } |
| 1855 | else |
| 1856 | __invalidate_iterators_past(__n); |
| 1857 | pointer __p = __get_pointer(); |
| 1858 | for (; __first != __last; ++__first, ++__p) |
| 1859 | traits_type::assign(*__p, *__first); |
| 1860 | traits_type::assign(*__p, value_type()); |
| 1861 | __set_size(__n); |
| 1862 | return *this; |
| 1863 | } |
| 1864 | |
| 1865 | template <class _CharT, class _Traits, class _Allocator> |
| 1866 | _LIBCPP_INLINE_VISIBILITY inline |
| 1867 | basic_string<_CharT, _Traits, _Allocator>& |
| 1868 | basic_string<_CharT, _Traits, _Allocator>::assign(const basic_string& __str) |
| 1869 | { |
| 1870 | return assign(__str.data(), __str.size()); |
| 1871 | } |
| 1872 | |
| 1873 | template <class _CharT, class _Traits, class _Allocator> |
| 1874 | basic_string<_CharT, _Traits, _Allocator>& |
| 1875 | basic_string<_CharT, _Traits, _Allocator>::assign(const basic_string& __str, size_type __pos, size_type __n) |
| 1876 | { |
| 1877 | size_type __sz = __str.size(); |
| 1878 | if (__pos > __sz) |
| 1879 | this->__throw_out_of_range(); |
| 1880 | return assign(__str.data() + __pos, _STD::min(__n, __sz - __pos)); |
| 1881 | } |
| 1882 | |
| 1883 | template <class _CharT, class _Traits, class _Allocator> |
| 1884 | basic_string<_CharT, _Traits, _Allocator>& |
| 1885 | basic_string<_CharT, _Traits, _Allocator>::assign(const_pointer __s) |
| 1886 | { |
| 1887 | #ifdef _LIBCPP_DEBUG |
| 1888 | assert(__s != 0); |
| 1889 | #endif |
| 1890 | return assign(__s, traits_type::length(__s)); |
| 1891 | } |
| 1892 | |
| 1893 | // append |
| 1894 | |
| 1895 | template <class _CharT, class _Traits, class _Allocator> |
| 1896 | basic_string<_CharT, _Traits, _Allocator>& |
| 1897 | basic_string<_CharT, _Traits, _Allocator>::append(const_pointer __s, size_type __n) |
| 1898 | { |
| 1899 | #ifdef _LIBCPP_DEBUG |
| 1900 | assert(__s != 0); |
| 1901 | #endif |
| 1902 | size_type __cap = capacity(); |
| 1903 | size_type __sz = size(); |
| 1904 | if (__cap - __sz >= __n) |
| 1905 | { |
| 1906 | if (__n) |
| 1907 | { |
| 1908 | pointer __p = __get_pointer(); |
| 1909 | traits_type::copy(__p + __sz, __s, __n); |
| 1910 | __sz += __n; |
| 1911 | __set_size(__sz); |
| 1912 | traits_type::assign(__p[__sz], value_type()); |
| 1913 | } |
| 1914 | } |
| 1915 | else |
| 1916 | __grow_by_and_replace(__cap, __sz + __n - __cap, __sz, __sz, 0, __n, __s); |
| 1917 | return *this; |
| 1918 | } |
| 1919 | |
| 1920 | template <class _CharT, class _Traits, class _Allocator> |
| 1921 | basic_string<_CharT, _Traits, _Allocator>& |
| 1922 | basic_string<_CharT, _Traits, _Allocator>::append(size_type __n, value_type __c) |
| 1923 | { |
| 1924 | if (__n) |
| 1925 | { |
| 1926 | size_type __cap = capacity(); |
| 1927 | size_type __sz = size(); |
| 1928 | if (__cap - __sz < __n) |
| 1929 | __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0); |
| 1930 | pointer __p = __get_pointer(); |
| 1931 | traits_type::assign(__p + __sz, __n, __c); |
| 1932 | __sz += __n; |
| 1933 | __set_size(__sz); |
| 1934 | traits_type::assign(__p[__sz], value_type()); |
| 1935 | } |
| 1936 | return *this; |
| 1937 | } |
| 1938 | |
| 1939 | template <class _CharT, class _Traits, class _Allocator> |
| 1940 | void |
| 1941 | basic_string<_CharT, _Traits, _Allocator>::push_back(value_type __c) |
| 1942 | { |
| 1943 | size_type __cap = capacity(); |
| 1944 | size_type __sz = size(); |
| 1945 | if (__sz == __cap) |
| 1946 | __grow_by(__cap, 1, __sz, __sz, 0); |
| 1947 | pointer __p = __get_pointer() + __sz; |
| 1948 | traits_type::assign(*__p, __c); |
| 1949 | traits_type::assign(*++__p, value_type()); |
| 1950 | __set_size(__sz+1); |
| 1951 | } |
| 1952 | |
| 1953 | template <class _CharT, class _Traits, class _Allocator> |
| 1954 | template<class _InputIterator> |
| 1955 | typename enable_if |
| 1956 | < |
| 1957 | __is_input_iterator <_InputIterator>::value && |
| 1958 | !__is_forward_iterator<_InputIterator>::value, |
| 1959 | basic_string<_CharT, _Traits, _Allocator>& |
| 1960 | >::type |
| 1961 | basic_string<_CharT, _Traits, _Allocator>::append(_InputIterator __first, _InputIterator __last) |
| 1962 | { |
| 1963 | for (; __first != __last; ++__first) |
| 1964 | push_back(*__first); |
| 1965 | return *this; |
| 1966 | } |
| 1967 | |
| 1968 | template <class _CharT, class _Traits, class _Allocator> |
| 1969 | template<class _ForwardIterator> |
| 1970 | typename enable_if |
| 1971 | < |
| 1972 | __is_forward_iterator<_ForwardIterator>::value, |
| 1973 | basic_string<_CharT, _Traits, _Allocator>& |
| 1974 | >::type |
| 1975 | basic_string<_CharT, _Traits, _Allocator>::append(_ForwardIterator __first, _ForwardIterator __last) |
| 1976 | { |
| 1977 | size_type __sz = size(); |
| 1978 | size_type __cap = capacity(); |
| 1979 | size_type __n = static_cast<size_type>(_STD::distance(__first, __last)); |
| 1980 | if (__n) |
| 1981 | { |
| 1982 | if (__cap - __sz < __n) |
| 1983 | __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0); |
| 1984 | pointer __p = __get_pointer() + __sz; |
| 1985 | for (; __first != __last; ++__p, ++__first) |
| 1986 | traits_type::assign(*__p, *__first); |
| 1987 | traits_type::assign(*__p, value_type()); |
| 1988 | __set_size(__sz + __n); |
| 1989 | } |
| 1990 | return *this; |
| 1991 | } |
| 1992 | |
| 1993 | template <class _CharT, class _Traits, class _Allocator> |
| 1994 | _LIBCPP_INLINE_VISIBILITY inline |
| 1995 | basic_string<_CharT, _Traits, _Allocator>& |
| 1996 | basic_string<_CharT, _Traits, _Allocator>::append(const basic_string& __str) |
| 1997 | { |
| 1998 | return append(__str.data(), __str.size()); |
| 1999 | } |
| 2000 | |
| 2001 | template <class _CharT, class _Traits, class _Allocator> |
| 2002 | basic_string<_CharT, _Traits, _Allocator>& |
| 2003 | basic_string<_CharT, _Traits, _Allocator>::append(const basic_string& __str, size_type __pos, size_type __n) |
| 2004 | { |
| 2005 | size_type __sz = __str.size(); |
| 2006 | if (__pos > __sz) |
| 2007 | this->__throw_out_of_range(); |
| 2008 | return append(__str.data() + __pos, _STD::min(__n, __sz - __pos)); |
| 2009 | } |
| 2010 | |
| 2011 | template <class _CharT, class _Traits, class _Allocator> |
| 2012 | basic_string<_CharT, _Traits, _Allocator>& |
| 2013 | basic_string<_CharT, _Traits, _Allocator>::append(const_pointer __s) |
| 2014 | { |
| 2015 | #ifdef _LIBCPP_DEBUG |
| 2016 | assert(__s != 0); |
| 2017 | #endif |
| 2018 | return append(__s, traits_type::length(__s)); |
| 2019 | } |
| 2020 | |
| 2021 | // insert |
| 2022 | |
| 2023 | template <class _CharT, class _Traits, class _Allocator> |
| 2024 | basic_string<_CharT, _Traits, _Allocator>& |
| 2025 | basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, const_pointer __s, size_type __n) |
| 2026 | { |
| 2027 | #ifdef _LIBCPP_DEBUG |
| 2028 | assert(__s != 0); |
| 2029 | #endif |
| 2030 | size_type __sz = size(); |
| 2031 | if (__pos > __sz) |
| 2032 | this->__throw_out_of_range(); |
| 2033 | size_type __cap = capacity(); |
| 2034 | if (__cap - __sz >= __n) |
| 2035 | { |
| 2036 | if (__n) |
| 2037 | { |
| 2038 | pointer __p = __get_pointer(); |
| 2039 | size_type __n_move = __sz - __pos; |
| 2040 | if (__n_move != 0) |
| 2041 | { |
| 2042 | if (__p + __pos <= __s && __s < __p + __sz) |
| 2043 | __s += __n; |
| 2044 | traits_type::move(__p + __pos + __n, __p + __pos, __n_move); |
| 2045 | } |
| 2046 | traits_type::move(__p + __pos, __s, __n); |
| 2047 | __sz += __n; |
| 2048 | __set_size(__sz); |
| 2049 | traits_type::assign(__p[__sz], value_type()); |
| 2050 | } |
| 2051 | } |
| 2052 | else |
| 2053 | __grow_by_and_replace(__cap, __sz + __n - __cap, __sz, __pos, 0, __n, __s); |
| 2054 | return *this; |
| 2055 | } |
| 2056 | |
| 2057 | template <class _CharT, class _Traits, class _Allocator> |
| 2058 | basic_string<_CharT, _Traits, _Allocator>& |
| 2059 | basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, size_type __n, value_type __c) |
| 2060 | { |
| 2061 | size_type __sz = size(); |
| 2062 | if (__pos > __sz) |
| 2063 | this->__throw_out_of_range(); |
| 2064 | if (__n) |
| 2065 | { |
| 2066 | size_type __cap = capacity(); |
| 2067 | pointer __p; |
| 2068 | if (__cap - __sz >= __n) |
| 2069 | { |
| 2070 | __p = __get_pointer(); |
| 2071 | size_type __n_move = __sz - __pos; |
| 2072 | if (__n_move != 0) |
| 2073 | traits_type::move(__p + __pos + __n, __p + __pos, __n_move); |
| 2074 | } |
| 2075 | else |
| 2076 | { |
| 2077 | __grow_by(__cap, __sz + __n - __cap, __sz, __pos, 0, __n); |
| 2078 | __p = __get_long_pointer(); |
| 2079 | } |
| 2080 | traits_type::assign(__p + __pos, __n, __c); |
| 2081 | __sz += __n; |
| 2082 | __set_size(__sz); |
| 2083 | traits_type::assign(__p[__sz], value_type()); |
| 2084 | } |
| 2085 | return *this; |
| 2086 | } |
| 2087 | |
| 2088 | template <class _CharT, class _Traits, class _Allocator> |
| 2089 | template<class _InputIterator> |
| 2090 | typename enable_if |
| 2091 | < |
| 2092 | __is_input_iterator <_InputIterator>::value && |
| 2093 | !__is_forward_iterator<_InputIterator>::value, |
| 2094 | typename basic_string<_CharT, _Traits, _Allocator>::iterator |
| 2095 | >::type |
| 2096 | basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, _InputIterator __first, _InputIterator __last) |
| 2097 | { |
| 2098 | size_type __old_sz = size(); |
| 2099 | difference_type __ip = __pos - begin(); |
| 2100 | for (; __first != __last; ++__first) |
| 2101 | push_back(*__first); |
| 2102 | pointer __p = __get_pointer(); |
| 2103 | _STD::rotate(__p + __ip, __p + __old_sz, __p + size()); |
| 2104 | return iterator(__p + __ip); |
| 2105 | } |
| 2106 | |
| 2107 | template <class _CharT, class _Traits, class _Allocator> |
| 2108 | template<class _ForwardIterator> |
| 2109 | typename enable_if |
| 2110 | < |
| 2111 | __is_forward_iterator<_ForwardIterator>::value, |
| 2112 | typename basic_string<_CharT, _Traits, _Allocator>::iterator |
| 2113 | >::type |
| 2114 | basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, _ForwardIterator __first, _ForwardIterator __last) |
| 2115 | { |
| 2116 | size_type __ip = static_cast<size_type>(__pos - begin()); |
| 2117 | size_type __sz = size(); |
| 2118 | size_type __cap = capacity(); |
| 2119 | size_type __n = static_cast<size_type>(_STD::distance(__first, __last)); |
| 2120 | if (__n) |
| 2121 | { |
| 2122 | pointer __p; |
| 2123 | if (__cap - __sz >= __n) |
| 2124 | { |
| 2125 | __p = __get_pointer(); |
| 2126 | size_type __n_move = __sz - __ip; |
| 2127 | if (__n_move != 0) |
| 2128 | traits_type::move(__p + __ip + __n, __p + __ip, __n_move); |
| 2129 | } |
| 2130 | else |
| 2131 | { |
| 2132 | __grow_by(__cap, __sz + __n - __cap, __sz, __ip, 0, __n); |
| 2133 | __p = __get_long_pointer(); |
| 2134 | } |
| 2135 | __sz += __n; |
| 2136 | __set_size(__sz); |
| 2137 | traits_type::assign(__p[__sz], value_type()); |
| 2138 | for (__p += __ip; __first != __last; ++__p, ++__first) |
| 2139 | traits_type::assign(*__p, *__first); |
| 2140 | } |
| 2141 | return begin() + __ip; |
| 2142 | } |
| 2143 | |
| 2144 | template <class _CharT, class _Traits, class _Allocator> |
| 2145 | _LIBCPP_INLINE_VISIBILITY inline |
| 2146 | basic_string<_CharT, _Traits, _Allocator>& |
| 2147 | basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const basic_string& __str) |
| 2148 | { |
| 2149 | return insert(__pos1, __str.data(), __str.size()); |
| 2150 | } |
| 2151 | |
| 2152 | template <class _CharT, class _Traits, class _Allocator> |
| 2153 | basic_string<_CharT, _Traits, _Allocator>& |
| 2154 | basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const basic_string& __str, |
| 2155 | size_type __pos2, size_type __n) |
| 2156 | { |
| 2157 | size_type __str_sz = __str.size(); |
| 2158 | if (__pos2 > __str_sz) |
| 2159 | this->__throw_out_of_range(); |
| 2160 | return insert(__pos1, __str.data() + __pos2, _STD::min(__n, __str_sz - __pos2)); |
| 2161 | } |
| 2162 | |
| 2163 | template <class _CharT, class _Traits, class _Allocator> |
| 2164 | basic_string<_CharT, _Traits, _Allocator>& |
| 2165 | basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, const_pointer __s) |
| 2166 | { |
| 2167 | #ifdef _LIBCPP_DEBUG |
| 2168 | assert(__s != 0); |
| 2169 | #endif |
| 2170 | return insert(__pos, __s, traits_type::length(__s)); |
| 2171 | } |
| 2172 | |
| 2173 | template <class _CharT, class _Traits, class _Allocator> |
| 2174 | typename basic_string<_CharT, _Traits, _Allocator>::iterator |
| 2175 | basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, value_type __c) |
| 2176 | { |
| 2177 | size_type __ip = static_cast<size_type>(__pos - begin()); |
| 2178 | size_type __sz = size(); |
| 2179 | size_type __cap = capacity(); |
| 2180 | pointer __p; |
| 2181 | if (__cap == __sz) |
| 2182 | { |
| 2183 | __grow_by(__cap, 1, __sz, __ip, 0, 1); |
| 2184 | __p = __get_long_pointer(); |
| 2185 | } |
| 2186 | else |
| 2187 | { |
| 2188 | __p = __get_pointer(); |
| 2189 | size_type __n_move = __sz - __ip; |
| 2190 | if (__n_move != 0) |
| 2191 | traits_type::move(__p + __ip + 1, __p + __ip, __n_move); |
| 2192 | } |
| 2193 | traits_type::assign(__p[__ip], __c); |
| 2194 | traits_type::assign(__p[++__sz], value_type()); |
| 2195 | __set_size(__sz); |
| 2196 | return begin() + static_cast<difference_type>(__ip); |
| 2197 | } |
| 2198 | |
| 2199 | template <class _CharT, class _Traits, class _Allocator> |
| 2200 | _LIBCPP_INLINE_VISIBILITY inline |
| 2201 | typename basic_string<_CharT, _Traits, _Allocator>::iterator |
| 2202 | basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, size_type __n, value_type __c) |
| 2203 | { |
| 2204 | difference_type __p = __pos - begin(); |
| 2205 | insert(static_cast<size_type>(__p), __n, __c); |
| 2206 | return begin() + __p; |
| 2207 | } |
| 2208 | |
| 2209 | // replace |
| 2210 | |
| 2211 | template <class _CharT, class _Traits, class _Allocator> |
| 2212 | basic_string<_CharT, _Traits, _Allocator>& |
| 2213 | basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos, size_type __n1, const_pointer __s, size_type __n2) |
| 2214 | { |
| 2215 | #ifdef _LIBCPP_DEBUG |
| 2216 | assert(__s != 0); |
| 2217 | #endif |
| 2218 | size_type __sz = size(); |
| 2219 | if (__pos > __sz) |
| 2220 | this->__throw_out_of_range(); |
| 2221 | __n1 = _STD::min(__n1, __sz - __pos); |
| 2222 | size_type __cap = capacity(); |
| 2223 | if (__cap - __sz + __n1 >= __n2) |
| 2224 | { |
| 2225 | pointer __p = __get_pointer(); |
| 2226 | if (__n1 != __n2) |
| 2227 | { |
| 2228 | size_type __n_move = __sz - __pos - __n1; |
| 2229 | if (__n_move != 0) |
| 2230 | { |
| 2231 | if (__n1 > __n2) |
| 2232 | { |
| 2233 | traits_type::move(__p + __pos, __s, __n2); |
| 2234 | traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move); |
| 2235 | goto __finish; |
| 2236 | } |
| 2237 | if (__p + __pos < __s && __s < __p + __sz) |
| 2238 | { |
| 2239 | if (__p + __pos + __n1 <= __s) |
| 2240 | __s += __n2 - __n1; |
| 2241 | else // __p + __pos < __s < __p + __pos + __n1 |
| 2242 | { |
| 2243 | traits_type::move(__p + __pos, __s, __n1); |
| 2244 | __pos += __n1; |
| 2245 | __s += __n2; |
| 2246 | __n2 -= __n1; |
| 2247 | __n1 = 0; |
| 2248 | } |
| 2249 | } |
| 2250 | traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move); |
| 2251 | } |
| 2252 | } |
| 2253 | traits_type::move(__p + __pos, __s, __n2); |
| 2254 | __finish: |
| 2255 | __sz += __n2 - __n1; |
| 2256 | __set_size(__sz); |
| 2257 | __invalidate_iterators_past(__sz); |
| 2258 | traits_type::assign(__p[__sz], value_type()); |
| 2259 | } |
| 2260 | else |
| 2261 | __grow_by_and_replace(__cap, __sz - __n1 + __n2 - __cap, __sz, __pos, __n1, __n2, __s); |
| 2262 | return *this; |
| 2263 | } |
| 2264 | |
| 2265 | template <class _CharT, class _Traits, class _Allocator> |
| 2266 | basic_string<_CharT, _Traits, _Allocator>& |
| 2267 | basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos, size_type __n1, size_type __n2, value_type __c) |
| 2268 | { |
| 2269 | size_type __sz = size(); |
| 2270 | if (__pos > __sz) |
| 2271 | this->__throw_out_of_range(); |
| 2272 | __n1 = _STD::min(__n1, __sz - __pos); |
| 2273 | size_type __cap = capacity(); |
| 2274 | pointer __p; |
| 2275 | if (__cap - __sz + __n1 >= __n2) |
| 2276 | { |
| 2277 | __p = __get_pointer(); |
| 2278 | if (__n1 != __n2) |
| 2279 | { |
| 2280 | size_type __n_move = __sz - __pos - __n1; |
| 2281 | if (__n_move != 0) |
| 2282 | traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move); |
| 2283 | } |
| 2284 | } |
| 2285 | else |
| 2286 | { |
| 2287 | __grow_by(__cap, __sz - __n1 + __n2 - __cap, __sz, __pos, __n1, __n2); |
| 2288 | __p = __get_long_pointer(); |
| 2289 | } |
| 2290 | traits_type::assign(__p + __pos, __n2, __c); |
| 2291 | __sz += __n2 - __n1; |
| 2292 | __set_size(__sz); |
| 2293 | __invalidate_iterators_past(__sz); |
| 2294 | traits_type::assign(__p[__sz], value_type()); |
| 2295 | return *this; |
| 2296 | } |
| 2297 | |
| 2298 | template <class _CharT, class _Traits, class _Allocator> |
| 2299 | template<class _InputIterator> |
| 2300 | typename enable_if |
| 2301 | < |
| 2302 | __is_input_iterator<_InputIterator>::value, |
| 2303 | basic_string<_CharT, _Traits, _Allocator>& |
| 2304 | >::type |
| 2305 | basic_string<_CharT, _Traits, _Allocator>::replace(iterator __i1, iterator __i2, |
| 2306 | _InputIterator __j1, _InputIterator __j2) |
| 2307 | { |
| 2308 | for (; true; ++__i1, ++__j1) |
| 2309 | { |
| 2310 | if (__i1 == __i2) |
| 2311 | { |
| 2312 | if (__j1 != __j2) |
| 2313 | insert(__i1, __j1, __j2); |
| 2314 | break; |
| 2315 | } |
| 2316 | if (__j1 == __j2) |
| 2317 | { |
| 2318 | erase(__i1, __i2); |
| 2319 | break; |
| 2320 | } |
| 2321 | traits_type::assign(*__i1, *__j1); |
| 2322 | } |
| 2323 | return *this; |
| 2324 | } |
| 2325 | |
| 2326 | template <class _CharT, class _Traits, class _Allocator> |
| 2327 | _LIBCPP_INLINE_VISIBILITY inline |
| 2328 | basic_string<_CharT, _Traits, _Allocator>& |
| 2329 | basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const basic_string& __str) |
| 2330 | { |
| 2331 | return replace(__pos1, __n1, __str.data(), __str.size()); |
| 2332 | } |
| 2333 | |
| 2334 | template <class _CharT, class _Traits, class _Allocator> |
| 2335 | basic_string<_CharT, _Traits, _Allocator>& |
| 2336 | basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const basic_string& __str, |
| 2337 | size_type __pos2, size_type __n2) |
| 2338 | { |
| 2339 | size_type __str_sz = __str.size(); |
| 2340 | if (__pos2 > __str_sz) |
| 2341 | this->__throw_out_of_range(); |
| 2342 | return replace(__pos1, __n1, __str.data() + __pos2, _STD::min(__n2, __str_sz - __pos2)); |
| 2343 | } |
| 2344 | |
| 2345 | template <class _CharT, class _Traits, class _Allocator> |
| 2346 | basic_string<_CharT, _Traits, _Allocator>& |
| 2347 | basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos, size_type __n1, const_pointer __s) |
| 2348 | { |
| 2349 | #ifdef _LIBCPP_DEBUG |
| 2350 | assert(__s != 0); |
| 2351 | #endif |
| 2352 | return replace(__pos, __n1, __s, traits_type::length(__s)); |
| 2353 | } |
| 2354 | |
| 2355 | template <class _CharT, class _Traits, class _Allocator> |
| 2356 | _LIBCPP_INLINE_VISIBILITY inline |
| 2357 | basic_string<_CharT, _Traits, _Allocator>& |
| 2358 | basic_string<_CharT, _Traits, _Allocator>::replace(iterator __i1, iterator __i2, const basic_string& __str) |
| 2359 | { |
| 2360 | return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), |
| 2361 | __str.data(), __str.size()); |
| 2362 | } |
| 2363 | |
| 2364 | template <class _CharT, class _Traits, class _Allocator> |
| 2365 | _LIBCPP_INLINE_VISIBILITY inline |
| 2366 | basic_string<_CharT, _Traits, _Allocator>& |
| 2367 | basic_string<_CharT, _Traits, _Allocator>::replace(iterator __i1, iterator __i2, const_pointer __s, size_type __n) |
| 2368 | { |
| 2369 | return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __s, __n); |
| 2370 | } |
| 2371 | |
| 2372 | template <class _CharT, class _Traits, class _Allocator> |
| 2373 | _LIBCPP_INLINE_VISIBILITY inline |
| 2374 | basic_string<_CharT, _Traits, _Allocator>& |
| 2375 | basic_string<_CharT, _Traits, _Allocator>::replace(iterator __i1, iterator __i2, const_pointer __s) |
| 2376 | { |
| 2377 | return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __s); |
| 2378 | } |
| 2379 | |
| 2380 | template <class _CharT, class _Traits, class _Allocator> |
| 2381 | _LIBCPP_INLINE_VISIBILITY inline |
| 2382 | basic_string<_CharT, _Traits, _Allocator>& |
| 2383 | basic_string<_CharT, _Traits, _Allocator>::replace(iterator __i1, iterator __i2, size_type __n, value_type __c) |
| 2384 | { |
| 2385 | return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __n, __c); |
| 2386 | } |
| 2387 | |
| 2388 | // erase |
| 2389 | |
| 2390 | template <class _CharT, class _Traits, class _Allocator> |
| 2391 | basic_string<_CharT, _Traits, _Allocator>& |
| 2392 | basic_string<_CharT, _Traits, _Allocator>::erase(size_type __pos, size_type __n) |
| 2393 | { |
| 2394 | size_type __sz = size(); |
| 2395 | if (__pos > __sz) |
| 2396 | this->__throw_out_of_range(); |
| 2397 | if (__n) |
| 2398 | { |
| 2399 | pointer __p = __get_pointer(); |
| 2400 | __n = _STD::min(__n, __sz - __pos); |
| 2401 | size_type __n_move = __sz - __pos - __n; |
| 2402 | if (__n_move != 0) |
| 2403 | traits_type::move(__p + __pos, __p + __pos + __n, __n_move); |
| 2404 | __sz -= __n; |
| 2405 | __set_size(__sz); |
| 2406 | __invalidate_iterators_past(__sz); |
| 2407 | traits_type::assign(__p[__sz], value_type()); |
| 2408 | } |
| 2409 | return *this; |
| 2410 | } |
| 2411 | |
| 2412 | template <class _CharT, class _Traits, class _Allocator> |
| 2413 | _LIBCPP_INLINE_VISIBILITY inline |
| 2414 | typename basic_string<_CharT, _Traits, _Allocator>::iterator |
| 2415 | basic_string<_CharT, _Traits, _Allocator>::erase(const_iterator __pos) |
| 2416 | { |
| 2417 | iterator __b = begin(); |
| 2418 | size_type __r = static_cast<size_type>(__pos - __b); |
| 2419 | erase(__r, 1); |
| 2420 | return __b + __r; |
| 2421 | } |
| 2422 | |
| 2423 | template <class _CharT, class _Traits, class _Allocator> |
| 2424 | _LIBCPP_INLINE_VISIBILITY inline |
| 2425 | typename basic_string<_CharT, _Traits, _Allocator>::iterator |
| 2426 | basic_string<_CharT, _Traits, _Allocator>::erase(const_iterator __first, const_iterator __last) |
| 2427 | { |
| 2428 | iterator __b = begin(); |
| 2429 | size_type __r = static_cast<size_type>(__first - __b); |
| 2430 | erase(__r, static_cast<size_type>(__last - __first)); |
| 2431 | return __b + __r; |
| 2432 | } |
| 2433 | |
| 2434 | template <class _CharT, class _Traits, class _Allocator> |
| 2435 | _LIBCPP_INLINE_VISIBILITY inline |
| 2436 | void |
| 2437 | basic_string<_CharT, _Traits, _Allocator>::pop_back() |
| 2438 | { |
| 2439 | #ifdef _LIBCPP_DEBUG |
| 2440 | assert(!empty()); |
| 2441 | #endif |
| 2442 | size_type __sz; |
| 2443 | if (__is_long()) |
| 2444 | { |
| 2445 | __sz = __get_long_size() - 1; |
| 2446 | __set_long_size(__sz); |
| 2447 | traits_type::assign(*(__get_long_pointer() + __sz), value_type()); |
| 2448 | } |
| 2449 | else |
| 2450 | { |
| 2451 | __sz = __get_short_size() - 1; |
| 2452 | __set_short_size(__sz); |
| 2453 | traits_type::assign(*(__get_short_pointer() + __sz), value_type()); |
| 2454 | } |
| 2455 | __invalidate_iterators_past(__sz); |
| 2456 | } |
| 2457 | |
| 2458 | template <class _CharT, class _Traits, class _Allocator> |
| 2459 | _LIBCPP_INLINE_VISIBILITY inline |
| 2460 | void |
| 2461 | basic_string<_CharT, _Traits, _Allocator>::clear() |
| 2462 | { |
| 2463 | __invalidate_all_iterators(); |
| 2464 | if (__is_long()) |
| 2465 | { |
| 2466 | traits_type::assign(*__get_long_pointer(), value_type()); |
| 2467 | __set_long_size(0); |
| 2468 | } |
| 2469 | else |
| 2470 | { |
| 2471 | traits_type::assign(*__get_short_pointer(), value_type()); |
| 2472 | __set_short_size(0); |
| 2473 | } |
| 2474 | } |
| 2475 | |
| 2476 | template <class _CharT, class _Traits, class _Allocator> |
| 2477 | _LIBCPP_INLINE_VISIBILITY inline |
| 2478 | void |
| 2479 | basic_string<_CharT, _Traits, _Allocator>::__erase_to_end(size_type __pos) |
| 2480 | { |
| 2481 | if (__is_long()) |
| 2482 | { |
| 2483 | traits_type::assign(*(__get_long_pointer() + __pos), value_type()); |
| 2484 | __set_long_size(__pos); |
| 2485 | } |
| 2486 | else |
| 2487 | { |
| 2488 | traits_type::assign(*(__get_short_pointer() + __pos), value_type()); |
| 2489 | __set_short_size(__pos); |
| 2490 | } |
| 2491 | __invalidate_iterators_past(__pos); |
| 2492 | } |
| 2493 | |
| 2494 | template <class _CharT, class _Traits, class _Allocator> |
| 2495 | void |
| 2496 | basic_string<_CharT, _Traits, _Allocator>::resize(size_type __n, value_type __c) |
| 2497 | { |
| 2498 | size_type __sz = size(); |
| 2499 | if (__n > __sz) |
| 2500 | append(__n - __sz, __c); |
| 2501 | else |
| 2502 | __erase_to_end(__n); |
| 2503 | } |
| 2504 | |
| 2505 | template <class _CharT, class _Traits, class _Allocator> |
| 2506 | _LIBCPP_INLINE_VISIBILITY inline |
| 2507 | typename basic_string<_CharT, _Traits, _Allocator>::size_type |
| 2508 | basic_string<_CharT, _Traits, _Allocator>::max_size() const |
| 2509 | { |
| 2510 | size_type __m = __alloc().max_size(); |
| 2511 | #if _LIBCPP_BIG_ENDIAN |
| 2512 | return (__m <= ~__long_mask ? __m : __m/2) - 1; |
| 2513 | #else |
| 2514 | return __m - 1; |
| 2515 | #endif |
| 2516 | } |
| 2517 | |
| 2518 | template <class _CharT, class _Traits, class _Allocator> |
| 2519 | void |
| 2520 | basic_string<_CharT, _Traits, _Allocator>::reserve(size_type __res_arg) |
| 2521 | { |
| 2522 | if (__res_arg > max_size()) |
| 2523 | this->__throw_length_error(); |
| 2524 | size_type __cap = capacity(); |
| 2525 | size_type __sz = size(); |
| 2526 | __res_arg = _STD::max(__res_arg, __sz); |
| 2527 | __res_arg = __recommend(__res_arg); |
| 2528 | if (__res_arg != __cap) |
| 2529 | { |
| 2530 | pointer __new_data, __p; |
| 2531 | bool __was_long, __now_long; |
| 2532 | if (__res_arg == __min_cap - 1) |
| 2533 | { |
| 2534 | __was_long = true; |
| 2535 | __now_long = false; |
| 2536 | __new_data = __get_short_pointer(); |
| 2537 | __p = __get_long_pointer(); |
| 2538 | } |
| 2539 | else |
| 2540 | { |
| 2541 | if (__res_arg > __cap) |
| 2542 | __new_data = __alloc().allocate(__res_arg+1); |
| 2543 | else |
| 2544 | { |
| 2545 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 2546 | try |
| 2547 | { |
| 2548 | #endif |
| 2549 | __new_data = __alloc().allocate(__res_arg+1); |
| 2550 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 2551 | } |
| 2552 | catch (...) |
| 2553 | { |
| 2554 | return; |
| 2555 | } |
| 2556 | #else |
| 2557 | if (__new_data == 0) |
| 2558 | return; |
| 2559 | #endif |
| 2560 | } |
| 2561 | __now_long = true; |
| 2562 | __was_long = __is_long(); |
| 2563 | __p = __get_pointer(); |
| 2564 | } |
| 2565 | traits_type::copy(__new_data, __p, size()+1); |
| 2566 | if (__was_long) |
| 2567 | __alloc().deallocate(__p, __cap+1); |
| 2568 | if (__now_long) |
| 2569 | { |
| 2570 | __set_long_cap(__res_arg+1); |
| 2571 | __set_long_size(__sz); |
| 2572 | __set_long_pointer(__new_data); |
| 2573 | } |
| 2574 | else |
| 2575 | __set_short_size(__sz); |
| 2576 | __invalidate_all_iterators(); |
| 2577 | } |
| 2578 | } |
| 2579 | |
| 2580 | template <class _CharT, class _Traits, class _Allocator> |
| 2581 | _LIBCPP_INLINE_VISIBILITY inline |
| 2582 | typename basic_string<_CharT, _Traits, _Allocator>::const_reference |
| 2583 | basic_string<_CharT, _Traits, _Allocator>::operator[](size_type __pos) const |
| 2584 | { |
| 2585 | #ifdef __LIBCPP_DEBUG |
| 2586 | assert(__pos <= size()); |
| 2587 | #endif |
| 2588 | return *(data() + __pos); |
| 2589 | } |
| 2590 | |
| 2591 | template <class _CharT, class _Traits, class _Allocator> |
| 2592 | _LIBCPP_INLINE_VISIBILITY inline |
| 2593 | typename basic_string<_CharT, _Traits, _Allocator>::reference |
| 2594 | basic_string<_CharT, _Traits, _Allocator>::operator[](size_type __pos) |
| 2595 | { |
| 2596 | #ifdef __LIBCPP_DEBUG |
| 2597 | assert(__pos < size()); |
| 2598 | #endif |
| 2599 | return *(__get_pointer() + __pos); |
| 2600 | } |
| 2601 | |
| 2602 | template <class _CharT, class _Traits, class _Allocator> |
| 2603 | typename basic_string<_CharT, _Traits, _Allocator>::const_reference |
| 2604 | basic_string<_CharT, _Traits, _Allocator>::at(size_type __n) const |
| 2605 | { |
| 2606 | if (__n >= size()) |
| 2607 | this->__throw_out_of_range(); |
| 2608 | return (*this)[__n]; |
| 2609 | } |
| 2610 | |
| 2611 | template <class _CharT, class _Traits, class _Allocator> |
| 2612 | typename basic_string<_CharT, _Traits, _Allocator>::reference |
| 2613 | basic_string<_CharT, _Traits, _Allocator>::at(size_type __n) |
| 2614 | { |
| 2615 | if (__n >= size()) |
| 2616 | this->__throw_out_of_range(); |
| 2617 | return (*this)[__n]; |
| 2618 | } |
| 2619 | |
| 2620 | template <class _CharT, class _Traits, class _Allocator> |
| 2621 | _LIBCPP_INLINE_VISIBILITY inline |
| 2622 | typename basic_string<_CharT, _Traits, _Allocator>::reference |
| 2623 | basic_string<_CharT, _Traits, _Allocator>::front() |
| 2624 | { |
| 2625 | #ifdef _LIBCPP_DEBUG |
| 2626 | assert(!empty()); |
| 2627 | #endif |
| 2628 | return *__get_pointer(); |
| 2629 | } |
| 2630 | |
| 2631 | template <class _CharT, class _Traits, class _Allocator> |
| 2632 | _LIBCPP_INLINE_VISIBILITY inline |
| 2633 | typename basic_string<_CharT, _Traits, _Allocator>::const_reference |
| 2634 | basic_string<_CharT, _Traits, _Allocator>::front() const |
| 2635 | { |
| 2636 | #ifdef _LIBCPP_DEBUG |
| 2637 | assert(!empty()); |
| 2638 | #endif |
| 2639 | return *data(); |
| 2640 | } |
| 2641 | |
| 2642 | template <class _CharT, class _Traits, class _Allocator> |
| 2643 | _LIBCPP_INLINE_VISIBILITY inline |
| 2644 | typename basic_string<_CharT, _Traits, _Allocator>::reference |
| 2645 | basic_string<_CharT, _Traits, _Allocator>::back() |
| 2646 | { |
| 2647 | #ifdef _LIBCPP_DEBUG |
| 2648 | assert(!empty()); |
| 2649 | #endif |
| 2650 | return *(__get_pointer() + size() - 1); |
| 2651 | } |
| 2652 | |
| 2653 | template <class _CharT, class _Traits, class _Allocator> |
| 2654 | _LIBCPP_INLINE_VISIBILITY inline |
| 2655 | typename basic_string<_CharT, _Traits, _Allocator>::const_reference |
| 2656 | basic_string<_CharT, _Traits, _Allocator>::back() const |
| 2657 | { |
| 2658 | #ifdef _LIBCPP_DEBUG |
| 2659 | assert(!empty()); |
| 2660 | #endif |
| 2661 | return *(data() + size() - 1); |
| 2662 | } |
| 2663 | |
| 2664 | template <class _CharT, class _Traits, class _Allocator> |
| 2665 | typename basic_string<_CharT, _Traits, _Allocator>::size_type |
| 2666 | basic_string<_CharT, _Traits, _Allocator>::copy(pointer __s, size_type __n, size_type __pos) const |
| 2667 | { |
| 2668 | size_type __sz = size(); |
| 2669 | if (__pos > __sz) |
| 2670 | this->__throw_out_of_range(); |
| 2671 | size_type __rlen = _STD::min(__n, __sz - __pos); |
| 2672 | traits_type::copy(__s, data() + __pos, __rlen); |
| 2673 | return __rlen; |
| 2674 | } |
| 2675 | |
| 2676 | template <class _CharT, class _Traits, class _Allocator> |
| 2677 | _LIBCPP_INLINE_VISIBILITY inline |
| 2678 | basic_string<_CharT, _Traits, _Allocator> |
| 2679 | basic_string<_CharT, _Traits, _Allocator>::substr(size_type __pos, size_type __n) const |
| 2680 | { |
| 2681 | return basic_string(*this, __pos, __n, __alloc()); |
| 2682 | } |
| 2683 | |
| 2684 | template <class _CharT, class _Traits, class _Allocator> |
| 2685 | _LIBCPP_INLINE_VISIBILITY inline |
| 2686 | void |
| 2687 | basic_string<_CharT, _Traits, _Allocator>::swap(basic_string& __str) |
| 2688 | { |
| 2689 | __r_.swap(__str.__r_); |
| 2690 | #ifdef _LIBCPP_DEBUG |
| 2691 | __invalidate_all_iterators(); |
| 2692 | __str.__invalidate_all_iterators(); |
| 2693 | #endif |
| 2694 | } |
| 2695 | |
| 2696 | // find |
| 2697 | |
| 2698 | template <class _Traits> |
| 2699 | struct _LIBCPP_HIDDEN __traits_eq |
| 2700 | { |
| 2701 | typedef typename _Traits::char_type char_type; |
| 2702 | _LIBCPP_INLINE_VISIBILITY bool operator()(const char_type& __x, const char_type& __y) {return _Traits::eq(__x, __y);} |
| 2703 | }; |
| 2704 | |
| 2705 | template<class _CharT, class _Traits, class _Allocator> |
| 2706 | typename basic_string<_CharT, _Traits, _Allocator>::size_type |
| 2707 | basic_string<_CharT, _Traits, _Allocator>::find(const_pointer __s, size_type __pos, size_type __n) const |
| 2708 | { |
| 2709 | #ifdef _LIBCPP_DEBUG |
| 2710 | assert(__s != 0); |
| 2711 | #endif |
| 2712 | size_type __sz = size(); |
| 2713 | if (__pos > __sz || __sz - __pos < __n) |
| 2714 | return npos; |
| 2715 | if (__n == 0) |
| 2716 | return __pos; |
| 2717 | const_pointer __p = data(); |
| 2718 | const_pointer __r = _STD::search(__p + __pos, __p + __sz, __s, __s + __n, __traits_eq<traits_type>()); |
| 2719 | if (__r == __p + __sz) |
| 2720 | return npos; |
| 2721 | return static_cast<size_type>(__r - __p); |
| 2722 | } |
| 2723 | |
| 2724 | template<class _CharT, class _Traits, class _Allocator> |
| 2725 | _LIBCPP_INLINE_VISIBILITY inline |
| 2726 | typename basic_string<_CharT, _Traits, _Allocator>::size_type |
| 2727 | basic_string<_CharT, _Traits, _Allocator>::find(const basic_string& __str, size_type __pos) const |
| 2728 | { |
| 2729 | return find(__str.data(), __pos, __str.size()); |
| 2730 | } |
| 2731 | |
| 2732 | template<class _CharT, class _Traits, class _Allocator> |
| 2733 | _LIBCPP_INLINE_VISIBILITY inline |
| 2734 | typename basic_string<_CharT, _Traits, _Allocator>::size_type |
| 2735 | basic_string<_CharT, _Traits, _Allocator>::find(const_pointer __s, size_type __pos) const |
| 2736 | { |
| 2737 | #ifdef _LIBCPP_DEBUG |
| 2738 | assert(__s != 0); |
| 2739 | #endif |
| 2740 | return find(__s, __pos, traits_type::length(__s)); |
| 2741 | } |
| 2742 | |
| 2743 | template<class _CharT, class _Traits, class _Allocator> |
| 2744 | typename basic_string<_CharT, _Traits, _Allocator>::size_type |
| 2745 | basic_string<_CharT, _Traits, _Allocator>::find(value_type __c, size_type __pos) const |
| 2746 | { |
| 2747 | size_type __sz = size(); |
| 2748 | if (__pos >= __sz) |
| 2749 | return npos; |
| 2750 | const_pointer __p = data(); |
| 2751 | const_pointer __r = traits_type::find(__p + __pos, __sz - __pos, __c); |
| 2752 | if (__r == 0) |
| 2753 | return npos; |
| 2754 | return static_cast<size_type>(__r - __p); |
| 2755 | } |
| 2756 | |
| 2757 | // rfind |
| 2758 | |
| 2759 | template<class _CharT, class _Traits, class _Allocator> |
| 2760 | typename basic_string<_CharT, _Traits, _Allocator>::size_type |
| 2761 | basic_string<_CharT, _Traits, _Allocator>::rfind(const_pointer __s, size_type __pos, size_type __n) const |
| 2762 | { |
| 2763 | #ifdef _LIBCPP_DEBUG |
| 2764 | assert(__s != 0); |
| 2765 | #endif |
| 2766 | size_type __sz = size(); |
| 2767 | __pos = _STD::min(__pos, __sz); |
| 2768 | if (__n < __sz - __pos) |
| 2769 | __pos += __n; |
| 2770 | else |
| 2771 | __pos = __sz; |
| 2772 | const_pointer __p = data(); |
| 2773 | const_pointer __r = _STD::find_end(__p, __p + __pos, __s, __s + __n, __traits_eq<traits_type>()); |
| 2774 | if (__n > 0 && __r == __p + __pos) |
| 2775 | return npos; |
| 2776 | return static_cast<size_type>(__r - __p); |
| 2777 | } |
| 2778 | |
| 2779 | template<class _CharT, class _Traits, class _Allocator> |
| 2780 | _LIBCPP_INLINE_VISIBILITY inline |
| 2781 | typename basic_string<_CharT, _Traits, _Allocator>::size_type |
| 2782 | basic_string<_CharT, _Traits, _Allocator>::rfind(const basic_string& __str, size_type __pos) const |
| 2783 | { |
| 2784 | return rfind(__str.data(), __pos, __str.size()); |
| 2785 | } |
| 2786 | |
| 2787 | template<class _CharT, class _Traits, class _Allocator> |
| 2788 | _LIBCPP_INLINE_VISIBILITY inline |
| 2789 | typename basic_string<_CharT, _Traits, _Allocator>::size_type |
| 2790 | basic_string<_CharT, _Traits, _Allocator>::rfind(const_pointer __s, size_type __pos) const |
| 2791 | { |
| 2792 | #ifdef _LIBCPP_DEBUG |
| 2793 | assert(__s != 0); |
| 2794 | #endif |
| 2795 | return rfind(__s, __pos, traits_type::length(__s)); |
| 2796 | } |
| 2797 | |
| 2798 | template<class _CharT, class _Traits, class _Allocator> |
| 2799 | typename basic_string<_CharT, _Traits, _Allocator>::size_type |
| 2800 | basic_string<_CharT, _Traits, _Allocator>::rfind(value_type __c, size_type __pos) const |
| 2801 | { |
| 2802 | size_type __sz = size(); |
| 2803 | if (__sz) |
| 2804 | { |
| 2805 | if (__pos < __sz) |
| 2806 | ++__pos; |
| 2807 | else |
| 2808 | __pos = __sz; |
| 2809 | const_pointer __p = data(); |
| 2810 | for (const_pointer __ps = __p + __pos; __ps != __p;) |
| 2811 | { |
| 2812 | if (traits_type::eq(*--__ps, __c)) |
| 2813 | return static_cast<size_type>(__ps - __p); |
| 2814 | } |
| 2815 | } |
| 2816 | return npos; |
| 2817 | } |
| 2818 | |
| 2819 | // find_first_of |
| 2820 | |
| 2821 | template<class _CharT, class _Traits, class _Allocator> |
| 2822 | typename basic_string<_CharT, _Traits, _Allocator>::size_type |
| 2823 | basic_string<_CharT, _Traits, _Allocator>::find_first_of(const_pointer __s, size_type __pos, size_type __n) const |
| 2824 | { |
| 2825 | #ifdef _LIBCPP_DEBUG |
| 2826 | assert(__s != 0); |
| 2827 | #endif |
| 2828 | size_type __sz = size(); |
| 2829 | if (__pos >= __sz || __n == 0) |
| 2830 | return npos; |
| 2831 | const_pointer __p = data(); |
| 2832 | const_pointer __r = _STD::find_first_of(__p + __pos, __p + __sz, __s, __s + __n, __traits_eq<traits_type>()); |
| 2833 | if (__r == __p + __sz) |
| 2834 | return npos; |
| 2835 | return static_cast<size_type>(__r - __p); |
| 2836 | } |
| 2837 | |
| 2838 | template<class _CharT, class _Traits, class _Allocator> |
| 2839 | _LIBCPP_INLINE_VISIBILITY inline |
| 2840 | typename basic_string<_CharT, _Traits, _Allocator>::size_type |
| 2841 | basic_string<_CharT, _Traits, _Allocator>::find_first_of(const basic_string& __str, size_type __pos) const |
| 2842 | { |
| 2843 | return find_first_of(__str.data(), __pos, __str.size()); |
| 2844 | } |
| 2845 | |
| 2846 | template<class _CharT, class _Traits, class _Allocator> |
| 2847 | _LIBCPP_INLINE_VISIBILITY inline |
| 2848 | typename basic_string<_CharT, _Traits, _Allocator>::size_type |
| 2849 | basic_string<_CharT, _Traits, _Allocator>::find_first_of(const_pointer __s, size_type __pos) const |
| 2850 | { |
| 2851 | #ifdef _LIBCPP_DEBUG |
| 2852 | assert(__s != 0); |
| 2853 | #endif |
| 2854 | return find_first_of(__s, __pos, traits_type::length(__s)); |
| 2855 | } |
| 2856 | |
| 2857 | template<class _CharT, class _Traits, class _Allocator> |
| 2858 | _LIBCPP_INLINE_VISIBILITY inline |
| 2859 | typename basic_string<_CharT, _Traits, _Allocator>::size_type |
| 2860 | basic_string<_CharT, _Traits, _Allocator>::find_first_of(value_type __c, size_type __pos) const |
| 2861 | { |
| 2862 | return find(__c, __pos); |
| 2863 | } |
| 2864 | |
| 2865 | // find_last_of |
| 2866 | |
| 2867 | template<class _CharT, class _Traits, class _Allocator> |
| 2868 | typename basic_string<_CharT, _Traits, _Allocator>::size_type |
| 2869 | basic_string<_CharT, _Traits, _Allocator>::find_last_of(const_pointer __s, size_type __pos, size_type __n) const |
| 2870 | { |
| 2871 | #ifdef _LIBCPP_DEBUG |
| 2872 | assert(__s != 0); |
| 2873 | #endif |
| 2874 | if (__n != 0) |
| 2875 | { |
| 2876 | size_type __sz = size(); |
| 2877 | if (__pos < __sz) |
| 2878 | ++__pos; |
| 2879 | else |
| 2880 | __pos = __sz; |
| 2881 | const_pointer __p = data(); |
| 2882 | for (const_pointer __ps = __p + __pos; __ps != __p;) |
| 2883 | { |
| 2884 | const_pointer __r = traits_type::find(__s, __n, *--__ps); |
| 2885 | if (__r) |
| 2886 | return static_cast<size_type>(__ps - __p); |
| 2887 | } |
| 2888 | } |
| 2889 | return npos; |
| 2890 | } |
| 2891 | |
| 2892 | template<class _CharT, class _Traits, class _Allocator> |
| 2893 | _LIBCPP_INLINE_VISIBILITY inline |
| 2894 | typename basic_string<_CharT, _Traits, _Allocator>::size_type |
| 2895 | basic_string<_CharT, _Traits, _Allocator>::find_last_of(const basic_string& __str, size_type __pos) const |
| 2896 | { |
| 2897 | return find_last_of(__str.data(), __pos, __str.size()); |
| 2898 | } |
| 2899 | |
| 2900 | template<class _CharT, class _Traits, class _Allocator> |
| 2901 | _LIBCPP_INLINE_VISIBILITY inline |
| 2902 | typename basic_string<_CharT, _Traits, _Allocator>::size_type |
| 2903 | basic_string<_CharT, _Traits, _Allocator>::find_last_of(const_pointer __s, size_type __pos) const |
| 2904 | { |
| 2905 | #ifdef _LIBCPP_DEBUG |
| 2906 | assert(__s != 0); |
| 2907 | #endif |
| 2908 | return find_last_of(__s, __pos, traits_type::length(__s)); |
| 2909 | } |
| 2910 | |
| 2911 | template<class _CharT, class _Traits, class _Allocator> |
| 2912 | _LIBCPP_INLINE_VISIBILITY inline |
| 2913 | typename basic_string<_CharT, _Traits, _Allocator>::size_type |
| 2914 | basic_string<_CharT, _Traits, _Allocator>::find_last_of(value_type __c, size_type __pos) const |
| 2915 | { |
| 2916 | return rfind(__c, __pos); |
| 2917 | } |
| 2918 | |
| 2919 | // find_first_not_of |
| 2920 | |
| 2921 | template<class _CharT, class _Traits, class _Allocator> |
| 2922 | typename basic_string<_CharT, _Traits, _Allocator>::size_type |
| 2923 | basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const_pointer __s, size_type __pos, size_type __n) const |
| 2924 | { |
| 2925 | #ifdef _LIBCPP_DEBUG |
| 2926 | assert(__s != 0); |
| 2927 | #endif |
| 2928 | size_type __sz = size(); |
| 2929 | if (__pos < __sz) |
| 2930 | { |
| 2931 | const_pointer __p = data(); |
| 2932 | const_pointer __pe = __p + __sz; |
| 2933 | for (const_pointer __ps = __p + __pos; __ps != __pe; ++__ps) |
| 2934 | if (traits_type::find(__s, __n, *__ps) == 0) |
| 2935 | return static_cast<size_type>(__ps - __p); |
| 2936 | } |
| 2937 | return npos; |
| 2938 | } |
| 2939 | |
| 2940 | template<class _CharT, class _Traits, class _Allocator> |
| 2941 | _LIBCPP_INLINE_VISIBILITY inline |
| 2942 | typename basic_string<_CharT, _Traits, _Allocator>::size_type |
| 2943 | basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const basic_string& __str, size_type __pos) const |
| 2944 | { |
| 2945 | return find_first_not_of(__str.data(), __pos, __str.size()); |
| 2946 | } |
| 2947 | |
| 2948 | template<class _CharT, class _Traits, class _Allocator> |
| 2949 | _LIBCPP_INLINE_VISIBILITY inline |
| 2950 | typename basic_string<_CharT, _Traits, _Allocator>::size_type |
| 2951 | basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const_pointer __s, size_type __pos) const |
| 2952 | { |
| 2953 | #ifdef _LIBCPP_DEBUG |
| 2954 | assert(__s != 0); |
| 2955 | #endif |
| 2956 | return find_first_not_of(__s, __pos, traits_type::length(__s)); |
| 2957 | } |
| 2958 | |
| 2959 | template<class _CharT, class _Traits, class _Allocator> |
| 2960 | _LIBCPP_INLINE_VISIBILITY inline |
| 2961 | typename basic_string<_CharT, _Traits, _Allocator>::size_type |
| 2962 | basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(value_type __c, size_type __pos) const |
| 2963 | { |
| 2964 | size_type __sz = size(); |
| 2965 | if (__pos < __sz) |
| 2966 | { |
| 2967 | const_pointer __p = data(); |
| 2968 | const_pointer __pe = __p + __sz; |
| 2969 | for (const_pointer __ps = __p + __pos; __p != __pe; ++__ps) |
| 2970 | if (!traits_type::eq(*__ps, __c)) |
| 2971 | return static_cast<size_type>(__ps - __p); |
| 2972 | } |
| 2973 | return npos; |
| 2974 | } |
| 2975 | |
| 2976 | // find_last_not_of |
| 2977 | |
| 2978 | template<class _CharT, class _Traits, class _Allocator> |
| 2979 | typename basic_string<_CharT, _Traits, _Allocator>::size_type |
| 2980 | basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const_pointer __s, size_type __pos, size_type __n) const |
| 2981 | { |
| 2982 | #ifdef _LIBCPP_DEBUG |
| 2983 | assert(__s != 0); |
| 2984 | #endif |
| 2985 | size_type __sz = size(); |
| 2986 | if (__pos < __sz) |
| 2987 | ++__pos; |
| 2988 | else |
| 2989 | __pos = __sz; |
| 2990 | const_pointer __p = data(); |
| 2991 | for (const_pointer __ps = __p + __pos; __ps != __p;) |
| 2992 | if (traits_type::find(__s, __n, *--__ps) == 0) |
| 2993 | return static_cast<size_type>(__ps - __p); |
| 2994 | return npos; |
| 2995 | } |
| 2996 | |
| 2997 | template<class _CharT, class _Traits, class _Allocator> |
| 2998 | _LIBCPP_INLINE_VISIBILITY inline |
| 2999 | typename basic_string<_CharT, _Traits, _Allocator>::size_type |
| 3000 | basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const basic_string& __str, size_type __pos) const |
| 3001 | { |
| 3002 | return find_last_not_of(__str.data(), __pos, __str.size()); |
| 3003 | } |
| 3004 | |
| 3005 | template<class _CharT, class _Traits, class _Allocator> |
| 3006 | _LIBCPP_INLINE_VISIBILITY inline |
| 3007 | typename basic_string<_CharT, _Traits, _Allocator>::size_type |
| 3008 | basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const_pointer __s, size_type __pos) const |
| 3009 | { |
| 3010 | #ifdef _LIBCPP_DEBUG |
| 3011 | assert(__s != 0); |
| 3012 | #endif |
| 3013 | return find_last_not_of(__s, __pos, traits_type::length(__s)); |
| 3014 | } |
| 3015 | |
| 3016 | template<class _CharT, class _Traits, class _Allocator> |
| 3017 | _LIBCPP_INLINE_VISIBILITY inline |
| 3018 | typename basic_string<_CharT, _Traits, _Allocator>::size_type |
| 3019 | basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(value_type __c, size_type __pos) const |
| 3020 | { |
| 3021 | size_type __sz = size(); |
| 3022 | if (__pos < __sz) |
| 3023 | ++__pos; |
| 3024 | else |
| 3025 | __pos = __sz; |
| 3026 | const_pointer __p = data(); |
| 3027 | for (const_pointer __ps = __p + __pos; __ps != __p;) |
| 3028 | if (!traits_type::eq(*--__ps, __c)) |
| 3029 | return static_cast<size_type>(__ps - __p); |
| 3030 | return npos; |
| 3031 | } |
| 3032 | |
| 3033 | // compare |
| 3034 | |
| 3035 | template <class _CharT, class _Traits, class _Allocator> |
| 3036 | _LIBCPP_INLINE_VISIBILITY inline |
| 3037 | int |
| 3038 | basic_string<_CharT, _Traits, _Allocator>::compare(const basic_string& __str) const |
| 3039 | { |
| 3040 | return compare(0, npos, __str.data(), __str.size()); |
| 3041 | } |
| 3042 | |
| 3043 | template <class _CharT, class _Traits, class _Allocator> |
| 3044 | _LIBCPP_INLINE_VISIBILITY inline |
| 3045 | int |
| 3046 | basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1, size_type __n1, const basic_string& __str) const |
| 3047 | { |
| 3048 | return compare(__pos1, __n1, __str.data(), __str.size()); |
| 3049 | } |
| 3050 | |
| 3051 | template <class _CharT, class _Traits, class _Allocator> |
| 3052 | int |
| 3053 | basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1, size_type __n1, const basic_string& __str, |
| 3054 | size_type __pos2, size_type __n2) const |
| 3055 | { |
| 3056 | size_type __sz = __str.size(); |
| 3057 | if (__pos2 > __sz) |
| 3058 | this->__throw_out_of_range(); |
| 3059 | return compare(__pos1, __n1, __str.data() + __pos2, _STD::min(__n2, __sz - __pos2)); |
| 3060 | } |
| 3061 | |
| 3062 | template <class _CharT, class _Traits, class _Allocator> |
| 3063 | int |
| 3064 | basic_string<_CharT, _Traits, _Allocator>::compare(const_pointer __s) const |
| 3065 | { |
| 3066 | #ifdef _LIBCPP_DEBUG |
| 3067 | assert(__s != 0); |
| 3068 | #endif |
| 3069 | return compare(0, npos, __s, traits_type::length(__s)); |
| 3070 | } |
| 3071 | |
| 3072 | template <class _CharT, class _Traits, class _Allocator> |
| 3073 | int |
| 3074 | basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1, size_type __n1, const_pointer __s) const |
| 3075 | { |
| 3076 | #ifdef _LIBCPP_DEBUG |
| 3077 | assert(__s != 0); |
| 3078 | #endif |
| 3079 | return compare(__pos1, __n1, __s, traits_type::length(__s)); |
| 3080 | } |
| 3081 | |
| 3082 | template <class _CharT, class _Traits, class _Allocator> |
| 3083 | int |
| 3084 | basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1, size_type __n1, |
| 3085 | const_pointer __s, size_type __n2) const |
| 3086 | { |
| 3087 | #ifdef _LIBCPP_DEBUG |
| 3088 | assert(__s != 0); |
| 3089 | #endif |
| 3090 | size_type __sz = size(); |
| 3091 | if (__pos1 > __sz || __n2 == npos) |
| 3092 | this->__throw_out_of_range(); |
| 3093 | size_type __rlen = _STD::min(__n1, __sz - __pos1); |
| 3094 | int __r = traits_type::compare(data() + __pos1, __s, _STD::min(__rlen, __n2)); |
| 3095 | if (__r == 0) |
| 3096 | { |
| 3097 | if (__rlen < __n2) |
| 3098 | __r = -1; |
| 3099 | else if (__rlen > __n2) |
| 3100 | __r = 1; |
| 3101 | } |
| 3102 | return __r; |
| 3103 | } |
| 3104 | |
| 3105 | // __invariants |
| 3106 | |
| 3107 | template<class _CharT, class _Traits, class _Allocator> |
| 3108 | bool |
| 3109 | basic_string<_CharT, _Traits, _Allocator>::__invariants() const |
| 3110 | { |
| 3111 | if (size() > capacity()) |
| 3112 | return false; |
| 3113 | if (capacity() < __min_cap - 1) |
| 3114 | return false; |
| 3115 | if (data() == 0) |
| 3116 | return false; |
| 3117 | if (data()[size()] != value_type(0)) |
| 3118 | return false; |
| 3119 | return true; |
| 3120 | } |
| 3121 | |
| 3122 | // operator== |
| 3123 | |
| 3124 | template<class _CharT, class _Traits, class _Allocator> |
| 3125 | _LIBCPP_INLINE_VISIBILITY inline |
| 3126 | bool |
| 3127 | operator==(const basic_string<_CharT, _Traits, _Allocator>& __lhs, |
| 3128 | const basic_string<_CharT, _Traits, _Allocator>& __rhs) |
| 3129 | { |
| 3130 | return __lhs.size() == __rhs.size() && _Traits::compare(__lhs.data(), __rhs.data(), __lhs.size()) == 0; |
| 3131 | } |
| 3132 | |
| 3133 | template<class _CharT, class _Traits, class _Allocator> |
| 3134 | _LIBCPP_INLINE_VISIBILITY inline |
| 3135 | bool |
| 3136 | operator==(const _CharT* __lhs, const basic_string<_CharT, _Traits, _Allocator>& __rhs) |
| 3137 | { |
| 3138 | return __rhs.compare(__lhs) == 0; |
| 3139 | } |
| 3140 | |
| 3141 | template<class _Allocator> |
| 3142 | _LIBCPP_INLINE_VISIBILITY inline |
| 3143 | bool |
| 3144 | operator==(const char* __lhs, const basic_string<char, char_traits<char>, _Allocator>& __rhs) |
| 3145 | { |
| 3146 | return strcmp(__lhs, __rhs.data()) == 0; |
| 3147 | } |
| 3148 | |
| 3149 | template<class _Allocator> |
| 3150 | _LIBCPP_INLINE_VISIBILITY inline |
| 3151 | bool |
| 3152 | operator==(const wchar_t* __lhs, const basic_string<wchar_t, char_traits<wchar_t>, _Allocator>& __rhs) |
| 3153 | { |
| 3154 | return wcscmp(__lhs, __rhs.data()) == 0; |
| 3155 | } |
| 3156 | |
| 3157 | template<class _CharT, class _Traits, class _Allocator> |
| 3158 | _LIBCPP_INLINE_VISIBILITY inline |
| 3159 | bool |
| 3160 | operator==(const basic_string<_CharT,_Traits,_Allocator>& __lhs, const _CharT* __rhs) |
| 3161 | { |
| 3162 | return __lhs.compare(__rhs) == 0; |
| 3163 | } |
| 3164 | |
| 3165 | template<class _Allocator> |
| 3166 | _LIBCPP_INLINE_VISIBILITY inline |
| 3167 | bool |
| 3168 | operator==(const basic_string<char, char_traits<char>, _Allocator>& __lhs, const char* __rhs) |
| 3169 | { |
| 3170 | return strcmp(__lhs.data(), __rhs) == 0; |
| 3171 | } |
| 3172 | |
| 3173 | template<class _Allocator> |
| 3174 | _LIBCPP_INLINE_VISIBILITY inline |
| 3175 | bool |
| 3176 | operator==(const basic_string<wchar_t, char_traits<wchar_t>, _Allocator>& __lhs, const wchar_t* __rhs) |
| 3177 | { |
| 3178 | return wcscmp(__lhs.data(), __rhs) == 0; |
| 3179 | } |
| 3180 | |
| 3181 | // operator!= |
| 3182 | |
| 3183 | template<class _CharT, class _Traits, class _Allocator> |
| 3184 | _LIBCPP_INLINE_VISIBILITY inline |
| 3185 | bool |
| 3186 | operator!=(const basic_string<_CharT,_Traits,_Allocator>& __lhs, |
| 3187 | const basic_string<_CharT, _Traits, _Allocator>& __rhs) |
| 3188 | { |
| 3189 | return !(__lhs == __rhs); |
| 3190 | } |
| 3191 | |
| 3192 | template<class _CharT, class _Traits, class _Allocator> |
| 3193 | _LIBCPP_INLINE_VISIBILITY inline |
| 3194 | bool |
| 3195 | operator!=(const _CharT* __lhs, const basic_string<_CharT, _Traits, _Allocator>& __rhs) |
| 3196 | { |
| 3197 | return !(__lhs == __rhs); |
| 3198 | } |
| 3199 | |
| 3200 | template<class _CharT, class _Traits, class _Allocator> |
| 3201 | _LIBCPP_INLINE_VISIBILITY inline |
| 3202 | bool |
| 3203 | operator!=(const basic_string<_CharT, _Traits, _Allocator>& __lhs, const _CharT* __rhs) |
| 3204 | { |
| 3205 | return !(__lhs == __rhs); |
| 3206 | } |
| 3207 | |
| 3208 | // operator< |
| 3209 | |
| 3210 | template<class _CharT, class _Traits, class _Allocator> |
| 3211 | _LIBCPP_INLINE_VISIBILITY inline |
| 3212 | bool |
| 3213 | operator< (const basic_string<_CharT, _Traits, _Allocator>& __lhs, |
| 3214 | const basic_string<_CharT, _Traits, _Allocator>& __rhs) |
| 3215 | { |
| 3216 | return __lhs.cmpare(__rhs) < 0; |
| 3217 | } |
| 3218 | |
| 3219 | template<class _Allocator> |
| 3220 | _LIBCPP_INLINE_VISIBILITY inline |
| 3221 | bool |
| 3222 | operator< (const basic_string<char, char_traits<char>, _Allocator>& __lhs, |
| 3223 | const basic_string<char, char_traits<char>, _Allocator>& __rhs) |
| 3224 | { |
| 3225 | return strcmp(__lhs.data(), __rhs.data()) < 0; |
| 3226 | } |
| 3227 | |
| 3228 | template<class _Allocator> |
| 3229 | _LIBCPP_INLINE_VISIBILITY inline |
| 3230 | bool |
| 3231 | operator< (const basic_string<wchar_t, char_traits<wchar_t>, _Allocator>& __lhs, |
| 3232 | const basic_string<wchar_t, char_traits<wchar_t>, _Allocator>& __rhs) |
| 3233 | { |
| 3234 | return wcscmp(__lhs.data(), __rhs.data()) < 0; |
| 3235 | } |
| 3236 | |
| 3237 | template<class _CharT, class _Traits, class _Allocator> |
| 3238 | _LIBCPP_INLINE_VISIBILITY inline |
| 3239 | bool |
| 3240 | operator< (const basic_string<_CharT, _Traits, _Allocator>& __lhs, const _CharT* __rhs) |
| 3241 | { |
| 3242 | return __lhs.compare(__rhs); |
| 3243 | } |
| 3244 | |
| 3245 | template<class _Allocator> |
| 3246 | _LIBCPP_INLINE_VISIBILITY inline |
| 3247 | bool |
| 3248 | operator< (const basic_string<char, char_traits<char>, _Allocator>& __lhs, const char* __rhs) |
| 3249 | { |
| 3250 | return strcmp(__lhs.data(), __rhs) < 0; |
| 3251 | } |
| 3252 | |
| 3253 | template<class _Allocator> |
| 3254 | _LIBCPP_INLINE_VISIBILITY inline |
| 3255 | bool |
| 3256 | operator< (const basic_string<wchar_t, char_traits<wchar_t>, _Allocator>& __lhs, const wchar_t* __rhs) |
| 3257 | { |
| 3258 | return wcscmp(__lhs.data(), __rhs) < 0; |
| 3259 | } |
| 3260 | |
| 3261 | template<class _CharT, class _Traits, class _Allocator> |
| 3262 | _LIBCPP_INLINE_VISIBILITY inline |
| 3263 | bool |
| 3264 | operator< (const _CharT* __lhs, const basic_string<_CharT, _Traits, _Allocator>& __rhs) |
| 3265 | { |
| 3266 | return __rhs.compare(__lhs) > 0; |
| 3267 | } |
| 3268 | |
| 3269 | template<class _Allocator> |
| 3270 | _LIBCPP_INLINE_VISIBILITY inline |
| 3271 | bool |
| 3272 | operator< (const char* __lhs, const basic_string<char, char_traits<char>, _Allocator>& __rhs) |
| 3273 | { |
| 3274 | return strcmp(__lhs, __rhs.data()) < 0; |
| 3275 | } |
| 3276 | |
| 3277 | template<class _Allocator> |
| 3278 | _LIBCPP_INLINE_VISIBILITY inline |
| 3279 | bool |
| 3280 | operator< (const wchar_t* __lhs, const basic_string<wchar_t, char_traits<wchar_t>, _Allocator>& __rhs) |
| 3281 | { |
| 3282 | return wcscmp(__lhs, __rhs.data()) < 0; |
| 3283 | } |
| 3284 | |
| 3285 | // operator> |
| 3286 | |
| 3287 | template<class _CharT, class _Traits, class _Allocator> |
| 3288 | _LIBCPP_INLINE_VISIBILITY inline |
| 3289 | bool |
| 3290 | operator> (const basic_string<_CharT, _Traits, _Allocator>& __lhs, |
| 3291 | const basic_string<_CharT, _Traits, _Allocator>& __rhs) |
| 3292 | { |
| 3293 | return __rhs < __lhs; |
| 3294 | } |
| 3295 | |
| 3296 | template<class _CharT, class _Traits, class _Allocator> |
| 3297 | _LIBCPP_INLINE_VISIBILITY inline |
| 3298 | bool |
| 3299 | operator> (const basic_string<_CharT, _Traits, _Allocator>& __lhs, const _CharT* __rhs) |
| 3300 | { |
| 3301 | return __rhs < __lhs; |
| 3302 | } |
| 3303 | |
| 3304 | template<class _CharT, class _Traits, class _Allocator> |
| 3305 | _LIBCPP_INLINE_VISIBILITY inline |
| 3306 | bool |
| 3307 | operator> (const _CharT* __lhs, const basic_string<_CharT, _Traits, _Allocator>& __rhs) |
| 3308 | { |
| 3309 | return __rhs < __lhs; |
| 3310 | } |
| 3311 | |
| 3312 | // operator<= |
| 3313 | |
| 3314 | template<class _CharT, class _Traits, class _Allocator> |
| 3315 | _LIBCPP_INLINE_VISIBILITY inline |
| 3316 | bool |
| 3317 | operator<=(const basic_string<_CharT, _Traits, _Allocator>& __lhs, |
| 3318 | const basic_string<_CharT, _Traits, _Allocator>& __rhs) |
| 3319 | { |
| 3320 | return !(__rhs < __lhs); |
| 3321 | } |
| 3322 | |
| 3323 | template<class _CharT, class _Traits, class _Allocator> |
| 3324 | _LIBCPP_INLINE_VISIBILITY inline |
| 3325 | bool |
| 3326 | operator<=(const basic_string<_CharT, _Traits, _Allocator>& __lhs, const _CharT* __rhs) |
| 3327 | { |
| 3328 | return !(__rhs < __lhs); |
| 3329 | } |
| 3330 | |
| 3331 | template<class _CharT, class _Traits, class _Allocator> |
| 3332 | _LIBCPP_INLINE_VISIBILITY inline |
| 3333 | bool |
| 3334 | operator<=(const _CharT* __lhs, const basic_string<_CharT, _Traits, _Allocator>& __rhs) |
| 3335 | { |
| 3336 | return !(__rhs < __lhs); |
| 3337 | } |
| 3338 | |
| 3339 | // operator>= |
| 3340 | |
| 3341 | template<class _CharT, class _Traits, class _Allocator> |
| 3342 | _LIBCPP_INLINE_VISIBILITY inline |
| 3343 | bool |
| 3344 | operator>=(const basic_string<_CharT, _Traits, _Allocator>& __lhs, |
| 3345 | const basic_string<_CharT, _Traits, _Allocator>& __rhs) |
| 3346 | { |
| 3347 | return !(__lhs < __rhs); |
| 3348 | } |
| 3349 | |
| 3350 | template<class _CharT, class _Traits, class _Allocator> |
| 3351 | _LIBCPP_INLINE_VISIBILITY inline |
| 3352 | bool |
| 3353 | operator>=(const basic_string<_CharT, _Traits, _Allocator>& __lhs, const _CharT* __rhs) |
| 3354 | { |
| 3355 | return !(__lhs < __rhs); |
| 3356 | } |
| 3357 | |
| 3358 | template<class _CharT, class _Traits, class _Allocator> |
| 3359 | _LIBCPP_INLINE_VISIBILITY inline |
| 3360 | bool |
| 3361 | operator>=(const _CharT* __lhs, const basic_string<_CharT, _Traits, _Allocator>& __rhs) |
| 3362 | { |
| 3363 | return !(__lhs < __rhs); |
| 3364 | } |
| 3365 | |
| 3366 | // operator + |
| 3367 | |
| 3368 | template<class _CharT, class _Traits, class _Allocator> |
| 3369 | basic_string<_CharT, _Traits, _Allocator> |
| 3370 | operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, |
| 3371 | const basic_string<_CharT, _Traits, _Allocator>& __rhs) |
| 3372 | { |
| 3373 | basic_string<_CharT, _Traits, _Allocator> __r(__lhs.get_allocator()); |
| 3374 | typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = __lhs.size(); |
| 3375 | typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = __rhs.size(); |
| 3376 | __r.__init(__lhs.data(), __lhs_sz, __lhs_sz + __rhs_sz); |
| 3377 | __r.append(__rhs.data(), __rhs_sz); |
| 3378 | return __r; |
| 3379 | } |
| 3380 | |
| 3381 | template<class _CharT, class _Traits, class _Allocator> |
| 3382 | basic_string<_CharT, _Traits, _Allocator> |
| 3383 | operator+(const _CharT* __lhs , const basic_string<_CharT,_Traits,_Allocator>& __rhs) |
| 3384 | { |
| 3385 | basic_string<_CharT, _Traits, _Allocator> __r(__rhs.get_allocator()); |
| 3386 | typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = _Traits::length(__lhs); |
| 3387 | typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = __rhs.size(); |
| 3388 | __r.__init(__lhs, __lhs_sz, __lhs_sz + __rhs_sz); |
| 3389 | __r.append(__rhs.data(), __rhs_sz); |
| 3390 | return __r; |
| 3391 | } |
| 3392 | |
| 3393 | template<class _CharT, class _Traits, class _Allocator> |
| 3394 | basic_string<_CharT, _Traits, _Allocator> |
| 3395 | operator+(_CharT __lhs, const basic_string<_CharT,_Traits,_Allocator>& __rhs) |
| 3396 | { |
| 3397 | basic_string<_CharT, _Traits, _Allocator> __r(__rhs.get_allocator()); |
| 3398 | typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = __rhs.size(); |
| 3399 | __r.__init(&__lhs, 1, 1 + __rhs_sz); |
| 3400 | __r.append(__rhs.data(), __rhs_sz); |
| 3401 | return __r; |
| 3402 | } |
| 3403 | |
| 3404 | template<class _CharT, class _Traits, class _Allocator> |
| 3405 | basic_string<_CharT, _Traits, _Allocator> |
| 3406 | operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, const _CharT* __rhs) |
| 3407 | { |
| 3408 | basic_string<_CharT, _Traits, _Allocator> __r(__lhs.get_allocator()); |
| 3409 | typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = __lhs.size(); |
| 3410 | typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = _Traits::length(__rhs); |
| 3411 | __r.__init(__lhs.data(), __lhs_sz, __lhs_sz + __rhs_sz); |
| 3412 | __r.append(__rhs, __rhs_sz); |
| 3413 | return __r; |
| 3414 | } |
| 3415 | |
| 3416 | template<class _CharT, class _Traits, class _Allocator> |
| 3417 | basic_string<_CharT, _Traits, _Allocator> |
| 3418 | operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, _CharT __rhs) |
| 3419 | { |
| 3420 | basic_string<_CharT, _Traits, _Allocator> __r(__lhs.get_allocator()); |
| 3421 | typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = __lhs.size(); |
| 3422 | __r.__init(__lhs.data(), __lhs_sz, __lhs_sz + 1); |
| 3423 | __r.push_back(__rhs); |
| 3424 | return __r; |
| 3425 | } |
| 3426 | |
| 3427 | #ifdef _LIBCPP_MOVE |
| 3428 | |
| 3429 | template<class _CharT, class _Traits, class _Allocator> |
| 3430 | _LIBCPP_INLINE_VISIBILITY inline |
| 3431 | basic_string<_CharT, _Traits, _Allocator> |
| 3432 | operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, const basic_string<_CharT, _Traits, _Allocator>& __rhs) |
| 3433 | { |
| 3434 | return _STD::move(__lhs.append(__rhs)); |
| 3435 | } |
| 3436 | |
| 3437 | template<class _CharT, class _Traits, class _Allocator> |
| 3438 | _LIBCPP_INLINE_VISIBILITY inline |
| 3439 | basic_string<_CharT, _Traits, _Allocator> |
| 3440 | operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, basic_string<_CharT, _Traits, _Allocator>&& __rhs) |
| 3441 | { |
| 3442 | return _STD::move(__rhs.insert(0, __lhs)); |
| 3443 | } |
| 3444 | |
| 3445 | template<class _CharT, class _Traits, class _Allocator> |
| 3446 | _LIBCPP_INLINE_VISIBILITY inline |
| 3447 | basic_string<_CharT, _Traits, _Allocator> |
| 3448 | operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, basic_string<_CharT, _Traits, _Allocator>&& __rhs) |
| 3449 | { |
| 3450 | return _STD::move(__lhs.append(__rhs)); |
| 3451 | } |
| 3452 | |
| 3453 | template<class _CharT, class _Traits, class _Allocator> |
| 3454 | _LIBCPP_INLINE_VISIBILITY inline |
| 3455 | basic_string<_CharT, _Traits, _Allocator> |
| 3456 | operator+(const _CharT* __lhs , basic_string<_CharT,_Traits,_Allocator>&& __rhs) |
| 3457 | { |
| 3458 | return _STD::move(__rhs.insert(0, __lhs)); |
| 3459 | } |
| 3460 | |
| 3461 | template<class _CharT, class _Traits, class _Allocator> |
| 3462 | _LIBCPP_INLINE_VISIBILITY inline |
| 3463 | basic_string<_CharT, _Traits, _Allocator> |
| 3464 | operator+(_CharT __lhs, basic_string<_CharT,_Traits,_Allocator>&& __rhs) |
| 3465 | { |
| 3466 | __rhs.insert(__rhs.begin(), __lhs); |
| 3467 | return _STD::move(__rhs); |
| 3468 | } |
| 3469 | |
| 3470 | template<class _CharT, class _Traits, class _Allocator> |
| 3471 | _LIBCPP_INLINE_VISIBILITY inline |
| 3472 | basic_string<_CharT, _Traits, _Allocator> |
| 3473 | operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, const _CharT* __rhs) |
| 3474 | { |
| 3475 | return _STD::move(__lhs.append(__rhs)); |
| 3476 | } |
| 3477 | |
| 3478 | template<class _CharT, class _Traits, class _Allocator> |
| 3479 | _LIBCPP_INLINE_VISIBILITY inline |
| 3480 | basic_string<_CharT, _Traits, _Allocator> |
| 3481 | operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, _CharT __rhs) |
| 3482 | { |
| 3483 | __lhs.push_back(__rhs); |
| 3484 | return _STD::move(__lhs); |
| 3485 | } |
| 3486 | |
| 3487 | #endif // _LIBCPP_MOVE |
| 3488 | |
| 3489 | // swap |
| 3490 | |
| 3491 | template<class _CharT, class _Traits, class _Allocator> |
| 3492 | _LIBCPP_INLINE_VISIBILITY inline |
| 3493 | void |
| 3494 | swap(basic_string<_CharT, _Traits, _Allocator>& __lhs, basic_string<_CharT, _Traits, _Allocator>& __rhs) |
| 3495 | { |
| 3496 | __lhs.swap(__rhs); |
| 3497 | } |
| 3498 | |
| 3499 | template<class _CharT, class _Traits, class _Allocator> |
| 3500 | struct __is_zero_default_constructible<basic_string<_CharT, _Traits, _Allocator> > |
| 3501 | : public integral_constant<bool, __is_zero_default_constructible<_Allocator>::value> {}; |
| 3502 | |
| 3503 | #ifndef _LIBCPP_HAS_NO_UNICODE_CHARS |
| 3504 | |
| 3505 | typedef basic_string<char16_t> u16string; |
| 3506 | typedef basic_string<char32_t> u32string; |
| 3507 | |
| 3508 | #endif |
| 3509 | |
Howard Hinnant | a6a062d | 2010-06-02 18:20:39 +0000 | [diff] [blame] | 3510 | int stoi (const string& __str, size_t* __idx = 0, int __base = 10); |
| 3511 | long stol (const string& __str, size_t* __idx = 0, int __base = 10); |
| 3512 | unsigned long stoul (const string& __str, size_t* __idx = 0, int __base = 10); |
| 3513 | long long stoll (const string& __str, size_t* __idx = 0, int __base = 10); |
| 3514 | unsigned long long stoull(const string& __str, size_t* __idx = 0, int __base = 10); |
| 3515 | |
| 3516 | float stof (const string& __str, size_t* __idx = 0); |
| 3517 | double stod (const string& __str, size_t* __idx = 0); |
| 3518 | long double stold(const string& __str, size_t* __idx = 0); |
| 3519 | |
| 3520 | string to_string(int __val); |
| 3521 | string to_string(unsigned __val); |
| 3522 | string to_string(long __val); |
| 3523 | string to_string(unsigned long __val); |
| 3524 | string to_string(long long __val); |
| 3525 | string to_string(unsigned long long __val); |
| 3526 | string to_string(float __val); |
| 3527 | string to_string(double __val); |
| 3528 | string to_string(long double __val); |
| 3529 | |
| 3530 | int stoi (const wstring& __str, size_t* __idx = 0, int __base = 10); |
| 3531 | long stol (const wstring& __str, size_t* __idx = 0, int __base = 10); |
| 3532 | unsigned long stoul (const wstring& __str, size_t* __idx = 0, int __base = 10); |
| 3533 | long long stoll (const wstring& __str, size_t* __idx = 0, int __base = 10); |
| 3534 | unsigned long long stoull(const wstring& __str, size_t* __idx = 0, int __base = 10); |
| 3535 | |
| 3536 | float stof (const wstring& __str, size_t* __idx = 0); |
| 3537 | double stod (const wstring& __str, size_t* __idx = 0); |
| 3538 | long double stold(const wstring& __str, size_t* __idx = 0); |
| 3539 | |
| 3540 | wstring to_wstring(int __val); |
| 3541 | wstring to_wstring(unsigned __val); |
| 3542 | wstring to_wstring(long __val); |
| 3543 | wstring to_wstring(unsigned long __val); |
| 3544 | wstring to_wstring(long long __val); |
| 3545 | wstring to_wstring(unsigned long long __val); |
| 3546 | wstring to_wstring(float __val); |
| 3547 | wstring to_wstring(double __val); |
| 3548 | wstring to_wstring(long double __val); |
| 3549 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3550 | template<class _CharT, class _Traits, class _Allocator> |
| 3551 | const typename basic_string<_CharT, _Traits, _Allocator>::size_type |
| 3552 | basic_string<_CharT, _Traits, _Allocator>::npos; |
| 3553 | |
| 3554 | template<class _CharT, class _Traits, class _Allocator> |
| 3555 | struct hash<basic_string<_CharT, _Traits, _Allocator> > |
| 3556 | : public unary_function<basic_string<_CharT, _Traits, _Allocator>, size_t> |
| 3557 | { |
| 3558 | size_t |
| 3559 | operator()(const basic_string<_CharT, _Traits, _Allocator>& __val) const; |
| 3560 | }; |
| 3561 | |
| 3562 | template<class _CharT, class _Traits, class _Allocator> |
| 3563 | size_t |
| 3564 | hash<basic_string<_CharT, _Traits, _Allocator> >::operator()( |
| 3565 | const basic_string<_CharT, _Traits, _Allocator>& __val) const |
| 3566 | { |
| 3567 | typedef basic_string<_CharT, _Traits, _Allocator> S; |
| 3568 | typedef typename S::const_pointer const_pointer; |
| 3569 | size_t __r = 0; |
| 3570 | const size_t __sr = __CHAR_BIT__ * sizeof(size_t) - 8; |
| 3571 | const size_t __m = size_t(0xF) << (__sr + 4); |
| 3572 | const_pointer __p = __val.data(); |
| 3573 | const_pointer __e = __p + __val.size(); |
| 3574 | for (; __p != __e; ++__p) |
| 3575 | { |
| 3576 | __r = (__r << 4) + *__p; |
| 3577 | size_t __g = __r & __m; |
| 3578 | __r ^= __g | (__g >> __sr); |
| 3579 | } |
| 3580 | return __r; |
| 3581 | } |
| 3582 | |
| 3583 | extern template class basic_string<char>; |
| 3584 | extern template class basic_string<wchar_t>; |
| 3585 | |
| 3586 | extern template |
| 3587 | enable_if<__is_forward_iterator<char const*>::value, void>::type |
| 3588 | basic_string<char, char_traits<char>, allocator<char> >:: |
| 3589 | __init<char const*>(char const*, char const*); |
| 3590 | |
| 3591 | extern template |
| 3592 | enable_if<__is_forward_iterator<wchar_t const*>::value, void>::type |
| 3593 | basic_string<wchar_t, char_traits<wchar_t>, allocator<wchar_t> >:: |
| 3594 | __init<wchar_t const*>(wchar_t const*, wchar_t const*); |
| 3595 | |
| 3596 | extern template |
| 3597 | enable_if<__is_forward_iterator<char*>::value, |
| 3598 | basic_string<char, char_traits<char>, allocator<char> >&>::type |
| 3599 | basic_string<char, char_traits<char>, allocator<char> >:: |
| 3600 | append<char*>(char*, char*); |
| 3601 | |
| 3602 | extern template |
| 3603 | enable_if<__is_forward_iterator<wchar_t*>::value, |
| 3604 | basic_string<wchar_t, char_traits<wchar_t>, allocator<wchar_t> >&>::type |
| 3605 | basic_string<wchar_t, char_traits<wchar_t>, allocator<wchar_t> >:: |
| 3606 | append<wchar_t*>(wchar_t*, wchar_t*); |
| 3607 | |
| 3608 | extern template |
| 3609 | enable_if<__is_forward_iterator<char const*>::value, |
| 3610 | string::iterator>::type |
| 3611 | string:: |
| 3612 | insert<char const*>(string::const_iterator, char const*, char const*); |
| 3613 | |
| 3614 | extern template |
| 3615 | enable_if<__is_forward_iterator<wchar_t const*>::value, |
| 3616 | wstring::iterator>::type |
| 3617 | wstring:: |
| 3618 | insert<wchar_t const*>(wstring::const_iterator, wchar_t const*, wchar_t const*); |
| 3619 | |
| 3620 | extern template |
| 3621 | enable_if<__is_input_iterator<char const*>::value, string&>::type |
| 3622 | string:: |
| 3623 | replace<char const*>(string::iterator, string::iterator, char const*, char const*); |
| 3624 | |
| 3625 | extern template |
| 3626 | enable_if<__is_input_iterator<wchar_t const*>::value, wstring&>::type |
| 3627 | wstring:: |
| 3628 | replace<wchar_t const*>(wstring::iterator, wstring::iterator, wchar_t const*, wchar_t const*); |
| 3629 | |
| 3630 | extern template |
| 3631 | enable_if<__is_forward_iterator<wchar_t*>::value, wstring&>::type |
| 3632 | wstring::assign<wchar_t*>(wchar_t*, wchar_t*); |
| 3633 | |
| 3634 | extern template |
| 3635 | string |
| 3636 | operator+<char, char_traits<char>, allocator<char> >(char const*, string const&); |
| 3637 | |
| 3638 | _LIBCPP_END_NAMESPACE_STD |
| 3639 | |
| 3640 | #endif // _LIBCPP_STRING |