blob: 010a4c7816ea72a46ae170dd3546dba96dbe4f2a [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
Eric Fiselierb9536102014-08-10 23:53:08 +0000487#include <__debug>
488
Howard Hinnant08e17472011-10-17 20:05:10 +0000489#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000490#pragma GCC system_header
Howard Hinnant08e17472011-10-17 20:05:10 +0000491#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000492
Eric Fiselier018a3d52017-05-31 22:07:49 +0000493_LIBCPP_PUSH_MACROS
494#include <__undef_macros>
495
496
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000497_LIBCPP_BEGIN_NAMESPACE_STD
498
499// fpos
500
501template <class _StateT>
Eric Fiselierc3589a82017-01-04 23:56:00 +0000502class _LIBCPP_TEMPLATE_VIS fpos
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000503{
504private:
505 _StateT __st_;
506 streamoff __off_;
507public:
508 _LIBCPP_INLINE_VISIBILITY fpos(streamoff __off = streamoff()) : __st_(), __off_(__off) {}
509
510 _LIBCPP_INLINE_VISIBILITY operator streamoff() const {return __off_;}
511
512 _LIBCPP_INLINE_VISIBILITY _StateT state() const {return __st_;}
513 _LIBCPP_INLINE_VISIBILITY void state(_StateT __st) {__st_ = __st;}
514
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 _LIBCPP_INLINE_VISIBILITY fpos& operator-=(streamoff __off) {__off_ -= __off; return *this;}
518 _LIBCPP_INLINE_VISIBILITY fpos operator- (streamoff __off) const {fpos __t(*this); __t -= __off; return __t;}
519};
520
521template <class _StateT>
522inline _LIBCPP_INLINE_VISIBILITY
523streamoff operator-(const fpos<_StateT>& __x, const fpos<_StateT>& __y)
524 {return streamoff(__x) - streamoff(__y);}
525
526template <class _StateT>
527inline _LIBCPP_INLINE_VISIBILITY
528bool operator==(const fpos<_StateT>& __x, const fpos<_StateT>& __y)
529 {return streamoff(__x) == streamoff(__y);}
530
531template <class _StateT>
532inline _LIBCPP_INLINE_VISIBILITY
533bool operator!=(const fpos<_StateT>& __x, const fpos<_StateT>& __y)
534 {return streamoff(__x) != streamoff(__y);}
535
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000536// basic_string
537
538template<class _CharT, class _Traits, class _Allocator>
539basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant2b1b2d42011-06-14 19:58:17 +0000540operator+(const basic_string<_CharT, _Traits, _Allocator>& __x,
541 const basic_string<_CharT, _Traits, _Allocator>& __y);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000542
543template<class _CharT, class _Traits, class _Allocator>
544basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant2b1b2d42011-06-14 19:58:17 +0000545operator+(const _CharT* __x, const basic_string<_CharT,_Traits,_Allocator>& __y);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000546
547template<class _CharT, class _Traits, class _Allocator>
548basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant2b1b2d42011-06-14 19:58:17 +0000549operator+(_CharT __x, const basic_string<_CharT,_Traits,_Allocator>& __y);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000550
551template<class _CharT, class _Traits, class _Allocator>
552basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant2b1b2d42011-06-14 19:58:17 +0000553operator+(const basic_string<_CharT, _Traits, _Allocator>& __x, const _CharT* __y);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000554
555template<class _CharT, class _Traits, class _Allocator>
556basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant2b1b2d42011-06-14 19:58:17 +0000557operator+(const basic_string<_CharT, _Traits, _Allocator>& __x, _CharT __y);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000558
559template <bool>
Eric Fiselierc3589a82017-01-04 23:56:00 +0000560class _LIBCPP_TEMPLATE_VIS __basic_string_common
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000561{
562protected:
Marshall Clow14c09a22016-08-25 15:09:01 +0000563 _LIBCPP_NORETURN void __throw_length_error() const;
564 _LIBCPP_NORETURN void __throw_out_of_range() const;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000565};
566
567template <bool __b>
568void
569__basic_string_common<__b>::__throw_length_error() const
570{
Marshall Clow14c09a22016-08-25 15:09:01 +0000571 _VSTD::__throw_length_error("basic_string");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000572}
573
574template <bool __b>
575void
576__basic_string_common<__b>::__throw_out_of_range() const
577{
Marshall Clow14c09a22016-08-25 15:09:01 +0000578 _VSTD::__throw_out_of_range("basic_string");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000579}
580
Howard Hinnante9df0a52013-08-01 18:17:34 +0000581#ifdef _LIBCPP_MSVC
Howard Hinnant78b68282011-10-22 20:59:45 +0000582#pragma warning( push )
583#pragma warning( disable: 4231 )
Howard Hinnante9df0a52013-08-01 18:17:34 +0000584#endif // _LIBCPP_MSVC
Eric Fiselier833d6442016-09-15 22:27:07 +0000585_LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS __basic_string_common<true>)
Howard Hinnante9df0a52013-08-01 18:17:34 +0000586#ifdef _LIBCPP_MSVC
Howard Hinnant78b68282011-10-22 20:59:45 +0000587#pragma warning( pop )
Howard Hinnante9df0a52013-08-01 18:17:34 +0000588#endif // _LIBCPP_MSVC
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000589
Marshall Clowdf9db312016-01-13 21:54:34 +0000590#ifdef _LIBCPP_NO_EXCEPTIONS
591template <class _Iter>
592struct __libcpp_string_gets_noexcept_iterator_impl : public true_type {};
593#elif defined(_LIBCPP_HAS_NO_NOEXCEPT)
594template <class _Iter>
595struct __libcpp_string_gets_noexcept_iterator_impl : public false_type {};
596#else
597template <class _Iter, bool = __is_forward_iterator<_Iter>::value>
598struct __libcpp_string_gets_noexcept_iterator_impl : public _LIBCPP_BOOL_CONSTANT((
599 noexcept(++(declval<_Iter&>())) &&
600 is_nothrow_assignable<_Iter&, _Iter>::value &&
601 noexcept(declval<_Iter>() == declval<_Iter>()) &&
602 noexcept(*declval<_Iter>())
603)) {};
604
605template <class _Iter>
606struct __libcpp_string_gets_noexcept_iterator_impl<_Iter, false> : public false_type {};
607#endif
608
609
610template <class _Iter>
611struct __libcpp_string_gets_noexcept_iterator
612 : public _LIBCPP_BOOL_CONSTANT(__libcpp_is_trivial_iterator<_Iter>::value || __libcpp_string_gets_noexcept_iterator_impl<_Iter>::value) {};
613
Marshall Clow6ac8de02016-09-24 22:45:42 +0000614template <class _CharT, class _Traits, class _Tp>
615struct __can_be_converted_to_string_view : public _LIBCPP_BOOL_CONSTANT(
616 ( is_convertible<const _Tp&, basic_string_view<_CharT, _Traits> >::value &&
617 !is_convertible<const _Tp&, const _CharT*>::value)) {};
618
Evgeniy Stepanov4f01aa82015-10-13 23:48:28 +0000619#ifdef _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant15467182013-04-30 21:44:48 +0000620
621template <class _CharT, size_t = sizeof(_CharT)>
622struct __padding
623{
624 unsigned char __xx[sizeof(_CharT)-1];
625};
626
627template <class _CharT>
628struct __padding<_CharT, 1>
629{
630};
631
Evgeniy Stepanov4f01aa82015-10-13 23:48:28 +0000632#endif // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant15467182013-04-30 21:44:48 +0000633
Howard Hinnant324bb032010-08-22 00:02:43 +0000634template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierc3589a82017-01-04 23:56:00 +0000635class _LIBCPP_TEMPLATE_VIS basic_string
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000636 : private __basic_string_common<true>
637{
638public:
639 typedef basic_string __self;
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000640 typedef basic_string_view<_CharT, _Traits> __self_view;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000641 typedef _Traits traits_type;
Marshall Clow2d4c3fa2017-03-15 18:41:11 +0000642 typedef _CharT value_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000643 typedef _Allocator allocator_type;
Howard Hinnante32b5e22010-11-17 17:55:08 +0000644 typedef allocator_traits<allocator_type> __alloc_traits;
645 typedef typename __alloc_traits::size_type size_type;
646 typedef typename __alloc_traits::difference_type difference_type;
Howard Hinnant53f7d4c2011-06-03 18:40:47 +0000647 typedef value_type& reference;
648 typedef const value_type& const_reference;
Howard Hinnante32b5e22010-11-17 17:55:08 +0000649 typedef typename __alloc_traits::pointer pointer;
650 typedef typename __alloc_traits::const_pointer const_pointer;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000651
Howard Hinnant499cea12013-08-23 17:37:05 +0000652 static_assert(is_pod<value_type>::value, "Character type of basic_string must be a POD");
Marshall Clow2d4c3fa2017-03-15 18:41:11 +0000653 static_assert((is_same<_CharT, typename traits_type::char_type>::value),
Howard Hinnant499cea12013-08-23 17:37:05 +0000654 "traits_type::char_type must be the same type as CharT");
655 static_assert((is_same<typename allocator_type::value_type, value_type>::value),
656 "Allocator::value_type must be same type as value_type");
657#if defined(_LIBCPP_RAW_ITERATORS)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000658 typedef pointer iterator;
659 typedef const_pointer const_iterator;
Howard Hinnant324bb032010-08-22 00:02:43 +0000660#else // defined(_LIBCPP_RAW_ITERATORS)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000661 typedef __wrap_iter<pointer> iterator;
662 typedef __wrap_iter<const_pointer> const_iterator;
Howard Hinnant324bb032010-08-22 00:02:43 +0000663#endif // defined(_LIBCPP_RAW_ITERATORS)
Howard Hinnant0949eed2011-06-30 21:18:19 +0000664 typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
665 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000666
667private:
Howard Hinnant15467182013-04-30 21:44:48 +0000668
Evgeniy Stepanov4f01aa82015-10-13 23:48:28 +0000669#ifdef _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant15467182013-04-30 21:44:48 +0000670
671 struct __long
672 {
673 pointer __data_;
674 size_type __size_;
675 size_type __cap_;
676 };
677
678#if _LIBCPP_BIG_ENDIAN
Ben Craigde79ab62017-07-12 01:45:13 +0000679 static const size_type __short_mask = 0x01;
680 static const size_type __long_mask = 0x1ul;
Howard Hinnant15467182013-04-30 21:44:48 +0000681#else // _LIBCPP_BIG_ENDIAN
Ben Craigde79ab62017-07-12 01:45:13 +0000682 static const size_type __short_mask = 0x80;
683 static const size_type __long_mask = ~(size_type(~0) >> 1);
Howard Hinnant15467182013-04-30 21:44:48 +0000684#endif // _LIBCPP_BIG_ENDIAN
685
686 enum {__min_cap = (sizeof(__long) - 1)/sizeof(value_type) > 2 ?
687 (sizeof(__long) - 1)/sizeof(value_type) : 2};
688
689 struct __short
690 {
691 value_type __data_[__min_cap];
692 struct
693 : __padding<value_type>
694 {
695 unsigned char __size_;
696 };
697 };
698
699#else
700
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000701 struct __long
702 {
703 size_type __cap_;
704 size_type __size_;
705 pointer __data_;
706 };
707
708#if _LIBCPP_BIG_ENDIAN
Ben Craigde79ab62017-07-12 01:45:13 +0000709 static const size_type __short_mask = 0x80;
710 static const size_type __long_mask = ~(size_type(~0) >> 1);
Howard Hinnant324bb032010-08-22 00:02:43 +0000711#else // _LIBCPP_BIG_ENDIAN
Ben Craigde79ab62017-07-12 01:45:13 +0000712 static const size_type __short_mask = 0x01;
713 static const size_type __long_mask = 0x1ul;
Howard Hinnant324bb032010-08-22 00:02:43 +0000714#endif // _LIBCPP_BIG_ENDIAN
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000715
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000716 enum {__min_cap = (sizeof(__long) - 1)/sizeof(value_type) > 2 ?
717 (sizeof(__long) - 1)/sizeof(value_type) : 2};
718
719 struct __short
720 {
721 union
722 {
723 unsigned char __size_;
Howard Hinnant9c0df142012-10-30 19:06:59 +0000724 value_type __lx;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000725 };
726 value_type __data_[__min_cap];
727 };
728
Evgeniy Stepanov4f01aa82015-10-13 23:48:28 +0000729#endif // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant15467182013-04-30 21:44:48 +0000730
Howard Hinnant499cea12013-08-23 17:37:05 +0000731 union __ulx{__long __lx; __short __lxx;};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000732
Howard Hinnant499cea12013-08-23 17:37:05 +0000733 enum {__n_words = sizeof(__ulx) / sizeof(size_type)};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000734
735 struct __raw
736 {
737 size_type __words[__n_words];
738 };
739
740 struct __rep
741 {
742 union
743 {
744 __long __l;
745 __short __s;
746 __raw __r;
747 };
748 };
749
750 __compressed_pair<__rep, allocator_type> __r_;
751
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000752public:
753 static const size_type npos = -1;
754
Howard Hinnant53f7d4c2011-06-03 18:40:47 +0000755 _LIBCPP_INLINE_VISIBILITY basic_string()
756 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
Marshall Clow7b193f72015-06-03 19:56:43 +0000757
758 _LIBCPP_INLINE_VISIBILITY explicit basic_string(const allocator_type& __a)
759#if _LIBCPP_STD_VER <= 14
760 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value);
761#else
762 _NOEXCEPT;
763#endif
764
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000765 basic_string(const basic_string& __str);
766 basic_string(const basic_string& __str, const allocator_type& __a);
Marshall Clow7b193f72015-06-03 19:56:43 +0000767
Eric Fiselier3e928972017-04-19 00:28:44 +0000768#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant9f193f22011-01-26 00:06:59 +0000769 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant53f7d4c2011-06-03 18:40:47 +0000770 basic_string(basic_string&& __str)
Marshall Clow7b193f72015-06-03 19:56:43 +0000771#if _LIBCPP_STD_VER <= 14
Howard Hinnant53f7d4c2011-06-03 18:40:47 +0000772 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
Marshall Clow7b193f72015-06-03 19:56:43 +0000773#else
774 _NOEXCEPT;
775#endif
776
Howard Hinnant9f193f22011-01-26 00:06:59 +0000777 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000778 basic_string(basic_string&& __str, const allocator_type& __a);
Eric Fiselier3e928972017-04-19 00:28:44 +0000779#endif // _LIBCPP_CXX03_LANG
Eric Fiselier0eaf2e82017-02-17 01:17:10 +0000780 _LIBCPP_INLINE_VISIBILITY basic_string(const _CharT* __s);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000781 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier0eaf2e82017-02-17 01:17:10 +0000782 basic_string(const _CharT* __s, const _Allocator& __a);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000783 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier0eaf2e82017-02-17 01:17:10 +0000784 basic_string(const _CharT* __s, size_type __n);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000785 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier0eaf2e82017-02-17 01:17:10 +0000786 basic_string(const _CharT* __s, size_type __n, const _Allocator& __a);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000787 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier0eaf2e82017-02-17 01:17:10 +0000788 basic_string(size_type __n, _CharT __c);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000789 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier0eaf2e82017-02-17 01:17:10 +0000790 basic_string(size_type __n, _CharT __c, const _Allocator& __a);
Marshall Clowf4f7d8f2016-04-07 18:13:41 +0000791 basic_string(const basic_string& __str, size_type __pos, size_type __n,
Eric Fiselier0eaf2e82017-02-17 01:17:10 +0000792 const _Allocator& __a = _Allocator());
Marshall Clowf4f7d8f2016-04-07 18:13:41 +0000793 _LIBCPP_INLINE_VISIBILITY
794 basic_string(const basic_string& __str, size_type __pos,
Eric Fiselier0eaf2e82017-02-17 01:17:10 +0000795 const _Allocator& __a = _Allocator());
Marshall Clowdb7fa112016-11-14 18:22:19 +0000796 template<class _Tp>
Shoaib Meenai24e8dbd2017-03-02 03:02:50 +0000797 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselier0eaf2e82017-02-17 01:17:10 +0000798 basic_string(const _Tp& __t, size_type __pos, size_type __n,
Marshall Clowdb7fa112016-11-14 18:22:19 +0000799 const allocator_type& __a = allocator_type(),
800 typename enable_if<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value, void>::type* = 0);
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000801 _LIBCPP_INLINE_VISIBILITY explicit
802 basic_string(__self_view __sv);
803 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier0eaf2e82017-02-17 01:17:10 +0000804 basic_string(__self_view __sv, const _Allocator& __a);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000805 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);
808 template<class _InputIterator>
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000809 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000810 basic_string(_InputIterator __first, _InputIterator __last, const allocator_type& __a);
Eric Fiselier3e928972017-04-19 00:28:44 +0000811#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000812 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier0eaf2e82017-02-17 01:17:10 +0000813 basic_string(initializer_list<_CharT> __il);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000814 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier0eaf2e82017-02-17 01:17:10 +0000815 basic_string(initializer_list<_CharT> __il, const _Allocator& __a);
Eric Fiselier3e928972017-04-19 00:28:44 +0000816#endif // _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000817
Eric Fiselier51eb1d52016-10-31 03:42:50 +0000818 inline ~basic_string();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000819
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000820 _LIBCPP_INLINE_VISIBILITY
821 operator __self_view() const _NOEXCEPT { return __self_view(data(), size()); }
822
Howard Hinnante32b5e22010-11-17 17:55:08 +0000823 basic_string& operator=(const basic_string& __str);
Eric Fiselierf472d6c2017-01-23 21:24:58 +0000824
825#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier37b2be92017-01-17 22:10:32 +0000826 template <class = void>
Eric Fiselierf472d6c2017-01-23 21:24:58 +0000827#endif
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000828 _LIBCPP_INLINE_VISIBILITY
829 basic_string& operator=(__self_view __sv) {return assign(__sv);}
Eric Fiselier3e928972017-04-19 00:28:44 +0000830#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000831 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant53f7d4c2011-06-03 18:40:47 +0000832 basic_string& operator=(basic_string&& __str)
Marshall Clowaf961ed2015-08-18 18:57:00 +0000833 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value));
Eric Fiselier3e928972017-04-19 00:28:44 +0000834 _LIBCPP_INLINE_VISIBILITY
835 basic_string& operator=(initializer_list<value_type> __il) {return assign(__il.begin(), __il.size());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000836#endif
Howard Hinnant9dcdcde2013-06-28 16:59:19 +0000837 _LIBCPP_INLINE_VISIBILITY basic_string& operator=(const value_type* __s) {return assign(__s);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000838 basic_string& operator=(value_type __c);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000839
Howard Hinnant499cea12013-08-23 17:37:05 +0000840#if _LIBCPP_DEBUG_LEVEL >= 2
841 _LIBCPP_INLINE_VISIBILITY
842 iterator begin() _NOEXCEPT
843 {return iterator(this, __get_pointer());}
844 _LIBCPP_INLINE_VISIBILITY
845 const_iterator begin() const _NOEXCEPT
846 {return const_iterator(this, __get_pointer());}
847 _LIBCPP_INLINE_VISIBILITY
848 iterator end() _NOEXCEPT
849 {return iterator(this, __get_pointer() + size());}
850 _LIBCPP_INLINE_VISIBILITY
851 const_iterator end() const _NOEXCEPT
852 {return const_iterator(this, __get_pointer() + size());}
853#else
Howard Hinnanta6119a82011-05-29 19:57:12 +0000854 _LIBCPP_INLINE_VISIBILITY
855 iterator begin() _NOEXCEPT
856 {return iterator(__get_pointer());}
857 _LIBCPP_INLINE_VISIBILITY
858 const_iterator begin() const _NOEXCEPT
Howard Hinnant9dcdcde2013-06-28 16:59:19 +0000859 {return const_iterator(__get_pointer());}
Howard Hinnanta6119a82011-05-29 19:57:12 +0000860 _LIBCPP_INLINE_VISIBILITY
861 iterator end() _NOEXCEPT
862 {return iterator(__get_pointer() + size());}
863 _LIBCPP_INLINE_VISIBILITY
864 const_iterator end() const _NOEXCEPT
Howard Hinnant9dcdcde2013-06-28 16:59:19 +0000865 {return const_iterator(__get_pointer() + size());}
Howard Hinnant499cea12013-08-23 17:37:05 +0000866#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnanta6119a82011-05-29 19:57:12 +0000867 _LIBCPP_INLINE_VISIBILITY
868 reverse_iterator rbegin() _NOEXCEPT
869 {return reverse_iterator(end());}
870 _LIBCPP_INLINE_VISIBILITY
871 const_reverse_iterator rbegin() const _NOEXCEPT
872 {return const_reverse_iterator(end());}
873 _LIBCPP_INLINE_VISIBILITY
874 reverse_iterator rend() _NOEXCEPT
875 {return reverse_iterator(begin());}
876 _LIBCPP_INLINE_VISIBILITY
877 const_reverse_iterator rend() const _NOEXCEPT
878 {return const_reverse_iterator(begin());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000879
Howard Hinnanta6119a82011-05-29 19:57:12 +0000880 _LIBCPP_INLINE_VISIBILITY
881 const_iterator cbegin() const _NOEXCEPT
882 {return begin();}
883 _LIBCPP_INLINE_VISIBILITY
884 const_iterator cend() const _NOEXCEPT
885 {return end();}
886 _LIBCPP_INLINE_VISIBILITY
887 const_reverse_iterator crbegin() const _NOEXCEPT
888 {return rbegin();}
889 _LIBCPP_INLINE_VISIBILITY
890 const_reverse_iterator crend() const _NOEXCEPT
891 {return rend();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000892
Howard Hinnanta6119a82011-05-29 19:57:12 +0000893 _LIBCPP_INLINE_VISIBILITY size_type size() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000894 {return __is_long() ? __get_long_size() : __get_short_size();}
Howard Hinnanta6119a82011-05-29 19:57:12 +0000895 _LIBCPP_INLINE_VISIBILITY size_type length() const _NOEXCEPT {return size();}
896 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT;
897 _LIBCPP_INLINE_VISIBILITY size_type capacity() const _NOEXCEPT
Eric Fiseliere2d48922015-08-28 07:02:42 +0000898 {return (__is_long() ? __get_long_cap()
899 : static_cast<size_type>(__min_cap)) - 1;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000900
901 void resize(size_type __n, value_type __c);
902 _LIBCPP_INLINE_VISIBILITY void resize(size_type __n) {resize(__n, value_type());}
903
Eric Fiselier59e24fe2017-06-01 02:29:37 +0000904 void reserve(size_type __res_arg = 0);
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000905 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant53f7d4c2011-06-03 18:40:47 +0000906 void shrink_to_fit() _NOEXCEPT {reserve();}
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000907 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta6119a82011-05-29 19:57:12 +0000908 void clear() _NOEXCEPT;
909 _LIBCPP_INLINE_VISIBILITY bool empty() const _NOEXCEPT {return size() == 0;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000910
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000911 _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_type __pos) const _NOEXCEPT;
912 _LIBCPP_INLINE_VISIBILITY reference operator[](size_type __pos) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000913
914 const_reference at(size_type __n) const;
915 reference at(size_type __n);
916
917 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(const basic_string& __str) {return append(__str);}
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000918 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(__self_view __sv) {return append(__sv);}
919 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(const value_type* __s) {return append(__s);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000920 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(value_type __c) {push_back(__c); return *this;}
Eric Fiselier3e928972017-04-19 00:28:44 +0000921#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000922 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(initializer_list<value_type> __il) {return append(__il);}
Eric Fiselier3e928972017-04-19 00:28:44 +0000923#endif // _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000924
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000925 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000926 basic_string& append(const basic_string& __str);
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000927 _LIBCPP_INLINE_VISIBILITY
928 basic_string& append(__self_view __sv) { return append(__sv.data(), __sv.size()); }
Marshall Clowa93b5e22014-03-04 19:17:19 +0000929 basic_string& append(const basic_string& __str, size_type __pos, size_type __n=npos);
Marshall Clow42a87db2016-10-03 23:40:48 +0000930 template <class _Tp>
Shoaib Meenai24e8dbd2017-03-02 03:02:50 +0000931 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
932 typename enable_if
Marshall Clow6ac8de02016-09-24 22:45:42 +0000933 <
934 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
935 basic_string&
936 >::type
937 append(const _Tp& __t, size_type __pos, size_type __n=npos);
Howard Hinnant9dcdcde2013-06-28 16:59:19 +0000938 basic_string& append(const value_type* __s, size_type __n);
939 basic_string& append(const value_type* __s);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000940 basic_string& append(size_type __n, value_type __c);
Eric Fiselier026d38e2016-10-31 02:46:25 +0000941 template <class _ForwardIterator>
Shoaib Meenai24e8dbd2017-03-02 03:02:50 +0000942 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
943 basic_string& __append_forward_unsafe(_ForwardIterator, _ForwardIterator);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000944 template<class _InputIterator>
Shoaib Meenai24e8dbd2017-03-02 03:02:50 +0000945 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
946 typename enable_if
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000947 <
Marshall Clowdf9db312016-01-13 21:54:34 +0000948 __is_exactly_input_iterator<_InputIterator>::value
949 || !__libcpp_string_gets_noexcept_iterator<_InputIterator>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000950 basic_string&
951 >::type
Eric Fiselier026d38e2016-10-31 02:46:25 +0000952 _LIBCPP_INLINE_VISIBILITY
953 append(_InputIterator __first, _InputIterator __last) {
954 const basic_string __temp (__first, __last, __alloc());
955 append(__temp.data(), __temp.size());
956 return *this;
957 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000958 template<class _ForwardIterator>
Shoaib Meenai24e8dbd2017-03-02 03:02:50 +0000959 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
960 typename enable_if
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000961 <
Marshall Clowdf9db312016-01-13 21:54:34 +0000962 __is_forward_iterator<_ForwardIterator>::value
963 && __libcpp_string_gets_noexcept_iterator<_ForwardIterator>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000964 basic_string&
965 >::type
Eric Fiselier026d38e2016-10-31 02:46:25 +0000966 _LIBCPP_INLINE_VISIBILITY
967 append(_ForwardIterator __first, _ForwardIterator __last) {
968 return __append_forward_unsafe(__first, __last);
969 }
970
Eric Fiselier3e928972017-04-19 00:28:44 +0000971#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000972 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000973 basic_string& append(initializer_list<value_type> __il) {return append(__il.begin(), __il.size());}
Eric Fiselier3e928972017-04-19 00:28:44 +0000974#endif // _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000975
976 void push_back(value_type __c);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000977 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000978 void pop_back();
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000979 _LIBCPP_INLINE_VISIBILITY reference front();
980 _LIBCPP_INLINE_VISIBILITY const_reference front() const;
981 _LIBCPP_INLINE_VISIBILITY reference back();
982 _LIBCPP_INLINE_VISIBILITY const_reference back() const;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000983
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000984 _LIBCPP_INLINE_VISIBILITY
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000985 basic_string& assign(__self_view __sv) { return assign(__sv.data(), __sv.size()); }
986 _LIBCPP_INLINE_VISIBILITY
Marshall Clowf40ec902016-03-09 18:08:29 +0000987 basic_string& assign(const basic_string& __str) { return *this = __str; }
Eric Fiselier3e928972017-04-19 00:28:44 +0000988#ifndef _LIBCPP_CXX03_LANG
Howard Hinnanta6119a82011-05-29 19:57:12 +0000989 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier59e24fe2017-06-01 02:29:37 +0000990 basic_string& assign(basic_string&& __str)
Marshall Clow7ed093b2015-10-05 16:17:34 +0000991 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
Eric Fiselier59e24fe2017-06-01 02:29:37 +0000992 {*this = _VSTD::move(__str); return *this;}
Howard Hinnanta6119a82011-05-29 19:57:12 +0000993#endif
Marshall Clowa93b5e22014-03-04 19:17:19 +0000994 basic_string& assign(const basic_string& __str, size_type __pos, size_type __n=npos);
Marshall Clow42a87db2016-10-03 23:40:48 +0000995 template <class _Tp>
Shoaib Meenai24e8dbd2017-03-02 03:02:50 +0000996 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
997 typename enable_if
Marshall Clow6ac8de02016-09-24 22:45:42 +0000998 <
999 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1000 basic_string&
1001 >::type
Eric Fiselier59e24fe2017-06-01 02:29:37 +00001002 assign(const _Tp & __t, size_type __pos, size_type __n=npos);
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001003 basic_string& assign(const value_type* __s, size_type __n);
1004 basic_string& assign(const value_type* __s);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001005 basic_string& assign(size_type __n, value_type __c);
1006 template<class _InputIterator>
Shoaib Meenai24e8dbd2017-03-02 03:02:50 +00001007 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
1008 typename enable_if
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001009 <
Marshall Clowdf9db312016-01-13 21:54:34 +00001010 __is_exactly_input_iterator<_InputIterator>::value
1011 || !__libcpp_string_gets_noexcept_iterator<_InputIterator>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001012 basic_string&
1013 >::type
1014 assign(_InputIterator __first, _InputIterator __last);
1015 template<class _ForwardIterator>
Shoaib Meenai24e8dbd2017-03-02 03:02:50 +00001016 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
1017 typename enable_if
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001018 <
Marshall Clowdf9db312016-01-13 21:54:34 +00001019 __is_forward_iterator<_ForwardIterator>::value
1020 && __libcpp_string_gets_noexcept_iterator<_ForwardIterator>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001021 basic_string&
1022 >::type
1023 assign(_ForwardIterator __first, _ForwardIterator __last);
Eric Fiselier3e928972017-04-19 00:28:44 +00001024#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant8d7a9552010-09-23 17:31:07 +00001025 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001026 basic_string& assign(initializer_list<value_type> __il) {return assign(__il.begin(), __il.size());}
Eric Fiselier3e928972017-04-19 00:28:44 +00001027#endif // _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001028
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001029 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001030 basic_string& insert(size_type __pos1, const basic_string& __str);
Marshall Clow1e00d6d2016-07-21 05:31:24 +00001031 _LIBCPP_INLINE_VISIBILITY
1032 basic_string& insert(size_type __pos1, __self_view __sv) { return insert(__pos1, __sv.data(), __sv.size()); }
Marshall Clow6ac8de02016-09-24 22:45:42 +00001033 template <class _Tp>
Shoaib Meenai24e8dbd2017-03-02 03:02:50 +00001034 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
1035 typename enable_if
Marshall Clow6ac8de02016-09-24 22:45:42 +00001036 <
1037 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1038 basic_string&
1039 >::type
1040 insert(size_type __pos1, const _Tp& __t, size_type __pos2, size_type __n=npos);
Marshall Clowa93b5e22014-03-04 19:17:19 +00001041 basic_string& insert(size_type __pos1, const basic_string& __str, size_type __pos2, size_type __n=npos);
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001042 basic_string& insert(size_type __pos, const value_type* __s, size_type __n);
1043 basic_string& insert(size_type __pos, const value_type* __s);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001044 basic_string& insert(size_type __pos, size_type __n, value_type __c);
1045 iterator insert(const_iterator __pos, value_type __c);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001046 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001047 iterator insert(const_iterator __pos, size_type __n, value_type __c);
1048 template<class _InputIterator>
Shoaib Meenai24e8dbd2017-03-02 03:02:50 +00001049 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
1050 typename enable_if
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001051 <
Marshall Clowdf9db312016-01-13 21:54:34 +00001052 __is_exactly_input_iterator<_InputIterator>::value
1053 || !__libcpp_string_gets_noexcept_iterator<_InputIterator>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001054 iterator
1055 >::type
1056 insert(const_iterator __pos, _InputIterator __first, _InputIterator __last);
1057 template<class _ForwardIterator>
Shoaib Meenai24e8dbd2017-03-02 03:02:50 +00001058 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
1059 typename enable_if
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001060 <
Marshall Clowdf9db312016-01-13 21:54:34 +00001061 __is_forward_iterator<_ForwardIterator>::value
1062 && __libcpp_string_gets_noexcept_iterator<_ForwardIterator>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001063 iterator
1064 >::type
1065 insert(const_iterator __pos, _ForwardIterator __first, _ForwardIterator __last);
Eric Fiselier3e928972017-04-19 00:28:44 +00001066#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant8d7a9552010-09-23 17:31:07 +00001067 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001068 iterator insert(const_iterator __pos, initializer_list<value_type> __il)
1069 {return insert(__pos, __il.begin(), __il.end());}
Eric Fiselier3e928972017-04-19 00:28:44 +00001070#endif // _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001071
1072 basic_string& erase(size_type __pos = 0, size_type __n = npos);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001073 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001074 iterator erase(const_iterator __pos);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001075 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001076 iterator erase(const_iterator __first, const_iterator __last);
1077
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001078 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001079 basic_string& replace(size_type __pos1, size_type __n1, const basic_string& __str);
Marshall Clow1e00d6d2016-07-21 05:31:24 +00001080 _LIBCPP_INLINE_VISIBILITY
1081 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 +00001082 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 +00001083 template <class _Tp>
Shoaib Meenai24e8dbd2017-03-02 03:02:50 +00001084 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
1085 typename enable_if
Marshall Clow6ac8de02016-09-24 22:45:42 +00001086 <
1087 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1088 basic_string&
1089 >::type
1090 replace(size_type __pos1, size_type __n1, const _Tp& __t, size_type __pos2, size_type __n2=npos);
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001091 basic_string& replace(size_type __pos, size_type __n1, const value_type* __s, size_type __n2);
1092 basic_string& replace(size_type __pos, size_type __n1, const value_type* __s);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001093 basic_string& replace(size_type __pos, size_type __n1, size_type __n2, value_type __c);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001094 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7b2cb482010-11-17 21:11:40 +00001095 basic_string& replace(const_iterator __i1, const_iterator __i2, const basic_string& __str);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001096 _LIBCPP_INLINE_VISIBILITY
Marshall Clow1e00d6d2016-07-21 05:31:24 +00001097 basic_string& replace(const_iterator __i1, const_iterator __i2, __self_view __sv) { return replace(__i1 - begin(), __i2 - __i1, __sv); }
1098 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001099 basic_string& replace(const_iterator __i1, const_iterator __i2, const value_type* __s, size_type __n);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001100 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001101 basic_string& replace(const_iterator __i1, const_iterator __i2, const value_type* __s);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001102 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7b2cb482010-11-17 21:11:40 +00001103 basic_string& replace(const_iterator __i1, const_iterator __i2, size_type __n, value_type __c);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001104 template<class _InputIterator>
Shoaib Meenai24e8dbd2017-03-02 03:02:50 +00001105 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
1106 typename enable_if
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001107 <
1108 __is_input_iterator<_InputIterator>::value,
1109 basic_string&
1110 >::type
Howard Hinnant7b2cb482010-11-17 21:11:40 +00001111 replace(const_iterator __i1, const_iterator __i2, _InputIterator __j1, _InputIterator __j2);
Eric Fiselier3e928972017-04-19 00:28:44 +00001112#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant8d7a9552010-09-23 17:31:07 +00001113 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7b2cb482010-11-17 21:11:40 +00001114 basic_string& replace(const_iterator __i1, const_iterator __i2, initializer_list<value_type> __il)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001115 {return replace(__i1, __i2, __il.begin(), __il.end());}
Eric Fiselier3e928972017-04-19 00:28:44 +00001116#endif // _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001117
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001118 size_type copy(value_type* __s, size_type __n, size_type __pos = 0) const;
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001119 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001120 basic_string substr(size_type __pos = 0, size_type __n = npos) const;
1121
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001122 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant53f7d4c2011-06-03 18:40:47 +00001123 void swap(basic_string& __str)
Marshall Clow7d914d12015-07-13 20:04:56 +00001124#if _LIBCPP_STD_VER >= 14
Eric Fiselier47257c42016-12-28 05:53:01 +00001125 _NOEXCEPT_DEBUG;
Marshall Clow7d914d12015-07-13 20:04:56 +00001126#else
Eric Fiselier47257c42016-12-28 05:53:01 +00001127 _NOEXCEPT_DEBUG_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow7d914d12015-07-13 20:04:56 +00001128 __is_nothrow_swappable<allocator_type>::value);
1129#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001130
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001131 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001132 const value_type* c_str() const _NOEXCEPT {return data();}
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001133 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001134 const value_type* data() const _NOEXCEPT {return _VSTD::__to_raw_pointer(__get_pointer());}
Marshall Clowf532a702016-03-08 15:44:30 +00001135#if _LIBCPP_STD_VER > 14
1136 _LIBCPP_INLINE_VISIBILITY
1137 value_type* data() _NOEXCEPT {return _VSTD::__to_raw_pointer(__get_pointer());}
1138#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001139
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001140 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta6119a82011-05-29 19:57:12 +00001141 allocator_type get_allocator() const _NOEXCEPT {return __alloc();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001142
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001143 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta6119a82011-05-29 19:57:12 +00001144 size_type find(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
Marshall Clow1e00d6d2016-07-21 05:31:24 +00001145 _LIBCPP_INLINE_VISIBILITY
1146 size_type find(__self_view __sv, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001147 size_type find(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001148 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001149 size_type find(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnanta6119a82011-05-29 19:57:12 +00001150 size_type find(value_type __c, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001151
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001152 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta6119a82011-05-29 19:57:12 +00001153 size_type rfind(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
Marshall Clow1e00d6d2016-07-21 05:31:24 +00001154 _LIBCPP_INLINE_VISIBILITY
1155 size_type rfind(__self_view __sv, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001156 size_type rfind(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001157 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001158 size_type rfind(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnanta6119a82011-05-29 19:57:12 +00001159 size_type rfind(value_type __c, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001160
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001161 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta6119a82011-05-29 19:57:12 +00001162 size_type find_first_of(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
Marshall Clow1e00d6d2016-07-21 05:31:24 +00001163 _LIBCPP_INLINE_VISIBILITY
1164 size_type find_first_of(__self_view __sv, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001165 size_type find_first_of(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001166 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001167 size_type find_first_of(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001168 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta6119a82011-05-29 19:57:12 +00001169 size_type find_first_of(value_type __c, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001170
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001171 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta6119a82011-05-29 19:57:12 +00001172 size_type find_last_of(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
Marshall Clow1e00d6d2016-07-21 05:31:24 +00001173 _LIBCPP_INLINE_VISIBILITY
1174 size_type find_last_of(__self_view __sv, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001175 size_type find_last_of(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001176 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001177 size_type find_last_of(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001178 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta6119a82011-05-29 19:57:12 +00001179 size_type find_last_of(value_type __c, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001180
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001181 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta6119a82011-05-29 19:57:12 +00001182 size_type find_first_not_of(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
Marshall Clow1e00d6d2016-07-21 05:31:24 +00001183 _LIBCPP_INLINE_VISIBILITY
1184 size_type find_first_not_of(__self_view __sv, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001185 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 +00001186 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001187 size_type find_first_not_of(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnanta6119a82011-05-29 19:57:12 +00001188 _LIBCPP_INLINE_VISIBILITY
1189 size_type find_first_not_of(value_type __c, size_type __pos = 0) const _NOEXCEPT;
1190
1191 _LIBCPP_INLINE_VISIBILITY
1192 size_type find_last_not_of(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
Marshall Clow1e00d6d2016-07-21 05:31:24 +00001193 _LIBCPP_INLINE_VISIBILITY
1194 size_type find_last_not_of(__self_view __sv, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001195 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 +00001196 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001197 size_type find_last_not_of(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnanta6119a82011-05-29 19:57:12 +00001198 _LIBCPP_INLINE_VISIBILITY
1199 size_type find_last_not_of(value_type __c, size_type __pos = npos) const _NOEXCEPT;
1200
1201 _LIBCPP_INLINE_VISIBILITY
1202 int compare(const basic_string& __str) const _NOEXCEPT;
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001203 _LIBCPP_INLINE_VISIBILITY
Marshall Clow1e00d6d2016-07-21 05:31:24 +00001204 int compare(__self_view __sv) const _NOEXCEPT;
1205 _LIBCPP_INLINE_VISIBILITY
1206 int compare(size_type __pos1, size_type __n1, __self_view __sv) const;
1207 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001208 int compare(size_type __pos1, size_type __n1, const basic_string& __str) const;
Marshall Clowa93b5e22014-03-04 19:17:19 +00001209 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 +00001210 template <class _Tp>
Eric Fiselier9dbc0532016-10-14 05:29:46 +00001211 inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow6ac8de02016-09-24 22:45:42 +00001212 typename enable_if
1213 <
1214 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1215 int
1216 >::type
1217 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 +00001218 int compare(const value_type* __s) const _NOEXCEPT;
1219 int compare(size_type __pos1, size_type __n1, const value_type* __s) const;
1220 int compare(size_type __pos1, size_type __n1, const value_type* __s, size_type __n2) const;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001221
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001222 _LIBCPP_INLINE_VISIBILITY bool __invariants() const;
Howard Hinnant08dd2532013-04-22 23:55:13 +00001223
1224 _LIBCPP_INLINE_VISIBILITY
1225 bool __is_long() const _NOEXCEPT
1226 {return bool(__r_.first().__s.__size_ & __short_mask);}
1227
Howard Hinnant499cea12013-08-23 17:37:05 +00001228#if _LIBCPP_DEBUG_LEVEL >= 2
1229
1230 bool __dereferenceable(const const_iterator* __i) const;
1231 bool __decrementable(const const_iterator* __i) const;
1232 bool __addable(const const_iterator* __i, ptrdiff_t __n) const;
1233 bool __subscriptable(const const_iterator* __i, ptrdiff_t __n) const;
1234
1235#endif // _LIBCPP_DEBUG_LEVEL >= 2
1236
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001237private:
Howard Hinnanta6119a82011-05-29 19:57:12 +00001238 _LIBCPP_INLINE_VISIBILITY
1239 allocator_type& __alloc() _NOEXCEPT
1240 {return __r_.second();}
1241 _LIBCPP_INLINE_VISIBILITY
1242 const allocator_type& __alloc() const _NOEXCEPT
1243 {return __r_.second();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001244
Evgeniy Stepanov4f01aa82015-10-13 23:48:28 +00001245#ifdef _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant15467182013-04-30 21:44:48 +00001246
Howard Hinnanta6119a82011-05-29 19:57:12 +00001247 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta6119a82011-05-29 19:57:12 +00001248 void __set_short_size(size_type __s) _NOEXCEPT
Howard Hinnant15467182013-04-30 21:44:48 +00001249# if _LIBCPP_BIG_ENDIAN
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001250 {__r_.first().__s.__size_ = (unsigned char)(__s << 1);}
Howard Hinnant15467182013-04-30 21:44:48 +00001251# else
1252 {__r_.first().__s.__size_ = (unsigned char)(__s);}
1253# endif
1254
Howard Hinnanta6119a82011-05-29 19:57:12 +00001255 _LIBCPP_INLINE_VISIBILITY
1256 size_type __get_short_size() const _NOEXCEPT
Howard Hinnant15467182013-04-30 21:44:48 +00001257# if _LIBCPP_BIG_ENDIAN
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001258 {return __r_.first().__s.__size_ >> 1;}
Howard Hinnant15467182013-04-30 21:44:48 +00001259# else
1260 {return __r_.first().__s.__size_;}
1261# endif
1262
Evgeniy Stepanov4f01aa82015-10-13 23:48:28 +00001263#else // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant15467182013-04-30 21:44:48 +00001264
1265 _LIBCPP_INLINE_VISIBILITY
1266 void __set_short_size(size_type __s) _NOEXCEPT
1267# if _LIBCPP_BIG_ENDIAN
1268 {__r_.first().__s.__size_ = (unsigned char)(__s);}
1269# else
1270 {__r_.first().__s.__size_ = (unsigned char)(__s << 1);}
1271# endif
1272
1273 _LIBCPP_INLINE_VISIBILITY
1274 size_type __get_short_size() const _NOEXCEPT
1275# if _LIBCPP_BIG_ENDIAN
1276 {return __r_.first().__s.__size_;}
1277# else
1278 {return __r_.first().__s.__size_ >> 1;}
1279# endif
1280
Evgeniy Stepanov4f01aa82015-10-13 23:48:28 +00001281#endif // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant15467182013-04-30 21:44:48 +00001282
Howard Hinnanta6119a82011-05-29 19:57:12 +00001283 _LIBCPP_INLINE_VISIBILITY
1284 void __set_long_size(size_type __s) _NOEXCEPT
1285 {__r_.first().__l.__size_ = __s;}
1286 _LIBCPP_INLINE_VISIBILITY
1287 size_type __get_long_size() const _NOEXCEPT
1288 {return __r_.first().__l.__size_;}
1289 _LIBCPP_INLINE_VISIBILITY
1290 void __set_size(size_type __s) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001291 {if (__is_long()) __set_long_size(__s); else __set_short_size(__s);}
1292
Howard Hinnanta6119a82011-05-29 19:57:12 +00001293 _LIBCPP_INLINE_VISIBILITY
1294 void __set_long_cap(size_type __s) _NOEXCEPT
1295 {__r_.first().__l.__cap_ = __long_mask | __s;}
1296 _LIBCPP_INLINE_VISIBILITY
1297 size_type __get_long_cap() const _NOEXCEPT
Howard Hinnantec3773c2011-12-01 20:21:04 +00001298 {return __r_.first().__l.__cap_ & size_type(~__long_mask);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001299
Howard Hinnanta6119a82011-05-29 19:57:12 +00001300 _LIBCPP_INLINE_VISIBILITY
1301 void __set_long_pointer(pointer __p) _NOEXCEPT
1302 {__r_.first().__l.__data_ = __p;}
1303 _LIBCPP_INLINE_VISIBILITY
1304 pointer __get_long_pointer() _NOEXCEPT
1305 {return __r_.first().__l.__data_;}
1306 _LIBCPP_INLINE_VISIBILITY
1307 const_pointer __get_long_pointer() const _NOEXCEPT
1308 {return __r_.first().__l.__data_;}
1309 _LIBCPP_INLINE_VISIBILITY
1310 pointer __get_short_pointer() _NOEXCEPT
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001311 {return pointer_traits<pointer>::pointer_to(__r_.first().__s.__data_[0]);}
Howard Hinnanta6119a82011-05-29 19:57:12 +00001312 _LIBCPP_INLINE_VISIBILITY
1313 const_pointer __get_short_pointer() const _NOEXCEPT
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001314 {return pointer_traits<const_pointer>::pointer_to(__r_.first().__s.__data_[0]);}
Howard Hinnanta6119a82011-05-29 19:57:12 +00001315 _LIBCPP_INLINE_VISIBILITY
1316 pointer __get_pointer() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001317 {return __is_long() ? __get_long_pointer() : __get_short_pointer();}
Howard Hinnanta6119a82011-05-29 19:57:12 +00001318 _LIBCPP_INLINE_VISIBILITY
1319 const_pointer __get_pointer() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001320 {return __is_long() ? __get_long_pointer() : __get_short_pointer();}
1321
Howard Hinnanta6119a82011-05-29 19:57:12 +00001322 _LIBCPP_INLINE_VISIBILITY
1323 void __zero() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001324 {
1325 size_type (&__a)[__n_words] = __r_.first().__r.__words;
1326 for (unsigned __i = 0; __i < __n_words; ++__i)
1327 __a[__i] = 0;
1328 }
1329
1330 template <size_type __a> static
Howard Hinnanta6119a82011-05-29 19:57:12 +00001331 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7f764502013-08-14 18:00:20 +00001332 size_type __align_it(size_type __s) _NOEXCEPT
Eric Fiseliere2d48922015-08-28 07:02:42 +00001333 {return (__s + (__a-1)) & ~(__a-1);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001334 enum {__alignment = 16};
Howard Hinnanta6119a82011-05-29 19:57:12 +00001335 static _LIBCPP_INLINE_VISIBILITY
1336 size_type __recommend(size_type __s) _NOEXCEPT
Eric Fiseliere2d48922015-08-28 07:02:42 +00001337 {return (__s < __min_cap ? static_cast<size_type>(__min_cap) :
Howard Hinnant7f764502013-08-14 18:00:20 +00001338 __align_it<sizeof(value_type) < __alignment ?
Howard Hinnanta6119a82011-05-29 19:57:12 +00001339 __alignment/sizeof(value_type) : 1 > (__s+1)) - 1;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001340
Duncan P. N. Exon Smithed3c0e62017-04-01 03:20:48 +00001341 inline
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001342 void __init(const value_type* __s, size_type __sz, size_type __reserve);
Duncan P. N. Exon Smithed3c0e62017-04-01 03:20:48 +00001343 inline
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001344 void __init(const value_type* __s, size_type __sz);
Duncan P. N. Exon Smithed3c0e62017-04-01 03:20:48 +00001345 inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001346 void __init(size_type __n, value_type __c);
Howard Hinnant324bb032010-08-22 00:02:43 +00001347
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001348 template <class _InputIterator>
Duncan P. N. Exon Smithed3c0e62017-04-01 03:20:48 +00001349 inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001350 typename enable_if
1351 <
Marshall Clowdf9db312016-01-13 21:54:34 +00001352 __is_exactly_input_iterator<_InputIterator>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001353 void
1354 >::type
1355 __init(_InputIterator __first, _InputIterator __last);
1356
1357 template <class _ForwardIterator>
Duncan P. N. Exon Smithed3c0e62017-04-01 03:20:48 +00001358 inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001359 typename enable_if
1360 <
1361 __is_forward_iterator<_ForwardIterator>::value,
1362 void
1363 >::type
1364 __init(_ForwardIterator __first, _ForwardIterator __last);
1365
1366 void __grow_by(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
Howard Hinnant324bb032010-08-22 00:02:43 +00001367 size_type __n_copy, size_type __n_del, size_type __n_add = 0);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001368 void __grow_by_and_replace(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
1369 size_type __n_copy, size_type __n_del,
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001370 size_type __n_add, const value_type* __p_new_stuff);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001371
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001372 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001373 void __erase_to_end(size_type __pos);
1374
Howard Hinnante32b5e22010-11-17 17:55:08 +00001375 _LIBCPP_INLINE_VISIBILITY
1376 void __copy_assign_alloc(const basic_string& __str)
1377 {__copy_assign_alloc(__str, integral_constant<bool,
1378 __alloc_traits::propagate_on_container_copy_assignment::value>());}
1379
1380 _LIBCPP_INLINE_VISIBILITY
1381 void __copy_assign_alloc(const basic_string& __str, true_type)
1382 {
Marshall Clow9247fd22017-01-31 03:40:52 +00001383 if (__alloc() == __str.__alloc())
1384 __alloc() = __str.__alloc();
1385 else
Howard Hinnante32b5e22010-11-17 17:55:08 +00001386 {
Marshall Clow9247fd22017-01-31 03:40:52 +00001387 if (!__str.__is_long())
1388 {
1389 clear();
1390 shrink_to_fit();
1391 __alloc() = __str.__alloc();
1392 }
1393 else
1394 {
1395 allocator_type __a = __str.__alloc();
1396 pointer __p = __alloc_traits::allocate(__a, __str.__get_long_cap());
1397 clear();
1398 shrink_to_fit();
1399 __alloc() = _VSTD::move(__a);
1400 __set_long_pointer(__p);
1401 __set_long_cap(__str.__get_long_cap());
1402 __set_long_size(__str.size());
1403 }
Howard Hinnante32b5e22010-11-17 17:55:08 +00001404 }
Howard Hinnante32b5e22010-11-17 17:55:08 +00001405 }
1406
1407 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantec3773c2011-12-01 20:21:04 +00001408 void __copy_assign_alloc(const basic_string&, false_type) _NOEXCEPT
Howard Hinnante32b5e22010-11-17 17:55:08 +00001409 {}
1410
Eric Fiselier3e928972017-04-19 00:28:44 +00001411#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001412 _LIBCPP_INLINE_VISIBILITY
Marshall Clowaf961ed2015-08-18 18:57:00 +00001413 void __move_assign(basic_string& __str, false_type)
1414 _NOEXCEPT_(__alloc_traits::is_always_equal::value);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001415 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant53f7d4c2011-06-03 18:40:47 +00001416 void __move_assign(basic_string& __str, true_type)
Marshall Clowaf961ed2015-08-18 18:57:00 +00001417#if _LIBCPP_STD_VER > 14
1418 _NOEXCEPT;
1419#else
Howard Hinnant53f7d4c2011-06-03 18:40:47 +00001420 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnante32b5e22010-11-17 17:55:08 +00001421#endif
Marshall Clowaf961ed2015-08-18 18:57:00 +00001422#endif
Howard Hinnante32b5e22010-11-17 17:55:08 +00001423
1424 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3fdbbd22011-08-17 20:36:18 +00001425 void
Howard Hinnant9cbee432011-09-02 20:42:31 +00001426 __move_assign_alloc(basic_string& __str)
Howard Hinnant3fdbbd22011-08-17 20:36:18 +00001427 _NOEXCEPT_(
1428 !__alloc_traits::propagate_on_container_move_assignment::value ||
1429 is_nothrow_move_assignable<allocator_type>::value)
1430 {__move_assign_alloc(__str, integral_constant<bool,
1431 __alloc_traits::propagate_on_container_move_assignment::value>());}
1432
1433 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9cbee432011-09-02 20:42:31 +00001434 void __move_assign_alloc(basic_string& __c, true_type)
Howard Hinnant3fdbbd22011-08-17 20:36:18 +00001435 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
1436 {
1437 __alloc() = _VSTD::move(__c.__alloc());
1438 }
1439
1440 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantec3773c2011-12-01 20:21:04 +00001441 void __move_assign_alloc(basic_string&, false_type)
Howard Hinnant3fdbbd22011-08-17 20:36:18 +00001442 _NOEXCEPT
1443 {}
1444
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001445 _LIBCPP_INLINE_VISIBILITY void __invalidate_all_iterators();
1446 _LIBCPP_INLINE_VISIBILITY void __invalidate_iterators_past(size_type);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001447
1448 friend basic_string operator+<>(const basic_string&, const basic_string&);
1449 friend basic_string operator+<>(const value_type*, const basic_string&);
1450 friend basic_string operator+<>(value_type, const basic_string&);
1451 friend basic_string operator+<>(const basic_string&, const value_type*);
1452 friend basic_string operator+<>(const basic_string&, value_type);
1453};
1454
1455template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001456inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001457void
1458basic_string<_CharT, _Traits, _Allocator>::__invalidate_all_iterators()
1459{
Howard Hinnant499cea12013-08-23 17:37:05 +00001460#if _LIBCPP_DEBUG_LEVEL >= 2
1461 __get_db()->__invalidate_all(this);
1462#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001463}
1464
1465template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001466inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001467void
Howard Hinnantec3773c2011-12-01 20:21:04 +00001468basic_string<_CharT, _Traits, _Allocator>::__invalidate_iterators_past(size_type
Howard Hinnant499cea12013-08-23 17:37:05 +00001469#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantec3773c2011-12-01 20:21:04 +00001470 __pos
1471#endif
1472 )
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001473{
Howard Hinnant499cea12013-08-23 17:37:05 +00001474#if _LIBCPP_DEBUG_LEVEL >= 2
1475 __c_node* __c = __get_db()->__find_c_and_lock(this);
1476 if (__c)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001477 {
Howard Hinnant499cea12013-08-23 17:37:05 +00001478 const_pointer __new_last = __get_pointer() + __pos;
1479 for (__i_node** __p = __c->end_; __p != __c->beg_; )
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001480 {
Howard Hinnant499cea12013-08-23 17:37:05 +00001481 --__p;
1482 const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_);
1483 if (__i->base() > __new_last)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001484 {
Howard Hinnant499cea12013-08-23 17:37:05 +00001485 (*__p)->__c_ = nullptr;
1486 if (--__c->end_ != __p)
1487 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001488 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001489 }
Howard Hinnant499cea12013-08-23 17:37:05 +00001490 __get_db()->unlock();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001491 }
Howard Hinnant499cea12013-08-23 17:37:05 +00001492#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001493}
1494
1495template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001496inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant53f7d4c2011-06-03 18:40:47 +00001497basic_string<_CharT, _Traits, _Allocator>::basic_string()
Marshall Clowc912c0c2015-06-04 02:05:41 +00001498 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001499{
Howard Hinnant499cea12013-08-23 17:37:05 +00001500#if _LIBCPP_DEBUG_LEVEL >= 2
1501 __get_db()->__insert_c(this);
1502#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001503 __zero();
1504}
1505
1506template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001507inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001508basic_string<_CharT, _Traits, _Allocator>::basic_string(const allocator_type& __a)
Eric Fiselier692177d2015-07-18 20:40:46 +00001509#if _LIBCPP_STD_VER <= 14
1510 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value)
1511#else
1512 _NOEXCEPT
1513#endif
Eric Fiselierdb14bcc2017-04-12 23:45:53 +00001514: __r_(__second_tag(), __a)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001515{
Howard Hinnant499cea12013-08-23 17:37:05 +00001516#if _LIBCPP_DEBUG_LEVEL >= 2
1517 __get_db()->__insert_c(this);
1518#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001519 __zero();
1520}
1521
1522template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier6dbed462016-09-16 00:00:48 +00001523void basic_string<_CharT, _Traits, _Allocator>::__init(const value_type* __s,
1524 size_type __sz,
1525 size_type __reserve)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001526{
1527 if (__reserve > max_size())
1528 this->__throw_length_error();
1529 pointer __p;
1530 if (__reserve < __min_cap)
1531 {
1532 __set_short_size(__sz);
1533 __p = __get_short_pointer();
1534 }
1535 else
1536 {
1537 size_type __cap = __recommend(__reserve);
Howard Hinnante32b5e22010-11-17 17:55:08 +00001538 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001539 __set_long_pointer(__p);
1540 __set_long_cap(__cap+1);
1541 __set_long_size(__sz);
1542 }
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001543 traits_type::copy(_VSTD::__to_raw_pointer(__p), __s, __sz);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001544 traits_type::assign(__p[__sz], value_type());
1545}
1546
1547template <class _CharT, class _Traits, class _Allocator>
1548void
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001549basic_string<_CharT, _Traits, _Allocator>::__init(const value_type* __s, size_type __sz)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001550{
1551 if (__sz > max_size())
1552 this->__throw_length_error();
1553 pointer __p;
1554 if (__sz < __min_cap)
1555 {
1556 __set_short_size(__sz);
1557 __p = __get_short_pointer();
1558 }
1559 else
1560 {
1561 size_type __cap = __recommend(__sz);
Howard Hinnante32b5e22010-11-17 17:55:08 +00001562 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001563 __set_long_pointer(__p);
1564 __set_long_cap(__cap+1);
1565 __set_long_size(__sz);
1566 }
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001567 traits_type::copy(_VSTD::__to_raw_pointer(__p), __s, __sz);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001568 traits_type::assign(__p[__sz], value_type());
1569}
1570
1571template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001572inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier0eaf2e82017-02-17 01:17:10 +00001573basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001574{
Howard Hinnant499cea12013-08-23 17:37:05 +00001575 _LIBCPP_ASSERT(__s != nullptr, "basic_string(const char*) detected nullptr");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001576 __init(__s, traits_type::length(__s));
Howard Hinnant499cea12013-08-23 17:37:05 +00001577#if _LIBCPP_DEBUG_LEVEL >= 2
1578 __get_db()->__insert_c(this);
1579#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001580}
1581
1582template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001583inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier0eaf2e82017-02-17 01:17:10 +00001584basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, const _Allocator& __a)
Eric Fiselierdb14bcc2017-04-12 23:45:53 +00001585 : __r_(__second_tag(), __a)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001586{
Howard Hinnant499cea12013-08-23 17:37:05 +00001587 _LIBCPP_ASSERT(__s != nullptr, "basic_string(const char*, allocator) detected nullptr");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001588 __init(__s, traits_type::length(__s));
Howard Hinnant499cea12013-08-23 17:37:05 +00001589#if _LIBCPP_DEBUG_LEVEL >= 2
1590 __get_db()->__insert_c(this);
1591#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001592}
1593
1594template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001595inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier0eaf2e82017-02-17 01:17:10 +00001596basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, size_type __n)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001597{
Howard Hinnant499cea12013-08-23 17:37:05 +00001598 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "basic_string(const char*, n) detected nullptr");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001599 __init(__s, __n);
Howard Hinnant499cea12013-08-23 17:37:05 +00001600#if _LIBCPP_DEBUG_LEVEL >= 2
1601 __get_db()->__insert_c(this);
1602#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001603}
1604
1605template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001606inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier0eaf2e82017-02-17 01:17:10 +00001607basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, size_type __n, const _Allocator& __a)
Eric Fiselierdb14bcc2017-04-12 23:45:53 +00001608 : __r_(__second_tag(), __a)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001609{
Howard Hinnant499cea12013-08-23 17:37:05 +00001610 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "basic_string(const char*, n, allocator) detected nullptr");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001611 __init(__s, __n);
Howard Hinnant499cea12013-08-23 17:37:05 +00001612#if _LIBCPP_DEBUG_LEVEL >= 2
1613 __get_db()->__insert_c(this);
1614#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001615}
1616
1617template <class _CharT, class _Traits, class _Allocator>
1618basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str)
Eric Fiselierdb14bcc2017-04-12 23:45:53 +00001619 : __r_(__second_tag(), __alloc_traits::select_on_container_copy_construction(__str.__alloc()))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001620{
1621 if (!__str.__is_long())
1622 __r_.first().__r = __str.__r_.first().__r;
1623 else
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001624 __init(_VSTD::__to_raw_pointer(__str.__get_long_pointer()), __str.__get_long_size());
Howard Hinnant499cea12013-08-23 17:37:05 +00001625#if _LIBCPP_DEBUG_LEVEL >= 2
1626 __get_db()->__insert_c(this);
1627#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001628}
1629
1630template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier0eaf2e82017-02-17 01:17:10 +00001631basic_string<_CharT, _Traits, _Allocator>::basic_string(
1632 const basic_string& __str, const allocator_type& __a)
Eric Fiselierdb14bcc2017-04-12 23:45:53 +00001633 : __r_(__second_tag(), __a)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001634{
1635 if (!__str.__is_long())
1636 __r_.first().__r = __str.__r_.first().__r;
1637 else
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001638 __init(_VSTD::__to_raw_pointer(__str.__get_long_pointer()), __str.__get_long_size());
Howard Hinnant499cea12013-08-23 17:37:05 +00001639#if _LIBCPP_DEBUG_LEVEL >= 2
1640 __get_db()->__insert_c(this);
1641#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001642}
1643
Eric Fiselier3e928972017-04-19 00:28:44 +00001644#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001645
1646template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001647inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant53f7d4c2011-06-03 18:40:47 +00001648basic_string<_CharT, _Traits, _Allocator>::basic_string(basic_string&& __str)
Marshall Clow7b193f72015-06-03 19:56:43 +00001649#if _LIBCPP_STD_VER <= 14
Howard Hinnant53f7d4c2011-06-03 18:40:47 +00001650 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Marshall Clow7b193f72015-06-03 19:56:43 +00001651#else
1652 _NOEXCEPT
1653#endif
Howard Hinnant0949eed2011-06-30 21:18:19 +00001654 : __r_(_VSTD::move(__str.__r_))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001655{
1656 __str.__zero();
Howard Hinnant499cea12013-08-23 17:37:05 +00001657#if _LIBCPP_DEBUG_LEVEL >= 2
1658 __get_db()->__insert_c(this);
1659 if (__is_long())
1660 __get_db()->swap(this, &__str);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001661#endif
1662}
1663
1664template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001665inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001666basic_string<_CharT, _Traits, _Allocator>::basic_string(basic_string&& __str, const allocator_type& __a)
Eric Fiselierdb14bcc2017-04-12 23:45:53 +00001667 : __r_(__second_tag(), __a)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001668{
Marshall Clowd5549cc2014-07-17 15:32:20 +00001669 if (__str.__is_long() && __a != __str.__alloc()) // copy, not move
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001670 __init(_VSTD::__to_raw_pointer(__str.__get_long_pointer()), __str.__get_long_size());
Marshall Clowd5549cc2014-07-17 15:32:20 +00001671 else
1672 {
1673 __r_.first().__r = __str.__r_.first().__r;
1674 __str.__zero();
1675 }
Howard Hinnant499cea12013-08-23 17:37:05 +00001676#if _LIBCPP_DEBUG_LEVEL >= 2
1677 __get_db()->__insert_c(this);
1678 if (__is_long())
1679 __get_db()->swap(this, &__str);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001680#endif
1681}
1682
Eric Fiselier3e928972017-04-19 00:28:44 +00001683#endif // _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001684
1685template <class _CharT, class _Traits, class _Allocator>
1686void
1687basic_string<_CharT, _Traits, _Allocator>::__init(size_type __n, value_type __c)
1688{
1689 if (__n > max_size())
1690 this->__throw_length_error();
1691 pointer __p;
1692 if (__n < __min_cap)
1693 {
1694 __set_short_size(__n);
1695 __p = __get_short_pointer();
1696 }
1697 else
1698 {
1699 size_type __cap = __recommend(__n);
Howard Hinnante32b5e22010-11-17 17:55:08 +00001700 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001701 __set_long_pointer(__p);
1702 __set_long_cap(__cap+1);
1703 __set_long_size(__n);
1704 }
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001705 traits_type::assign(_VSTD::__to_raw_pointer(__p), __n, __c);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001706 traits_type::assign(__p[__n], value_type());
1707}
1708
1709template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001710inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier0eaf2e82017-02-17 01:17:10 +00001711basic_string<_CharT, _Traits, _Allocator>::basic_string(size_type __n, _CharT __c)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001712{
1713 __init(__n, __c);
Howard Hinnant499cea12013-08-23 17:37:05 +00001714#if _LIBCPP_DEBUG_LEVEL >= 2
1715 __get_db()->__insert_c(this);
1716#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001717}
1718
1719template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001720inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier0eaf2e82017-02-17 01:17:10 +00001721basic_string<_CharT, _Traits, _Allocator>::basic_string(size_type __n, _CharT __c, const _Allocator& __a)
Eric Fiselierdb14bcc2017-04-12 23:45:53 +00001722 : __r_(__second_tag(), __a)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001723{
1724 __init(__n, __c);
Howard Hinnant499cea12013-08-23 17:37:05 +00001725#if _LIBCPP_DEBUG_LEVEL >= 2
1726 __get_db()->__insert_c(this);
1727#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001728}
1729
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001730template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier0eaf2e82017-02-17 01:17:10 +00001731basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str,
1732 size_type __pos, size_type __n,
1733 const _Allocator& __a)
Eric Fiselierdb14bcc2017-04-12 23:45:53 +00001734 : __r_(__second_tag(), __a)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001735{
1736 size_type __str_sz = __str.size();
1737 if (__pos > __str_sz)
1738 this->__throw_out_of_range();
Howard Hinnant0949eed2011-06-30 21:18:19 +00001739 __init(__str.data() + __pos, _VSTD::min(__n, __str_sz - __pos));
Howard Hinnant499cea12013-08-23 17:37:05 +00001740#if _LIBCPP_DEBUG_LEVEL >= 2
1741 __get_db()->__insert_c(this);
1742#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001743}
1744
1745template <class _CharT, class _Traits, class _Allocator>
Marshall Clowf4f7d8f2016-04-07 18:13:41 +00001746inline _LIBCPP_INLINE_VISIBILITY
1747basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str, size_type __pos,
Eric Fiselier0eaf2e82017-02-17 01:17:10 +00001748 const _Allocator& __a)
Eric Fiselierdb14bcc2017-04-12 23:45:53 +00001749 : __r_(__second_tag(), __a)
Marshall Clowf4f7d8f2016-04-07 18:13:41 +00001750{
1751 size_type __str_sz = __str.size();
1752 if (__pos > __str_sz)
1753 this->__throw_out_of_range();
1754 __init(__str.data() + __pos, __str_sz - __pos);
1755#if _LIBCPP_DEBUG_LEVEL >= 2
1756 __get_db()->__insert_c(this);
1757#endif
1758}
1759
1760template <class _CharT, class _Traits, class _Allocator>
Marshall Clowdb7fa112016-11-14 18:22:19 +00001761template <class _Tp>
1762basic_string<_CharT, _Traits, _Allocator>::basic_string(
1763 const _Tp& __t, size_type __pos, size_type __n, const allocator_type& __a,
1764 typename enable_if<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value, void>::type *)
Eric Fiselierdb14bcc2017-04-12 23:45:53 +00001765 : __r_(__second_tag(), __a)
Marshall Clowdb7fa112016-11-14 18:22:19 +00001766{
1767 __self_view __sv = __self_view(__t).substr(__pos, __n);
1768 __init(__sv.data(), __sv.size());
1769#if _LIBCPP_DEBUG_LEVEL >= 2
1770 __get_db()->__insert_c(this);
Eric Fiselier0eaf2e82017-02-17 01:17:10 +00001771#endif
Marshall Clowdb7fa112016-11-14 18:22:19 +00001772}
1773
1774template <class _CharT, class _Traits, class _Allocator>
Marshall Clow1e00d6d2016-07-21 05:31:24 +00001775inline _LIBCPP_INLINE_VISIBILITY
1776basic_string<_CharT, _Traits, _Allocator>::basic_string(__self_view __sv)
1777{
1778 __init(__sv.data(), __sv.size());
1779#if _LIBCPP_DEBUG_LEVEL >= 2
1780 __get_db()->__insert_c(this);
1781#endif
1782}
1783
1784template <class _CharT, class _Traits, class _Allocator>
1785inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier0eaf2e82017-02-17 01:17:10 +00001786basic_string<_CharT, _Traits, _Allocator>::basic_string(__self_view __sv, const _Allocator& __a)
Eric Fiselierdb14bcc2017-04-12 23:45:53 +00001787 : __r_(__second_tag(), __a)
Marshall Clow1e00d6d2016-07-21 05:31:24 +00001788{
1789 __init(__sv.data(), __sv.size());
1790#if _LIBCPP_DEBUG_LEVEL >= 2
1791 __get_db()->__insert_c(this);
1792#endif
1793}
1794
1795template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001796template <class _InputIterator>
1797typename enable_if
1798<
Marshall Clowdf9db312016-01-13 21:54:34 +00001799 __is_exactly_input_iterator<_InputIterator>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001800 void
1801>::type
1802basic_string<_CharT, _Traits, _Allocator>::__init(_InputIterator __first, _InputIterator __last)
1803{
1804 __zero();
1805#ifndef _LIBCPP_NO_EXCEPTIONS
1806 try
1807 {
Howard Hinnant324bb032010-08-22 00:02:43 +00001808#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001809 for (; __first != __last; ++__first)
1810 push_back(*__first);
1811#ifndef _LIBCPP_NO_EXCEPTIONS
1812 }
1813 catch (...)
1814 {
1815 if (__is_long())
Howard Hinnante32b5e22010-11-17 17:55:08 +00001816 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001817 throw;
1818 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001819#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001820}
1821
1822template <class _CharT, class _Traits, class _Allocator>
1823template <class _ForwardIterator>
1824typename enable_if
1825<
1826 __is_forward_iterator<_ForwardIterator>::value,
1827 void
1828>::type
1829basic_string<_CharT, _Traits, _Allocator>::__init(_ForwardIterator __first, _ForwardIterator __last)
1830{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001831 size_type __sz = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001832 if (__sz > max_size())
1833 this->__throw_length_error();
1834 pointer __p;
1835 if (__sz < __min_cap)
1836 {
1837 __set_short_size(__sz);
1838 __p = __get_short_pointer();
1839 }
1840 else
1841 {
1842 size_type __cap = __recommend(__sz);
Howard Hinnante32b5e22010-11-17 17:55:08 +00001843 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001844 __set_long_pointer(__p);
1845 __set_long_cap(__cap+1);
1846 __set_long_size(__sz);
1847 }
Eric Fiselierb9919752014-10-27 19:28:20 +00001848 for (; __first != __last; ++__first, (void) ++__p)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001849 traits_type::assign(*__p, *__first);
1850 traits_type::assign(*__p, value_type());
1851}
1852
1853template <class _CharT, class _Traits, class _Allocator>
1854template<class _InputIterator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001855inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001856basic_string<_CharT, _Traits, _Allocator>::basic_string(_InputIterator __first, _InputIterator __last)
1857{
1858 __init(__first, __last);
Howard Hinnant499cea12013-08-23 17:37:05 +00001859#if _LIBCPP_DEBUG_LEVEL >= 2
1860 __get_db()->__insert_c(this);
1861#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001862}
1863
1864template <class _CharT, class _Traits, class _Allocator>
1865template<class _InputIterator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001866inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001867basic_string<_CharT, _Traits, _Allocator>::basic_string(_InputIterator __first, _InputIterator __last,
1868 const allocator_type& __a)
Eric Fiselierdb14bcc2017-04-12 23:45:53 +00001869 : __r_(__second_tag(), __a)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001870{
1871 __init(__first, __last);
Howard Hinnant499cea12013-08-23 17:37:05 +00001872#if _LIBCPP_DEBUG_LEVEL >= 2
1873 __get_db()->__insert_c(this);
1874#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001875}
1876
Eric Fiselier3e928972017-04-19 00:28:44 +00001877#ifndef _LIBCPP_CXX03_LANG
Howard Hinnante3e32912011-08-12 21:56:02 +00001878
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001879template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001880inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier0eaf2e82017-02-17 01:17:10 +00001881basic_string<_CharT, _Traits, _Allocator>::basic_string(
1882 initializer_list<_CharT> __il)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001883{
1884 __init(__il.begin(), __il.end());
Howard Hinnant499cea12013-08-23 17:37:05 +00001885#if _LIBCPP_DEBUG_LEVEL >= 2
1886 __get_db()->__insert_c(this);
1887#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001888}
1889
1890template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001891inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselierdb14bcc2017-04-12 23:45:53 +00001892
Eric Fiselier0eaf2e82017-02-17 01:17:10 +00001893basic_string<_CharT, _Traits, _Allocator>::basic_string(
1894 initializer_list<_CharT> __il, const _Allocator& __a)
Eric Fiselierdb14bcc2017-04-12 23:45:53 +00001895 : __r_(__second_tag(), __a)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001896{
1897 __init(__il.begin(), __il.end());
Howard Hinnant499cea12013-08-23 17:37:05 +00001898#if _LIBCPP_DEBUG_LEVEL >= 2
1899 __get_db()->__insert_c(this);
1900#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001901}
1902
Eric Fiselier3e928972017-04-19 00:28:44 +00001903#endif // _LIBCPP_CXX03_LANG
Howard Hinnante3e32912011-08-12 21:56:02 +00001904
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001905template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001906basic_string<_CharT, _Traits, _Allocator>::~basic_string()
1907{
Howard Hinnant499cea12013-08-23 17:37:05 +00001908#if _LIBCPP_DEBUG_LEVEL >= 2
1909 __get_db()->__erase_c(this);
1910#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001911 if (__is_long())
Howard Hinnante32b5e22010-11-17 17:55:08 +00001912 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001913}
1914
1915template <class _CharT, class _Traits, class _Allocator>
1916void
1917basic_string<_CharT, _Traits, _Allocator>::__grow_by_and_replace
1918 (size_type __old_cap, size_type __delta_cap, size_type __old_sz,
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001919 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 +00001920{
1921 size_type __ms = max_size();
1922 if (__delta_cap > __ms - __old_cap - 1)
1923 this->__throw_length_error();
1924 pointer __old_p = __get_pointer();
1925 size_type __cap = __old_cap < __ms / 2 - __alignment ?
Howard Hinnant0949eed2011-06-30 21:18:19 +00001926 __recommend(_VSTD::max(__old_cap + __delta_cap, 2 * __old_cap)) :
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001927 __ms - 1;
Howard Hinnante32b5e22010-11-17 17:55:08 +00001928 pointer __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001929 __invalidate_all_iterators();
1930 if (__n_copy != 0)
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001931 traits_type::copy(_VSTD::__to_raw_pointer(__p),
1932 _VSTD::__to_raw_pointer(__old_p), __n_copy);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001933 if (__n_add != 0)
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001934 traits_type::copy(_VSTD::__to_raw_pointer(__p) + __n_copy, __p_new_stuff, __n_add);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001935 size_type __sec_cp_sz = __old_sz - __n_del - __n_copy;
1936 if (__sec_cp_sz != 0)
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001937 traits_type::copy(_VSTD::__to_raw_pointer(__p) + __n_copy + __n_add,
1938 _VSTD::__to_raw_pointer(__old_p) + __n_copy + __n_del, __sec_cp_sz);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001939 if (__old_cap+1 != __min_cap)
Howard Hinnante32b5e22010-11-17 17:55:08 +00001940 __alloc_traits::deallocate(__alloc(), __old_p, __old_cap+1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001941 __set_long_pointer(__p);
1942 __set_long_cap(__cap+1);
1943 __old_sz = __n_copy + __n_add + __sec_cp_sz;
1944 __set_long_size(__old_sz);
1945 traits_type::assign(__p[__old_sz], value_type());
1946}
1947
1948template <class _CharT, class _Traits, class _Allocator>
1949void
1950basic_string<_CharT, _Traits, _Allocator>::__grow_by(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
1951 size_type __n_copy, size_type __n_del, size_type __n_add)
1952{
1953 size_type __ms = max_size();
Marshall Clowecc8d7b2013-11-06 14:24:38 +00001954 if (__delta_cap > __ms - __old_cap)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001955 this->__throw_length_error();
1956 pointer __old_p = __get_pointer();
1957 size_type __cap = __old_cap < __ms / 2 - __alignment ?
Howard Hinnant0949eed2011-06-30 21:18:19 +00001958 __recommend(_VSTD::max(__old_cap + __delta_cap, 2 * __old_cap)) :
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001959 __ms - 1;
Howard Hinnante32b5e22010-11-17 17:55:08 +00001960 pointer __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001961 __invalidate_all_iterators();
1962 if (__n_copy != 0)
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001963 traits_type::copy(_VSTD::__to_raw_pointer(__p),
1964 _VSTD::__to_raw_pointer(__old_p), __n_copy);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001965 size_type __sec_cp_sz = __old_sz - __n_del - __n_copy;
1966 if (__sec_cp_sz != 0)
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001967 traits_type::copy(_VSTD::__to_raw_pointer(__p) + __n_copy + __n_add,
1968 _VSTD::__to_raw_pointer(__old_p) + __n_copy + __n_del,
1969 __sec_cp_sz);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001970 if (__old_cap+1 != __min_cap)
Howard Hinnante32b5e22010-11-17 17:55:08 +00001971 __alloc_traits::deallocate(__alloc(), __old_p, __old_cap+1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001972 __set_long_pointer(__p);
1973 __set_long_cap(__cap+1);
1974}
1975
1976// assign
1977
1978template <class _CharT, class _Traits, class _Allocator>
1979basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001980basic_string<_CharT, _Traits, _Allocator>::assign(const value_type* __s, size_type __n)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001981{
Alp Tokerec34c482014-05-15 11:27:39 +00001982 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::assign received nullptr");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001983 size_type __cap = capacity();
1984 if (__cap >= __n)
1985 {
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001986 value_type* __p = _VSTD::__to_raw_pointer(__get_pointer());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001987 traits_type::move(__p, __s, __n);
1988 traits_type::assign(__p[__n], value_type());
1989 __set_size(__n);
1990 __invalidate_iterators_past(__n);
1991 }
1992 else
1993 {
1994 size_type __sz = size();
1995 __grow_by_and_replace(__cap, __n - __cap, __sz, 0, __sz, __n, __s);
1996 }
1997 return *this;
1998}
1999
2000template <class _CharT, class _Traits, class _Allocator>
2001basic_string<_CharT, _Traits, _Allocator>&
2002basic_string<_CharT, _Traits, _Allocator>::assign(size_type __n, value_type __c)
2003{
2004 size_type __cap = capacity();
2005 if (__cap < __n)
2006 {
2007 size_type __sz = size();
2008 __grow_by(__cap, __n - __cap, __sz, 0, __sz);
2009 }
2010 else
2011 __invalidate_iterators_past(__n);
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002012 value_type* __p = _VSTD::__to_raw_pointer(__get_pointer());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002013 traits_type::assign(__p, __n, __c);
2014 traits_type::assign(__p[__n], value_type());
2015 __set_size(__n);
2016 return *this;
2017}
2018
2019template <class _CharT, class _Traits, class _Allocator>
2020basic_string<_CharT, _Traits, _Allocator>&
2021basic_string<_CharT, _Traits, _Allocator>::operator=(value_type __c)
2022{
2023 pointer __p;
2024 if (__is_long())
2025 {
2026 __p = __get_long_pointer();
2027 __set_long_size(1);
2028 }
2029 else
2030 {
2031 __p = __get_short_pointer();
2032 __set_short_size(1);
2033 }
2034 traits_type::assign(*__p, __c);
2035 traits_type::assign(*++__p, value_type());
2036 __invalidate_iterators_past(1);
2037 return *this;
2038}
2039
2040template <class _CharT, class _Traits, class _Allocator>
Howard Hinnante32b5e22010-11-17 17:55:08 +00002041basic_string<_CharT, _Traits, _Allocator>&
2042basic_string<_CharT, _Traits, _Allocator>::operator=(const basic_string& __str)
2043{
2044 if (this != &__str)
2045 {
2046 __copy_assign_alloc(__str);
Marshall Clowf40ec902016-03-09 18:08:29 +00002047 assign(__str.data(), __str.size());
Howard Hinnante32b5e22010-11-17 17:55:08 +00002048 }
2049 return *this;
2050}
2051
Eric Fiselier3e928972017-04-19 00:28:44 +00002052#ifndef _LIBCPP_CXX03_LANG
Howard Hinnante32b5e22010-11-17 17:55:08 +00002053
2054template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002055inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnante32b5e22010-11-17 17:55:08 +00002056void
2057basic_string<_CharT, _Traits, _Allocator>::__move_assign(basic_string& __str, false_type)
Marshall Clowaf961ed2015-08-18 18:57:00 +00002058 _NOEXCEPT_(__alloc_traits::is_always_equal::value)
Howard Hinnante32b5e22010-11-17 17:55:08 +00002059{
2060 if (__alloc() != __str.__alloc())
2061 assign(__str);
2062 else
2063 __move_assign(__str, true_type());
2064}
2065
2066template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002067inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnante32b5e22010-11-17 17:55:08 +00002068void
2069basic_string<_CharT, _Traits, _Allocator>::__move_assign(basic_string& __str, true_type)
Marshall Clowaf961ed2015-08-18 18:57:00 +00002070#if _LIBCPP_STD_VER > 14
2071 _NOEXCEPT
2072#else
Howard Hinnant53f7d4c2011-06-03 18:40:47 +00002073 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Marshall Clowaf961ed2015-08-18 18:57:00 +00002074#endif
Howard Hinnante32b5e22010-11-17 17:55:08 +00002075{
2076 clear();
2077 shrink_to_fit();
Howard Hinnant3fdbbd22011-08-17 20:36:18 +00002078 __r_.first() = __str.__r_.first();
2079 __move_assign_alloc(__str);
Howard Hinnante32b5e22010-11-17 17:55:08 +00002080 __str.__zero();
2081}
2082
2083template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002084inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnante32b5e22010-11-17 17:55:08 +00002085basic_string<_CharT, _Traits, _Allocator>&
2086basic_string<_CharT, _Traits, _Allocator>::operator=(basic_string&& __str)
Marshall Clowaf961ed2015-08-18 18:57:00 +00002087 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
Howard Hinnante32b5e22010-11-17 17:55:08 +00002088{
2089 __move_assign(__str, integral_constant<bool,
2090 __alloc_traits::propagate_on_container_move_assignment::value>());
2091 return *this;
2092}
2093
2094#endif
2095
2096template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002097template<class _InputIterator>
2098typename enable_if
2099<
Marshall Clowdf9db312016-01-13 21:54:34 +00002100 __is_exactly_input_iterator <_InputIterator>::value
2101 || !__libcpp_string_gets_noexcept_iterator<_InputIterator>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002102 basic_string<_CharT, _Traits, _Allocator>&
2103>::type
2104basic_string<_CharT, _Traits, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
2105{
Marshall Clowd979eed2016-09-05 01:54:30 +00002106 const basic_string __temp(__first, __last, __alloc());
Marshall Clowdf9db312016-01-13 21:54:34 +00002107 assign(__temp.data(), __temp.size());
Argyrios Kyrtzidis1dc6f7a2012-10-13 02:03:45 +00002108 return *this;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002109}
2110
2111template <class _CharT, class _Traits, class _Allocator>
2112template<class _ForwardIterator>
2113typename enable_if
2114<
Marshall Clowdf9db312016-01-13 21:54:34 +00002115 __is_forward_iterator<_ForwardIterator>::value
2116 && __libcpp_string_gets_noexcept_iterator<_ForwardIterator>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002117 basic_string<_CharT, _Traits, _Allocator>&
2118>::type
2119basic_string<_CharT, _Traits, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
2120{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002121 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002122 size_type __cap = capacity();
2123 if (__cap < __n)
2124 {
2125 size_type __sz = size();
2126 __grow_by(__cap, __n - __cap, __sz, 0, __sz);
2127 }
2128 else
2129 __invalidate_iterators_past(__n);
2130 pointer __p = __get_pointer();
2131 for (; __first != __last; ++__first, ++__p)
2132 traits_type::assign(*__p, *__first);
2133 traits_type::assign(*__p, value_type());
2134 __set_size(__n);
2135 return *this;
2136}
2137
2138template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002139basic_string<_CharT, _Traits, _Allocator>&
2140basic_string<_CharT, _Traits, _Allocator>::assign(const basic_string& __str, size_type __pos, size_type __n)
2141{
2142 size_type __sz = __str.size();
2143 if (__pos > __sz)
2144 this->__throw_out_of_range();
Howard Hinnant0949eed2011-06-30 21:18:19 +00002145 return assign(__str.data() + __pos, _VSTD::min(__n, __sz - __pos));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002146}
2147
2148template <class _CharT, class _Traits, class _Allocator>
Marshall Clow6ac8de02016-09-24 22:45:42 +00002149template <class _Tp>
2150typename enable_if
2151<
2152 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
2153 basic_string<_CharT, _Traits, _Allocator>&
2154>::type
2155basic_string<_CharT, _Traits, _Allocator>::assign(const _Tp & __t, size_type __pos, size_type __n)
Marshall Clow1e00d6d2016-07-21 05:31:24 +00002156{
Marshall Clow6ac8de02016-09-24 22:45:42 +00002157 __self_view __sv = __t;
Marshall Clow1e00d6d2016-07-21 05:31:24 +00002158 size_type __sz = __sv.size();
2159 if (__pos > __sz)
2160 this->__throw_out_of_range();
2161 return assign(__sv.data() + __pos, _VSTD::min(__n, __sz - __pos));
2162}
2163
2164
2165template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002166basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002167basic_string<_CharT, _Traits, _Allocator>::assign(const value_type* __s)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002168{
Alp Tokerec34c482014-05-15 11:27:39 +00002169 _LIBCPP_ASSERT(__s != nullptr, "string::assign received nullptr");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002170 return assign(__s, traits_type::length(__s));
2171}
2172
2173// append
2174
2175template <class _CharT, class _Traits, class _Allocator>
2176basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002177basic_string<_CharT, _Traits, _Allocator>::append(const value_type* __s, size_type __n)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002178{
Alp Tokerec34c482014-05-15 11:27:39 +00002179 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::append received nullptr");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002180 size_type __cap = capacity();
2181 size_type __sz = size();
2182 if (__cap - __sz >= __n)
2183 {
2184 if (__n)
2185 {
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002186 value_type* __p = _VSTD::__to_raw_pointer(__get_pointer());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002187 traits_type::copy(__p + __sz, __s, __n);
2188 __sz += __n;
2189 __set_size(__sz);
2190 traits_type::assign(__p[__sz], value_type());
2191 }
2192 }
2193 else
2194 __grow_by_and_replace(__cap, __sz + __n - __cap, __sz, __sz, 0, __n, __s);
2195 return *this;
2196}
2197
2198template <class _CharT, class _Traits, class _Allocator>
2199basic_string<_CharT, _Traits, _Allocator>&
2200basic_string<_CharT, _Traits, _Allocator>::append(size_type __n, value_type __c)
2201{
2202 if (__n)
2203 {
2204 size_type __cap = capacity();
2205 size_type __sz = size();
2206 if (__cap - __sz < __n)
2207 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2208 pointer __p = __get_pointer();
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002209 traits_type::assign(_VSTD::__to_raw_pointer(__p) + __sz, __n, __c);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002210 __sz += __n;
2211 __set_size(__sz);
2212 traits_type::assign(__p[__sz], value_type());
2213 }
2214 return *this;
2215}
2216
2217template <class _CharT, class _Traits, class _Allocator>
2218void
2219basic_string<_CharT, _Traits, _Allocator>::push_back(value_type __c)
2220{
Howard Hinnant15467182013-04-30 21:44:48 +00002221 bool __is_short = !__is_long();
2222 size_type __cap;
2223 size_type __sz;
2224 if (__is_short)
2225 {
2226 __cap = __min_cap - 1;
2227 __sz = __get_short_size();
2228 }
2229 else
2230 {
2231 __cap = __get_long_cap() - 1;
2232 __sz = __get_long_size();
2233 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002234 if (__sz == __cap)
Howard Hinnant15467182013-04-30 21:44:48 +00002235 {
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002236 __grow_by(__cap, 1, __sz, __sz, 0);
Howard Hinnant15467182013-04-30 21:44:48 +00002237 __is_short = !__is_long();
2238 }
2239 pointer __p;
2240 if (__is_short)
2241 {
2242 __p = __get_short_pointer() + __sz;
2243 __set_short_size(__sz+1);
2244 }
2245 else
2246 {
2247 __p = __get_long_pointer() + __sz;
2248 __set_long_size(__sz+1);
2249 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002250 traits_type::assign(*__p, __c);
2251 traits_type::assign(*++__p, value_type());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002252}
2253
Marshall Clowac655ef2016-09-07 03:32:06 +00002254template <class _Tp>
Marshall Clowd979eed2016-09-05 01:54:30 +00002255bool __ptr_in_range (const _Tp* __p, const _Tp* __first, const _Tp* __last)
2256{
2257 return __first <= __p && __p < __last;
2258}
2259
Marshall Clowac655ef2016-09-07 03:32:06 +00002260template <class _Tp1, class _Tp2>
Eric Fiselier0e5ebbc2016-12-23 23:37:52 +00002261bool __ptr_in_range (const _Tp1*, const _Tp2*, const _Tp2*)
Marshall Clowac655ef2016-09-07 03:32:06 +00002262{
2263 return false;
2264}
2265
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002266template <class _CharT, class _Traits, class _Allocator>
2267template<class _ForwardIterator>
Eric Fiselier026d38e2016-10-31 02:46:25 +00002268basic_string<_CharT, _Traits, _Allocator>&
2269basic_string<_CharT, _Traits, _Allocator>::__append_forward_unsafe(
2270 _ForwardIterator __first, _ForwardIterator __last)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002271{
Eric Fiselier026d38e2016-10-31 02:46:25 +00002272 static_assert(__is_forward_iterator<_ForwardIterator>::value,
2273 "function requires a ForwardIterator");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002274 size_type __sz = size();
2275 size_type __cap = capacity();
Howard Hinnant0949eed2011-06-30 21:18:19 +00002276 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002277 if (__n)
2278 {
Eric Fiselier622c7d52017-04-15 06:49:02 +00002279 typedef typename iterator_traits<_ForwardIterator>::reference _CharRef;
2280 _CharRef __tmp_ref = *__first;
2281 if (__ptr_in_range(_VSTD::addressof(__tmp_ref), data(), data() + size()))
Marshall Clowd979eed2016-09-05 01:54:30 +00002282 {
2283 const basic_string __temp (__first, __last, __alloc());
2284 append(__temp.data(), __temp.size());
2285 }
2286 else
2287 {
2288 if (__cap - __sz < __n)
2289 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2290 pointer __p = __get_pointer() + __sz;
2291 for (; __first != __last; ++__p, ++__first)
2292 traits_type::assign(*__p, *__first);
2293 traits_type::assign(*__p, value_type());
2294 __set_size(__sz + __n);
2295 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002296 }
2297 return *this;
2298}
2299
2300template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002301inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002302basic_string<_CharT, _Traits, _Allocator>&
2303basic_string<_CharT, _Traits, _Allocator>::append(const basic_string& __str)
2304{
2305 return append(__str.data(), __str.size());
2306}
2307
2308template <class _CharT, class _Traits, class _Allocator>
2309basic_string<_CharT, _Traits, _Allocator>&
2310basic_string<_CharT, _Traits, _Allocator>::append(const basic_string& __str, size_type __pos, size_type __n)
2311{
2312 size_type __sz = __str.size();
2313 if (__pos > __sz)
2314 this->__throw_out_of_range();
Howard Hinnant0949eed2011-06-30 21:18:19 +00002315 return append(__str.data() + __pos, _VSTD::min(__n, __sz - __pos));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002316}
2317
2318template <class _CharT, class _Traits, class _Allocator>
Marshall Clow42a87db2016-10-03 23:40:48 +00002319template <class _Tp>
Marshall Clow6ac8de02016-09-24 22:45:42 +00002320 typename enable_if
2321 <
2322 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
2323 basic_string<_CharT, _Traits, _Allocator>&
2324 >::type
2325basic_string<_CharT, _Traits, _Allocator>::append(const _Tp & __t, size_type __pos, size_type __n)
Marshall Clow1e00d6d2016-07-21 05:31:24 +00002326{
Marshall Clow6ac8de02016-09-24 22:45:42 +00002327 __self_view __sv = __t;
Marshall Clow1e00d6d2016-07-21 05:31:24 +00002328 size_type __sz = __sv.size();
2329 if (__pos > __sz)
2330 this->__throw_out_of_range();
2331 return append(__sv.data() + __pos, _VSTD::min(__n, __sz - __pos));
2332}
2333
2334template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002335basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002336basic_string<_CharT, _Traits, _Allocator>::append(const value_type* __s)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002337{
Alp Tokerec34c482014-05-15 11:27:39 +00002338 _LIBCPP_ASSERT(__s != nullptr, "string::append received nullptr");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002339 return append(__s, traits_type::length(__s));
2340}
2341
2342// insert
2343
2344template <class _CharT, class _Traits, class _Allocator>
2345basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002346basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, const value_type* __s, size_type __n)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002347{
Alp Tokerec34c482014-05-15 11:27:39 +00002348 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::insert received nullptr");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002349 size_type __sz = size();
2350 if (__pos > __sz)
2351 this->__throw_out_of_range();
2352 size_type __cap = capacity();
2353 if (__cap - __sz >= __n)
2354 {
2355 if (__n)
2356 {
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002357 value_type* __p = _VSTD::__to_raw_pointer(__get_pointer());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002358 size_type __n_move = __sz - __pos;
2359 if (__n_move != 0)
2360 {
2361 if (__p + __pos <= __s && __s < __p + __sz)
2362 __s += __n;
2363 traits_type::move(__p + __pos + __n, __p + __pos, __n_move);
2364 }
2365 traits_type::move(__p + __pos, __s, __n);
2366 __sz += __n;
2367 __set_size(__sz);
2368 traits_type::assign(__p[__sz], value_type());
2369 }
2370 }
2371 else
2372 __grow_by_and_replace(__cap, __sz + __n - __cap, __sz, __pos, 0, __n, __s);
2373 return *this;
2374}
2375
2376template <class _CharT, class _Traits, class _Allocator>
2377basic_string<_CharT, _Traits, _Allocator>&
2378basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, size_type __n, value_type __c)
2379{
2380 size_type __sz = size();
2381 if (__pos > __sz)
2382 this->__throw_out_of_range();
2383 if (__n)
2384 {
2385 size_type __cap = capacity();
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002386 value_type* __p;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002387 if (__cap - __sz >= __n)
2388 {
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002389 __p = _VSTD::__to_raw_pointer(__get_pointer());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002390 size_type __n_move = __sz - __pos;
2391 if (__n_move != 0)
2392 traits_type::move(__p + __pos + __n, __p + __pos, __n_move);
2393 }
2394 else
2395 {
2396 __grow_by(__cap, __sz + __n - __cap, __sz, __pos, 0, __n);
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002397 __p = _VSTD::__to_raw_pointer(__get_long_pointer());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002398 }
2399 traits_type::assign(__p + __pos, __n, __c);
2400 __sz += __n;
2401 __set_size(__sz);
2402 traits_type::assign(__p[__sz], value_type());
2403 }
2404 return *this;
2405}
2406
2407template <class _CharT, class _Traits, class _Allocator>
2408template<class _InputIterator>
2409typename enable_if
2410<
Marshall Clowdf9db312016-01-13 21:54:34 +00002411 __is_exactly_input_iterator<_InputIterator>::value
2412 || !__libcpp_string_gets_noexcept_iterator<_InputIterator>::value,
2413 typename basic_string<_CharT, _Traits, _Allocator>::iterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002414>::type
2415basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, _InputIterator __first, _InputIterator __last)
2416{
Howard Hinnant499cea12013-08-23 17:37:05 +00002417#if _LIBCPP_DEBUG_LEVEL >= 2
2418 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2419 "string::insert(iterator, range) called with an iterator not"
2420 " referring to this string");
2421#endif
Marshall Clowd979eed2016-09-05 01:54:30 +00002422 const basic_string __temp(__first, __last, __alloc());
Marshall Clowdf9db312016-01-13 21:54:34 +00002423 return insert(__pos, __temp.data(), __temp.data() + __temp.size());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002424}
2425
2426template <class _CharT, class _Traits, class _Allocator>
2427template<class _ForwardIterator>
2428typename enable_if
2429<
Marshall Clowdf9db312016-01-13 21:54:34 +00002430 __is_forward_iterator<_ForwardIterator>::value
2431 && __libcpp_string_gets_noexcept_iterator<_ForwardIterator>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002432 typename basic_string<_CharT, _Traits, _Allocator>::iterator
2433>::type
2434basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, _ForwardIterator __first, _ForwardIterator __last)
2435{
Howard Hinnant499cea12013-08-23 17:37:05 +00002436#if _LIBCPP_DEBUG_LEVEL >= 2
2437 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2438 "string::insert(iterator, range) called with an iterator not"
2439 " referring to this string");
2440#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002441 size_type __ip = static_cast<size_type>(__pos - begin());
Howard Hinnant0949eed2011-06-30 21:18:19 +00002442 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002443 if (__n)
2444 {
Eric Fiselier622c7d52017-04-15 06:49:02 +00002445 typedef typename iterator_traits<_ForwardIterator>::reference _CharRef;
2446 _CharRef __tmp_char = *__first;
2447 if (__ptr_in_range(_VSTD::addressof(__tmp_char), data(), data() + size()))
Marshall Clowd979eed2016-09-05 01:54:30 +00002448 {
2449 const basic_string __temp(__first, __last, __alloc());
2450 return insert(__pos, __temp.data(), __temp.data() + __temp.size());
2451 }
2452
2453 size_type __sz = size();
2454 size_type __cap = capacity();
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002455 value_type* __p;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002456 if (__cap - __sz >= __n)
2457 {
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002458 __p = _VSTD::__to_raw_pointer(__get_pointer());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002459 size_type __n_move = __sz - __ip;
2460 if (__n_move != 0)
2461 traits_type::move(__p + __ip + __n, __p + __ip, __n_move);
2462 }
2463 else
2464 {
2465 __grow_by(__cap, __sz + __n - __cap, __sz, __ip, 0, __n);
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002466 __p = _VSTD::__to_raw_pointer(__get_long_pointer());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002467 }
2468 __sz += __n;
2469 __set_size(__sz);
2470 traits_type::assign(__p[__sz], value_type());
2471 for (__p += __ip; __first != __last; ++__p, ++__first)
2472 traits_type::assign(*__p, *__first);
2473 }
2474 return begin() + __ip;
2475}
2476
2477template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002478inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002479basic_string<_CharT, _Traits, _Allocator>&
2480basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const basic_string& __str)
2481{
2482 return insert(__pos1, __str.data(), __str.size());
2483}
2484
2485template <class _CharT, class _Traits, class _Allocator>
2486basic_string<_CharT, _Traits, _Allocator>&
2487basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const basic_string& __str,
2488 size_type __pos2, size_type __n)
2489{
2490 size_type __str_sz = __str.size();
2491 if (__pos2 > __str_sz)
2492 this->__throw_out_of_range();
Howard Hinnant0949eed2011-06-30 21:18:19 +00002493 return insert(__pos1, __str.data() + __pos2, _VSTD::min(__n, __str_sz - __pos2));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002494}
2495
2496template <class _CharT, class _Traits, class _Allocator>
Marshall Clow6ac8de02016-09-24 22:45:42 +00002497template <class _Tp>
2498typename enable_if
2499<
2500 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
2501 basic_string<_CharT, _Traits, _Allocator>&
2502>::type
2503basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const _Tp& __t,
Marshall Clow1e00d6d2016-07-21 05:31:24 +00002504 size_type __pos2, size_type __n)
2505{
Marshall Clow6ac8de02016-09-24 22:45:42 +00002506 __self_view __sv = __t;
Marshall Clow1e00d6d2016-07-21 05:31:24 +00002507 size_type __str_sz = __sv.size();
2508 if (__pos2 > __str_sz)
2509 this->__throw_out_of_range();
2510 return insert(__pos1, __sv.data() + __pos2, _VSTD::min(__n, __str_sz - __pos2));
2511}
2512
2513template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002514basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002515basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, const value_type* __s)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002516{
Alp Tokerec34c482014-05-15 11:27:39 +00002517 _LIBCPP_ASSERT(__s != nullptr, "string::insert received nullptr");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002518 return insert(__pos, __s, traits_type::length(__s));
2519}
2520
2521template <class _CharT, class _Traits, class _Allocator>
2522typename basic_string<_CharT, _Traits, _Allocator>::iterator
2523basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, value_type __c)
2524{
2525 size_type __ip = static_cast<size_type>(__pos - begin());
2526 size_type __sz = size();
2527 size_type __cap = capacity();
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002528 value_type* __p;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002529 if (__cap == __sz)
2530 {
2531 __grow_by(__cap, 1, __sz, __ip, 0, 1);
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002532 __p = _VSTD::__to_raw_pointer(__get_long_pointer());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002533 }
2534 else
2535 {
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002536 __p = _VSTD::__to_raw_pointer(__get_pointer());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002537 size_type __n_move = __sz - __ip;
2538 if (__n_move != 0)
2539 traits_type::move(__p + __ip + 1, __p + __ip, __n_move);
2540 }
2541 traits_type::assign(__p[__ip], __c);
2542 traits_type::assign(__p[++__sz], value_type());
2543 __set_size(__sz);
2544 return begin() + static_cast<difference_type>(__ip);
2545}
2546
2547template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002548inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002549typename basic_string<_CharT, _Traits, _Allocator>::iterator
2550basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, size_type __n, value_type __c)
2551{
Howard Hinnant499cea12013-08-23 17:37:05 +00002552#if _LIBCPP_DEBUG_LEVEL >= 2
2553 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2554 "string::insert(iterator, n, value) called with an iterator not"
2555 " referring to this string");
2556#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002557 difference_type __p = __pos - begin();
2558 insert(static_cast<size_type>(__p), __n, __c);
2559 return begin() + __p;
2560}
2561
2562// replace
2563
2564template <class _CharT, class _Traits, class _Allocator>
2565basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002566basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos, size_type __n1, const value_type* __s, size_type __n2)
Eric Fiselier3b7c1342017-03-09 01:54:13 +00002567 _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002568{
Alp Tokerec34c482014-05-15 11:27:39 +00002569 _LIBCPP_ASSERT(__n2 == 0 || __s != nullptr, "string::replace received nullptr");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002570 size_type __sz = size();
2571 if (__pos > __sz)
2572 this->__throw_out_of_range();
Howard Hinnant0949eed2011-06-30 21:18:19 +00002573 __n1 = _VSTD::min(__n1, __sz - __pos);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002574 size_type __cap = capacity();
2575 if (__cap - __sz + __n1 >= __n2)
2576 {
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002577 value_type* __p = _VSTD::__to_raw_pointer(__get_pointer());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002578 if (__n1 != __n2)
2579 {
2580 size_type __n_move = __sz - __pos - __n1;
2581 if (__n_move != 0)
2582 {
2583 if (__n1 > __n2)
2584 {
2585 traits_type::move(__p + __pos, __s, __n2);
2586 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
2587 goto __finish;
2588 }
2589 if (__p + __pos < __s && __s < __p + __sz)
2590 {
2591 if (__p + __pos + __n1 <= __s)
2592 __s += __n2 - __n1;
2593 else // __p + __pos < __s < __p + __pos + __n1
2594 {
2595 traits_type::move(__p + __pos, __s, __n1);
2596 __pos += __n1;
2597 __s += __n2;
2598 __n2 -= __n1;
2599 __n1 = 0;
2600 }
2601 }
2602 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
2603 }
2604 }
2605 traits_type::move(__p + __pos, __s, __n2);
2606__finish:
Eric Fiselier3b7c1342017-03-09 01:54:13 +00002607// __sz += __n2 - __n1; in this and the below function below can cause unsigned integer overflow,
2608// but this is a safe operation, so we disable the check.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002609 __sz += __n2 - __n1;
2610 __set_size(__sz);
2611 __invalidate_iterators_past(__sz);
2612 traits_type::assign(__p[__sz], value_type());
2613 }
2614 else
2615 __grow_by_and_replace(__cap, __sz - __n1 + __n2 - __cap, __sz, __pos, __n1, __n2, __s);
2616 return *this;
2617}
2618
2619template <class _CharT, class _Traits, class _Allocator>
2620basic_string<_CharT, _Traits, _Allocator>&
2621basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos, size_type __n1, size_type __n2, value_type __c)
Eric Fiselier3b7c1342017-03-09 01:54:13 +00002622 _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002623{
2624 size_type __sz = size();
2625 if (__pos > __sz)
2626 this->__throw_out_of_range();
Howard Hinnant0949eed2011-06-30 21:18:19 +00002627 __n1 = _VSTD::min(__n1, __sz - __pos);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002628 size_type __cap = capacity();
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002629 value_type* __p;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002630 if (__cap - __sz + __n1 >= __n2)
2631 {
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002632 __p = _VSTD::__to_raw_pointer(__get_pointer());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002633 if (__n1 != __n2)
2634 {
2635 size_type __n_move = __sz - __pos - __n1;
2636 if (__n_move != 0)
2637 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
2638 }
2639 }
2640 else
2641 {
2642 __grow_by(__cap, __sz - __n1 + __n2 - __cap, __sz, __pos, __n1, __n2);
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002643 __p = _VSTD::__to_raw_pointer(__get_long_pointer());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002644 }
2645 traits_type::assign(__p + __pos, __n2, __c);
2646 __sz += __n2 - __n1;
2647 __set_size(__sz);
2648 __invalidate_iterators_past(__sz);
2649 traits_type::assign(__p[__sz], value_type());
2650 return *this;
2651}
2652
2653template <class _CharT, class _Traits, class _Allocator>
2654template<class _InputIterator>
2655typename enable_if
2656<
2657 __is_input_iterator<_InputIterator>::value,
2658 basic_string<_CharT, _Traits, _Allocator>&
2659>::type
Howard Hinnant7b2cb482010-11-17 21:11:40 +00002660basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002661 _InputIterator __j1, _InputIterator __j2)
2662{
Marshall Clowd979eed2016-09-05 01:54:30 +00002663 const basic_string __temp(__j1, __j2, __alloc());
Marshall Clowdf9db312016-01-13 21:54:34 +00002664 return this->replace(__i1, __i2, __temp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002665}
2666
2667template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002668inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002669basic_string<_CharT, _Traits, _Allocator>&
2670basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const basic_string& __str)
2671{
2672 return replace(__pos1, __n1, __str.data(), __str.size());
2673}
2674
2675template <class _CharT, class _Traits, class _Allocator>
2676basic_string<_CharT, _Traits, _Allocator>&
2677basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const basic_string& __str,
2678 size_type __pos2, size_type __n2)
2679{
2680 size_type __str_sz = __str.size();
2681 if (__pos2 > __str_sz)
2682 this->__throw_out_of_range();
Howard Hinnant0949eed2011-06-30 21:18:19 +00002683 return replace(__pos1, __n1, __str.data() + __pos2, _VSTD::min(__n2, __str_sz - __pos2));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002684}
2685
2686template <class _CharT, class _Traits, class _Allocator>
Marshall Clow6ac8de02016-09-24 22:45:42 +00002687template <class _Tp>
2688typename enable_if
2689<
2690 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
2691 basic_string<_CharT, _Traits, _Allocator>&
2692>::type
2693basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const _Tp& __t,
Marshall Clow1e00d6d2016-07-21 05:31:24 +00002694 size_type __pos2, size_type __n2)
2695{
Marshall Clow6ac8de02016-09-24 22:45:42 +00002696 __self_view __sv = __t;
Marshall Clow1e00d6d2016-07-21 05:31:24 +00002697 size_type __str_sz = __sv.size();
2698 if (__pos2 > __str_sz)
2699 this->__throw_out_of_range();
2700 return replace(__pos1, __n1, __sv.data() + __pos2, _VSTD::min(__n2, __str_sz - __pos2));
2701}
2702
2703template <class _CharT, class _Traits, class _Allocator>
2704basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002705basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos, size_type __n1, const value_type* __s)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002706{
Alp Tokerec34c482014-05-15 11:27:39 +00002707 _LIBCPP_ASSERT(__s != nullptr, "string::replace received nullptr");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002708 return replace(__pos, __n1, __s, traits_type::length(__s));
2709}
2710
2711template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002712inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002713basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant7b2cb482010-11-17 21:11:40 +00002714basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, const basic_string& __str)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002715{
2716 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1),
2717 __str.data(), __str.size());
2718}
2719
2720template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002721inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002722basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002723basic_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 +00002724{
2725 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __s, __n);
2726}
2727
2728template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002729inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002730basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002731basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, const value_type* __s)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002732{
2733 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __s);
2734}
2735
2736template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002737inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002738basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant7b2cb482010-11-17 21:11:40 +00002739basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, size_type __n, value_type __c)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002740{
2741 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __n, __c);
2742}
2743
2744// erase
2745
2746template <class _CharT, class _Traits, class _Allocator>
2747basic_string<_CharT, _Traits, _Allocator>&
2748basic_string<_CharT, _Traits, _Allocator>::erase(size_type __pos, size_type __n)
2749{
2750 size_type __sz = size();
2751 if (__pos > __sz)
2752 this->__throw_out_of_range();
2753 if (__n)
2754 {
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002755 value_type* __p = _VSTD::__to_raw_pointer(__get_pointer());
Howard Hinnant0949eed2011-06-30 21:18:19 +00002756 __n = _VSTD::min(__n, __sz - __pos);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002757 size_type __n_move = __sz - __pos - __n;
2758 if (__n_move != 0)
2759 traits_type::move(__p + __pos, __p + __pos + __n, __n_move);
2760 __sz -= __n;
2761 __set_size(__sz);
2762 __invalidate_iterators_past(__sz);
2763 traits_type::assign(__p[__sz], value_type());
2764 }
2765 return *this;
2766}
2767
2768template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002769inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002770typename basic_string<_CharT, _Traits, _Allocator>::iterator
2771basic_string<_CharT, _Traits, _Allocator>::erase(const_iterator __pos)
2772{
Howard Hinnant499cea12013-08-23 17:37:05 +00002773#if _LIBCPP_DEBUG_LEVEL >= 2
2774 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2775 "string::erase(iterator) called with an iterator not"
2776 " referring to this string");
2777#endif
2778 _LIBCPP_ASSERT(__pos != end(),
2779 "string::erase(iterator) called with a non-dereferenceable iterator");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002780 iterator __b = begin();
2781 size_type __r = static_cast<size_type>(__pos - __b);
2782 erase(__r, 1);
Howard Hinnantec3773c2011-12-01 20:21:04 +00002783 return __b + static_cast<difference_type>(__r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002784}
2785
2786template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002787inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002788typename basic_string<_CharT, _Traits, _Allocator>::iterator
2789basic_string<_CharT, _Traits, _Allocator>::erase(const_iterator __first, const_iterator __last)
2790{
Howard Hinnant499cea12013-08-23 17:37:05 +00002791#if _LIBCPP_DEBUG_LEVEL >= 2
2792 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__first) == this,
2793 "string::erase(iterator, iterator) called with an iterator not"
2794 " referring to this string");
2795#endif
2796 _LIBCPP_ASSERT(__first <= __last, "string::erase(first, last) called with invalid range");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002797 iterator __b = begin();
2798 size_type __r = static_cast<size_type>(__first - __b);
2799 erase(__r, static_cast<size_type>(__last - __first));
Howard Hinnantec3773c2011-12-01 20:21:04 +00002800 return __b + static_cast<difference_type>(__r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002801}
2802
2803template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002804inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002805void
2806basic_string<_CharT, _Traits, _Allocator>::pop_back()
2807{
Howard Hinnant499cea12013-08-23 17:37:05 +00002808 _LIBCPP_ASSERT(!empty(), "string::pop_back(): string is already empty");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002809 size_type __sz;
2810 if (__is_long())
2811 {
2812 __sz = __get_long_size() - 1;
2813 __set_long_size(__sz);
2814 traits_type::assign(*(__get_long_pointer() + __sz), value_type());
2815 }
2816 else
2817 {
2818 __sz = __get_short_size() - 1;
2819 __set_short_size(__sz);
2820 traits_type::assign(*(__get_short_pointer() + __sz), value_type());
2821 }
2822 __invalidate_iterators_past(__sz);
2823}
2824
2825template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002826inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002827void
Howard Hinnanta6119a82011-05-29 19:57:12 +00002828basic_string<_CharT, _Traits, _Allocator>::clear() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002829{
2830 __invalidate_all_iterators();
2831 if (__is_long())
2832 {
2833 traits_type::assign(*__get_long_pointer(), value_type());
2834 __set_long_size(0);
2835 }
2836 else
2837 {
2838 traits_type::assign(*__get_short_pointer(), value_type());
2839 __set_short_size(0);
2840 }
2841}
2842
2843template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002844inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002845void
2846basic_string<_CharT, _Traits, _Allocator>::__erase_to_end(size_type __pos)
2847{
2848 if (__is_long())
2849 {
2850 traits_type::assign(*(__get_long_pointer() + __pos), value_type());
2851 __set_long_size(__pos);
2852 }
2853 else
2854 {
2855 traits_type::assign(*(__get_short_pointer() + __pos), value_type());
2856 __set_short_size(__pos);
2857 }
2858 __invalidate_iterators_past(__pos);
2859}
2860
2861template <class _CharT, class _Traits, class _Allocator>
2862void
2863basic_string<_CharT, _Traits, _Allocator>::resize(size_type __n, value_type __c)
2864{
2865 size_type __sz = size();
2866 if (__n > __sz)
2867 append(__n - __sz, __c);
2868 else
2869 __erase_to_end(__n);
2870}
2871
2872template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002873inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002874typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnanta6119a82011-05-29 19:57:12 +00002875basic_string<_CharT, _Traits, _Allocator>::max_size() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002876{
Howard Hinnante32b5e22010-11-17 17:55:08 +00002877 size_type __m = __alloc_traits::max_size(__alloc());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002878#if _LIBCPP_BIG_ENDIAN
Marshall Clow09f85502013-10-31 17:23:08 +00002879 return (__m <= ~__long_mask ? __m : __m/2) - __alignment;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002880#else
Marshall Clow09f85502013-10-31 17:23:08 +00002881 return __m - __alignment;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002882#endif
2883}
2884
2885template <class _CharT, class _Traits, class _Allocator>
2886void
2887basic_string<_CharT, _Traits, _Allocator>::reserve(size_type __res_arg)
2888{
2889 if (__res_arg > max_size())
2890 this->__throw_length_error();
2891 size_type __cap = capacity();
2892 size_type __sz = size();
Howard Hinnant0949eed2011-06-30 21:18:19 +00002893 __res_arg = _VSTD::max(__res_arg, __sz);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002894 __res_arg = __recommend(__res_arg);
2895 if (__res_arg != __cap)
2896 {
2897 pointer __new_data, __p;
2898 bool __was_long, __now_long;
2899 if (__res_arg == __min_cap - 1)
2900 {
2901 __was_long = true;
2902 __now_long = false;
2903 __new_data = __get_short_pointer();
2904 __p = __get_long_pointer();
2905 }
2906 else
2907 {
2908 if (__res_arg > __cap)
Howard Hinnante32b5e22010-11-17 17:55:08 +00002909 __new_data = __alloc_traits::allocate(__alloc(), __res_arg+1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002910 else
2911 {
2912 #ifndef _LIBCPP_NO_EXCEPTIONS
2913 try
2914 {
Howard Hinnant324bb032010-08-22 00:02:43 +00002915 #endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnante32b5e22010-11-17 17:55:08 +00002916 __new_data = __alloc_traits::allocate(__alloc(), __res_arg+1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002917 #ifndef _LIBCPP_NO_EXCEPTIONS
2918 }
2919 catch (...)
2920 {
2921 return;
2922 }
Howard Hinnant324bb032010-08-22 00:02:43 +00002923 #else // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002924 if (__new_data == nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002925 return;
Howard Hinnant324bb032010-08-22 00:02:43 +00002926 #endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002927 }
2928 __now_long = true;
2929 __was_long = __is_long();
2930 __p = __get_pointer();
2931 }
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002932 traits_type::copy(_VSTD::__to_raw_pointer(__new_data),
2933 _VSTD::__to_raw_pointer(__p), size()+1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002934 if (__was_long)
Howard Hinnante32b5e22010-11-17 17:55:08 +00002935 __alloc_traits::deallocate(__alloc(), __p, __cap+1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002936 if (__now_long)
2937 {
2938 __set_long_cap(__res_arg+1);
2939 __set_long_size(__sz);
2940 __set_long_pointer(__new_data);
2941 }
2942 else
2943 __set_short_size(__sz);
2944 __invalidate_all_iterators();
2945 }
2946}
2947
2948template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002949inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002950typename basic_string<_CharT, _Traits, _Allocator>::const_reference
Marshall Clow1e00d6d2016-07-21 05:31:24 +00002951basic_string<_CharT, _Traits, _Allocator>::operator[](size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002952{
Howard Hinnant499cea12013-08-23 17:37:05 +00002953 _LIBCPP_ASSERT(__pos <= size(), "string index out of bounds");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002954 return *(data() + __pos);
2955}
2956
2957template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002958inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002959typename basic_string<_CharT, _Traits, _Allocator>::reference
Marshall Clow1e00d6d2016-07-21 05:31:24 +00002960basic_string<_CharT, _Traits, _Allocator>::operator[](size_type __pos) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002961{
Howard Hinnant499cea12013-08-23 17:37:05 +00002962 _LIBCPP_ASSERT(__pos <= size(), "string index out of bounds");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002963 return *(__get_pointer() + __pos);
2964}
2965
2966template <class _CharT, class _Traits, class _Allocator>
2967typename basic_string<_CharT, _Traits, _Allocator>::const_reference
2968basic_string<_CharT, _Traits, _Allocator>::at(size_type __n) const
2969{
2970 if (__n >= size())
2971 this->__throw_out_of_range();
2972 return (*this)[__n];
2973}
2974
2975template <class _CharT, class _Traits, class _Allocator>
2976typename basic_string<_CharT, _Traits, _Allocator>::reference
2977basic_string<_CharT, _Traits, _Allocator>::at(size_type __n)
2978{
2979 if (__n >= size())
2980 this->__throw_out_of_range();
2981 return (*this)[__n];
2982}
2983
2984template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002985inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002986typename basic_string<_CharT, _Traits, _Allocator>::reference
2987basic_string<_CharT, _Traits, _Allocator>::front()
2988{
Howard Hinnant499cea12013-08-23 17:37:05 +00002989 _LIBCPP_ASSERT(!empty(), "string::front(): string is empty");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002990 return *__get_pointer();
2991}
2992
2993template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002994inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002995typename basic_string<_CharT, _Traits, _Allocator>::const_reference
2996basic_string<_CharT, _Traits, _Allocator>::front() const
2997{
Howard Hinnant499cea12013-08-23 17:37:05 +00002998 _LIBCPP_ASSERT(!empty(), "string::front(): string is empty");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002999 return *data();
3000}
3001
3002template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003003inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003004typename basic_string<_CharT, _Traits, _Allocator>::reference
3005basic_string<_CharT, _Traits, _Allocator>::back()
3006{
Howard Hinnant499cea12013-08-23 17:37:05 +00003007 _LIBCPP_ASSERT(!empty(), "string::back(): string is empty");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003008 return *(__get_pointer() + size() - 1);
3009}
3010
3011template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003012inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003013typename basic_string<_CharT, _Traits, _Allocator>::const_reference
3014basic_string<_CharT, _Traits, _Allocator>::back() const
3015{
Howard Hinnant499cea12013-08-23 17:37:05 +00003016 _LIBCPP_ASSERT(!empty(), "string::back(): string is empty");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003017 return *(data() + size() - 1);
3018}
3019
3020template <class _CharT, class _Traits, class _Allocator>
3021typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00003022basic_string<_CharT, _Traits, _Allocator>::copy(value_type* __s, size_type __n, size_type __pos) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003023{
3024 size_type __sz = size();
3025 if (__pos > __sz)
3026 this->__throw_out_of_range();
Howard Hinnant0949eed2011-06-30 21:18:19 +00003027 size_type __rlen = _VSTD::min(__n, __sz - __pos);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003028 traits_type::copy(__s, data() + __pos, __rlen);
3029 return __rlen;
3030}
3031
3032template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003033inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003034basic_string<_CharT, _Traits, _Allocator>
3035basic_string<_CharT, _Traits, _Allocator>::substr(size_type __pos, size_type __n) const
3036{
3037 return basic_string(*this, __pos, __n, __alloc());
3038}
3039
3040template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003041inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003042void
3043basic_string<_CharT, _Traits, _Allocator>::swap(basic_string& __str)
Marshall Clow7d914d12015-07-13 20:04:56 +00003044#if _LIBCPP_STD_VER >= 14
Eric Fiselier47257c42016-12-28 05:53:01 +00003045 _NOEXCEPT_DEBUG
Marshall Clow7d914d12015-07-13 20:04:56 +00003046#else
Eric Fiselier47257c42016-12-28 05:53:01 +00003047 _NOEXCEPT_DEBUG_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow7d914d12015-07-13 20:04:56 +00003048 __is_nothrow_swappable<allocator_type>::value)
3049#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003050{
Howard Hinnant499cea12013-08-23 17:37:05 +00003051#if _LIBCPP_DEBUG_LEVEL >= 2
3052 if (!__is_long())
3053 __get_db()->__invalidate_all(this);
3054 if (!__str.__is_long())
3055 __get_db()->__invalidate_all(&__str);
3056 __get_db()->swap(this, &__str);
3057#endif
Eric Fiselier47257c42016-12-28 05:53:01 +00003058 _LIBCPP_ASSERT(
3059 __alloc_traits::propagate_on_container_swap::value ||
3060 __alloc_traits::is_always_equal::value ||
3061 __alloc() == __str.__alloc(), "swapping non-equal allocators");
Howard Hinnant0949eed2011-06-30 21:18:19 +00003062 _VSTD::swap(__r_.first(), __str.__r_.first());
Marshall Clow7d914d12015-07-13 20:04:56 +00003063 __swap_allocator(__alloc(), __str.__alloc());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003064}
3065
3066// find
3067
3068template <class _Traits>
3069struct _LIBCPP_HIDDEN __traits_eq
3070{
3071 typedef typename _Traits::char_type char_type;
Howard Hinnanta6119a82011-05-29 19:57:12 +00003072 _LIBCPP_INLINE_VISIBILITY
3073 bool operator()(const char_type& __x, const char_type& __y) _NOEXCEPT
3074 {return _Traits::eq(__x, __y);}
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 Hinnant9dcdcde2013-06-28 16:59:19 +00003079basic_string<_CharT, _Traits, _Allocator>::find(const value_type* __s,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003080 size_type __pos,
3081 size_type __n) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003082{
Alp Tokerec34c482014-05-15 11:27:39 +00003083 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find(): received nullptr");
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003084 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clow360f3192014-06-02 02:22:49 +00003085 (data(), size(), __s, __pos, __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003086}
3087
3088template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003089inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003090typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnanta6119a82011-05-29 19:57:12 +00003091basic_string<_CharT, _Traits, _Allocator>::find(const basic_string& __str,
3092 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003093{
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003094 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clow360f3192014-06-02 02:22:49 +00003095 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003096}
3097
3098template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003099inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003100typename basic_string<_CharT, _Traits, _Allocator>::size_type
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003101basic_string<_CharT, _Traits, _Allocator>::find(__self_view __sv,
3102 size_type __pos) const _NOEXCEPT
3103{
3104 return __str_find<value_type, size_type, traits_type, npos>
3105 (data(), size(), __sv.data(), __pos, __sv.size());
3106}
3107
3108template<class _CharT, class _Traits, class _Allocator>
3109inline _LIBCPP_INLINE_VISIBILITY
3110typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00003111basic_string<_CharT, _Traits, _Allocator>::find(const value_type* __s,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003112 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003113{
Alp Tokerec34c482014-05-15 11:27:39 +00003114 _LIBCPP_ASSERT(__s != nullptr, "string::find(): received nullptr");
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003115 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clow360f3192014-06-02 02:22:49 +00003116 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003117}
3118
3119template<class _CharT, class _Traits, class _Allocator>
3120typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnanta6119a82011-05-29 19:57:12 +00003121basic_string<_CharT, _Traits, _Allocator>::find(value_type __c,
3122 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003123{
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003124 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clow360f3192014-06-02 02:22:49 +00003125 (data(), size(), __c, __pos);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003126}
3127
3128// rfind
3129
3130template<class _CharT, class _Traits, class _Allocator>
3131typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00003132basic_string<_CharT, _Traits, _Allocator>::rfind(const value_type* __s,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003133 size_type __pos,
3134 size_type __n) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003135{
Alp Tokerec34c482014-05-15 11:27:39 +00003136 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::rfind(): received nullptr");
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003137 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clow360f3192014-06-02 02:22:49 +00003138 (data(), size(), __s, __pos, __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003139}
3140
3141template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003142inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003143typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnanta6119a82011-05-29 19:57:12 +00003144basic_string<_CharT, _Traits, _Allocator>::rfind(const basic_string& __str,
3145 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003146{
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003147 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clow360f3192014-06-02 02:22:49 +00003148 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003149}
3150
3151template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003152inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003153typename basic_string<_CharT, _Traits, _Allocator>::size_type
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003154basic_string<_CharT, _Traits, _Allocator>::rfind(__self_view __sv,
3155 size_type __pos) const _NOEXCEPT
3156{
3157 return __str_rfind<value_type, size_type, traits_type, npos>
3158 (data(), size(), __sv.data(), __pos, __sv.size());
3159}
3160
3161template<class _CharT, class _Traits, class _Allocator>
3162inline _LIBCPP_INLINE_VISIBILITY
3163typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00003164basic_string<_CharT, _Traits, _Allocator>::rfind(const value_type* __s,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003165 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003166{
Alp Tokerec34c482014-05-15 11:27:39 +00003167 _LIBCPP_ASSERT(__s != nullptr, "string::rfind(): received nullptr");
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003168 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clow360f3192014-06-02 02:22:49 +00003169 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003170}
3171
3172template<class _CharT, class _Traits, class _Allocator>
3173typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnanta6119a82011-05-29 19:57:12 +00003174basic_string<_CharT, _Traits, _Allocator>::rfind(value_type __c,
3175 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003176{
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003177 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clow360f3192014-06-02 02:22:49 +00003178 (data(), size(), __c, __pos);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003179}
3180
3181// find_first_of
3182
3183template<class _CharT, class _Traits, class _Allocator>
3184typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00003185basic_string<_CharT, _Traits, _Allocator>::find_first_of(const value_type* __s,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003186 size_type __pos,
3187 size_type __n) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003188{
Alp Tokerec34c482014-05-15 11:27:39 +00003189 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_first_of(): received nullptr");
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003190 return __str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow8eb5acc2014-02-16 01:57:26 +00003191 (data(), size(), __s, __pos, __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003192}
3193
3194template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003195inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003196typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnanta6119a82011-05-29 19:57:12 +00003197basic_string<_CharT, _Traits, _Allocator>::find_first_of(const basic_string& __str,
3198 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003199{
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003200 return __str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow8eb5acc2014-02-16 01:57:26 +00003201 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003202}
3203
3204template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003205inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003206typename basic_string<_CharT, _Traits, _Allocator>::size_type
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003207basic_string<_CharT, _Traits, _Allocator>::find_first_of(__self_view __sv,
3208 size_type __pos) const _NOEXCEPT
3209{
3210 return __str_find_first_of<value_type, size_type, traits_type, npos>
3211 (data(), size(), __sv.data(), __pos, __sv.size());
3212}
3213
3214template<class _CharT, class _Traits, class _Allocator>
3215inline _LIBCPP_INLINE_VISIBILITY
3216typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00003217basic_string<_CharT, _Traits, _Allocator>::find_first_of(const value_type* __s,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003218 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003219{
Alp Tokerec34c482014-05-15 11:27:39 +00003220 _LIBCPP_ASSERT(__s != nullptr, "string::find_first_of(): received nullptr");
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003221 return __str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow8eb5acc2014-02-16 01:57:26 +00003222 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003223}
3224
3225template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003226inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003227typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnanta6119a82011-05-29 19:57:12 +00003228basic_string<_CharT, _Traits, _Allocator>::find_first_of(value_type __c,
3229 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003230{
3231 return find(__c, __pos);
3232}
3233
3234// find_last_of
3235
3236template<class _CharT, class _Traits, class _Allocator>
3237typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00003238basic_string<_CharT, _Traits, _Allocator>::find_last_of(const value_type* __s,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003239 size_type __pos,
3240 size_type __n) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003241{
Alp Tokerec34c482014-05-15 11:27:39 +00003242 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_last_of(): received nullptr");
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003243 return __str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow8eb5acc2014-02-16 01:57:26 +00003244 (data(), size(), __s, __pos, __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003245}
3246
3247template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003248inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003249typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnanta6119a82011-05-29 19:57:12 +00003250basic_string<_CharT, _Traits, _Allocator>::find_last_of(const basic_string& __str,
3251 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003252{
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003253 return __str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow8eb5acc2014-02-16 01:57:26 +00003254 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003255}
3256
3257template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003258inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003259typename basic_string<_CharT, _Traits, _Allocator>::size_type
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003260basic_string<_CharT, _Traits, _Allocator>::find_last_of(__self_view __sv,
3261 size_type __pos) const _NOEXCEPT
3262{
3263 return __str_find_last_of<value_type, size_type, traits_type, npos>
3264 (data(), size(), __sv.data(), __pos, __sv.size());
3265}
3266
3267template<class _CharT, class _Traits, class _Allocator>
3268inline _LIBCPP_INLINE_VISIBILITY
3269typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00003270basic_string<_CharT, _Traits, _Allocator>::find_last_of(const value_type* __s,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003271 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003272{
Alp Tokerec34c482014-05-15 11:27:39 +00003273 _LIBCPP_ASSERT(__s != nullptr, "string::find_last_of(): received nullptr");
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003274 return __str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow8eb5acc2014-02-16 01:57:26 +00003275 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003276}
3277
3278template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003279inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003280typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnanta6119a82011-05-29 19:57:12 +00003281basic_string<_CharT, _Traits, _Allocator>::find_last_of(value_type __c,
3282 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003283{
3284 return rfind(__c, __pos);
3285}
3286
3287// find_first_not_of
3288
3289template<class _CharT, class _Traits, class _Allocator>
3290typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00003291basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const value_type* __s,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003292 size_type __pos,
3293 size_type __n) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003294{
Alp Tokerec34c482014-05-15 11:27:39 +00003295 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_first_not_of(): received nullptr");
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003296 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow8eb5acc2014-02-16 01:57:26 +00003297 (data(), size(), __s, __pos, __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003298}
3299
3300template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003301inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003302typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnanta6119a82011-05-29 19:57:12 +00003303basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const basic_string& __str,
3304 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003305{
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003306 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow8eb5acc2014-02-16 01:57:26 +00003307 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003308}
3309
3310template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003311inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003312typename basic_string<_CharT, _Traits, _Allocator>::size_type
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003313basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(__self_view __sv,
3314 size_type __pos) const _NOEXCEPT
3315{
3316 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
3317 (data(), size(), __sv.data(), __pos, __sv.size());
3318}
3319
3320template<class _CharT, class _Traits, class _Allocator>
3321inline _LIBCPP_INLINE_VISIBILITY
3322typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00003323basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const value_type* __s,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003324 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003325{
Alp Tokerec34c482014-05-15 11:27:39 +00003326 _LIBCPP_ASSERT(__s != nullptr, "string::find_first_not_of(): received nullptr");
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003327 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow8eb5acc2014-02-16 01:57:26 +00003328 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003329}
3330
3331template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003332inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003333typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnanta6119a82011-05-29 19:57:12 +00003334basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(value_type __c,
3335 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003336{
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003337 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow8eb5acc2014-02-16 01:57:26 +00003338 (data(), size(), __c, __pos);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003339}
3340
3341// find_last_not_of
3342
3343template<class _CharT, class _Traits, class _Allocator>
3344typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00003345basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const value_type* __s,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003346 size_type __pos,
3347 size_type __n) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003348{
Alp Tokerec34c482014-05-15 11:27:39 +00003349 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_last_not_of(): received nullptr");
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003350 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow8eb5acc2014-02-16 01:57:26 +00003351 (data(), size(), __s, __pos, __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003352}
3353
3354template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003355inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003356typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnanta6119a82011-05-29 19:57:12 +00003357basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const basic_string& __str,
3358 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003359{
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003360 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow8eb5acc2014-02-16 01:57:26 +00003361 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003362}
3363
3364template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003365inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003366typename basic_string<_CharT, _Traits, _Allocator>::size_type
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003367basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(__self_view __sv,
3368 size_type __pos) const _NOEXCEPT
3369{
3370 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
3371 (data(), size(), __sv.data(), __pos, __sv.size());
3372}
3373
3374template<class _CharT, class _Traits, class _Allocator>
3375inline _LIBCPP_INLINE_VISIBILITY
3376typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00003377basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const value_type* __s,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003378 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003379{
Alp Tokerec34c482014-05-15 11:27:39 +00003380 _LIBCPP_ASSERT(__s != nullptr, "string::find_last_not_of(): received nullptr");
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003381 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow8eb5acc2014-02-16 01:57:26 +00003382 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003383}
3384
3385template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003386inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003387typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnanta6119a82011-05-29 19:57:12 +00003388basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(value_type __c,
3389 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003390{
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003391 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow8eb5acc2014-02-16 01:57:26 +00003392 (data(), size(), __c, __pos);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003393}
3394
3395// compare
3396
3397template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003398inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003399int
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003400basic_string<_CharT, _Traits, _Allocator>::compare(__self_view __sv) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003401{
Howard Hinnantfa06d752011-07-24 21:45:06 +00003402 size_t __lhs_sz = size();
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003403 size_t __rhs_sz = __sv.size();
3404 int __result = traits_type::compare(data(), __sv.data(),
Howard Hinnantfa06d752011-07-24 21:45:06 +00003405 _VSTD::min(__lhs_sz, __rhs_sz));
3406 if (__result != 0)
3407 return __result;
3408 if (__lhs_sz < __rhs_sz)
3409 return -1;
3410 if (__lhs_sz > __rhs_sz)
3411 return 1;
3412 return 0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003413}
3414
3415template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003416inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003417int
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003418basic_string<_CharT, _Traits, _Allocator>::compare(const basic_string& __str) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003419{
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003420 return compare(__self_view(__str));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003421}
3422
3423template <class _CharT, class _Traits, class _Allocator>
3424int
Howard Hinnanta6119a82011-05-29 19:57:12 +00003425basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3426 size_type __n1,
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00003427 const value_type* __s,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003428 size_type __n2) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003429{
Alp Tokerec34c482014-05-15 11:27:39 +00003430 _LIBCPP_ASSERT(__n2 == 0 || __s != nullptr, "string::compare(): received nullptr");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003431 size_type __sz = size();
3432 if (__pos1 > __sz || __n2 == npos)
3433 this->__throw_out_of_range();
Howard Hinnant0949eed2011-06-30 21:18:19 +00003434 size_type __rlen = _VSTD::min(__n1, __sz - __pos1);
3435 int __r = traits_type::compare(data() + __pos1, __s, _VSTD::min(__rlen, __n2));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003436 if (__r == 0)
3437 {
3438 if (__rlen < __n2)
3439 __r = -1;
3440 else if (__rlen > __n2)
3441 __r = 1;
3442 }
3443 return __r;
3444}
3445
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003446template <class _CharT, class _Traits, class _Allocator>
3447inline _LIBCPP_INLINE_VISIBILITY
3448int
3449basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3450 size_type __n1,
3451 __self_view __sv) const
3452{
3453 return compare(__pos1, __n1, __sv.data(), __sv.size());
3454}
3455
3456template <class _CharT, class _Traits, class _Allocator>
3457inline _LIBCPP_INLINE_VISIBILITY
3458int
3459basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3460 size_type __n1,
3461 const basic_string& __str) const
3462{
3463 return compare(__pos1, __n1, __str.data(), __str.size());
3464}
3465
3466template <class _CharT, class _Traits, class _Allocator>
Marshall Clow6ac8de02016-09-24 22:45:42 +00003467template <class _Tp>
3468typename enable_if
3469<
3470 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3471 int
3472>::type
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003473basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3474 size_type __n1,
Marshall Clow6ac8de02016-09-24 22:45:42 +00003475 const _Tp& __t,
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003476 size_type __pos2,
3477 size_type __n2) const
3478{
Marshall Clow6ac8de02016-09-24 22:45:42 +00003479 __self_view __sv = __t;
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003480 return __self_view(*this).substr(__pos1, __n1).compare(__sv.substr(__pos2, __n2));
3481}
3482
3483template <class _CharT, class _Traits, class _Allocator>
3484int
3485basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3486 size_type __n1,
3487 const basic_string& __str,
3488 size_type __pos2,
3489 size_type __n2) const
3490{
3491 return compare(__pos1, __n1, __self_view(__str), __pos2, __n2);
3492}
3493
3494template <class _CharT, class _Traits, class _Allocator>
3495int
3496basic_string<_CharT, _Traits, _Allocator>::compare(const value_type* __s) const _NOEXCEPT
3497{
3498 _LIBCPP_ASSERT(__s != nullptr, "string::compare(): received nullptr");
3499 return compare(0, npos, __s, traits_type::length(__s));
3500}
3501
3502template <class _CharT, class _Traits, class _Allocator>
3503int
3504basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3505 size_type __n1,
3506 const value_type* __s) const
3507{
3508 _LIBCPP_ASSERT(__s != nullptr, "string::compare(): received nullptr");
3509 return compare(__pos1, __n1, __s, traits_type::length(__s));
3510}
3511
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003512// __invariants
3513
3514template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003515inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003516bool
3517basic_string<_CharT, _Traits, _Allocator>::__invariants() const
3518{
3519 if (size() > capacity())
3520 return false;
3521 if (capacity() < __min_cap - 1)
3522 return false;
3523 if (data() == 0)
3524 return false;
3525 if (data()[size()] != value_type(0))
3526 return false;
3527 return true;
3528}
3529
3530// operator==
3531
3532template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003533inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003534bool
3535operator==(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003536 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003537{
Howard Hinnant08dd2532013-04-22 23:55:13 +00003538 size_t __lhs_sz = __lhs.size();
3539 return __lhs_sz == __rhs.size() && _Traits::compare(__lhs.data(),
3540 __rhs.data(),
3541 __lhs_sz) == 0;
3542}
3543
3544template<class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003545inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant08dd2532013-04-22 23:55:13 +00003546bool
3547operator==(const basic_string<char, char_traits<char>, _Allocator>& __lhs,
3548 const basic_string<char, char_traits<char>, _Allocator>& __rhs) _NOEXCEPT
3549{
3550 size_t __lhs_sz = __lhs.size();
3551 if (__lhs_sz != __rhs.size())
3552 return false;
3553 const char* __lp = __lhs.data();
3554 const char* __rp = __rhs.data();
3555 if (__lhs.__is_long())
3556 return char_traits<char>::compare(__lp, __rp, __lhs_sz) == 0;
3557 for (; __lhs_sz != 0; --__lhs_sz, ++__lp, ++__rp)
3558 if (*__lp != *__rp)
3559 return false;
3560 return true;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003561}
3562
3563template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003564inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003565bool
Howard Hinnanta6119a82011-05-29 19:57:12 +00003566operator==(const _CharT* __lhs,
3567 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003568{
Eric Fiselier4f241822015-08-28 03:02:37 +00003569 typedef basic_string<_CharT, _Traits, _Allocator> _String;
3570 _LIBCPP_ASSERT(__lhs != nullptr, "operator==(char*, basic_string): received nullptr");
3571 size_t __lhs_len = _Traits::length(__lhs);
3572 if (__lhs_len != __rhs.size()) return false;
3573 return __rhs.compare(0, _String::npos, __lhs, __lhs_len) == 0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003574}
3575
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003576template<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
Howard Hinnanta6119a82011-05-29 19:57:12 +00003579operator==(const basic_string<_CharT,_Traits,_Allocator>& __lhs,
3580 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003581{
Eric Fiselier4f241822015-08-28 03:02:37 +00003582 typedef basic_string<_CharT, _Traits, _Allocator> _String;
3583 _LIBCPP_ASSERT(__rhs != nullptr, "operator==(basic_string, char*): received nullptr");
3584 size_t __rhs_len = _Traits::length(__rhs);
3585 if (__rhs_len != __lhs.size()) return false;
3586 return __lhs.compare(0, _String::npos, __rhs, __rhs_len) == 0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003587}
3588
Howard Hinnant324bb032010-08-22 00:02:43 +00003589template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003590inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003591bool
3592operator!=(const basic_string<_CharT,_Traits,_Allocator>& __lhs,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003593 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003594{
3595 return !(__lhs == __rhs);
3596}
3597
3598template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003599inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003600bool
Howard Hinnanta6119a82011-05-29 19:57:12 +00003601operator!=(const _CharT* __lhs,
3602 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003603{
3604 return !(__lhs == __rhs);
3605}
3606
3607template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003608inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003609bool
Howard Hinnanta6119a82011-05-29 19:57:12 +00003610operator!=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
3611 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003612{
3613 return !(__lhs == __rhs);
3614}
3615
3616// operator<
3617
3618template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003619inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003620bool
3621operator< (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003622 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003623{
Howard Hinnant2644a7b2011-07-24 15:07:21 +00003624 return __lhs.compare(__rhs) < 0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003625}
3626
3627template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003628inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003629bool
Howard Hinnanta6119a82011-05-29 19:57:12 +00003630operator< (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
3631 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003632{
Howard Hinnant2644a7b2011-07-24 15:07:21 +00003633 return __lhs.compare(__rhs) < 0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003634}
3635
3636template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003637inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003638bool
Howard Hinnanta6119a82011-05-29 19:57:12 +00003639operator< (const _CharT* __lhs,
3640 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003641{
3642 return __rhs.compare(__lhs) > 0;
3643}
3644
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003645// operator>
3646
3647template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003648inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003649bool
3650operator> (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003651 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003652{
3653 return __rhs < __lhs;
3654}
3655
3656template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003657inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003658bool
Howard Hinnanta6119a82011-05-29 19:57:12 +00003659operator> (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
3660 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003661{
3662 return __rhs < __lhs;
3663}
3664
3665template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003666inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003667bool
Howard Hinnanta6119a82011-05-29 19:57:12 +00003668operator> (const _CharT* __lhs,
3669 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003670{
3671 return __rhs < __lhs;
3672}
3673
3674// operator<=
3675
3676template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003677inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003678bool
3679operator<=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003680 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003681{
3682 return !(__rhs < __lhs);
3683}
3684
3685template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003686inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003687bool
Howard Hinnanta6119a82011-05-29 19:57:12 +00003688operator<=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
3689 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003690{
3691 return !(__rhs < __lhs);
3692}
3693
3694template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003695inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003696bool
Howard Hinnanta6119a82011-05-29 19:57:12 +00003697operator<=(const _CharT* __lhs,
3698 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003699{
3700 return !(__rhs < __lhs);
3701}
3702
3703// operator>=
3704
3705template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003706inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003707bool
3708operator>=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003709 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003710{
3711 return !(__lhs < __rhs);
3712}
3713
3714template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003715inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003716bool
Howard Hinnanta6119a82011-05-29 19:57:12 +00003717operator>=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
3718 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003719{
3720 return !(__lhs < __rhs);
3721}
3722
3723template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003724inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003725bool
Howard Hinnanta6119a82011-05-29 19:57:12 +00003726operator>=(const _CharT* __lhs,
3727 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003728{
3729 return !(__lhs < __rhs);
3730}
3731
3732// operator +
3733
3734template<class _CharT, class _Traits, class _Allocator>
3735basic_string<_CharT, _Traits, _Allocator>
3736operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
3737 const basic_string<_CharT, _Traits, _Allocator>& __rhs)
3738{
3739 basic_string<_CharT, _Traits, _Allocator> __r(__lhs.get_allocator());
3740 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = __lhs.size();
3741 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = __rhs.size();
3742 __r.__init(__lhs.data(), __lhs_sz, __lhs_sz + __rhs_sz);
3743 __r.append(__rhs.data(), __rhs_sz);
3744 return __r;
3745}
3746
3747template<class _CharT, class _Traits, class _Allocator>
3748basic_string<_CharT, _Traits, _Allocator>
3749operator+(const _CharT* __lhs , const basic_string<_CharT,_Traits,_Allocator>& __rhs)
3750{
3751 basic_string<_CharT, _Traits, _Allocator> __r(__rhs.get_allocator());
3752 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = _Traits::length(__lhs);
3753 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = __rhs.size();
3754 __r.__init(__lhs, __lhs_sz, __lhs_sz + __rhs_sz);
3755 __r.append(__rhs.data(), __rhs_sz);
3756 return __r;
3757}
3758
3759template<class _CharT, class _Traits, class _Allocator>
3760basic_string<_CharT, _Traits, _Allocator>
3761operator+(_CharT __lhs, const basic_string<_CharT,_Traits,_Allocator>& __rhs)
3762{
3763 basic_string<_CharT, _Traits, _Allocator> __r(__rhs.get_allocator());
3764 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = __rhs.size();
3765 __r.__init(&__lhs, 1, 1 + __rhs_sz);
3766 __r.append(__rhs.data(), __rhs_sz);
3767 return __r;
3768}
3769
3770template<class _CharT, class _Traits, class _Allocator>
3771basic_string<_CharT, _Traits, _Allocator>
3772operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, const _CharT* __rhs)
3773{
3774 basic_string<_CharT, _Traits, _Allocator> __r(__lhs.get_allocator());
3775 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = __lhs.size();
3776 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = _Traits::length(__rhs);
3777 __r.__init(__lhs.data(), __lhs_sz, __lhs_sz + __rhs_sz);
3778 __r.append(__rhs, __rhs_sz);
3779 return __r;
3780}
3781
3782template<class _CharT, class _Traits, class _Allocator>
3783basic_string<_CharT, _Traits, _Allocator>
3784operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, _CharT __rhs)
3785{
3786 basic_string<_CharT, _Traits, _Allocator> __r(__lhs.get_allocator());
3787 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = __lhs.size();
3788 __r.__init(__lhs.data(), __lhs_sz, __lhs_sz + 1);
3789 __r.push_back(__rhs);
3790 return __r;
3791}
3792
Eric Fiselier3e928972017-04-19 00:28:44 +00003793#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003794
3795template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003796inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003797basic_string<_CharT, _Traits, _Allocator>
3798operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, const basic_string<_CharT, _Traits, _Allocator>& __rhs)
3799{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003800 return _VSTD::move(__lhs.append(__rhs));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003801}
3802
3803template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003804inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003805basic_string<_CharT, _Traits, _Allocator>
3806operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, basic_string<_CharT, _Traits, _Allocator>&& __rhs)
3807{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003808 return _VSTD::move(__rhs.insert(0, __lhs));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003809}
3810
3811template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003812inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003813basic_string<_CharT, _Traits, _Allocator>
3814operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, basic_string<_CharT, _Traits, _Allocator>&& __rhs)
3815{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003816 return _VSTD::move(__lhs.append(__rhs));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003817}
3818
3819template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003820inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003821basic_string<_CharT, _Traits, _Allocator>
3822operator+(const _CharT* __lhs , basic_string<_CharT,_Traits,_Allocator>&& __rhs)
3823{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003824 return _VSTD::move(__rhs.insert(0, __lhs));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003825}
3826
3827template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003828inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003829basic_string<_CharT, _Traits, _Allocator>
3830operator+(_CharT __lhs, basic_string<_CharT,_Traits,_Allocator>&& __rhs)
3831{
3832 __rhs.insert(__rhs.begin(), __lhs);
Howard Hinnant0949eed2011-06-30 21:18:19 +00003833 return _VSTD::move(__rhs);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003834}
3835
3836template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003837inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003838basic_string<_CharT, _Traits, _Allocator>
3839operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, const _CharT* __rhs)
3840{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003841 return _VSTD::move(__lhs.append(__rhs));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003842}
3843
3844template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003845inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003846basic_string<_CharT, _Traits, _Allocator>
3847operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, _CharT __rhs)
3848{
3849 __lhs.push_back(__rhs);
Howard Hinnant0949eed2011-06-30 21:18:19 +00003850 return _VSTD::move(__lhs);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003851}
3852
Eric Fiselier3e928972017-04-19 00:28:44 +00003853#endif // _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003854
3855// swap
3856
3857template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003858inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003859void
Howard Hinnanta6119a82011-05-29 19:57:12 +00003860swap(basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnant53f7d4c2011-06-03 18:40:47 +00003861 basic_string<_CharT, _Traits, _Allocator>& __rhs)
3862 _NOEXCEPT_(_NOEXCEPT_(__lhs.swap(__rhs)))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003863{
3864 __lhs.swap(__rhs);
3865}
3866
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003867#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
3868
3869typedef basic_string<char16_t> u16string;
3870typedef basic_string<char32_t> u32string;
3871
Howard Hinnant324bb032010-08-22 00:02:43 +00003872#endif // _LIBCPP_HAS_NO_UNICODE_CHARS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003873
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003874_LIBCPP_FUNC_VIS int stoi (const string& __str, size_t* __idx = 0, int __base = 10);
3875_LIBCPP_FUNC_VIS long stol (const string& __str, size_t* __idx = 0, int __base = 10);
3876_LIBCPP_FUNC_VIS unsigned long stoul (const string& __str, size_t* __idx = 0, int __base = 10);
3877_LIBCPP_FUNC_VIS long long stoll (const string& __str, size_t* __idx = 0, int __base = 10);
3878_LIBCPP_FUNC_VIS unsigned long long stoull(const string& __str, size_t* __idx = 0, int __base = 10);
Howard Hinnanta6a062d2010-06-02 18:20:39 +00003879
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003880_LIBCPP_FUNC_VIS float stof (const string& __str, size_t* __idx = 0);
3881_LIBCPP_FUNC_VIS double stod (const string& __str, size_t* __idx = 0);
3882_LIBCPP_FUNC_VIS long double stold(const string& __str, size_t* __idx = 0);
Howard Hinnanta6a062d2010-06-02 18:20:39 +00003883
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003884_LIBCPP_FUNC_VIS string to_string(int __val);
3885_LIBCPP_FUNC_VIS string to_string(unsigned __val);
3886_LIBCPP_FUNC_VIS string to_string(long __val);
3887_LIBCPP_FUNC_VIS string to_string(unsigned long __val);
3888_LIBCPP_FUNC_VIS string to_string(long long __val);
3889_LIBCPP_FUNC_VIS string to_string(unsigned long long __val);
3890_LIBCPP_FUNC_VIS string to_string(float __val);
3891_LIBCPP_FUNC_VIS string to_string(double __val);
3892_LIBCPP_FUNC_VIS string to_string(long double __val);
Howard Hinnanta6a062d2010-06-02 18:20:39 +00003893
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003894_LIBCPP_FUNC_VIS int stoi (const wstring& __str, size_t* __idx = 0, int __base = 10);
3895_LIBCPP_FUNC_VIS long stol (const wstring& __str, size_t* __idx = 0, int __base = 10);
3896_LIBCPP_FUNC_VIS unsigned long stoul (const wstring& __str, size_t* __idx = 0, int __base = 10);
3897_LIBCPP_FUNC_VIS long long stoll (const wstring& __str, size_t* __idx = 0, int __base = 10);
3898_LIBCPP_FUNC_VIS unsigned long long stoull(const wstring& __str, size_t* __idx = 0, int __base = 10);
Howard Hinnanta6a062d2010-06-02 18:20:39 +00003899
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003900_LIBCPP_FUNC_VIS float stof (const wstring& __str, size_t* __idx = 0);
3901_LIBCPP_FUNC_VIS double stod (const wstring& __str, size_t* __idx = 0);
3902_LIBCPP_FUNC_VIS long double stold(const wstring& __str, size_t* __idx = 0);
Howard Hinnanta6a062d2010-06-02 18:20:39 +00003903
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003904_LIBCPP_FUNC_VIS wstring to_wstring(int __val);
3905_LIBCPP_FUNC_VIS wstring to_wstring(unsigned __val);
3906_LIBCPP_FUNC_VIS wstring to_wstring(long __val);
3907_LIBCPP_FUNC_VIS wstring to_wstring(unsigned long __val);
3908_LIBCPP_FUNC_VIS wstring to_wstring(long long __val);
3909_LIBCPP_FUNC_VIS wstring to_wstring(unsigned long long __val);
3910_LIBCPP_FUNC_VIS wstring to_wstring(float __val);
3911_LIBCPP_FUNC_VIS wstring to_wstring(double __val);
3912_LIBCPP_FUNC_VIS wstring to_wstring(long double __val);
Howard Hinnanta6a062d2010-06-02 18:20:39 +00003913
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003914template<class _CharT, class _Traits, class _Allocator>
3915 const typename basic_string<_CharT, _Traits, _Allocator>::size_type
3916 basic_string<_CharT, _Traits, _Allocator>::npos;
3917
3918template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierc3589a82017-01-04 23:56:00 +00003919struct _LIBCPP_TEMPLATE_VIS hash<basic_string<_CharT, _Traits, _Allocator> >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003920 : public unary_function<basic_string<_CharT, _Traits, _Allocator>, size_t>
3921{
3922 size_t
Howard Hinnanta6119a82011-05-29 19:57:12 +00003923 operator()(const basic_string<_CharT, _Traits, _Allocator>& __val) const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003924};
3925
3926template<class _CharT, class _Traits, class _Allocator>
3927size_t
3928hash<basic_string<_CharT, _Traits, _Allocator> >::operator()(
Howard Hinnanta6119a82011-05-29 19:57:12 +00003929 const basic_string<_CharT, _Traits, _Allocator>& __val) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003930{
Sean Huntaffd9e52011-07-29 23:31:56 +00003931 return __do_string_hash(__val.data(), __val.data() + __val.size());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003932}
3933
Howard Hinnant464aa5c2011-07-18 15:51:59 +00003934template<class _CharT, class _Traits, class _Allocator>
3935basic_ostream<_CharT, _Traits>&
3936operator<<(basic_ostream<_CharT, _Traits>& __os,
3937 const basic_string<_CharT, _Traits, _Allocator>& __str);
3938
3939template<class _CharT, class _Traits, class _Allocator>
3940basic_istream<_CharT, _Traits>&
3941operator>>(basic_istream<_CharT, _Traits>& __is,
3942 basic_string<_CharT, _Traits, _Allocator>& __str);
3943
3944template<class _CharT, class _Traits, class _Allocator>
3945basic_istream<_CharT, _Traits>&
3946getline(basic_istream<_CharT, _Traits>& __is,
3947 basic_string<_CharT, _Traits, _Allocator>& __str, _CharT __dlm);
3948
3949template<class _CharT, class _Traits, class _Allocator>
3950inline _LIBCPP_INLINE_VISIBILITY
3951basic_istream<_CharT, _Traits>&
3952getline(basic_istream<_CharT, _Traits>& __is,
3953 basic_string<_CharT, _Traits, _Allocator>& __str);
3954
Eric Fiselier3e928972017-04-19 00:28:44 +00003955#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant464aa5c2011-07-18 15:51:59 +00003956
3957template<class _CharT, class _Traits, class _Allocator>
3958inline _LIBCPP_INLINE_VISIBILITY
3959basic_istream<_CharT, _Traits>&
3960getline(basic_istream<_CharT, _Traits>&& __is,
3961 basic_string<_CharT, _Traits, _Allocator>& __str, _CharT __dlm);
3962
3963template<class _CharT, class _Traits, class _Allocator>
3964inline _LIBCPP_INLINE_VISIBILITY
3965basic_istream<_CharT, _Traits>&
3966getline(basic_istream<_CharT, _Traits>&& __is,
3967 basic_string<_CharT, _Traits, _Allocator>& __str);
3968
Eric Fiselier3e928972017-04-19 00:28:44 +00003969#endif // _LIBCPP_CXX03_LANG
Howard Hinnant464aa5c2011-07-18 15:51:59 +00003970
Howard Hinnant499cea12013-08-23 17:37:05 +00003971#if _LIBCPP_DEBUG_LEVEL >= 2
3972
3973template<class _CharT, class _Traits, class _Allocator>
3974bool
3975basic_string<_CharT, _Traits, _Allocator>::__dereferenceable(const const_iterator* __i) const
3976{
3977 return this->data() <= _VSTD::__to_raw_pointer(__i->base()) &&
3978 _VSTD::__to_raw_pointer(__i->base()) < this->data() + this->size();
3979}
3980
3981template<class _CharT, class _Traits, class _Allocator>
3982bool
3983basic_string<_CharT, _Traits, _Allocator>::__decrementable(const const_iterator* __i) const
3984{
3985 return this->data() < _VSTD::__to_raw_pointer(__i->base()) &&
3986 _VSTD::__to_raw_pointer(__i->base()) <= this->data() + this->size();
3987}
3988
3989template<class _CharT, class _Traits, class _Allocator>
3990bool
3991basic_string<_CharT, _Traits, _Allocator>::__addable(const const_iterator* __i, ptrdiff_t __n) const
3992{
3993 const value_type* __p = _VSTD::__to_raw_pointer(__i->base()) + __n;
3994 return this->data() <= __p && __p <= this->data() + this->size();
3995}
3996
3997template<class _CharT, class _Traits, class _Allocator>
3998bool
3999basic_string<_CharT, _Traits, _Allocator>::__subscriptable(const const_iterator* __i, ptrdiff_t __n) const
4000{
4001 const value_type* __p = _VSTD::__to_raw_pointer(__i->base()) + __n;
4002 return this->data() <= __p && __p < this->data() + this->size();
4003}
4004
4005#endif // _LIBCPP_DEBUG_LEVEL >= 2
4006
Shoaib Meenai68506702017-06-29 02:52:46 +00004007_LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_string<char>)
4008_LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_string<wchar_t>)
4009_LIBCPP_EXTERN_TEMPLATE(string operator+<char, char_traits<char>, allocator<char> >(char const*, string const&))
4010
Marshall Clow15234322013-07-23 17:05:24 +00004011#if _LIBCPP_STD_VER > 11
4012// Literal suffixes for basic_string [basic.string.literals]
Marshall Clow8d9dd7a2013-10-05 21:18:32 +00004013inline namespace literals
Marshall Clow15234322013-07-23 17:05:24 +00004014{
4015 inline namespace string_literals
4016 {
Howard Hinnantab61b2c2013-08-07 19:39:48 +00004017 inline _LIBCPP_INLINE_VISIBILITY
4018 basic_string<char> operator "" s( const char *__str, size_t __len )
4019 {
4020 return basic_string<char> (__str, __len);
4021 }
Marshall Clow15234322013-07-23 17:05:24 +00004022
Howard Hinnantab61b2c2013-08-07 19:39:48 +00004023 inline _LIBCPP_INLINE_VISIBILITY
4024 basic_string<wchar_t> operator "" s( const wchar_t *__str, size_t __len )
4025 {
4026 return basic_string<wchar_t> (__str, __len);
4027 }
Marshall Clow15234322013-07-23 17:05:24 +00004028
Howard Hinnantab61b2c2013-08-07 19:39:48 +00004029 inline _LIBCPP_INLINE_VISIBILITY
4030 basic_string<char16_t> operator "" s( const char16_t *__str, size_t __len )
4031 {
4032 return basic_string<char16_t> (__str, __len);
4033 }
Marshall Clow15234322013-07-23 17:05:24 +00004034
Howard Hinnantab61b2c2013-08-07 19:39:48 +00004035 inline _LIBCPP_INLINE_VISIBILITY
4036 basic_string<char32_t> operator "" s( const char32_t *__str, size_t __len )
4037 {
4038 return basic_string<char32_t> (__str, __len);
4039 }
Marshall Clow15234322013-07-23 17:05:24 +00004040 }
4041}
4042#endif
4043
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004044_LIBCPP_END_NAMESPACE_STD
4045
Eric Fiselier018a3d52017-05-31 22:07:49 +00004046_LIBCPP_POP_MACROS
4047
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004048#endif // _LIBCPP_STRING