blob: ba311efa5a318785fe32ff394d47abcc32b03b6a [file] [log] [blame]
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001// -*- C++ -*-
2//===--------------------------- string -----------------------------------===//
3//
Howard Hinnantf5256e12010-05-11 21:36:01 +00004// The LLVM Compiler Infrastructure
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005//
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
17namespace std
18{
19
20template <class stateT>
21class fpos
22{
23private:
24 stateT st;
25public:
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
39template <class stateT> streamoff operator-(const fpos<stateT>& x, const fpos<stateT>& y);
40
41template <class stateT> bool operator==(const fpos<stateT>& x, const fpos<stateT>& y);
42template <class stateT> bool operator!=(const fpos<stateT>& x, const fpos<stateT>& y);
43
44template <class charT>
45struct 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
Howard Hinnanta6119a82011-05-29 19:57:12 +000053 static void assign(char_type& c1, const char_type& c2) noexcept;
Howard Hinnant03d71812012-07-20 19:09:12 +000054 static constexpr bool eq(char_type c1, char_type c2) noexcept;
55 static constexpr bool lt(char_type c1, char_type c2) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000056
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
Howard Hinnant03d71812012-07-20 19:09:12 +000064 static constexpr int_type not_eof(int_type c) noexcept;
65 static constexpr char_type to_char_type(int_type c) noexcept;
66 static constexpr int_type to_int_type(char_type c) noexcept;
67 static constexpr bool eq_int_type(int_type c1, int_type c2) noexcept;
68 static constexpr int_type eof() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000069};
70
71template <> struct char_traits<char>;
72template <> struct char_traits<wchar_t>;
73
Howard Hinnant324bb032010-08-22 00:02:43 +000074template<class charT, class traits = char_traits<charT>, class Allocator = allocator<charT> >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000075class basic_string
76{
Howard Hinnant324bb032010-08-22 00:02:43 +000077public:
78// types:
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000079 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
Howard Hinnant53f7d4c2011-06-03 18:40:47 +000095 basic_string()
96 noexcept(is_nothrow_default_constructible<allocator_type>::value);
97 explicit basic_string(const allocator_type& a);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000098 basic_string(const basic_string& str);
Howard Hinnant53f7d4c2011-06-03 18:40:47 +000099 basic_string(basic_string&& str)
100 noexcept(is_nothrow_move_constructible<allocator_type>::value);
Marshall Clowf4f7d8f2016-04-07 18:13:41 +0000101 basic_string(const basic_string& str, size_type pos,
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000102 const allocator_type& a = allocator_type());
Marshall Clowf4f7d8f2016-04-07 18:13:41 +0000103 basic_string(const basic_string& str, size_type pos, size_type n,
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000104 const Allocator& a = Allocator());
Marshall Clowdb7fa112016-11-14 18:22:19 +0000105 template<class T>
106 basic_string(const T& t, size_type pos, size_type n, const Allocator& a = Allocator()); // C++17
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000107 explicit basic_string(const basic_string_view<charT, traits> sv, const Allocator& a = Allocator());
Howard Hinnant9dcdcde2013-06-28 16:59:19 +0000108 basic_string(const value_type* s, const allocator_type& a = allocator_type());
109 basic_string(const value_type* s, size_type n, const allocator_type& a = allocator_type());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000110 basic_string(size_type n, value_type c, const allocator_type& a = allocator_type());
111 template<class InputIterator>
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000112 basic_string(InputIterator begin, InputIterator end,
113 const allocator_type& a = allocator_type());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000114 basic_string(initializer_list<value_type>, const Allocator& = Allocator());
115 basic_string(const basic_string&, const Allocator&);
116 basic_string(basic_string&&, const Allocator&);
117
118 ~basic_string();
119
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000120 operator basic_string_view<charT, traits>() const noexcept;
121
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000122 basic_string& operator=(const basic_string& str);
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000123 basic_string& operator=(basic_string_view<charT, traits> sv);
Howard Hinnant53f7d4c2011-06-03 18:40:47 +0000124 basic_string& operator=(basic_string&& str)
125 noexcept(
Marshall Clowaf961ed2015-08-18 18:57:00 +0000126 allocator_type::propagate_on_container_move_assignment::value ||
127 allocator_type::is_always_equal::value ); // C++17
Howard Hinnant9dcdcde2013-06-28 16:59:19 +0000128 basic_string& operator=(const value_type* s);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000129 basic_string& operator=(value_type c);
130 basic_string& operator=(initializer_list<value_type>);
131
Howard Hinnanta6119a82011-05-29 19:57:12 +0000132 iterator begin() noexcept;
133 const_iterator begin() const noexcept;
134 iterator end() noexcept;
135 const_iterator end() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000136
Howard Hinnanta6119a82011-05-29 19:57:12 +0000137 reverse_iterator rbegin() noexcept;
138 const_reverse_iterator rbegin() const noexcept;
139 reverse_iterator rend() noexcept;
140 const_reverse_iterator rend() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000141
Howard Hinnanta6119a82011-05-29 19:57:12 +0000142 const_iterator cbegin() const noexcept;
143 const_iterator cend() const noexcept;
144 const_reverse_iterator crbegin() const noexcept;
145 const_reverse_iterator crend() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000146
Howard Hinnanta6119a82011-05-29 19:57:12 +0000147 size_type size() const noexcept;
148 size_type length() const noexcept;
149 size_type max_size() const noexcept;
150 size_type capacity() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000151
152 void resize(size_type n, value_type c);
153 void resize(size_type n);
154
155 void reserve(size_type res_arg = 0);
156 void shrink_to_fit();
Howard Hinnanta6119a82011-05-29 19:57:12 +0000157 void clear() noexcept;
158 bool empty() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000159
160 const_reference operator[](size_type pos) const;
161 reference operator[](size_type pos);
162
163 const_reference at(size_type n) const;
164 reference at(size_type n);
165
166 basic_string& operator+=(const basic_string& str);
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000167 basic_string& operator+=(basic_string_view<charT, traits> sv);
Howard Hinnant9dcdcde2013-06-28 16:59:19 +0000168 basic_string& operator+=(const value_type* s);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000169 basic_string& operator+=(value_type c);
170 basic_string& operator+=(initializer_list<value_type>);
171
172 basic_string& append(const basic_string& str);
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000173 basic_string& append(basic_string_view<charT, traits> sv);
Marshall Clowa93b5e22014-03-04 19:17:19 +0000174 basic_string& append(const basic_string& str, size_type pos, size_type n=npos); //C++14
Marshall Clow6ac8de02016-09-24 22:45:42 +0000175 template <class T>
176 basic_string& append(const T& t, size_type pos, size_type n=npos); // C++17
Howard Hinnant9dcdcde2013-06-28 16:59:19 +0000177 basic_string& append(const value_type* s, size_type n);
178 basic_string& append(const value_type* s);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000179 basic_string& append(size_type n, value_type c);
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000180 template<class InputIterator>
181 basic_string& append(InputIterator first, InputIterator last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000182 basic_string& append(initializer_list<value_type>);
183
184 void push_back(value_type c);
185 void pop_back();
186 reference front();
187 const_reference front() const;
188 reference back();
189 const_reference back() const;
190
191 basic_string& assign(const basic_string& str);
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000192 basic_string& assign(basic_string_view<charT, traits> sv);
Howard Hinnanta6119a82011-05-29 19:57:12 +0000193 basic_string& assign(basic_string&& str);
Marshall Clowa93b5e22014-03-04 19:17:19 +0000194 basic_string& assign(const basic_string& str, size_type pos, size_type n=npos); // C++14
Marshall Clow6ac8de02016-09-24 22:45:42 +0000195 template <class T>
196 basic_string& assign(const T& t, size_type pos, size_type n=npos); // C++17
Howard Hinnant9dcdcde2013-06-28 16:59:19 +0000197 basic_string& assign(const value_type* s, size_type n);
198 basic_string& assign(const value_type* s);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000199 basic_string& assign(size_type n, value_type c);
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000200 template<class InputIterator>
201 basic_string& assign(InputIterator first, InputIterator last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000202 basic_string& assign(initializer_list<value_type>);
203
204 basic_string& insert(size_type pos1, const basic_string& str);
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000205 basic_string& insert(size_type pos1, basic_string_view<charT, traits> sv);
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000206 basic_string& insert(size_type pos1, const basic_string& str,
207 size_type pos2, size_type n);
Marshall Clow6ac8de02016-09-24 22:45:42 +0000208 template <class T>
209 basic_string& insert(size_type pos1, const T& t, size_type pos2, size_type n); // C++17
Marshall Clowa93b5e22014-03-04 19:17:19 +0000210 basic_string& insert(size_type pos, const value_type* s, size_type n=npos); //C++14
Howard Hinnant9dcdcde2013-06-28 16:59:19 +0000211 basic_string& insert(size_type pos, const value_type* s);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000212 basic_string& insert(size_type pos, size_type n, value_type c);
213 iterator insert(const_iterator p, value_type c);
214 iterator insert(const_iterator p, size_type n, value_type c);
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000215 template<class InputIterator>
216 iterator insert(const_iterator p, InputIterator first, InputIterator last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000217 iterator insert(const_iterator p, initializer_list<value_type>);
218
219 basic_string& erase(size_type pos = 0, size_type n = npos);
220 iterator erase(const_iterator position);
221 iterator erase(const_iterator first, const_iterator last);
222
223 basic_string& replace(size_type pos1, size_type n1, const basic_string& str);
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000224 basic_string& replace(size_type pos1, size_type n1, basic_string_view<charT, traits> sv);
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000225 basic_string& replace(size_type pos1, size_type n1, const basic_string& str,
Marshall Clowa93b5e22014-03-04 19:17:19 +0000226 size_type pos2, size_type n2=npos); // C++14
Marshall Clow6ac8de02016-09-24 22:45:42 +0000227 template <class T>
228 basic_string& replace(size_type pos1, size_type n1, const T& t,
229 size_type pos2, size_type n); // C++17
Howard Hinnant9dcdcde2013-06-28 16:59:19 +0000230 basic_string& replace(size_type pos, size_type n1, const value_type* s, size_type n2);
231 basic_string& replace(size_type pos, size_type n1, const value_type* s);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000232 basic_string& replace(size_type pos, size_type n1, size_type n2, value_type c);
Howard Hinnant7b2cb482010-11-17 21:11:40 +0000233 basic_string& replace(const_iterator i1, const_iterator i2, const basic_string& str);
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000234 basic_string& replace(const_iterator i1, const_iterator i2, basic_string_view<charT, traits> sv);
Howard Hinnant9dcdcde2013-06-28 16:59:19 +0000235 basic_string& replace(const_iterator i1, const_iterator i2, const value_type* s, size_type n);
236 basic_string& replace(const_iterator i1, const_iterator i2, const value_type* s);
Howard Hinnant7b2cb482010-11-17 21:11:40 +0000237 basic_string& replace(const_iterator i1, const_iterator i2, size_type n, value_type c);
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000238 template<class InputIterator>
Howard Hinnant7b2cb482010-11-17 21:11:40 +0000239 basic_string& replace(const_iterator i1, const_iterator i2, InputIterator j1, InputIterator j2);
240 basic_string& replace(const_iterator i1, const_iterator i2, initializer_list<value_type>);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000241
Howard Hinnant9dcdcde2013-06-28 16:59:19 +0000242 size_type copy(value_type* s, size_type n, size_type pos = 0) const;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000243 basic_string substr(size_type pos = 0, size_type n = npos) const;
244
Howard Hinnant53f7d4c2011-06-03 18:40:47 +0000245 void swap(basic_string& str)
Marshall Clow7d914d12015-07-13 20:04:56 +0000246 noexcept(allocator_traits<allocator_type>::propagate_on_container_swap::value ||
247 allocator_traits<allocator_type>::is_always_equal::value); // C++17
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000248
Howard Hinnant9dcdcde2013-06-28 16:59:19 +0000249 const value_type* c_str() const noexcept;
250 const value_type* data() const noexcept;
Marshall Clowf532a702016-03-08 15:44:30 +0000251 value_type* data() noexcept; // C++17
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000252
Howard Hinnanta6119a82011-05-29 19:57:12 +0000253 allocator_type get_allocator() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000254
Howard Hinnanta6119a82011-05-29 19:57:12 +0000255 size_type find(const basic_string& str, size_type pos = 0) const noexcept;
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000256 size_type find(basic_string_view<charT, traits> sv, size_type pos = 0) const noexcept;
Howard Hinnant9dcdcde2013-06-28 16:59:19 +0000257 size_type find(const value_type* s, size_type pos, size_type n) const noexcept;
258 size_type find(const value_type* s, size_type pos = 0) const noexcept;
Howard Hinnanta6119a82011-05-29 19:57:12 +0000259 size_type find(value_type c, size_type pos = 0) const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000260
Howard Hinnanta6119a82011-05-29 19:57:12 +0000261 size_type rfind(const basic_string& str, size_type pos = npos) const noexcept;
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000262 size_type ffind(basic_string_view<charT, traits> sv, size_type pos = 0) const noexcept;
Howard Hinnant9dcdcde2013-06-28 16:59:19 +0000263 size_type rfind(const value_type* s, size_type pos, size_type n) const noexcept;
264 size_type rfind(const value_type* s, size_type pos = npos) const noexcept;
Howard Hinnanta6119a82011-05-29 19:57:12 +0000265 size_type rfind(value_type c, size_type pos = npos) const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000266
Howard Hinnanta6119a82011-05-29 19:57:12 +0000267 size_type find_first_of(const basic_string& str, size_type pos = 0) const noexcept;
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000268 size_type find_first_of(basic_string_view<charT, traits> sv, size_type pos = 0) const noexcept;
Howard Hinnant9dcdcde2013-06-28 16:59:19 +0000269 size_type find_first_of(const value_type* s, size_type pos, size_type n) const noexcept;
270 size_type find_first_of(const value_type* s, size_type pos = 0) const noexcept;
Howard Hinnanta6119a82011-05-29 19:57:12 +0000271 size_type find_first_of(value_type c, size_type pos = 0) const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000272
Howard Hinnanta6119a82011-05-29 19:57:12 +0000273 size_type find_last_of(const basic_string& str, size_type pos = npos) const noexcept;
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000274 size_type find_last_of(basic_string_view<charT, traits> sv, size_type pos = 0) const noexcept;
Howard Hinnant9dcdcde2013-06-28 16:59:19 +0000275 size_type find_last_of(const value_type* s, size_type pos, size_type n) const noexcept;
276 size_type find_last_of(const value_type* s, size_type pos = npos) const noexcept;
Howard Hinnanta6119a82011-05-29 19:57:12 +0000277 size_type find_last_of(value_type c, size_type pos = npos) const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000278
Howard Hinnanta6119a82011-05-29 19:57:12 +0000279 size_type find_first_not_of(const basic_string& str, size_type pos = 0) const noexcept;
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000280 size_type find_first_not_of(basic_string_view<charT, traits> sv, size_type pos = 0) const noexcept;
Howard Hinnant9dcdcde2013-06-28 16:59:19 +0000281 size_type find_first_not_of(const value_type* s, size_type pos, size_type n) const noexcept;
282 size_type find_first_not_of(const value_type* s, size_type pos = 0) const noexcept;
Howard Hinnanta6119a82011-05-29 19:57:12 +0000283 size_type find_first_not_of(value_type c, size_type pos = 0) const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000284
Howard Hinnanta6119a82011-05-29 19:57:12 +0000285 size_type find_last_not_of(const basic_string& str, size_type pos = npos) const noexcept;
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000286 size_type find_last_not_of(basic_string_view<charT, traits> sv, size_type pos = 0) const noexcept;
Howard Hinnant9dcdcde2013-06-28 16:59:19 +0000287 size_type find_last_not_of(const value_type* s, size_type pos, size_type n) const noexcept;
288 size_type find_last_not_of(const value_type* s, size_type pos = npos) const noexcept;
Howard Hinnanta6119a82011-05-29 19:57:12 +0000289 size_type find_last_not_of(value_type c, size_type pos = npos) const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000290
Howard Hinnanta6119a82011-05-29 19:57:12 +0000291 int compare(const basic_string& str) const noexcept;
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000292 int compare(basic_string_view<charT, traits> sv) const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000293 int compare(size_type pos1, size_type n1, const basic_string& str) const;
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000294 int compare(size_type pos1, size_type n1, basic_string_view<charT, traits> sv) const;
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000295 int compare(size_type pos1, size_type n1, const basic_string& str,
Marshall Clowa93b5e22014-03-04 19:17:19 +0000296 size_type pos2, size_type n2=npos) const; // C++14
Marshall Clow6ac8de02016-09-24 22:45:42 +0000297 template <class T>
298 int compare(size_type pos1, size_type n1, const T& t,
299 size_type pos2, size_type n2=npos) const; // C++17
Howard Hinnant9dcdcde2013-06-28 16:59:19 +0000300 int compare(const value_type* s) const noexcept;
301 int compare(size_type pos1, size_type n1, const value_type* s) const;
302 int compare(size_type pos1, size_type n1, const value_type* s, size_type n2) const;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000303
304 bool __invariants() const;
305};
306
307template<class charT, class traits, class Allocator>
308basic_string<charT, traits, Allocator>
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000309operator+(const basic_string<charT, traits, Allocator>& lhs,
310 const basic_string<charT, traits, Allocator>& rhs);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000311
312template<class charT, class traits, class Allocator>
313basic_string<charT, traits, Allocator>
314operator+(const charT* lhs , const basic_string<charT,traits,Allocator>&rhs);
315
316template<class charT, class traits, class Allocator>
317basic_string<charT, traits, Allocator>
318operator+(charT lhs, const basic_string<charT,traits,Allocator>& rhs);
319
320template<class charT, class traits, class Allocator>
321basic_string<charT, traits, Allocator>
322operator+(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs);
323
324template<class charT, class traits, class Allocator>
325basic_string<charT, traits, Allocator>
326operator+(const basic_string<charT, traits, Allocator>& lhs, charT rhs);
327
328template<class charT, class traits, class Allocator>
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000329bool operator==(const basic_string<charT, traits, Allocator>& lhs,
Howard Hinnanta6119a82011-05-29 19:57:12 +0000330 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000331
332template<class charT, class traits, class Allocator>
Howard Hinnanta6119a82011-05-29 19:57:12 +0000333bool operator==(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000334
335template<class charT, class traits, class Allocator>
Howard Hinnanta6119a82011-05-29 19:57:12 +0000336bool operator==(const basic_string<charT,traits,Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000337
Howard Hinnant324bb032010-08-22 00:02:43 +0000338template<class charT, class traits, class Allocator>
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000339bool operator!=(const basic_string<charT,traits,Allocator>& lhs,
Howard Hinnanta6119a82011-05-29 19:57:12 +0000340 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000341
342template<class charT, class traits, class Allocator>
Howard Hinnanta6119a82011-05-29 19:57:12 +0000343bool operator!=(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000344
345template<class charT, class traits, class Allocator>
Howard Hinnanta6119a82011-05-29 19:57:12 +0000346bool operator!=(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000347
348template<class charT, class traits, class Allocator>
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000349bool operator< (const basic_string<charT, traits, Allocator>& lhs,
Howard Hinnanta6119a82011-05-29 19:57:12 +0000350 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000351
352template<class charT, class traits, class Allocator>
Howard Hinnanta6119a82011-05-29 19:57:12 +0000353bool operator< (const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000354
355template<class charT, class traits, class Allocator>
Howard Hinnanta6119a82011-05-29 19:57:12 +0000356bool operator< (const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000357
358template<class charT, class traits, class Allocator>
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000359bool operator> (const basic_string<charT, traits, Allocator>& lhs,
Howard Hinnanta6119a82011-05-29 19:57:12 +0000360 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000361
362template<class charT, class traits, class Allocator>
Howard Hinnanta6119a82011-05-29 19:57:12 +0000363bool operator> (const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000364
365template<class charT, class traits, class Allocator>
Howard Hinnanta6119a82011-05-29 19:57:12 +0000366bool operator> (const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000367
368template<class charT, class traits, class Allocator>
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000369bool operator<=(const basic_string<charT, traits, Allocator>& lhs,
Howard Hinnanta6119a82011-05-29 19:57:12 +0000370 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000371
372template<class charT, class traits, class Allocator>
Howard Hinnanta6119a82011-05-29 19:57:12 +0000373bool operator<=(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000374
375template<class charT, class traits, class Allocator>
Howard Hinnanta6119a82011-05-29 19:57:12 +0000376bool operator<=(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000377
378template<class charT, class traits, class Allocator>
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000379bool operator>=(const basic_string<charT, traits, Allocator>& lhs,
Howard Hinnanta6119a82011-05-29 19:57:12 +0000380 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000381
382template<class charT, class traits, class Allocator>
Howard Hinnanta6119a82011-05-29 19:57:12 +0000383bool operator>=(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000384
385template<class charT, class traits, class Allocator>
Howard Hinnanta6119a82011-05-29 19:57:12 +0000386bool operator>=(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000387
388template<class charT, class traits, class Allocator>
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000389void swap(basic_string<charT, traits, Allocator>& lhs,
Howard Hinnant53f7d4c2011-06-03 18:40:47 +0000390 basic_string<charT, traits, Allocator>& rhs)
391 noexcept(noexcept(lhs.swap(rhs)));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000392
393template<class charT, class traits, class Allocator>
394basic_istream<charT, traits>&
395operator>>(basic_istream<charT, traits>& is, basic_string<charT, traits, Allocator>& str);
396
397template<class charT, class traits, class Allocator>
398basic_ostream<charT, traits>&
399operator<<(basic_ostream<charT, traits>& os, const basic_string<charT, traits, Allocator>& str);
400
401template<class charT, class traits, class Allocator>
Howard Hinnant324bb032010-08-22 00:02:43 +0000402basic_istream<charT, traits>&
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000403getline(basic_istream<charT, traits>& is, basic_string<charT, traits, Allocator>& str,
404 charT delim);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000405
406template<class charT, class traits, class Allocator>
407basic_istream<charT, traits>&
408getline(basic_istream<charT, traits>& is, basic_string<charT, traits, Allocator>& str);
409
410typedef basic_string<char> string;
411typedef basic_string<wchar_t> wstring;
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000412typedef basic_string<char16_t> u16string;
413typedef basic_string<char32_t> u32string;
414
415int stoi (const string& str, size_t* idx = 0, int base = 10);
416long stol (const string& str, size_t* idx = 0, int base = 10);
417unsigned long stoul (const string& str, size_t* idx = 0, int base = 10);
418long long stoll (const string& str, size_t* idx = 0, int base = 10);
419unsigned long long stoull(const string& str, size_t* idx = 0, int base = 10);
420
421float stof (const string& str, size_t* idx = 0);
422double stod (const string& str, size_t* idx = 0);
423long double stold(const string& str, size_t* idx = 0);
424
425string to_string(int val);
426string to_string(unsigned val);
427string to_string(long val);
428string to_string(unsigned long val);
429string to_string(long long val);
430string to_string(unsigned long long val);
431string to_string(float val);
432string to_string(double val);
433string to_string(long double val);
434
435int stoi (const wstring& str, size_t* idx = 0, int base = 10);
436long stol (const wstring& str, size_t* idx = 0, int base = 10);
437unsigned long stoul (const wstring& str, size_t* idx = 0, int base = 10);
438long long stoll (const wstring& str, size_t* idx = 0, int base = 10);
439unsigned long long stoull(const wstring& str, size_t* idx = 0, int base = 10);
440
441float stof (const wstring& str, size_t* idx = 0);
442double stod (const wstring& str, size_t* idx = 0);
443long double stold(const wstring& str, size_t* idx = 0);
444
445wstring to_wstring(int val);
446wstring to_wstring(unsigned val);
447wstring to_wstring(long val);
448wstring to_wstring(unsigned long val);
449wstring to_wstring(long long val);
450wstring to_wstring(unsigned long long val);
451wstring to_wstring(float val);
452wstring to_wstring(double val);
453wstring to_wstring(long double val);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000454
455template <> struct hash<string>;
456template <> struct hash<u16string>;
457template <> struct hash<u32string>;
458template <> struct hash<wstring>;
459
Marshall Clow15234322013-07-23 17:05:24 +0000460basic_string<char> operator "" s( const char *str, size_t len ); // C++14
461basic_string<wchar_t> operator "" s( const wchar_t *str, size_t len ); // C++14
462basic_string<char16_t> operator "" s( const char16_t *str, size_t len ); // C++14
463basic_string<char32_t> operator "" s( const char32_t *str, size_t len ); // C++14
464
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000465} // std
466
467*/
468
469#include <__config>
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000470#include <string_view>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000471#include <iosfwd>
472#include <cstring>
Howard Hinnantadff4892010-05-24 17:49:41 +0000473#include <cstdio> // For EOF.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000474#include <cwchar>
475#include <algorithm>
476#include <iterator>
477#include <utility>
478#include <memory>
479#include <stdexcept>
480#include <type_traits>
481#include <initializer_list>
482#include <__functional_base>
483#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
484#include <cstdint>
485#endif
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000486
Howard Hinnant66c6f972011-11-29 16:45:27 +0000487#include <__undef_min_max>
488
Eric Fiselierb9536102014-08-10 23:53:08 +0000489#include <__debug>
490
Howard Hinnant08e17472011-10-17 20:05:10 +0000491#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000492#pragma GCC system_header
Howard Hinnant08e17472011-10-17 20:05:10 +0000493#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000494
495_LIBCPP_BEGIN_NAMESPACE_STD
496
497// fpos
498
499template <class _StateT>
Eric Fiselierc3589a82017-01-04 23:56:00 +0000500class _LIBCPP_TEMPLATE_VIS fpos
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000501{
502private:
503 _StateT __st_;
504 streamoff __off_;
505public:
506 _LIBCPP_INLINE_VISIBILITY fpos(streamoff __off = streamoff()) : __st_(), __off_(__off) {}
507
508 _LIBCPP_INLINE_VISIBILITY operator streamoff() const {return __off_;}
509
510 _LIBCPP_INLINE_VISIBILITY _StateT state() const {return __st_;}
511 _LIBCPP_INLINE_VISIBILITY void state(_StateT __st) {__st_ = __st;}
512
513 _LIBCPP_INLINE_VISIBILITY fpos& operator+=(streamoff __off) {__off_ += __off; return *this;}
514 _LIBCPP_INLINE_VISIBILITY fpos operator+ (streamoff __off) const {fpos __t(*this); __t += __off; return __t;}
515 _LIBCPP_INLINE_VISIBILITY fpos& operator-=(streamoff __off) {__off_ -= __off; return *this;}
516 _LIBCPP_INLINE_VISIBILITY fpos operator- (streamoff __off) const {fpos __t(*this); __t -= __off; return __t;}
517};
518
519template <class _StateT>
520inline _LIBCPP_INLINE_VISIBILITY
521streamoff operator-(const fpos<_StateT>& __x, const fpos<_StateT>& __y)
522 {return streamoff(__x) - streamoff(__y);}
523
524template <class _StateT>
525inline _LIBCPP_INLINE_VISIBILITY
526bool operator==(const fpos<_StateT>& __x, const fpos<_StateT>& __y)
527 {return streamoff(__x) == streamoff(__y);}
528
529template <class _StateT>
530inline _LIBCPP_INLINE_VISIBILITY
531bool operator!=(const fpos<_StateT>& __x, const fpos<_StateT>& __y)
532 {return streamoff(__x) != streamoff(__y);}
533
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000534// basic_string
535
536template<class _CharT, class _Traits, class _Allocator>
537basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant2b1b2d42011-06-14 19:58:17 +0000538operator+(const basic_string<_CharT, _Traits, _Allocator>& __x,
539 const basic_string<_CharT, _Traits, _Allocator>& __y);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000540
541template<class _CharT, class _Traits, class _Allocator>
542basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant2b1b2d42011-06-14 19:58:17 +0000543operator+(const _CharT* __x, const basic_string<_CharT,_Traits,_Allocator>& __y);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000544
545template<class _CharT, class _Traits, class _Allocator>
546basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant2b1b2d42011-06-14 19:58:17 +0000547operator+(_CharT __x, const basic_string<_CharT,_Traits,_Allocator>& __y);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000548
549template<class _CharT, class _Traits, class _Allocator>
550basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant2b1b2d42011-06-14 19:58:17 +0000551operator+(const basic_string<_CharT, _Traits, _Allocator>& __x, const _CharT* __y);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000552
553template<class _CharT, class _Traits, class _Allocator>
554basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant2b1b2d42011-06-14 19:58:17 +0000555operator+(const basic_string<_CharT, _Traits, _Allocator>& __x, _CharT __y);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000556
557template <bool>
Eric Fiselierc3589a82017-01-04 23:56:00 +0000558class _LIBCPP_TEMPLATE_VIS __basic_string_common
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000559{
560protected:
Marshall Clow14c09a22016-08-25 15:09:01 +0000561 _LIBCPP_NORETURN void __throw_length_error() const;
562 _LIBCPP_NORETURN void __throw_out_of_range() const;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000563};
564
565template <bool __b>
566void
567__basic_string_common<__b>::__throw_length_error() const
568{
Marshall Clow14c09a22016-08-25 15:09:01 +0000569 _VSTD::__throw_length_error("basic_string");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000570}
571
572template <bool __b>
573void
574__basic_string_common<__b>::__throw_out_of_range() const
575{
Marshall Clow14c09a22016-08-25 15:09:01 +0000576 _VSTD::__throw_out_of_range("basic_string");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000577}
578
Howard Hinnante9df0a52013-08-01 18:17:34 +0000579#ifdef _LIBCPP_MSVC
Howard Hinnant78b68282011-10-22 20:59:45 +0000580#pragma warning( push )
581#pragma warning( disable: 4231 )
Howard Hinnante9df0a52013-08-01 18:17:34 +0000582#endif // _LIBCPP_MSVC
Eric Fiselier833d6442016-09-15 22:27:07 +0000583_LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS __basic_string_common<true>)
Howard Hinnante9df0a52013-08-01 18:17:34 +0000584#ifdef _LIBCPP_MSVC
Howard Hinnant78b68282011-10-22 20:59:45 +0000585#pragma warning( pop )
Howard Hinnante9df0a52013-08-01 18:17:34 +0000586#endif // _LIBCPP_MSVC
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000587
Marshall Clowdf9db312016-01-13 21:54:34 +0000588#ifdef _LIBCPP_NO_EXCEPTIONS
589template <class _Iter>
590struct __libcpp_string_gets_noexcept_iterator_impl : public true_type {};
591#elif defined(_LIBCPP_HAS_NO_NOEXCEPT)
592template <class _Iter>
593struct __libcpp_string_gets_noexcept_iterator_impl : public false_type {};
594#else
595template <class _Iter, bool = __is_forward_iterator<_Iter>::value>
596struct __libcpp_string_gets_noexcept_iterator_impl : public _LIBCPP_BOOL_CONSTANT((
597 noexcept(++(declval<_Iter&>())) &&
598 is_nothrow_assignable<_Iter&, _Iter>::value &&
599 noexcept(declval<_Iter>() == declval<_Iter>()) &&
600 noexcept(*declval<_Iter>())
601)) {};
602
603template <class _Iter>
604struct __libcpp_string_gets_noexcept_iterator_impl<_Iter, false> : public false_type {};
605#endif
606
607
608template <class _Iter>
609struct __libcpp_string_gets_noexcept_iterator
610 : public _LIBCPP_BOOL_CONSTANT(__libcpp_is_trivial_iterator<_Iter>::value || __libcpp_string_gets_noexcept_iterator_impl<_Iter>::value) {};
611
Marshall Clow6ac8de02016-09-24 22:45:42 +0000612template <class _CharT, class _Traits, class _Tp>
613struct __can_be_converted_to_string_view : public _LIBCPP_BOOL_CONSTANT(
614 ( is_convertible<const _Tp&, basic_string_view<_CharT, _Traits> >::value &&
615 !is_convertible<const _Tp&, const _CharT*>::value)) {};
616
Evgeniy Stepanov4f01aa82015-10-13 23:48:28 +0000617#ifdef _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant15467182013-04-30 21:44:48 +0000618
619template <class _CharT, size_t = sizeof(_CharT)>
620struct __padding
621{
622 unsigned char __xx[sizeof(_CharT)-1];
623};
624
625template <class _CharT>
626struct __padding<_CharT, 1>
627{
628};
629
Evgeniy Stepanov4f01aa82015-10-13 23:48:28 +0000630#endif // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant15467182013-04-30 21:44:48 +0000631
Howard Hinnant324bb032010-08-22 00:02:43 +0000632template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierc3589a82017-01-04 23:56:00 +0000633class _LIBCPP_TEMPLATE_VIS basic_string
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000634 : private __basic_string_common<true>
635{
636public:
637 typedef basic_string __self;
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000638 typedef basic_string_view<_CharT, _Traits> __self_view;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000639 typedef _Traits traits_type;
640 typedef typename traits_type::char_type value_type;
641 typedef _Allocator allocator_type;
Howard Hinnante32b5e22010-11-17 17:55:08 +0000642 typedef allocator_traits<allocator_type> __alloc_traits;
643 typedef typename __alloc_traits::size_type size_type;
644 typedef typename __alloc_traits::difference_type difference_type;
Howard Hinnant53f7d4c2011-06-03 18:40:47 +0000645 typedef value_type& reference;
646 typedef const value_type& const_reference;
Howard Hinnante32b5e22010-11-17 17:55:08 +0000647 typedef typename __alloc_traits::pointer pointer;
648 typedef typename __alloc_traits::const_pointer const_pointer;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000649
Howard Hinnant499cea12013-08-23 17:37:05 +0000650 static_assert(is_pod<value_type>::value, "Character type of basic_string must be a POD");
651 static_assert((is_same<_CharT, value_type>::value),
652 "traits_type::char_type must be the same type as CharT");
653 static_assert((is_same<typename allocator_type::value_type, value_type>::value),
654 "Allocator::value_type must be same type as value_type");
655#if defined(_LIBCPP_RAW_ITERATORS)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000656 typedef pointer iterator;
657 typedef const_pointer const_iterator;
Howard Hinnant324bb032010-08-22 00:02:43 +0000658#else // defined(_LIBCPP_RAW_ITERATORS)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000659 typedef __wrap_iter<pointer> iterator;
660 typedef __wrap_iter<const_pointer> const_iterator;
Howard Hinnant324bb032010-08-22 00:02:43 +0000661#endif // defined(_LIBCPP_RAW_ITERATORS)
Howard Hinnant0949eed2011-06-30 21:18:19 +0000662 typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
663 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000664
665private:
Howard Hinnant15467182013-04-30 21:44:48 +0000666
Evgeniy Stepanov4f01aa82015-10-13 23:48:28 +0000667#ifdef _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant15467182013-04-30 21:44:48 +0000668
669 struct __long
670 {
671 pointer __data_;
672 size_type __size_;
673 size_type __cap_;
674 };
675
676#if _LIBCPP_BIG_ENDIAN
677 enum {__short_mask = 0x01};
678 enum {__long_mask = 0x1ul};
679#else // _LIBCPP_BIG_ENDIAN
680 enum {__short_mask = 0x80};
681 enum {__long_mask = ~(size_type(~0) >> 1)};
682#endif // _LIBCPP_BIG_ENDIAN
683
684 enum {__min_cap = (sizeof(__long) - 1)/sizeof(value_type) > 2 ?
685 (sizeof(__long) - 1)/sizeof(value_type) : 2};
686
687 struct __short
688 {
689 value_type __data_[__min_cap];
690 struct
691 : __padding<value_type>
692 {
693 unsigned char __size_;
694 };
695 };
696
697#else
698
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000699 struct __long
700 {
701 size_type __cap_;
702 size_type __size_;
703 pointer __data_;
704 };
705
706#if _LIBCPP_BIG_ENDIAN
707 enum {__short_mask = 0x80};
708 enum {__long_mask = ~(size_type(~0) >> 1)};
Howard Hinnant324bb032010-08-22 00:02:43 +0000709#else // _LIBCPP_BIG_ENDIAN
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000710 enum {__short_mask = 0x01};
Howard Hinnantec3773c2011-12-01 20:21:04 +0000711 enum {__long_mask = 0x1ul};
Howard Hinnant324bb032010-08-22 00:02:43 +0000712#endif // _LIBCPP_BIG_ENDIAN
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000713
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000714 enum {__min_cap = (sizeof(__long) - 1)/sizeof(value_type) > 2 ?
715 (sizeof(__long) - 1)/sizeof(value_type) : 2};
716
717 struct __short
718 {
719 union
720 {
721 unsigned char __size_;
Howard Hinnant9c0df142012-10-30 19:06:59 +0000722 value_type __lx;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000723 };
724 value_type __data_[__min_cap];
725 };
726
Evgeniy Stepanov4f01aa82015-10-13 23:48:28 +0000727#endif // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant15467182013-04-30 21:44:48 +0000728
Howard Hinnant499cea12013-08-23 17:37:05 +0000729 union __ulx{__long __lx; __short __lxx;};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000730
Howard Hinnant499cea12013-08-23 17:37:05 +0000731 enum {__n_words = sizeof(__ulx) / sizeof(size_type)};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000732
733 struct __raw
734 {
735 size_type __words[__n_words];
736 };
737
738 struct __rep
739 {
740 union
741 {
742 __long __l;
743 __short __s;
744 __raw __r;
745 };
746 };
747
748 __compressed_pair<__rep, allocator_type> __r_;
749
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000750public:
751 static const size_type npos = -1;
752
Howard Hinnant53f7d4c2011-06-03 18:40:47 +0000753 _LIBCPP_INLINE_VISIBILITY basic_string()
754 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
Marshall Clow7b193f72015-06-03 19:56:43 +0000755
756 _LIBCPP_INLINE_VISIBILITY explicit basic_string(const allocator_type& __a)
757#if _LIBCPP_STD_VER <= 14
758 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value);
759#else
760 _NOEXCEPT;
761#endif
762
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000763 basic_string(const basic_string& __str);
764 basic_string(const basic_string& __str, const allocator_type& __a);
Marshall Clow7b193f72015-06-03 19:56:43 +0000765
Howard Hinnant73d21a42010-09-04 23:28:19 +0000766#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant9f193f22011-01-26 00:06:59 +0000767 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant53f7d4c2011-06-03 18:40:47 +0000768 basic_string(basic_string&& __str)
Marshall Clow7b193f72015-06-03 19:56:43 +0000769#if _LIBCPP_STD_VER <= 14
Howard Hinnant53f7d4c2011-06-03 18:40:47 +0000770 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
Marshall Clow7b193f72015-06-03 19:56:43 +0000771#else
772 _NOEXCEPT;
773#endif
774
Howard Hinnant9f193f22011-01-26 00:06:59 +0000775 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000776 basic_string(basic_string&& __str, const allocator_type& __a);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000777#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant9dcdcde2013-06-28 16:59:19 +0000778 _LIBCPP_INLINE_VISIBILITY basic_string(const value_type* __s);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000779 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9dcdcde2013-06-28 16:59:19 +0000780 basic_string(const value_type* __s, const allocator_type& __a);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000781 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9dcdcde2013-06-28 16:59:19 +0000782 basic_string(const value_type* __s, size_type __n);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000783 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9dcdcde2013-06-28 16:59:19 +0000784 basic_string(const value_type* __s, size_type __n, const allocator_type& __a);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000785 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000786 basic_string(size_type __n, value_type __c);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000787 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000788 basic_string(size_type __n, value_type __c, const allocator_type& __a);
Marshall Clowf4f7d8f2016-04-07 18:13:41 +0000789 basic_string(const basic_string& __str, size_type __pos, size_type __n,
790 const allocator_type& __a = allocator_type());
791 _LIBCPP_INLINE_VISIBILITY
792 basic_string(const basic_string& __str, size_type __pos,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000793 const allocator_type& __a = allocator_type());
Marshall Clowdb7fa112016-11-14 18:22:19 +0000794 template<class _Tp>
795 basic_string(const _Tp& __t, size_type __pos, size_type __n,
796 const allocator_type& __a = allocator_type(),
797 typename enable_if<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value, void>::type* = 0);
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000798 _LIBCPP_INLINE_VISIBILITY explicit
799 basic_string(__self_view __sv);
800 _LIBCPP_INLINE_VISIBILITY
801 basic_string(__self_view __sv, const allocator_type& __a);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000802 template<class _InputIterator>
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000803 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000804 basic_string(_InputIterator __first, _InputIterator __last);
805 template<class _InputIterator>
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000806 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000807 basic_string(_InputIterator __first, _InputIterator __last, const allocator_type& __a);
Howard Hinnante3e32912011-08-12 21:56:02 +0000808#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000809 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000810 basic_string(initializer_list<value_type> __il);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000811 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000812 basic_string(initializer_list<value_type> __il, const allocator_type& __a);
Howard Hinnante3e32912011-08-12 21:56:02 +0000813#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000814
Eric Fiselier51eb1d52016-10-31 03:42:50 +0000815 inline ~basic_string();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000816
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000817 _LIBCPP_INLINE_VISIBILITY
818 operator __self_view() const _NOEXCEPT { return __self_view(data(), size()); }
819
Howard Hinnante32b5e22010-11-17 17:55:08 +0000820 basic_string& operator=(const basic_string& __str);
Eric Fiselierf472d6c2017-01-23 21:24:58 +0000821
822#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier37b2be92017-01-17 22:10:32 +0000823 template <class = void>
Eric Fiselierf472d6c2017-01-23 21:24:58 +0000824#endif
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000825 _LIBCPP_INLINE_VISIBILITY
826 basic_string& operator=(__self_view __sv) {return assign(__sv);}
Howard Hinnant73d21a42010-09-04 23:28:19 +0000827#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000828 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant53f7d4c2011-06-03 18:40:47 +0000829 basic_string& operator=(basic_string&& __str)
Marshall Clowaf961ed2015-08-18 18:57:00 +0000830 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000831#endif
Howard Hinnant9dcdcde2013-06-28 16:59:19 +0000832 _LIBCPP_INLINE_VISIBILITY basic_string& operator=(const value_type* __s) {return assign(__s);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000833 basic_string& operator=(value_type __c);
Howard Hinnante3e32912011-08-12 21:56:02 +0000834#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000835 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000836 basic_string& operator=(initializer_list<value_type> __il) {return assign(__il.begin(), __il.size());}
Howard Hinnante3e32912011-08-12 21:56:02 +0000837#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000838
Howard Hinnant499cea12013-08-23 17:37:05 +0000839#if _LIBCPP_DEBUG_LEVEL >= 2
840 _LIBCPP_INLINE_VISIBILITY
841 iterator begin() _NOEXCEPT
842 {return iterator(this, __get_pointer());}
843 _LIBCPP_INLINE_VISIBILITY
844 const_iterator begin() const _NOEXCEPT
845 {return const_iterator(this, __get_pointer());}
846 _LIBCPP_INLINE_VISIBILITY
847 iterator end() _NOEXCEPT
848 {return iterator(this, __get_pointer() + size());}
849 _LIBCPP_INLINE_VISIBILITY
850 const_iterator end() const _NOEXCEPT
851 {return const_iterator(this, __get_pointer() + size());}
852#else
Howard Hinnanta6119a82011-05-29 19:57:12 +0000853 _LIBCPP_INLINE_VISIBILITY
854 iterator begin() _NOEXCEPT
855 {return iterator(__get_pointer());}
856 _LIBCPP_INLINE_VISIBILITY
857 const_iterator begin() const _NOEXCEPT
Howard Hinnant9dcdcde2013-06-28 16:59:19 +0000858 {return const_iterator(__get_pointer());}
Howard Hinnanta6119a82011-05-29 19:57:12 +0000859 _LIBCPP_INLINE_VISIBILITY
860 iterator end() _NOEXCEPT
861 {return iterator(__get_pointer() + size());}
862 _LIBCPP_INLINE_VISIBILITY
863 const_iterator end() const _NOEXCEPT
Howard Hinnant9dcdcde2013-06-28 16:59:19 +0000864 {return const_iterator(__get_pointer() + size());}
Howard Hinnant499cea12013-08-23 17:37:05 +0000865#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnanta6119a82011-05-29 19:57:12 +0000866 _LIBCPP_INLINE_VISIBILITY
867 reverse_iterator rbegin() _NOEXCEPT
868 {return reverse_iterator(end());}
869 _LIBCPP_INLINE_VISIBILITY
870 const_reverse_iterator rbegin() const _NOEXCEPT
871 {return const_reverse_iterator(end());}
872 _LIBCPP_INLINE_VISIBILITY
873 reverse_iterator rend() _NOEXCEPT
874 {return reverse_iterator(begin());}
875 _LIBCPP_INLINE_VISIBILITY
876 const_reverse_iterator rend() const _NOEXCEPT
877 {return const_reverse_iterator(begin());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000878
Howard Hinnanta6119a82011-05-29 19:57:12 +0000879 _LIBCPP_INLINE_VISIBILITY
880 const_iterator cbegin() const _NOEXCEPT
881 {return begin();}
882 _LIBCPP_INLINE_VISIBILITY
883 const_iterator cend() const _NOEXCEPT
884 {return end();}
885 _LIBCPP_INLINE_VISIBILITY
886 const_reverse_iterator crbegin() const _NOEXCEPT
887 {return rbegin();}
888 _LIBCPP_INLINE_VISIBILITY
889 const_reverse_iterator crend() const _NOEXCEPT
890 {return rend();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000891
Howard Hinnanta6119a82011-05-29 19:57:12 +0000892 _LIBCPP_INLINE_VISIBILITY size_type size() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000893 {return __is_long() ? __get_long_size() : __get_short_size();}
Howard Hinnanta6119a82011-05-29 19:57:12 +0000894 _LIBCPP_INLINE_VISIBILITY size_type length() const _NOEXCEPT {return size();}
895 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT;
896 _LIBCPP_INLINE_VISIBILITY size_type capacity() const _NOEXCEPT
Eric Fiseliere2d48922015-08-28 07:02:42 +0000897 {return (__is_long() ? __get_long_cap()
898 : static_cast<size_type>(__min_cap)) - 1;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000899
900 void resize(size_type __n, value_type __c);
901 _LIBCPP_INLINE_VISIBILITY void resize(size_type __n) {resize(__n, value_type());}
902
903 void reserve(size_type res_arg = 0);
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000904 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant53f7d4c2011-06-03 18:40:47 +0000905 void shrink_to_fit() _NOEXCEPT {reserve();}
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000906 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta6119a82011-05-29 19:57:12 +0000907 void clear() _NOEXCEPT;
908 _LIBCPP_INLINE_VISIBILITY bool empty() const _NOEXCEPT {return size() == 0;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000909
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000910 _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_type __pos) const _NOEXCEPT;
911 _LIBCPP_INLINE_VISIBILITY reference operator[](size_type __pos) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000912
913 const_reference at(size_type __n) const;
914 reference at(size_type __n);
915
916 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(const basic_string& __str) {return append(__str);}
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000917 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(__self_view __sv) {return append(__sv);}
918 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(const value_type* __s) {return append(__s);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000919 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(value_type __c) {push_back(__c); return *this;}
Howard Hinnante3e32912011-08-12 21:56:02 +0000920#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000921 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(initializer_list<value_type> __il) {return append(__il);}
Howard Hinnante3e32912011-08-12 21:56:02 +0000922#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000923
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000924 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000925 basic_string& append(const basic_string& __str);
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000926 _LIBCPP_INLINE_VISIBILITY
927 basic_string& append(__self_view __sv) { return append(__sv.data(), __sv.size()); }
Marshall Clowa93b5e22014-03-04 19:17:19 +0000928 basic_string& append(const basic_string& __str, size_type __pos, size_type __n=npos);
Marshall Clow42a87db2016-10-03 23:40:48 +0000929 template <class _Tp>
Marshall Clow6ac8de02016-09-24 22:45:42 +0000930 typename enable_if
931 <
932 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
933 basic_string&
934 >::type
935 append(const _Tp& __t, size_type __pos, size_type __n=npos);
Howard Hinnant9dcdcde2013-06-28 16:59:19 +0000936 basic_string& append(const value_type* __s, size_type __n);
937 basic_string& append(const value_type* __s);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000938 basic_string& append(size_type __n, value_type __c);
Eric Fiselier026d38e2016-10-31 02:46:25 +0000939 template <class _ForwardIterator>
Eric Fiselierd5b0db52016-10-31 03:40:29 +0000940 inline basic_string& __append_forward_unsafe(_ForwardIterator, _ForwardIterator);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000941 template<class _InputIterator>
942 typename enable_if
943 <
Marshall Clowdf9db312016-01-13 21:54:34 +0000944 __is_exactly_input_iterator<_InputIterator>::value
945 || !__libcpp_string_gets_noexcept_iterator<_InputIterator>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000946 basic_string&
947 >::type
Eric Fiselier026d38e2016-10-31 02:46:25 +0000948 _LIBCPP_INLINE_VISIBILITY
949 append(_InputIterator __first, _InputIterator __last) {
950 const basic_string __temp (__first, __last, __alloc());
951 append(__temp.data(), __temp.size());
952 return *this;
953 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000954 template<class _ForwardIterator>
955 typename enable_if
956 <
Marshall Clowdf9db312016-01-13 21:54:34 +0000957 __is_forward_iterator<_ForwardIterator>::value
958 && __libcpp_string_gets_noexcept_iterator<_ForwardIterator>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000959 basic_string&
960 >::type
Eric Fiselier026d38e2016-10-31 02:46:25 +0000961 _LIBCPP_INLINE_VISIBILITY
962 append(_ForwardIterator __first, _ForwardIterator __last) {
963 return __append_forward_unsafe(__first, __last);
964 }
965
Howard Hinnante3e32912011-08-12 21:56:02 +0000966#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000967 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000968 basic_string& append(initializer_list<value_type> __il) {return append(__il.begin(), __il.size());}
Howard Hinnante3e32912011-08-12 21:56:02 +0000969#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000970
971 void push_back(value_type __c);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000972 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000973 void pop_back();
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000974 _LIBCPP_INLINE_VISIBILITY reference front();
975 _LIBCPP_INLINE_VISIBILITY const_reference front() const;
976 _LIBCPP_INLINE_VISIBILITY reference back();
977 _LIBCPP_INLINE_VISIBILITY const_reference back() const;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000978
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000979 _LIBCPP_INLINE_VISIBILITY
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000980 basic_string& assign(__self_view __sv) { return assign(__sv.data(), __sv.size()); }
981 _LIBCPP_INLINE_VISIBILITY
Marshall Clowf40ec902016-03-09 18:08:29 +0000982 basic_string& assign(const basic_string& __str) { return *this = __str; }
Howard Hinnanta6119a82011-05-29 19:57:12 +0000983#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
984 _LIBCPP_INLINE_VISIBILITY
985 basic_string& assign(basic_string&& str)
Marshall Clow7ed093b2015-10-05 16:17:34 +0000986 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
Howard Hinnant0949eed2011-06-30 21:18:19 +0000987 {*this = _VSTD::move(str); return *this;}
Howard Hinnanta6119a82011-05-29 19:57:12 +0000988#endif
Marshall Clowa93b5e22014-03-04 19:17:19 +0000989 basic_string& assign(const basic_string& __str, size_type __pos, size_type __n=npos);
Marshall Clow42a87db2016-10-03 23:40:48 +0000990 template <class _Tp>
Marshall Clow6ac8de02016-09-24 22:45:42 +0000991 typename enable_if
992 <
993 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
994 basic_string&
995 >::type
996 assign(const _Tp & __t, size_type pos, size_type n=npos);
Howard Hinnant9dcdcde2013-06-28 16:59:19 +0000997 basic_string& assign(const value_type* __s, size_type __n);
998 basic_string& assign(const value_type* __s);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000999 basic_string& assign(size_type __n, value_type __c);
1000 template<class _InputIterator>
1001 typename enable_if
1002 <
Marshall Clowdf9db312016-01-13 21:54:34 +00001003 __is_exactly_input_iterator<_InputIterator>::value
1004 || !__libcpp_string_gets_noexcept_iterator<_InputIterator>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001005 basic_string&
1006 >::type
1007 assign(_InputIterator __first, _InputIterator __last);
1008 template<class _ForwardIterator>
1009 typename enable_if
1010 <
Marshall Clowdf9db312016-01-13 21:54:34 +00001011 __is_forward_iterator<_ForwardIterator>::value
1012 && __libcpp_string_gets_noexcept_iterator<_ForwardIterator>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001013 basic_string&
1014 >::type
1015 assign(_ForwardIterator __first, _ForwardIterator __last);
Howard Hinnante3e32912011-08-12 21:56:02 +00001016#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant8d7a9552010-09-23 17:31:07 +00001017 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001018 basic_string& assign(initializer_list<value_type> __il) {return assign(__il.begin(), __il.size());}
Howard Hinnante3e32912011-08-12 21:56:02 +00001019#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001020
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001021 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001022 basic_string& insert(size_type __pos1, const basic_string& __str);
Marshall Clow1e00d6d2016-07-21 05:31:24 +00001023 _LIBCPP_INLINE_VISIBILITY
1024 basic_string& insert(size_type __pos1, __self_view __sv) { return insert(__pos1, __sv.data(), __sv.size()); }
Marshall Clow6ac8de02016-09-24 22:45:42 +00001025 template <class _Tp>
1026 typename enable_if
1027 <
1028 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1029 basic_string&
1030 >::type
1031 insert(size_type __pos1, const _Tp& __t, size_type __pos2, size_type __n=npos);
Marshall Clowa93b5e22014-03-04 19:17:19 +00001032 basic_string& insert(size_type __pos1, const basic_string& __str, size_type __pos2, size_type __n=npos);
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001033 basic_string& insert(size_type __pos, const value_type* __s, size_type __n);
1034 basic_string& insert(size_type __pos, const value_type* __s);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001035 basic_string& insert(size_type __pos, size_type __n, value_type __c);
1036 iterator insert(const_iterator __pos, value_type __c);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001037 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001038 iterator insert(const_iterator __pos, size_type __n, value_type __c);
1039 template<class _InputIterator>
1040 typename enable_if
1041 <
Marshall Clowdf9db312016-01-13 21:54:34 +00001042 __is_exactly_input_iterator<_InputIterator>::value
1043 || !__libcpp_string_gets_noexcept_iterator<_InputIterator>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001044 iterator
1045 >::type
1046 insert(const_iterator __pos, _InputIterator __first, _InputIterator __last);
1047 template<class _ForwardIterator>
1048 typename enable_if
1049 <
Marshall Clowdf9db312016-01-13 21:54:34 +00001050 __is_forward_iterator<_ForwardIterator>::value
1051 && __libcpp_string_gets_noexcept_iterator<_ForwardIterator>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001052 iterator
1053 >::type
1054 insert(const_iterator __pos, _ForwardIterator __first, _ForwardIterator __last);
Howard Hinnante3e32912011-08-12 21:56:02 +00001055#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant8d7a9552010-09-23 17:31:07 +00001056 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001057 iterator insert(const_iterator __pos, initializer_list<value_type> __il)
1058 {return insert(__pos, __il.begin(), __il.end());}
Howard Hinnante3e32912011-08-12 21:56:02 +00001059#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001060
1061 basic_string& erase(size_type __pos = 0, size_type __n = npos);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001062 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001063 iterator erase(const_iterator __pos);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001064 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001065 iterator erase(const_iterator __first, const_iterator __last);
1066
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001067 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001068 basic_string& replace(size_type __pos1, size_type __n1, const basic_string& __str);
Marshall Clow1e00d6d2016-07-21 05:31:24 +00001069 _LIBCPP_INLINE_VISIBILITY
1070 basic_string& replace(size_type __pos1, size_type __n1, __self_view __sv) { return replace(__pos1, __n1, __sv.data(), __sv.size()); }
Marshall Clowa93b5e22014-03-04 19:17:19 +00001071 basic_string& replace(size_type __pos1, size_type __n1, const basic_string& __str, size_type __pos2, size_type __n2=npos);
Marshall Clow6ac8de02016-09-24 22:45:42 +00001072 template <class _Tp>
1073 typename enable_if
1074 <
1075 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1076 basic_string&
1077 >::type
1078 replace(size_type __pos1, size_type __n1, const _Tp& __t, size_type __pos2, size_type __n2=npos);
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001079 basic_string& replace(size_type __pos, size_type __n1, const value_type* __s, size_type __n2);
1080 basic_string& replace(size_type __pos, size_type __n1, const value_type* __s);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001081 basic_string& replace(size_type __pos, size_type __n1, size_type __n2, value_type __c);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001082 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7b2cb482010-11-17 21:11:40 +00001083 basic_string& replace(const_iterator __i1, const_iterator __i2, const basic_string& __str);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001084 _LIBCPP_INLINE_VISIBILITY
Marshall Clow1e00d6d2016-07-21 05:31:24 +00001085 basic_string& replace(const_iterator __i1, const_iterator __i2, __self_view __sv) { return replace(__i1 - begin(), __i2 - __i1, __sv); }
1086 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001087 basic_string& replace(const_iterator __i1, const_iterator __i2, const value_type* __s, size_type __n);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001088 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001089 basic_string& replace(const_iterator __i1, const_iterator __i2, const value_type* __s);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001090 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7b2cb482010-11-17 21:11:40 +00001091 basic_string& replace(const_iterator __i1, const_iterator __i2, size_type __n, value_type __c);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001092 template<class _InputIterator>
1093 typename enable_if
1094 <
1095 __is_input_iterator<_InputIterator>::value,
1096 basic_string&
1097 >::type
Howard Hinnant7b2cb482010-11-17 21:11:40 +00001098 replace(const_iterator __i1, const_iterator __i2, _InputIterator __j1, _InputIterator __j2);
Howard Hinnante3e32912011-08-12 21:56:02 +00001099#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant8d7a9552010-09-23 17:31:07 +00001100 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7b2cb482010-11-17 21:11:40 +00001101 basic_string& replace(const_iterator __i1, const_iterator __i2, initializer_list<value_type> __il)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001102 {return replace(__i1, __i2, __il.begin(), __il.end());}
Howard Hinnante3e32912011-08-12 21:56:02 +00001103#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001104
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001105 size_type copy(value_type* __s, size_type __n, size_type __pos = 0) const;
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001106 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001107 basic_string substr(size_type __pos = 0, size_type __n = npos) const;
1108
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001109 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant53f7d4c2011-06-03 18:40:47 +00001110 void swap(basic_string& __str)
Marshall Clow7d914d12015-07-13 20:04:56 +00001111#if _LIBCPP_STD_VER >= 14
Eric Fiselier47257c42016-12-28 05:53:01 +00001112 _NOEXCEPT_DEBUG;
Marshall Clow7d914d12015-07-13 20:04:56 +00001113#else
Eric Fiselier47257c42016-12-28 05:53:01 +00001114 _NOEXCEPT_DEBUG_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow7d914d12015-07-13 20:04:56 +00001115 __is_nothrow_swappable<allocator_type>::value);
1116#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001117
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001118 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001119 const value_type* c_str() const _NOEXCEPT {return data();}
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001120 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001121 const value_type* data() const _NOEXCEPT {return _VSTD::__to_raw_pointer(__get_pointer());}
Marshall Clowf532a702016-03-08 15:44:30 +00001122#if _LIBCPP_STD_VER > 14
1123 _LIBCPP_INLINE_VISIBILITY
1124 value_type* data() _NOEXCEPT {return _VSTD::__to_raw_pointer(__get_pointer());}
1125#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001126
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001127 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta6119a82011-05-29 19:57:12 +00001128 allocator_type get_allocator() const _NOEXCEPT {return __alloc();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001129
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001130 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta6119a82011-05-29 19:57:12 +00001131 size_type find(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
Marshall Clow1e00d6d2016-07-21 05:31:24 +00001132 _LIBCPP_INLINE_VISIBILITY
1133 size_type find(__self_view __sv, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001134 size_type find(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001135 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001136 size_type find(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnanta6119a82011-05-29 19:57:12 +00001137 size_type find(value_type __c, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001138
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001139 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta6119a82011-05-29 19:57:12 +00001140 size_type rfind(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
Marshall Clow1e00d6d2016-07-21 05:31:24 +00001141 _LIBCPP_INLINE_VISIBILITY
1142 size_type rfind(__self_view __sv, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001143 size_type rfind(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001144 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001145 size_type rfind(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnanta6119a82011-05-29 19:57:12 +00001146 size_type rfind(value_type __c, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001147
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001148 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta6119a82011-05-29 19:57:12 +00001149 size_type find_first_of(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
Marshall Clow1e00d6d2016-07-21 05:31:24 +00001150 _LIBCPP_INLINE_VISIBILITY
1151 size_type find_first_of(__self_view __sv, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001152 size_type find_first_of(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001153 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001154 size_type find_first_of(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001155 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta6119a82011-05-29 19:57:12 +00001156 size_type find_first_of(value_type __c, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001157
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001158 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta6119a82011-05-29 19:57:12 +00001159 size_type find_last_of(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
Marshall Clow1e00d6d2016-07-21 05:31:24 +00001160 _LIBCPP_INLINE_VISIBILITY
1161 size_type find_last_of(__self_view __sv, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001162 size_type find_last_of(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001163 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001164 size_type find_last_of(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001165 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta6119a82011-05-29 19:57:12 +00001166 size_type find_last_of(value_type __c, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001167
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001168 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta6119a82011-05-29 19:57:12 +00001169 size_type find_first_not_of(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
Marshall Clow1e00d6d2016-07-21 05:31:24 +00001170 _LIBCPP_INLINE_VISIBILITY
1171 size_type find_first_not_of(__self_view __sv, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001172 size_type find_first_not_of(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnanta6119a82011-05-29 19:57:12 +00001173 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001174 size_type find_first_not_of(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnanta6119a82011-05-29 19:57:12 +00001175 _LIBCPP_INLINE_VISIBILITY
1176 size_type find_first_not_of(value_type __c, size_type __pos = 0) const _NOEXCEPT;
1177
1178 _LIBCPP_INLINE_VISIBILITY
1179 size_type find_last_not_of(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
Marshall Clow1e00d6d2016-07-21 05:31:24 +00001180 _LIBCPP_INLINE_VISIBILITY
1181 size_type find_last_not_of(__self_view __sv, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001182 size_type find_last_not_of(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnanta6119a82011-05-29 19:57:12 +00001183 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001184 size_type find_last_not_of(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnanta6119a82011-05-29 19:57:12 +00001185 _LIBCPP_INLINE_VISIBILITY
1186 size_type find_last_not_of(value_type __c, size_type __pos = npos) const _NOEXCEPT;
1187
1188 _LIBCPP_INLINE_VISIBILITY
1189 int compare(const basic_string& __str) const _NOEXCEPT;
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001190 _LIBCPP_INLINE_VISIBILITY
Marshall Clow1e00d6d2016-07-21 05:31:24 +00001191 int compare(__self_view __sv) const _NOEXCEPT;
1192 _LIBCPP_INLINE_VISIBILITY
1193 int compare(size_type __pos1, size_type __n1, __self_view __sv) const;
1194 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001195 int compare(size_type __pos1, size_type __n1, const basic_string& __str) const;
Marshall Clowa93b5e22014-03-04 19:17:19 +00001196 int compare(size_type __pos1, size_type __n1, const basic_string& __str, size_type __pos2, size_type __n2=npos) const;
Marshall Clow6ac8de02016-09-24 22:45:42 +00001197 template <class _Tp>
Eric Fiselier9dbc0532016-10-14 05:29:46 +00001198 inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow6ac8de02016-09-24 22:45:42 +00001199 typename enable_if
1200 <
1201 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1202 int
1203 >::type
1204 compare(size_type __pos1, size_type __n1, const _Tp& __t, size_type __pos2, size_type __n2=npos) const;
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001205 int compare(const value_type* __s) const _NOEXCEPT;
1206 int compare(size_type __pos1, size_type __n1, const value_type* __s) const;
1207 int compare(size_type __pos1, size_type __n1, const value_type* __s, size_type __n2) const;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001208
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001209 _LIBCPP_INLINE_VISIBILITY bool __invariants() const;
Howard Hinnant08dd2532013-04-22 23:55:13 +00001210
1211 _LIBCPP_INLINE_VISIBILITY
1212 bool __is_long() const _NOEXCEPT
1213 {return bool(__r_.first().__s.__size_ & __short_mask);}
1214
Howard Hinnant499cea12013-08-23 17:37:05 +00001215#if _LIBCPP_DEBUG_LEVEL >= 2
1216
1217 bool __dereferenceable(const const_iterator* __i) const;
1218 bool __decrementable(const const_iterator* __i) const;
1219 bool __addable(const const_iterator* __i, ptrdiff_t __n) const;
1220 bool __subscriptable(const const_iterator* __i, ptrdiff_t __n) const;
1221
1222#endif // _LIBCPP_DEBUG_LEVEL >= 2
1223
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001224private:
Howard Hinnanta6119a82011-05-29 19:57:12 +00001225 _LIBCPP_INLINE_VISIBILITY
1226 allocator_type& __alloc() _NOEXCEPT
1227 {return __r_.second();}
1228 _LIBCPP_INLINE_VISIBILITY
1229 const allocator_type& __alloc() const _NOEXCEPT
1230 {return __r_.second();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001231
Evgeniy Stepanov4f01aa82015-10-13 23:48:28 +00001232#ifdef _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant15467182013-04-30 21:44:48 +00001233
Howard Hinnanta6119a82011-05-29 19:57:12 +00001234 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta6119a82011-05-29 19:57:12 +00001235 void __set_short_size(size_type __s) _NOEXCEPT
Howard Hinnant15467182013-04-30 21:44:48 +00001236# if _LIBCPP_BIG_ENDIAN
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001237 {__r_.first().__s.__size_ = (unsigned char)(__s << 1);}
Howard Hinnant15467182013-04-30 21:44:48 +00001238# else
1239 {__r_.first().__s.__size_ = (unsigned char)(__s);}
1240# endif
1241
Howard Hinnanta6119a82011-05-29 19:57:12 +00001242 _LIBCPP_INLINE_VISIBILITY
1243 size_type __get_short_size() const _NOEXCEPT
Howard Hinnant15467182013-04-30 21:44:48 +00001244# if _LIBCPP_BIG_ENDIAN
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001245 {return __r_.first().__s.__size_ >> 1;}
Howard Hinnant15467182013-04-30 21:44:48 +00001246# else
1247 {return __r_.first().__s.__size_;}
1248# endif
1249
Evgeniy Stepanov4f01aa82015-10-13 23:48:28 +00001250#else // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant15467182013-04-30 21:44:48 +00001251
1252 _LIBCPP_INLINE_VISIBILITY
1253 void __set_short_size(size_type __s) _NOEXCEPT
1254# if _LIBCPP_BIG_ENDIAN
1255 {__r_.first().__s.__size_ = (unsigned char)(__s);}
1256# else
1257 {__r_.first().__s.__size_ = (unsigned char)(__s << 1);}
1258# endif
1259
1260 _LIBCPP_INLINE_VISIBILITY
1261 size_type __get_short_size() const _NOEXCEPT
1262# if _LIBCPP_BIG_ENDIAN
1263 {return __r_.first().__s.__size_;}
1264# else
1265 {return __r_.first().__s.__size_ >> 1;}
1266# endif
1267
Evgeniy Stepanov4f01aa82015-10-13 23:48:28 +00001268#endif // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant15467182013-04-30 21:44:48 +00001269
Howard Hinnanta6119a82011-05-29 19:57:12 +00001270 _LIBCPP_INLINE_VISIBILITY
1271 void __set_long_size(size_type __s) _NOEXCEPT
1272 {__r_.first().__l.__size_ = __s;}
1273 _LIBCPP_INLINE_VISIBILITY
1274 size_type __get_long_size() const _NOEXCEPT
1275 {return __r_.first().__l.__size_;}
1276 _LIBCPP_INLINE_VISIBILITY
1277 void __set_size(size_type __s) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001278 {if (__is_long()) __set_long_size(__s); else __set_short_size(__s);}
1279
Howard Hinnanta6119a82011-05-29 19:57:12 +00001280 _LIBCPP_INLINE_VISIBILITY
1281 void __set_long_cap(size_type __s) _NOEXCEPT
1282 {__r_.first().__l.__cap_ = __long_mask | __s;}
1283 _LIBCPP_INLINE_VISIBILITY
1284 size_type __get_long_cap() const _NOEXCEPT
Howard Hinnantec3773c2011-12-01 20:21:04 +00001285 {return __r_.first().__l.__cap_ & size_type(~__long_mask);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001286
Howard Hinnanta6119a82011-05-29 19:57:12 +00001287 _LIBCPP_INLINE_VISIBILITY
1288 void __set_long_pointer(pointer __p) _NOEXCEPT
1289 {__r_.first().__l.__data_ = __p;}
1290 _LIBCPP_INLINE_VISIBILITY
1291 pointer __get_long_pointer() _NOEXCEPT
1292 {return __r_.first().__l.__data_;}
1293 _LIBCPP_INLINE_VISIBILITY
1294 const_pointer __get_long_pointer() const _NOEXCEPT
1295 {return __r_.first().__l.__data_;}
1296 _LIBCPP_INLINE_VISIBILITY
1297 pointer __get_short_pointer() _NOEXCEPT
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001298 {return pointer_traits<pointer>::pointer_to(__r_.first().__s.__data_[0]);}
Howard Hinnanta6119a82011-05-29 19:57:12 +00001299 _LIBCPP_INLINE_VISIBILITY
1300 const_pointer __get_short_pointer() const _NOEXCEPT
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001301 {return pointer_traits<const_pointer>::pointer_to(__r_.first().__s.__data_[0]);}
Howard Hinnanta6119a82011-05-29 19:57:12 +00001302 _LIBCPP_INLINE_VISIBILITY
1303 pointer __get_pointer() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001304 {return __is_long() ? __get_long_pointer() : __get_short_pointer();}
Howard Hinnanta6119a82011-05-29 19:57:12 +00001305 _LIBCPP_INLINE_VISIBILITY
1306 const_pointer __get_pointer() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001307 {return __is_long() ? __get_long_pointer() : __get_short_pointer();}
1308
Howard Hinnanta6119a82011-05-29 19:57:12 +00001309 _LIBCPP_INLINE_VISIBILITY
1310 void __zero() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001311 {
1312 size_type (&__a)[__n_words] = __r_.first().__r.__words;
1313 for (unsigned __i = 0; __i < __n_words; ++__i)
1314 __a[__i] = 0;
1315 }
1316
1317 template <size_type __a> static
Howard Hinnanta6119a82011-05-29 19:57:12 +00001318 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7f764502013-08-14 18:00:20 +00001319 size_type __align_it(size_type __s) _NOEXCEPT
Eric Fiseliere2d48922015-08-28 07:02:42 +00001320 {return (__s + (__a-1)) & ~(__a-1);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001321 enum {__alignment = 16};
Howard Hinnanta6119a82011-05-29 19:57:12 +00001322 static _LIBCPP_INLINE_VISIBILITY
1323 size_type __recommend(size_type __s) _NOEXCEPT
Eric Fiseliere2d48922015-08-28 07:02:42 +00001324 {return (__s < __min_cap ? static_cast<size_type>(__min_cap) :
Howard Hinnant7f764502013-08-14 18:00:20 +00001325 __align_it<sizeof(value_type) < __alignment ?
Howard Hinnanta6119a82011-05-29 19:57:12 +00001326 __alignment/sizeof(value_type) : 1 > (__s+1)) - 1;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001327
Eric Fiselier6dbed462016-09-16 00:00:48 +00001328 inline _LIBCPP_EXTERN_TEMPLATE_INLINE_VISIBILITY
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001329 void __init(const value_type* __s, size_type __sz, size_type __reserve);
Eric Fiselier6dbed462016-09-16 00:00:48 +00001330 inline _LIBCPP_EXTERN_TEMPLATE_INLINE_VISIBILITY
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001331 void __init(const value_type* __s, size_type __sz);
Eric Fiselier6dbed462016-09-16 00:00:48 +00001332 inline _LIBCPP_EXTERN_TEMPLATE_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001333 void __init(size_type __n, value_type __c);
Howard Hinnant324bb032010-08-22 00:02:43 +00001334
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001335 template <class _InputIterator>
Eric Fiselier6dbed462016-09-16 00:00:48 +00001336 inline _LIBCPP_EXTERN_TEMPLATE_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001337 typename enable_if
1338 <
Marshall Clowdf9db312016-01-13 21:54:34 +00001339 __is_exactly_input_iterator<_InputIterator>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001340 void
1341 >::type
1342 __init(_InputIterator __first, _InputIterator __last);
1343
1344 template <class _ForwardIterator>
Eric Fiselier6dbed462016-09-16 00:00:48 +00001345 inline _LIBCPP_EXTERN_TEMPLATE_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001346 typename enable_if
1347 <
1348 __is_forward_iterator<_ForwardIterator>::value,
1349 void
1350 >::type
1351 __init(_ForwardIterator __first, _ForwardIterator __last);
1352
1353 void __grow_by(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
Howard Hinnant324bb032010-08-22 00:02:43 +00001354 size_type __n_copy, size_type __n_del, size_type __n_add = 0);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001355 void __grow_by_and_replace(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
1356 size_type __n_copy, size_type __n_del,
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001357 size_type __n_add, const value_type* __p_new_stuff);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001358
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001359 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001360 void __erase_to_end(size_type __pos);
1361
Howard Hinnante32b5e22010-11-17 17:55:08 +00001362 _LIBCPP_INLINE_VISIBILITY
1363 void __copy_assign_alloc(const basic_string& __str)
1364 {__copy_assign_alloc(__str, integral_constant<bool,
1365 __alloc_traits::propagate_on_container_copy_assignment::value>());}
1366
1367 _LIBCPP_INLINE_VISIBILITY
1368 void __copy_assign_alloc(const basic_string& __str, true_type)
1369 {
1370 if (__alloc() != __str.__alloc())
1371 {
1372 clear();
1373 shrink_to_fit();
1374 }
1375 __alloc() = __str.__alloc();
1376 }
1377
1378 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantec3773c2011-12-01 20:21:04 +00001379 void __copy_assign_alloc(const basic_string&, false_type) _NOEXCEPT
Howard Hinnante32b5e22010-11-17 17:55:08 +00001380 {}
1381
1382#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001383 _LIBCPP_INLINE_VISIBILITY
Marshall Clowaf961ed2015-08-18 18:57:00 +00001384 void __move_assign(basic_string& __str, false_type)
1385 _NOEXCEPT_(__alloc_traits::is_always_equal::value);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001386 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant53f7d4c2011-06-03 18:40:47 +00001387 void __move_assign(basic_string& __str, true_type)
Marshall Clowaf961ed2015-08-18 18:57:00 +00001388#if _LIBCPP_STD_VER > 14
1389 _NOEXCEPT;
1390#else
Howard Hinnant53f7d4c2011-06-03 18:40:47 +00001391 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnante32b5e22010-11-17 17:55:08 +00001392#endif
Marshall Clowaf961ed2015-08-18 18:57:00 +00001393#endif
Howard Hinnante32b5e22010-11-17 17:55:08 +00001394
1395 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3fdbbd22011-08-17 20:36:18 +00001396 void
Howard Hinnant9cbee432011-09-02 20:42:31 +00001397 __move_assign_alloc(basic_string& __str)
Howard Hinnant3fdbbd22011-08-17 20:36:18 +00001398 _NOEXCEPT_(
1399 !__alloc_traits::propagate_on_container_move_assignment::value ||
1400 is_nothrow_move_assignable<allocator_type>::value)
1401 {__move_assign_alloc(__str, integral_constant<bool,
1402 __alloc_traits::propagate_on_container_move_assignment::value>());}
1403
1404 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9cbee432011-09-02 20:42:31 +00001405 void __move_assign_alloc(basic_string& __c, true_type)
Howard Hinnant3fdbbd22011-08-17 20:36:18 +00001406 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
1407 {
1408 __alloc() = _VSTD::move(__c.__alloc());
1409 }
1410
1411 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantec3773c2011-12-01 20:21:04 +00001412 void __move_assign_alloc(basic_string&, false_type)
Howard Hinnant3fdbbd22011-08-17 20:36:18 +00001413 _NOEXCEPT
1414 {}
1415
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001416 _LIBCPP_INLINE_VISIBILITY void __invalidate_all_iterators();
1417 _LIBCPP_INLINE_VISIBILITY void __invalidate_iterators_past(size_type);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001418
1419 friend basic_string operator+<>(const basic_string&, const basic_string&);
1420 friend basic_string operator+<>(const value_type*, const basic_string&);
1421 friend basic_string operator+<>(value_type, const basic_string&);
1422 friend basic_string operator+<>(const basic_string&, const value_type*);
1423 friend basic_string operator+<>(const basic_string&, value_type);
1424};
1425
1426template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001427inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001428void
1429basic_string<_CharT, _Traits, _Allocator>::__invalidate_all_iterators()
1430{
Howard Hinnant499cea12013-08-23 17:37:05 +00001431#if _LIBCPP_DEBUG_LEVEL >= 2
1432 __get_db()->__invalidate_all(this);
1433#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001434}
1435
1436template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001437inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001438void
Howard Hinnantec3773c2011-12-01 20:21:04 +00001439basic_string<_CharT, _Traits, _Allocator>::__invalidate_iterators_past(size_type
Howard Hinnant499cea12013-08-23 17:37:05 +00001440#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantec3773c2011-12-01 20:21:04 +00001441 __pos
1442#endif
1443 )
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001444{
Howard Hinnant499cea12013-08-23 17:37:05 +00001445#if _LIBCPP_DEBUG_LEVEL >= 2
1446 __c_node* __c = __get_db()->__find_c_and_lock(this);
1447 if (__c)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001448 {
Howard Hinnant499cea12013-08-23 17:37:05 +00001449 const_pointer __new_last = __get_pointer() + __pos;
1450 for (__i_node** __p = __c->end_; __p != __c->beg_; )
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001451 {
Howard Hinnant499cea12013-08-23 17:37:05 +00001452 --__p;
1453 const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_);
1454 if (__i->base() > __new_last)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001455 {
Howard Hinnant499cea12013-08-23 17:37:05 +00001456 (*__p)->__c_ = nullptr;
1457 if (--__c->end_ != __p)
1458 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001459 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001460 }
Howard Hinnant499cea12013-08-23 17:37:05 +00001461 __get_db()->unlock();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001462 }
Howard Hinnant499cea12013-08-23 17:37:05 +00001463#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001464}
1465
1466template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001467inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant53f7d4c2011-06-03 18:40:47 +00001468basic_string<_CharT, _Traits, _Allocator>::basic_string()
Marshall Clowc912c0c2015-06-04 02:05:41 +00001469 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001470{
Howard Hinnant499cea12013-08-23 17:37:05 +00001471#if _LIBCPP_DEBUG_LEVEL >= 2
1472 __get_db()->__insert_c(this);
1473#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001474 __zero();
1475}
1476
1477template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001478inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001479basic_string<_CharT, _Traits, _Allocator>::basic_string(const allocator_type& __a)
Eric Fiselier692177d2015-07-18 20:40:46 +00001480#if _LIBCPP_STD_VER <= 14
1481 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value)
1482#else
1483 _NOEXCEPT
1484#endif
1485: __r_(__a)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001486{
Howard Hinnant499cea12013-08-23 17:37:05 +00001487#if _LIBCPP_DEBUG_LEVEL >= 2
1488 __get_db()->__insert_c(this);
1489#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001490 __zero();
1491}
1492
1493template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier6dbed462016-09-16 00:00:48 +00001494void basic_string<_CharT, _Traits, _Allocator>::__init(const value_type* __s,
1495 size_type __sz,
1496 size_type __reserve)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001497{
1498 if (__reserve > max_size())
1499 this->__throw_length_error();
1500 pointer __p;
1501 if (__reserve < __min_cap)
1502 {
1503 __set_short_size(__sz);
1504 __p = __get_short_pointer();
1505 }
1506 else
1507 {
1508 size_type __cap = __recommend(__reserve);
Howard Hinnante32b5e22010-11-17 17:55:08 +00001509 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001510 __set_long_pointer(__p);
1511 __set_long_cap(__cap+1);
1512 __set_long_size(__sz);
1513 }
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001514 traits_type::copy(_VSTD::__to_raw_pointer(__p), __s, __sz);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001515 traits_type::assign(__p[__sz], value_type());
1516}
1517
1518template <class _CharT, class _Traits, class _Allocator>
1519void
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001520basic_string<_CharT, _Traits, _Allocator>::__init(const value_type* __s, size_type __sz)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001521{
1522 if (__sz > max_size())
1523 this->__throw_length_error();
1524 pointer __p;
1525 if (__sz < __min_cap)
1526 {
1527 __set_short_size(__sz);
1528 __p = __get_short_pointer();
1529 }
1530 else
1531 {
1532 size_type __cap = __recommend(__sz);
Howard Hinnante32b5e22010-11-17 17:55:08 +00001533 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001534 __set_long_pointer(__p);
1535 __set_long_cap(__cap+1);
1536 __set_long_size(__sz);
1537 }
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001538 traits_type::copy(_VSTD::__to_raw_pointer(__p), __s, __sz);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001539 traits_type::assign(__p[__sz], value_type());
1540}
1541
1542template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001543inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001544basic_string<_CharT, _Traits, _Allocator>::basic_string(const value_type* __s)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001545{
Howard Hinnant499cea12013-08-23 17:37:05 +00001546 _LIBCPP_ASSERT(__s != nullptr, "basic_string(const char*) detected nullptr");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001547 __init(__s, traits_type::length(__s));
Howard Hinnant499cea12013-08-23 17:37:05 +00001548#if _LIBCPP_DEBUG_LEVEL >= 2
1549 __get_db()->__insert_c(this);
1550#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001551}
1552
1553template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001554inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001555basic_string<_CharT, _Traits, _Allocator>::basic_string(const value_type* __s, const allocator_type& __a)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001556 : __r_(__a)
1557{
Howard Hinnant499cea12013-08-23 17:37:05 +00001558 _LIBCPP_ASSERT(__s != nullptr, "basic_string(const char*, allocator) detected nullptr");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001559 __init(__s, traits_type::length(__s));
Howard Hinnant499cea12013-08-23 17:37:05 +00001560#if _LIBCPP_DEBUG_LEVEL >= 2
1561 __get_db()->__insert_c(this);
1562#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001563}
1564
1565template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001566inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001567basic_string<_CharT, _Traits, _Allocator>::basic_string(const value_type* __s, size_type __n)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001568{
Howard Hinnant499cea12013-08-23 17:37:05 +00001569 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "basic_string(const char*, n) detected nullptr");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001570 __init(__s, __n);
Howard Hinnant499cea12013-08-23 17:37:05 +00001571#if _LIBCPP_DEBUG_LEVEL >= 2
1572 __get_db()->__insert_c(this);
1573#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001574}
1575
1576template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001577inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001578basic_string<_CharT, _Traits, _Allocator>::basic_string(const value_type* __s, size_type __n, const allocator_type& __a)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001579 : __r_(__a)
1580{
Howard Hinnant499cea12013-08-23 17:37:05 +00001581 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "basic_string(const char*, n, allocator) detected nullptr");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001582 __init(__s, __n);
Howard Hinnant499cea12013-08-23 17:37:05 +00001583#if _LIBCPP_DEBUG_LEVEL >= 2
1584 __get_db()->__insert_c(this);
1585#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001586}
1587
1588template <class _CharT, class _Traits, class _Allocator>
1589basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str)
Howard Hinnante32b5e22010-11-17 17:55:08 +00001590 : __r_(__alloc_traits::select_on_container_copy_construction(__str.__alloc()))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001591{
1592 if (!__str.__is_long())
1593 __r_.first().__r = __str.__r_.first().__r;
1594 else
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001595 __init(_VSTD::__to_raw_pointer(__str.__get_long_pointer()), __str.__get_long_size());
Howard Hinnant499cea12013-08-23 17:37:05 +00001596#if _LIBCPP_DEBUG_LEVEL >= 2
1597 __get_db()->__insert_c(this);
1598#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001599}
1600
1601template <class _CharT, class _Traits, class _Allocator>
1602basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str, const allocator_type& __a)
1603 : __r_(__a)
1604{
1605 if (!__str.__is_long())
1606 __r_.first().__r = __str.__r_.first().__r;
1607 else
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001608 __init(_VSTD::__to_raw_pointer(__str.__get_long_pointer()), __str.__get_long_size());
Howard Hinnant499cea12013-08-23 17:37:05 +00001609#if _LIBCPP_DEBUG_LEVEL >= 2
1610 __get_db()->__insert_c(this);
1611#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001612}
1613
Howard Hinnant73d21a42010-09-04 23:28:19 +00001614#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001615
1616template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001617inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant53f7d4c2011-06-03 18:40:47 +00001618basic_string<_CharT, _Traits, _Allocator>::basic_string(basic_string&& __str)
Marshall Clow7b193f72015-06-03 19:56:43 +00001619#if _LIBCPP_STD_VER <= 14
Howard Hinnant53f7d4c2011-06-03 18:40:47 +00001620 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Marshall Clow7b193f72015-06-03 19:56:43 +00001621#else
1622 _NOEXCEPT
1623#endif
Howard Hinnant0949eed2011-06-30 21:18:19 +00001624 : __r_(_VSTD::move(__str.__r_))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001625{
1626 __str.__zero();
Howard Hinnant499cea12013-08-23 17:37:05 +00001627#if _LIBCPP_DEBUG_LEVEL >= 2
1628 __get_db()->__insert_c(this);
1629 if (__is_long())
1630 __get_db()->swap(this, &__str);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001631#endif
1632}
1633
1634template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001635inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001636basic_string<_CharT, _Traits, _Allocator>::basic_string(basic_string&& __str, const allocator_type& __a)
Howard Hinnante32b5e22010-11-17 17:55:08 +00001637 : __r_(__a)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001638{
Marshall Clowd5549cc2014-07-17 15:32:20 +00001639 if (__str.__is_long() && __a != __str.__alloc()) // copy, not move
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001640 __init(_VSTD::__to_raw_pointer(__str.__get_long_pointer()), __str.__get_long_size());
Marshall Clowd5549cc2014-07-17 15:32:20 +00001641 else
1642 {
1643 __r_.first().__r = __str.__r_.first().__r;
1644 __str.__zero();
1645 }
Howard Hinnant499cea12013-08-23 17:37:05 +00001646#if _LIBCPP_DEBUG_LEVEL >= 2
1647 __get_db()->__insert_c(this);
1648 if (__is_long())
1649 __get_db()->swap(this, &__str);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001650#endif
1651}
1652
Howard Hinnant73d21a42010-09-04 23:28:19 +00001653#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001654
1655template <class _CharT, class _Traits, class _Allocator>
1656void
1657basic_string<_CharT, _Traits, _Allocator>::__init(size_type __n, value_type __c)
1658{
1659 if (__n > max_size())
1660 this->__throw_length_error();
1661 pointer __p;
1662 if (__n < __min_cap)
1663 {
1664 __set_short_size(__n);
1665 __p = __get_short_pointer();
1666 }
1667 else
1668 {
1669 size_type __cap = __recommend(__n);
Howard Hinnante32b5e22010-11-17 17:55:08 +00001670 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001671 __set_long_pointer(__p);
1672 __set_long_cap(__cap+1);
1673 __set_long_size(__n);
1674 }
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001675 traits_type::assign(_VSTD::__to_raw_pointer(__p), __n, __c);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001676 traits_type::assign(__p[__n], value_type());
1677}
1678
1679template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001680inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001681basic_string<_CharT, _Traits, _Allocator>::basic_string(size_type __n, value_type __c)
1682{
1683 __init(__n, __c);
Howard Hinnant499cea12013-08-23 17:37:05 +00001684#if _LIBCPP_DEBUG_LEVEL >= 2
1685 __get_db()->__insert_c(this);
1686#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001687}
1688
1689template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001690inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001691basic_string<_CharT, _Traits, _Allocator>::basic_string(size_type __n, value_type __c, const allocator_type& __a)
1692 : __r_(__a)
1693{
1694 __init(__n, __c);
Howard Hinnant499cea12013-08-23 17:37:05 +00001695#if _LIBCPP_DEBUG_LEVEL >= 2
1696 __get_db()->__insert_c(this);
1697#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001698}
1699
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001700template <class _CharT, class _Traits, class _Allocator>
1701basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str, size_type __pos, size_type __n,
1702 const allocator_type& __a)
1703 : __r_(__a)
1704{
1705 size_type __str_sz = __str.size();
1706 if (__pos > __str_sz)
1707 this->__throw_out_of_range();
Howard Hinnant0949eed2011-06-30 21:18:19 +00001708 __init(__str.data() + __pos, _VSTD::min(__n, __str_sz - __pos));
Howard Hinnant499cea12013-08-23 17:37:05 +00001709#if _LIBCPP_DEBUG_LEVEL >= 2
1710 __get_db()->__insert_c(this);
1711#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001712}
1713
1714template <class _CharT, class _Traits, class _Allocator>
Marshall Clowf4f7d8f2016-04-07 18:13:41 +00001715inline _LIBCPP_INLINE_VISIBILITY
1716basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str, size_type __pos,
1717 const allocator_type& __a)
1718 : __r_(__a)
1719{
1720 size_type __str_sz = __str.size();
1721 if (__pos > __str_sz)
1722 this->__throw_out_of_range();
1723 __init(__str.data() + __pos, __str_sz - __pos);
1724#if _LIBCPP_DEBUG_LEVEL >= 2
1725 __get_db()->__insert_c(this);
1726#endif
1727}
1728
1729template <class _CharT, class _Traits, class _Allocator>
Marshall Clowdb7fa112016-11-14 18:22:19 +00001730template <class _Tp>
1731basic_string<_CharT, _Traits, _Allocator>::basic_string(
1732 const _Tp& __t, size_type __pos, size_type __n, const allocator_type& __a,
1733 typename enable_if<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value, void>::type *)
1734 : __r_(__a)
1735{
1736 __self_view __sv = __self_view(__t).substr(__pos, __n);
1737 __init(__sv.data(), __sv.size());
1738#if _LIBCPP_DEBUG_LEVEL >= 2
1739 __get_db()->__insert_c(this);
1740#endif
1741}
1742
1743template <class _CharT, class _Traits, class _Allocator>
Marshall Clow1e00d6d2016-07-21 05:31:24 +00001744inline _LIBCPP_INLINE_VISIBILITY
1745basic_string<_CharT, _Traits, _Allocator>::basic_string(__self_view __sv)
1746{
1747 __init(__sv.data(), __sv.size());
1748#if _LIBCPP_DEBUG_LEVEL >= 2
1749 __get_db()->__insert_c(this);
1750#endif
1751}
1752
1753template <class _CharT, class _Traits, class _Allocator>
1754inline _LIBCPP_INLINE_VISIBILITY
1755basic_string<_CharT, _Traits, _Allocator>::basic_string(__self_view __sv, const allocator_type& __a)
1756 : __r_(__a)
1757{
1758 __init(__sv.data(), __sv.size());
1759#if _LIBCPP_DEBUG_LEVEL >= 2
1760 __get_db()->__insert_c(this);
1761#endif
1762}
1763
1764template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001765template <class _InputIterator>
1766typename enable_if
1767<
Marshall Clowdf9db312016-01-13 21:54:34 +00001768 __is_exactly_input_iterator<_InputIterator>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001769 void
1770>::type
1771basic_string<_CharT, _Traits, _Allocator>::__init(_InputIterator __first, _InputIterator __last)
1772{
1773 __zero();
1774#ifndef _LIBCPP_NO_EXCEPTIONS
1775 try
1776 {
Howard Hinnant324bb032010-08-22 00:02:43 +00001777#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001778 for (; __first != __last; ++__first)
1779 push_back(*__first);
1780#ifndef _LIBCPP_NO_EXCEPTIONS
1781 }
1782 catch (...)
1783 {
1784 if (__is_long())
Howard Hinnante32b5e22010-11-17 17:55:08 +00001785 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001786 throw;
1787 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001788#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001789}
1790
1791template <class _CharT, class _Traits, class _Allocator>
1792template <class _ForwardIterator>
1793typename enable_if
1794<
1795 __is_forward_iterator<_ForwardIterator>::value,
1796 void
1797>::type
1798basic_string<_CharT, _Traits, _Allocator>::__init(_ForwardIterator __first, _ForwardIterator __last)
1799{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001800 size_type __sz = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001801 if (__sz > max_size())
1802 this->__throw_length_error();
1803 pointer __p;
1804 if (__sz < __min_cap)
1805 {
1806 __set_short_size(__sz);
1807 __p = __get_short_pointer();
1808 }
1809 else
1810 {
1811 size_type __cap = __recommend(__sz);
Howard Hinnante32b5e22010-11-17 17:55:08 +00001812 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001813 __set_long_pointer(__p);
1814 __set_long_cap(__cap+1);
1815 __set_long_size(__sz);
1816 }
Eric Fiselierb9919752014-10-27 19:28:20 +00001817 for (; __first != __last; ++__first, (void) ++__p)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001818 traits_type::assign(*__p, *__first);
1819 traits_type::assign(*__p, value_type());
1820}
1821
1822template <class _CharT, class _Traits, class _Allocator>
1823template<class _InputIterator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001824inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001825basic_string<_CharT, _Traits, _Allocator>::basic_string(_InputIterator __first, _InputIterator __last)
1826{
1827 __init(__first, __last);
Howard Hinnant499cea12013-08-23 17:37:05 +00001828#if _LIBCPP_DEBUG_LEVEL >= 2
1829 __get_db()->__insert_c(this);
1830#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001831}
1832
1833template <class _CharT, class _Traits, class _Allocator>
1834template<class _InputIterator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001835inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001836basic_string<_CharT, _Traits, _Allocator>::basic_string(_InputIterator __first, _InputIterator __last,
1837 const allocator_type& __a)
1838 : __r_(__a)
1839{
1840 __init(__first, __last);
Howard Hinnant499cea12013-08-23 17:37:05 +00001841#if _LIBCPP_DEBUG_LEVEL >= 2
1842 __get_db()->__insert_c(this);
1843#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001844}
1845
Howard Hinnante3e32912011-08-12 21:56:02 +00001846#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
1847
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001848template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001849inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001850basic_string<_CharT, _Traits, _Allocator>::basic_string(initializer_list<value_type> __il)
1851{
1852 __init(__il.begin(), __il.end());
Howard Hinnant499cea12013-08-23 17:37:05 +00001853#if _LIBCPP_DEBUG_LEVEL >= 2
1854 __get_db()->__insert_c(this);
1855#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001856}
1857
1858template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001859inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001860basic_string<_CharT, _Traits, _Allocator>::basic_string(initializer_list<value_type> __il, const allocator_type& __a)
1861 : __r_(__a)
1862{
1863 __init(__il.begin(), __il.end());
Howard Hinnant499cea12013-08-23 17:37:05 +00001864#if _LIBCPP_DEBUG_LEVEL >= 2
1865 __get_db()->__insert_c(this);
1866#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001867}
1868
Howard Hinnante3e32912011-08-12 21:56:02 +00001869#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
1870
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001871template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001872basic_string<_CharT, _Traits, _Allocator>::~basic_string()
1873{
Howard Hinnant499cea12013-08-23 17:37:05 +00001874#if _LIBCPP_DEBUG_LEVEL >= 2
1875 __get_db()->__erase_c(this);
1876#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001877 if (__is_long())
Howard Hinnante32b5e22010-11-17 17:55:08 +00001878 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001879}
1880
1881template <class _CharT, class _Traits, class _Allocator>
1882void
1883basic_string<_CharT, _Traits, _Allocator>::__grow_by_and_replace
1884 (size_type __old_cap, size_type __delta_cap, size_type __old_sz,
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001885 size_type __n_copy, size_type __n_del, size_type __n_add, const value_type* __p_new_stuff)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001886{
1887 size_type __ms = max_size();
1888 if (__delta_cap > __ms - __old_cap - 1)
1889 this->__throw_length_error();
1890 pointer __old_p = __get_pointer();
1891 size_type __cap = __old_cap < __ms / 2 - __alignment ?
Howard Hinnant0949eed2011-06-30 21:18:19 +00001892 __recommend(_VSTD::max(__old_cap + __delta_cap, 2 * __old_cap)) :
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001893 __ms - 1;
Howard Hinnante32b5e22010-11-17 17:55:08 +00001894 pointer __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001895 __invalidate_all_iterators();
1896 if (__n_copy != 0)
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001897 traits_type::copy(_VSTD::__to_raw_pointer(__p),
1898 _VSTD::__to_raw_pointer(__old_p), __n_copy);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001899 if (__n_add != 0)
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001900 traits_type::copy(_VSTD::__to_raw_pointer(__p) + __n_copy, __p_new_stuff, __n_add);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001901 size_type __sec_cp_sz = __old_sz - __n_del - __n_copy;
1902 if (__sec_cp_sz != 0)
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001903 traits_type::copy(_VSTD::__to_raw_pointer(__p) + __n_copy + __n_add,
1904 _VSTD::__to_raw_pointer(__old_p) + __n_copy + __n_del, __sec_cp_sz);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001905 if (__old_cap+1 != __min_cap)
Howard Hinnante32b5e22010-11-17 17:55:08 +00001906 __alloc_traits::deallocate(__alloc(), __old_p, __old_cap+1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001907 __set_long_pointer(__p);
1908 __set_long_cap(__cap+1);
1909 __old_sz = __n_copy + __n_add + __sec_cp_sz;
1910 __set_long_size(__old_sz);
1911 traits_type::assign(__p[__old_sz], value_type());
1912}
1913
1914template <class _CharT, class _Traits, class _Allocator>
1915void
1916basic_string<_CharT, _Traits, _Allocator>::__grow_by(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
1917 size_type __n_copy, size_type __n_del, size_type __n_add)
1918{
1919 size_type __ms = max_size();
Marshall Clowecc8d7b2013-11-06 14:24:38 +00001920 if (__delta_cap > __ms - __old_cap)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001921 this->__throw_length_error();
1922 pointer __old_p = __get_pointer();
1923 size_type __cap = __old_cap < __ms / 2 - __alignment ?
Howard Hinnant0949eed2011-06-30 21:18:19 +00001924 __recommend(_VSTD::max(__old_cap + __delta_cap, 2 * __old_cap)) :
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001925 __ms - 1;
Howard Hinnante32b5e22010-11-17 17:55:08 +00001926 pointer __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001927 __invalidate_all_iterators();
1928 if (__n_copy != 0)
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001929 traits_type::copy(_VSTD::__to_raw_pointer(__p),
1930 _VSTD::__to_raw_pointer(__old_p), __n_copy);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001931 size_type __sec_cp_sz = __old_sz - __n_del - __n_copy;
1932 if (__sec_cp_sz != 0)
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001933 traits_type::copy(_VSTD::__to_raw_pointer(__p) + __n_copy + __n_add,
1934 _VSTD::__to_raw_pointer(__old_p) + __n_copy + __n_del,
1935 __sec_cp_sz);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001936 if (__old_cap+1 != __min_cap)
Howard Hinnante32b5e22010-11-17 17:55:08 +00001937 __alloc_traits::deallocate(__alloc(), __old_p, __old_cap+1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001938 __set_long_pointer(__p);
1939 __set_long_cap(__cap+1);
1940}
1941
1942// assign
1943
1944template <class _CharT, class _Traits, class _Allocator>
1945basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001946basic_string<_CharT, _Traits, _Allocator>::assign(const value_type* __s, size_type __n)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001947{
Alp Tokerec34c482014-05-15 11:27:39 +00001948 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::assign received nullptr");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001949 size_type __cap = capacity();
1950 if (__cap >= __n)
1951 {
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001952 value_type* __p = _VSTD::__to_raw_pointer(__get_pointer());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001953 traits_type::move(__p, __s, __n);
1954 traits_type::assign(__p[__n], value_type());
1955 __set_size(__n);
1956 __invalidate_iterators_past(__n);
1957 }
1958 else
1959 {
1960 size_type __sz = size();
1961 __grow_by_and_replace(__cap, __n - __cap, __sz, 0, __sz, __n, __s);
1962 }
1963 return *this;
1964}
1965
1966template <class _CharT, class _Traits, class _Allocator>
1967basic_string<_CharT, _Traits, _Allocator>&
1968basic_string<_CharT, _Traits, _Allocator>::assign(size_type __n, value_type __c)
1969{
1970 size_type __cap = capacity();
1971 if (__cap < __n)
1972 {
1973 size_type __sz = size();
1974 __grow_by(__cap, __n - __cap, __sz, 0, __sz);
1975 }
1976 else
1977 __invalidate_iterators_past(__n);
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001978 value_type* __p = _VSTD::__to_raw_pointer(__get_pointer());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001979 traits_type::assign(__p, __n, __c);
1980 traits_type::assign(__p[__n], value_type());
1981 __set_size(__n);
1982 return *this;
1983}
1984
1985template <class _CharT, class _Traits, class _Allocator>
1986basic_string<_CharT, _Traits, _Allocator>&
1987basic_string<_CharT, _Traits, _Allocator>::operator=(value_type __c)
1988{
1989 pointer __p;
1990 if (__is_long())
1991 {
1992 __p = __get_long_pointer();
1993 __set_long_size(1);
1994 }
1995 else
1996 {
1997 __p = __get_short_pointer();
1998 __set_short_size(1);
1999 }
2000 traits_type::assign(*__p, __c);
2001 traits_type::assign(*++__p, value_type());
2002 __invalidate_iterators_past(1);
2003 return *this;
2004}
2005
2006template <class _CharT, class _Traits, class _Allocator>
Howard Hinnante32b5e22010-11-17 17:55:08 +00002007basic_string<_CharT, _Traits, _Allocator>&
2008basic_string<_CharT, _Traits, _Allocator>::operator=(const basic_string& __str)
2009{
2010 if (this != &__str)
2011 {
2012 __copy_assign_alloc(__str);
Marshall Clowf40ec902016-03-09 18:08:29 +00002013 assign(__str.data(), __str.size());
Howard Hinnante32b5e22010-11-17 17:55:08 +00002014 }
2015 return *this;
2016}
2017
2018#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
2019
2020template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002021inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnante32b5e22010-11-17 17:55:08 +00002022void
2023basic_string<_CharT, _Traits, _Allocator>::__move_assign(basic_string& __str, false_type)
Marshall Clowaf961ed2015-08-18 18:57:00 +00002024 _NOEXCEPT_(__alloc_traits::is_always_equal::value)
Howard Hinnante32b5e22010-11-17 17:55:08 +00002025{
2026 if (__alloc() != __str.__alloc())
2027 assign(__str);
2028 else
2029 __move_assign(__str, true_type());
2030}
2031
2032template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002033inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnante32b5e22010-11-17 17:55:08 +00002034void
2035basic_string<_CharT, _Traits, _Allocator>::__move_assign(basic_string& __str, true_type)
Marshall Clowaf961ed2015-08-18 18:57:00 +00002036#if _LIBCPP_STD_VER > 14
2037 _NOEXCEPT
2038#else
Howard Hinnant53f7d4c2011-06-03 18:40:47 +00002039 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Marshall Clowaf961ed2015-08-18 18:57:00 +00002040#endif
Howard Hinnante32b5e22010-11-17 17:55:08 +00002041{
2042 clear();
2043 shrink_to_fit();
Howard Hinnant3fdbbd22011-08-17 20:36:18 +00002044 __r_.first() = __str.__r_.first();
2045 __move_assign_alloc(__str);
Howard Hinnante32b5e22010-11-17 17:55:08 +00002046 __str.__zero();
2047}
2048
2049template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002050inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnante32b5e22010-11-17 17:55:08 +00002051basic_string<_CharT, _Traits, _Allocator>&
2052basic_string<_CharT, _Traits, _Allocator>::operator=(basic_string&& __str)
Marshall Clowaf961ed2015-08-18 18:57:00 +00002053 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
Howard Hinnante32b5e22010-11-17 17:55:08 +00002054{
2055 __move_assign(__str, integral_constant<bool,
2056 __alloc_traits::propagate_on_container_move_assignment::value>());
2057 return *this;
2058}
2059
2060#endif
2061
2062template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002063template<class _InputIterator>
2064typename enable_if
2065<
Marshall Clowdf9db312016-01-13 21:54:34 +00002066 __is_exactly_input_iterator <_InputIterator>::value
2067 || !__libcpp_string_gets_noexcept_iterator<_InputIterator>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002068 basic_string<_CharT, _Traits, _Allocator>&
2069>::type
2070basic_string<_CharT, _Traits, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
2071{
Marshall Clowd979eed2016-09-05 01:54:30 +00002072 const basic_string __temp(__first, __last, __alloc());
Marshall Clowdf9db312016-01-13 21:54:34 +00002073 assign(__temp.data(), __temp.size());
Argyrios Kyrtzidis1dc6f7a2012-10-13 02:03:45 +00002074 return *this;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002075}
2076
2077template <class _CharT, class _Traits, class _Allocator>
2078template<class _ForwardIterator>
2079typename enable_if
2080<
Marshall Clowdf9db312016-01-13 21:54:34 +00002081 __is_forward_iterator<_ForwardIterator>::value
2082 && __libcpp_string_gets_noexcept_iterator<_ForwardIterator>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002083 basic_string<_CharT, _Traits, _Allocator>&
2084>::type
2085basic_string<_CharT, _Traits, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
2086{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002087 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002088 size_type __cap = capacity();
2089 if (__cap < __n)
2090 {
2091 size_type __sz = size();
2092 __grow_by(__cap, __n - __cap, __sz, 0, __sz);
2093 }
2094 else
2095 __invalidate_iterators_past(__n);
2096 pointer __p = __get_pointer();
2097 for (; __first != __last; ++__first, ++__p)
2098 traits_type::assign(*__p, *__first);
2099 traits_type::assign(*__p, value_type());
2100 __set_size(__n);
2101 return *this;
2102}
2103
2104template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002105basic_string<_CharT, _Traits, _Allocator>&
2106basic_string<_CharT, _Traits, _Allocator>::assign(const basic_string& __str, size_type __pos, size_type __n)
2107{
2108 size_type __sz = __str.size();
2109 if (__pos > __sz)
2110 this->__throw_out_of_range();
Howard Hinnant0949eed2011-06-30 21:18:19 +00002111 return assign(__str.data() + __pos, _VSTD::min(__n, __sz - __pos));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002112}
2113
2114template <class _CharT, class _Traits, class _Allocator>
Marshall Clow6ac8de02016-09-24 22:45:42 +00002115template <class _Tp>
2116typename enable_if
2117<
2118 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
2119 basic_string<_CharT, _Traits, _Allocator>&
2120>::type
2121basic_string<_CharT, _Traits, _Allocator>::assign(const _Tp & __t, size_type __pos, size_type __n)
Marshall Clow1e00d6d2016-07-21 05:31:24 +00002122{
Marshall Clow6ac8de02016-09-24 22:45:42 +00002123 __self_view __sv = __t;
Marshall Clow1e00d6d2016-07-21 05:31:24 +00002124 size_type __sz = __sv.size();
2125 if (__pos > __sz)
2126 this->__throw_out_of_range();
2127 return assign(__sv.data() + __pos, _VSTD::min(__n, __sz - __pos));
2128}
2129
2130
2131template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002132basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002133basic_string<_CharT, _Traits, _Allocator>::assign(const value_type* __s)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002134{
Alp Tokerec34c482014-05-15 11:27:39 +00002135 _LIBCPP_ASSERT(__s != nullptr, "string::assign received nullptr");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002136 return assign(__s, traits_type::length(__s));
2137}
2138
2139// append
2140
2141template <class _CharT, class _Traits, class _Allocator>
2142basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002143basic_string<_CharT, _Traits, _Allocator>::append(const value_type* __s, size_type __n)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002144{
Alp Tokerec34c482014-05-15 11:27:39 +00002145 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::append received nullptr");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002146 size_type __cap = capacity();
2147 size_type __sz = size();
2148 if (__cap - __sz >= __n)
2149 {
2150 if (__n)
2151 {
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002152 value_type* __p = _VSTD::__to_raw_pointer(__get_pointer());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002153 traits_type::copy(__p + __sz, __s, __n);
2154 __sz += __n;
2155 __set_size(__sz);
2156 traits_type::assign(__p[__sz], value_type());
2157 }
2158 }
2159 else
2160 __grow_by_and_replace(__cap, __sz + __n - __cap, __sz, __sz, 0, __n, __s);
2161 return *this;
2162}
2163
2164template <class _CharT, class _Traits, class _Allocator>
2165basic_string<_CharT, _Traits, _Allocator>&
2166basic_string<_CharT, _Traits, _Allocator>::append(size_type __n, value_type __c)
2167{
2168 if (__n)
2169 {
2170 size_type __cap = capacity();
2171 size_type __sz = size();
2172 if (__cap - __sz < __n)
2173 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2174 pointer __p = __get_pointer();
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002175 traits_type::assign(_VSTD::__to_raw_pointer(__p) + __sz, __n, __c);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002176 __sz += __n;
2177 __set_size(__sz);
2178 traits_type::assign(__p[__sz], value_type());
2179 }
2180 return *this;
2181}
2182
2183template <class _CharT, class _Traits, class _Allocator>
2184void
2185basic_string<_CharT, _Traits, _Allocator>::push_back(value_type __c)
2186{
Howard Hinnant15467182013-04-30 21:44:48 +00002187 bool __is_short = !__is_long();
2188 size_type __cap;
2189 size_type __sz;
2190 if (__is_short)
2191 {
2192 __cap = __min_cap - 1;
2193 __sz = __get_short_size();
2194 }
2195 else
2196 {
2197 __cap = __get_long_cap() - 1;
2198 __sz = __get_long_size();
2199 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002200 if (__sz == __cap)
Howard Hinnant15467182013-04-30 21:44:48 +00002201 {
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002202 __grow_by(__cap, 1, __sz, __sz, 0);
Howard Hinnant15467182013-04-30 21:44:48 +00002203 __is_short = !__is_long();
2204 }
2205 pointer __p;
2206 if (__is_short)
2207 {
2208 __p = __get_short_pointer() + __sz;
2209 __set_short_size(__sz+1);
2210 }
2211 else
2212 {
2213 __p = __get_long_pointer() + __sz;
2214 __set_long_size(__sz+1);
2215 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002216 traits_type::assign(*__p, __c);
2217 traits_type::assign(*++__p, value_type());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002218}
2219
Marshall Clowac655ef2016-09-07 03:32:06 +00002220template <class _Tp>
Marshall Clowd979eed2016-09-05 01:54:30 +00002221bool __ptr_in_range (const _Tp* __p, const _Tp* __first, const _Tp* __last)
2222{
2223 return __first <= __p && __p < __last;
2224}
2225
Marshall Clowac655ef2016-09-07 03:32:06 +00002226template <class _Tp1, class _Tp2>
Eric Fiselier0e5ebbc2016-12-23 23:37:52 +00002227bool __ptr_in_range (const _Tp1*, const _Tp2*, const _Tp2*)
Marshall Clowac655ef2016-09-07 03:32:06 +00002228{
2229 return false;
2230}
2231
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002232template <class _CharT, class _Traits, class _Allocator>
2233template<class _ForwardIterator>
Eric Fiselier026d38e2016-10-31 02:46:25 +00002234basic_string<_CharT, _Traits, _Allocator>&
2235basic_string<_CharT, _Traits, _Allocator>::__append_forward_unsafe(
2236 _ForwardIterator __first, _ForwardIterator __last)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002237{
Eric Fiselier026d38e2016-10-31 02:46:25 +00002238 static_assert(__is_forward_iterator<_ForwardIterator>::value,
2239 "function requires a ForwardIterator");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002240 size_type __sz = size();
2241 size_type __cap = capacity();
Howard Hinnant0949eed2011-06-30 21:18:19 +00002242 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002243 if (__n)
2244 {
Marshall Clowd979eed2016-09-05 01:54:30 +00002245 if ( __ptr_in_range(&*__first, data(), data() + size()))
2246 {
2247 const basic_string __temp (__first, __last, __alloc());
2248 append(__temp.data(), __temp.size());
2249 }
2250 else
2251 {
2252 if (__cap - __sz < __n)
2253 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2254 pointer __p = __get_pointer() + __sz;
2255 for (; __first != __last; ++__p, ++__first)
2256 traits_type::assign(*__p, *__first);
2257 traits_type::assign(*__p, value_type());
2258 __set_size(__sz + __n);
2259 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002260 }
2261 return *this;
2262}
2263
2264template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002265inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002266basic_string<_CharT, _Traits, _Allocator>&
2267basic_string<_CharT, _Traits, _Allocator>::append(const basic_string& __str)
2268{
2269 return append(__str.data(), __str.size());
2270}
2271
2272template <class _CharT, class _Traits, class _Allocator>
2273basic_string<_CharT, _Traits, _Allocator>&
2274basic_string<_CharT, _Traits, _Allocator>::append(const basic_string& __str, size_type __pos, size_type __n)
2275{
2276 size_type __sz = __str.size();
2277 if (__pos > __sz)
2278 this->__throw_out_of_range();
Howard Hinnant0949eed2011-06-30 21:18:19 +00002279 return append(__str.data() + __pos, _VSTD::min(__n, __sz - __pos));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002280}
2281
2282template <class _CharT, class _Traits, class _Allocator>
Marshall Clow42a87db2016-10-03 23:40:48 +00002283template <class _Tp>
Marshall Clow6ac8de02016-09-24 22:45:42 +00002284 typename enable_if
2285 <
2286 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
2287 basic_string<_CharT, _Traits, _Allocator>&
2288 >::type
2289basic_string<_CharT, _Traits, _Allocator>::append(const _Tp & __t, size_type __pos, size_type __n)
Marshall Clow1e00d6d2016-07-21 05:31:24 +00002290{
Marshall Clow6ac8de02016-09-24 22:45:42 +00002291 __self_view __sv = __t;
Marshall Clow1e00d6d2016-07-21 05:31:24 +00002292 size_type __sz = __sv.size();
2293 if (__pos > __sz)
2294 this->__throw_out_of_range();
2295 return append(__sv.data() + __pos, _VSTD::min(__n, __sz - __pos));
2296}
2297
2298template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002299basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002300basic_string<_CharT, _Traits, _Allocator>::append(const value_type* __s)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002301{
Alp Tokerec34c482014-05-15 11:27:39 +00002302 _LIBCPP_ASSERT(__s != nullptr, "string::append received nullptr");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002303 return append(__s, traits_type::length(__s));
2304}
2305
2306// insert
2307
2308template <class _CharT, class _Traits, class _Allocator>
2309basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002310basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, const value_type* __s, size_type __n)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002311{
Alp Tokerec34c482014-05-15 11:27:39 +00002312 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::insert received nullptr");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002313 size_type __sz = size();
2314 if (__pos > __sz)
2315 this->__throw_out_of_range();
2316 size_type __cap = capacity();
2317 if (__cap - __sz >= __n)
2318 {
2319 if (__n)
2320 {
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002321 value_type* __p = _VSTD::__to_raw_pointer(__get_pointer());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002322 size_type __n_move = __sz - __pos;
2323 if (__n_move != 0)
2324 {
2325 if (__p + __pos <= __s && __s < __p + __sz)
2326 __s += __n;
2327 traits_type::move(__p + __pos + __n, __p + __pos, __n_move);
2328 }
2329 traits_type::move(__p + __pos, __s, __n);
2330 __sz += __n;
2331 __set_size(__sz);
2332 traits_type::assign(__p[__sz], value_type());
2333 }
2334 }
2335 else
2336 __grow_by_and_replace(__cap, __sz + __n - __cap, __sz, __pos, 0, __n, __s);
2337 return *this;
2338}
2339
2340template <class _CharT, class _Traits, class _Allocator>
2341basic_string<_CharT, _Traits, _Allocator>&
2342basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, size_type __n, value_type __c)
2343{
2344 size_type __sz = size();
2345 if (__pos > __sz)
2346 this->__throw_out_of_range();
2347 if (__n)
2348 {
2349 size_type __cap = capacity();
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002350 value_type* __p;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002351 if (__cap - __sz >= __n)
2352 {
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002353 __p = _VSTD::__to_raw_pointer(__get_pointer());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002354 size_type __n_move = __sz - __pos;
2355 if (__n_move != 0)
2356 traits_type::move(__p + __pos + __n, __p + __pos, __n_move);
2357 }
2358 else
2359 {
2360 __grow_by(__cap, __sz + __n - __cap, __sz, __pos, 0, __n);
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002361 __p = _VSTD::__to_raw_pointer(__get_long_pointer());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002362 }
2363 traits_type::assign(__p + __pos, __n, __c);
2364 __sz += __n;
2365 __set_size(__sz);
2366 traits_type::assign(__p[__sz], value_type());
2367 }
2368 return *this;
2369}
2370
2371template <class _CharT, class _Traits, class _Allocator>
2372template<class _InputIterator>
2373typename enable_if
2374<
Marshall Clowdf9db312016-01-13 21:54:34 +00002375 __is_exactly_input_iterator<_InputIterator>::value
2376 || !__libcpp_string_gets_noexcept_iterator<_InputIterator>::value,
2377 typename basic_string<_CharT, _Traits, _Allocator>::iterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002378>::type
2379basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, _InputIterator __first, _InputIterator __last)
2380{
Howard Hinnant499cea12013-08-23 17:37:05 +00002381#if _LIBCPP_DEBUG_LEVEL >= 2
2382 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2383 "string::insert(iterator, range) called with an iterator not"
2384 " referring to this string");
2385#endif
Marshall Clowd979eed2016-09-05 01:54:30 +00002386 const basic_string __temp(__first, __last, __alloc());
Marshall Clowdf9db312016-01-13 21:54:34 +00002387 return insert(__pos, __temp.data(), __temp.data() + __temp.size());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002388}
2389
2390template <class _CharT, class _Traits, class _Allocator>
2391template<class _ForwardIterator>
2392typename enable_if
2393<
Marshall Clowdf9db312016-01-13 21:54:34 +00002394 __is_forward_iterator<_ForwardIterator>::value
2395 && __libcpp_string_gets_noexcept_iterator<_ForwardIterator>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002396 typename basic_string<_CharT, _Traits, _Allocator>::iterator
2397>::type
2398basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, _ForwardIterator __first, _ForwardIterator __last)
2399{
Howard Hinnant499cea12013-08-23 17:37:05 +00002400#if _LIBCPP_DEBUG_LEVEL >= 2
2401 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2402 "string::insert(iterator, range) called with an iterator not"
2403 " referring to this string");
2404#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002405 size_type __ip = static_cast<size_type>(__pos - begin());
Howard Hinnant0949eed2011-06-30 21:18:19 +00002406 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002407 if (__n)
2408 {
Marshall Clowd979eed2016-09-05 01:54:30 +00002409 if ( __ptr_in_range(&*__first, data(), data() + size()))
2410 {
2411 const basic_string __temp(__first, __last, __alloc());
2412 return insert(__pos, __temp.data(), __temp.data() + __temp.size());
2413 }
2414
2415 size_type __sz = size();
2416 size_type __cap = capacity();
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002417 value_type* __p;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002418 if (__cap - __sz >= __n)
2419 {
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002420 __p = _VSTD::__to_raw_pointer(__get_pointer());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002421 size_type __n_move = __sz - __ip;
2422 if (__n_move != 0)
2423 traits_type::move(__p + __ip + __n, __p + __ip, __n_move);
2424 }
2425 else
2426 {
2427 __grow_by(__cap, __sz + __n - __cap, __sz, __ip, 0, __n);
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002428 __p = _VSTD::__to_raw_pointer(__get_long_pointer());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002429 }
2430 __sz += __n;
2431 __set_size(__sz);
2432 traits_type::assign(__p[__sz], value_type());
2433 for (__p += __ip; __first != __last; ++__p, ++__first)
2434 traits_type::assign(*__p, *__first);
2435 }
2436 return begin() + __ip;
2437}
2438
2439template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002440inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002441basic_string<_CharT, _Traits, _Allocator>&
2442basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const basic_string& __str)
2443{
2444 return insert(__pos1, __str.data(), __str.size());
2445}
2446
2447template <class _CharT, class _Traits, class _Allocator>
2448basic_string<_CharT, _Traits, _Allocator>&
2449basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const basic_string& __str,
2450 size_type __pos2, size_type __n)
2451{
2452 size_type __str_sz = __str.size();
2453 if (__pos2 > __str_sz)
2454 this->__throw_out_of_range();
Howard Hinnant0949eed2011-06-30 21:18:19 +00002455 return insert(__pos1, __str.data() + __pos2, _VSTD::min(__n, __str_sz - __pos2));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002456}
2457
2458template <class _CharT, class _Traits, class _Allocator>
Marshall Clow6ac8de02016-09-24 22:45:42 +00002459template <class _Tp>
2460typename enable_if
2461<
2462 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
2463 basic_string<_CharT, _Traits, _Allocator>&
2464>::type
2465basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const _Tp& __t,
Marshall Clow1e00d6d2016-07-21 05:31:24 +00002466 size_type __pos2, size_type __n)
2467{
Marshall Clow6ac8de02016-09-24 22:45:42 +00002468 __self_view __sv = __t;
Marshall Clow1e00d6d2016-07-21 05:31:24 +00002469 size_type __str_sz = __sv.size();
2470 if (__pos2 > __str_sz)
2471 this->__throw_out_of_range();
2472 return insert(__pos1, __sv.data() + __pos2, _VSTD::min(__n, __str_sz - __pos2));
2473}
2474
2475template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002476basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002477basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, const value_type* __s)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002478{
Alp Tokerec34c482014-05-15 11:27:39 +00002479 _LIBCPP_ASSERT(__s != nullptr, "string::insert received nullptr");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002480 return insert(__pos, __s, traits_type::length(__s));
2481}
2482
2483template <class _CharT, class _Traits, class _Allocator>
2484typename basic_string<_CharT, _Traits, _Allocator>::iterator
2485basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, value_type __c)
2486{
2487 size_type __ip = static_cast<size_type>(__pos - begin());
2488 size_type __sz = size();
2489 size_type __cap = capacity();
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002490 value_type* __p;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002491 if (__cap == __sz)
2492 {
2493 __grow_by(__cap, 1, __sz, __ip, 0, 1);
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002494 __p = _VSTD::__to_raw_pointer(__get_long_pointer());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002495 }
2496 else
2497 {
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002498 __p = _VSTD::__to_raw_pointer(__get_pointer());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002499 size_type __n_move = __sz - __ip;
2500 if (__n_move != 0)
2501 traits_type::move(__p + __ip + 1, __p + __ip, __n_move);
2502 }
2503 traits_type::assign(__p[__ip], __c);
2504 traits_type::assign(__p[++__sz], value_type());
2505 __set_size(__sz);
2506 return begin() + static_cast<difference_type>(__ip);
2507}
2508
2509template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002510inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002511typename basic_string<_CharT, _Traits, _Allocator>::iterator
2512basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, size_type __n, value_type __c)
2513{
Howard Hinnant499cea12013-08-23 17:37:05 +00002514#if _LIBCPP_DEBUG_LEVEL >= 2
2515 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2516 "string::insert(iterator, n, value) called with an iterator not"
2517 " referring to this string");
2518#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002519 difference_type __p = __pos - begin();
2520 insert(static_cast<size_type>(__p), __n, __c);
2521 return begin() + __p;
2522}
2523
2524// replace
2525
2526template <class _CharT, class _Traits, class _Allocator>
2527basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002528basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos, size_type __n1, const value_type* __s, size_type __n2)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002529{
Alp Tokerec34c482014-05-15 11:27:39 +00002530 _LIBCPP_ASSERT(__n2 == 0 || __s != nullptr, "string::replace received nullptr");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002531 size_type __sz = size();
2532 if (__pos > __sz)
2533 this->__throw_out_of_range();
Howard Hinnant0949eed2011-06-30 21:18:19 +00002534 __n1 = _VSTD::min(__n1, __sz - __pos);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002535 size_type __cap = capacity();
2536 if (__cap - __sz + __n1 >= __n2)
2537 {
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002538 value_type* __p = _VSTD::__to_raw_pointer(__get_pointer());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002539 if (__n1 != __n2)
2540 {
2541 size_type __n_move = __sz - __pos - __n1;
2542 if (__n_move != 0)
2543 {
2544 if (__n1 > __n2)
2545 {
2546 traits_type::move(__p + __pos, __s, __n2);
2547 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
2548 goto __finish;
2549 }
2550 if (__p + __pos < __s && __s < __p + __sz)
2551 {
2552 if (__p + __pos + __n1 <= __s)
2553 __s += __n2 - __n1;
2554 else // __p + __pos < __s < __p + __pos + __n1
2555 {
2556 traits_type::move(__p + __pos, __s, __n1);
2557 __pos += __n1;
2558 __s += __n2;
2559 __n2 -= __n1;
2560 __n1 = 0;
2561 }
2562 }
2563 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
2564 }
2565 }
2566 traits_type::move(__p + __pos, __s, __n2);
2567__finish:
2568 __sz += __n2 - __n1;
2569 __set_size(__sz);
2570 __invalidate_iterators_past(__sz);
2571 traits_type::assign(__p[__sz], value_type());
2572 }
2573 else
2574 __grow_by_and_replace(__cap, __sz - __n1 + __n2 - __cap, __sz, __pos, __n1, __n2, __s);
2575 return *this;
2576}
2577
2578template <class _CharT, class _Traits, class _Allocator>
2579basic_string<_CharT, _Traits, _Allocator>&
2580basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos, size_type __n1, size_type __n2, value_type __c)
2581{
2582 size_type __sz = size();
2583 if (__pos > __sz)
2584 this->__throw_out_of_range();
Howard Hinnant0949eed2011-06-30 21:18:19 +00002585 __n1 = _VSTD::min(__n1, __sz - __pos);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002586 size_type __cap = capacity();
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002587 value_type* __p;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002588 if (__cap - __sz + __n1 >= __n2)
2589 {
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002590 __p = _VSTD::__to_raw_pointer(__get_pointer());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002591 if (__n1 != __n2)
2592 {
2593 size_type __n_move = __sz - __pos - __n1;
2594 if (__n_move != 0)
2595 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
2596 }
2597 }
2598 else
2599 {
2600 __grow_by(__cap, __sz - __n1 + __n2 - __cap, __sz, __pos, __n1, __n2);
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002601 __p = _VSTD::__to_raw_pointer(__get_long_pointer());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002602 }
2603 traits_type::assign(__p + __pos, __n2, __c);
2604 __sz += __n2 - __n1;
2605 __set_size(__sz);
2606 __invalidate_iterators_past(__sz);
2607 traits_type::assign(__p[__sz], value_type());
2608 return *this;
2609}
2610
2611template <class _CharT, class _Traits, class _Allocator>
2612template<class _InputIterator>
2613typename enable_if
2614<
2615 __is_input_iterator<_InputIterator>::value,
2616 basic_string<_CharT, _Traits, _Allocator>&
2617>::type
Howard Hinnant7b2cb482010-11-17 21:11:40 +00002618basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002619 _InputIterator __j1, _InputIterator __j2)
2620{
Marshall Clowd979eed2016-09-05 01:54:30 +00002621 const basic_string __temp(__j1, __j2, __alloc());
Marshall Clowdf9db312016-01-13 21:54:34 +00002622 return this->replace(__i1, __i2, __temp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002623}
2624
2625template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002626inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002627basic_string<_CharT, _Traits, _Allocator>&
2628basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const basic_string& __str)
2629{
2630 return replace(__pos1, __n1, __str.data(), __str.size());
2631}
2632
2633template <class _CharT, class _Traits, class _Allocator>
2634basic_string<_CharT, _Traits, _Allocator>&
2635basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const basic_string& __str,
2636 size_type __pos2, size_type __n2)
2637{
2638 size_type __str_sz = __str.size();
2639 if (__pos2 > __str_sz)
2640 this->__throw_out_of_range();
Howard Hinnant0949eed2011-06-30 21:18:19 +00002641 return replace(__pos1, __n1, __str.data() + __pos2, _VSTD::min(__n2, __str_sz - __pos2));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002642}
2643
2644template <class _CharT, class _Traits, class _Allocator>
Marshall Clow6ac8de02016-09-24 22:45:42 +00002645template <class _Tp>
2646typename enable_if
2647<
2648 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
2649 basic_string<_CharT, _Traits, _Allocator>&
2650>::type
2651basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const _Tp& __t,
Marshall Clow1e00d6d2016-07-21 05:31:24 +00002652 size_type __pos2, size_type __n2)
2653{
Marshall Clow6ac8de02016-09-24 22:45:42 +00002654 __self_view __sv = __t;
Marshall Clow1e00d6d2016-07-21 05:31:24 +00002655 size_type __str_sz = __sv.size();
2656 if (__pos2 > __str_sz)
2657 this->__throw_out_of_range();
2658 return replace(__pos1, __n1, __sv.data() + __pos2, _VSTD::min(__n2, __str_sz - __pos2));
2659}
2660
2661template <class _CharT, class _Traits, class _Allocator>
2662basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002663basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos, size_type __n1, const value_type* __s)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002664{
Alp Tokerec34c482014-05-15 11:27:39 +00002665 _LIBCPP_ASSERT(__s != nullptr, "string::replace received nullptr");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002666 return replace(__pos, __n1, __s, traits_type::length(__s));
2667}
2668
2669template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002670inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002671basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant7b2cb482010-11-17 21:11:40 +00002672basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, const basic_string& __str)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002673{
2674 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1),
2675 __str.data(), __str.size());
2676}
2677
2678template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002679inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002680basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002681basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, const value_type* __s, size_type __n)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002682{
2683 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __s, __n);
2684}
2685
2686template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002687inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002688basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002689basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, const value_type* __s)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002690{
2691 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __s);
2692}
2693
2694template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002695inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002696basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant7b2cb482010-11-17 21:11:40 +00002697basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, size_type __n, value_type __c)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002698{
2699 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __n, __c);
2700}
2701
2702// erase
2703
2704template <class _CharT, class _Traits, class _Allocator>
2705basic_string<_CharT, _Traits, _Allocator>&
2706basic_string<_CharT, _Traits, _Allocator>::erase(size_type __pos, size_type __n)
2707{
2708 size_type __sz = size();
2709 if (__pos > __sz)
2710 this->__throw_out_of_range();
2711 if (__n)
2712 {
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002713 value_type* __p = _VSTD::__to_raw_pointer(__get_pointer());
Howard Hinnant0949eed2011-06-30 21:18:19 +00002714 __n = _VSTD::min(__n, __sz - __pos);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002715 size_type __n_move = __sz - __pos - __n;
2716 if (__n_move != 0)
2717 traits_type::move(__p + __pos, __p + __pos + __n, __n_move);
2718 __sz -= __n;
2719 __set_size(__sz);
2720 __invalidate_iterators_past(__sz);
2721 traits_type::assign(__p[__sz], value_type());
2722 }
2723 return *this;
2724}
2725
2726template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002727inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002728typename basic_string<_CharT, _Traits, _Allocator>::iterator
2729basic_string<_CharT, _Traits, _Allocator>::erase(const_iterator __pos)
2730{
Howard Hinnant499cea12013-08-23 17:37:05 +00002731#if _LIBCPP_DEBUG_LEVEL >= 2
2732 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2733 "string::erase(iterator) called with an iterator not"
2734 " referring to this string");
2735#endif
2736 _LIBCPP_ASSERT(__pos != end(),
2737 "string::erase(iterator) called with a non-dereferenceable iterator");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002738 iterator __b = begin();
2739 size_type __r = static_cast<size_type>(__pos - __b);
2740 erase(__r, 1);
Howard Hinnantec3773c2011-12-01 20:21:04 +00002741 return __b + static_cast<difference_type>(__r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002742}
2743
2744template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002745inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002746typename basic_string<_CharT, _Traits, _Allocator>::iterator
2747basic_string<_CharT, _Traits, _Allocator>::erase(const_iterator __first, const_iterator __last)
2748{
Howard Hinnant499cea12013-08-23 17:37:05 +00002749#if _LIBCPP_DEBUG_LEVEL >= 2
2750 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__first) == this,
2751 "string::erase(iterator, iterator) called with an iterator not"
2752 " referring to this string");
2753#endif
2754 _LIBCPP_ASSERT(__first <= __last, "string::erase(first, last) called with invalid range");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002755 iterator __b = begin();
2756 size_type __r = static_cast<size_type>(__first - __b);
2757 erase(__r, static_cast<size_type>(__last - __first));
Howard Hinnantec3773c2011-12-01 20:21:04 +00002758 return __b + static_cast<difference_type>(__r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002759}
2760
2761template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002762inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002763void
2764basic_string<_CharT, _Traits, _Allocator>::pop_back()
2765{
Howard Hinnant499cea12013-08-23 17:37:05 +00002766 _LIBCPP_ASSERT(!empty(), "string::pop_back(): string is already empty");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002767 size_type __sz;
2768 if (__is_long())
2769 {
2770 __sz = __get_long_size() - 1;
2771 __set_long_size(__sz);
2772 traits_type::assign(*(__get_long_pointer() + __sz), value_type());
2773 }
2774 else
2775 {
2776 __sz = __get_short_size() - 1;
2777 __set_short_size(__sz);
2778 traits_type::assign(*(__get_short_pointer() + __sz), value_type());
2779 }
2780 __invalidate_iterators_past(__sz);
2781}
2782
2783template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002784inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002785void
Howard Hinnanta6119a82011-05-29 19:57:12 +00002786basic_string<_CharT, _Traits, _Allocator>::clear() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002787{
2788 __invalidate_all_iterators();
2789 if (__is_long())
2790 {
2791 traits_type::assign(*__get_long_pointer(), value_type());
2792 __set_long_size(0);
2793 }
2794 else
2795 {
2796 traits_type::assign(*__get_short_pointer(), value_type());
2797 __set_short_size(0);
2798 }
2799}
2800
2801template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002802inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002803void
2804basic_string<_CharT, _Traits, _Allocator>::__erase_to_end(size_type __pos)
2805{
2806 if (__is_long())
2807 {
2808 traits_type::assign(*(__get_long_pointer() + __pos), value_type());
2809 __set_long_size(__pos);
2810 }
2811 else
2812 {
2813 traits_type::assign(*(__get_short_pointer() + __pos), value_type());
2814 __set_short_size(__pos);
2815 }
2816 __invalidate_iterators_past(__pos);
2817}
2818
2819template <class _CharT, class _Traits, class _Allocator>
2820void
2821basic_string<_CharT, _Traits, _Allocator>::resize(size_type __n, value_type __c)
2822{
2823 size_type __sz = size();
2824 if (__n > __sz)
2825 append(__n - __sz, __c);
2826 else
2827 __erase_to_end(__n);
2828}
2829
2830template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002831inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002832typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnanta6119a82011-05-29 19:57:12 +00002833basic_string<_CharT, _Traits, _Allocator>::max_size() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002834{
Howard Hinnante32b5e22010-11-17 17:55:08 +00002835 size_type __m = __alloc_traits::max_size(__alloc());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002836#if _LIBCPP_BIG_ENDIAN
Marshall Clow09f85502013-10-31 17:23:08 +00002837 return (__m <= ~__long_mask ? __m : __m/2) - __alignment;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002838#else
Marshall Clow09f85502013-10-31 17:23:08 +00002839 return __m - __alignment;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002840#endif
2841}
2842
2843template <class _CharT, class _Traits, class _Allocator>
2844void
2845basic_string<_CharT, _Traits, _Allocator>::reserve(size_type __res_arg)
2846{
2847 if (__res_arg > max_size())
2848 this->__throw_length_error();
2849 size_type __cap = capacity();
2850 size_type __sz = size();
Howard Hinnant0949eed2011-06-30 21:18:19 +00002851 __res_arg = _VSTD::max(__res_arg, __sz);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002852 __res_arg = __recommend(__res_arg);
2853 if (__res_arg != __cap)
2854 {
2855 pointer __new_data, __p;
2856 bool __was_long, __now_long;
2857 if (__res_arg == __min_cap - 1)
2858 {
2859 __was_long = true;
2860 __now_long = false;
2861 __new_data = __get_short_pointer();
2862 __p = __get_long_pointer();
2863 }
2864 else
2865 {
2866 if (__res_arg > __cap)
Howard Hinnante32b5e22010-11-17 17:55:08 +00002867 __new_data = __alloc_traits::allocate(__alloc(), __res_arg+1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002868 else
2869 {
2870 #ifndef _LIBCPP_NO_EXCEPTIONS
2871 try
2872 {
Howard Hinnant324bb032010-08-22 00:02:43 +00002873 #endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnante32b5e22010-11-17 17:55:08 +00002874 __new_data = __alloc_traits::allocate(__alloc(), __res_arg+1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002875 #ifndef _LIBCPP_NO_EXCEPTIONS
2876 }
2877 catch (...)
2878 {
2879 return;
2880 }
Howard Hinnant324bb032010-08-22 00:02:43 +00002881 #else // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002882 if (__new_data == nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002883 return;
Howard Hinnant324bb032010-08-22 00:02:43 +00002884 #endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002885 }
2886 __now_long = true;
2887 __was_long = __is_long();
2888 __p = __get_pointer();
2889 }
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002890 traits_type::copy(_VSTD::__to_raw_pointer(__new_data),
2891 _VSTD::__to_raw_pointer(__p), size()+1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002892 if (__was_long)
Howard Hinnante32b5e22010-11-17 17:55:08 +00002893 __alloc_traits::deallocate(__alloc(), __p, __cap+1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002894 if (__now_long)
2895 {
2896 __set_long_cap(__res_arg+1);
2897 __set_long_size(__sz);
2898 __set_long_pointer(__new_data);
2899 }
2900 else
2901 __set_short_size(__sz);
2902 __invalidate_all_iterators();
2903 }
2904}
2905
2906template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002907inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002908typename basic_string<_CharT, _Traits, _Allocator>::const_reference
Marshall Clow1e00d6d2016-07-21 05:31:24 +00002909basic_string<_CharT, _Traits, _Allocator>::operator[](size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002910{
Howard Hinnant499cea12013-08-23 17:37:05 +00002911 _LIBCPP_ASSERT(__pos <= size(), "string index out of bounds");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002912 return *(data() + __pos);
2913}
2914
2915template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002916inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002917typename basic_string<_CharT, _Traits, _Allocator>::reference
Marshall Clow1e00d6d2016-07-21 05:31:24 +00002918basic_string<_CharT, _Traits, _Allocator>::operator[](size_type __pos) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002919{
Howard Hinnant499cea12013-08-23 17:37:05 +00002920 _LIBCPP_ASSERT(__pos <= size(), "string index out of bounds");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002921 return *(__get_pointer() + __pos);
2922}
2923
2924template <class _CharT, class _Traits, class _Allocator>
2925typename basic_string<_CharT, _Traits, _Allocator>::const_reference
2926basic_string<_CharT, _Traits, _Allocator>::at(size_type __n) const
2927{
2928 if (__n >= size())
2929 this->__throw_out_of_range();
2930 return (*this)[__n];
2931}
2932
2933template <class _CharT, class _Traits, class _Allocator>
2934typename basic_string<_CharT, _Traits, _Allocator>::reference
2935basic_string<_CharT, _Traits, _Allocator>::at(size_type __n)
2936{
2937 if (__n >= size())
2938 this->__throw_out_of_range();
2939 return (*this)[__n];
2940}
2941
2942template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002943inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002944typename basic_string<_CharT, _Traits, _Allocator>::reference
2945basic_string<_CharT, _Traits, _Allocator>::front()
2946{
Howard Hinnant499cea12013-08-23 17:37:05 +00002947 _LIBCPP_ASSERT(!empty(), "string::front(): string is empty");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002948 return *__get_pointer();
2949}
2950
2951template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002952inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002953typename basic_string<_CharT, _Traits, _Allocator>::const_reference
2954basic_string<_CharT, _Traits, _Allocator>::front() const
2955{
Howard Hinnant499cea12013-08-23 17:37:05 +00002956 _LIBCPP_ASSERT(!empty(), "string::front(): string is empty");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002957 return *data();
2958}
2959
2960template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002961inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002962typename basic_string<_CharT, _Traits, _Allocator>::reference
2963basic_string<_CharT, _Traits, _Allocator>::back()
2964{
Howard Hinnant499cea12013-08-23 17:37:05 +00002965 _LIBCPP_ASSERT(!empty(), "string::back(): string is empty");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002966 return *(__get_pointer() + size() - 1);
2967}
2968
2969template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002970inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002971typename basic_string<_CharT, _Traits, _Allocator>::const_reference
2972basic_string<_CharT, _Traits, _Allocator>::back() const
2973{
Howard Hinnant499cea12013-08-23 17:37:05 +00002974 _LIBCPP_ASSERT(!empty(), "string::back(): string is empty");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002975 return *(data() + size() - 1);
2976}
2977
2978template <class _CharT, class _Traits, class _Allocator>
2979typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002980basic_string<_CharT, _Traits, _Allocator>::copy(value_type* __s, size_type __n, size_type __pos) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002981{
2982 size_type __sz = size();
2983 if (__pos > __sz)
2984 this->__throw_out_of_range();
Howard Hinnant0949eed2011-06-30 21:18:19 +00002985 size_type __rlen = _VSTD::min(__n, __sz - __pos);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002986 traits_type::copy(__s, data() + __pos, __rlen);
2987 return __rlen;
2988}
2989
2990template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002991inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002992basic_string<_CharT, _Traits, _Allocator>
2993basic_string<_CharT, _Traits, _Allocator>::substr(size_type __pos, size_type __n) const
2994{
2995 return basic_string(*this, __pos, __n, __alloc());
2996}
2997
2998template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002999inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003000void
3001basic_string<_CharT, _Traits, _Allocator>::swap(basic_string& __str)
Marshall Clow7d914d12015-07-13 20:04:56 +00003002#if _LIBCPP_STD_VER >= 14
Eric Fiselier47257c42016-12-28 05:53:01 +00003003 _NOEXCEPT_DEBUG
Marshall Clow7d914d12015-07-13 20:04:56 +00003004#else
Eric Fiselier47257c42016-12-28 05:53:01 +00003005 _NOEXCEPT_DEBUG_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow7d914d12015-07-13 20:04:56 +00003006 __is_nothrow_swappable<allocator_type>::value)
3007#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003008{
Howard Hinnant499cea12013-08-23 17:37:05 +00003009#if _LIBCPP_DEBUG_LEVEL >= 2
3010 if (!__is_long())
3011 __get_db()->__invalidate_all(this);
3012 if (!__str.__is_long())
3013 __get_db()->__invalidate_all(&__str);
3014 __get_db()->swap(this, &__str);
3015#endif
Eric Fiselier47257c42016-12-28 05:53:01 +00003016 _LIBCPP_ASSERT(
3017 __alloc_traits::propagate_on_container_swap::value ||
3018 __alloc_traits::is_always_equal::value ||
3019 __alloc() == __str.__alloc(), "swapping non-equal allocators");
Howard Hinnant0949eed2011-06-30 21:18:19 +00003020 _VSTD::swap(__r_.first(), __str.__r_.first());
Marshall Clow7d914d12015-07-13 20:04:56 +00003021 __swap_allocator(__alloc(), __str.__alloc());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003022}
3023
3024// find
3025
3026template <class _Traits>
3027struct _LIBCPP_HIDDEN __traits_eq
3028{
3029 typedef typename _Traits::char_type char_type;
Howard Hinnanta6119a82011-05-29 19:57:12 +00003030 _LIBCPP_INLINE_VISIBILITY
3031 bool operator()(const char_type& __x, const char_type& __y) _NOEXCEPT
3032 {return _Traits::eq(__x, __y);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003033};
3034
3035template<class _CharT, class _Traits, class _Allocator>
3036typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00003037basic_string<_CharT, _Traits, _Allocator>::find(const value_type* __s,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003038 size_type __pos,
3039 size_type __n) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003040{
Alp Tokerec34c482014-05-15 11:27:39 +00003041 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find(): received nullptr");
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003042 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clow360f3192014-06-02 02:22:49 +00003043 (data(), size(), __s, __pos, __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003044}
3045
3046template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003047inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003048typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnanta6119a82011-05-29 19:57:12 +00003049basic_string<_CharT, _Traits, _Allocator>::find(const basic_string& __str,
3050 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003051{
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003052 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clow360f3192014-06-02 02:22:49 +00003053 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003054}
3055
3056template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003057inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003058typename basic_string<_CharT, _Traits, _Allocator>::size_type
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003059basic_string<_CharT, _Traits, _Allocator>::find(__self_view __sv,
3060 size_type __pos) const _NOEXCEPT
3061{
3062 return __str_find<value_type, size_type, traits_type, npos>
3063 (data(), size(), __sv.data(), __pos, __sv.size());
3064}
3065
3066template<class _CharT, class _Traits, class _Allocator>
3067inline _LIBCPP_INLINE_VISIBILITY
3068typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00003069basic_string<_CharT, _Traits, _Allocator>::find(const value_type* __s,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003070 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003071{
Alp Tokerec34c482014-05-15 11:27:39 +00003072 _LIBCPP_ASSERT(__s != nullptr, "string::find(): received nullptr");
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003073 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clow360f3192014-06-02 02:22:49 +00003074 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003075}
3076
3077template<class _CharT, class _Traits, class _Allocator>
3078typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnanta6119a82011-05-29 19:57:12 +00003079basic_string<_CharT, _Traits, _Allocator>::find(value_type __c,
3080 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003081{
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003082 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clow360f3192014-06-02 02:22:49 +00003083 (data(), size(), __c, __pos);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003084}
3085
3086// rfind
3087
3088template<class _CharT, class _Traits, class _Allocator>
3089typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00003090basic_string<_CharT, _Traits, _Allocator>::rfind(const value_type* __s,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003091 size_type __pos,
3092 size_type __n) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003093{
Alp Tokerec34c482014-05-15 11:27:39 +00003094 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::rfind(): received nullptr");
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003095 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clow360f3192014-06-02 02:22:49 +00003096 (data(), size(), __s, __pos, __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003097}
3098
3099template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003100inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003101typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnanta6119a82011-05-29 19:57:12 +00003102basic_string<_CharT, _Traits, _Allocator>::rfind(const basic_string& __str,
3103 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003104{
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003105 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clow360f3192014-06-02 02:22:49 +00003106 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003107}
3108
3109template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003110inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003111typename basic_string<_CharT, _Traits, _Allocator>::size_type
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003112basic_string<_CharT, _Traits, _Allocator>::rfind(__self_view __sv,
3113 size_type __pos) const _NOEXCEPT
3114{
3115 return __str_rfind<value_type, size_type, traits_type, npos>
3116 (data(), size(), __sv.data(), __pos, __sv.size());
3117}
3118
3119template<class _CharT, class _Traits, class _Allocator>
3120inline _LIBCPP_INLINE_VISIBILITY
3121typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00003122basic_string<_CharT, _Traits, _Allocator>::rfind(const value_type* __s,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003123 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003124{
Alp Tokerec34c482014-05-15 11:27:39 +00003125 _LIBCPP_ASSERT(__s != nullptr, "string::rfind(): received nullptr");
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003126 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clow360f3192014-06-02 02:22:49 +00003127 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003128}
3129
3130template<class _CharT, class _Traits, class _Allocator>
3131typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnanta6119a82011-05-29 19:57:12 +00003132basic_string<_CharT, _Traits, _Allocator>::rfind(value_type __c,
3133 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003134{
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003135 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clow360f3192014-06-02 02:22:49 +00003136 (data(), size(), __c, __pos);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003137}
3138
3139// find_first_of
3140
3141template<class _CharT, class _Traits, class _Allocator>
3142typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00003143basic_string<_CharT, _Traits, _Allocator>::find_first_of(const value_type* __s,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003144 size_type __pos,
3145 size_type __n) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003146{
Alp Tokerec34c482014-05-15 11:27:39 +00003147 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_first_of(): received nullptr");
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003148 return __str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow8eb5acc2014-02-16 01:57:26 +00003149 (data(), size(), __s, __pos, __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003150}
3151
3152template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003153inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003154typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnanta6119a82011-05-29 19:57:12 +00003155basic_string<_CharT, _Traits, _Allocator>::find_first_of(const basic_string& __str,
3156 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003157{
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003158 return __str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow8eb5acc2014-02-16 01:57:26 +00003159 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003160}
3161
3162template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003163inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003164typename basic_string<_CharT, _Traits, _Allocator>::size_type
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003165basic_string<_CharT, _Traits, _Allocator>::find_first_of(__self_view __sv,
3166 size_type __pos) const _NOEXCEPT
3167{
3168 return __str_find_first_of<value_type, size_type, traits_type, npos>
3169 (data(), size(), __sv.data(), __pos, __sv.size());
3170}
3171
3172template<class _CharT, class _Traits, class _Allocator>
3173inline _LIBCPP_INLINE_VISIBILITY
3174typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00003175basic_string<_CharT, _Traits, _Allocator>::find_first_of(const value_type* __s,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003176 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003177{
Alp Tokerec34c482014-05-15 11:27:39 +00003178 _LIBCPP_ASSERT(__s != nullptr, "string::find_first_of(): received nullptr");
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003179 return __str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow8eb5acc2014-02-16 01:57:26 +00003180 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003181}
3182
3183template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003184inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003185typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnanta6119a82011-05-29 19:57:12 +00003186basic_string<_CharT, _Traits, _Allocator>::find_first_of(value_type __c,
3187 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003188{
3189 return find(__c, __pos);
3190}
3191
3192// find_last_of
3193
3194template<class _CharT, class _Traits, class _Allocator>
3195typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00003196basic_string<_CharT, _Traits, _Allocator>::find_last_of(const value_type* __s,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003197 size_type __pos,
3198 size_type __n) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003199{
Alp Tokerec34c482014-05-15 11:27:39 +00003200 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_last_of(): received nullptr");
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003201 return __str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow8eb5acc2014-02-16 01:57:26 +00003202 (data(), size(), __s, __pos, __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003203}
3204
3205template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003206inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003207typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnanta6119a82011-05-29 19:57:12 +00003208basic_string<_CharT, _Traits, _Allocator>::find_last_of(const basic_string& __str,
3209 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003210{
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003211 return __str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow8eb5acc2014-02-16 01:57:26 +00003212 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003213}
3214
3215template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003216inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003217typename basic_string<_CharT, _Traits, _Allocator>::size_type
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003218basic_string<_CharT, _Traits, _Allocator>::find_last_of(__self_view __sv,
3219 size_type __pos) const _NOEXCEPT
3220{
3221 return __str_find_last_of<value_type, size_type, traits_type, npos>
3222 (data(), size(), __sv.data(), __pos, __sv.size());
3223}
3224
3225template<class _CharT, class _Traits, class _Allocator>
3226inline _LIBCPP_INLINE_VISIBILITY
3227typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00003228basic_string<_CharT, _Traits, _Allocator>::find_last_of(const value_type* __s,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003229 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003230{
Alp Tokerec34c482014-05-15 11:27:39 +00003231 _LIBCPP_ASSERT(__s != nullptr, "string::find_last_of(): received nullptr");
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003232 return __str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow8eb5acc2014-02-16 01:57:26 +00003233 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003234}
3235
3236template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003237inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003238typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnanta6119a82011-05-29 19:57:12 +00003239basic_string<_CharT, _Traits, _Allocator>::find_last_of(value_type __c,
3240 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003241{
3242 return rfind(__c, __pos);
3243}
3244
3245// find_first_not_of
3246
3247template<class _CharT, class _Traits, class _Allocator>
3248typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00003249basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const value_type* __s,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003250 size_type __pos,
3251 size_type __n) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003252{
Alp Tokerec34c482014-05-15 11:27:39 +00003253 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_first_not_of(): received nullptr");
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003254 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow8eb5acc2014-02-16 01:57:26 +00003255 (data(), size(), __s, __pos, __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003256}
3257
3258template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003259inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003260typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnanta6119a82011-05-29 19:57:12 +00003261basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const basic_string& __str,
3262 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003263{
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003264 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow8eb5acc2014-02-16 01:57:26 +00003265 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003266}
3267
3268template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003269inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003270typename basic_string<_CharT, _Traits, _Allocator>::size_type
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003271basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(__self_view __sv,
3272 size_type __pos) const _NOEXCEPT
3273{
3274 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
3275 (data(), size(), __sv.data(), __pos, __sv.size());
3276}
3277
3278template<class _CharT, class _Traits, class _Allocator>
3279inline _LIBCPP_INLINE_VISIBILITY
3280typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00003281basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const value_type* __s,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003282 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003283{
Alp Tokerec34c482014-05-15 11:27:39 +00003284 _LIBCPP_ASSERT(__s != nullptr, "string::find_first_not_of(): received nullptr");
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003285 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow8eb5acc2014-02-16 01:57:26 +00003286 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003287}
3288
3289template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003290inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003291typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnanta6119a82011-05-29 19:57:12 +00003292basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(value_type __c,
3293 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003294{
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003295 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow8eb5acc2014-02-16 01:57:26 +00003296 (data(), size(), __c, __pos);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003297}
3298
3299// find_last_not_of
3300
3301template<class _CharT, class _Traits, class _Allocator>
3302typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00003303basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const value_type* __s,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003304 size_type __pos,
3305 size_type __n) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003306{
Alp Tokerec34c482014-05-15 11:27:39 +00003307 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_last_not_of(): received nullptr");
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003308 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow8eb5acc2014-02-16 01:57:26 +00003309 (data(), size(), __s, __pos, __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003310}
3311
3312template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003313inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003314typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnanta6119a82011-05-29 19:57:12 +00003315basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const basic_string& __str,
3316 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003317{
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003318 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow8eb5acc2014-02-16 01:57:26 +00003319 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003320}
3321
3322template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003323inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003324typename basic_string<_CharT, _Traits, _Allocator>::size_type
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003325basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(__self_view __sv,
3326 size_type __pos) const _NOEXCEPT
3327{
3328 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
3329 (data(), size(), __sv.data(), __pos, __sv.size());
3330}
3331
3332template<class _CharT, class _Traits, class _Allocator>
3333inline _LIBCPP_INLINE_VISIBILITY
3334typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00003335basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const value_type* __s,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003336 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003337{
Alp Tokerec34c482014-05-15 11:27:39 +00003338 _LIBCPP_ASSERT(__s != nullptr, "string::find_last_not_of(): received nullptr");
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003339 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow8eb5acc2014-02-16 01:57:26 +00003340 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003341}
3342
3343template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003344inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003345typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnanta6119a82011-05-29 19:57:12 +00003346basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(value_type __c,
3347 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003348{
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003349 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow8eb5acc2014-02-16 01:57:26 +00003350 (data(), size(), __c, __pos);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003351}
3352
3353// compare
3354
3355template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003356inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003357int
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003358basic_string<_CharT, _Traits, _Allocator>::compare(__self_view __sv) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003359{
Howard Hinnantfa06d752011-07-24 21:45:06 +00003360 size_t __lhs_sz = size();
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003361 size_t __rhs_sz = __sv.size();
3362 int __result = traits_type::compare(data(), __sv.data(),
Howard Hinnantfa06d752011-07-24 21:45:06 +00003363 _VSTD::min(__lhs_sz, __rhs_sz));
3364 if (__result != 0)
3365 return __result;
3366 if (__lhs_sz < __rhs_sz)
3367 return -1;
3368 if (__lhs_sz > __rhs_sz)
3369 return 1;
3370 return 0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003371}
3372
3373template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003374inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003375int
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003376basic_string<_CharT, _Traits, _Allocator>::compare(const basic_string& __str) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003377{
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003378 return compare(__self_view(__str));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003379}
3380
3381template <class _CharT, class _Traits, class _Allocator>
3382int
Howard Hinnanta6119a82011-05-29 19:57:12 +00003383basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3384 size_type __n1,
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00003385 const value_type* __s,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003386 size_type __n2) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003387{
Alp Tokerec34c482014-05-15 11:27:39 +00003388 _LIBCPP_ASSERT(__n2 == 0 || __s != nullptr, "string::compare(): received nullptr");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003389 size_type __sz = size();
3390 if (__pos1 > __sz || __n2 == npos)
3391 this->__throw_out_of_range();
Howard Hinnant0949eed2011-06-30 21:18:19 +00003392 size_type __rlen = _VSTD::min(__n1, __sz - __pos1);
3393 int __r = traits_type::compare(data() + __pos1, __s, _VSTD::min(__rlen, __n2));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003394 if (__r == 0)
3395 {
3396 if (__rlen < __n2)
3397 __r = -1;
3398 else if (__rlen > __n2)
3399 __r = 1;
3400 }
3401 return __r;
3402}
3403
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003404template <class _CharT, class _Traits, class _Allocator>
3405inline _LIBCPP_INLINE_VISIBILITY
3406int
3407basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3408 size_type __n1,
3409 __self_view __sv) const
3410{
3411 return compare(__pos1, __n1, __sv.data(), __sv.size());
3412}
3413
3414template <class _CharT, class _Traits, class _Allocator>
3415inline _LIBCPP_INLINE_VISIBILITY
3416int
3417basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3418 size_type __n1,
3419 const basic_string& __str) const
3420{
3421 return compare(__pos1, __n1, __str.data(), __str.size());
3422}
3423
3424template <class _CharT, class _Traits, class _Allocator>
Marshall Clow6ac8de02016-09-24 22:45:42 +00003425template <class _Tp>
3426typename enable_if
3427<
3428 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3429 int
3430>::type
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003431basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3432 size_type __n1,
Marshall Clow6ac8de02016-09-24 22:45:42 +00003433 const _Tp& __t,
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003434 size_type __pos2,
3435 size_type __n2) const
3436{
Marshall Clow6ac8de02016-09-24 22:45:42 +00003437 __self_view __sv = __t;
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003438 return __self_view(*this).substr(__pos1, __n1).compare(__sv.substr(__pos2, __n2));
3439}
3440
3441template <class _CharT, class _Traits, class _Allocator>
3442int
3443basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3444 size_type __n1,
3445 const basic_string& __str,
3446 size_type __pos2,
3447 size_type __n2) const
3448{
3449 return compare(__pos1, __n1, __self_view(__str), __pos2, __n2);
3450}
3451
3452template <class _CharT, class _Traits, class _Allocator>
3453int
3454basic_string<_CharT, _Traits, _Allocator>::compare(const value_type* __s) const _NOEXCEPT
3455{
3456 _LIBCPP_ASSERT(__s != nullptr, "string::compare(): received nullptr");
3457 return compare(0, npos, __s, traits_type::length(__s));
3458}
3459
3460template <class _CharT, class _Traits, class _Allocator>
3461int
3462basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3463 size_type __n1,
3464 const value_type* __s) const
3465{
3466 _LIBCPP_ASSERT(__s != nullptr, "string::compare(): received nullptr");
3467 return compare(__pos1, __n1, __s, traits_type::length(__s));
3468}
3469
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003470// __invariants
3471
3472template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003473inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003474bool
3475basic_string<_CharT, _Traits, _Allocator>::__invariants() const
3476{
3477 if (size() > capacity())
3478 return false;
3479 if (capacity() < __min_cap - 1)
3480 return false;
3481 if (data() == 0)
3482 return false;
3483 if (data()[size()] != value_type(0))
3484 return false;
3485 return true;
3486}
3487
3488// operator==
3489
3490template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003491inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003492bool
3493operator==(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003494 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003495{
Howard Hinnant08dd2532013-04-22 23:55:13 +00003496 size_t __lhs_sz = __lhs.size();
3497 return __lhs_sz == __rhs.size() && _Traits::compare(__lhs.data(),
3498 __rhs.data(),
3499 __lhs_sz) == 0;
3500}
3501
3502template<class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003503inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant08dd2532013-04-22 23:55:13 +00003504bool
3505operator==(const basic_string<char, char_traits<char>, _Allocator>& __lhs,
3506 const basic_string<char, char_traits<char>, _Allocator>& __rhs) _NOEXCEPT
3507{
3508 size_t __lhs_sz = __lhs.size();
3509 if (__lhs_sz != __rhs.size())
3510 return false;
3511 const char* __lp = __lhs.data();
3512 const char* __rp = __rhs.data();
3513 if (__lhs.__is_long())
3514 return char_traits<char>::compare(__lp, __rp, __lhs_sz) == 0;
3515 for (; __lhs_sz != 0; --__lhs_sz, ++__lp, ++__rp)
3516 if (*__lp != *__rp)
3517 return false;
3518 return true;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003519}
3520
3521template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003522inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003523bool
Howard Hinnanta6119a82011-05-29 19:57:12 +00003524operator==(const _CharT* __lhs,
3525 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003526{
Eric Fiselier4f241822015-08-28 03:02:37 +00003527 typedef basic_string<_CharT, _Traits, _Allocator> _String;
3528 _LIBCPP_ASSERT(__lhs != nullptr, "operator==(char*, basic_string): received nullptr");
3529 size_t __lhs_len = _Traits::length(__lhs);
3530 if (__lhs_len != __rhs.size()) return false;
3531 return __rhs.compare(0, _String::npos, __lhs, __lhs_len) == 0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003532}
3533
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003534template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003535inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003536bool
Howard Hinnanta6119a82011-05-29 19:57:12 +00003537operator==(const basic_string<_CharT,_Traits,_Allocator>& __lhs,
3538 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003539{
Eric Fiselier4f241822015-08-28 03:02:37 +00003540 typedef basic_string<_CharT, _Traits, _Allocator> _String;
3541 _LIBCPP_ASSERT(__rhs != nullptr, "operator==(basic_string, char*): received nullptr");
3542 size_t __rhs_len = _Traits::length(__rhs);
3543 if (__rhs_len != __lhs.size()) return false;
3544 return __lhs.compare(0, _String::npos, __rhs, __rhs_len) == 0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003545}
3546
Howard Hinnant324bb032010-08-22 00:02:43 +00003547template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003548inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003549bool
3550operator!=(const basic_string<_CharT,_Traits,_Allocator>& __lhs,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003551 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003552{
3553 return !(__lhs == __rhs);
3554}
3555
3556template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003557inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003558bool
Howard Hinnanta6119a82011-05-29 19:57:12 +00003559operator!=(const _CharT* __lhs,
3560 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003561{
3562 return !(__lhs == __rhs);
3563}
3564
3565template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003566inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003567bool
Howard Hinnanta6119a82011-05-29 19:57:12 +00003568operator!=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
3569 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003570{
3571 return !(__lhs == __rhs);
3572}
3573
3574// operator<
3575
3576template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003577inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003578bool
3579operator< (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003580 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003581{
Howard Hinnant2644a7b2011-07-24 15:07:21 +00003582 return __lhs.compare(__rhs) < 0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003583}
3584
3585template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003586inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003587bool
Howard Hinnanta6119a82011-05-29 19:57:12 +00003588operator< (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
3589 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003590{
Howard Hinnant2644a7b2011-07-24 15:07:21 +00003591 return __lhs.compare(__rhs) < 0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003592}
3593
3594template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003595inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003596bool
Howard Hinnanta6119a82011-05-29 19:57:12 +00003597operator< (const _CharT* __lhs,
3598 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003599{
3600 return __rhs.compare(__lhs) > 0;
3601}
3602
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003603// operator>
3604
3605template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003606inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003607bool
3608operator> (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003609 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003610{
3611 return __rhs < __lhs;
3612}
3613
3614template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003615inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003616bool
Howard Hinnanta6119a82011-05-29 19:57:12 +00003617operator> (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
3618 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003619{
3620 return __rhs < __lhs;
3621}
3622
3623template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003624inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003625bool
Howard Hinnanta6119a82011-05-29 19:57:12 +00003626operator> (const _CharT* __lhs,
3627 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003628{
3629 return __rhs < __lhs;
3630}
3631
3632// operator<=
3633
3634template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003635inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003636bool
3637operator<=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003638 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003639{
3640 return !(__rhs < __lhs);
3641}
3642
3643template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003644inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003645bool
Howard Hinnanta6119a82011-05-29 19:57:12 +00003646operator<=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
3647 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003648{
3649 return !(__rhs < __lhs);
3650}
3651
3652template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003653inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003654bool
Howard Hinnanta6119a82011-05-29 19:57:12 +00003655operator<=(const _CharT* __lhs,
3656 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003657{
3658 return !(__rhs < __lhs);
3659}
3660
3661// operator>=
3662
3663template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003664inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003665bool
3666operator>=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003667 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003668{
3669 return !(__lhs < __rhs);
3670}
3671
3672template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003673inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003674bool
Howard Hinnanta6119a82011-05-29 19:57:12 +00003675operator>=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
3676 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003677{
3678 return !(__lhs < __rhs);
3679}
3680
3681template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003682inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003683bool
Howard Hinnanta6119a82011-05-29 19:57:12 +00003684operator>=(const _CharT* __lhs,
3685 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003686{
3687 return !(__lhs < __rhs);
3688}
3689
3690// operator +
3691
3692template<class _CharT, class _Traits, class _Allocator>
3693basic_string<_CharT, _Traits, _Allocator>
3694operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
3695 const basic_string<_CharT, _Traits, _Allocator>& __rhs)
3696{
3697 basic_string<_CharT, _Traits, _Allocator> __r(__lhs.get_allocator());
3698 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = __lhs.size();
3699 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = __rhs.size();
3700 __r.__init(__lhs.data(), __lhs_sz, __lhs_sz + __rhs_sz);
3701 __r.append(__rhs.data(), __rhs_sz);
3702 return __r;
3703}
3704
3705template<class _CharT, class _Traits, class _Allocator>
3706basic_string<_CharT, _Traits, _Allocator>
3707operator+(const _CharT* __lhs , const basic_string<_CharT,_Traits,_Allocator>& __rhs)
3708{
3709 basic_string<_CharT, _Traits, _Allocator> __r(__rhs.get_allocator());
3710 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = _Traits::length(__lhs);
3711 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = __rhs.size();
3712 __r.__init(__lhs, __lhs_sz, __lhs_sz + __rhs_sz);
3713 __r.append(__rhs.data(), __rhs_sz);
3714 return __r;
3715}
3716
3717template<class _CharT, class _Traits, class _Allocator>
3718basic_string<_CharT, _Traits, _Allocator>
3719operator+(_CharT __lhs, const basic_string<_CharT,_Traits,_Allocator>& __rhs)
3720{
3721 basic_string<_CharT, _Traits, _Allocator> __r(__rhs.get_allocator());
3722 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = __rhs.size();
3723 __r.__init(&__lhs, 1, 1 + __rhs_sz);
3724 __r.append(__rhs.data(), __rhs_sz);
3725 return __r;
3726}
3727
3728template<class _CharT, class _Traits, class _Allocator>
3729basic_string<_CharT, _Traits, _Allocator>
3730operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, const _CharT* __rhs)
3731{
3732 basic_string<_CharT, _Traits, _Allocator> __r(__lhs.get_allocator());
3733 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = __lhs.size();
3734 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = _Traits::length(__rhs);
3735 __r.__init(__lhs.data(), __lhs_sz, __lhs_sz + __rhs_sz);
3736 __r.append(__rhs, __rhs_sz);
3737 return __r;
3738}
3739
3740template<class _CharT, class _Traits, class _Allocator>
3741basic_string<_CharT, _Traits, _Allocator>
3742operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, _CharT __rhs)
3743{
3744 basic_string<_CharT, _Traits, _Allocator> __r(__lhs.get_allocator());
3745 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = __lhs.size();
3746 __r.__init(__lhs.data(), __lhs_sz, __lhs_sz + 1);
3747 __r.push_back(__rhs);
3748 return __r;
3749}
3750
Howard Hinnant73d21a42010-09-04 23:28:19 +00003751#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003752
3753template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003754inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003755basic_string<_CharT, _Traits, _Allocator>
3756operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, const basic_string<_CharT, _Traits, _Allocator>& __rhs)
3757{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003758 return _VSTD::move(__lhs.append(__rhs));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003759}
3760
3761template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003762inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003763basic_string<_CharT, _Traits, _Allocator>
3764operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, basic_string<_CharT, _Traits, _Allocator>&& __rhs)
3765{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003766 return _VSTD::move(__rhs.insert(0, __lhs));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003767}
3768
3769template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003770inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003771basic_string<_CharT, _Traits, _Allocator>
3772operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, basic_string<_CharT, _Traits, _Allocator>&& __rhs)
3773{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003774 return _VSTD::move(__lhs.append(__rhs));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003775}
3776
3777template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003778inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003779basic_string<_CharT, _Traits, _Allocator>
3780operator+(const _CharT* __lhs , basic_string<_CharT,_Traits,_Allocator>&& __rhs)
3781{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003782 return _VSTD::move(__rhs.insert(0, __lhs));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003783}
3784
3785template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003786inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003787basic_string<_CharT, _Traits, _Allocator>
3788operator+(_CharT __lhs, basic_string<_CharT,_Traits,_Allocator>&& __rhs)
3789{
3790 __rhs.insert(__rhs.begin(), __lhs);
Howard Hinnant0949eed2011-06-30 21:18:19 +00003791 return _VSTD::move(__rhs);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003792}
3793
3794template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003795inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003796basic_string<_CharT, _Traits, _Allocator>
3797operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, const _CharT* __rhs)
3798{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003799 return _VSTD::move(__lhs.append(__rhs));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003800}
3801
3802template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003803inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003804basic_string<_CharT, _Traits, _Allocator>
3805operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, _CharT __rhs)
3806{
3807 __lhs.push_back(__rhs);
Howard Hinnant0949eed2011-06-30 21:18:19 +00003808 return _VSTD::move(__lhs);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003809}
3810
Howard Hinnant73d21a42010-09-04 23:28:19 +00003811#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003812
3813// swap
3814
3815template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003816inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003817void
Howard Hinnanta6119a82011-05-29 19:57:12 +00003818swap(basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnant53f7d4c2011-06-03 18:40:47 +00003819 basic_string<_CharT, _Traits, _Allocator>& __rhs)
3820 _NOEXCEPT_(_NOEXCEPT_(__lhs.swap(__rhs)))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003821{
3822 __lhs.swap(__rhs);
3823}
3824
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003825#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
3826
3827typedef basic_string<char16_t> u16string;
3828typedef basic_string<char32_t> u32string;
3829
Howard Hinnant324bb032010-08-22 00:02:43 +00003830#endif // _LIBCPP_HAS_NO_UNICODE_CHARS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003831
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003832_LIBCPP_FUNC_VIS int stoi (const string& __str, size_t* __idx = 0, int __base = 10);
3833_LIBCPP_FUNC_VIS long stol (const string& __str, size_t* __idx = 0, int __base = 10);
3834_LIBCPP_FUNC_VIS unsigned long stoul (const string& __str, size_t* __idx = 0, int __base = 10);
3835_LIBCPP_FUNC_VIS long long stoll (const string& __str, size_t* __idx = 0, int __base = 10);
3836_LIBCPP_FUNC_VIS unsigned long long stoull(const string& __str, size_t* __idx = 0, int __base = 10);
Howard Hinnanta6a062d2010-06-02 18:20:39 +00003837
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003838_LIBCPP_FUNC_VIS float stof (const string& __str, size_t* __idx = 0);
3839_LIBCPP_FUNC_VIS double stod (const string& __str, size_t* __idx = 0);
3840_LIBCPP_FUNC_VIS long double stold(const string& __str, size_t* __idx = 0);
Howard Hinnanta6a062d2010-06-02 18:20:39 +00003841
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003842_LIBCPP_FUNC_VIS string to_string(int __val);
3843_LIBCPP_FUNC_VIS string to_string(unsigned __val);
3844_LIBCPP_FUNC_VIS string to_string(long __val);
3845_LIBCPP_FUNC_VIS string to_string(unsigned long __val);
3846_LIBCPP_FUNC_VIS string to_string(long long __val);
3847_LIBCPP_FUNC_VIS string to_string(unsigned long long __val);
3848_LIBCPP_FUNC_VIS string to_string(float __val);
3849_LIBCPP_FUNC_VIS string to_string(double __val);
3850_LIBCPP_FUNC_VIS string to_string(long double __val);
Howard Hinnanta6a062d2010-06-02 18:20:39 +00003851
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003852_LIBCPP_FUNC_VIS int stoi (const wstring& __str, size_t* __idx = 0, int __base = 10);
3853_LIBCPP_FUNC_VIS long stol (const wstring& __str, size_t* __idx = 0, int __base = 10);
3854_LIBCPP_FUNC_VIS unsigned long stoul (const wstring& __str, size_t* __idx = 0, int __base = 10);
3855_LIBCPP_FUNC_VIS long long stoll (const wstring& __str, size_t* __idx = 0, int __base = 10);
3856_LIBCPP_FUNC_VIS unsigned long long stoull(const wstring& __str, size_t* __idx = 0, int __base = 10);
Howard Hinnanta6a062d2010-06-02 18:20:39 +00003857
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003858_LIBCPP_FUNC_VIS float stof (const wstring& __str, size_t* __idx = 0);
3859_LIBCPP_FUNC_VIS double stod (const wstring& __str, size_t* __idx = 0);
3860_LIBCPP_FUNC_VIS long double stold(const wstring& __str, size_t* __idx = 0);
Howard Hinnanta6a062d2010-06-02 18:20:39 +00003861
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003862_LIBCPP_FUNC_VIS wstring to_wstring(int __val);
3863_LIBCPP_FUNC_VIS wstring to_wstring(unsigned __val);
3864_LIBCPP_FUNC_VIS wstring to_wstring(long __val);
3865_LIBCPP_FUNC_VIS wstring to_wstring(unsigned long __val);
3866_LIBCPP_FUNC_VIS wstring to_wstring(long long __val);
3867_LIBCPP_FUNC_VIS wstring to_wstring(unsigned long long __val);
3868_LIBCPP_FUNC_VIS wstring to_wstring(float __val);
3869_LIBCPP_FUNC_VIS wstring to_wstring(double __val);
3870_LIBCPP_FUNC_VIS wstring to_wstring(long double __val);
Howard Hinnanta6a062d2010-06-02 18:20:39 +00003871
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003872template<class _CharT, class _Traits, class _Allocator>
3873 const typename basic_string<_CharT, _Traits, _Allocator>::size_type
3874 basic_string<_CharT, _Traits, _Allocator>::npos;
3875
3876template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierc3589a82017-01-04 23:56:00 +00003877struct _LIBCPP_TEMPLATE_VIS hash<basic_string<_CharT, _Traits, _Allocator> >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003878 : public unary_function<basic_string<_CharT, _Traits, _Allocator>, size_t>
3879{
3880 size_t
Howard Hinnanta6119a82011-05-29 19:57:12 +00003881 operator()(const basic_string<_CharT, _Traits, _Allocator>& __val) const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003882};
3883
3884template<class _CharT, class _Traits, class _Allocator>
3885size_t
3886hash<basic_string<_CharT, _Traits, _Allocator> >::operator()(
Howard Hinnanta6119a82011-05-29 19:57:12 +00003887 const basic_string<_CharT, _Traits, _Allocator>& __val) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003888{
Sean Huntaffd9e52011-07-29 23:31:56 +00003889 return __do_string_hash(__val.data(), __val.data() + __val.size());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003890}
3891
Howard Hinnant464aa5c2011-07-18 15:51:59 +00003892template<class _CharT, class _Traits, class _Allocator>
3893basic_ostream<_CharT, _Traits>&
3894operator<<(basic_ostream<_CharT, _Traits>& __os,
3895 const basic_string<_CharT, _Traits, _Allocator>& __str);
3896
3897template<class _CharT, class _Traits, class _Allocator>
3898basic_istream<_CharT, _Traits>&
3899operator>>(basic_istream<_CharT, _Traits>& __is,
3900 basic_string<_CharT, _Traits, _Allocator>& __str);
3901
3902template<class _CharT, class _Traits, class _Allocator>
3903basic_istream<_CharT, _Traits>&
3904getline(basic_istream<_CharT, _Traits>& __is,
3905 basic_string<_CharT, _Traits, _Allocator>& __str, _CharT __dlm);
3906
3907template<class _CharT, class _Traits, class _Allocator>
3908inline _LIBCPP_INLINE_VISIBILITY
3909basic_istream<_CharT, _Traits>&
3910getline(basic_istream<_CharT, _Traits>& __is,
3911 basic_string<_CharT, _Traits, _Allocator>& __str);
3912
3913#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
3914
3915template<class _CharT, class _Traits, class _Allocator>
3916inline _LIBCPP_INLINE_VISIBILITY
3917basic_istream<_CharT, _Traits>&
3918getline(basic_istream<_CharT, _Traits>&& __is,
3919 basic_string<_CharT, _Traits, _Allocator>& __str, _CharT __dlm);
3920
3921template<class _CharT, class _Traits, class _Allocator>
3922inline _LIBCPP_INLINE_VISIBILITY
3923basic_istream<_CharT, _Traits>&
3924getline(basic_istream<_CharT, _Traits>&& __is,
3925 basic_string<_CharT, _Traits, _Allocator>& __str);
3926
3927#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
3928
Howard Hinnant499cea12013-08-23 17:37:05 +00003929#if _LIBCPP_DEBUG_LEVEL >= 2
3930
3931template<class _CharT, class _Traits, class _Allocator>
3932bool
3933basic_string<_CharT, _Traits, _Allocator>::__dereferenceable(const const_iterator* __i) const
3934{
3935 return this->data() <= _VSTD::__to_raw_pointer(__i->base()) &&
3936 _VSTD::__to_raw_pointer(__i->base()) < this->data() + this->size();
3937}
3938
3939template<class _CharT, class _Traits, class _Allocator>
3940bool
3941basic_string<_CharT, _Traits, _Allocator>::__decrementable(const const_iterator* __i) const
3942{
3943 return this->data() < _VSTD::__to_raw_pointer(__i->base()) &&
3944 _VSTD::__to_raw_pointer(__i->base()) <= this->data() + this->size();
3945}
3946
3947template<class _CharT, class _Traits, class _Allocator>
3948bool
3949basic_string<_CharT, _Traits, _Allocator>::__addable(const const_iterator* __i, ptrdiff_t __n) const
3950{
3951 const value_type* __p = _VSTD::__to_raw_pointer(__i->base()) + __n;
3952 return this->data() <= __p && __p <= this->data() + this->size();
3953}
3954
3955template<class _CharT, class _Traits, class _Allocator>
3956bool
3957basic_string<_CharT, _Traits, _Allocator>::__subscriptable(const const_iterator* __i, ptrdiff_t __n) const
3958{
3959 const value_type* __p = _VSTD::__to_raw_pointer(__i->base()) + __n;
3960 return this->data() <= __p && __p < this->data() + this->size();
3961}
3962
3963#endif // _LIBCPP_DEBUG_LEVEL >= 2
3964
Marshall Clow15234322013-07-23 17:05:24 +00003965#if _LIBCPP_STD_VER > 11
3966// Literal suffixes for basic_string [basic.string.literals]
Marshall Clow8d9dd7a2013-10-05 21:18:32 +00003967inline namespace literals
Marshall Clow15234322013-07-23 17:05:24 +00003968{
3969 inline namespace string_literals
3970 {
Howard Hinnantab61b2c2013-08-07 19:39:48 +00003971 inline _LIBCPP_INLINE_VISIBILITY
3972 basic_string<char> operator "" s( const char *__str, size_t __len )
3973 {
3974 return basic_string<char> (__str, __len);
3975 }
Marshall Clow15234322013-07-23 17:05:24 +00003976
Howard Hinnantab61b2c2013-08-07 19:39:48 +00003977 inline _LIBCPP_INLINE_VISIBILITY
3978 basic_string<wchar_t> operator "" s( const wchar_t *__str, size_t __len )
3979 {
3980 return basic_string<wchar_t> (__str, __len);
3981 }
Marshall Clow15234322013-07-23 17:05:24 +00003982
Howard Hinnantab61b2c2013-08-07 19:39:48 +00003983 inline _LIBCPP_INLINE_VISIBILITY
3984 basic_string<char16_t> operator "" s( const char16_t *__str, size_t __len )
3985 {
3986 return basic_string<char16_t> (__str, __len);
3987 }
Marshall Clow15234322013-07-23 17:05:24 +00003988
Howard Hinnantab61b2c2013-08-07 19:39:48 +00003989 inline _LIBCPP_INLINE_VISIBILITY
3990 basic_string<char32_t> operator "" s( const char32_t *__str, size_t __len )
3991 {
3992 return basic_string<char32_t> (__str, __len);
3993 }
Marshall Clow15234322013-07-23 17:05:24 +00003994 }
3995}
3996#endif
3997
Eric Fiselier833d6442016-09-15 22:27:07 +00003998_LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_string<char>)
3999_LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_string<wchar_t>)
Howard Hinnant499cea12013-08-23 17:37:05 +00004000_LIBCPP_EXTERN_TEMPLATE(string operator+<char, char_traits<char>, allocator<char> >(char const*, string const&))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004001
4002_LIBCPP_END_NAMESPACE_STD
4003
4004#endif // _LIBCPP_STRING