blob: 7218aa2e4d434298fc76036ff8164ab99e35cda6 [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 Clow0b9db072017-09-07 04:19:32 +0000262 size_type rfind(basic_string_view<charT, traits> sv, size_type pos = npos) 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 Clow0b9db072017-09-07 04:19:32 +0000274 size_type find_last_of(basic_string_view<charT, traits> sv, size_type pos = npos) 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 Clow0b9db072017-09-07 04:19:32 +0000286 size_type find_last_not_of(basic_string_view<charT, traits> sv, size_type pos = npos) 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
Marshall Clow46b4ad52017-12-04 20:11:38 +0000304 bool starts_with(basic_string_view<charT, traits> sv) const noexcept; // C++2a
305 bool starts_with(charT c) const noexcept; // C++2a
306 bool starts_with(const charT* s) const; // C++2a
307 bool ends_with(basic_string_view<charT, traits> sv) const noexcept; // C++2a
308 bool ends_with(charT c) const noexcept; // C++2a
309 bool ends_with(const charT* s) const; // C++2a
310
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000311 bool __invariants() const;
312};
313
Marshall Clow5b1e87e2018-02-08 06:34:03 +0000314template<class InputIterator,
315 class Allocator = allocator<typename iterator_traits<InputIterator>::value_type>>
316basic_string(InputIterator, InputIterator, Allocator = Allocator())
317 -> basic_string<typename iterator_traits<InputIterator>::value_type,
318 char_traits<typename iterator_traits<InputIterator>::value_type>,
319 Allocator>; // C++17
320
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000321template<class charT, class traits, class Allocator>
322basic_string<charT, traits, Allocator>
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000323operator+(const basic_string<charT, traits, Allocator>& lhs,
324 const basic_string<charT, traits, Allocator>& rhs);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000325
326template<class charT, class traits, class Allocator>
327basic_string<charT, traits, Allocator>
328operator+(const charT* lhs , const basic_string<charT,traits,Allocator>&rhs);
329
330template<class charT, class traits, class Allocator>
331basic_string<charT, traits, Allocator>
332operator+(charT lhs, const basic_string<charT,traits,Allocator>& rhs);
333
334template<class charT, class traits, class Allocator>
335basic_string<charT, traits, Allocator>
336operator+(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs);
337
338template<class charT, class traits, class Allocator>
339basic_string<charT, traits, Allocator>
340operator+(const basic_string<charT, traits, Allocator>& lhs, charT rhs);
341
342template<class charT, class traits, class Allocator>
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000343bool operator==(const basic_string<charT, traits, Allocator>& lhs,
Howard Hinnanta6119a82011-05-29 19:57:12 +0000344 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000345
346template<class charT, class traits, class Allocator>
Howard Hinnanta6119a82011-05-29 19:57:12 +0000347bool operator==(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000348
349template<class charT, class traits, class Allocator>
Howard Hinnanta6119a82011-05-29 19:57:12 +0000350bool operator==(const basic_string<charT,traits,Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000351
Howard Hinnant324bb032010-08-22 00:02:43 +0000352template<class charT, class traits, class Allocator>
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000353bool operator!=(const basic_string<charT,traits,Allocator>& lhs,
Howard Hinnanta6119a82011-05-29 19:57:12 +0000354 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000355
356template<class charT, class traits, class Allocator>
Howard Hinnanta6119a82011-05-29 19:57:12 +0000357bool operator!=(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000358
359template<class charT, class traits, class Allocator>
Howard Hinnanta6119a82011-05-29 19:57:12 +0000360bool operator!=(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000361
362template<class charT, class traits, class Allocator>
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000363bool operator< (const basic_string<charT, traits, Allocator>& lhs,
Howard Hinnanta6119a82011-05-29 19:57:12 +0000364 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000365
366template<class charT, class traits, class Allocator>
Howard Hinnanta6119a82011-05-29 19:57:12 +0000367bool operator< (const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000368
369template<class charT, class traits, class Allocator>
Howard Hinnanta6119a82011-05-29 19:57:12 +0000370bool operator< (const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000371
372template<class charT, class traits, class Allocator>
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000373bool operator> (const basic_string<charT, traits, Allocator>& lhs,
Howard Hinnanta6119a82011-05-29 19:57:12 +0000374 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000375
376template<class charT, class traits, class Allocator>
Howard Hinnanta6119a82011-05-29 19:57:12 +0000377bool operator> (const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000378
379template<class charT, class traits, class Allocator>
Howard Hinnanta6119a82011-05-29 19:57:12 +0000380bool operator> (const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000381
382template<class charT, class traits, class Allocator>
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000383bool operator<=(const basic_string<charT, traits, Allocator>& lhs,
Howard Hinnanta6119a82011-05-29 19:57:12 +0000384 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000385
386template<class charT, class traits, class Allocator>
Howard Hinnanta6119a82011-05-29 19:57:12 +0000387bool operator<=(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000388
389template<class charT, class traits, class Allocator>
Howard Hinnanta6119a82011-05-29 19:57:12 +0000390bool operator<=(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000391
392template<class charT, class traits, class Allocator>
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000393bool operator>=(const basic_string<charT, traits, Allocator>& lhs,
Howard Hinnanta6119a82011-05-29 19:57:12 +0000394 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000395
396template<class charT, class traits, class Allocator>
Howard Hinnanta6119a82011-05-29 19:57:12 +0000397bool operator>=(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000398
399template<class charT, class traits, class Allocator>
Howard Hinnanta6119a82011-05-29 19:57:12 +0000400bool operator>=(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000401
402template<class charT, class traits, class Allocator>
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000403void swap(basic_string<charT, traits, Allocator>& lhs,
Howard Hinnant53f7d4c2011-06-03 18:40:47 +0000404 basic_string<charT, traits, Allocator>& rhs)
405 noexcept(noexcept(lhs.swap(rhs)));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000406
407template<class charT, class traits, class Allocator>
408basic_istream<charT, traits>&
409operator>>(basic_istream<charT, traits>& is, basic_string<charT, traits, Allocator>& str);
410
411template<class charT, class traits, class Allocator>
412basic_ostream<charT, traits>&
413operator<<(basic_ostream<charT, traits>& os, const basic_string<charT, traits, Allocator>& str);
414
415template<class charT, class traits, class Allocator>
Howard Hinnant324bb032010-08-22 00:02:43 +0000416basic_istream<charT, traits>&
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000417getline(basic_istream<charT, traits>& is, basic_string<charT, traits, Allocator>& str,
418 charT delim);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000419
420template<class charT, class traits, class Allocator>
421basic_istream<charT, traits>&
422getline(basic_istream<charT, traits>& is, basic_string<charT, traits, Allocator>& str);
423
424typedef basic_string<char> string;
425typedef basic_string<wchar_t> wstring;
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000426typedef basic_string<char16_t> u16string;
427typedef basic_string<char32_t> u32string;
428
429int stoi (const string& str, size_t* idx = 0, int base = 10);
430long stol (const string& str, size_t* idx = 0, int base = 10);
431unsigned long stoul (const string& str, size_t* idx = 0, int base = 10);
432long long stoll (const string& str, size_t* idx = 0, int base = 10);
433unsigned long long stoull(const string& str, size_t* idx = 0, int base = 10);
434
435float stof (const string& str, size_t* idx = 0);
436double stod (const string& str, size_t* idx = 0);
437long double stold(const string& str, size_t* idx = 0);
438
439string to_string(int val);
440string to_string(unsigned val);
441string to_string(long val);
442string to_string(unsigned long val);
443string to_string(long long val);
444string to_string(unsigned long long val);
445string to_string(float val);
446string to_string(double val);
447string to_string(long double val);
448
449int stoi (const wstring& str, size_t* idx = 0, int base = 10);
450long stol (const wstring& str, size_t* idx = 0, int base = 10);
451unsigned long stoul (const wstring& str, size_t* idx = 0, int base = 10);
452long long stoll (const wstring& str, size_t* idx = 0, int base = 10);
453unsigned long long stoull(const wstring& str, size_t* idx = 0, int base = 10);
454
455float stof (const wstring& str, size_t* idx = 0);
456double stod (const wstring& str, size_t* idx = 0);
457long double stold(const wstring& str, size_t* idx = 0);
458
459wstring to_wstring(int val);
460wstring to_wstring(unsigned val);
461wstring to_wstring(long val);
462wstring to_wstring(unsigned long val);
463wstring to_wstring(long long val);
464wstring to_wstring(unsigned long long val);
465wstring to_wstring(float val);
466wstring to_wstring(double val);
467wstring to_wstring(long double val);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000468
469template <> struct hash<string>;
470template <> struct hash<u16string>;
471template <> struct hash<u32string>;
472template <> struct hash<wstring>;
473
Marshall Clow15234322013-07-23 17:05:24 +0000474basic_string<char> operator "" s( const char *str, size_t len ); // C++14
475basic_string<wchar_t> operator "" s( const wchar_t *str, size_t len ); // C++14
476basic_string<char16_t> operator "" s( const char16_t *str, size_t len ); // C++14
477basic_string<char32_t> operator "" s( const char32_t *str, size_t len ); // C++14
478
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000479} // std
480
481*/
482
483#include <__config>
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000484#include <string_view>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000485#include <iosfwd>
486#include <cstring>
Howard Hinnantadff4892010-05-24 17:49:41 +0000487#include <cstdio> // For EOF.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000488#include <cwchar>
489#include <algorithm>
490#include <iterator>
491#include <utility>
492#include <memory>
493#include <stdexcept>
494#include <type_traits>
495#include <initializer_list>
496#include <__functional_base>
497#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
498#include <cstdint>
499#endif
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000500
Eric Fiselierb9536102014-08-10 23:53:08 +0000501#include <__debug>
502
Howard Hinnant08e17472011-10-17 20:05:10 +0000503#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000504#pragma GCC system_header
Howard Hinnant08e17472011-10-17 20:05:10 +0000505#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000506
Eric Fiselier018a3d52017-05-31 22:07:49 +0000507_LIBCPP_PUSH_MACROS
508#include <__undef_macros>
509
510
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000511_LIBCPP_BEGIN_NAMESPACE_STD
512
513// fpos
514
515template <class _StateT>
Eric Fiselierc3589a82017-01-04 23:56:00 +0000516class _LIBCPP_TEMPLATE_VIS fpos
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000517{
518private:
519 _StateT __st_;
520 streamoff __off_;
521public:
522 _LIBCPP_INLINE_VISIBILITY fpos(streamoff __off = streamoff()) : __st_(), __off_(__off) {}
523
524 _LIBCPP_INLINE_VISIBILITY operator streamoff() const {return __off_;}
525
526 _LIBCPP_INLINE_VISIBILITY _StateT state() const {return __st_;}
527 _LIBCPP_INLINE_VISIBILITY void state(_StateT __st) {__st_ = __st;}
528
529 _LIBCPP_INLINE_VISIBILITY fpos& operator+=(streamoff __off) {__off_ += __off; return *this;}
530 _LIBCPP_INLINE_VISIBILITY fpos operator+ (streamoff __off) const {fpos __t(*this); __t += __off; return __t;}
531 _LIBCPP_INLINE_VISIBILITY fpos& operator-=(streamoff __off) {__off_ -= __off; return *this;}
532 _LIBCPP_INLINE_VISIBILITY fpos operator- (streamoff __off) const {fpos __t(*this); __t -= __off; return __t;}
533};
534
535template <class _StateT>
536inline _LIBCPP_INLINE_VISIBILITY
537streamoff operator-(const fpos<_StateT>& __x, const fpos<_StateT>& __y)
538 {return streamoff(__x) - streamoff(__y);}
539
540template <class _StateT>
541inline _LIBCPP_INLINE_VISIBILITY
542bool operator==(const fpos<_StateT>& __x, const fpos<_StateT>& __y)
543 {return streamoff(__x) == streamoff(__y);}
544
545template <class _StateT>
546inline _LIBCPP_INLINE_VISIBILITY
547bool operator!=(const fpos<_StateT>& __x, const fpos<_StateT>& __y)
548 {return streamoff(__x) != streamoff(__y);}
549
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000550// basic_string
551
552template<class _CharT, class _Traits, class _Allocator>
553basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant2b1b2d42011-06-14 19:58:17 +0000554operator+(const basic_string<_CharT, _Traits, _Allocator>& __x,
555 const basic_string<_CharT, _Traits, _Allocator>& __y);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000556
557template<class _CharT, class _Traits, class _Allocator>
558basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant2b1b2d42011-06-14 19:58:17 +0000559operator+(const _CharT* __x, const basic_string<_CharT,_Traits,_Allocator>& __y);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000560
561template<class _CharT, class _Traits, class _Allocator>
562basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant2b1b2d42011-06-14 19:58:17 +0000563operator+(_CharT __x, const basic_string<_CharT,_Traits,_Allocator>& __y);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000564
565template<class _CharT, class _Traits, class _Allocator>
566basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant2b1b2d42011-06-14 19:58:17 +0000567operator+(const basic_string<_CharT, _Traits, _Allocator>& __x, const _CharT* __y);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000568
569template<class _CharT, class _Traits, class _Allocator>
570basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant2b1b2d42011-06-14 19:58:17 +0000571operator+(const basic_string<_CharT, _Traits, _Allocator>& __x, _CharT __y);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000572
Shoaib Meenai487562f2017-07-29 02:54:41 +0000573_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS string operator+<char, char_traits<char>, allocator<char> >(char const*, string const&))
574
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000575template <bool>
Eric Fiselierc3589a82017-01-04 23:56:00 +0000576class _LIBCPP_TEMPLATE_VIS __basic_string_common
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000577{
578protected:
Marshall Clow14c09a22016-08-25 15:09:01 +0000579 _LIBCPP_NORETURN void __throw_length_error() const;
580 _LIBCPP_NORETURN void __throw_out_of_range() const;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000581};
582
583template <bool __b>
584void
585__basic_string_common<__b>::__throw_length_error() const
586{
Marshall Clow14c09a22016-08-25 15:09:01 +0000587 _VSTD::__throw_length_error("basic_string");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000588}
589
590template <bool __b>
591void
592__basic_string_common<__b>::__throw_out_of_range() const
593{
Marshall Clow14c09a22016-08-25 15:09:01 +0000594 _VSTD::__throw_out_of_range("basic_string");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000595}
596
Eric Fiselier833d6442016-09-15 22:27:07 +0000597_LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS __basic_string_common<true>)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000598
Marshall Clowdf9db312016-01-13 21:54:34 +0000599#ifdef _LIBCPP_NO_EXCEPTIONS
600template <class _Iter>
601struct __libcpp_string_gets_noexcept_iterator_impl : public true_type {};
602#elif defined(_LIBCPP_HAS_NO_NOEXCEPT)
603template <class _Iter>
604struct __libcpp_string_gets_noexcept_iterator_impl : public false_type {};
605#else
606template <class _Iter, bool = __is_forward_iterator<_Iter>::value>
607struct __libcpp_string_gets_noexcept_iterator_impl : public _LIBCPP_BOOL_CONSTANT((
608 noexcept(++(declval<_Iter&>())) &&
609 is_nothrow_assignable<_Iter&, _Iter>::value &&
610 noexcept(declval<_Iter>() == declval<_Iter>()) &&
611 noexcept(*declval<_Iter>())
612)) {};
613
614template <class _Iter>
615struct __libcpp_string_gets_noexcept_iterator_impl<_Iter, false> : public false_type {};
616#endif
617
618
619template <class _Iter>
620struct __libcpp_string_gets_noexcept_iterator
621 : public _LIBCPP_BOOL_CONSTANT(__libcpp_is_trivial_iterator<_Iter>::value || __libcpp_string_gets_noexcept_iterator_impl<_Iter>::value) {};
622
Marshall Clow6ac8de02016-09-24 22:45:42 +0000623template <class _CharT, class _Traits, class _Tp>
624struct __can_be_converted_to_string_view : public _LIBCPP_BOOL_CONSTANT(
Marshall Clowf1729d92017-11-15 20:02:27 +0000625 ( is_convertible<const _Tp&, basic_string_view<_CharT, _Traits> >::value &&
Marshall Clow6ac8de02016-09-24 22:45:42 +0000626 !is_convertible<const _Tp&, const _CharT*>::value)) {};
627
Evgeniy Stepanov4f01aa82015-10-13 23:48:28 +0000628#ifdef _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant15467182013-04-30 21:44:48 +0000629
630template <class _CharT, size_t = sizeof(_CharT)>
631struct __padding
632{
633 unsigned char __xx[sizeof(_CharT)-1];
634};
635
636template <class _CharT>
637struct __padding<_CharT, 1>
638{
639};
640
Evgeniy Stepanov4f01aa82015-10-13 23:48:28 +0000641#endif // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant15467182013-04-30 21:44:48 +0000642
Howard Hinnant324bb032010-08-22 00:02:43 +0000643template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierc3589a82017-01-04 23:56:00 +0000644class _LIBCPP_TEMPLATE_VIS basic_string
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000645 : private __basic_string_common<true>
646{
647public:
648 typedef basic_string __self;
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000649 typedef basic_string_view<_CharT, _Traits> __self_view;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000650 typedef _Traits traits_type;
Marshall Clow2d4c3fa2017-03-15 18:41:11 +0000651 typedef _CharT value_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000652 typedef _Allocator allocator_type;
Howard Hinnante32b5e22010-11-17 17:55:08 +0000653 typedef allocator_traits<allocator_type> __alloc_traits;
654 typedef typename __alloc_traits::size_type size_type;
655 typedef typename __alloc_traits::difference_type difference_type;
Howard Hinnant53f7d4c2011-06-03 18:40:47 +0000656 typedef value_type& reference;
657 typedef const value_type& const_reference;
Howard Hinnante32b5e22010-11-17 17:55:08 +0000658 typedef typename __alloc_traits::pointer pointer;
659 typedef typename __alloc_traits::const_pointer const_pointer;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000660
Marshall Clow256f1872018-03-21 00:36:05 +0000661 static_assert((!is_array<value_type>::value), "Character type of basic_string must not be an array");
662 static_assert(( is_standard_layout<value_type>::value), "Character type of basic_string must be standard-layout");
663 static_assert(( is_trivial<value_type>::value), "Character type of basic_string must be trivial");
664 static_assert(( is_same<_CharT, typename traits_type::char_type>::value),
Howard Hinnant499cea12013-08-23 17:37:05 +0000665 "traits_type::char_type must be the same type as CharT");
Marshall Clow256f1872018-03-21 00:36:05 +0000666 static_assert(( is_same<typename allocator_type::value_type, value_type>::value),
Howard Hinnant499cea12013-08-23 17:37:05 +0000667 "Allocator::value_type must be same type as value_type");
668#if defined(_LIBCPP_RAW_ITERATORS)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000669 typedef pointer iterator;
670 typedef const_pointer const_iterator;
Howard Hinnant324bb032010-08-22 00:02:43 +0000671#else // defined(_LIBCPP_RAW_ITERATORS)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000672 typedef __wrap_iter<pointer> iterator;
673 typedef __wrap_iter<const_pointer> const_iterator;
Howard Hinnant324bb032010-08-22 00:02:43 +0000674#endif // defined(_LIBCPP_RAW_ITERATORS)
Howard Hinnant0949eed2011-06-30 21:18:19 +0000675 typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
676 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000677
678private:
Howard Hinnant15467182013-04-30 21:44:48 +0000679
Evgeniy Stepanov4f01aa82015-10-13 23:48:28 +0000680#ifdef _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant15467182013-04-30 21:44:48 +0000681
682 struct __long
683 {
684 pointer __data_;
685 size_type __size_;
686 size_type __cap_;
687 };
688
Eric Fiselier5ccf0432017-10-17 13:16:01 +0000689#ifdef _LIBCPP_BIG_ENDIAN
Ben Craigde79ab62017-07-12 01:45:13 +0000690 static const size_type __short_mask = 0x01;
691 static const size_type __long_mask = 0x1ul;
Howard Hinnant15467182013-04-30 21:44:48 +0000692#else // _LIBCPP_BIG_ENDIAN
Ben Craigde79ab62017-07-12 01:45:13 +0000693 static const size_type __short_mask = 0x80;
694 static const size_type __long_mask = ~(size_type(~0) >> 1);
Howard Hinnant15467182013-04-30 21:44:48 +0000695#endif // _LIBCPP_BIG_ENDIAN
696
697 enum {__min_cap = (sizeof(__long) - 1)/sizeof(value_type) > 2 ?
698 (sizeof(__long) - 1)/sizeof(value_type) : 2};
699
700 struct __short
701 {
702 value_type __data_[__min_cap];
703 struct
704 : __padding<value_type>
705 {
706 unsigned char __size_;
707 };
708 };
709
710#else
711
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000712 struct __long
713 {
714 size_type __cap_;
715 size_type __size_;
716 pointer __data_;
717 };
718
Eric Fiselier5ccf0432017-10-17 13:16:01 +0000719#ifdef _LIBCPP_BIG_ENDIAN
Ben Craigde79ab62017-07-12 01:45:13 +0000720 static const size_type __short_mask = 0x80;
721 static const size_type __long_mask = ~(size_type(~0) >> 1);
Howard Hinnant324bb032010-08-22 00:02:43 +0000722#else // _LIBCPP_BIG_ENDIAN
Ben Craigde79ab62017-07-12 01:45:13 +0000723 static const size_type __short_mask = 0x01;
724 static const size_type __long_mask = 0x1ul;
Howard Hinnant324bb032010-08-22 00:02:43 +0000725#endif // _LIBCPP_BIG_ENDIAN
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000726
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000727 enum {__min_cap = (sizeof(__long) - 1)/sizeof(value_type) > 2 ?
728 (sizeof(__long) - 1)/sizeof(value_type) : 2};
729
730 struct __short
731 {
732 union
733 {
734 unsigned char __size_;
Howard Hinnant9c0df142012-10-30 19:06:59 +0000735 value_type __lx;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000736 };
737 value_type __data_[__min_cap];
738 };
739
Evgeniy Stepanov4f01aa82015-10-13 23:48:28 +0000740#endif // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant15467182013-04-30 21:44:48 +0000741
Howard Hinnant499cea12013-08-23 17:37:05 +0000742 union __ulx{__long __lx; __short __lxx;};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000743
Howard Hinnant499cea12013-08-23 17:37:05 +0000744 enum {__n_words = sizeof(__ulx) / sizeof(size_type)};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000745
746 struct __raw
747 {
748 size_type __words[__n_words];
749 };
750
751 struct __rep
752 {
753 union
754 {
755 __long __l;
756 __short __s;
757 __raw __r;
758 };
759 };
760
761 __compressed_pair<__rep, allocator_type> __r_;
762
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000763public:
764 static const size_type npos = -1;
765
Howard Hinnant53f7d4c2011-06-03 18:40:47 +0000766 _LIBCPP_INLINE_VISIBILITY basic_string()
767 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
Marshall Clow7b193f72015-06-03 19:56:43 +0000768
769 _LIBCPP_INLINE_VISIBILITY explicit basic_string(const allocator_type& __a)
770#if _LIBCPP_STD_VER <= 14
771 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value);
772#else
773 _NOEXCEPT;
774#endif
775
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000776 basic_string(const basic_string& __str);
777 basic_string(const basic_string& __str, const allocator_type& __a);
Marshall Clow7b193f72015-06-03 19:56:43 +0000778
Eric Fiselier3e928972017-04-19 00:28:44 +0000779#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant9f193f22011-01-26 00:06:59 +0000780 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant53f7d4c2011-06-03 18:40:47 +0000781 basic_string(basic_string&& __str)
Marshall Clow7b193f72015-06-03 19:56:43 +0000782#if _LIBCPP_STD_VER <= 14
Howard Hinnant53f7d4c2011-06-03 18:40:47 +0000783 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
Marshall Clow7b193f72015-06-03 19:56:43 +0000784#else
785 _NOEXCEPT;
786#endif
787
Howard Hinnant9f193f22011-01-26 00:06:59 +0000788 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000789 basic_string(basic_string&& __str, const allocator_type& __a);
Eric Fiselier3e928972017-04-19 00:28:44 +0000790#endif // _LIBCPP_CXX03_LANG
Eric Fiselier0eaf2e82017-02-17 01:17:10 +0000791 _LIBCPP_INLINE_VISIBILITY basic_string(const _CharT* __s);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000792 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier0eaf2e82017-02-17 01:17:10 +0000793 basic_string(const _CharT* __s, const _Allocator& __a);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000794 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier0eaf2e82017-02-17 01:17:10 +0000795 basic_string(const _CharT* __s, size_type __n);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000796 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier0eaf2e82017-02-17 01:17:10 +0000797 basic_string(const _CharT* __s, size_type __n, const _Allocator& __a);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000798 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier0eaf2e82017-02-17 01:17:10 +0000799 basic_string(size_type __n, _CharT __c);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000800 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier0eaf2e82017-02-17 01:17:10 +0000801 basic_string(size_type __n, _CharT __c, const _Allocator& __a);
Marshall Clowf4f7d8f2016-04-07 18:13:41 +0000802 basic_string(const basic_string& __str, size_type __pos, size_type __n,
Eric Fiselier0eaf2e82017-02-17 01:17:10 +0000803 const _Allocator& __a = _Allocator());
Marshall Clowf4f7d8f2016-04-07 18:13:41 +0000804 _LIBCPP_INLINE_VISIBILITY
805 basic_string(const basic_string& __str, size_type __pos,
Eric Fiselier0eaf2e82017-02-17 01:17:10 +0000806 const _Allocator& __a = _Allocator());
Marshall Clowdb7fa112016-11-14 18:22:19 +0000807 template<class _Tp>
Shoaib Meenai24e8dbd2017-03-02 03:02:50 +0000808 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselier0eaf2e82017-02-17 01:17:10 +0000809 basic_string(const _Tp& __t, size_type __pos, size_type __n,
Marshall Clowdb7fa112016-11-14 18:22:19 +0000810 const allocator_type& __a = allocator_type(),
811 typename enable_if<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value, void>::type* = 0);
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000812 _LIBCPP_INLINE_VISIBILITY explicit
813 basic_string(__self_view __sv);
814 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier0eaf2e82017-02-17 01:17:10 +0000815 basic_string(__self_view __sv, const _Allocator& __a);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000816 template<class _InputIterator>
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000817 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000818 basic_string(_InputIterator __first, _InputIterator __last);
819 template<class _InputIterator>
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000820 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000821 basic_string(_InputIterator __first, _InputIterator __last, const allocator_type& __a);
Eric Fiselier3e928972017-04-19 00:28:44 +0000822#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000823 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier0eaf2e82017-02-17 01:17:10 +0000824 basic_string(initializer_list<_CharT> __il);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000825 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier0eaf2e82017-02-17 01:17:10 +0000826 basic_string(initializer_list<_CharT> __il, const _Allocator& __a);
Eric Fiselier3e928972017-04-19 00:28:44 +0000827#endif // _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000828
Eric Fiselier51eb1d52016-10-31 03:42:50 +0000829 inline ~basic_string();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000830
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000831 _LIBCPP_INLINE_VISIBILITY
832 operator __self_view() const _NOEXCEPT { return __self_view(data(), size()); }
833
Howard Hinnante32b5e22010-11-17 17:55:08 +0000834 basic_string& operator=(const basic_string& __str);
Eric Fiselierf472d6c2017-01-23 21:24:58 +0000835
836#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier37b2be92017-01-17 22:10:32 +0000837 template <class = void>
Eric Fiselierf472d6c2017-01-23 21:24:58 +0000838#endif
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000839 _LIBCPP_INLINE_VISIBILITY
840 basic_string& operator=(__self_view __sv) {return assign(__sv);}
Eric Fiselier3e928972017-04-19 00:28:44 +0000841#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000842 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant53f7d4c2011-06-03 18:40:47 +0000843 basic_string& operator=(basic_string&& __str)
Marshall Clowaf961ed2015-08-18 18:57:00 +0000844 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value));
Eric Fiselier3e928972017-04-19 00:28:44 +0000845 _LIBCPP_INLINE_VISIBILITY
846 basic_string& operator=(initializer_list<value_type> __il) {return assign(__il.begin(), __il.size());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000847#endif
Howard Hinnant9dcdcde2013-06-28 16:59:19 +0000848 _LIBCPP_INLINE_VISIBILITY basic_string& operator=(const value_type* __s) {return assign(__s);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000849 basic_string& operator=(value_type __c);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000850
Howard Hinnant499cea12013-08-23 17:37:05 +0000851#if _LIBCPP_DEBUG_LEVEL >= 2
852 _LIBCPP_INLINE_VISIBILITY
853 iterator begin() _NOEXCEPT
854 {return iterator(this, __get_pointer());}
855 _LIBCPP_INLINE_VISIBILITY
856 const_iterator begin() const _NOEXCEPT
857 {return const_iterator(this, __get_pointer());}
858 _LIBCPP_INLINE_VISIBILITY
859 iterator end() _NOEXCEPT
860 {return iterator(this, __get_pointer() + size());}
861 _LIBCPP_INLINE_VISIBILITY
862 const_iterator end() const _NOEXCEPT
863 {return const_iterator(this, __get_pointer() + size());}
864#else
Howard Hinnanta6119a82011-05-29 19:57:12 +0000865 _LIBCPP_INLINE_VISIBILITY
866 iterator begin() _NOEXCEPT
867 {return iterator(__get_pointer());}
868 _LIBCPP_INLINE_VISIBILITY
869 const_iterator begin() const _NOEXCEPT
Howard Hinnant9dcdcde2013-06-28 16:59:19 +0000870 {return const_iterator(__get_pointer());}
Howard Hinnanta6119a82011-05-29 19:57:12 +0000871 _LIBCPP_INLINE_VISIBILITY
872 iterator end() _NOEXCEPT
873 {return iterator(__get_pointer() + size());}
874 _LIBCPP_INLINE_VISIBILITY
875 const_iterator end() const _NOEXCEPT
Howard Hinnant9dcdcde2013-06-28 16:59:19 +0000876 {return const_iterator(__get_pointer() + size());}
Howard Hinnant499cea12013-08-23 17:37:05 +0000877#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnanta6119a82011-05-29 19:57:12 +0000878 _LIBCPP_INLINE_VISIBILITY
879 reverse_iterator rbegin() _NOEXCEPT
880 {return reverse_iterator(end());}
881 _LIBCPP_INLINE_VISIBILITY
882 const_reverse_iterator rbegin() const _NOEXCEPT
883 {return const_reverse_iterator(end());}
884 _LIBCPP_INLINE_VISIBILITY
885 reverse_iterator rend() _NOEXCEPT
886 {return reverse_iterator(begin());}
887 _LIBCPP_INLINE_VISIBILITY
888 const_reverse_iterator rend() const _NOEXCEPT
889 {return const_reverse_iterator(begin());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000890
Howard Hinnanta6119a82011-05-29 19:57:12 +0000891 _LIBCPP_INLINE_VISIBILITY
892 const_iterator cbegin() const _NOEXCEPT
893 {return begin();}
894 _LIBCPP_INLINE_VISIBILITY
895 const_iterator cend() const _NOEXCEPT
896 {return end();}
897 _LIBCPP_INLINE_VISIBILITY
898 const_reverse_iterator crbegin() const _NOEXCEPT
899 {return rbegin();}
900 _LIBCPP_INLINE_VISIBILITY
901 const_reverse_iterator crend() const _NOEXCEPT
902 {return rend();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000903
Howard Hinnanta6119a82011-05-29 19:57:12 +0000904 _LIBCPP_INLINE_VISIBILITY size_type size() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000905 {return __is_long() ? __get_long_size() : __get_short_size();}
Howard Hinnanta6119a82011-05-29 19:57:12 +0000906 _LIBCPP_INLINE_VISIBILITY size_type length() const _NOEXCEPT {return size();}
907 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT;
908 _LIBCPP_INLINE_VISIBILITY size_type capacity() const _NOEXCEPT
Eric Fiseliere2d48922015-08-28 07:02:42 +0000909 {return (__is_long() ? __get_long_cap()
910 : static_cast<size_type>(__min_cap)) - 1;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000911
912 void resize(size_type __n, value_type __c);
913 _LIBCPP_INLINE_VISIBILITY void resize(size_type __n) {resize(__n, value_type());}
914
Eric Fiselier59e24fe2017-06-01 02:29:37 +0000915 void reserve(size_type __res_arg = 0);
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000916 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant53f7d4c2011-06-03 18:40:47 +0000917 void shrink_to_fit() _NOEXCEPT {reserve();}
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000918 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta6119a82011-05-29 19:57:12 +0000919 void clear() _NOEXCEPT;
Marshall Clowf1729d92017-11-15 20:02:27 +0000920 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
921 bool empty() const _NOEXCEPT {return size() == 0;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000922
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000923 _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_type __pos) const _NOEXCEPT;
924 _LIBCPP_INLINE_VISIBILITY reference operator[](size_type __pos) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000925
926 const_reference at(size_type __n) const;
927 reference at(size_type __n);
928
929 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(const basic_string& __str) {return append(__str);}
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000930 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(__self_view __sv) {return append(__sv);}
931 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(const value_type* __s) {return append(__s);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000932 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(value_type __c) {push_back(__c); return *this;}
Eric Fiselier3e928972017-04-19 00:28:44 +0000933#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000934 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(initializer_list<value_type> __il) {return append(__il);}
Eric Fiselier3e928972017-04-19 00:28:44 +0000935#endif // _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000936
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000937 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000938 basic_string& append(const basic_string& __str);
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000939 _LIBCPP_INLINE_VISIBILITY
940 basic_string& append(__self_view __sv) { return append(__sv.data(), __sv.size()); }
Marshall Clowa93b5e22014-03-04 19:17:19 +0000941 basic_string& append(const basic_string& __str, size_type __pos, size_type __n=npos);
Marshall Clow42a87db2016-10-03 23:40:48 +0000942 template <class _Tp>
Shoaib Meenai24e8dbd2017-03-02 03:02:50 +0000943 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
944 typename enable_if
Marshall Clow6ac8de02016-09-24 22:45:42 +0000945 <
946 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
947 basic_string&
948 >::type
949 append(const _Tp& __t, size_type __pos, size_type __n=npos);
Howard Hinnant9dcdcde2013-06-28 16:59:19 +0000950 basic_string& append(const value_type* __s, size_type __n);
951 basic_string& append(const value_type* __s);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000952 basic_string& append(size_type __n, value_type __c);
Eric Fiselier026d38e2016-10-31 02:46:25 +0000953 template <class _ForwardIterator>
Shoaib Meenai24e8dbd2017-03-02 03:02:50 +0000954 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
955 basic_string& __append_forward_unsafe(_ForwardIterator, _ForwardIterator);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000956 template<class _InputIterator>
Shoaib Meenai24e8dbd2017-03-02 03:02:50 +0000957 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
958 typename enable_if
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000959 <
Marshall Clowdf9db312016-01-13 21:54:34 +0000960 __is_exactly_input_iterator<_InputIterator>::value
961 || !__libcpp_string_gets_noexcept_iterator<_InputIterator>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000962 basic_string&
963 >::type
Eric Fiselier026d38e2016-10-31 02:46:25 +0000964 _LIBCPP_INLINE_VISIBILITY
965 append(_InputIterator __first, _InputIterator __last) {
966 const basic_string __temp (__first, __last, __alloc());
967 append(__temp.data(), __temp.size());
968 return *this;
969 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000970 template<class _ForwardIterator>
Shoaib Meenai24e8dbd2017-03-02 03:02:50 +0000971 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
972 typename enable_if
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000973 <
Marshall Clowdf9db312016-01-13 21:54:34 +0000974 __is_forward_iterator<_ForwardIterator>::value
975 && __libcpp_string_gets_noexcept_iterator<_ForwardIterator>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000976 basic_string&
977 >::type
Eric Fiselier026d38e2016-10-31 02:46:25 +0000978 _LIBCPP_INLINE_VISIBILITY
979 append(_ForwardIterator __first, _ForwardIterator __last) {
980 return __append_forward_unsafe(__first, __last);
981 }
982
Eric Fiselier3e928972017-04-19 00:28:44 +0000983#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000984 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000985 basic_string& append(initializer_list<value_type> __il) {return append(__il.begin(), __il.size());}
Eric Fiselier3e928972017-04-19 00:28:44 +0000986#endif // _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000987
988 void push_back(value_type __c);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000989 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000990 void pop_back();
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000991 _LIBCPP_INLINE_VISIBILITY reference front();
992 _LIBCPP_INLINE_VISIBILITY const_reference front() const;
993 _LIBCPP_INLINE_VISIBILITY reference back();
994 _LIBCPP_INLINE_VISIBILITY const_reference back() const;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000995
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000996 _LIBCPP_INLINE_VISIBILITY
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000997 basic_string& assign(__self_view __sv) { return assign(__sv.data(), __sv.size()); }
998 _LIBCPP_INLINE_VISIBILITY
Marshall Clowf40ec902016-03-09 18:08:29 +0000999 basic_string& assign(const basic_string& __str) { return *this = __str; }
Eric Fiselier3e928972017-04-19 00:28:44 +00001000#ifndef _LIBCPP_CXX03_LANG
Howard Hinnanta6119a82011-05-29 19:57:12 +00001001 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier59e24fe2017-06-01 02:29:37 +00001002 basic_string& assign(basic_string&& __str)
Marshall Clow7ed093b2015-10-05 16:17:34 +00001003 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
Eric Fiselier59e24fe2017-06-01 02:29:37 +00001004 {*this = _VSTD::move(__str); return *this;}
Howard Hinnanta6119a82011-05-29 19:57:12 +00001005#endif
Marshall Clowa93b5e22014-03-04 19:17:19 +00001006 basic_string& assign(const basic_string& __str, size_type __pos, size_type __n=npos);
Marshall Clow42a87db2016-10-03 23:40:48 +00001007 template <class _Tp>
Shoaib Meenai24e8dbd2017-03-02 03:02:50 +00001008 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
1009 typename enable_if
Marshall Clow6ac8de02016-09-24 22:45:42 +00001010 <
1011 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1012 basic_string&
1013 >::type
Eric Fiselier59e24fe2017-06-01 02:29:37 +00001014 assign(const _Tp & __t, size_type __pos, size_type __n=npos);
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001015 basic_string& assign(const value_type* __s, size_type __n);
1016 basic_string& assign(const value_type* __s);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001017 basic_string& assign(size_type __n, value_type __c);
1018 template<class _InputIterator>
Shoaib Meenai24e8dbd2017-03-02 03:02:50 +00001019 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
1020 typename enable_if
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001021 <
Marshall Clowdf9db312016-01-13 21:54:34 +00001022 __is_exactly_input_iterator<_InputIterator>::value
1023 || !__libcpp_string_gets_noexcept_iterator<_InputIterator>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001024 basic_string&
1025 >::type
1026 assign(_InputIterator __first, _InputIterator __last);
1027 template<class _ForwardIterator>
Shoaib Meenai24e8dbd2017-03-02 03:02:50 +00001028 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
1029 typename enable_if
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001030 <
Marshall Clowdf9db312016-01-13 21:54:34 +00001031 __is_forward_iterator<_ForwardIterator>::value
1032 && __libcpp_string_gets_noexcept_iterator<_ForwardIterator>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001033 basic_string&
1034 >::type
1035 assign(_ForwardIterator __first, _ForwardIterator __last);
Eric Fiselier3e928972017-04-19 00:28:44 +00001036#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant8d7a9552010-09-23 17:31:07 +00001037 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001038 basic_string& assign(initializer_list<value_type> __il) {return assign(__il.begin(), __il.size());}
Eric Fiselier3e928972017-04-19 00:28:44 +00001039#endif // _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001040
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001041 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001042 basic_string& insert(size_type __pos1, const basic_string& __str);
Marshall Clow1e00d6d2016-07-21 05:31:24 +00001043 _LIBCPP_INLINE_VISIBILITY
1044 basic_string& insert(size_type __pos1, __self_view __sv) { return insert(__pos1, __sv.data(), __sv.size()); }
Marshall Clow6ac8de02016-09-24 22:45:42 +00001045 template <class _Tp>
Shoaib Meenai24e8dbd2017-03-02 03:02:50 +00001046 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
1047 typename enable_if
Marshall Clow6ac8de02016-09-24 22:45:42 +00001048 <
1049 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1050 basic_string&
1051 >::type
1052 insert(size_type __pos1, const _Tp& __t, size_type __pos2, size_type __n=npos);
Marshall Clowa93b5e22014-03-04 19:17:19 +00001053 basic_string& insert(size_type __pos1, const basic_string& __str, size_type __pos2, size_type __n=npos);
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001054 basic_string& insert(size_type __pos, const value_type* __s, size_type __n);
1055 basic_string& insert(size_type __pos, const value_type* __s);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001056 basic_string& insert(size_type __pos, size_type __n, value_type __c);
1057 iterator insert(const_iterator __pos, value_type __c);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001058 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001059 iterator insert(const_iterator __pos, size_type __n, value_type __c);
1060 template<class _InputIterator>
Shoaib Meenai24e8dbd2017-03-02 03:02:50 +00001061 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
1062 typename enable_if
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001063 <
Marshall Clowdf9db312016-01-13 21:54:34 +00001064 __is_exactly_input_iterator<_InputIterator>::value
1065 || !__libcpp_string_gets_noexcept_iterator<_InputIterator>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001066 iterator
1067 >::type
1068 insert(const_iterator __pos, _InputIterator __first, _InputIterator __last);
1069 template<class _ForwardIterator>
Shoaib Meenai24e8dbd2017-03-02 03:02:50 +00001070 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
1071 typename enable_if
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001072 <
Marshall Clowdf9db312016-01-13 21:54:34 +00001073 __is_forward_iterator<_ForwardIterator>::value
1074 && __libcpp_string_gets_noexcept_iterator<_ForwardIterator>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001075 iterator
1076 >::type
1077 insert(const_iterator __pos, _ForwardIterator __first, _ForwardIterator __last);
Eric Fiselier3e928972017-04-19 00:28:44 +00001078#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant8d7a9552010-09-23 17:31:07 +00001079 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001080 iterator insert(const_iterator __pos, initializer_list<value_type> __il)
1081 {return insert(__pos, __il.begin(), __il.end());}
Eric Fiselier3e928972017-04-19 00:28:44 +00001082#endif // _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001083
1084 basic_string& erase(size_type __pos = 0, size_type __n = npos);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001085 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001086 iterator erase(const_iterator __pos);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001087 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001088 iterator erase(const_iterator __first, const_iterator __last);
1089
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001090 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001091 basic_string& replace(size_type __pos1, size_type __n1, const basic_string& __str);
Marshall Clow1e00d6d2016-07-21 05:31:24 +00001092 _LIBCPP_INLINE_VISIBILITY
1093 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 +00001094 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 +00001095 template <class _Tp>
Shoaib Meenai24e8dbd2017-03-02 03:02:50 +00001096 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
1097 typename enable_if
Marshall Clow6ac8de02016-09-24 22:45:42 +00001098 <
1099 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1100 basic_string&
1101 >::type
1102 replace(size_type __pos1, size_type __n1, const _Tp& __t, size_type __pos2, size_type __n2=npos);
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001103 basic_string& replace(size_type __pos, size_type __n1, const value_type* __s, size_type __n2);
1104 basic_string& replace(size_type __pos, size_type __n1, const value_type* __s);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001105 basic_string& replace(size_type __pos, size_type __n1, size_type __n2, value_type __c);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001106 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7b2cb482010-11-17 21:11:40 +00001107 basic_string& replace(const_iterator __i1, const_iterator __i2, const basic_string& __str);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001108 _LIBCPP_INLINE_VISIBILITY
Marshall Clow1e00d6d2016-07-21 05:31:24 +00001109 basic_string& replace(const_iterator __i1, const_iterator __i2, __self_view __sv) { return replace(__i1 - begin(), __i2 - __i1, __sv); }
1110 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001111 basic_string& replace(const_iterator __i1, const_iterator __i2, const value_type* __s, size_type __n);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001112 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001113 basic_string& replace(const_iterator __i1, const_iterator __i2, const value_type* __s);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001114 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7b2cb482010-11-17 21:11:40 +00001115 basic_string& replace(const_iterator __i1, const_iterator __i2, size_type __n, value_type __c);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001116 template<class _InputIterator>
Shoaib Meenai24e8dbd2017-03-02 03:02:50 +00001117 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
1118 typename enable_if
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001119 <
1120 __is_input_iterator<_InputIterator>::value,
1121 basic_string&
1122 >::type
Howard Hinnant7b2cb482010-11-17 21:11:40 +00001123 replace(const_iterator __i1, const_iterator __i2, _InputIterator __j1, _InputIterator __j2);
Eric Fiselier3e928972017-04-19 00:28:44 +00001124#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant8d7a9552010-09-23 17:31:07 +00001125 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7b2cb482010-11-17 21:11:40 +00001126 basic_string& replace(const_iterator __i1, const_iterator __i2, initializer_list<value_type> __il)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001127 {return replace(__i1, __i2, __il.begin(), __il.end());}
Eric Fiselier3e928972017-04-19 00:28:44 +00001128#endif // _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001129
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001130 size_type copy(value_type* __s, size_type __n, size_type __pos = 0) const;
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001131 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001132 basic_string substr(size_type __pos = 0, size_type __n = npos) const;
1133
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001134 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant53f7d4c2011-06-03 18:40:47 +00001135 void swap(basic_string& __str)
Marshall Clow7d914d12015-07-13 20:04:56 +00001136#if _LIBCPP_STD_VER >= 14
Eric Fiselier47257c42016-12-28 05:53:01 +00001137 _NOEXCEPT_DEBUG;
Marshall Clow7d914d12015-07-13 20:04:56 +00001138#else
Eric Fiselier47257c42016-12-28 05:53:01 +00001139 _NOEXCEPT_DEBUG_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow7d914d12015-07-13 20:04:56 +00001140 __is_nothrow_swappable<allocator_type>::value);
1141#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001142
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001143 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001144 const value_type* c_str() const _NOEXCEPT {return data();}
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001145 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001146 const value_type* data() const _NOEXCEPT {return _VSTD::__to_raw_pointer(__get_pointer());}
Eric Fiselier10bebe22017-11-20 20:23:27 +00001147#if _LIBCPP_STD_VER > 14 || defined(_LIBCPP_BUILDING_LIBRARY)
Marshall Clowf532a702016-03-08 15:44:30 +00001148 _LIBCPP_INLINE_VISIBILITY
1149 value_type* data() _NOEXCEPT {return _VSTD::__to_raw_pointer(__get_pointer());}
1150#endif
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 allocator_type get_allocator() const _NOEXCEPT {return __alloc();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001154
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001155 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta6119a82011-05-29 19:57:12 +00001156 size_type find(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
Marshall Clow1e00d6d2016-07-21 05:31:24 +00001157 _LIBCPP_INLINE_VISIBILITY
1158 size_type find(__self_view __sv, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001159 size_type find(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001160 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001161 size_type find(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnanta6119a82011-05-29 19:57:12 +00001162 size_type find(value_type __c, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001163
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001164 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta6119a82011-05-29 19:57:12 +00001165 size_type rfind(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
Marshall Clow1e00d6d2016-07-21 05:31:24 +00001166 _LIBCPP_INLINE_VISIBILITY
Marshall Clow0b9db072017-09-07 04:19:32 +00001167 size_type rfind(__self_view __sv, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001168 size_type rfind(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001169 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001170 size_type rfind(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnanta6119a82011-05-29 19:57:12 +00001171 size_type rfind(value_type __c, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001172
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001173 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta6119a82011-05-29 19:57:12 +00001174 size_type find_first_of(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
Marshall Clow1e00d6d2016-07-21 05:31:24 +00001175 _LIBCPP_INLINE_VISIBILITY
1176 size_type find_first_of(__self_view __sv, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001177 size_type find_first_of(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001178 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001179 size_type find_first_of(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001180 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta6119a82011-05-29 19:57:12 +00001181 size_type find_first_of(value_type __c, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001182
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001183 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta6119a82011-05-29 19:57:12 +00001184 size_type find_last_of(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
Marshall Clow1e00d6d2016-07-21 05:31:24 +00001185 _LIBCPP_INLINE_VISIBILITY
Marshall Clow0b9db072017-09-07 04:19:32 +00001186 size_type find_last_of(__self_view __sv, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001187 size_type find_last_of(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001188 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001189 size_type find_last_of(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001190 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta6119a82011-05-29 19:57:12 +00001191 size_type find_last_of(value_type __c, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001192
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001193 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta6119a82011-05-29 19:57:12 +00001194 size_type find_first_not_of(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
Marshall Clow1e00d6d2016-07-21 05:31:24 +00001195 _LIBCPP_INLINE_VISIBILITY
1196 size_type find_first_not_of(__self_view __sv, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001197 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 +00001198 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001199 size_type find_first_not_of(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnanta6119a82011-05-29 19:57:12 +00001200 _LIBCPP_INLINE_VISIBILITY
1201 size_type find_first_not_of(value_type __c, size_type __pos = 0) const _NOEXCEPT;
1202
1203 _LIBCPP_INLINE_VISIBILITY
1204 size_type find_last_not_of(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
Marshall Clow1e00d6d2016-07-21 05:31:24 +00001205 _LIBCPP_INLINE_VISIBILITY
Marshall Clow0b9db072017-09-07 04:19:32 +00001206 size_type find_last_not_of(__self_view __sv, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001207 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 +00001208 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001209 size_type find_last_not_of(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnanta6119a82011-05-29 19:57:12 +00001210 _LIBCPP_INLINE_VISIBILITY
1211 size_type find_last_not_of(value_type __c, size_type __pos = npos) const _NOEXCEPT;
1212
1213 _LIBCPP_INLINE_VISIBILITY
1214 int compare(const basic_string& __str) const _NOEXCEPT;
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001215 _LIBCPP_INLINE_VISIBILITY
Marshall Clow1e00d6d2016-07-21 05:31:24 +00001216 int compare(__self_view __sv) const _NOEXCEPT;
1217 _LIBCPP_INLINE_VISIBILITY
1218 int compare(size_type __pos1, size_type __n1, __self_view __sv) const;
1219 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001220 int compare(size_type __pos1, size_type __n1, const basic_string& __str) const;
Marshall Clowa93b5e22014-03-04 19:17:19 +00001221 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 +00001222 template <class _Tp>
Eric Fiselier9dbc0532016-10-14 05:29:46 +00001223 inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow6ac8de02016-09-24 22:45:42 +00001224 typename enable_if
1225 <
1226 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1227 int
1228 >::type
1229 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 +00001230 int compare(const value_type* __s) const _NOEXCEPT;
1231 int compare(size_type __pos1, size_type __n1, const value_type* __s) const;
1232 int compare(size_type __pos1, size_type __n1, const value_type* __s, size_type __n2) const;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001233
Marshall Clow46b4ad52017-12-04 20:11:38 +00001234#if _LIBCPP_STD_VER > 17
1235 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1236 bool starts_with(__self_view __sv) const _NOEXCEPT
1237 { return __self_view(data(), size()).starts_with(__sv); }
1238
1239 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1240 bool starts_with(value_type __c) const _NOEXCEPT
1241 { return !empty() && _Traits::eq(front(), __c); }
1242
1243 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1244 bool starts_with(const value_type* __s) const _NOEXCEPT
1245 { return starts_with(__self_view(__s)); }
1246
1247 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1248 bool ends_with(__self_view __sv) const _NOEXCEPT
1249 { return __self_view(data(), size()).ends_with( __sv); }
1250
1251 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1252 bool ends_with(value_type __c) const _NOEXCEPT
1253 { return !empty() && _Traits::eq(back(), __c); }
1254
1255 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1256 bool ends_with(const value_type* __s) const _NOEXCEPT
1257 { return ends_with(__self_view(__s)); }
1258#endif
1259
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001260 _LIBCPP_INLINE_VISIBILITY bool __invariants() const;
Howard Hinnant08dd2532013-04-22 23:55:13 +00001261
Vedant Kumar2b588cb2018-03-08 21:15:26 +00001262 _LIBCPP_INLINE_VISIBILITY void __clear_and_shrink();
1263
Howard Hinnant08dd2532013-04-22 23:55:13 +00001264 _LIBCPP_INLINE_VISIBILITY
1265 bool __is_long() const _NOEXCEPT
1266 {return bool(__r_.first().__s.__size_ & __short_mask);}
1267
Howard Hinnant499cea12013-08-23 17:37:05 +00001268#if _LIBCPP_DEBUG_LEVEL >= 2
1269
1270 bool __dereferenceable(const const_iterator* __i) const;
1271 bool __decrementable(const const_iterator* __i) const;
1272 bool __addable(const const_iterator* __i, ptrdiff_t __n) const;
1273 bool __subscriptable(const const_iterator* __i, ptrdiff_t __n) const;
1274
1275#endif // _LIBCPP_DEBUG_LEVEL >= 2
1276
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001277private:
Howard Hinnanta6119a82011-05-29 19:57:12 +00001278 _LIBCPP_INLINE_VISIBILITY
1279 allocator_type& __alloc() _NOEXCEPT
1280 {return __r_.second();}
1281 _LIBCPP_INLINE_VISIBILITY
1282 const allocator_type& __alloc() const _NOEXCEPT
1283 {return __r_.second();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001284
Evgeniy Stepanov4f01aa82015-10-13 23:48:28 +00001285#ifdef _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant15467182013-04-30 21:44:48 +00001286
Howard Hinnanta6119a82011-05-29 19:57:12 +00001287 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta6119a82011-05-29 19:57:12 +00001288 void __set_short_size(size_type __s) _NOEXCEPT
Eric Fiselier5ccf0432017-10-17 13:16:01 +00001289# ifdef _LIBCPP_BIG_ENDIAN
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001290 {__r_.first().__s.__size_ = (unsigned char)(__s << 1);}
Howard Hinnant15467182013-04-30 21:44:48 +00001291# else
1292 {__r_.first().__s.__size_ = (unsigned char)(__s);}
1293# endif
1294
Howard Hinnanta6119a82011-05-29 19:57:12 +00001295 _LIBCPP_INLINE_VISIBILITY
1296 size_type __get_short_size() const _NOEXCEPT
Eric Fiselier5ccf0432017-10-17 13:16:01 +00001297# ifdef _LIBCPP_BIG_ENDIAN
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001298 {return __r_.first().__s.__size_ >> 1;}
Howard Hinnant15467182013-04-30 21:44:48 +00001299# else
1300 {return __r_.first().__s.__size_;}
1301# endif
1302
Evgeniy Stepanov4f01aa82015-10-13 23:48:28 +00001303#else // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant15467182013-04-30 21:44:48 +00001304
1305 _LIBCPP_INLINE_VISIBILITY
1306 void __set_short_size(size_type __s) _NOEXCEPT
Eric Fiselier5ccf0432017-10-17 13:16:01 +00001307# ifdef _LIBCPP_BIG_ENDIAN
Howard Hinnant15467182013-04-30 21:44:48 +00001308 {__r_.first().__s.__size_ = (unsigned char)(__s);}
1309# else
1310 {__r_.first().__s.__size_ = (unsigned char)(__s << 1);}
1311# endif
1312
1313 _LIBCPP_INLINE_VISIBILITY
1314 size_type __get_short_size() const _NOEXCEPT
Eric Fiselier5ccf0432017-10-17 13:16:01 +00001315# ifdef _LIBCPP_BIG_ENDIAN
Howard Hinnant15467182013-04-30 21:44:48 +00001316 {return __r_.first().__s.__size_;}
1317# else
1318 {return __r_.first().__s.__size_ >> 1;}
1319# endif
1320
Evgeniy Stepanov4f01aa82015-10-13 23:48:28 +00001321#endif // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant15467182013-04-30 21:44:48 +00001322
Howard Hinnanta6119a82011-05-29 19:57:12 +00001323 _LIBCPP_INLINE_VISIBILITY
1324 void __set_long_size(size_type __s) _NOEXCEPT
1325 {__r_.first().__l.__size_ = __s;}
1326 _LIBCPP_INLINE_VISIBILITY
1327 size_type __get_long_size() const _NOEXCEPT
1328 {return __r_.first().__l.__size_;}
1329 _LIBCPP_INLINE_VISIBILITY
1330 void __set_size(size_type __s) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001331 {if (__is_long()) __set_long_size(__s); else __set_short_size(__s);}
1332
Howard Hinnanta6119a82011-05-29 19:57:12 +00001333 _LIBCPP_INLINE_VISIBILITY
1334 void __set_long_cap(size_type __s) _NOEXCEPT
1335 {__r_.first().__l.__cap_ = __long_mask | __s;}
1336 _LIBCPP_INLINE_VISIBILITY
1337 size_type __get_long_cap() const _NOEXCEPT
Howard Hinnantec3773c2011-12-01 20:21:04 +00001338 {return __r_.first().__l.__cap_ & size_type(~__long_mask);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001339
Howard Hinnanta6119a82011-05-29 19:57:12 +00001340 _LIBCPP_INLINE_VISIBILITY
1341 void __set_long_pointer(pointer __p) _NOEXCEPT
1342 {__r_.first().__l.__data_ = __p;}
1343 _LIBCPP_INLINE_VISIBILITY
1344 pointer __get_long_pointer() _NOEXCEPT
1345 {return __r_.first().__l.__data_;}
1346 _LIBCPP_INLINE_VISIBILITY
1347 const_pointer __get_long_pointer() const _NOEXCEPT
1348 {return __r_.first().__l.__data_;}
1349 _LIBCPP_INLINE_VISIBILITY
1350 pointer __get_short_pointer() _NOEXCEPT
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001351 {return pointer_traits<pointer>::pointer_to(__r_.first().__s.__data_[0]);}
Howard Hinnanta6119a82011-05-29 19:57:12 +00001352 _LIBCPP_INLINE_VISIBILITY
1353 const_pointer __get_short_pointer() const _NOEXCEPT
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001354 {return pointer_traits<const_pointer>::pointer_to(__r_.first().__s.__data_[0]);}
Howard Hinnanta6119a82011-05-29 19:57:12 +00001355 _LIBCPP_INLINE_VISIBILITY
1356 pointer __get_pointer() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001357 {return __is_long() ? __get_long_pointer() : __get_short_pointer();}
Howard Hinnanta6119a82011-05-29 19:57:12 +00001358 _LIBCPP_INLINE_VISIBILITY
1359 const_pointer __get_pointer() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001360 {return __is_long() ? __get_long_pointer() : __get_short_pointer();}
1361
Howard Hinnanta6119a82011-05-29 19:57:12 +00001362 _LIBCPP_INLINE_VISIBILITY
1363 void __zero() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001364 {
1365 size_type (&__a)[__n_words] = __r_.first().__r.__words;
1366 for (unsigned __i = 0; __i < __n_words; ++__i)
1367 __a[__i] = 0;
1368 }
1369
1370 template <size_type __a> static
Howard Hinnanta6119a82011-05-29 19:57:12 +00001371 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7f764502013-08-14 18:00:20 +00001372 size_type __align_it(size_type __s) _NOEXCEPT
Eric Fiseliere2d48922015-08-28 07:02:42 +00001373 {return (__s + (__a-1)) & ~(__a-1);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001374 enum {__alignment = 16};
Howard Hinnanta6119a82011-05-29 19:57:12 +00001375 static _LIBCPP_INLINE_VISIBILITY
1376 size_type __recommend(size_type __s) _NOEXCEPT
Marshall Clow088e6012018-02-07 21:30:17 +00001377 {
1378 if (__s < __min_cap) return static_cast<size_type>(__min_cap) - 1;
1379 size_type __guess = __align_it<sizeof(value_type) < __alignment ?
1380 __alignment/sizeof(value_type) : 1 > (__s+1) - 1;
1381 if (__guess == __min_cap) ++__guess;
1382 return __guess;
1383 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001384
Duncan P. N. Exon Smithed3c0e62017-04-01 03:20:48 +00001385 inline
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001386 void __init(const value_type* __s, size_type __sz, size_type __reserve);
Duncan P. N. Exon Smithed3c0e62017-04-01 03:20:48 +00001387 inline
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001388 void __init(const value_type* __s, size_type __sz);
Duncan P. N. Exon Smithed3c0e62017-04-01 03:20:48 +00001389 inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001390 void __init(size_type __n, value_type __c);
Howard Hinnant324bb032010-08-22 00:02:43 +00001391
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001392 template <class _InputIterator>
Duncan P. N. Exon Smithed3c0e62017-04-01 03:20:48 +00001393 inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001394 typename enable_if
1395 <
Marshall Clowdf9db312016-01-13 21:54:34 +00001396 __is_exactly_input_iterator<_InputIterator>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001397 void
1398 >::type
1399 __init(_InputIterator __first, _InputIterator __last);
1400
1401 template <class _ForwardIterator>
Duncan P. N. Exon Smithed3c0e62017-04-01 03:20:48 +00001402 inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001403 typename enable_if
1404 <
1405 __is_forward_iterator<_ForwardIterator>::value,
1406 void
1407 >::type
1408 __init(_ForwardIterator __first, _ForwardIterator __last);
1409
1410 void __grow_by(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
Howard Hinnant324bb032010-08-22 00:02:43 +00001411 size_type __n_copy, size_type __n_del, size_type __n_add = 0);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001412 void __grow_by_and_replace(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
1413 size_type __n_copy, size_type __n_del,
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001414 size_type __n_add, const value_type* __p_new_stuff);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001415
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001416 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001417 void __erase_to_end(size_type __pos);
1418
Howard Hinnante32b5e22010-11-17 17:55:08 +00001419 _LIBCPP_INLINE_VISIBILITY
1420 void __copy_assign_alloc(const basic_string& __str)
1421 {__copy_assign_alloc(__str, integral_constant<bool,
1422 __alloc_traits::propagate_on_container_copy_assignment::value>());}
1423
1424 _LIBCPP_INLINE_VISIBILITY
1425 void __copy_assign_alloc(const basic_string& __str, true_type)
1426 {
Marshall Clow9247fd22017-01-31 03:40:52 +00001427 if (__alloc() == __str.__alloc())
1428 __alloc() = __str.__alloc();
1429 else
Howard Hinnante32b5e22010-11-17 17:55:08 +00001430 {
Marshall Clow9247fd22017-01-31 03:40:52 +00001431 if (!__str.__is_long())
1432 {
Vedant Kumar2b588cb2018-03-08 21:15:26 +00001433 __clear_and_shrink();
Marshall Clow9247fd22017-01-31 03:40:52 +00001434 __alloc() = __str.__alloc();
1435 }
1436 else
1437 {
1438 allocator_type __a = __str.__alloc();
1439 pointer __p = __alloc_traits::allocate(__a, __str.__get_long_cap());
Vedant Kumar2b588cb2018-03-08 21:15:26 +00001440 __clear_and_shrink();
Marshall Clow9247fd22017-01-31 03:40:52 +00001441 __alloc() = _VSTD::move(__a);
1442 __set_long_pointer(__p);
1443 __set_long_cap(__str.__get_long_cap());
1444 __set_long_size(__str.size());
1445 }
Howard Hinnante32b5e22010-11-17 17:55:08 +00001446 }
Howard Hinnante32b5e22010-11-17 17:55:08 +00001447 }
1448
1449 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantec3773c2011-12-01 20:21:04 +00001450 void __copy_assign_alloc(const basic_string&, false_type) _NOEXCEPT
Howard Hinnante32b5e22010-11-17 17:55:08 +00001451 {}
1452
Eric Fiselier3e928972017-04-19 00:28:44 +00001453#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001454 _LIBCPP_INLINE_VISIBILITY
Marshall Clowaf961ed2015-08-18 18:57:00 +00001455 void __move_assign(basic_string& __str, false_type)
1456 _NOEXCEPT_(__alloc_traits::is_always_equal::value);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001457 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant53f7d4c2011-06-03 18:40:47 +00001458 void __move_assign(basic_string& __str, true_type)
Marshall Clowaf961ed2015-08-18 18:57:00 +00001459#if _LIBCPP_STD_VER > 14
1460 _NOEXCEPT;
1461#else
Howard Hinnant53f7d4c2011-06-03 18:40:47 +00001462 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnante32b5e22010-11-17 17:55:08 +00001463#endif
Marshall Clowaf961ed2015-08-18 18:57:00 +00001464#endif
Howard Hinnante32b5e22010-11-17 17:55:08 +00001465
1466 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3fdbbd22011-08-17 20:36:18 +00001467 void
Howard Hinnant9cbee432011-09-02 20:42:31 +00001468 __move_assign_alloc(basic_string& __str)
Howard Hinnant3fdbbd22011-08-17 20:36:18 +00001469 _NOEXCEPT_(
1470 !__alloc_traits::propagate_on_container_move_assignment::value ||
1471 is_nothrow_move_assignable<allocator_type>::value)
1472 {__move_assign_alloc(__str, integral_constant<bool,
1473 __alloc_traits::propagate_on_container_move_assignment::value>());}
1474
1475 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9cbee432011-09-02 20:42:31 +00001476 void __move_assign_alloc(basic_string& __c, true_type)
Howard Hinnant3fdbbd22011-08-17 20:36:18 +00001477 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
1478 {
1479 __alloc() = _VSTD::move(__c.__alloc());
1480 }
1481
1482 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantec3773c2011-12-01 20:21:04 +00001483 void __move_assign_alloc(basic_string&, false_type)
Howard Hinnant3fdbbd22011-08-17 20:36:18 +00001484 _NOEXCEPT
1485 {}
1486
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001487 _LIBCPP_INLINE_VISIBILITY void __invalidate_all_iterators();
1488 _LIBCPP_INLINE_VISIBILITY void __invalidate_iterators_past(size_type);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001489
1490 friend basic_string operator+<>(const basic_string&, const basic_string&);
1491 friend basic_string operator+<>(const value_type*, const basic_string&);
1492 friend basic_string operator+<>(value_type, const basic_string&);
1493 friend basic_string operator+<>(const basic_string&, const value_type*);
1494 friend basic_string operator+<>(const basic_string&, value_type);
1495};
1496
Marshall Clow5b1e87e2018-02-08 06:34:03 +00001497#ifndef _LIBCPP_HAS_NO_DEDUCTION_GUIDES
1498template<class _InputIterator,
1499 class _CharT = typename iterator_traits<_InputIterator>::value_type,
1500 class _Allocator = allocator<_CharT>,
1501 class = typename enable_if<__is_input_iterator<_InputIterator>::value, void>::type,
1502 class = typename enable_if<__is_allocator<_Allocator>::value, void>::type
1503 >
1504basic_string(_InputIterator, _InputIterator, _Allocator = _Allocator())
1505 -> basic_string<_CharT, char_traits<_CharT>, _Allocator>;
1506#endif
1507
1508
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001509template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001510inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001511void
1512basic_string<_CharT, _Traits, _Allocator>::__invalidate_all_iterators()
1513{
Howard Hinnant499cea12013-08-23 17:37:05 +00001514#if _LIBCPP_DEBUG_LEVEL >= 2
1515 __get_db()->__invalidate_all(this);
1516#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001517}
1518
1519template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001520inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001521void
Howard Hinnantec3773c2011-12-01 20:21:04 +00001522basic_string<_CharT, _Traits, _Allocator>::__invalidate_iterators_past(size_type
Howard Hinnant499cea12013-08-23 17:37:05 +00001523#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantec3773c2011-12-01 20:21:04 +00001524 __pos
1525#endif
1526 )
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001527{
Howard Hinnant499cea12013-08-23 17:37:05 +00001528#if _LIBCPP_DEBUG_LEVEL >= 2
1529 __c_node* __c = __get_db()->__find_c_and_lock(this);
1530 if (__c)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001531 {
Howard Hinnant499cea12013-08-23 17:37:05 +00001532 const_pointer __new_last = __get_pointer() + __pos;
1533 for (__i_node** __p = __c->end_; __p != __c->beg_; )
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001534 {
Howard Hinnant499cea12013-08-23 17:37:05 +00001535 --__p;
1536 const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_);
1537 if (__i->base() > __new_last)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001538 {
Howard Hinnant499cea12013-08-23 17:37:05 +00001539 (*__p)->__c_ = nullptr;
1540 if (--__c->end_ != __p)
1541 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001542 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001543 }
Howard Hinnant499cea12013-08-23 17:37:05 +00001544 __get_db()->unlock();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001545 }
Howard Hinnant499cea12013-08-23 17:37:05 +00001546#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001547}
1548
1549template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001550inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant53f7d4c2011-06-03 18:40:47 +00001551basic_string<_CharT, _Traits, _Allocator>::basic_string()
Marshall Clowc912c0c2015-06-04 02:05:41 +00001552 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001553{
Howard Hinnant499cea12013-08-23 17:37:05 +00001554#if _LIBCPP_DEBUG_LEVEL >= 2
1555 __get_db()->__insert_c(this);
1556#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001557 __zero();
1558}
1559
1560template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001561inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001562basic_string<_CharT, _Traits, _Allocator>::basic_string(const allocator_type& __a)
Eric Fiselier692177d2015-07-18 20:40:46 +00001563#if _LIBCPP_STD_VER <= 14
1564 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value)
1565#else
1566 _NOEXCEPT
1567#endif
Eric Fiselierdb14bcc2017-04-12 23:45:53 +00001568: __r_(__second_tag(), __a)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001569{
Howard Hinnant499cea12013-08-23 17:37:05 +00001570#if _LIBCPP_DEBUG_LEVEL >= 2
1571 __get_db()->__insert_c(this);
1572#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001573 __zero();
1574}
1575
1576template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier6dbed462016-09-16 00:00:48 +00001577void basic_string<_CharT, _Traits, _Allocator>::__init(const value_type* __s,
1578 size_type __sz,
1579 size_type __reserve)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001580{
1581 if (__reserve > max_size())
1582 this->__throw_length_error();
1583 pointer __p;
1584 if (__reserve < __min_cap)
1585 {
1586 __set_short_size(__sz);
1587 __p = __get_short_pointer();
1588 }
1589 else
1590 {
1591 size_type __cap = __recommend(__reserve);
Howard Hinnante32b5e22010-11-17 17:55:08 +00001592 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001593 __set_long_pointer(__p);
1594 __set_long_cap(__cap+1);
1595 __set_long_size(__sz);
1596 }
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001597 traits_type::copy(_VSTD::__to_raw_pointer(__p), __s, __sz);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001598 traits_type::assign(__p[__sz], value_type());
1599}
1600
1601template <class _CharT, class _Traits, class _Allocator>
1602void
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001603basic_string<_CharT, _Traits, _Allocator>::__init(const value_type* __s, size_type __sz)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001604{
1605 if (__sz > max_size())
1606 this->__throw_length_error();
1607 pointer __p;
1608 if (__sz < __min_cap)
1609 {
1610 __set_short_size(__sz);
1611 __p = __get_short_pointer();
1612 }
1613 else
1614 {
1615 size_type __cap = __recommend(__sz);
Howard Hinnante32b5e22010-11-17 17:55:08 +00001616 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001617 __set_long_pointer(__p);
1618 __set_long_cap(__cap+1);
1619 __set_long_size(__sz);
1620 }
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001621 traits_type::copy(_VSTD::__to_raw_pointer(__p), __s, __sz);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001622 traits_type::assign(__p[__sz], value_type());
1623}
1624
1625template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001626inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier0eaf2e82017-02-17 01:17:10 +00001627basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001628{
Howard Hinnant499cea12013-08-23 17:37:05 +00001629 _LIBCPP_ASSERT(__s != nullptr, "basic_string(const char*) detected nullptr");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001630 __init(__s, traits_type::length(__s));
Howard Hinnant499cea12013-08-23 17:37:05 +00001631#if _LIBCPP_DEBUG_LEVEL >= 2
1632 __get_db()->__insert_c(this);
1633#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001634}
1635
1636template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001637inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier0eaf2e82017-02-17 01:17:10 +00001638basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, const _Allocator& __a)
Eric Fiselierdb14bcc2017-04-12 23:45:53 +00001639 : __r_(__second_tag(), __a)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001640{
Howard Hinnant499cea12013-08-23 17:37:05 +00001641 _LIBCPP_ASSERT(__s != nullptr, "basic_string(const char*, allocator) detected nullptr");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001642 __init(__s, traits_type::length(__s));
Howard Hinnant499cea12013-08-23 17:37:05 +00001643#if _LIBCPP_DEBUG_LEVEL >= 2
1644 __get_db()->__insert_c(this);
1645#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001646}
1647
1648template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001649inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier0eaf2e82017-02-17 01:17:10 +00001650basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, size_type __n)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001651{
Howard Hinnant499cea12013-08-23 17:37:05 +00001652 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "basic_string(const char*, n) detected nullptr");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001653 __init(__s, __n);
Howard Hinnant499cea12013-08-23 17:37:05 +00001654#if _LIBCPP_DEBUG_LEVEL >= 2
1655 __get_db()->__insert_c(this);
1656#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001657}
1658
1659template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001660inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier0eaf2e82017-02-17 01:17:10 +00001661basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, size_type __n, const _Allocator& __a)
Eric Fiselierdb14bcc2017-04-12 23:45:53 +00001662 : __r_(__second_tag(), __a)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001663{
Howard Hinnant499cea12013-08-23 17:37:05 +00001664 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "basic_string(const char*, n, allocator) detected nullptr");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001665 __init(__s, __n);
Howard Hinnant499cea12013-08-23 17:37:05 +00001666#if _LIBCPP_DEBUG_LEVEL >= 2
1667 __get_db()->__insert_c(this);
1668#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001669}
1670
1671template <class _CharT, class _Traits, class _Allocator>
1672basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str)
Eric Fiselierdb14bcc2017-04-12 23:45:53 +00001673 : __r_(__second_tag(), __alloc_traits::select_on_container_copy_construction(__str.__alloc()))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001674{
1675 if (!__str.__is_long())
1676 __r_.first().__r = __str.__r_.first().__r;
1677 else
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001678 __init(_VSTD::__to_raw_pointer(__str.__get_long_pointer()), __str.__get_long_size());
Howard Hinnant499cea12013-08-23 17:37:05 +00001679#if _LIBCPP_DEBUG_LEVEL >= 2
1680 __get_db()->__insert_c(this);
1681#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001682}
1683
1684template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier0eaf2e82017-02-17 01:17:10 +00001685basic_string<_CharT, _Traits, _Allocator>::basic_string(
1686 const basic_string& __str, const allocator_type& __a)
Eric Fiselierdb14bcc2017-04-12 23:45:53 +00001687 : __r_(__second_tag(), __a)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001688{
1689 if (!__str.__is_long())
1690 __r_.first().__r = __str.__r_.first().__r;
1691 else
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001692 __init(_VSTD::__to_raw_pointer(__str.__get_long_pointer()), __str.__get_long_size());
Howard Hinnant499cea12013-08-23 17:37:05 +00001693#if _LIBCPP_DEBUG_LEVEL >= 2
1694 __get_db()->__insert_c(this);
1695#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001696}
1697
Eric Fiselier3e928972017-04-19 00:28:44 +00001698#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001699
1700template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001701inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant53f7d4c2011-06-03 18:40:47 +00001702basic_string<_CharT, _Traits, _Allocator>::basic_string(basic_string&& __str)
Marshall Clow7b193f72015-06-03 19:56:43 +00001703#if _LIBCPP_STD_VER <= 14
Howard Hinnant53f7d4c2011-06-03 18:40:47 +00001704 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Marshall Clow7b193f72015-06-03 19:56:43 +00001705#else
1706 _NOEXCEPT
1707#endif
Howard Hinnant0949eed2011-06-30 21:18:19 +00001708 : __r_(_VSTD::move(__str.__r_))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001709{
1710 __str.__zero();
Howard Hinnant499cea12013-08-23 17:37:05 +00001711#if _LIBCPP_DEBUG_LEVEL >= 2
1712 __get_db()->__insert_c(this);
1713 if (__is_long())
1714 __get_db()->swap(this, &__str);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001715#endif
1716}
1717
1718template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001719inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001720basic_string<_CharT, _Traits, _Allocator>::basic_string(basic_string&& __str, const allocator_type& __a)
Eric Fiselierdb14bcc2017-04-12 23:45:53 +00001721 : __r_(__second_tag(), __a)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001722{
Marshall Clowd5549cc2014-07-17 15:32:20 +00001723 if (__str.__is_long() && __a != __str.__alloc()) // copy, not move
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001724 __init(_VSTD::__to_raw_pointer(__str.__get_long_pointer()), __str.__get_long_size());
Marshall Clowd5549cc2014-07-17 15:32:20 +00001725 else
1726 {
1727 __r_.first().__r = __str.__r_.first().__r;
1728 __str.__zero();
1729 }
Howard Hinnant499cea12013-08-23 17:37:05 +00001730#if _LIBCPP_DEBUG_LEVEL >= 2
1731 __get_db()->__insert_c(this);
1732 if (__is_long())
1733 __get_db()->swap(this, &__str);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001734#endif
1735}
1736
Eric Fiselier3e928972017-04-19 00:28:44 +00001737#endif // _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001738
1739template <class _CharT, class _Traits, class _Allocator>
1740void
1741basic_string<_CharT, _Traits, _Allocator>::__init(size_type __n, value_type __c)
1742{
1743 if (__n > max_size())
1744 this->__throw_length_error();
1745 pointer __p;
1746 if (__n < __min_cap)
1747 {
1748 __set_short_size(__n);
1749 __p = __get_short_pointer();
1750 }
1751 else
1752 {
1753 size_type __cap = __recommend(__n);
Howard Hinnante32b5e22010-11-17 17:55:08 +00001754 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001755 __set_long_pointer(__p);
1756 __set_long_cap(__cap+1);
1757 __set_long_size(__n);
1758 }
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001759 traits_type::assign(_VSTD::__to_raw_pointer(__p), __n, __c);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001760 traits_type::assign(__p[__n], value_type());
1761}
1762
1763template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001764inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier0eaf2e82017-02-17 01:17:10 +00001765basic_string<_CharT, _Traits, _Allocator>::basic_string(size_type __n, _CharT __c)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001766{
1767 __init(__n, __c);
Howard Hinnant499cea12013-08-23 17:37:05 +00001768#if _LIBCPP_DEBUG_LEVEL >= 2
1769 __get_db()->__insert_c(this);
1770#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001771}
1772
1773template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001774inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier0eaf2e82017-02-17 01:17:10 +00001775basic_string<_CharT, _Traits, _Allocator>::basic_string(size_type __n, _CharT __c, const _Allocator& __a)
Eric Fiselierdb14bcc2017-04-12 23:45:53 +00001776 : __r_(__second_tag(), __a)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001777{
1778 __init(__n, __c);
Howard Hinnant499cea12013-08-23 17:37:05 +00001779#if _LIBCPP_DEBUG_LEVEL >= 2
1780 __get_db()->__insert_c(this);
1781#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001782}
1783
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001784template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier0eaf2e82017-02-17 01:17:10 +00001785basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str,
1786 size_type __pos, size_type __n,
1787 const _Allocator& __a)
Eric Fiselierdb14bcc2017-04-12 23:45:53 +00001788 : __r_(__second_tag(), __a)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001789{
1790 size_type __str_sz = __str.size();
1791 if (__pos > __str_sz)
1792 this->__throw_out_of_range();
Howard Hinnant0949eed2011-06-30 21:18:19 +00001793 __init(__str.data() + __pos, _VSTD::min(__n, __str_sz - __pos));
Howard Hinnant499cea12013-08-23 17:37:05 +00001794#if _LIBCPP_DEBUG_LEVEL >= 2
1795 __get_db()->__insert_c(this);
1796#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001797}
1798
1799template <class _CharT, class _Traits, class _Allocator>
Marshall Clowf4f7d8f2016-04-07 18:13:41 +00001800inline _LIBCPP_INLINE_VISIBILITY
1801basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str, size_type __pos,
Eric Fiselier0eaf2e82017-02-17 01:17:10 +00001802 const _Allocator& __a)
Eric Fiselierdb14bcc2017-04-12 23:45:53 +00001803 : __r_(__second_tag(), __a)
Marshall Clowf4f7d8f2016-04-07 18:13:41 +00001804{
1805 size_type __str_sz = __str.size();
1806 if (__pos > __str_sz)
1807 this->__throw_out_of_range();
1808 __init(__str.data() + __pos, __str_sz - __pos);
1809#if _LIBCPP_DEBUG_LEVEL >= 2
1810 __get_db()->__insert_c(this);
1811#endif
1812}
1813
1814template <class _CharT, class _Traits, class _Allocator>
Marshall Clowdb7fa112016-11-14 18:22:19 +00001815template <class _Tp>
1816basic_string<_CharT, _Traits, _Allocator>::basic_string(
1817 const _Tp& __t, size_type __pos, size_type __n, const allocator_type& __a,
Marshall Clowf1729d92017-11-15 20:02:27 +00001818 typename enable_if<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value, void>::type *)
Eric Fiselierdb14bcc2017-04-12 23:45:53 +00001819 : __r_(__second_tag(), __a)
Marshall Clowdb7fa112016-11-14 18:22:19 +00001820{
Marshall Clowf1729d92017-11-15 20:02:27 +00001821 __self_view __sv = __self_view(__t).substr(__pos, __n);
Marshall Clowdb7fa112016-11-14 18:22:19 +00001822 __init(__sv.data(), __sv.size());
1823#if _LIBCPP_DEBUG_LEVEL >= 2
1824 __get_db()->__insert_c(this);
Eric Fiselier0eaf2e82017-02-17 01:17:10 +00001825#endif
Marshall Clowdb7fa112016-11-14 18:22:19 +00001826}
1827
1828template <class _CharT, class _Traits, class _Allocator>
Marshall Clow1e00d6d2016-07-21 05:31:24 +00001829inline _LIBCPP_INLINE_VISIBILITY
1830basic_string<_CharT, _Traits, _Allocator>::basic_string(__self_view __sv)
1831{
1832 __init(__sv.data(), __sv.size());
1833#if _LIBCPP_DEBUG_LEVEL >= 2
1834 __get_db()->__insert_c(this);
1835#endif
1836}
1837
1838template <class _CharT, class _Traits, class _Allocator>
1839inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier0eaf2e82017-02-17 01:17:10 +00001840basic_string<_CharT, _Traits, _Allocator>::basic_string(__self_view __sv, const _Allocator& __a)
Eric Fiselierdb14bcc2017-04-12 23:45:53 +00001841 : __r_(__second_tag(), __a)
Marshall Clow1e00d6d2016-07-21 05:31:24 +00001842{
1843 __init(__sv.data(), __sv.size());
1844#if _LIBCPP_DEBUG_LEVEL >= 2
1845 __get_db()->__insert_c(this);
1846#endif
1847}
1848
1849template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001850template <class _InputIterator>
1851typename enable_if
1852<
Marshall Clowdf9db312016-01-13 21:54:34 +00001853 __is_exactly_input_iterator<_InputIterator>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001854 void
1855>::type
1856basic_string<_CharT, _Traits, _Allocator>::__init(_InputIterator __first, _InputIterator __last)
1857{
1858 __zero();
1859#ifndef _LIBCPP_NO_EXCEPTIONS
1860 try
1861 {
Howard Hinnant324bb032010-08-22 00:02:43 +00001862#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001863 for (; __first != __last; ++__first)
1864 push_back(*__first);
1865#ifndef _LIBCPP_NO_EXCEPTIONS
1866 }
1867 catch (...)
1868 {
1869 if (__is_long())
Howard Hinnante32b5e22010-11-17 17:55:08 +00001870 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001871 throw;
1872 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001873#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001874}
1875
1876template <class _CharT, class _Traits, class _Allocator>
1877template <class _ForwardIterator>
1878typename enable_if
1879<
1880 __is_forward_iterator<_ForwardIterator>::value,
1881 void
1882>::type
1883basic_string<_CharT, _Traits, _Allocator>::__init(_ForwardIterator __first, _ForwardIterator __last)
1884{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001885 size_type __sz = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001886 if (__sz > max_size())
1887 this->__throw_length_error();
1888 pointer __p;
1889 if (__sz < __min_cap)
1890 {
1891 __set_short_size(__sz);
1892 __p = __get_short_pointer();
1893 }
1894 else
1895 {
1896 size_type __cap = __recommend(__sz);
Howard Hinnante32b5e22010-11-17 17:55:08 +00001897 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001898 __set_long_pointer(__p);
1899 __set_long_cap(__cap+1);
1900 __set_long_size(__sz);
1901 }
Eric Fiselierb9919752014-10-27 19:28:20 +00001902 for (; __first != __last; ++__first, (void) ++__p)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001903 traits_type::assign(*__p, *__first);
1904 traits_type::assign(*__p, value_type());
1905}
1906
1907template <class _CharT, class _Traits, class _Allocator>
1908template<class _InputIterator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001909inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001910basic_string<_CharT, _Traits, _Allocator>::basic_string(_InputIterator __first, _InputIterator __last)
1911{
1912 __init(__first, __last);
Howard Hinnant499cea12013-08-23 17:37:05 +00001913#if _LIBCPP_DEBUG_LEVEL >= 2
1914 __get_db()->__insert_c(this);
1915#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001916}
1917
1918template <class _CharT, class _Traits, class _Allocator>
1919template<class _InputIterator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001920inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001921basic_string<_CharT, _Traits, _Allocator>::basic_string(_InputIterator __first, _InputIterator __last,
1922 const allocator_type& __a)
Eric Fiselierdb14bcc2017-04-12 23:45:53 +00001923 : __r_(__second_tag(), __a)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001924{
1925 __init(__first, __last);
Howard Hinnant499cea12013-08-23 17:37:05 +00001926#if _LIBCPP_DEBUG_LEVEL >= 2
1927 __get_db()->__insert_c(this);
1928#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001929}
1930
Eric Fiselier3e928972017-04-19 00:28:44 +00001931#ifndef _LIBCPP_CXX03_LANG
Howard Hinnante3e32912011-08-12 21:56:02 +00001932
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001933template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001934inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier0eaf2e82017-02-17 01:17:10 +00001935basic_string<_CharT, _Traits, _Allocator>::basic_string(
1936 initializer_list<_CharT> __il)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001937{
1938 __init(__il.begin(), __il.end());
Howard Hinnant499cea12013-08-23 17:37:05 +00001939#if _LIBCPP_DEBUG_LEVEL >= 2
1940 __get_db()->__insert_c(this);
1941#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001942}
1943
1944template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001945inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselierdb14bcc2017-04-12 23:45:53 +00001946
Eric Fiselier0eaf2e82017-02-17 01:17:10 +00001947basic_string<_CharT, _Traits, _Allocator>::basic_string(
1948 initializer_list<_CharT> __il, const _Allocator& __a)
Eric Fiselierdb14bcc2017-04-12 23:45:53 +00001949 : __r_(__second_tag(), __a)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001950{
1951 __init(__il.begin(), __il.end());
Howard Hinnant499cea12013-08-23 17:37:05 +00001952#if _LIBCPP_DEBUG_LEVEL >= 2
1953 __get_db()->__insert_c(this);
1954#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001955}
1956
Eric Fiselier3e928972017-04-19 00:28:44 +00001957#endif // _LIBCPP_CXX03_LANG
Howard Hinnante3e32912011-08-12 21:56:02 +00001958
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001959template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001960basic_string<_CharT, _Traits, _Allocator>::~basic_string()
1961{
Howard Hinnant499cea12013-08-23 17:37:05 +00001962#if _LIBCPP_DEBUG_LEVEL >= 2
1963 __get_db()->__erase_c(this);
1964#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001965 if (__is_long())
Howard Hinnante32b5e22010-11-17 17:55:08 +00001966 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001967}
1968
1969template <class _CharT, class _Traits, class _Allocator>
1970void
1971basic_string<_CharT, _Traits, _Allocator>::__grow_by_and_replace
1972 (size_type __old_cap, size_type __delta_cap, size_type __old_sz,
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001973 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 +00001974{
1975 size_type __ms = max_size();
1976 if (__delta_cap > __ms - __old_cap - 1)
1977 this->__throw_length_error();
1978 pointer __old_p = __get_pointer();
1979 size_type __cap = __old_cap < __ms / 2 - __alignment ?
Howard Hinnant0949eed2011-06-30 21:18:19 +00001980 __recommend(_VSTD::max(__old_cap + __delta_cap, 2 * __old_cap)) :
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001981 __ms - 1;
Howard Hinnante32b5e22010-11-17 17:55:08 +00001982 pointer __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001983 __invalidate_all_iterators();
1984 if (__n_copy != 0)
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001985 traits_type::copy(_VSTD::__to_raw_pointer(__p),
1986 _VSTD::__to_raw_pointer(__old_p), __n_copy);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001987 if (__n_add != 0)
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001988 traits_type::copy(_VSTD::__to_raw_pointer(__p) + __n_copy, __p_new_stuff, __n_add);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001989 size_type __sec_cp_sz = __old_sz - __n_del - __n_copy;
1990 if (__sec_cp_sz != 0)
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001991 traits_type::copy(_VSTD::__to_raw_pointer(__p) + __n_copy + __n_add,
1992 _VSTD::__to_raw_pointer(__old_p) + __n_copy + __n_del, __sec_cp_sz);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001993 if (__old_cap+1 != __min_cap)
Howard Hinnante32b5e22010-11-17 17:55:08 +00001994 __alloc_traits::deallocate(__alloc(), __old_p, __old_cap+1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001995 __set_long_pointer(__p);
1996 __set_long_cap(__cap+1);
1997 __old_sz = __n_copy + __n_add + __sec_cp_sz;
1998 __set_long_size(__old_sz);
1999 traits_type::assign(__p[__old_sz], value_type());
2000}
2001
2002template <class _CharT, class _Traits, class _Allocator>
2003void
2004basic_string<_CharT, _Traits, _Allocator>::__grow_by(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
2005 size_type __n_copy, size_type __n_del, size_type __n_add)
2006{
2007 size_type __ms = max_size();
Marshall Clowecc8d7b2013-11-06 14:24:38 +00002008 if (__delta_cap > __ms - __old_cap)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002009 this->__throw_length_error();
2010 pointer __old_p = __get_pointer();
2011 size_type __cap = __old_cap < __ms / 2 - __alignment ?
Howard Hinnant0949eed2011-06-30 21:18:19 +00002012 __recommend(_VSTD::max(__old_cap + __delta_cap, 2 * __old_cap)) :
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002013 __ms - 1;
Howard Hinnante32b5e22010-11-17 17:55:08 +00002014 pointer __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002015 __invalidate_all_iterators();
2016 if (__n_copy != 0)
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002017 traits_type::copy(_VSTD::__to_raw_pointer(__p),
2018 _VSTD::__to_raw_pointer(__old_p), __n_copy);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002019 size_type __sec_cp_sz = __old_sz - __n_del - __n_copy;
2020 if (__sec_cp_sz != 0)
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002021 traits_type::copy(_VSTD::__to_raw_pointer(__p) + __n_copy + __n_add,
2022 _VSTD::__to_raw_pointer(__old_p) + __n_copy + __n_del,
2023 __sec_cp_sz);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002024 if (__old_cap+1 != __min_cap)
Howard Hinnante32b5e22010-11-17 17:55:08 +00002025 __alloc_traits::deallocate(__alloc(), __old_p, __old_cap+1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002026 __set_long_pointer(__p);
2027 __set_long_cap(__cap+1);
2028}
2029
2030// assign
2031
2032template <class _CharT, class _Traits, class _Allocator>
2033basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002034basic_string<_CharT, _Traits, _Allocator>::assign(const value_type* __s, size_type __n)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002035{
Alp Tokerec34c482014-05-15 11:27:39 +00002036 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::assign received nullptr");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002037 size_type __cap = capacity();
2038 if (__cap >= __n)
2039 {
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002040 value_type* __p = _VSTD::__to_raw_pointer(__get_pointer());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002041 traits_type::move(__p, __s, __n);
2042 traits_type::assign(__p[__n], value_type());
2043 __set_size(__n);
2044 __invalidate_iterators_past(__n);
2045 }
2046 else
2047 {
2048 size_type __sz = size();
2049 __grow_by_and_replace(__cap, __n - __cap, __sz, 0, __sz, __n, __s);
2050 }
2051 return *this;
2052}
2053
2054template <class _CharT, class _Traits, class _Allocator>
2055basic_string<_CharT, _Traits, _Allocator>&
2056basic_string<_CharT, _Traits, _Allocator>::assign(size_type __n, value_type __c)
2057{
2058 size_type __cap = capacity();
2059 if (__cap < __n)
2060 {
2061 size_type __sz = size();
2062 __grow_by(__cap, __n - __cap, __sz, 0, __sz);
2063 }
2064 else
2065 __invalidate_iterators_past(__n);
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002066 value_type* __p = _VSTD::__to_raw_pointer(__get_pointer());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002067 traits_type::assign(__p, __n, __c);
2068 traits_type::assign(__p[__n], value_type());
2069 __set_size(__n);
2070 return *this;
2071}
2072
2073template <class _CharT, class _Traits, class _Allocator>
2074basic_string<_CharT, _Traits, _Allocator>&
2075basic_string<_CharT, _Traits, _Allocator>::operator=(value_type __c)
2076{
2077 pointer __p;
2078 if (__is_long())
2079 {
2080 __p = __get_long_pointer();
2081 __set_long_size(1);
2082 }
2083 else
2084 {
2085 __p = __get_short_pointer();
2086 __set_short_size(1);
2087 }
2088 traits_type::assign(*__p, __c);
2089 traits_type::assign(*++__p, value_type());
2090 __invalidate_iterators_past(1);
2091 return *this;
2092}
2093
2094template <class _CharT, class _Traits, class _Allocator>
Howard Hinnante32b5e22010-11-17 17:55:08 +00002095basic_string<_CharT, _Traits, _Allocator>&
2096basic_string<_CharT, _Traits, _Allocator>::operator=(const basic_string& __str)
2097{
2098 if (this != &__str)
2099 {
2100 __copy_assign_alloc(__str);
Marshall Clowf40ec902016-03-09 18:08:29 +00002101 assign(__str.data(), __str.size());
Howard Hinnante32b5e22010-11-17 17:55:08 +00002102 }
2103 return *this;
2104}
2105
Eric Fiselier3e928972017-04-19 00:28:44 +00002106#ifndef _LIBCPP_CXX03_LANG
Howard Hinnante32b5e22010-11-17 17:55:08 +00002107
2108template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002109inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnante32b5e22010-11-17 17:55:08 +00002110void
2111basic_string<_CharT, _Traits, _Allocator>::__move_assign(basic_string& __str, false_type)
Marshall Clowaf961ed2015-08-18 18:57:00 +00002112 _NOEXCEPT_(__alloc_traits::is_always_equal::value)
Howard Hinnante32b5e22010-11-17 17:55:08 +00002113{
2114 if (__alloc() != __str.__alloc())
2115 assign(__str);
2116 else
2117 __move_assign(__str, true_type());
2118}
2119
2120template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002121inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnante32b5e22010-11-17 17:55:08 +00002122void
2123basic_string<_CharT, _Traits, _Allocator>::__move_assign(basic_string& __str, true_type)
Marshall Clowaf961ed2015-08-18 18:57:00 +00002124#if _LIBCPP_STD_VER > 14
2125 _NOEXCEPT
2126#else
Howard Hinnant53f7d4c2011-06-03 18:40:47 +00002127 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Marshall Clowaf961ed2015-08-18 18:57:00 +00002128#endif
Howard Hinnante32b5e22010-11-17 17:55:08 +00002129{
Vedant Kumar2b588cb2018-03-08 21:15:26 +00002130 __clear_and_shrink();
Howard Hinnant3fdbbd22011-08-17 20:36:18 +00002131 __r_.first() = __str.__r_.first();
2132 __move_assign_alloc(__str);
Howard Hinnante32b5e22010-11-17 17:55:08 +00002133 __str.__zero();
2134}
2135
2136template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002137inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnante32b5e22010-11-17 17:55:08 +00002138basic_string<_CharT, _Traits, _Allocator>&
2139basic_string<_CharT, _Traits, _Allocator>::operator=(basic_string&& __str)
Marshall Clowaf961ed2015-08-18 18:57:00 +00002140 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
Howard Hinnante32b5e22010-11-17 17:55:08 +00002141{
2142 __move_assign(__str, integral_constant<bool,
2143 __alloc_traits::propagate_on_container_move_assignment::value>());
2144 return *this;
2145}
2146
2147#endif
2148
2149template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002150template<class _InputIterator>
2151typename enable_if
2152<
Marshall Clowdf9db312016-01-13 21:54:34 +00002153 __is_exactly_input_iterator <_InputIterator>::value
2154 || !__libcpp_string_gets_noexcept_iterator<_InputIterator>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002155 basic_string<_CharT, _Traits, _Allocator>&
2156>::type
2157basic_string<_CharT, _Traits, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
2158{
Marshall Clowd979eed2016-09-05 01:54:30 +00002159 const basic_string __temp(__first, __last, __alloc());
Marshall Clowdf9db312016-01-13 21:54:34 +00002160 assign(__temp.data(), __temp.size());
Argyrios Kyrtzidis1dc6f7a2012-10-13 02:03:45 +00002161 return *this;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002162}
2163
2164template <class _CharT, class _Traits, class _Allocator>
2165template<class _ForwardIterator>
2166typename enable_if
2167<
Marshall Clowdf9db312016-01-13 21:54:34 +00002168 __is_forward_iterator<_ForwardIterator>::value
2169 && __libcpp_string_gets_noexcept_iterator<_ForwardIterator>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002170 basic_string<_CharT, _Traits, _Allocator>&
2171>::type
2172basic_string<_CharT, _Traits, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
2173{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002174 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002175 size_type __cap = capacity();
2176 if (__cap < __n)
2177 {
2178 size_type __sz = size();
2179 __grow_by(__cap, __n - __cap, __sz, 0, __sz);
2180 }
2181 else
2182 __invalidate_iterators_past(__n);
2183 pointer __p = __get_pointer();
2184 for (; __first != __last; ++__first, ++__p)
2185 traits_type::assign(*__p, *__first);
2186 traits_type::assign(*__p, value_type());
2187 __set_size(__n);
2188 return *this;
2189}
2190
2191template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002192basic_string<_CharT, _Traits, _Allocator>&
2193basic_string<_CharT, _Traits, _Allocator>::assign(const basic_string& __str, size_type __pos, size_type __n)
2194{
2195 size_type __sz = __str.size();
2196 if (__pos > __sz)
2197 this->__throw_out_of_range();
Howard Hinnant0949eed2011-06-30 21:18:19 +00002198 return assign(__str.data() + __pos, _VSTD::min(__n, __sz - __pos));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002199}
2200
2201template <class _CharT, class _Traits, class _Allocator>
Marshall Clow6ac8de02016-09-24 22:45:42 +00002202template <class _Tp>
2203typename enable_if
2204<
2205 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
Marshall Clowf1729d92017-11-15 20:02:27 +00002206 basic_string<_CharT, _Traits, _Allocator>&
Marshall Clow6ac8de02016-09-24 22:45:42 +00002207>::type
2208basic_string<_CharT, _Traits, _Allocator>::assign(const _Tp & __t, size_type __pos, size_type __n)
Marshall Clow1e00d6d2016-07-21 05:31:24 +00002209{
Marshall Clow6ac8de02016-09-24 22:45:42 +00002210 __self_view __sv = __t;
Marshall Clow1e00d6d2016-07-21 05:31:24 +00002211 size_type __sz = __sv.size();
2212 if (__pos > __sz)
2213 this->__throw_out_of_range();
2214 return assign(__sv.data() + __pos, _VSTD::min(__n, __sz - __pos));
2215}
2216
2217
2218template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002219basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002220basic_string<_CharT, _Traits, _Allocator>::assign(const value_type* __s)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002221{
Alp Tokerec34c482014-05-15 11:27:39 +00002222 _LIBCPP_ASSERT(__s != nullptr, "string::assign received nullptr");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002223 return assign(__s, traits_type::length(__s));
2224}
2225
2226// append
2227
2228template <class _CharT, class _Traits, class _Allocator>
2229basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002230basic_string<_CharT, _Traits, _Allocator>::append(const value_type* __s, size_type __n)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002231{
Alp Tokerec34c482014-05-15 11:27:39 +00002232 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::append received nullptr");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002233 size_type __cap = capacity();
2234 size_type __sz = size();
2235 if (__cap - __sz >= __n)
2236 {
2237 if (__n)
2238 {
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002239 value_type* __p = _VSTD::__to_raw_pointer(__get_pointer());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002240 traits_type::copy(__p + __sz, __s, __n);
2241 __sz += __n;
2242 __set_size(__sz);
2243 traits_type::assign(__p[__sz], value_type());
2244 }
2245 }
2246 else
2247 __grow_by_and_replace(__cap, __sz + __n - __cap, __sz, __sz, 0, __n, __s);
2248 return *this;
2249}
2250
2251template <class _CharT, class _Traits, class _Allocator>
2252basic_string<_CharT, _Traits, _Allocator>&
2253basic_string<_CharT, _Traits, _Allocator>::append(size_type __n, value_type __c)
2254{
2255 if (__n)
2256 {
2257 size_type __cap = capacity();
2258 size_type __sz = size();
2259 if (__cap - __sz < __n)
2260 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2261 pointer __p = __get_pointer();
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002262 traits_type::assign(_VSTD::__to_raw_pointer(__p) + __sz, __n, __c);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002263 __sz += __n;
2264 __set_size(__sz);
2265 traits_type::assign(__p[__sz], value_type());
2266 }
2267 return *this;
2268}
2269
2270template <class _CharT, class _Traits, class _Allocator>
2271void
2272basic_string<_CharT, _Traits, _Allocator>::push_back(value_type __c)
2273{
Howard Hinnant15467182013-04-30 21:44:48 +00002274 bool __is_short = !__is_long();
2275 size_type __cap;
2276 size_type __sz;
2277 if (__is_short)
2278 {
2279 __cap = __min_cap - 1;
2280 __sz = __get_short_size();
2281 }
2282 else
2283 {
2284 __cap = __get_long_cap() - 1;
2285 __sz = __get_long_size();
2286 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002287 if (__sz == __cap)
Howard Hinnant15467182013-04-30 21:44:48 +00002288 {
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002289 __grow_by(__cap, 1, __sz, __sz, 0);
Howard Hinnant15467182013-04-30 21:44:48 +00002290 __is_short = !__is_long();
2291 }
2292 pointer __p;
2293 if (__is_short)
2294 {
2295 __p = __get_short_pointer() + __sz;
2296 __set_short_size(__sz+1);
2297 }
2298 else
2299 {
2300 __p = __get_long_pointer() + __sz;
2301 __set_long_size(__sz+1);
2302 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002303 traits_type::assign(*__p, __c);
2304 traits_type::assign(*++__p, value_type());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002305}
2306
Marshall Clowac655ef2016-09-07 03:32:06 +00002307template <class _Tp>
Marshall Clowd979eed2016-09-05 01:54:30 +00002308bool __ptr_in_range (const _Tp* __p, const _Tp* __first, const _Tp* __last)
2309{
2310 return __first <= __p && __p < __last;
2311}
2312
Marshall Clowac655ef2016-09-07 03:32:06 +00002313template <class _Tp1, class _Tp2>
Eric Fiselier0e5ebbc2016-12-23 23:37:52 +00002314bool __ptr_in_range (const _Tp1*, const _Tp2*, const _Tp2*)
Marshall Clowac655ef2016-09-07 03:32:06 +00002315{
2316 return false;
2317}
2318
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002319template <class _CharT, class _Traits, class _Allocator>
2320template<class _ForwardIterator>
Eric Fiselier026d38e2016-10-31 02:46:25 +00002321basic_string<_CharT, _Traits, _Allocator>&
2322basic_string<_CharT, _Traits, _Allocator>::__append_forward_unsafe(
2323 _ForwardIterator __first, _ForwardIterator __last)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002324{
Eric Fiselier026d38e2016-10-31 02:46:25 +00002325 static_assert(__is_forward_iterator<_ForwardIterator>::value,
2326 "function requires a ForwardIterator");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002327 size_type __sz = size();
2328 size_type __cap = capacity();
Howard Hinnant0949eed2011-06-30 21:18:19 +00002329 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002330 if (__n)
2331 {
Eric Fiselier622c7d52017-04-15 06:49:02 +00002332 typedef typename iterator_traits<_ForwardIterator>::reference _CharRef;
2333 _CharRef __tmp_ref = *__first;
2334 if (__ptr_in_range(_VSTD::addressof(__tmp_ref), data(), data() + size()))
Marshall Clowd979eed2016-09-05 01:54:30 +00002335 {
2336 const basic_string __temp (__first, __last, __alloc());
2337 append(__temp.data(), __temp.size());
2338 }
2339 else
2340 {
2341 if (__cap - __sz < __n)
2342 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2343 pointer __p = __get_pointer() + __sz;
2344 for (; __first != __last; ++__p, ++__first)
2345 traits_type::assign(*__p, *__first);
2346 traits_type::assign(*__p, value_type());
2347 __set_size(__sz + __n);
2348 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002349 }
2350 return *this;
2351}
2352
2353template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002354inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002355basic_string<_CharT, _Traits, _Allocator>&
2356basic_string<_CharT, _Traits, _Allocator>::append(const basic_string& __str)
2357{
2358 return append(__str.data(), __str.size());
2359}
2360
2361template <class _CharT, class _Traits, class _Allocator>
2362basic_string<_CharT, _Traits, _Allocator>&
2363basic_string<_CharT, _Traits, _Allocator>::append(const basic_string& __str, size_type __pos, size_type __n)
2364{
2365 size_type __sz = __str.size();
2366 if (__pos > __sz)
2367 this->__throw_out_of_range();
Howard Hinnant0949eed2011-06-30 21:18:19 +00002368 return append(__str.data() + __pos, _VSTD::min(__n, __sz - __pos));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002369}
2370
2371template <class _CharT, class _Traits, class _Allocator>
Marshall Clow42a87db2016-10-03 23:40:48 +00002372template <class _Tp>
Marshall Clow6ac8de02016-09-24 22:45:42 +00002373 typename enable_if
2374 <
2375 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
2376 basic_string<_CharT, _Traits, _Allocator>&
2377 >::type
2378basic_string<_CharT, _Traits, _Allocator>::append(const _Tp & __t, size_type __pos, size_type __n)
Marshall Clow1e00d6d2016-07-21 05:31:24 +00002379{
Marshall Clow6ac8de02016-09-24 22:45:42 +00002380 __self_view __sv = __t;
Marshall Clow1e00d6d2016-07-21 05:31:24 +00002381 size_type __sz = __sv.size();
2382 if (__pos > __sz)
2383 this->__throw_out_of_range();
2384 return append(__sv.data() + __pos, _VSTD::min(__n, __sz - __pos));
2385}
2386
2387template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002388basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002389basic_string<_CharT, _Traits, _Allocator>::append(const value_type* __s)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002390{
Alp Tokerec34c482014-05-15 11:27:39 +00002391 _LIBCPP_ASSERT(__s != nullptr, "string::append received nullptr");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002392 return append(__s, traits_type::length(__s));
2393}
2394
2395// insert
2396
2397template <class _CharT, class _Traits, class _Allocator>
2398basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002399basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, const value_type* __s, size_type __n)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002400{
Alp Tokerec34c482014-05-15 11:27:39 +00002401 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::insert received nullptr");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002402 size_type __sz = size();
2403 if (__pos > __sz)
2404 this->__throw_out_of_range();
2405 size_type __cap = capacity();
2406 if (__cap - __sz >= __n)
2407 {
2408 if (__n)
2409 {
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002410 value_type* __p = _VSTD::__to_raw_pointer(__get_pointer());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002411 size_type __n_move = __sz - __pos;
2412 if (__n_move != 0)
2413 {
2414 if (__p + __pos <= __s && __s < __p + __sz)
2415 __s += __n;
2416 traits_type::move(__p + __pos + __n, __p + __pos, __n_move);
2417 }
2418 traits_type::move(__p + __pos, __s, __n);
2419 __sz += __n;
2420 __set_size(__sz);
2421 traits_type::assign(__p[__sz], value_type());
2422 }
2423 }
2424 else
2425 __grow_by_and_replace(__cap, __sz + __n - __cap, __sz, __pos, 0, __n, __s);
2426 return *this;
2427}
2428
2429template <class _CharT, class _Traits, class _Allocator>
2430basic_string<_CharT, _Traits, _Allocator>&
2431basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, size_type __n, value_type __c)
2432{
2433 size_type __sz = size();
2434 if (__pos > __sz)
2435 this->__throw_out_of_range();
2436 if (__n)
2437 {
2438 size_type __cap = capacity();
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002439 value_type* __p;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002440 if (__cap - __sz >= __n)
2441 {
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002442 __p = _VSTD::__to_raw_pointer(__get_pointer());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002443 size_type __n_move = __sz - __pos;
2444 if (__n_move != 0)
2445 traits_type::move(__p + __pos + __n, __p + __pos, __n_move);
2446 }
2447 else
2448 {
2449 __grow_by(__cap, __sz + __n - __cap, __sz, __pos, 0, __n);
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002450 __p = _VSTD::__to_raw_pointer(__get_long_pointer());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002451 }
2452 traits_type::assign(__p + __pos, __n, __c);
2453 __sz += __n;
2454 __set_size(__sz);
2455 traits_type::assign(__p[__sz], value_type());
2456 }
2457 return *this;
2458}
2459
2460template <class _CharT, class _Traits, class _Allocator>
2461template<class _InputIterator>
2462typename enable_if
2463<
Marshall Clowdf9db312016-01-13 21:54:34 +00002464 __is_exactly_input_iterator<_InputIterator>::value
2465 || !__libcpp_string_gets_noexcept_iterator<_InputIterator>::value,
2466 typename basic_string<_CharT, _Traits, _Allocator>::iterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002467>::type
2468basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, _InputIterator __first, _InputIterator __last)
2469{
Howard Hinnant499cea12013-08-23 17:37:05 +00002470#if _LIBCPP_DEBUG_LEVEL >= 2
2471 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2472 "string::insert(iterator, range) called with an iterator not"
2473 " referring to this string");
2474#endif
Marshall Clowd979eed2016-09-05 01:54:30 +00002475 const basic_string __temp(__first, __last, __alloc());
Marshall Clowdf9db312016-01-13 21:54:34 +00002476 return insert(__pos, __temp.data(), __temp.data() + __temp.size());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002477}
2478
2479template <class _CharT, class _Traits, class _Allocator>
2480template<class _ForwardIterator>
2481typename enable_if
2482<
Marshall Clowdf9db312016-01-13 21:54:34 +00002483 __is_forward_iterator<_ForwardIterator>::value
2484 && __libcpp_string_gets_noexcept_iterator<_ForwardIterator>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002485 typename basic_string<_CharT, _Traits, _Allocator>::iterator
2486>::type
2487basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, _ForwardIterator __first, _ForwardIterator __last)
2488{
Howard Hinnant499cea12013-08-23 17:37:05 +00002489#if _LIBCPP_DEBUG_LEVEL >= 2
2490 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2491 "string::insert(iterator, range) called with an iterator not"
2492 " referring to this string");
2493#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002494 size_type __ip = static_cast<size_type>(__pos - begin());
Howard Hinnant0949eed2011-06-30 21:18:19 +00002495 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002496 if (__n)
2497 {
Eric Fiselier622c7d52017-04-15 06:49:02 +00002498 typedef typename iterator_traits<_ForwardIterator>::reference _CharRef;
2499 _CharRef __tmp_char = *__first;
2500 if (__ptr_in_range(_VSTD::addressof(__tmp_char), data(), data() + size()))
Marshall Clowd979eed2016-09-05 01:54:30 +00002501 {
2502 const basic_string __temp(__first, __last, __alloc());
2503 return insert(__pos, __temp.data(), __temp.data() + __temp.size());
2504 }
2505
2506 size_type __sz = size();
2507 size_type __cap = capacity();
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002508 value_type* __p;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002509 if (__cap - __sz >= __n)
2510 {
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002511 __p = _VSTD::__to_raw_pointer(__get_pointer());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002512 size_type __n_move = __sz - __ip;
2513 if (__n_move != 0)
2514 traits_type::move(__p + __ip + __n, __p + __ip, __n_move);
2515 }
2516 else
2517 {
2518 __grow_by(__cap, __sz + __n - __cap, __sz, __ip, 0, __n);
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002519 __p = _VSTD::__to_raw_pointer(__get_long_pointer());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002520 }
2521 __sz += __n;
2522 __set_size(__sz);
2523 traits_type::assign(__p[__sz], value_type());
2524 for (__p += __ip; __first != __last; ++__p, ++__first)
2525 traits_type::assign(*__p, *__first);
2526 }
2527 return begin() + __ip;
2528}
2529
2530template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002531inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002532basic_string<_CharT, _Traits, _Allocator>&
2533basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const basic_string& __str)
2534{
2535 return insert(__pos1, __str.data(), __str.size());
2536}
2537
2538template <class _CharT, class _Traits, class _Allocator>
2539basic_string<_CharT, _Traits, _Allocator>&
2540basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const basic_string& __str,
2541 size_type __pos2, size_type __n)
2542{
2543 size_type __str_sz = __str.size();
2544 if (__pos2 > __str_sz)
2545 this->__throw_out_of_range();
Howard Hinnant0949eed2011-06-30 21:18:19 +00002546 return insert(__pos1, __str.data() + __pos2, _VSTD::min(__n, __str_sz - __pos2));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002547}
2548
2549template <class _CharT, class _Traits, class _Allocator>
Marshall Clow6ac8de02016-09-24 22:45:42 +00002550template <class _Tp>
2551typename enable_if
2552<
2553 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
Marshall Clowf1729d92017-11-15 20:02:27 +00002554 basic_string<_CharT, _Traits, _Allocator>&
Marshall Clow6ac8de02016-09-24 22:45:42 +00002555>::type
2556basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const _Tp& __t,
Marshall Clow1e00d6d2016-07-21 05:31:24 +00002557 size_type __pos2, size_type __n)
2558{
Marshall Clow6ac8de02016-09-24 22:45:42 +00002559 __self_view __sv = __t;
Marshall Clow1e00d6d2016-07-21 05:31:24 +00002560 size_type __str_sz = __sv.size();
2561 if (__pos2 > __str_sz)
2562 this->__throw_out_of_range();
2563 return insert(__pos1, __sv.data() + __pos2, _VSTD::min(__n, __str_sz - __pos2));
2564}
2565
2566template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002567basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002568basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, const value_type* __s)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002569{
Alp Tokerec34c482014-05-15 11:27:39 +00002570 _LIBCPP_ASSERT(__s != nullptr, "string::insert received nullptr");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002571 return insert(__pos, __s, traits_type::length(__s));
2572}
2573
2574template <class _CharT, class _Traits, class _Allocator>
2575typename basic_string<_CharT, _Traits, _Allocator>::iterator
2576basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, value_type __c)
2577{
2578 size_type __ip = static_cast<size_type>(__pos - begin());
2579 size_type __sz = size();
2580 size_type __cap = capacity();
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002581 value_type* __p;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002582 if (__cap == __sz)
2583 {
2584 __grow_by(__cap, 1, __sz, __ip, 0, 1);
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002585 __p = _VSTD::__to_raw_pointer(__get_long_pointer());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002586 }
2587 else
2588 {
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002589 __p = _VSTD::__to_raw_pointer(__get_pointer());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002590 size_type __n_move = __sz - __ip;
2591 if (__n_move != 0)
2592 traits_type::move(__p + __ip + 1, __p + __ip, __n_move);
2593 }
2594 traits_type::assign(__p[__ip], __c);
2595 traits_type::assign(__p[++__sz], value_type());
2596 __set_size(__sz);
2597 return begin() + static_cast<difference_type>(__ip);
2598}
2599
2600template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002601inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002602typename basic_string<_CharT, _Traits, _Allocator>::iterator
2603basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, size_type __n, value_type __c)
2604{
Howard Hinnant499cea12013-08-23 17:37:05 +00002605#if _LIBCPP_DEBUG_LEVEL >= 2
2606 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2607 "string::insert(iterator, n, value) called with an iterator not"
2608 " referring to this string");
2609#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002610 difference_type __p = __pos - begin();
2611 insert(static_cast<size_type>(__p), __n, __c);
2612 return begin() + __p;
2613}
2614
2615// replace
2616
2617template <class _CharT, class _Traits, class _Allocator>
2618basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002619basic_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 +00002620 _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002621{
Alp Tokerec34c482014-05-15 11:27:39 +00002622 _LIBCPP_ASSERT(__n2 == 0 || __s != nullptr, "string::replace received nullptr");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002623 size_type __sz = size();
2624 if (__pos > __sz)
2625 this->__throw_out_of_range();
Howard Hinnant0949eed2011-06-30 21:18:19 +00002626 __n1 = _VSTD::min(__n1, __sz - __pos);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002627 size_type __cap = capacity();
2628 if (__cap - __sz + __n1 >= __n2)
2629 {
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002630 value_type* __p = _VSTD::__to_raw_pointer(__get_pointer());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002631 if (__n1 != __n2)
2632 {
2633 size_type __n_move = __sz - __pos - __n1;
2634 if (__n_move != 0)
2635 {
2636 if (__n1 > __n2)
2637 {
2638 traits_type::move(__p + __pos, __s, __n2);
2639 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
2640 goto __finish;
2641 }
2642 if (__p + __pos < __s && __s < __p + __sz)
2643 {
2644 if (__p + __pos + __n1 <= __s)
2645 __s += __n2 - __n1;
2646 else // __p + __pos < __s < __p + __pos + __n1
2647 {
2648 traits_type::move(__p + __pos, __s, __n1);
2649 __pos += __n1;
2650 __s += __n2;
2651 __n2 -= __n1;
2652 __n1 = 0;
2653 }
2654 }
2655 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
2656 }
2657 }
2658 traits_type::move(__p + __pos, __s, __n2);
2659__finish:
Eric Fiselier3b7c1342017-03-09 01:54:13 +00002660// __sz += __n2 - __n1; in this and the below function below can cause unsigned integer overflow,
2661// but this is a safe operation, so we disable the check.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002662 __sz += __n2 - __n1;
2663 __set_size(__sz);
2664 __invalidate_iterators_past(__sz);
2665 traits_type::assign(__p[__sz], value_type());
2666 }
2667 else
2668 __grow_by_and_replace(__cap, __sz - __n1 + __n2 - __cap, __sz, __pos, __n1, __n2, __s);
2669 return *this;
2670}
2671
2672template <class _CharT, class _Traits, class _Allocator>
2673basic_string<_CharT, _Traits, _Allocator>&
2674basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos, size_type __n1, size_type __n2, value_type __c)
Eric Fiselier3b7c1342017-03-09 01:54:13 +00002675 _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002676{
2677 size_type __sz = size();
2678 if (__pos > __sz)
2679 this->__throw_out_of_range();
Howard Hinnant0949eed2011-06-30 21:18:19 +00002680 __n1 = _VSTD::min(__n1, __sz - __pos);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002681 size_type __cap = capacity();
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002682 value_type* __p;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002683 if (__cap - __sz + __n1 >= __n2)
2684 {
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002685 __p = _VSTD::__to_raw_pointer(__get_pointer());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002686 if (__n1 != __n2)
2687 {
2688 size_type __n_move = __sz - __pos - __n1;
2689 if (__n_move != 0)
2690 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
2691 }
2692 }
2693 else
2694 {
2695 __grow_by(__cap, __sz - __n1 + __n2 - __cap, __sz, __pos, __n1, __n2);
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002696 __p = _VSTD::__to_raw_pointer(__get_long_pointer());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002697 }
2698 traits_type::assign(__p + __pos, __n2, __c);
2699 __sz += __n2 - __n1;
2700 __set_size(__sz);
2701 __invalidate_iterators_past(__sz);
2702 traits_type::assign(__p[__sz], value_type());
2703 return *this;
2704}
2705
2706template <class _CharT, class _Traits, class _Allocator>
2707template<class _InputIterator>
2708typename enable_if
2709<
2710 __is_input_iterator<_InputIterator>::value,
2711 basic_string<_CharT, _Traits, _Allocator>&
2712>::type
Howard Hinnant7b2cb482010-11-17 21:11:40 +00002713basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002714 _InputIterator __j1, _InputIterator __j2)
2715{
Marshall Clowd979eed2016-09-05 01:54:30 +00002716 const basic_string __temp(__j1, __j2, __alloc());
Marshall Clowdf9db312016-01-13 21:54:34 +00002717 return this->replace(__i1, __i2, __temp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002718}
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>&
2723basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const basic_string& __str)
2724{
2725 return replace(__pos1, __n1, __str.data(), __str.size());
2726}
2727
2728template <class _CharT, class _Traits, class _Allocator>
2729basic_string<_CharT, _Traits, _Allocator>&
2730basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const basic_string& __str,
2731 size_type __pos2, size_type __n2)
2732{
2733 size_type __str_sz = __str.size();
2734 if (__pos2 > __str_sz)
2735 this->__throw_out_of_range();
Howard Hinnant0949eed2011-06-30 21:18:19 +00002736 return replace(__pos1, __n1, __str.data() + __pos2, _VSTD::min(__n2, __str_sz - __pos2));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002737}
2738
2739template <class _CharT, class _Traits, class _Allocator>
Marshall Clow6ac8de02016-09-24 22:45:42 +00002740template <class _Tp>
2741typename enable_if
2742<
Marshall Clowf1729d92017-11-15 20:02:27 +00002743 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
2744 basic_string<_CharT, _Traits, _Allocator>&
Marshall Clow6ac8de02016-09-24 22:45:42 +00002745>::type
2746basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const _Tp& __t,
Marshall Clow1e00d6d2016-07-21 05:31:24 +00002747 size_type __pos2, size_type __n2)
2748{
Marshall Clow6ac8de02016-09-24 22:45:42 +00002749 __self_view __sv = __t;
Marshall Clow1e00d6d2016-07-21 05:31:24 +00002750 size_type __str_sz = __sv.size();
2751 if (__pos2 > __str_sz)
2752 this->__throw_out_of_range();
2753 return replace(__pos1, __n1, __sv.data() + __pos2, _VSTD::min(__n2, __str_sz - __pos2));
2754}
2755
2756template <class _CharT, class _Traits, class _Allocator>
2757basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002758basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos, size_type __n1, const value_type* __s)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002759{
Alp Tokerec34c482014-05-15 11:27:39 +00002760 _LIBCPP_ASSERT(__s != nullptr, "string::replace received nullptr");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002761 return replace(__pos, __n1, __s, traits_type::length(__s));
2762}
2763
2764template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002765inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002766basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant7b2cb482010-11-17 21:11:40 +00002767basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, const basic_string& __str)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002768{
2769 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1),
2770 __str.data(), __str.size());
2771}
2772
2773template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002774inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002775basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002776basic_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 +00002777{
2778 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __s, __n);
2779}
2780
2781template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002782inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002783basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002784basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, const value_type* __s)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002785{
2786 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __s);
2787}
2788
2789template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002790inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002791basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant7b2cb482010-11-17 21:11:40 +00002792basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, size_type __n, value_type __c)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002793{
2794 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __n, __c);
2795}
2796
2797// erase
2798
2799template <class _CharT, class _Traits, class _Allocator>
2800basic_string<_CharT, _Traits, _Allocator>&
2801basic_string<_CharT, _Traits, _Allocator>::erase(size_type __pos, size_type __n)
2802{
2803 size_type __sz = size();
2804 if (__pos > __sz)
2805 this->__throw_out_of_range();
2806 if (__n)
2807 {
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002808 value_type* __p = _VSTD::__to_raw_pointer(__get_pointer());
Howard Hinnant0949eed2011-06-30 21:18:19 +00002809 __n = _VSTD::min(__n, __sz - __pos);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002810 size_type __n_move = __sz - __pos - __n;
2811 if (__n_move != 0)
2812 traits_type::move(__p + __pos, __p + __pos + __n, __n_move);
2813 __sz -= __n;
2814 __set_size(__sz);
2815 __invalidate_iterators_past(__sz);
2816 traits_type::assign(__p[__sz], value_type());
2817 }
2818 return *this;
2819}
2820
2821template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002822inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002823typename basic_string<_CharT, _Traits, _Allocator>::iterator
2824basic_string<_CharT, _Traits, _Allocator>::erase(const_iterator __pos)
2825{
Howard Hinnant499cea12013-08-23 17:37:05 +00002826#if _LIBCPP_DEBUG_LEVEL >= 2
2827 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2828 "string::erase(iterator) called with an iterator not"
2829 " referring to this string");
2830#endif
2831 _LIBCPP_ASSERT(__pos != end(),
2832 "string::erase(iterator) called with a non-dereferenceable iterator");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002833 iterator __b = begin();
2834 size_type __r = static_cast<size_type>(__pos - __b);
2835 erase(__r, 1);
Howard Hinnantec3773c2011-12-01 20:21:04 +00002836 return __b + static_cast<difference_type>(__r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002837}
2838
2839template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002840inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002841typename basic_string<_CharT, _Traits, _Allocator>::iterator
2842basic_string<_CharT, _Traits, _Allocator>::erase(const_iterator __first, const_iterator __last)
2843{
Howard Hinnant499cea12013-08-23 17:37:05 +00002844#if _LIBCPP_DEBUG_LEVEL >= 2
2845 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__first) == this,
2846 "string::erase(iterator, iterator) called with an iterator not"
2847 " referring to this string");
2848#endif
2849 _LIBCPP_ASSERT(__first <= __last, "string::erase(first, last) called with invalid range");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002850 iterator __b = begin();
2851 size_type __r = static_cast<size_type>(__first - __b);
2852 erase(__r, static_cast<size_type>(__last - __first));
Howard Hinnantec3773c2011-12-01 20:21:04 +00002853 return __b + static_cast<difference_type>(__r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002854}
2855
2856template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002857inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002858void
2859basic_string<_CharT, _Traits, _Allocator>::pop_back()
2860{
Howard Hinnant499cea12013-08-23 17:37:05 +00002861 _LIBCPP_ASSERT(!empty(), "string::pop_back(): string is already empty");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002862 size_type __sz;
2863 if (__is_long())
2864 {
2865 __sz = __get_long_size() - 1;
2866 __set_long_size(__sz);
2867 traits_type::assign(*(__get_long_pointer() + __sz), value_type());
2868 }
2869 else
2870 {
2871 __sz = __get_short_size() - 1;
2872 __set_short_size(__sz);
2873 traits_type::assign(*(__get_short_pointer() + __sz), value_type());
2874 }
2875 __invalidate_iterators_past(__sz);
2876}
2877
2878template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002879inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002880void
Howard Hinnanta6119a82011-05-29 19:57:12 +00002881basic_string<_CharT, _Traits, _Allocator>::clear() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002882{
2883 __invalidate_all_iterators();
2884 if (__is_long())
2885 {
2886 traits_type::assign(*__get_long_pointer(), value_type());
2887 __set_long_size(0);
2888 }
2889 else
2890 {
2891 traits_type::assign(*__get_short_pointer(), value_type());
2892 __set_short_size(0);
2893 }
2894}
2895
2896template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002897inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002898void
2899basic_string<_CharT, _Traits, _Allocator>::__erase_to_end(size_type __pos)
2900{
2901 if (__is_long())
2902 {
2903 traits_type::assign(*(__get_long_pointer() + __pos), value_type());
2904 __set_long_size(__pos);
2905 }
2906 else
2907 {
2908 traits_type::assign(*(__get_short_pointer() + __pos), value_type());
2909 __set_short_size(__pos);
2910 }
2911 __invalidate_iterators_past(__pos);
2912}
2913
2914template <class _CharT, class _Traits, class _Allocator>
2915void
2916basic_string<_CharT, _Traits, _Allocator>::resize(size_type __n, value_type __c)
2917{
2918 size_type __sz = size();
2919 if (__n > __sz)
2920 append(__n - __sz, __c);
2921 else
2922 __erase_to_end(__n);
2923}
2924
2925template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002926inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002927typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnanta6119a82011-05-29 19:57:12 +00002928basic_string<_CharT, _Traits, _Allocator>::max_size() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002929{
Howard Hinnante32b5e22010-11-17 17:55:08 +00002930 size_type __m = __alloc_traits::max_size(__alloc());
Eric Fiselier5ccf0432017-10-17 13:16:01 +00002931#ifdef _LIBCPP_BIG_ENDIAN
Marshall Clow09f85502013-10-31 17:23:08 +00002932 return (__m <= ~__long_mask ? __m : __m/2) - __alignment;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002933#else
Marshall Clow09f85502013-10-31 17:23:08 +00002934 return __m - __alignment;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002935#endif
2936}
2937
2938template <class _CharT, class _Traits, class _Allocator>
2939void
2940basic_string<_CharT, _Traits, _Allocator>::reserve(size_type __res_arg)
2941{
2942 if (__res_arg > max_size())
2943 this->__throw_length_error();
2944 size_type __cap = capacity();
2945 size_type __sz = size();
Howard Hinnant0949eed2011-06-30 21:18:19 +00002946 __res_arg = _VSTD::max(__res_arg, __sz);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002947 __res_arg = __recommend(__res_arg);
2948 if (__res_arg != __cap)
2949 {
2950 pointer __new_data, __p;
2951 bool __was_long, __now_long;
2952 if (__res_arg == __min_cap - 1)
2953 {
2954 __was_long = true;
2955 __now_long = false;
2956 __new_data = __get_short_pointer();
2957 __p = __get_long_pointer();
2958 }
2959 else
2960 {
2961 if (__res_arg > __cap)
Howard Hinnante32b5e22010-11-17 17:55:08 +00002962 __new_data = __alloc_traits::allocate(__alloc(), __res_arg+1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002963 else
2964 {
2965 #ifndef _LIBCPP_NO_EXCEPTIONS
2966 try
2967 {
Howard Hinnant324bb032010-08-22 00:02:43 +00002968 #endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnante32b5e22010-11-17 17:55:08 +00002969 __new_data = __alloc_traits::allocate(__alloc(), __res_arg+1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002970 #ifndef _LIBCPP_NO_EXCEPTIONS
2971 }
2972 catch (...)
2973 {
2974 return;
2975 }
Howard Hinnant324bb032010-08-22 00:02:43 +00002976 #else // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002977 if (__new_data == nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002978 return;
Howard Hinnant324bb032010-08-22 00:02:43 +00002979 #endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002980 }
2981 __now_long = true;
2982 __was_long = __is_long();
2983 __p = __get_pointer();
2984 }
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002985 traits_type::copy(_VSTD::__to_raw_pointer(__new_data),
2986 _VSTD::__to_raw_pointer(__p), size()+1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002987 if (__was_long)
Howard Hinnante32b5e22010-11-17 17:55:08 +00002988 __alloc_traits::deallocate(__alloc(), __p, __cap+1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002989 if (__now_long)
2990 {
2991 __set_long_cap(__res_arg+1);
2992 __set_long_size(__sz);
2993 __set_long_pointer(__new_data);
2994 }
2995 else
2996 __set_short_size(__sz);
2997 __invalidate_all_iterators();
2998 }
2999}
3000
3001template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003002inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003003typename basic_string<_CharT, _Traits, _Allocator>::const_reference
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003004basic_string<_CharT, _Traits, _Allocator>::operator[](size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003005{
Howard Hinnant499cea12013-08-23 17:37:05 +00003006 _LIBCPP_ASSERT(__pos <= size(), "string index out of bounds");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003007 return *(data() + __pos);
3008}
3009
3010template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003011inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003012typename basic_string<_CharT, _Traits, _Allocator>::reference
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003013basic_string<_CharT, _Traits, _Allocator>::operator[](size_type __pos) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003014{
Howard Hinnant499cea12013-08-23 17:37:05 +00003015 _LIBCPP_ASSERT(__pos <= size(), "string index out of bounds");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003016 return *(__get_pointer() + __pos);
3017}
3018
3019template <class _CharT, class _Traits, class _Allocator>
3020typename basic_string<_CharT, _Traits, _Allocator>::const_reference
3021basic_string<_CharT, _Traits, _Allocator>::at(size_type __n) const
3022{
3023 if (__n >= size())
3024 this->__throw_out_of_range();
3025 return (*this)[__n];
3026}
3027
3028template <class _CharT, class _Traits, class _Allocator>
3029typename basic_string<_CharT, _Traits, _Allocator>::reference
3030basic_string<_CharT, _Traits, _Allocator>::at(size_type __n)
3031{
3032 if (__n >= size())
3033 this->__throw_out_of_range();
3034 return (*this)[__n];
3035}
3036
3037template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003038inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003039typename basic_string<_CharT, _Traits, _Allocator>::reference
3040basic_string<_CharT, _Traits, _Allocator>::front()
3041{
Howard Hinnant499cea12013-08-23 17:37:05 +00003042 _LIBCPP_ASSERT(!empty(), "string::front(): string is empty");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003043 return *__get_pointer();
3044}
3045
3046template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003047inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003048typename basic_string<_CharT, _Traits, _Allocator>::const_reference
3049basic_string<_CharT, _Traits, _Allocator>::front() const
3050{
Howard Hinnant499cea12013-08-23 17:37:05 +00003051 _LIBCPP_ASSERT(!empty(), "string::front(): string is empty");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003052 return *data();
3053}
3054
3055template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003056inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003057typename basic_string<_CharT, _Traits, _Allocator>::reference
3058basic_string<_CharT, _Traits, _Allocator>::back()
3059{
Howard Hinnant499cea12013-08-23 17:37:05 +00003060 _LIBCPP_ASSERT(!empty(), "string::back(): string is empty");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003061 return *(__get_pointer() + size() - 1);
3062}
3063
3064template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003065inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003066typename basic_string<_CharT, _Traits, _Allocator>::const_reference
3067basic_string<_CharT, _Traits, _Allocator>::back() const
3068{
Howard Hinnant499cea12013-08-23 17:37:05 +00003069 _LIBCPP_ASSERT(!empty(), "string::back(): string is empty");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003070 return *(data() + size() - 1);
3071}
3072
3073template <class _CharT, class _Traits, class _Allocator>
3074typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00003075basic_string<_CharT, _Traits, _Allocator>::copy(value_type* __s, size_type __n, size_type __pos) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003076{
3077 size_type __sz = size();
3078 if (__pos > __sz)
3079 this->__throw_out_of_range();
Howard Hinnant0949eed2011-06-30 21:18:19 +00003080 size_type __rlen = _VSTD::min(__n, __sz - __pos);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003081 traits_type::copy(__s, data() + __pos, __rlen);
3082 return __rlen;
3083}
3084
3085template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003086inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003087basic_string<_CharT, _Traits, _Allocator>
3088basic_string<_CharT, _Traits, _Allocator>::substr(size_type __pos, size_type __n) const
3089{
3090 return basic_string(*this, __pos, __n, __alloc());
3091}
3092
3093template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003094inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003095void
3096basic_string<_CharT, _Traits, _Allocator>::swap(basic_string& __str)
Marshall Clow7d914d12015-07-13 20:04:56 +00003097#if _LIBCPP_STD_VER >= 14
Eric Fiselier47257c42016-12-28 05:53:01 +00003098 _NOEXCEPT_DEBUG
Marshall Clow7d914d12015-07-13 20:04:56 +00003099#else
Eric Fiselier47257c42016-12-28 05:53:01 +00003100 _NOEXCEPT_DEBUG_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow7d914d12015-07-13 20:04:56 +00003101 __is_nothrow_swappable<allocator_type>::value)
3102#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003103{
Howard Hinnant499cea12013-08-23 17:37:05 +00003104#if _LIBCPP_DEBUG_LEVEL >= 2
3105 if (!__is_long())
3106 __get_db()->__invalidate_all(this);
3107 if (!__str.__is_long())
3108 __get_db()->__invalidate_all(&__str);
3109 __get_db()->swap(this, &__str);
3110#endif
Eric Fiselier47257c42016-12-28 05:53:01 +00003111 _LIBCPP_ASSERT(
3112 __alloc_traits::propagate_on_container_swap::value ||
3113 __alloc_traits::is_always_equal::value ||
3114 __alloc() == __str.__alloc(), "swapping non-equal allocators");
Howard Hinnant0949eed2011-06-30 21:18:19 +00003115 _VSTD::swap(__r_.first(), __str.__r_.first());
Marshall Clow7d914d12015-07-13 20:04:56 +00003116 __swap_allocator(__alloc(), __str.__alloc());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003117}
3118
3119// find
3120
3121template <class _Traits>
3122struct _LIBCPP_HIDDEN __traits_eq
3123{
3124 typedef typename _Traits::char_type char_type;
Howard Hinnanta6119a82011-05-29 19:57:12 +00003125 _LIBCPP_INLINE_VISIBILITY
3126 bool operator()(const char_type& __x, const char_type& __y) _NOEXCEPT
3127 {return _Traits::eq(__x, __y);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003128};
3129
3130template<class _CharT, class _Traits, class _Allocator>
3131typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00003132basic_string<_CharT, _Traits, _Allocator>::find(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::find(): received nullptr");
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003137 return __str_find<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>::find(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_find<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>::find(__self_view __sv,
3155 size_type __pos) const _NOEXCEPT
3156{
3157 return __str_find<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>::find(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::find(): received nullptr");
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003168 return __str_find<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>::find(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_find<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// rfind
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>::rfind(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::rfind(): received nullptr");
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003190 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clow360f3192014-06-02 02:22:49 +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>::rfind(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_rfind<value_type, size_type, traits_type, npos>
Marshall Clow360f3192014-06-02 02:22:49 +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>::rfind(__self_view __sv,
3208 size_type __pos) const _NOEXCEPT
3209{
3210 return __str_rfind<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>::rfind(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::rfind(): received nullptr");
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003221 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clow360f3192014-06-02 02:22:49 +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>
3226typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnanta6119a82011-05-29 19:57:12 +00003227basic_string<_CharT, _Traits, _Allocator>::rfind(value_type __c,
3228 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003229{
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003230 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clow360f3192014-06-02 02:22:49 +00003231 (data(), size(), __c, __pos);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003232}
3233
3234// find_first_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_first_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_first_of(): received nullptr");
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003243 return __str_find_first_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_first_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_first_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_first_of(__self_view __sv,
3261 size_type __pos) const _NOEXCEPT
3262{
3263 return __str_find_first_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_first_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_first_of(): received nullptr");
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003274 return __str_find_first_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_first_of(value_type __c,
3282 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003283{
3284 return find(__c, __pos);
3285}
3286
3287// find_last_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_last_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_last_of(): received nullptr");
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003296 return __str_find_last_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_last_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_last_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_last_of(__self_view __sv,
3314 size_type __pos) const _NOEXCEPT
3315{
3316 return __str_find_last_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_last_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_last_of(): received nullptr");
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003327 return __str_find_last_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_last_of(value_type __c,
3335 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003336{
3337 return rfind(__c, __pos);
3338}
3339
3340// find_first_not_of
3341
3342template<class _CharT, class _Traits, class _Allocator>
3343typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00003344basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const value_type* __s,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003345 size_type __pos,
3346 size_type __n) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003347{
Alp Tokerec34c482014-05-15 11:27:39 +00003348 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_first_not_of(): received nullptr");
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003349 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow8eb5acc2014-02-16 01:57:26 +00003350 (data(), size(), __s, __pos, __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003351}
3352
3353template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003354inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003355typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnanta6119a82011-05-29 19:57:12 +00003356basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const basic_string& __str,
3357 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003358{
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003359 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow8eb5acc2014-02-16 01:57:26 +00003360 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003361}
3362
3363template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003364inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003365typename basic_string<_CharT, _Traits, _Allocator>::size_type
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003366basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(__self_view __sv,
3367 size_type __pos) const _NOEXCEPT
3368{
3369 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
3370 (data(), size(), __sv.data(), __pos, __sv.size());
3371}
3372
3373template<class _CharT, class _Traits, class _Allocator>
3374inline _LIBCPP_INLINE_VISIBILITY
3375typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00003376basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const value_type* __s,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003377 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003378{
Alp Tokerec34c482014-05-15 11:27:39 +00003379 _LIBCPP_ASSERT(__s != nullptr, "string::find_first_not_of(): received nullptr");
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003380 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow8eb5acc2014-02-16 01:57:26 +00003381 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003382}
3383
3384template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003385inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003386typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnanta6119a82011-05-29 19:57:12 +00003387basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(value_type __c,
3388 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003389{
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003390 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow8eb5acc2014-02-16 01:57:26 +00003391 (data(), size(), __c, __pos);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003392}
3393
3394// find_last_not_of
3395
3396template<class _CharT, class _Traits, class _Allocator>
3397typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00003398basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const value_type* __s,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003399 size_type __pos,
3400 size_type __n) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003401{
Alp Tokerec34c482014-05-15 11:27:39 +00003402 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_last_not_of(): received nullptr");
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003403 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow8eb5acc2014-02-16 01:57:26 +00003404 (data(), size(), __s, __pos, __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003405}
3406
3407template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003408inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003409typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnanta6119a82011-05-29 19:57:12 +00003410basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const basic_string& __str,
3411 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003412{
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003413 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow8eb5acc2014-02-16 01:57:26 +00003414 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003415}
3416
3417template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003418inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003419typename basic_string<_CharT, _Traits, _Allocator>::size_type
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003420basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(__self_view __sv,
3421 size_type __pos) const _NOEXCEPT
3422{
3423 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
3424 (data(), size(), __sv.data(), __pos, __sv.size());
3425}
3426
3427template<class _CharT, class _Traits, class _Allocator>
3428inline _LIBCPP_INLINE_VISIBILITY
3429typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00003430basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const value_type* __s,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003431 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003432{
Alp Tokerec34c482014-05-15 11:27:39 +00003433 _LIBCPP_ASSERT(__s != nullptr, "string::find_last_not_of(): received nullptr");
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003434 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow8eb5acc2014-02-16 01:57:26 +00003435 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003436}
3437
3438template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003439inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003440typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnanta6119a82011-05-29 19:57:12 +00003441basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(value_type __c,
3442 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003443{
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003444 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow8eb5acc2014-02-16 01:57:26 +00003445 (data(), size(), __c, __pos);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003446}
3447
3448// compare
3449
3450template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003451inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003452int
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003453basic_string<_CharT, _Traits, _Allocator>::compare(__self_view __sv) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003454{
Howard Hinnantfa06d752011-07-24 21:45:06 +00003455 size_t __lhs_sz = size();
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003456 size_t __rhs_sz = __sv.size();
3457 int __result = traits_type::compare(data(), __sv.data(),
Howard Hinnantfa06d752011-07-24 21:45:06 +00003458 _VSTD::min(__lhs_sz, __rhs_sz));
3459 if (__result != 0)
3460 return __result;
3461 if (__lhs_sz < __rhs_sz)
3462 return -1;
3463 if (__lhs_sz > __rhs_sz)
3464 return 1;
3465 return 0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003466}
3467
3468template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003469inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003470int
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003471basic_string<_CharT, _Traits, _Allocator>::compare(const basic_string& __str) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003472{
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003473 return compare(__self_view(__str));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003474}
3475
3476template <class _CharT, class _Traits, class _Allocator>
3477int
Howard Hinnanta6119a82011-05-29 19:57:12 +00003478basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3479 size_type __n1,
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00003480 const value_type* __s,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003481 size_type __n2) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003482{
Alp Tokerec34c482014-05-15 11:27:39 +00003483 _LIBCPP_ASSERT(__n2 == 0 || __s != nullptr, "string::compare(): received nullptr");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003484 size_type __sz = size();
3485 if (__pos1 > __sz || __n2 == npos)
3486 this->__throw_out_of_range();
Howard Hinnant0949eed2011-06-30 21:18:19 +00003487 size_type __rlen = _VSTD::min(__n1, __sz - __pos1);
3488 int __r = traits_type::compare(data() + __pos1, __s, _VSTD::min(__rlen, __n2));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003489 if (__r == 0)
3490 {
3491 if (__rlen < __n2)
3492 __r = -1;
3493 else if (__rlen > __n2)
3494 __r = 1;
3495 }
3496 return __r;
3497}
3498
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003499template <class _CharT, class _Traits, class _Allocator>
3500inline _LIBCPP_INLINE_VISIBILITY
3501int
3502basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3503 size_type __n1,
3504 __self_view __sv) const
3505{
3506 return compare(__pos1, __n1, __sv.data(), __sv.size());
3507}
3508
3509template <class _CharT, class _Traits, class _Allocator>
3510inline _LIBCPP_INLINE_VISIBILITY
3511int
3512basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3513 size_type __n1,
3514 const basic_string& __str) const
3515{
3516 return compare(__pos1, __n1, __str.data(), __str.size());
3517}
3518
3519template <class _CharT, class _Traits, class _Allocator>
Marshall Clow6ac8de02016-09-24 22:45:42 +00003520template <class _Tp>
3521typename enable_if
3522<
Marshall Clowf1729d92017-11-15 20:02:27 +00003523 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3524 int
Marshall Clow6ac8de02016-09-24 22:45:42 +00003525>::type
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003526basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3527 size_type __n1,
Marshall Clow6ac8de02016-09-24 22:45:42 +00003528 const _Tp& __t,
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003529 size_type __pos2,
3530 size_type __n2) const
3531{
Marshall Clow6ac8de02016-09-24 22:45:42 +00003532 __self_view __sv = __t;
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003533 return __self_view(*this).substr(__pos1, __n1).compare(__sv.substr(__pos2, __n2));
3534}
3535
3536template <class _CharT, class _Traits, class _Allocator>
3537int
3538basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3539 size_type __n1,
3540 const basic_string& __str,
3541 size_type __pos2,
3542 size_type __n2) const
3543{
3544 return compare(__pos1, __n1, __self_view(__str), __pos2, __n2);
3545}
3546
3547template <class _CharT, class _Traits, class _Allocator>
3548int
3549basic_string<_CharT, _Traits, _Allocator>::compare(const value_type* __s) const _NOEXCEPT
3550{
3551 _LIBCPP_ASSERT(__s != nullptr, "string::compare(): received nullptr");
3552 return compare(0, npos, __s, traits_type::length(__s));
3553}
3554
3555template <class _CharT, class _Traits, class _Allocator>
3556int
3557basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3558 size_type __n1,
3559 const value_type* __s) const
3560{
3561 _LIBCPP_ASSERT(__s != nullptr, "string::compare(): received nullptr");
3562 return compare(__pos1, __n1, __s, traits_type::length(__s));
3563}
3564
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003565// __invariants
3566
3567template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003568inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003569bool
3570basic_string<_CharT, _Traits, _Allocator>::__invariants() const
3571{
3572 if (size() > capacity())
3573 return false;
3574 if (capacity() < __min_cap - 1)
3575 return false;
3576 if (data() == 0)
3577 return false;
3578 if (data()[size()] != value_type(0))
3579 return false;
3580 return true;
3581}
3582
Vedant Kumar2b588cb2018-03-08 21:15:26 +00003583// __clear_and_shrink
3584
3585template<class _CharT, class _Traits, class _Allocator>
3586inline _LIBCPP_INLINE_VISIBILITY
3587void
3588basic_string<_CharT, _Traits, _Allocator>::__clear_and_shrink()
3589{
3590 clear();
3591 if(__is_long())
3592 {
3593 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), capacity() + 1);
3594 __set_long_cap(0);
3595 __set_short_size(0);
3596 }
3597}
3598
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003599// operator==
3600
3601template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003602inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003603bool
3604operator==(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003605 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003606{
Howard Hinnant08dd2532013-04-22 23:55:13 +00003607 size_t __lhs_sz = __lhs.size();
3608 return __lhs_sz == __rhs.size() && _Traits::compare(__lhs.data(),
3609 __rhs.data(),
3610 __lhs_sz) == 0;
3611}
3612
3613template<class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003614inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant08dd2532013-04-22 23:55:13 +00003615bool
3616operator==(const basic_string<char, char_traits<char>, _Allocator>& __lhs,
3617 const basic_string<char, char_traits<char>, _Allocator>& __rhs) _NOEXCEPT
3618{
3619 size_t __lhs_sz = __lhs.size();
3620 if (__lhs_sz != __rhs.size())
3621 return false;
3622 const char* __lp = __lhs.data();
3623 const char* __rp = __rhs.data();
3624 if (__lhs.__is_long())
3625 return char_traits<char>::compare(__lp, __rp, __lhs_sz) == 0;
3626 for (; __lhs_sz != 0; --__lhs_sz, ++__lp, ++__rp)
3627 if (*__lp != *__rp)
3628 return false;
3629 return true;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003630}
3631
3632template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003633inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003634bool
Howard Hinnanta6119a82011-05-29 19:57:12 +00003635operator==(const _CharT* __lhs,
3636 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003637{
Eric Fiselier4f241822015-08-28 03:02:37 +00003638 typedef basic_string<_CharT, _Traits, _Allocator> _String;
3639 _LIBCPP_ASSERT(__lhs != nullptr, "operator==(char*, basic_string): received nullptr");
3640 size_t __lhs_len = _Traits::length(__lhs);
3641 if (__lhs_len != __rhs.size()) return false;
3642 return __rhs.compare(0, _String::npos, __lhs, __lhs_len) == 0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003643}
3644
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003645template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003646inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003647bool
Howard Hinnanta6119a82011-05-29 19:57:12 +00003648operator==(const basic_string<_CharT,_Traits,_Allocator>& __lhs,
3649 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003650{
Eric Fiselier4f241822015-08-28 03:02:37 +00003651 typedef basic_string<_CharT, _Traits, _Allocator> _String;
3652 _LIBCPP_ASSERT(__rhs != nullptr, "operator==(basic_string, char*): received nullptr");
3653 size_t __rhs_len = _Traits::length(__rhs);
3654 if (__rhs_len != __lhs.size()) return false;
3655 return __lhs.compare(0, _String::npos, __rhs, __rhs_len) == 0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003656}
3657
Howard Hinnant324bb032010-08-22 00:02:43 +00003658template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003659inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003660bool
3661operator!=(const basic_string<_CharT,_Traits,_Allocator>& __lhs,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003662 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003663{
3664 return !(__lhs == __rhs);
3665}
3666
3667template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003668inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003669bool
Howard Hinnanta6119a82011-05-29 19:57:12 +00003670operator!=(const _CharT* __lhs,
3671 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003672{
3673 return !(__lhs == __rhs);
3674}
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
Howard Hinnanta6119a82011-05-29 19:57:12 +00003679operator!=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
3680 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003681{
3682 return !(__lhs == __rhs);
3683}
3684
3685// operator<
3686
3687template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003688inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003689bool
3690operator< (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003691 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003692{
Howard Hinnant2644a7b2011-07-24 15:07:21 +00003693 return __lhs.compare(__rhs) < 0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003694}
3695
3696template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003697inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003698bool
Howard Hinnanta6119a82011-05-29 19:57:12 +00003699operator< (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
3700 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003701{
Howard Hinnant2644a7b2011-07-24 15:07:21 +00003702 return __lhs.compare(__rhs) < 0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003703}
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
Howard Hinnanta6119a82011-05-29 19:57:12 +00003708operator< (const _CharT* __lhs,
3709 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003710{
3711 return __rhs.compare(__lhs) > 0;
3712}
3713
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003714// operator>
3715
3716template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003717inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003718bool
3719operator> (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003720 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003721{
3722 return __rhs < __lhs;
3723}
3724
3725template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003726inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003727bool
Howard Hinnanta6119a82011-05-29 19:57:12 +00003728operator> (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
3729 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003730{
3731 return __rhs < __lhs;
3732}
3733
3734template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003735inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003736bool
Howard Hinnanta6119a82011-05-29 19:57:12 +00003737operator> (const _CharT* __lhs,
3738 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003739{
3740 return __rhs < __lhs;
3741}
3742
3743// operator<=
3744
3745template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003746inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003747bool
3748operator<=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003749 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003750{
3751 return !(__rhs < __lhs);
3752}
3753
3754template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003755inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003756bool
Howard Hinnanta6119a82011-05-29 19:57:12 +00003757operator<=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
3758 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003759{
3760 return !(__rhs < __lhs);
3761}
3762
3763template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003764inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003765bool
Howard Hinnanta6119a82011-05-29 19:57:12 +00003766operator<=(const _CharT* __lhs,
3767 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003768{
3769 return !(__rhs < __lhs);
3770}
3771
3772// operator>=
3773
3774template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003775inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003776bool
3777operator>=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003778 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003779{
3780 return !(__lhs < __rhs);
3781}
3782
3783template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003784inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003785bool
Howard Hinnanta6119a82011-05-29 19:57:12 +00003786operator>=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
3787 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003788{
3789 return !(__lhs < __rhs);
3790}
3791
3792template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003793inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003794bool
Howard Hinnanta6119a82011-05-29 19:57:12 +00003795operator>=(const _CharT* __lhs,
3796 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003797{
3798 return !(__lhs < __rhs);
3799}
3800
3801// operator +
3802
3803template<class _CharT, class _Traits, class _Allocator>
3804basic_string<_CharT, _Traits, _Allocator>
3805operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
3806 const basic_string<_CharT, _Traits, _Allocator>& __rhs)
3807{
3808 basic_string<_CharT, _Traits, _Allocator> __r(__lhs.get_allocator());
3809 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = __lhs.size();
3810 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = __rhs.size();
3811 __r.__init(__lhs.data(), __lhs_sz, __lhs_sz + __rhs_sz);
3812 __r.append(__rhs.data(), __rhs_sz);
3813 return __r;
3814}
3815
3816template<class _CharT, class _Traits, class _Allocator>
3817basic_string<_CharT, _Traits, _Allocator>
3818operator+(const _CharT* __lhs , const basic_string<_CharT,_Traits,_Allocator>& __rhs)
3819{
3820 basic_string<_CharT, _Traits, _Allocator> __r(__rhs.get_allocator());
3821 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = _Traits::length(__lhs);
3822 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = __rhs.size();
3823 __r.__init(__lhs, __lhs_sz, __lhs_sz + __rhs_sz);
3824 __r.append(__rhs.data(), __rhs_sz);
3825 return __r;
3826}
3827
3828template<class _CharT, class _Traits, class _Allocator>
3829basic_string<_CharT, _Traits, _Allocator>
3830operator+(_CharT __lhs, const basic_string<_CharT,_Traits,_Allocator>& __rhs)
3831{
3832 basic_string<_CharT, _Traits, _Allocator> __r(__rhs.get_allocator());
3833 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = __rhs.size();
3834 __r.__init(&__lhs, 1, 1 + __rhs_sz);
3835 __r.append(__rhs.data(), __rhs_sz);
3836 return __r;
3837}
3838
3839template<class _CharT, class _Traits, class _Allocator>
3840basic_string<_CharT, _Traits, _Allocator>
3841operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, const _CharT* __rhs)
3842{
3843 basic_string<_CharT, _Traits, _Allocator> __r(__lhs.get_allocator());
3844 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = __lhs.size();
3845 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = _Traits::length(__rhs);
3846 __r.__init(__lhs.data(), __lhs_sz, __lhs_sz + __rhs_sz);
3847 __r.append(__rhs, __rhs_sz);
3848 return __r;
3849}
3850
3851template<class _CharT, class _Traits, class _Allocator>
3852basic_string<_CharT, _Traits, _Allocator>
3853operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, _CharT __rhs)
3854{
3855 basic_string<_CharT, _Traits, _Allocator> __r(__lhs.get_allocator());
3856 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = __lhs.size();
3857 __r.__init(__lhs.data(), __lhs_sz, __lhs_sz + 1);
3858 __r.push_back(__rhs);
3859 return __r;
3860}
3861
Eric Fiselier3e928972017-04-19 00:28:44 +00003862#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003863
3864template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003865inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003866basic_string<_CharT, _Traits, _Allocator>
3867operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, const basic_string<_CharT, _Traits, _Allocator>& __rhs)
3868{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003869 return _VSTD::move(__lhs.append(__rhs));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003870}
3871
3872template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003873inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003874basic_string<_CharT, _Traits, _Allocator>
3875operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, basic_string<_CharT, _Traits, _Allocator>&& __rhs)
3876{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003877 return _VSTD::move(__rhs.insert(0, __lhs));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003878}
3879
3880template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003881inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003882basic_string<_CharT, _Traits, _Allocator>
3883operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, basic_string<_CharT, _Traits, _Allocator>&& __rhs)
3884{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003885 return _VSTD::move(__lhs.append(__rhs));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003886}
3887
3888template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003889inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003890basic_string<_CharT, _Traits, _Allocator>
3891operator+(const _CharT* __lhs , basic_string<_CharT,_Traits,_Allocator>&& __rhs)
3892{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003893 return _VSTD::move(__rhs.insert(0, __lhs));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003894}
3895
3896template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003897inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003898basic_string<_CharT, _Traits, _Allocator>
3899operator+(_CharT __lhs, basic_string<_CharT,_Traits,_Allocator>&& __rhs)
3900{
3901 __rhs.insert(__rhs.begin(), __lhs);
Howard Hinnant0949eed2011-06-30 21:18:19 +00003902 return _VSTD::move(__rhs);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003903}
3904
3905template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003906inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003907basic_string<_CharT, _Traits, _Allocator>
3908operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, const _CharT* __rhs)
3909{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003910 return _VSTD::move(__lhs.append(__rhs));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003911}
3912
3913template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003914inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003915basic_string<_CharT, _Traits, _Allocator>
3916operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, _CharT __rhs)
3917{
3918 __lhs.push_back(__rhs);
Howard Hinnant0949eed2011-06-30 21:18:19 +00003919 return _VSTD::move(__lhs);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003920}
3921
Eric Fiselier3e928972017-04-19 00:28:44 +00003922#endif // _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003923
3924// swap
3925
3926template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003927inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003928void
Howard Hinnanta6119a82011-05-29 19:57:12 +00003929swap(basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnant53f7d4c2011-06-03 18:40:47 +00003930 basic_string<_CharT, _Traits, _Allocator>& __rhs)
3931 _NOEXCEPT_(_NOEXCEPT_(__lhs.swap(__rhs)))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003932{
3933 __lhs.swap(__rhs);
3934}
3935
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003936#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
3937
3938typedef basic_string<char16_t> u16string;
3939typedef basic_string<char32_t> u32string;
3940
Howard Hinnant324bb032010-08-22 00:02:43 +00003941#endif // _LIBCPP_HAS_NO_UNICODE_CHARS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003942
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003943_LIBCPP_FUNC_VIS int stoi (const string& __str, size_t* __idx = 0, int __base = 10);
3944_LIBCPP_FUNC_VIS long stol (const string& __str, size_t* __idx = 0, int __base = 10);
3945_LIBCPP_FUNC_VIS unsigned long stoul (const string& __str, size_t* __idx = 0, int __base = 10);
3946_LIBCPP_FUNC_VIS long long stoll (const string& __str, size_t* __idx = 0, int __base = 10);
3947_LIBCPP_FUNC_VIS unsigned long long stoull(const string& __str, size_t* __idx = 0, int __base = 10);
Howard Hinnanta6a062d2010-06-02 18:20:39 +00003948
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003949_LIBCPP_FUNC_VIS float stof (const string& __str, size_t* __idx = 0);
3950_LIBCPP_FUNC_VIS double stod (const string& __str, size_t* __idx = 0);
3951_LIBCPP_FUNC_VIS long double stold(const string& __str, size_t* __idx = 0);
Howard Hinnanta6a062d2010-06-02 18:20:39 +00003952
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003953_LIBCPP_FUNC_VIS string to_string(int __val);
3954_LIBCPP_FUNC_VIS string to_string(unsigned __val);
3955_LIBCPP_FUNC_VIS string to_string(long __val);
3956_LIBCPP_FUNC_VIS string to_string(unsigned long __val);
3957_LIBCPP_FUNC_VIS string to_string(long long __val);
3958_LIBCPP_FUNC_VIS string to_string(unsigned long long __val);
3959_LIBCPP_FUNC_VIS string to_string(float __val);
3960_LIBCPP_FUNC_VIS string to_string(double __val);
3961_LIBCPP_FUNC_VIS string to_string(long double __val);
Howard Hinnanta6a062d2010-06-02 18:20:39 +00003962
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003963_LIBCPP_FUNC_VIS int stoi (const wstring& __str, size_t* __idx = 0, int __base = 10);
3964_LIBCPP_FUNC_VIS long stol (const wstring& __str, size_t* __idx = 0, int __base = 10);
3965_LIBCPP_FUNC_VIS unsigned long stoul (const wstring& __str, size_t* __idx = 0, int __base = 10);
3966_LIBCPP_FUNC_VIS long long stoll (const wstring& __str, size_t* __idx = 0, int __base = 10);
3967_LIBCPP_FUNC_VIS unsigned long long stoull(const wstring& __str, size_t* __idx = 0, int __base = 10);
Howard Hinnanta6a062d2010-06-02 18:20:39 +00003968
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003969_LIBCPP_FUNC_VIS float stof (const wstring& __str, size_t* __idx = 0);
3970_LIBCPP_FUNC_VIS double stod (const wstring& __str, size_t* __idx = 0);
3971_LIBCPP_FUNC_VIS long double stold(const wstring& __str, size_t* __idx = 0);
Howard Hinnanta6a062d2010-06-02 18:20:39 +00003972
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003973_LIBCPP_FUNC_VIS wstring to_wstring(int __val);
3974_LIBCPP_FUNC_VIS wstring to_wstring(unsigned __val);
3975_LIBCPP_FUNC_VIS wstring to_wstring(long __val);
3976_LIBCPP_FUNC_VIS wstring to_wstring(unsigned long __val);
3977_LIBCPP_FUNC_VIS wstring to_wstring(long long __val);
3978_LIBCPP_FUNC_VIS wstring to_wstring(unsigned long long __val);
3979_LIBCPP_FUNC_VIS wstring to_wstring(float __val);
3980_LIBCPP_FUNC_VIS wstring to_wstring(double __val);
3981_LIBCPP_FUNC_VIS wstring to_wstring(long double __val);
Howard Hinnanta6a062d2010-06-02 18:20:39 +00003982
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003983template<class _CharT, class _Traits, class _Allocator>
3984 const typename basic_string<_CharT, _Traits, _Allocator>::size_type
3985 basic_string<_CharT, _Traits, _Allocator>::npos;
3986
3987template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierc3589a82017-01-04 23:56:00 +00003988struct _LIBCPP_TEMPLATE_VIS hash<basic_string<_CharT, _Traits, _Allocator> >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003989 : public unary_function<basic_string<_CharT, _Traits, _Allocator>, size_t>
3990{
3991 size_t
Howard Hinnanta6119a82011-05-29 19:57:12 +00003992 operator()(const basic_string<_CharT, _Traits, _Allocator>& __val) const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003993};
3994
3995template<class _CharT, class _Traits, class _Allocator>
3996size_t
3997hash<basic_string<_CharT, _Traits, _Allocator> >::operator()(
Howard Hinnanta6119a82011-05-29 19:57:12 +00003998 const basic_string<_CharT, _Traits, _Allocator>& __val) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003999{
Sean Huntaffd9e52011-07-29 23:31:56 +00004000 return __do_string_hash(__val.data(), __val.data() + __val.size());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004001}
4002
Howard Hinnant464aa5c2011-07-18 15:51:59 +00004003template<class _CharT, class _Traits, class _Allocator>
4004basic_ostream<_CharT, _Traits>&
4005operator<<(basic_ostream<_CharT, _Traits>& __os,
4006 const basic_string<_CharT, _Traits, _Allocator>& __str);
4007
4008template<class _CharT, class _Traits, class _Allocator>
4009basic_istream<_CharT, _Traits>&
4010operator>>(basic_istream<_CharT, _Traits>& __is,
4011 basic_string<_CharT, _Traits, _Allocator>& __str);
4012
4013template<class _CharT, class _Traits, class _Allocator>
4014basic_istream<_CharT, _Traits>&
4015getline(basic_istream<_CharT, _Traits>& __is,
4016 basic_string<_CharT, _Traits, _Allocator>& __str, _CharT __dlm);
4017
4018template<class _CharT, class _Traits, class _Allocator>
4019inline _LIBCPP_INLINE_VISIBILITY
4020basic_istream<_CharT, _Traits>&
4021getline(basic_istream<_CharT, _Traits>& __is,
4022 basic_string<_CharT, _Traits, _Allocator>& __str);
4023
Eric Fiselier3e928972017-04-19 00:28:44 +00004024#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant464aa5c2011-07-18 15:51:59 +00004025
4026template<class _CharT, class _Traits, class _Allocator>
4027inline _LIBCPP_INLINE_VISIBILITY
4028basic_istream<_CharT, _Traits>&
4029getline(basic_istream<_CharT, _Traits>&& __is,
4030 basic_string<_CharT, _Traits, _Allocator>& __str, _CharT __dlm);
4031
4032template<class _CharT, class _Traits, class _Allocator>
4033inline _LIBCPP_INLINE_VISIBILITY
4034basic_istream<_CharT, _Traits>&
4035getline(basic_istream<_CharT, _Traits>&& __is,
4036 basic_string<_CharT, _Traits, _Allocator>& __str);
4037
Eric Fiselier3e928972017-04-19 00:28:44 +00004038#endif // _LIBCPP_CXX03_LANG
Howard Hinnant464aa5c2011-07-18 15:51:59 +00004039
Howard Hinnant499cea12013-08-23 17:37:05 +00004040#if _LIBCPP_DEBUG_LEVEL >= 2
4041
4042template<class _CharT, class _Traits, class _Allocator>
4043bool
4044basic_string<_CharT, _Traits, _Allocator>::__dereferenceable(const const_iterator* __i) const
4045{
4046 return this->data() <= _VSTD::__to_raw_pointer(__i->base()) &&
4047 _VSTD::__to_raw_pointer(__i->base()) < this->data() + this->size();
4048}
4049
4050template<class _CharT, class _Traits, class _Allocator>
4051bool
4052basic_string<_CharT, _Traits, _Allocator>::__decrementable(const const_iterator* __i) const
4053{
4054 return this->data() < _VSTD::__to_raw_pointer(__i->base()) &&
4055 _VSTD::__to_raw_pointer(__i->base()) <= this->data() + this->size();
4056}
4057
4058template<class _CharT, class _Traits, class _Allocator>
4059bool
4060basic_string<_CharT, _Traits, _Allocator>::__addable(const const_iterator* __i, ptrdiff_t __n) const
4061{
4062 const value_type* __p = _VSTD::__to_raw_pointer(__i->base()) + __n;
4063 return this->data() <= __p && __p <= this->data() + this->size();
4064}
4065
4066template<class _CharT, class _Traits, class _Allocator>
4067bool
4068basic_string<_CharT, _Traits, _Allocator>::__subscriptable(const const_iterator* __i, ptrdiff_t __n) const
4069{
4070 const value_type* __p = _VSTD::__to_raw_pointer(__i->base()) + __n;
4071 return this->data() <= __p && __p < this->data() + this->size();
4072}
4073
4074#endif // _LIBCPP_DEBUG_LEVEL >= 2
4075
Shoaib Meenai68506702017-06-29 02:52:46 +00004076_LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_string<char>)
4077_LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_string<wchar_t>)
Shoaib Meenai68506702017-06-29 02:52:46 +00004078
Marshall Clow15234322013-07-23 17:05:24 +00004079#if _LIBCPP_STD_VER > 11
4080// Literal suffixes for basic_string [basic.string.literals]
Marshall Clow8d9dd7a2013-10-05 21:18:32 +00004081inline namespace literals
Marshall Clow15234322013-07-23 17:05:24 +00004082{
4083 inline namespace string_literals
4084 {
Howard Hinnantab61b2c2013-08-07 19:39:48 +00004085 inline _LIBCPP_INLINE_VISIBILITY
4086 basic_string<char> operator "" s( const char *__str, size_t __len )
4087 {
4088 return basic_string<char> (__str, __len);
4089 }
Marshall Clow15234322013-07-23 17:05:24 +00004090
Howard Hinnantab61b2c2013-08-07 19:39:48 +00004091 inline _LIBCPP_INLINE_VISIBILITY
4092 basic_string<wchar_t> operator "" s( const wchar_t *__str, size_t __len )
4093 {
4094 return basic_string<wchar_t> (__str, __len);
4095 }
Marshall Clow15234322013-07-23 17:05:24 +00004096
Howard Hinnantab61b2c2013-08-07 19:39:48 +00004097 inline _LIBCPP_INLINE_VISIBILITY
4098 basic_string<char16_t> operator "" s( const char16_t *__str, size_t __len )
4099 {
4100 return basic_string<char16_t> (__str, __len);
4101 }
Marshall Clow15234322013-07-23 17:05:24 +00004102
Howard Hinnantab61b2c2013-08-07 19:39:48 +00004103 inline _LIBCPP_INLINE_VISIBILITY
4104 basic_string<char32_t> operator "" s( const char32_t *__str, size_t __len )
4105 {
4106 return basic_string<char32_t> (__str, __len);
4107 }
Marshall Clow15234322013-07-23 17:05:24 +00004108 }
4109}
4110#endif
4111
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004112_LIBCPP_END_NAMESPACE_STD
4113
Eric Fiselier018a3d52017-05-31 22:07:49 +00004114_LIBCPP_POP_MACROS
4115
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004116#endif // _LIBCPP_STRING