blob: af028a023c299b3a97e734d24def62d3fc73694b [file] [log] [blame]
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001// -*- C++ -*-
2//===--------------------------- string -----------------------------------===//
3//
Howard Hinnantf5256e12010-05-11 21:36:01 +00004// The LLVM Compiler Infrastructure
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005//
6// This file is distributed under the University of Illinois Open Source
7// License. See LICENSE.TXT for details.
8//
9//===----------------------------------------------------------------------===//
10
11#ifndef _LIBCPP_STRING
12#define _LIBCPP_STRING
13
14/*
15 string synopsis
16
17namespace std
18{
19
20template <class stateT>
21class fpos
22{
23private:
24 stateT st;
25public:
26 fpos(streamoff = streamoff());
27
28 operator streamoff() const;
29
30 stateT state() const;
31 void state(stateT);
32
33 fpos& operator+=(streamoff);
34 fpos operator+ (streamoff) const;
35 fpos& operator-=(streamoff);
36 fpos operator- (streamoff) const;
37};
38
39template <class stateT> streamoff operator-(const fpos<stateT>& x, const fpos<stateT>& y);
40
41template <class stateT> bool operator==(const fpos<stateT>& x, const fpos<stateT>& y);
42template <class stateT> bool operator!=(const fpos<stateT>& x, const fpos<stateT>& y);
43
44template <class charT>
45struct char_traits
46{
47 typedef charT char_type;
48 typedef ... int_type;
49 typedef streamoff off_type;
50 typedef streampos pos_type;
51 typedef mbstate_t state_type;
52
Howard Hinnanta6119a82011-05-29 19:57:12 +000053 static void assign(char_type& c1, const char_type& c2) noexcept;
Howard Hinnant03d71812012-07-20 19:09:12 +000054 static constexpr bool eq(char_type c1, char_type c2) noexcept;
55 static constexpr bool lt(char_type c1, char_type c2) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000056
57 static int compare(const char_type* s1, const char_type* s2, size_t n);
58 static size_t length(const char_type* s);
59 static const char_type* find(const char_type* s, size_t n, const char_type& a);
60 static char_type* move(char_type* s1, const char_type* s2, size_t n);
61 static char_type* copy(char_type* s1, const char_type* s2, size_t n);
62 static char_type* assign(char_type* s, size_t n, char_type a);
63
Howard Hinnant03d71812012-07-20 19:09:12 +000064 static constexpr int_type not_eof(int_type c) noexcept;
65 static constexpr char_type to_char_type(int_type c) noexcept;
66 static constexpr int_type to_int_type(char_type c) noexcept;
67 static constexpr bool eq_int_type(int_type c1, int_type c2) noexcept;
68 static constexpr int_type eof() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000069};
70
71template <> struct char_traits<char>;
72template <> struct char_traits<wchar_t>;
73
Howard Hinnant324bb032010-08-22 00:02:43 +000074template<class charT, class traits = char_traits<charT>, class Allocator = allocator<charT> >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000075class basic_string
76{
Howard Hinnant324bb032010-08-22 00:02:43 +000077public:
78// types:
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000079 typedef traits traits_type;
80 typedef typename traits_type::char_type value_type;
81 typedef Allocator allocator_type;
82 typedef typename allocator_type::size_type size_type;
83 typedef typename allocator_type::difference_type difference_type;
84 typedef typename allocator_type::reference reference;
85 typedef typename allocator_type::const_reference const_reference;
86 typedef typename allocator_type::pointer pointer;
87 typedef typename allocator_type::const_pointer const_pointer;
88 typedef implementation-defined iterator;
89 typedef implementation-defined const_iterator;
90 typedef std::reverse_iterator<iterator> reverse_iterator;
91 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
92
93 static const size_type npos = -1;
94
Howard Hinnant53f7d4c2011-06-03 18:40:47 +000095 basic_string()
96 noexcept(is_nothrow_default_constructible<allocator_type>::value);
97 explicit basic_string(const allocator_type& a);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000098 basic_string(const basic_string& str);
Howard Hinnant53f7d4c2011-06-03 18:40:47 +000099 basic_string(basic_string&& str)
100 noexcept(is_nothrow_move_constructible<allocator_type>::value);
Marshall Clowf4f7d8f2016-04-07 18:13:41 +0000101 basic_string(const basic_string& str, size_type pos,
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000102 const allocator_type& a = allocator_type());
Marshall Clowf4f7d8f2016-04-07 18:13:41 +0000103 basic_string(const basic_string& str, size_type pos, size_type n,
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000104 const Allocator& a = Allocator());
Marshall Clowdb7fa112016-11-14 18:22:19 +0000105 template<class T>
106 basic_string(const T& t, size_type pos, size_type n, const Allocator& a = Allocator()); // C++17
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000107 explicit basic_string(const basic_string_view<charT, traits> sv, const Allocator& a = Allocator());
Howard Hinnant9dcdcde2013-06-28 16:59:19 +0000108 basic_string(const value_type* s, const allocator_type& a = allocator_type());
109 basic_string(const value_type* s, size_type n, const allocator_type& a = allocator_type());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000110 basic_string(size_type n, value_type c, const allocator_type& a = allocator_type());
111 template<class InputIterator>
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000112 basic_string(InputIterator begin, InputIterator end,
113 const allocator_type& a = allocator_type());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000114 basic_string(initializer_list<value_type>, const Allocator& = Allocator());
115 basic_string(const basic_string&, const Allocator&);
116 basic_string(basic_string&&, const Allocator&);
117
118 ~basic_string();
119
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000120 operator basic_string_view<charT, traits>() const noexcept;
121
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000122 basic_string& operator=(const basic_string& str);
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000123 basic_string& operator=(basic_string_view<charT, traits> sv);
Howard Hinnant53f7d4c2011-06-03 18:40:47 +0000124 basic_string& operator=(basic_string&& str)
125 noexcept(
Marshall Clowaf961ed2015-08-18 18:57:00 +0000126 allocator_type::propagate_on_container_move_assignment::value ||
127 allocator_type::is_always_equal::value ); // C++17
Howard Hinnant9dcdcde2013-06-28 16:59:19 +0000128 basic_string& operator=(const value_type* s);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000129 basic_string& operator=(value_type c);
130 basic_string& operator=(initializer_list<value_type>);
131
Howard Hinnanta6119a82011-05-29 19:57:12 +0000132 iterator begin() noexcept;
133 const_iterator begin() const noexcept;
134 iterator end() noexcept;
135 const_iterator end() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000136
Howard Hinnanta6119a82011-05-29 19:57:12 +0000137 reverse_iterator rbegin() noexcept;
138 const_reverse_iterator rbegin() const noexcept;
139 reverse_iterator rend() noexcept;
140 const_reverse_iterator rend() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000141
Howard Hinnanta6119a82011-05-29 19:57:12 +0000142 const_iterator cbegin() const noexcept;
143 const_iterator cend() const noexcept;
144 const_reverse_iterator crbegin() const noexcept;
145 const_reverse_iterator crend() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000146
Howard Hinnanta6119a82011-05-29 19:57:12 +0000147 size_type size() const noexcept;
148 size_type length() const noexcept;
149 size_type max_size() const noexcept;
150 size_type capacity() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000151
152 void resize(size_type n, value_type c);
153 void resize(size_type n);
154
155 void reserve(size_type res_arg = 0);
156 void shrink_to_fit();
Howard Hinnanta6119a82011-05-29 19:57:12 +0000157 void clear() noexcept;
158 bool empty() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000159
160 const_reference operator[](size_type pos) const;
161 reference operator[](size_type pos);
162
163 const_reference at(size_type n) const;
164 reference at(size_type n);
165
166 basic_string& operator+=(const basic_string& str);
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000167 basic_string& operator+=(basic_string_view<charT, traits> sv);
Howard Hinnant9dcdcde2013-06-28 16:59:19 +0000168 basic_string& operator+=(const value_type* s);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000169 basic_string& operator+=(value_type c);
170 basic_string& operator+=(initializer_list<value_type>);
171
172 basic_string& append(const basic_string& str);
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000173 basic_string& append(basic_string_view<charT, traits> sv);
Marshall Clowa93b5e22014-03-04 19:17:19 +0000174 basic_string& append(const basic_string& str, size_type pos, size_type n=npos); //C++14
Marshall Clow6ac8de02016-09-24 22:45:42 +0000175 template <class T>
176 basic_string& append(const T& t, size_type pos, size_type n=npos); // C++17
Howard Hinnant9dcdcde2013-06-28 16:59:19 +0000177 basic_string& append(const value_type* s, size_type n);
178 basic_string& append(const value_type* s);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000179 basic_string& append(size_type n, value_type c);
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000180 template<class InputIterator>
181 basic_string& append(InputIterator first, InputIterator last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000182 basic_string& append(initializer_list<value_type>);
183
184 void push_back(value_type c);
185 void pop_back();
186 reference front();
187 const_reference front() const;
188 reference back();
189 const_reference back() const;
190
191 basic_string& assign(const basic_string& str);
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000192 basic_string& assign(basic_string_view<charT, traits> sv);
Howard Hinnanta6119a82011-05-29 19:57:12 +0000193 basic_string& assign(basic_string&& str);
Marshall Clowa93b5e22014-03-04 19:17:19 +0000194 basic_string& assign(const basic_string& str, size_type pos, size_type n=npos); // C++14
Marshall Clow6ac8de02016-09-24 22:45:42 +0000195 template <class T>
196 basic_string& assign(const T& t, size_type pos, size_type n=npos); // C++17
Howard Hinnant9dcdcde2013-06-28 16:59:19 +0000197 basic_string& assign(const value_type* s, size_type n);
198 basic_string& assign(const value_type* s);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000199 basic_string& assign(size_type n, value_type c);
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000200 template<class InputIterator>
201 basic_string& assign(InputIterator first, InputIterator last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000202 basic_string& assign(initializer_list<value_type>);
203
204 basic_string& insert(size_type pos1, const basic_string& str);
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000205 basic_string& insert(size_type pos1, basic_string_view<charT, traits> sv);
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000206 basic_string& insert(size_type pos1, const basic_string& str,
207 size_type pos2, size_type n);
Marshall Clow6ac8de02016-09-24 22:45:42 +0000208 template <class T>
209 basic_string& insert(size_type pos1, const T& t, size_type pos2, size_type n); // C++17
Marshall Clowa93b5e22014-03-04 19:17:19 +0000210 basic_string& insert(size_type pos, const value_type* s, size_type n=npos); //C++14
Howard Hinnant9dcdcde2013-06-28 16:59:19 +0000211 basic_string& insert(size_type pos, const value_type* s);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000212 basic_string& insert(size_type pos, size_type n, value_type c);
213 iterator insert(const_iterator p, value_type c);
214 iterator insert(const_iterator p, size_type n, value_type c);
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000215 template<class InputIterator>
216 iterator insert(const_iterator p, InputIterator first, InputIterator last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000217 iterator insert(const_iterator p, initializer_list<value_type>);
218
219 basic_string& erase(size_type pos = 0, size_type n = npos);
220 iterator erase(const_iterator position);
221 iterator erase(const_iterator first, const_iterator last);
222
223 basic_string& replace(size_type pos1, size_type n1, const basic_string& str);
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000224 basic_string& replace(size_type pos1, size_type n1, basic_string_view<charT, traits> sv);
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000225 basic_string& replace(size_type pos1, size_type n1, const basic_string& str,
Marshall Clowa93b5e22014-03-04 19:17:19 +0000226 size_type pos2, size_type n2=npos); // C++14
Marshall Clow6ac8de02016-09-24 22:45:42 +0000227 template <class T>
228 basic_string& replace(size_type pos1, size_type n1, const T& t,
229 size_type pos2, size_type n); // C++17
Howard Hinnant9dcdcde2013-06-28 16:59:19 +0000230 basic_string& replace(size_type pos, size_type n1, const value_type* s, size_type n2);
231 basic_string& replace(size_type pos, size_type n1, const value_type* s);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000232 basic_string& replace(size_type pos, size_type n1, size_type n2, value_type c);
Howard Hinnant7b2cb482010-11-17 21:11:40 +0000233 basic_string& replace(const_iterator i1, const_iterator i2, const basic_string& str);
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000234 basic_string& replace(const_iterator i1, const_iterator i2, basic_string_view<charT, traits> sv);
Howard Hinnant9dcdcde2013-06-28 16:59:19 +0000235 basic_string& replace(const_iterator i1, const_iterator i2, const value_type* s, size_type n);
236 basic_string& replace(const_iterator i1, const_iterator i2, const value_type* s);
Howard Hinnant7b2cb482010-11-17 21:11:40 +0000237 basic_string& replace(const_iterator i1, const_iterator i2, size_type n, value_type c);
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000238 template<class InputIterator>
Howard Hinnant7b2cb482010-11-17 21:11:40 +0000239 basic_string& replace(const_iterator i1, const_iterator i2, InputIterator j1, InputIterator j2);
240 basic_string& replace(const_iterator i1, const_iterator i2, initializer_list<value_type>);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000241
Howard Hinnant9dcdcde2013-06-28 16:59:19 +0000242 size_type copy(value_type* s, size_type n, size_type pos = 0) const;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000243 basic_string substr(size_type pos = 0, size_type n = npos) const;
244
Howard Hinnant53f7d4c2011-06-03 18:40:47 +0000245 void swap(basic_string& str)
Marshall Clow7d914d12015-07-13 20:04:56 +0000246 noexcept(allocator_traits<allocator_type>::propagate_on_container_swap::value ||
247 allocator_traits<allocator_type>::is_always_equal::value); // C++17
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000248
Howard Hinnant9dcdcde2013-06-28 16:59:19 +0000249 const value_type* c_str() const noexcept;
250 const value_type* data() const noexcept;
Marshall Clowf532a702016-03-08 15:44:30 +0000251 value_type* data() noexcept; // C++17
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000252
Howard Hinnanta6119a82011-05-29 19:57:12 +0000253 allocator_type get_allocator() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000254
Howard Hinnanta6119a82011-05-29 19:57:12 +0000255 size_type find(const basic_string& str, size_type pos = 0) const noexcept;
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000256 size_type find(basic_string_view<charT, traits> sv, size_type pos = 0) const noexcept;
Howard Hinnant9dcdcde2013-06-28 16:59:19 +0000257 size_type find(const value_type* s, size_type pos, size_type n) const noexcept;
258 size_type find(const value_type* s, size_type pos = 0) const noexcept;
Howard Hinnanta6119a82011-05-29 19:57:12 +0000259 size_type find(value_type c, size_type pos = 0) const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000260
Howard Hinnanta6119a82011-05-29 19:57:12 +0000261 size_type rfind(const basic_string& str, size_type pos = npos) const noexcept;
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000262 size_type ffind(basic_string_view<charT, traits> sv, size_type pos = 0) const noexcept;
Howard Hinnant9dcdcde2013-06-28 16:59:19 +0000263 size_type rfind(const value_type* s, size_type pos, size_type n) const noexcept;
264 size_type rfind(const value_type* s, size_type pos = npos) const noexcept;
Howard Hinnanta6119a82011-05-29 19:57:12 +0000265 size_type rfind(value_type c, size_type pos = npos) const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000266
Howard Hinnanta6119a82011-05-29 19:57:12 +0000267 size_type find_first_of(const basic_string& str, size_type pos = 0) const noexcept;
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000268 size_type find_first_of(basic_string_view<charT, traits> sv, size_type pos = 0) const noexcept;
Howard Hinnant9dcdcde2013-06-28 16:59:19 +0000269 size_type find_first_of(const value_type* s, size_type pos, size_type n) const noexcept;
270 size_type find_first_of(const value_type* s, size_type pos = 0) const noexcept;
Howard Hinnanta6119a82011-05-29 19:57:12 +0000271 size_type find_first_of(value_type c, size_type pos = 0) const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000272
Howard Hinnanta6119a82011-05-29 19:57:12 +0000273 size_type find_last_of(const basic_string& str, size_type pos = npos) const noexcept;
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000274 size_type find_last_of(basic_string_view<charT, traits> sv, size_type pos = 0) const noexcept;
Howard Hinnant9dcdcde2013-06-28 16:59:19 +0000275 size_type find_last_of(const value_type* s, size_type pos, size_type n) const noexcept;
276 size_type find_last_of(const value_type* s, size_type pos = npos) const noexcept;
Howard Hinnanta6119a82011-05-29 19:57:12 +0000277 size_type find_last_of(value_type c, size_type pos = npos) const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000278
Howard Hinnanta6119a82011-05-29 19:57:12 +0000279 size_type find_first_not_of(const basic_string& str, size_type pos = 0) const noexcept;
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000280 size_type find_first_not_of(basic_string_view<charT, traits> sv, size_type pos = 0) const noexcept;
Howard Hinnant9dcdcde2013-06-28 16:59:19 +0000281 size_type find_first_not_of(const value_type* s, size_type pos, size_type n) const noexcept;
282 size_type find_first_not_of(const value_type* s, size_type pos = 0) const noexcept;
Howard Hinnanta6119a82011-05-29 19:57:12 +0000283 size_type find_first_not_of(value_type c, size_type pos = 0) const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000284
Howard Hinnanta6119a82011-05-29 19:57:12 +0000285 size_type find_last_not_of(const basic_string& str, size_type pos = npos) const noexcept;
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000286 size_type find_last_not_of(basic_string_view<charT, traits> sv, size_type pos = 0) const noexcept;
Howard Hinnant9dcdcde2013-06-28 16:59:19 +0000287 size_type find_last_not_of(const value_type* s, size_type pos, size_type n) const noexcept;
288 size_type find_last_not_of(const value_type* s, size_type pos = npos) const noexcept;
Howard Hinnanta6119a82011-05-29 19:57:12 +0000289 size_type find_last_not_of(value_type c, size_type pos = npos) const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000290
Howard Hinnanta6119a82011-05-29 19:57:12 +0000291 int compare(const basic_string& str) const noexcept;
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000292 int compare(basic_string_view<charT, traits> sv) const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000293 int compare(size_type pos1, size_type n1, const basic_string& str) const;
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000294 int compare(size_type pos1, size_type n1, basic_string_view<charT, traits> sv) const;
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000295 int compare(size_type pos1, size_type n1, const basic_string& str,
Marshall Clowa93b5e22014-03-04 19:17:19 +0000296 size_type pos2, size_type n2=npos) const; // C++14
Marshall Clow6ac8de02016-09-24 22:45:42 +0000297 template <class T>
298 int compare(size_type pos1, size_type n1, const T& t,
299 size_type pos2, size_type n2=npos) const; // C++17
Howard Hinnant9dcdcde2013-06-28 16:59:19 +0000300 int compare(const value_type* s) const noexcept;
301 int compare(size_type pos1, size_type n1, const value_type* s) const;
302 int compare(size_type pos1, size_type n1, const value_type* s, size_type n2) const;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000303
304 bool __invariants() const;
305};
306
307template<class charT, class traits, class Allocator>
308basic_string<charT, traits, Allocator>
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000309operator+(const basic_string<charT, traits, Allocator>& lhs,
310 const basic_string<charT, traits, Allocator>& rhs);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000311
312template<class charT, class traits, class Allocator>
313basic_string<charT, traits, Allocator>
314operator+(const charT* lhs , const basic_string<charT,traits,Allocator>&rhs);
315
316template<class charT, class traits, class Allocator>
317basic_string<charT, traits, Allocator>
318operator+(charT lhs, const basic_string<charT,traits,Allocator>& rhs);
319
320template<class charT, class traits, class Allocator>
321basic_string<charT, traits, Allocator>
322operator+(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs);
323
324template<class charT, class traits, class Allocator>
325basic_string<charT, traits, Allocator>
326operator+(const basic_string<charT, traits, Allocator>& lhs, charT rhs);
327
328template<class charT, class traits, class Allocator>
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000329bool operator==(const basic_string<charT, traits, Allocator>& lhs,
Howard Hinnanta6119a82011-05-29 19:57:12 +0000330 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000331
332template<class charT, class traits, class Allocator>
Howard Hinnanta6119a82011-05-29 19:57:12 +0000333bool operator==(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000334
335template<class charT, class traits, class Allocator>
Howard Hinnanta6119a82011-05-29 19:57:12 +0000336bool operator==(const basic_string<charT,traits,Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000337
Howard Hinnant324bb032010-08-22 00:02:43 +0000338template<class charT, class traits, class Allocator>
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000339bool operator!=(const basic_string<charT,traits,Allocator>& lhs,
Howard Hinnanta6119a82011-05-29 19:57:12 +0000340 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000341
342template<class charT, class traits, class Allocator>
Howard Hinnanta6119a82011-05-29 19:57:12 +0000343bool operator!=(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000344
345template<class charT, class traits, class Allocator>
Howard Hinnanta6119a82011-05-29 19:57:12 +0000346bool operator!=(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000347
348template<class charT, class traits, class Allocator>
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000349bool operator< (const basic_string<charT, traits, Allocator>& lhs,
Howard Hinnanta6119a82011-05-29 19:57:12 +0000350 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000351
352template<class charT, class traits, class Allocator>
Howard Hinnanta6119a82011-05-29 19:57:12 +0000353bool operator< (const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000354
355template<class charT, class traits, class Allocator>
Howard Hinnanta6119a82011-05-29 19:57:12 +0000356bool operator< (const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000357
358template<class charT, class traits, class Allocator>
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000359bool operator> (const basic_string<charT, traits, Allocator>& lhs,
Howard Hinnanta6119a82011-05-29 19:57:12 +0000360 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000361
362template<class charT, class traits, class Allocator>
Howard Hinnanta6119a82011-05-29 19:57:12 +0000363bool operator> (const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000364
365template<class charT, class traits, class Allocator>
Howard Hinnanta6119a82011-05-29 19:57:12 +0000366bool operator> (const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000367
368template<class charT, class traits, class Allocator>
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000369bool operator<=(const basic_string<charT, traits, Allocator>& lhs,
Howard Hinnanta6119a82011-05-29 19:57:12 +0000370 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000371
372template<class charT, class traits, class Allocator>
Howard Hinnanta6119a82011-05-29 19:57:12 +0000373bool operator<=(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000374
375template<class charT, class traits, class Allocator>
Howard Hinnanta6119a82011-05-29 19:57:12 +0000376bool operator<=(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000377
378template<class charT, class traits, class Allocator>
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000379bool operator>=(const basic_string<charT, traits, Allocator>& lhs,
Howard Hinnanta6119a82011-05-29 19:57:12 +0000380 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000381
382template<class charT, class traits, class Allocator>
Howard Hinnanta6119a82011-05-29 19:57:12 +0000383bool operator>=(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000384
385template<class charT, class traits, class Allocator>
Howard Hinnanta6119a82011-05-29 19:57:12 +0000386bool operator>=(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000387
388template<class charT, class traits, class Allocator>
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000389void swap(basic_string<charT, traits, Allocator>& lhs,
Howard Hinnant53f7d4c2011-06-03 18:40:47 +0000390 basic_string<charT, traits, Allocator>& rhs)
391 noexcept(noexcept(lhs.swap(rhs)));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000392
393template<class charT, class traits, class Allocator>
394basic_istream<charT, traits>&
395operator>>(basic_istream<charT, traits>& is, basic_string<charT, traits, Allocator>& str);
396
397template<class charT, class traits, class Allocator>
398basic_ostream<charT, traits>&
399operator<<(basic_ostream<charT, traits>& os, const basic_string<charT, traits, Allocator>& str);
400
401template<class charT, class traits, class Allocator>
Howard Hinnant324bb032010-08-22 00:02:43 +0000402basic_istream<charT, traits>&
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000403getline(basic_istream<charT, traits>& is, basic_string<charT, traits, Allocator>& str,
404 charT delim);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000405
406template<class charT, class traits, class Allocator>
407basic_istream<charT, traits>&
408getline(basic_istream<charT, traits>& is, basic_string<charT, traits, Allocator>& str);
409
410typedef basic_string<char> string;
411typedef basic_string<wchar_t> wstring;
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000412typedef basic_string<char16_t> u16string;
413typedef basic_string<char32_t> u32string;
414
415int stoi (const string& str, size_t* idx = 0, int base = 10);
416long stol (const string& str, size_t* idx = 0, int base = 10);
417unsigned long stoul (const string& str, size_t* idx = 0, int base = 10);
418long long stoll (const string& str, size_t* idx = 0, int base = 10);
419unsigned long long stoull(const string& str, size_t* idx = 0, int base = 10);
420
421float stof (const string& str, size_t* idx = 0);
422double stod (const string& str, size_t* idx = 0);
423long double stold(const string& str, size_t* idx = 0);
424
425string to_string(int val);
426string to_string(unsigned val);
427string to_string(long val);
428string to_string(unsigned long val);
429string to_string(long long val);
430string to_string(unsigned long long val);
431string to_string(float val);
432string to_string(double val);
433string to_string(long double val);
434
435int stoi (const wstring& str, size_t* idx = 0, int base = 10);
436long stol (const wstring& str, size_t* idx = 0, int base = 10);
437unsigned long stoul (const wstring& str, size_t* idx = 0, int base = 10);
438long long stoll (const wstring& str, size_t* idx = 0, int base = 10);
439unsigned long long stoull(const wstring& str, size_t* idx = 0, int base = 10);
440
441float stof (const wstring& str, size_t* idx = 0);
442double stod (const wstring& str, size_t* idx = 0);
443long double stold(const wstring& str, size_t* idx = 0);
444
445wstring to_wstring(int val);
446wstring to_wstring(unsigned val);
447wstring to_wstring(long val);
448wstring to_wstring(unsigned long val);
449wstring to_wstring(long long val);
450wstring to_wstring(unsigned long long val);
451wstring to_wstring(float val);
452wstring to_wstring(double val);
453wstring to_wstring(long double val);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000454
455template <> struct hash<string>;
456template <> struct hash<u16string>;
457template <> struct hash<u32string>;
458template <> struct hash<wstring>;
459
Marshall Clow15234322013-07-23 17:05:24 +0000460basic_string<char> operator "" s( const char *str, size_t len ); // C++14
461basic_string<wchar_t> operator "" s( const wchar_t *str, size_t len ); // C++14
462basic_string<char16_t> operator "" s( const char16_t *str, size_t len ); // C++14
463basic_string<char32_t> operator "" s( const char32_t *str, size_t len ); // C++14
464
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000465} // std
466
467*/
468
469#include <__config>
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000470#include <string_view>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000471#include <iosfwd>
472#include <cstring>
Howard Hinnantadff4892010-05-24 17:49:41 +0000473#include <cstdio> // For EOF.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000474#include <cwchar>
475#include <algorithm>
476#include <iterator>
477#include <utility>
478#include <memory>
479#include <stdexcept>
480#include <type_traits>
481#include <initializer_list>
482#include <__functional_base>
483#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
484#include <cstdint>
485#endif
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000486
Howard Hinnant66c6f972011-11-29 16:45:27 +0000487#include <__undef_min_max>
488
Eric Fiselierb9536102014-08-10 23:53:08 +0000489#include <__debug>
490
Howard Hinnant08e17472011-10-17 20:05:10 +0000491#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000492#pragma GCC system_header
Howard Hinnant08e17472011-10-17 20:05:10 +0000493#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000494
495_LIBCPP_BEGIN_NAMESPACE_STD
496
497// fpos
498
499template <class _StateT>
Eric Fiselierc3589a82017-01-04 23:56:00 +0000500class _LIBCPP_TEMPLATE_VIS fpos
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000501{
502private:
503 _StateT __st_;
504 streamoff __off_;
505public:
506 _LIBCPP_INLINE_VISIBILITY fpos(streamoff __off = streamoff()) : __st_(), __off_(__off) {}
507
508 _LIBCPP_INLINE_VISIBILITY operator streamoff() const {return __off_;}
509
510 _LIBCPP_INLINE_VISIBILITY _StateT state() const {return __st_;}
511 _LIBCPP_INLINE_VISIBILITY void state(_StateT __st) {__st_ = __st;}
512
513 _LIBCPP_INLINE_VISIBILITY fpos& operator+=(streamoff __off) {__off_ += __off; return *this;}
514 _LIBCPP_INLINE_VISIBILITY fpos operator+ (streamoff __off) const {fpos __t(*this); __t += __off; return __t;}
515 _LIBCPP_INLINE_VISIBILITY fpos& operator-=(streamoff __off) {__off_ -= __off; return *this;}
516 _LIBCPP_INLINE_VISIBILITY fpos operator- (streamoff __off) const {fpos __t(*this); __t -= __off; return __t;}
517};
518
519template <class _StateT>
520inline _LIBCPP_INLINE_VISIBILITY
521streamoff operator-(const fpos<_StateT>& __x, const fpos<_StateT>& __y)
522 {return streamoff(__x) - streamoff(__y);}
523
524template <class _StateT>
525inline _LIBCPP_INLINE_VISIBILITY
526bool operator==(const fpos<_StateT>& __x, const fpos<_StateT>& __y)
527 {return streamoff(__x) == streamoff(__y);}
528
529template <class _StateT>
530inline _LIBCPP_INLINE_VISIBILITY
531bool operator!=(const fpos<_StateT>& __x, const fpos<_StateT>& __y)
532 {return streamoff(__x) != streamoff(__y);}
533
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000534// basic_string
535
536template<class _CharT, class _Traits, class _Allocator>
537basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant2b1b2d42011-06-14 19:58:17 +0000538operator+(const basic_string<_CharT, _Traits, _Allocator>& __x,
539 const basic_string<_CharT, _Traits, _Allocator>& __y);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000540
541template<class _CharT, class _Traits, class _Allocator>
542basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant2b1b2d42011-06-14 19:58:17 +0000543operator+(const _CharT* __x, const basic_string<_CharT,_Traits,_Allocator>& __y);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000544
545template<class _CharT, class _Traits, class _Allocator>
546basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant2b1b2d42011-06-14 19:58:17 +0000547operator+(_CharT __x, const basic_string<_CharT,_Traits,_Allocator>& __y);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000548
549template<class _CharT, class _Traits, class _Allocator>
550basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant2b1b2d42011-06-14 19:58:17 +0000551operator+(const basic_string<_CharT, _Traits, _Allocator>& __x, const _CharT* __y);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000552
553template<class _CharT, class _Traits, class _Allocator>
554basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant2b1b2d42011-06-14 19:58:17 +0000555operator+(const basic_string<_CharT, _Traits, _Allocator>& __x, _CharT __y);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000556
557template <bool>
Eric Fiselierc3589a82017-01-04 23:56:00 +0000558class _LIBCPP_TEMPLATE_VIS __basic_string_common
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000559{
560protected:
Marshall Clow14c09a22016-08-25 15:09:01 +0000561 _LIBCPP_NORETURN void __throw_length_error() const;
562 _LIBCPP_NORETURN void __throw_out_of_range() const;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000563};
564
565template <bool __b>
566void
567__basic_string_common<__b>::__throw_length_error() const
568{
Marshall Clow14c09a22016-08-25 15:09:01 +0000569 _VSTD::__throw_length_error("basic_string");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000570}
571
572template <bool __b>
573void
574__basic_string_common<__b>::__throw_out_of_range() const
575{
Marshall Clow14c09a22016-08-25 15:09:01 +0000576 _VSTD::__throw_out_of_range("basic_string");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000577}
578
Howard Hinnante9df0a52013-08-01 18:17:34 +0000579#ifdef _LIBCPP_MSVC
Howard Hinnant78b68282011-10-22 20:59:45 +0000580#pragma warning( push )
581#pragma warning( disable: 4231 )
Howard Hinnante9df0a52013-08-01 18:17:34 +0000582#endif // _LIBCPP_MSVC
Eric Fiselier833d6442016-09-15 22:27:07 +0000583_LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS __basic_string_common<true>)
Howard Hinnante9df0a52013-08-01 18:17:34 +0000584#ifdef _LIBCPP_MSVC
Howard Hinnant78b68282011-10-22 20:59:45 +0000585#pragma warning( pop )
Howard Hinnante9df0a52013-08-01 18:17:34 +0000586#endif // _LIBCPP_MSVC
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000587
Marshall Clowdf9db312016-01-13 21:54:34 +0000588#ifdef _LIBCPP_NO_EXCEPTIONS
589template <class _Iter>
590struct __libcpp_string_gets_noexcept_iterator_impl : public true_type {};
591#elif defined(_LIBCPP_HAS_NO_NOEXCEPT)
592template <class _Iter>
593struct __libcpp_string_gets_noexcept_iterator_impl : public false_type {};
594#else
595template <class _Iter, bool = __is_forward_iterator<_Iter>::value>
596struct __libcpp_string_gets_noexcept_iterator_impl : public _LIBCPP_BOOL_CONSTANT((
597 noexcept(++(declval<_Iter&>())) &&
598 is_nothrow_assignable<_Iter&, _Iter>::value &&
599 noexcept(declval<_Iter>() == declval<_Iter>()) &&
600 noexcept(*declval<_Iter>())
601)) {};
602
603template <class _Iter>
604struct __libcpp_string_gets_noexcept_iterator_impl<_Iter, false> : public false_type {};
605#endif
606
607
608template <class _Iter>
609struct __libcpp_string_gets_noexcept_iterator
610 : public _LIBCPP_BOOL_CONSTANT(__libcpp_is_trivial_iterator<_Iter>::value || __libcpp_string_gets_noexcept_iterator_impl<_Iter>::value) {};
611
Marshall Clow6ac8de02016-09-24 22:45:42 +0000612template <class _CharT, class _Traits, class _Tp>
613struct __can_be_converted_to_string_view : public _LIBCPP_BOOL_CONSTANT(
614 ( is_convertible<const _Tp&, basic_string_view<_CharT, _Traits> >::value &&
615 !is_convertible<const _Tp&, const _CharT*>::value)) {};
616
Evgeniy Stepanov4f01aa82015-10-13 23:48:28 +0000617#ifdef _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant15467182013-04-30 21:44:48 +0000618
619template <class _CharT, size_t = sizeof(_CharT)>
620struct __padding
621{
622 unsigned char __xx[sizeof(_CharT)-1];
623};
624
625template <class _CharT>
626struct __padding<_CharT, 1>
627{
628};
629
Evgeniy Stepanov4f01aa82015-10-13 23:48:28 +0000630#endif // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant15467182013-04-30 21:44:48 +0000631
Howard Hinnant324bb032010-08-22 00:02:43 +0000632template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierc3589a82017-01-04 23:56:00 +0000633class _LIBCPP_TEMPLATE_VIS basic_string
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000634 : private __basic_string_common<true>
635{
636public:
637 typedef basic_string __self;
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000638 typedef basic_string_view<_CharT, _Traits> __self_view;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000639 typedef _Traits traits_type;
Marshall Clow2d4c3fa2017-03-15 18:41:11 +0000640 typedef _CharT value_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000641 typedef _Allocator allocator_type;
Howard Hinnante32b5e22010-11-17 17:55:08 +0000642 typedef allocator_traits<allocator_type> __alloc_traits;
643 typedef typename __alloc_traits::size_type size_type;
644 typedef typename __alloc_traits::difference_type difference_type;
Howard Hinnant53f7d4c2011-06-03 18:40:47 +0000645 typedef value_type& reference;
646 typedef const value_type& const_reference;
Howard Hinnante32b5e22010-11-17 17:55:08 +0000647 typedef typename __alloc_traits::pointer pointer;
648 typedef typename __alloc_traits::const_pointer const_pointer;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000649
Howard Hinnant499cea12013-08-23 17:37:05 +0000650 static_assert(is_pod<value_type>::value, "Character type of basic_string must be a POD");
Marshall Clow2d4c3fa2017-03-15 18:41:11 +0000651 static_assert((is_same<_CharT, typename traits_type::char_type>::value),
Howard Hinnant499cea12013-08-23 17:37:05 +0000652 "traits_type::char_type must be the same type as CharT");
653 static_assert((is_same<typename allocator_type::value_type, value_type>::value),
654 "Allocator::value_type must be same type as value_type");
655#if defined(_LIBCPP_RAW_ITERATORS)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000656 typedef pointer iterator;
657 typedef const_pointer const_iterator;
Howard Hinnant324bb032010-08-22 00:02:43 +0000658#else // defined(_LIBCPP_RAW_ITERATORS)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000659 typedef __wrap_iter<pointer> iterator;
660 typedef __wrap_iter<const_pointer> const_iterator;
Howard Hinnant324bb032010-08-22 00:02:43 +0000661#endif // defined(_LIBCPP_RAW_ITERATORS)
Howard Hinnant0949eed2011-06-30 21:18:19 +0000662 typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
663 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000664
665private:
Howard Hinnant15467182013-04-30 21:44:48 +0000666
Evgeniy Stepanov4f01aa82015-10-13 23:48:28 +0000667#ifdef _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant15467182013-04-30 21:44:48 +0000668
669 struct __long
670 {
671 pointer __data_;
672 size_type __size_;
673 size_type __cap_;
674 };
675
676#if _LIBCPP_BIG_ENDIAN
677 enum {__short_mask = 0x01};
678 enum {__long_mask = 0x1ul};
679#else // _LIBCPP_BIG_ENDIAN
680 enum {__short_mask = 0x80};
681 enum {__long_mask = ~(size_type(~0) >> 1)};
682#endif // _LIBCPP_BIG_ENDIAN
683
684 enum {__min_cap = (sizeof(__long) - 1)/sizeof(value_type) > 2 ?
685 (sizeof(__long) - 1)/sizeof(value_type) : 2};
686
687 struct __short
688 {
689 value_type __data_[__min_cap];
690 struct
691 : __padding<value_type>
692 {
693 unsigned char __size_;
694 };
695 };
696
697#else
698
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000699 struct __long
700 {
701 size_type __cap_;
702 size_type __size_;
703 pointer __data_;
704 };
705
706#if _LIBCPP_BIG_ENDIAN
707 enum {__short_mask = 0x80};
708 enum {__long_mask = ~(size_type(~0) >> 1)};
Howard Hinnant324bb032010-08-22 00:02:43 +0000709#else // _LIBCPP_BIG_ENDIAN
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000710 enum {__short_mask = 0x01};
Howard Hinnantec3773c2011-12-01 20:21:04 +0000711 enum {__long_mask = 0x1ul};
Howard Hinnant324bb032010-08-22 00:02:43 +0000712#endif // _LIBCPP_BIG_ENDIAN
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000713
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000714 enum {__min_cap = (sizeof(__long) - 1)/sizeof(value_type) > 2 ?
715 (sizeof(__long) - 1)/sizeof(value_type) : 2};
716
717 struct __short
718 {
719 union
720 {
721 unsigned char __size_;
Howard Hinnant9c0df142012-10-30 19:06:59 +0000722 value_type __lx;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000723 };
724 value_type __data_[__min_cap];
725 };
726
Evgeniy Stepanov4f01aa82015-10-13 23:48:28 +0000727#endif // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant15467182013-04-30 21:44:48 +0000728
Howard Hinnant499cea12013-08-23 17:37:05 +0000729 union __ulx{__long __lx; __short __lxx;};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000730
Howard Hinnant499cea12013-08-23 17:37:05 +0000731 enum {__n_words = sizeof(__ulx) / sizeof(size_type)};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000732
733 struct __raw
734 {
735 size_type __words[__n_words];
736 };
737
738 struct __rep
739 {
740 union
741 {
742 __long __l;
743 __short __s;
744 __raw __r;
745 };
746 };
747
748 __compressed_pair<__rep, allocator_type> __r_;
749
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000750public:
751 static const size_type npos = -1;
752
Howard Hinnant53f7d4c2011-06-03 18:40:47 +0000753 _LIBCPP_INLINE_VISIBILITY basic_string()
754 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
Marshall Clow7b193f72015-06-03 19:56:43 +0000755
756 _LIBCPP_INLINE_VISIBILITY explicit basic_string(const allocator_type& __a)
757#if _LIBCPP_STD_VER <= 14
758 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value);
759#else
760 _NOEXCEPT;
761#endif
762
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000763 basic_string(const basic_string& __str);
764 basic_string(const basic_string& __str, const allocator_type& __a);
Marshall Clow7b193f72015-06-03 19:56:43 +0000765
Eric Fiselier3e928972017-04-19 00:28:44 +0000766#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant9f193f22011-01-26 00:06:59 +0000767 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant53f7d4c2011-06-03 18:40:47 +0000768 basic_string(basic_string&& __str)
Marshall Clow7b193f72015-06-03 19:56:43 +0000769#if _LIBCPP_STD_VER <= 14
Howard Hinnant53f7d4c2011-06-03 18:40:47 +0000770 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
Marshall Clow7b193f72015-06-03 19:56:43 +0000771#else
772 _NOEXCEPT;
773#endif
774
Howard Hinnant9f193f22011-01-26 00:06:59 +0000775 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000776 basic_string(basic_string&& __str, const allocator_type& __a);
Eric Fiselier3e928972017-04-19 00:28:44 +0000777#endif // _LIBCPP_CXX03_LANG
Eric Fiselier0eaf2e82017-02-17 01:17:10 +0000778 _LIBCPP_INLINE_VISIBILITY basic_string(const _CharT* __s);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000779 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier0eaf2e82017-02-17 01:17:10 +0000780 basic_string(const _CharT* __s, const _Allocator& __a);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000781 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier0eaf2e82017-02-17 01:17:10 +0000782 basic_string(const _CharT* __s, size_type __n);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000783 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier0eaf2e82017-02-17 01:17:10 +0000784 basic_string(const _CharT* __s, size_type __n, const _Allocator& __a);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000785 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier0eaf2e82017-02-17 01:17:10 +0000786 basic_string(size_type __n, _CharT __c);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000787 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier0eaf2e82017-02-17 01:17:10 +0000788 basic_string(size_type __n, _CharT __c, const _Allocator& __a);
Marshall Clowf4f7d8f2016-04-07 18:13:41 +0000789 basic_string(const basic_string& __str, size_type __pos, size_type __n,
Eric Fiselier0eaf2e82017-02-17 01:17:10 +0000790 const _Allocator& __a = _Allocator());
Marshall Clowf4f7d8f2016-04-07 18:13:41 +0000791 _LIBCPP_INLINE_VISIBILITY
792 basic_string(const basic_string& __str, size_type __pos,
Eric Fiselier0eaf2e82017-02-17 01:17:10 +0000793 const _Allocator& __a = _Allocator());
Marshall Clowdb7fa112016-11-14 18:22:19 +0000794 template<class _Tp>
Shoaib Meenai24e8dbd2017-03-02 03:02:50 +0000795 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselier0eaf2e82017-02-17 01:17:10 +0000796 basic_string(const _Tp& __t, size_type __pos, size_type __n,
Marshall Clowdb7fa112016-11-14 18:22:19 +0000797 const allocator_type& __a = allocator_type(),
798 typename enable_if<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value, void>::type* = 0);
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000799 _LIBCPP_INLINE_VISIBILITY explicit
800 basic_string(__self_view __sv);
801 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier0eaf2e82017-02-17 01:17:10 +0000802 basic_string(__self_view __sv, const _Allocator& __a);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000803 template<class _InputIterator>
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000804 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000805 basic_string(_InputIterator __first, _InputIterator __last);
806 template<class _InputIterator>
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000807 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000808 basic_string(_InputIterator __first, _InputIterator __last, const allocator_type& __a);
Eric Fiselier3e928972017-04-19 00:28:44 +0000809#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000810 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier0eaf2e82017-02-17 01:17:10 +0000811 basic_string(initializer_list<_CharT> __il);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000812 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier0eaf2e82017-02-17 01:17:10 +0000813 basic_string(initializer_list<_CharT> __il, const _Allocator& __a);
Eric Fiselier3e928972017-04-19 00:28:44 +0000814#endif // _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000815
Eric Fiselier51eb1d52016-10-31 03:42:50 +0000816 inline ~basic_string();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000817
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000818 _LIBCPP_INLINE_VISIBILITY
819 operator __self_view() const _NOEXCEPT { return __self_view(data(), size()); }
820
Howard Hinnante32b5e22010-11-17 17:55:08 +0000821 basic_string& operator=(const basic_string& __str);
Eric Fiselierf472d6c2017-01-23 21:24:58 +0000822
823#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier37b2be92017-01-17 22:10:32 +0000824 template <class = void>
Eric Fiselierf472d6c2017-01-23 21:24:58 +0000825#endif
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000826 _LIBCPP_INLINE_VISIBILITY
827 basic_string& operator=(__self_view __sv) {return assign(__sv);}
Eric Fiselier3e928972017-04-19 00:28:44 +0000828#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000829 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant53f7d4c2011-06-03 18:40:47 +0000830 basic_string& operator=(basic_string&& __str)
Marshall Clowaf961ed2015-08-18 18:57:00 +0000831 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value));
Eric Fiselier3e928972017-04-19 00:28:44 +0000832 _LIBCPP_INLINE_VISIBILITY
833 basic_string& operator=(initializer_list<value_type> __il) {return assign(__il.begin(), __il.size());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000834#endif
Howard Hinnant9dcdcde2013-06-28 16:59:19 +0000835 _LIBCPP_INLINE_VISIBILITY basic_string& operator=(const value_type* __s) {return assign(__s);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000836 basic_string& operator=(value_type __c);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000837
Howard Hinnant499cea12013-08-23 17:37:05 +0000838#if _LIBCPP_DEBUG_LEVEL >= 2
839 _LIBCPP_INLINE_VISIBILITY
840 iterator begin() _NOEXCEPT
841 {return iterator(this, __get_pointer());}
842 _LIBCPP_INLINE_VISIBILITY
843 const_iterator begin() const _NOEXCEPT
844 {return const_iterator(this, __get_pointer());}
845 _LIBCPP_INLINE_VISIBILITY
846 iterator end() _NOEXCEPT
847 {return iterator(this, __get_pointer() + size());}
848 _LIBCPP_INLINE_VISIBILITY
849 const_iterator end() const _NOEXCEPT
850 {return const_iterator(this, __get_pointer() + size());}
851#else
Howard Hinnanta6119a82011-05-29 19:57:12 +0000852 _LIBCPP_INLINE_VISIBILITY
853 iterator begin() _NOEXCEPT
854 {return iterator(__get_pointer());}
855 _LIBCPP_INLINE_VISIBILITY
856 const_iterator begin() const _NOEXCEPT
Howard Hinnant9dcdcde2013-06-28 16:59:19 +0000857 {return const_iterator(__get_pointer());}
Howard Hinnanta6119a82011-05-29 19:57:12 +0000858 _LIBCPP_INLINE_VISIBILITY
859 iterator end() _NOEXCEPT
860 {return iterator(__get_pointer() + size());}
861 _LIBCPP_INLINE_VISIBILITY
862 const_iterator end() const _NOEXCEPT
Howard Hinnant9dcdcde2013-06-28 16:59:19 +0000863 {return const_iterator(__get_pointer() + size());}
Howard Hinnant499cea12013-08-23 17:37:05 +0000864#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnanta6119a82011-05-29 19:57:12 +0000865 _LIBCPP_INLINE_VISIBILITY
866 reverse_iterator rbegin() _NOEXCEPT
867 {return reverse_iterator(end());}
868 _LIBCPP_INLINE_VISIBILITY
869 const_reverse_iterator rbegin() const _NOEXCEPT
870 {return const_reverse_iterator(end());}
871 _LIBCPP_INLINE_VISIBILITY
872 reverse_iterator rend() _NOEXCEPT
873 {return reverse_iterator(begin());}
874 _LIBCPP_INLINE_VISIBILITY
875 const_reverse_iterator rend() const _NOEXCEPT
876 {return const_reverse_iterator(begin());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000877
Howard Hinnanta6119a82011-05-29 19:57:12 +0000878 _LIBCPP_INLINE_VISIBILITY
879 const_iterator cbegin() const _NOEXCEPT
880 {return begin();}
881 _LIBCPP_INLINE_VISIBILITY
882 const_iterator cend() const _NOEXCEPT
883 {return end();}
884 _LIBCPP_INLINE_VISIBILITY
885 const_reverse_iterator crbegin() const _NOEXCEPT
886 {return rbegin();}
887 _LIBCPP_INLINE_VISIBILITY
888 const_reverse_iterator crend() const _NOEXCEPT
889 {return rend();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000890
Howard Hinnanta6119a82011-05-29 19:57:12 +0000891 _LIBCPP_INLINE_VISIBILITY size_type size() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000892 {return __is_long() ? __get_long_size() : __get_short_size();}
Howard Hinnanta6119a82011-05-29 19:57:12 +0000893 _LIBCPP_INLINE_VISIBILITY size_type length() const _NOEXCEPT {return size();}
894 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT;
895 _LIBCPP_INLINE_VISIBILITY size_type capacity() const _NOEXCEPT
Eric Fiseliere2d48922015-08-28 07:02:42 +0000896 {return (__is_long() ? __get_long_cap()
897 : static_cast<size_type>(__min_cap)) - 1;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000898
899 void resize(size_type __n, value_type __c);
900 _LIBCPP_INLINE_VISIBILITY void resize(size_type __n) {resize(__n, value_type());}
901
902 void reserve(size_type res_arg = 0);
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000903 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant53f7d4c2011-06-03 18:40:47 +0000904 void shrink_to_fit() _NOEXCEPT {reserve();}
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000905 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta6119a82011-05-29 19:57:12 +0000906 void clear() _NOEXCEPT;
907 _LIBCPP_INLINE_VISIBILITY bool empty() const _NOEXCEPT {return size() == 0;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000908
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000909 _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_type __pos) const _NOEXCEPT;
910 _LIBCPP_INLINE_VISIBILITY reference operator[](size_type __pos) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000911
912 const_reference at(size_type __n) const;
913 reference at(size_type __n);
914
915 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(const basic_string& __str) {return append(__str);}
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000916 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(__self_view __sv) {return append(__sv);}
917 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(const value_type* __s) {return append(__s);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000918 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(value_type __c) {push_back(__c); return *this;}
Eric Fiselier3e928972017-04-19 00:28:44 +0000919#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000920 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(initializer_list<value_type> __il) {return append(__il);}
Eric Fiselier3e928972017-04-19 00:28:44 +0000921#endif // _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000922
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000923 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000924 basic_string& append(const basic_string& __str);
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000925 _LIBCPP_INLINE_VISIBILITY
926 basic_string& append(__self_view __sv) { return append(__sv.data(), __sv.size()); }
Marshall Clowa93b5e22014-03-04 19:17:19 +0000927 basic_string& append(const basic_string& __str, size_type __pos, size_type __n=npos);
Marshall Clow42a87db2016-10-03 23:40:48 +0000928 template <class _Tp>
Shoaib Meenai24e8dbd2017-03-02 03:02:50 +0000929 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
930 typename enable_if
Marshall Clow6ac8de02016-09-24 22:45:42 +0000931 <
932 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
933 basic_string&
934 >::type
935 append(const _Tp& __t, size_type __pos, size_type __n=npos);
Howard Hinnant9dcdcde2013-06-28 16:59:19 +0000936 basic_string& append(const value_type* __s, size_type __n);
937 basic_string& append(const value_type* __s);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000938 basic_string& append(size_type __n, value_type __c);
Eric Fiselier026d38e2016-10-31 02:46:25 +0000939 template <class _ForwardIterator>
Shoaib Meenai24e8dbd2017-03-02 03:02:50 +0000940 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
941 basic_string& __append_forward_unsafe(_ForwardIterator, _ForwardIterator);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000942 template<class _InputIterator>
Shoaib Meenai24e8dbd2017-03-02 03:02:50 +0000943 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
944 typename enable_if
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000945 <
Marshall Clowdf9db312016-01-13 21:54:34 +0000946 __is_exactly_input_iterator<_InputIterator>::value
947 || !__libcpp_string_gets_noexcept_iterator<_InputIterator>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000948 basic_string&
949 >::type
Eric Fiselier026d38e2016-10-31 02:46:25 +0000950 _LIBCPP_INLINE_VISIBILITY
951 append(_InputIterator __first, _InputIterator __last) {
952 const basic_string __temp (__first, __last, __alloc());
953 append(__temp.data(), __temp.size());
954 return *this;
955 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000956 template<class _ForwardIterator>
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_forward_iterator<_ForwardIterator>::value
961 && __libcpp_string_gets_noexcept_iterator<_ForwardIterator>::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(_ForwardIterator __first, _ForwardIterator __last) {
966 return __append_forward_unsafe(__first, __last);
967 }
968
Eric Fiselier3e928972017-04-19 00:28:44 +0000969#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000970 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000971 basic_string& append(initializer_list<value_type> __il) {return append(__il.begin(), __il.size());}
Eric Fiselier3e928972017-04-19 00:28:44 +0000972#endif // _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000973
974 void push_back(value_type __c);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000975 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000976 void pop_back();
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000977 _LIBCPP_INLINE_VISIBILITY reference front();
978 _LIBCPP_INLINE_VISIBILITY const_reference front() const;
979 _LIBCPP_INLINE_VISIBILITY reference back();
980 _LIBCPP_INLINE_VISIBILITY const_reference back() const;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000981
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000982 _LIBCPP_INLINE_VISIBILITY
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000983 basic_string& assign(__self_view __sv) { return assign(__sv.data(), __sv.size()); }
984 _LIBCPP_INLINE_VISIBILITY
Marshall Clowf40ec902016-03-09 18:08:29 +0000985 basic_string& assign(const basic_string& __str) { return *this = __str; }
Eric Fiselier3e928972017-04-19 00:28:44 +0000986#ifndef _LIBCPP_CXX03_LANG
Howard Hinnanta6119a82011-05-29 19:57:12 +0000987 _LIBCPP_INLINE_VISIBILITY
988 basic_string& assign(basic_string&& str)
Marshall Clow7ed093b2015-10-05 16:17:34 +0000989 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
Howard Hinnant0949eed2011-06-30 21:18:19 +0000990 {*this = _VSTD::move(str); return *this;}
Howard Hinnanta6119a82011-05-29 19:57:12 +0000991#endif
Marshall Clowa93b5e22014-03-04 19:17:19 +0000992 basic_string& assign(const basic_string& __str, size_type __pos, size_type __n=npos);
Marshall Clow42a87db2016-10-03 23:40:48 +0000993 template <class _Tp>
Shoaib Meenai24e8dbd2017-03-02 03:02:50 +0000994 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
995 typename enable_if
Marshall Clow6ac8de02016-09-24 22:45:42 +0000996 <
997 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
998 basic_string&
999 >::type
1000 assign(const _Tp & __t, size_type pos, size_type n=npos);
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001001 basic_string& assign(const value_type* __s, size_type __n);
1002 basic_string& assign(const value_type* __s);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001003 basic_string& assign(size_type __n, value_type __c);
1004 template<class _InputIterator>
Shoaib Meenai24e8dbd2017-03-02 03:02:50 +00001005 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
1006 typename enable_if
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001007 <
Marshall Clowdf9db312016-01-13 21:54:34 +00001008 __is_exactly_input_iterator<_InputIterator>::value
1009 || !__libcpp_string_gets_noexcept_iterator<_InputIterator>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001010 basic_string&
1011 >::type
1012 assign(_InputIterator __first, _InputIterator __last);
1013 template<class _ForwardIterator>
Shoaib Meenai24e8dbd2017-03-02 03:02:50 +00001014 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
1015 typename enable_if
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001016 <
Marshall Clowdf9db312016-01-13 21:54:34 +00001017 __is_forward_iterator<_ForwardIterator>::value
1018 && __libcpp_string_gets_noexcept_iterator<_ForwardIterator>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001019 basic_string&
1020 >::type
1021 assign(_ForwardIterator __first, _ForwardIterator __last);
Eric Fiselier3e928972017-04-19 00:28:44 +00001022#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant8d7a9552010-09-23 17:31:07 +00001023 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001024 basic_string& assign(initializer_list<value_type> __il) {return assign(__il.begin(), __il.size());}
Eric Fiselier3e928972017-04-19 00:28:44 +00001025#endif // _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001026
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001027 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001028 basic_string& insert(size_type __pos1, const basic_string& __str);
Marshall Clow1e00d6d2016-07-21 05:31:24 +00001029 _LIBCPP_INLINE_VISIBILITY
1030 basic_string& insert(size_type __pos1, __self_view __sv) { return insert(__pos1, __sv.data(), __sv.size()); }
Marshall Clow6ac8de02016-09-24 22:45:42 +00001031 template <class _Tp>
Shoaib Meenai24e8dbd2017-03-02 03:02:50 +00001032 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
1033 typename enable_if
Marshall Clow6ac8de02016-09-24 22:45:42 +00001034 <
1035 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1036 basic_string&
1037 >::type
1038 insert(size_type __pos1, const _Tp& __t, size_type __pos2, size_type __n=npos);
Marshall Clowa93b5e22014-03-04 19:17:19 +00001039 basic_string& insert(size_type __pos1, const basic_string& __str, size_type __pos2, size_type __n=npos);
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001040 basic_string& insert(size_type __pos, const value_type* __s, size_type __n);
1041 basic_string& insert(size_type __pos, const value_type* __s);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001042 basic_string& insert(size_type __pos, size_type __n, value_type __c);
1043 iterator insert(const_iterator __pos, value_type __c);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001044 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001045 iterator insert(const_iterator __pos, size_type __n, value_type __c);
1046 template<class _InputIterator>
Shoaib Meenai24e8dbd2017-03-02 03:02:50 +00001047 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
1048 typename enable_if
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001049 <
Marshall Clowdf9db312016-01-13 21:54:34 +00001050 __is_exactly_input_iterator<_InputIterator>::value
1051 || !__libcpp_string_gets_noexcept_iterator<_InputIterator>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001052 iterator
1053 >::type
1054 insert(const_iterator __pos, _InputIterator __first, _InputIterator __last);
1055 template<class _ForwardIterator>
Shoaib Meenai24e8dbd2017-03-02 03:02:50 +00001056 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
1057 typename enable_if
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001058 <
Marshall Clowdf9db312016-01-13 21:54:34 +00001059 __is_forward_iterator<_ForwardIterator>::value
1060 && __libcpp_string_gets_noexcept_iterator<_ForwardIterator>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001061 iterator
1062 >::type
1063 insert(const_iterator __pos, _ForwardIterator __first, _ForwardIterator __last);
Eric Fiselier3e928972017-04-19 00:28:44 +00001064#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant8d7a9552010-09-23 17:31:07 +00001065 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001066 iterator insert(const_iterator __pos, initializer_list<value_type> __il)
1067 {return insert(__pos, __il.begin(), __il.end());}
Eric Fiselier3e928972017-04-19 00:28:44 +00001068#endif // _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001069
1070 basic_string& erase(size_type __pos = 0, size_type __n = npos);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001071 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001072 iterator erase(const_iterator __pos);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001073 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001074 iterator erase(const_iterator __first, const_iterator __last);
1075
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001076 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001077 basic_string& replace(size_type __pos1, size_type __n1, const basic_string& __str);
Marshall Clow1e00d6d2016-07-21 05:31:24 +00001078 _LIBCPP_INLINE_VISIBILITY
1079 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 +00001080 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 +00001081 template <class _Tp>
Shoaib Meenai24e8dbd2017-03-02 03:02:50 +00001082 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
1083 typename enable_if
Marshall Clow6ac8de02016-09-24 22:45:42 +00001084 <
1085 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1086 basic_string&
1087 >::type
1088 replace(size_type __pos1, size_type __n1, const _Tp& __t, size_type __pos2, size_type __n2=npos);
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001089 basic_string& replace(size_type __pos, size_type __n1, const value_type* __s, size_type __n2);
1090 basic_string& replace(size_type __pos, size_type __n1, const value_type* __s);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001091 basic_string& replace(size_type __pos, size_type __n1, size_type __n2, value_type __c);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001092 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7b2cb482010-11-17 21:11:40 +00001093 basic_string& replace(const_iterator __i1, const_iterator __i2, const basic_string& __str);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001094 _LIBCPP_INLINE_VISIBILITY
Marshall Clow1e00d6d2016-07-21 05:31:24 +00001095 basic_string& replace(const_iterator __i1, const_iterator __i2, __self_view __sv) { return replace(__i1 - begin(), __i2 - __i1, __sv); }
1096 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001097 basic_string& replace(const_iterator __i1, const_iterator __i2, const value_type* __s, size_type __n);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001098 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001099 basic_string& replace(const_iterator __i1, const_iterator __i2, const value_type* __s);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001100 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7b2cb482010-11-17 21:11:40 +00001101 basic_string& replace(const_iterator __i1, const_iterator __i2, size_type __n, value_type __c);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001102 template<class _InputIterator>
Shoaib Meenai24e8dbd2017-03-02 03:02:50 +00001103 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
1104 typename enable_if
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001105 <
1106 __is_input_iterator<_InputIterator>::value,
1107 basic_string&
1108 >::type
Howard Hinnant7b2cb482010-11-17 21:11:40 +00001109 replace(const_iterator __i1, const_iterator __i2, _InputIterator __j1, _InputIterator __j2);
Eric Fiselier3e928972017-04-19 00:28:44 +00001110#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant8d7a9552010-09-23 17:31:07 +00001111 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7b2cb482010-11-17 21:11:40 +00001112 basic_string& replace(const_iterator __i1, const_iterator __i2, initializer_list<value_type> __il)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001113 {return replace(__i1, __i2, __il.begin(), __il.end());}
Eric Fiselier3e928972017-04-19 00:28:44 +00001114#endif // _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001115
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001116 size_type copy(value_type* __s, size_type __n, size_type __pos = 0) const;
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001117 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001118 basic_string substr(size_type __pos = 0, size_type __n = npos) const;
1119
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001120 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant53f7d4c2011-06-03 18:40:47 +00001121 void swap(basic_string& __str)
Marshall Clow7d914d12015-07-13 20:04:56 +00001122#if _LIBCPP_STD_VER >= 14
Eric Fiselier47257c42016-12-28 05:53:01 +00001123 _NOEXCEPT_DEBUG;
Marshall Clow7d914d12015-07-13 20:04:56 +00001124#else
Eric Fiselier47257c42016-12-28 05:53:01 +00001125 _NOEXCEPT_DEBUG_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow7d914d12015-07-13 20:04:56 +00001126 __is_nothrow_swappable<allocator_type>::value);
1127#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001128
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001129 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001130 const value_type* c_str() const _NOEXCEPT {return data();}
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001131 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001132 const value_type* data() const _NOEXCEPT {return _VSTD::__to_raw_pointer(__get_pointer());}
Marshall Clowf532a702016-03-08 15:44:30 +00001133#if _LIBCPP_STD_VER > 14
1134 _LIBCPP_INLINE_VISIBILITY
1135 value_type* data() _NOEXCEPT {return _VSTD::__to_raw_pointer(__get_pointer());}
1136#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001137
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001138 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta6119a82011-05-29 19:57:12 +00001139 allocator_type get_allocator() const _NOEXCEPT {return __alloc();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001140
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001141 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta6119a82011-05-29 19:57:12 +00001142 size_type find(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
Marshall Clow1e00d6d2016-07-21 05:31:24 +00001143 _LIBCPP_INLINE_VISIBILITY
1144 size_type find(__self_view __sv, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001145 size_type find(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001146 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001147 size_type find(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnanta6119a82011-05-29 19:57:12 +00001148 size_type find(value_type __c, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001149
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001150 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta6119a82011-05-29 19:57:12 +00001151 size_type rfind(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
Marshall Clow1e00d6d2016-07-21 05:31:24 +00001152 _LIBCPP_INLINE_VISIBILITY
1153 size_type rfind(__self_view __sv, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001154 size_type rfind(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001155 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001156 size_type rfind(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnanta6119a82011-05-29 19:57:12 +00001157 size_type rfind(value_type __c, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001158
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001159 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta6119a82011-05-29 19:57:12 +00001160 size_type find_first_of(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
Marshall Clow1e00d6d2016-07-21 05:31:24 +00001161 _LIBCPP_INLINE_VISIBILITY
1162 size_type find_first_of(__self_view __sv, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001163 size_type find_first_of(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001164 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001165 size_type find_first_of(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001166 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta6119a82011-05-29 19:57:12 +00001167 size_type find_first_of(value_type __c, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001168
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001169 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta6119a82011-05-29 19:57:12 +00001170 size_type find_last_of(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
Marshall Clow1e00d6d2016-07-21 05:31:24 +00001171 _LIBCPP_INLINE_VISIBILITY
1172 size_type find_last_of(__self_view __sv, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001173 size_type find_last_of(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001174 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001175 size_type find_last_of(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001176 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta6119a82011-05-29 19:57:12 +00001177 size_type find_last_of(value_type __c, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001178
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001179 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta6119a82011-05-29 19:57:12 +00001180 size_type find_first_not_of(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
Marshall Clow1e00d6d2016-07-21 05:31:24 +00001181 _LIBCPP_INLINE_VISIBILITY
1182 size_type find_first_not_of(__self_view __sv, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001183 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 +00001184 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001185 size_type find_first_not_of(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnanta6119a82011-05-29 19:57:12 +00001186 _LIBCPP_INLINE_VISIBILITY
1187 size_type find_first_not_of(value_type __c, size_type __pos = 0) const _NOEXCEPT;
1188
1189 _LIBCPP_INLINE_VISIBILITY
1190 size_type find_last_not_of(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
Marshall Clow1e00d6d2016-07-21 05:31:24 +00001191 _LIBCPP_INLINE_VISIBILITY
1192 size_type find_last_not_of(__self_view __sv, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001193 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 +00001194 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001195 size_type find_last_not_of(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnanta6119a82011-05-29 19:57:12 +00001196 _LIBCPP_INLINE_VISIBILITY
1197 size_type find_last_not_of(value_type __c, size_type __pos = npos) const _NOEXCEPT;
1198
1199 _LIBCPP_INLINE_VISIBILITY
1200 int compare(const basic_string& __str) const _NOEXCEPT;
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001201 _LIBCPP_INLINE_VISIBILITY
Marshall Clow1e00d6d2016-07-21 05:31:24 +00001202 int compare(__self_view __sv) const _NOEXCEPT;
1203 _LIBCPP_INLINE_VISIBILITY
1204 int compare(size_type __pos1, size_type __n1, __self_view __sv) const;
1205 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001206 int compare(size_type __pos1, size_type __n1, const basic_string& __str) const;
Marshall Clowa93b5e22014-03-04 19:17:19 +00001207 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 +00001208 template <class _Tp>
Eric Fiselier9dbc0532016-10-14 05:29:46 +00001209 inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow6ac8de02016-09-24 22:45:42 +00001210 typename enable_if
1211 <
1212 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1213 int
1214 >::type
1215 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 +00001216 int compare(const value_type* __s) const _NOEXCEPT;
1217 int compare(size_type __pos1, size_type __n1, const value_type* __s) const;
1218 int compare(size_type __pos1, size_type __n1, const value_type* __s, size_type __n2) const;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001219
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001220 _LIBCPP_INLINE_VISIBILITY bool __invariants() const;
Howard Hinnant08dd2532013-04-22 23:55:13 +00001221
1222 _LIBCPP_INLINE_VISIBILITY
1223 bool __is_long() const _NOEXCEPT
1224 {return bool(__r_.first().__s.__size_ & __short_mask);}
1225
Howard Hinnant499cea12013-08-23 17:37:05 +00001226#if _LIBCPP_DEBUG_LEVEL >= 2
1227
1228 bool __dereferenceable(const const_iterator* __i) const;
1229 bool __decrementable(const const_iterator* __i) const;
1230 bool __addable(const const_iterator* __i, ptrdiff_t __n) const;
1231 bool __subscriptable(const const_iterator* __i, ptrdiff_t __n) const;
1232
1233#endif // _LIBCPP_DEBUG_LEVEL >= 2
1234
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001235private:
Howard Hinnanta6119a82011-05-29 19:57:12 +00001236 _LIBCPP_INLINE_VISIBILITY
1237 allocator_type& __alloc() _NOEXCEPT
1238 {return __r_.second();}
1239 _LIBCPP_INLINE_VISIBILITY
1240 const allocator_type& __alloc() const _NOEXCEPT
1241 {return __r_.second();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001242
Evgeniy Stepanov4f01aa82015-10-13 23:48:28 +00001243#ifdef _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant15467182013-04-30 21:44:48 +00001244
Howard Hinnanta6119a82011-05-29 19:57:12 +00001245 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta6119a82011-05-29 19:57:12 +00001246 void __set_short_size(size_type __s) _NOEXCEPT
Howard Hinnant15467182013-04-30 21:44:48 +00001247# if _LIBCPP_BIG_ENDIAN
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001248 {__r_.first().__s.__size_ = (unsigned char)(__s << 1);}
Howard Hinnant15467182013-04-30 21:44:48 +00001249# else
1250 {__r_.first().__s.__size_ = (unsigned char)(__s);}
1251# endif
1252
Howard Hinnanta6119a82011-05-29 19:57:12 +00001253 _LIBCPP_INLINE_VISIBILITY
1254 size_type __get_short_size() const _NOEXCEPT
Howard Hinnant15467182013-04-30 21:44:48 +00001255# if _LIBCPP_BIG_ENDIAN
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001256 {return __r_.first().__s.__size_ >> 1;}
Howard Hinnant15467182013-04-30 21:44:48 +00001257# else
1258 {return __r_.first().__s.__size_;}
1259# endif
1260
Evgeniy Stepanov4f01aa82015-10-13 23:48:28 +00001261#else // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant15467182013-04-30 21:44:48 +00001262
1263 _LIBCPP_INLINE_VISIBILITY
1264 void __set_short_size(size_type __s) _NOEXCEPT
1265# if _LIBCPP_BIG_ENDIAN
1266 {__r_.first().__s.__size_ = (unsigned char)(__s);}
1267# else
1268 {__r_.first().__s.__size_ = (unsigned char)(__s << 1);}
1269# endif
1270
1271 _LIBCPP_INLINE_VISIBILITY
1272 size_type __get_short_size() const _NOEXCEPT
1273# if _LIBCPP_BIG_ENDIAN
1274 {return __r_.first().__s.__size_;}
1275# else
1276 {return __r_.first().__s.__size_ >> 1;}
1277# endif
1278
Evgeniy Stepanov4f01aa82015-10-13 23:48:28 +00001279#endif // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant15467182013-04-30 21:44:48 +00001280
Howard Hinnanta6119a82011-05-29 19:57:12 +00001281 _LIBCPP_INLINE_VISIBILITY
1282 void __set_long_size(size_type __s) _NOEXCEPT
1283 {__r_.first().__l.__size_ = __s;}
1284 _LIBCPP_INLINE_VISIBILITY
1285 size_type __get_long_size() const _NOEXCEPT
1286 {return __r_.first().__l.__size_;}
1287 _LIBCPP_INLINE_VISIBILITY
1288 void __set_size(size_type __s) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001289 {if (__is_long()) __set_long_size(__s); else __set_short_size(__s);}
1290
Howard Hinnanta6119a82011-05-29 19:57:12 +00001291 _LIBCPP_INLINE_VISIBILITY
1292 void __set_long_cap(size_type __s) _NOEXCEPT
1293 {__r_.first().__l.__cap_ = __long_mask | __s;}
1294 _LIBCPP_INLINE_VISIBILITY
1295 size_type __get_long_cap() const _NOEXCEPT
Howard Hinnantec3773c2011-12-01 20:21:04 +00001296 {return __r_.first().__l.__cap_ & size_type(~__long_mask);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001297
Howard Hinnanta6119a82011-05-29 19:57:12 +00001298 _LIBCPP_INLINE_VISIBILITY
1299 void __set_long_pointer(pointer __p) _NOEXCEPT
1300 {__r_.first().__l.__data_ = __p;}
1301 _LIBCPP_INLINE_VISIBILITY
1302 pointer __get_long_pointer() _NOEXCEPT
1303 {return __r_.first().__l.__data_;}
1304 _LIBCPP_INLINE_VISIBILITY
1305 const_pointer __get_long_pointer() const _NOEXCEPT
1306 {return __r_.first().__l.__data_;}
1307 _LIBCPP_INLINE_VISIBILITY
1308 pointer __get_short_pointer() _NOEXCEPT
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001309 {return pointer_traits<pointer>::pointer_to(__r_.first().__s.__data_[0]);}
Howard Hinnanta6119a82011-05-29 19:57:12 +00001310 _LIBCPP_INLINE_VISIBILITY
1311 const_pointer __get_short_pointer() const _NOEXCEPT
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001312 {return pointer_traits<const_pointer>::pointer_to(__r_.first().__s.__data_[0]);}
Howard Hinnanta6119a82011-05-29 19:57:12 +00001313 _LIBCPP_INLINE_VISIBILITY
1314 pointer __get_pointer() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001315 {return __is_long() ? __get_long_pointer() : __get_short_pointer();}
Howard Hinnanta6119a82011-05-29 19:57:12 +00001316 _LIBCPP_INLINE_VISIBILITY
1317 const_pointer __get_pointer() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001318 {return __is_long() ? __get_long_pointer() : __get_short_pointer();}
1319
Howard Hinnanta6119a82011-05-29 19:57:12 +00001320 _LIBCPP_INLINE_VISIBILITY
1321 void __zero() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001322 {
1323 size_type (&__a)[__n_words] = __r_.first().__r.__words;
1324 for (unsigned __i = 0; __i < __n_words; ++__i)
1325 __a[__i] = 0;
1326 }
1327
1328 template <size_type __a> static
Howard Hinnanta6119a82011-05-29 19:57:12 +00001329 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7f764502013-08-14 18:00:20 +00001330 size_type __align_it(size_type __s) _NOEXCEPT
Eric Fiseliere2d48922015-08-28 07:02:42 +00001331 {return (__s + (__a-1)) & ~(__a-1);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001332 enum {__alignment = 16};
Howard Hinnanta6119a82011-05-29 19:57:12 +00001333 static _LIBCPP_INLINE_VISIBILITY
1334 size_type __recommend(size_type __s) _NOEXCEPT
Eric Fiseliere2d48922015-08-28 07:02:42 +00001335 {return (__s < __min_cap ? static_cast<size_type>(__min_cap) :
Howard Hinnant7f764502013-08-14 18:00:20 +00001336 __align_it<sizeof(value_type) < __alignment ?
Howard Hinnanta6119a82011-05-29 19:57:12 +00001337 __alignment/sizeof(value_type) : 1 > (__s+1)) - 1;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001338
Duncan P. N. Exon Smithed3c0e62017-04-01 03:20:48 +00001339 inline
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001340 void __init(const value_type* __s, size_type __sz, size_type __reserve);
Duncan P. N. Exon Smithed3c0e62017-04-01 03:20:48 +00001341 inline
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001342 void __init(const value_type* __s, size_type __sz);
Duncan P. N. Exon Smithed3c0e62017-04-01 03:20:48 +00001343 inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001344 void __init(size_type __n, value_type __c);
Howard Hinnant324bb032010-08-22 00:02:43 +00001345
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001346 template <class _InputIterator>
Duncan P. N. Exon Smithed3c0e62017-04-01 03:20:48 +00001347 inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001348 typename enable_if
1349 <
Marshall Clowdf9db312016-01-13 21:54:34 +00001350 __is_exactly_input_iterator<_InputIterator>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001351 void
1352 >::type
1353 __init(_InputIterator __first, _InputIterator __last);
1354
1355 template <class _ForwardIterator>
Duncan P. N. Exon Smithed3c0e62017-04-01 03:20:48 +00001356 inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001357 typename enable_if
1358 <
1359 __is_forward_iterator<_ForwardIterator>::value,
1360 void
1361 >::type
1362 __init(_ForwardIterator __first, _ForwardIterator __last);
1363
1364 void __grow_by(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
Howard Hinnant324bb032010-08-22 00:02:43 +00001365 size_type __n_copy, size_type __n_del, size_type __n_add = 0);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001366 void __grow_by_and_replace(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
1367 size_type __n_copy, size_type __n_del,
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001368 size_type __n_add, const value_type* __p_new_stuff);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001369
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001370 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001371 void __erase_to_end(size_type __pos);
1372
Howard Hinnante32b5e22010-11-17 17:55:08 +00001373 _LIBCPP_INLINE_VISIBILITY
1374 void __copy_assign_alloc(const basic_string& __str)
1375 {__copy_assign_alloc(__str, integral_constant<bool,
1376 __alloc_traits::propagate_on_container_copy_assignment::value>());}
1377
1378 _LIBCPP_INLINE_VISIBILITY
1379 void __copy_assign_alloc(const basic_string& __str, true_type)
1380 {
Marshall Clow9247fd22017-01-31 03:40:52 +00001381 if (__alloc() == __str.__alloc())
1382 __alloc() = __str.__alloc();
1383 else
Howard Hinnante32b5e22010-11-17 17:55:08 +00001384 {
Marshall Clow9247fd22017-01-31 03:40:52 +00001385 if (!__str.__is_long())
1386 {
1387 clear();
1388 shrink_to_fit();
1389 __alloc() = __str.__alloc();
1390 }
1391 else
1392 {
1393 allocator_type __a = __str.__alloc();
1394 pointer __p = __alloc_traits::allocate(__a, __str.__get_long_cap());
1395 clear();
1396 shrink_to_fit();
1397 __alloc() = _VSTD::move(__a);
1398 __set_long_pointer(__p);
1399 __set_long_cap(__str.__get_long_cap());
1400 __set_long_size(__str.size());
1401 }
Howard Hinnante32b5e22010-11-17 17:55:08 +00001402 }
Howard Hinnante32b5e22010-11-17 17:55:08 +00001403 }
1404
1405 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantec3773c2011-12-01 20:21:04 +00001406 void __copy_assign_alloc(const basic_string&, false_type) _NOEXCEPT
Howard Hinnante32b5e22010-11-17 17:55:08 +00001407 {}
1408
Eric Fiselier3e928972017-04-19 00:28:44 +00001409#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001410 _LIBCPP_INLINE_VISIBILITY
Marshall Clowaf961ed2015-08-18 18:57:00 +00001411 void __move_assign(basic_string& __str, false_type)
1412 _NOEXCEPT_(__alloc_traits::is_always_equal::value);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001413 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant53f7d4c2011-06-03 18:40:47 +00001414 void __move_assign(basic_string& __str, true_type)
Marshall Clowaf961ed2015-08-18 18:57:00 +00001415#if _LIBCPP_STD_VER > 14
1416 _NOEXCEPT;
1417#else
Howard Hinnant53f7d4c2011-06-03 18:40:47 +00001418 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnante32b5e22010-11-17 17:55:08 +00001419#endif
Marshall Clowaf961ed2015-08-18 18:57:00 +00001420#endif
Howard Hinnante32b5e22010-11-17 17:55:08 +00001421
1422 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3fdbbd22011-08-17 20:36:18 +00001423 void
Howard Hinnant9cbee432011-09-02 20:42:31 +00001424 __move_assign_alloc(basic_string& __str)
Howard Hinnant3fdbbd22011-08-17 20:36:18 +00001425 _NOEXCEPT_(
1426 !__alloc_traits::propagate_on_container_move_assignment::value ||
1427 is_nothrow_move_assignable<allocator_type>::value)
1428 {__move_assign_alloc(__str, integral_constant<bool,
1429 __alloc_traits::propagate_on_container_move_assignment::value>());}
1430
1431 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9cbee432011-09-02 20:42:31 +00001432 void __move_assign_alloc(basic_string& __c, true_type)
Howard Hinnant3fdbbd22011-08-17 20:36:18 +00001433 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
1434 {
1435 __alloc() = _VSTD::move(__c.__alloc());
1436 }
1437
1438 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantec3773c2011-12-01 20:21:04 +00001439 void __move_assign_alloc(basic_string&, false_type)
Howard Hinnant3fdbbd22011-08-17 20:36:18 +00001440 _NOEXCEPT
1441 {}
1442
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001443 _LIBCPP_INLINE_VISIBILITY void __invalidate_all_iterators();
1444 _LIBCPP_INLINE_VISIBILITY void __invalidate_iterators_past(size_type);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001445
1446 friend basic_string operator+<>(const basic_string&, const basic_string&);
1447 friend basic_string operator+<>(const value_type*, const basic_string&);
1448 friend basic_string operator+<>(value_type, const basic_string&);
1449 friend basic_string operator+<>(const basic_string&, const value_type*);
1450 friend basic_string operator+<>(const basic_string&, value_type);
1451};
1452
1453template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001454inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001455void
1456basic_string<_CharT, _Traits, _Allocator>::__invalidate_all_iterators()
1457{
Howard Hinnant499cea12013-08-23 17:37:05 +00001458#if _LIBCPP_DEBUG_LEVEL >= 2
1459 __get_db()->__invalidate_all(this);
1460#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001461}
1462
1463template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001464inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001465void
Howard Hinnantec3773c2011-12-01 20:21:04 +00001466basic_string<_CharT, _Traits, _Allocator>::__invalidate_iterators_past(size_type
Howard Hinnant499cea12013-08-23 17:37:05 +00001467#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantec3773c2011-12-01 20:21:04 +00001468 __pos
1469#endif
1470 )
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001471{
Howard Hinnant499cea12013-08-23 17:37:05 +00001472#if _LIBCPP_DEBUG_LEVEL >= 2
1473 __c_node* __c = __get_db()->__find_c_and_lock(this);
1474 if (__c)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001475 {
Howard Hinnant499cea12013-08-23 17:37:05 +00001476 const_pointer __new_last = __get_pointer() + __pos;
1477 for (__i_node** __p = __c->end_; __p != __c->beg_; )
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001478 {
Howard Hinnant499cea12013-08-23 17:37:05 +00001479 --__p;
1480 const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_);
1481 if (__i->base() > __new_last)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001482 {
Howard Hinnant499cea12013-08-23 17:37:05 +00001483 (*__p)->__c_ = nullptr;
1484 if (--__c->end_ != __p)
1485 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001486 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001487 }
Howard Hinnant499cea12013-08-23 17:37:05 +00001488 __get_db()->unlock();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001489 }
Howard Hinnant499cea12013-08-23 17:37:05 +00001490#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001491}
1492
1493template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001494inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant53f7d4c2011-06-03 18:40:47 +00001495basic_string<_CharT, _Traits, _Allocator>::basic_string()
Marshall Clowc912c0c2015-06-04 02:05:41 +00001496 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001497{
Howard Hinnant499cea12013-08-23 17:37:05 +00001498#if _LIBCPP_DEBUG_LEVEL >= 2
1499 __get_db()->__insert_c(this);
1500#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001501 __zero();
1502}
1503
1504template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001505inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001506basic_string<_CharT, _Traits, _Allocator>::basic_string(const allocator_type& __a)
Eric Fiselier692177d2015-07-18 20:40:46 +00001507#if _LIBCPP_STD_VER <= 14
1508 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value)
1509#else
1510 _NOEXCEPT
1511#endif
Eric Fiselierdb14bcc2017-04-12 23:45:53 +00001512: __r_(__second_tag(), __a)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001513{
Howard Hinnant499cea12013-08-23 17:37:05 +00001514#if _LIBCPP_DEBUG_LEVEL >= 2
1515 __get_db()->__insert_c(this);
1516#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001517 __zero();
1518}
1519
1520template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier6dbed462016-09-16 00:00:48 +00001521void basic_string<_CharT, _Traits, _Allocator>::__init(const value_type* __s,
1522 size_type __sz,
1523 size_type __reserve)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001524{
1525 if (__reserve > max_size())
1526 this->__throw_length_error();
1527 pointer __p;
1528 if (__reserve < __min_cap)
1529 {
1530 __set_short_size(__sz);
1531 __p = __get_short_pointer();
1532 }
1533 else
1534 {
1535 size_type __cap = __recommend(__reserve);
Howard Hinnante32b5e22010-11-17 17:55:08 +00001536 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001537 __set_long_pointer(__p);
1538 __set_long_cap(__cap+1);
1539 __set_long_size(__sz);
1540 }
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001541 traits_type::copy(_VSTD::__to_raw_pointer(__p), __s, __sz);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001542 traits_type::assign(__p[__sz], value_type());
1543}
1544
1545template <class _CharT, class _Traits, class _Allocator>
1546void
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001547basic_string<_CharT, _Traits, _Allocator>::__init(const value_type* __s, size_type __sz)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001548{
1549 if (__sz > max_size())
1550 this->__throw_length_error();
1551 pointer __p;
1552 if (__sz < __min_cap)
1553 {
1554 __set_short_size(__sz);
1555 __p = __get_short_pointer();
1556 }
1557 else
1558 {
1559 size_type __cap = __recommend(__sz);
Howard Hinnante32b5e22010-11-17 17:55:08 +00001560 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001561 __set_long_pointer(__p);
1562 __set_long_cap(__cap+1);
1563 __set_long_size(__sz);
1564 }
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001565 traits_type::copy(_VSTD::__to_raw_pointer(__p), __s, __sz);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001566 traits_type::assign(__p[__sz], value_type());
1567}
1568
1569template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001570inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier0eaf2e82017-02-17 01:17:10 +00001571basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001572{
Howard Hinnant499cea12013-08-23 17:37:05 +00001573 _LIBCPP_ASSERT(__s != nullptr, "basic_string(const char*) detected nullptr");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001574 __init(__s, traits_type::length(__s));
Howard Hinnant499cea12013-08-23 17:37:05 +00001575#if _LIBCPP_DEBUG_LEVEL >= 2
1576 __get_db()->__insert_c(this);
1577#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001578}
1579
1580template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001581inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier0eaf2e82017-02-17 01:17:10 +00001582basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, const _Allocator& __a)
Eric Fiselierdb14bcc2017-04-12 23:45:53 +00001583 : __r_(__second_tag(), __a)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001584{
Howard Hinnant499cea12013-08-23 17:37:05 +00001585 _LIBCPP_ASSERT(__s != nullptr, "basic_string(const char*, allocator) detected nullptr");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001586 __init(__s, traits_type::length(__s));
Howard Hinnant499cea12013-08-23 17:37:05 +00001587#if _LIBCPP_DEBUG_LEVEL >= 2
1588 __get_db()->__insert_c(this);
1589#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001590}
1591
1592template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001593inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier0eaf2e82017-02-17 01:17:10 +00001594basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, size_type __n)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001595{
Howard Hinnant499cea12013-08-23 17:37:05 +00001596 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "basic_string(const char*, n) detected nullptr");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001597 __init(__s, __n);
Howard Hinnant499cea12013-08-23 17:37:05 +00001598#if _LIBCPP_DEBUG_LEVEL >= 2
1599 __get_db()->__insert_c(this);
1600#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001601}
1602
1603template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001604inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier0eaf2e82017-02-17 01:17:10 +00001605basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, size_type __n, const _Allocator& __a)
Eric Fiselierdb14bcc2017-04-12 23:45:53 +00001606 : __r_(__second_tag(), __a)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001607{
Howard Hinnant499cea12013-08-23 17:37:05 +00001608 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "basic_string(const char*, n, allocator) detected nullptr");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001609 __init(__s, __n);
Howard Hinnant499cea12013-08-23 17:37:05 +00001610#if _LIBCPP_DEBUG_LEVEL >= 2
1611 __get_db()->__insert_c(this);
1612#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001613}
1614
1615template <class _CharT, class _Traits, class _Allocator>
1616basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str)
Eric Fiselierdb14bcc2017-04-12 23:45:53 +00001617 : __r_(__second_tag(), __alloc_traits::select_on_container_copy_construction(__str.__alloc()))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001618{
1619 if (!__str.__is_long())
1620 __r_.first().__r = __str.__r_.first().__r;
1621 else
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001622 __init(_VSTD::__to_raw_pointer(__str.__get_long_pointer()), __str.__get_long_size());
Howard Hinnant499cea12013-08-23 17:37:05 +00001623#if _LIBCPP_DEBUG_LEVEL >= 2
1624 __get_db()->__insert_c(this);
1625#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001626}
1627
1628template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier0eaf2e82017-02-17 01:17:10 +00001629basic_string<_CharT, _Traits, _Allocator>::basic_string(
1630 const basic_string& __str, const allocator_type& __a)
Eric Fiselierdb14bcc2017-04-12 23:45:53 +00001631 : __r_(__second_tag(), __a)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001632{
1633 if (!__str.__is_long())
1634 __r_.first().__r = __str.__r_.first().__r;
1635 else
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001636 __init(_VSTD::__to_raw_pointer(__str.__get_long_pointer()), __str.__get_long_size());
Howard Hinnant499cea12013-08-23 17:37:05 +00001637#if _LIBCPP_DEBUG_LEVEL >= 2
1638 __get_db()->__insert_c(this);
1639#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001640}
1641
Eric Fiselier3e928972017-04-19 00:28:44 +00001642#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001643
1644template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001645inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant53f7d4c2011-06-03 18:40:47 +00001646basic_string<_CharT, _Traits, _Allocator>::basic_string(basic_string&& __str)
Marshall Clow7b193f72015-06-03 19:56:43 +00001647#if _LIBCPP_STD_VER <= 14
Howard Hinnant53f7d4c2011-06-03 18:40:47 +00001648 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Marshall Clow7b193f72015-06-03 19:56:43 +00001649#else
1650 _NOEXCEPT
1651#endif
Howard Hinnant0949eed2011-06-30 21:18:19 +00001652 : __r_(_VSTD::move(__str.__r_))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001653{
1654 __str.__zero();
Howard Hinnant499cea12013-08-23 17:37:05 +00001655#if _LIBCPP_DEBUG_LEVEL >= 2
1656 __get_db()->__insert_c(this);
1657 if (__is_long())
1658 __get_db()->swap(this, &__str);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001659#endif
1660}
1661
1662template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001663inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001664basic_string<_CharT, _Traits, _Allocator>::basic_string(basic_string&& __str, const allocator_type& __a)
Eric Fiselierdb14bcc2017-04-12 23:45:53 +00001665 : __r_(__second_tag(), __a)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001666{
Marshall Clowd5549cc2014-07-17 15:32:20 +00001667 if (__str.__is_long() && __a != __str.__alloc()) // copy, not move
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001668 __init(_VSTD::__to_raw_pointer(__str.__get_long_pointer()), __str.__get_long_size());
Marshall Clowd5549cc2014-07-17 15:32:20 +00001669 else
1670 {
1671 __r_.first().__r = __str.__r_.first().__r;
1672 __str.__zero();
1673 }
Howard Hinnant499cea12013-08-23 17:37:05 +00001674#if _LIBCPP_DEBUG_LEVEL >= 2
1675 __get_db()->__insert_c(this);
1676 if (__is_long())
1677 __get_db()->swap(this, &__str);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001678#endif
1679}
1680
Eric Fiselier3e928972017-04-19 00:28:44 +00001681#endif // _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001682
1683template <class _CharT, class _Traits, class _Allocator>
1684void
1685basic_string<_CharT, _Traits, _Allocator>::__init(size_type __n, value_type __c)
1686{
1687 if (__n > max_size())
1688 this->__throw_length_error();
1689 pointer __p;
1690 if (__n < __min_cap)
1691 {
1692 __set_short_size(__n);
1693 __p = __get_short_pointer();
1694 }
1695 else
1696 {
1697 size_type __cap = __recommend(__n);
Howard Hinnante32b5e22010-11-17 17:55:08 +00001698 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001699 __set_long_pointer(__p);
1700 __set_long_cap(__cap+1);
1701 __set_long_size(__n);
1702 }
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001703 traits_type::assign(_VSTD::__to_raw_pointer(__p), __n, __c);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001704 traits_type::assign(__p[__n], value_type());
1705}
1706
1707template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001708inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier0eaf2e82017-02-17 01:17:10 +00001709basic_string<_CharT, _Traits, _Allocator>::basic_string(size_type __n, _CharT __c)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001710{
1711 __init(__n, __c);
Howard Hinnant499cea12013-08-23 17:37:05 +00001712#if _LIBCPP_DEBUG_LEVEL >= 2
1713 __get_db()->__insert_c(this);
1714#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001715}
1716
1717template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001718inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier0eaf2e82017-02-17 01:17:10 +00001719basic_string<_CharT, _Traits, _Allocator>::basic_string(size_type __n, _CharT __c, const _Allocator& __a)
Eric Fiselierdb14bcc2017-04-12 23:45:53 +00001720 : __r_(__second_tag(), __a)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001721{
1722 __init(__n, __c);
Howard Hinnant499cea12013-08-23 17:37:05 +00001723#if _LIBCPP_DEBUG_LEVEL >= 2
1724 __get_db()->__insert_c(this);
1725#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001726}
1727
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001728template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier0eaf2e82017-02-17 01:17:10 +00001729basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str,
1730 size_type __pos, size_type __n,
1731 const _Allocator& __a)
Eric Fiselierdb14bcc2017-04-12 23:45:53 +00001732 : __r_(__second_tag(), __a)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001733{
1734 size_type __str_sz = __str.size();
1735 if (__pos > __str_sz)
1736 this->__throw_out_of_range();
Howard Hinnant0949eed2011-06-30 21:18:19 +00001737 __init(__str.data() + __pos, _VSTD::min(__n, __str_sz - __pos));
Howard Hinnant499cea12013-08-23 17:37:05 +00001738#if _LIBCPP_DEBUG_LEVEL >= 2
1739 __get_db()->__insert_c(this);
1740#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001741}
1742
1743template <class _CharT, class _Traits, class _Allocator>
Marshall Clowf4f7d8f2016-04-07 18:13:41 +00001744inline _LIBCPP_INLINE_VISIBILITY
1745basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str, size_type __pos,
Eric Fiselier0eaf2e82017-02-17 01:17:10 +00001746 const _Allocator& __a)
Eric Fiselierdb14bcc2017-04-12 23:45:53 +00001747 : __r_(__second_tag(), __a)
Marshall Clowf4f7d8f2016-04-07 18:13:41 +00001748{
1749 size_type __str_sz = __str.size();
1750 if (__pos > __str_sz)
1751 this->__throw_out_of_range();
1752 __init(__str.data() + __pos, __str_sz - __pos);
1753#if _LIBCPP_DEBUG_LEVEL >= 2
1754 __get_db()->__insert_c(this);
1755#endif
1756}
1757
1758template <class _CharT, class _Traits, class _Allocator>
Marshall Clowdb7fa112016-11-14 18:22:19 +00001759template <class _Tp>
1760basic_string<_CharT, _Traits, _Allocator>::basic_string(
1761 const _Tp& __t, size_type __pos, size_type __n, const allocator_type& __a,
1762 typename enable_if<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value, void>::type *)
Eric Fiselierdb14bcc2017-04-12 23:45:53 +00001763 : __r_(__second_tag(), __a)
Marshall Clowdb7fa112016-11-14 18:22:19 +00001764{
1765 __self_view __sv = __self_view(__t).substr(__pos, __n);
1766 __init(__sv.data(), __sv.size());
1767#if _LIBCPP_DEBUG_LEVEL >= 2
1768 __get_db()->__insert_c(this);
Eric Fiselier0eaf2e82017-02-17 01:17:10 +00001769#endif
Marshall Clowdb7fa112016-11-14 18:22:19 +00001770}
1771
1772template <class _CharT, class _Traits, class _Allocator>
Marshall Clow1e00d6d2016-07-21 05:31:24 +00001773inline _LIBCPP_INLINE_VISIBILITY
1774basic_string<_CharT, _Traits, _Allocator>::basic_string(__self_view __sv)
1775{
1776 __init(__sv.data(), __sv.size());
1777#if _LIBCPP_DEBUG_LEVEL >= 2
1778 __get_db()->__insert_c(this);
1779#endif
1780}
1781
1782template <class _CharT, class _Traits, class _Allocator>
1783inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier0eaf2e82017-02-17 01:17:10 +00001784basic_string<_CharT, _Traits, _Allocator>::basic_string(__self_view __sv, const _Allocator& __a)
Eric Fiselierdb14bcc2017-04-12 23:45:53 +00001785 : __r_(__second_tag(), __a)
Marshall Clow1e00d6d2016-07-21 05:31:24 +00001786{
1787 __init(__sv.data(), __sv.size());
1788#if _LIBCPP_DEBUG_LEVEL >= 2
1789 __get_db()->__insert_c(this);
1790#endif
1791}
1792
1793template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001794template <class _InputIterator>
1795typename enable_if
1796<
Marshall Clowdf9db312016-01-13 21:54:34 +00001797 __is_exactly_input_iterator<_InputIterator>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001798 void
1799>::type
1800basic_string<_CharT, _Traits, _Allocator>::__init(_InputIterator __first, _InputIterator __last)
1801{
1802 __zero();
1803#ifndef _LIBCPP_NO_EXCEPTIONS
1804 try
1805 {
Howard Hinnant324bb032010-08-22 00:02:43 +00001806#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001807 for (; __first != __last; ++__first)
1808 push_back(*__first);
1809#ifndef _LIBCPP_NO_EXCEPTIONS
1810 }
1811 catch (...)
1812 {
1813 if (__is_long())
Howard Hinnante32b5e22010-11-17 17:55:08 +00001814 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001815 throw;
1816 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001817#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001818}
1819
1820template <class _CharT, class _Traits, class _Allocator>
1821template <class _ForwardIterator>
1822typename enable_if
1823<
1824 __is_forward_iterator<_ForwardIterator>::value,
1825 void
1826>::type
1827basic_string<_CharT, _Traits, _Allocator>::__init(_ForwardIterator __first, _ForwardIterator __last)
1828{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001829 size_type __sz = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001830 if (__sz > max_size())
1831 this->__throw_length_error();
1832 pointer __p;
1833 if (__sz < __min_cap)
1834 {
1835 __set_short_size(__sz);
1836 __p = __get_short_pointer();
1837 }
1838 else
1839 {
1840 size_type __cap = __recommend(__sz);
Howard Hinnante32b5e22010-11-17 17:55:08 +00001841 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001842 __set_long_pointer(__p);
1843 __set_long_cap(__cap+1);
1844 __set_long_size(__sz);
1845 }
Eric Fiselierb9919752014-10-27 19:28:20 +00001846 for (; __first != __last; ++__first, (void) ++__p)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001847 traits_type::assign(*__p, *__first);
1848 traits_type::assign(*__p, value_type());
1849}
1850
1851template <class _CharT, class _Traits, class _Allocator>
1852template<class _InputIterator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001853inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001854basic_string<_CharT, _Traits, _Allocator>::basic_string(_InputIterator __first, _InputIterator __last)
1855{
1856 __init(__first, __last);
Howard Hinnant499cea12013-08-23 17:37:05 +00001857#if _LIBCPP_DEBUG_LEVEL >= 2
1858 __get_db()->__insert_c(this);
1859#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001860}
1861
1862template <class _CharT, class _Traits, class _Allocator>
1863template<class _InputIterator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001864inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001865basic_string<_CharT, _Traits, _Allocator>::basic_string(_InputIterator __first, _InputIterator __last,
1866 const allocator_type& __a)
Eric Fiselierdb14bcc2017-04-12 23:45:53 +00001867 : __r_(__second_tag(), __a)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001868{
1869 __init(__first, __last);
Howard Hinnant499cea12013-08-23 17:37:05 +00001870#if _LIBCPP_DEBUG_LEVEL >= 2
1871 __get_db()->__insert_c(this);
1872#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001873}
1874
Eric Fiselier3e928972017-04-19 00:28:44 +00001875#ifndef _LIBCPP_CXX03_LANG
Howard Hinnante3e32912011-08-12 21:56:02 +00001876
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001877template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001878inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier0eaf2e82017-02-17 01:17:10 +00001879basic_string<_CharT, _Traits, _Allocator>::basic_string(
1880 initializer_list<_CharT> __il)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001881{
1882 __init(__il.begin(), __il.end());
Howard Hinnant499cea12013-08-23 17:37:05 +00001883#if _LIBCPP_DEBUG_LEVEL >= 2
1884 __get_db()->__insert_c(this);
1885#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001886}
1887
1888template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001889inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselierdb14bcc2017-04-12 23:45:53 +00001890
Eric Fiselier0eaf2e82017-02-17 01:17:10 +00001891basic_string<_CharT, _Traits, _Allocator>::basic_string(
1892 initializer_list<_CharT> __il, const _Allocator& __a)
Eric Fiselierdb14bcc2017-04-12 23:45:53 +00001893 : __r_(__second_tag(), __a)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001894{
1895 __init(__il.begin(), __il.end());
Howard Hinnant499cea12013-08-23 17:37:05 +00001896#if _LIBCPP_DEBUG_LEVEL >= 2
1897 __get_db()->__insert_c(this);
1898#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001899}
1900
Eric Fiselier3e928972017-04-19 00:28:44 +00001901#endif // _LIBCPP_CXX03_LANG
Howard Hinnante3e32912011-08-12 21:56:02 +00001902
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001903template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001904basic_string<_CharT, _Traits, _Allocator>::~basic_string()
1905{
Howard Hinnant499cea12013-08-23 17:37:05 +00001906#if _LIBCPP_DEBUG_LEVEL >= 2
1907 __get_db()->__erase_c(this);
1908#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001909 if (__is_long())
Howard Hinnante32b5e22010-11-17 17:55:08 +00001910 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001911}
1912
1913template <class _CharT, class _Traits, class _Allocator>
1914void
1915basic_string<_CharT, _Traits, _Allocator>::__grow_by_and_replace
1916 (size_type __old_cap, size_type __delta_cap, size_type __old_sz,
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001917 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 +00001918{
1919 size_type __ms = max_size();
1920 if (__delta_cap > __ms - __old_cap - 1)
1921 this->__throw_length_error();
1922 pointer __old_p = __get_pointer();
1923 size_type __cap = __old_cap < __ms / 2 - __alignment ?
Howard Hinnant0949eed2011-06-30 21:18:19 +00001924 __recommend(_VSTD::max(__old_cap + __delta_cap, 2 * __old_cap)) :
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001925 __ms - 1;
Howard Hinnante32b5e22010-11-17 17:55:08 +00001926 pointer __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001927 __invalidate_all_iterators();
1928 if (__n_copy != 0)
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001929 traits_type::copy(_VSTD::__to_raw_pointer(__p),
1930 _VSTD::__to_raw_pointer(__old_p), __n_copy);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001931 if (__n_add != 0)
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001932 traits_type::copy(_VSTD::__to_raw_pointer(__p) + __n_copy, __p_new_stuff, __n_add);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001933 size_type __sec_cp_sz = __old_sz - __n_del - __n_copy;
1934 if (__sec_cp_sz != 0)
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001935 traits_type::copy(_VSTD::__to_raw_pointer(__p) + __n_copy + __n_add,
1936 _VSTD::__to_raw_pointer(__old_p) + __n_copy + __n_del, __sec_cp_sz);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001937 if (__old_cap+1 != __min_cap)
Howard Hinnante32b5e22010-11-17 17:55:08 +00001938 __alloc_traits::deallocate(__alloc(), __old_p, __old_cap+1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001939 __set_long_pointer(__p);
1940 __set_long_cap(__cap+1);
1941 __old_sz = __n_copy + __n_add + __sec_cp_sz;
1942 __set_long_size(__old_sz);
1943 traits_type::assign(__p[__old_sz], value_type());
1944}
1945
1946template <class _CharT, class _Traits, class _Allocator>
1947void
1948basic_string<_CharT, _Traits, _Allocator>::__grow_by(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
1949 size_type __n_copy, size_type __n_del, size_type __n_add)
1950{
1951 size_type __ms = max_size();
Marshall Clowecc8d7b2013-11-06 14:24:38 +00001952 if (__delta_cap > __ms - __old_cap)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001953 this->__throw_length_error();
1954 pointer __old_p = __get_pointer();
1955 size_type __cap = __old_cap < __ms / 2 - __alignment ?
Howard Hinnant0949eed2011-06-30 21:18:19 +00001956 __recommend(_VSTD::max(__old_cap + __delta_cap, 2 * __old_cap)) :
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001957 __ms - 1;
Howard Hinnante32b5e22010-11-17 17:55:08 +00001958 pointer __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001959 __invalidate_all_iterators();
1960 if (__n_copy != 0)
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001961 traits_type::copy(_VSTD::__to_raw_pointer(__p),
1962 _VSTD::__to_raw_pointer(__old_p), __n_copy);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001963 size_type __sec_cp_sz = __old_sz - __n_del - __n_copy;
1964 if (__sec_cp_sz != 0)
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001965 traits_type::copy(_VSTD::__to_raw_pointer(__p) + __n_copy + __n_add,
1966 _VSTD::__to_raw_pointer(__old_p) + __n_copy + __n_del,
1967 __sec_cp_sz);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001968 if (__old_cap+1 != __min_cap)
Howard Hinnante32b5e22010-11-17 17:55:08 +00001969 __alloc_traits::deallocate(__alloc(), __old_p, __old_cap+1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001970 __set_long_pointer(__p);
1971 __set_long_cap(__cap+1);
1972}
1973
1974// assign
1975
1976template <class _CharT, class _Traits, class _Allocator>
1977basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001978basic_string<_CharT, _Traits, _Allocator>::assign(const value_type* __s, size_type __n)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001979{
Alp Tokerec34c482014-05-15 11:27:39 +00001980 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::assign received nullptr");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001981 size_type __cap = capacity();
1982 if (__cap >= __n)
1983 {
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001984 value_type* __p = _VSTD::__to_raw_pointer(__get_pointer());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001985 traits_type::move(__p, __s, __n);
1986 traits_type::assign(__p[__n], value_type());
1987 __set_size(__n);
1988 __invalidate_iterators_past(__n);
1989 }
1990 else
1991 {
1992 size_type __sz = size();
1993 __grow_by_and_replace(__cap, __n - __cap, __sz, 0, __sz, __n, __s);
1994 }
1995 return *this;
1996}
1997
1998template <class _CharT, class _Traits, class _Allocator>
1999basic_string<_CharT, _Traits, _Allocator>&
2000basic_string<_CharT, _Traits, _Allocator>::assign(size_type __n, value_type __c)
2001{
2002 size_type __cap = capacity();
2003 if (__cap < __n)
2004 {
2005 size_type __sz = size();
2006 __grow_by(__cap, __n - __cap, __sz, 0, __sz);
2007 }
2008 else
2009 __invalidate_iterators_past(__n);
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002010 value_type* __p = _VSTD::__to_raw_pointer(__get_pointer());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002011 traits_type::assign(__p, __n, __c);
2012 traits_type::assign(__p[__n], value_type());
2013 __set_size(__n);
2014 return *this;
2015}
2016
2017template <class _CharT, class _Traits, class _Allocator>
2018basic_string<_CharT, _Traits, _Allocator>&
2019basic_string<_CharT, _Traits, _Allocator>::operator=(value_type __c)
2020{
2021 pointer __p;
2022 if (__is_long())
2023 {
2024 __p = __get_long_pointer();
2025 __set_long_size(1);
2026 }
2027 else
2028 {
2029 __p = __get_short_pointer();
2030 __set_short_size(1);
2031 }
2032 traits_type::assign(*__p, __c);
2033 traits_type::assign(*++__p, value_type());
2034 __invalidate_iterators_past(1);
2035 return *this;
2036}
2037
2038template <class _CharT, class _Traits, class _Allocator>
Howard Hinnante32b5e22010-11-17 17:55:08 +00002039basic_string<_CharT, _Traits, _Allocator>&
2040basic_string<_CharT, _Traits, _Allocator>::operator=(const basic_string& __str)
2041{
2042 if (this != &__str)
2043 {
2044 __copy_assign_alloc(__str);
Marshall Clowf40ec902016-03-09 18:08:29 +00002045 assign(__str.data(), __str.size());
Howard Hinnante32b5e22010-11-17 17:55:08 +00002046 }
2047 return *this;
2048}
2049
Eric Fiselier3e928972017-04-19 00:28:44 +00002050#ifndef _LIBCPP_CXX03_LANG
Howard Hinnante32b5e22010-11-17 17:55:08 +00002051
2052template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002053inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnante32b5e22010-11-17 17:55:08 +00002054void
2055basic_string<_CharT, _Traits, _Allocator>::__move_assign(basic_string& __str, false_type)
Marshall Clowaf961ed2015-08-18 18:57:00 +00002056 _NOEXCEPT_(__alloc_traits::is_always_equal::value)
Howard Hinnante32b5e22010-11-17 17:55:08 +00002057{
2058 if (__alloc() != __str.__alloc())
2059 assign(__str);
2060 else
2061 __move_assign(__str, true_type());
2062}
2063
2064template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002065inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnante32b5e22010-11-17 17:55:08 +00002066void
2067basic_string<_CharT, _Traits, _Allocator>::__move_assign(basic_string& __str, true_type)
Marshall Clowaf961ed2015-08-18 18:57:00 +00002068#if _LIBCPP_STD_VER > 14
2069 _NOEXCEPT
2070#else
Howard Hinnant53f7d4c2011-06-03 18:40:47 +00002071 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Marshall Clowaf961ed2015-08-18 18:57:00 +00002072#endif
Howard Hinnante32b5e22010-11-17 17:55:08 +00002073{
2074 clear();
2075 shrink_to_fit();
Howard Hinnant3fdbbd22011-08-17 20:36:18 +00002076 __r_.first() = __str.__r_.first();
2077 __move_assign_alloc(__str);
Howard Hinnante32b5e22010-11-17 17:55:08 +00002078 __str.__zero();
2079}
2080
2081template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002082inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnante32b5e22010-11-17 17:55:08 +00002083basic_string<_CharT, _Traits, _Allocator>&
2084basic_string<_CharT, _Traits, _Allocator>::operator=(basic_string&& __str)
Marshall Clowaf961ed2015-08-18 18:57:00 +00002085 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
Howard Hinnante32b5e22010-11-17 17:55:08 +00002086{
2087 __move_assign(__str, integral_constant<bool,
2088 __alloc_traits::propagate_on_container_move_assignment::value>());
2089 return *this;
2090}
2091
2092#endif
2093
2094template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002095template<class _InputIterator>
2096typename enable_if
2097<
Marshall Clowdf9db312016-01-13 21:54:34 +00002098 __is_exactly_input_iterator <_InputIterator>::value
2099 || !__libcpp_string_gets_noexcept_iterator<_InputIterator>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002100 basic_string<_CharT, _Traits, _Allocator>&
2101>::type
2102basic_string<_CharT, _Traits, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
2103{
Marshall Clowd979eed2016-09-05 01:54:30 +00002104 const basic_string __temp(__first, __last, __alloc());
Marshall Clowdf9db312016-01-13 21:54:34 +00002105 assign(__temp.data(), __temp.size());
Argyrios Kyrtzidis1dc6f7a2012-10-13 02:03:45 +00002106 return *this;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002107}
2108
2109template <class _CharT, class _Traits, class _Allocator>
2110template<class _ForwardIterator>
2111typename enable_if
2112<
Marshall Clowdf9db312016-01-13 21:54:34 +00002113 __is_forward_iterator<_ForwardIterator>::value
2114 && __libcpp_string_gets_noexcept_iterator<_ForwardIterator>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002115 basic_string<_CharT, _Traits, _Allocator>&
2116>::type
2117basic_string<_CharT, _Traits, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
2118{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002119 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002120 size_type __cap = capacity();
2121 if (__cap < __n)
2122 {
2123 size_type __sz = size();
2124 __grow_by(__cap, __n - __cap, __sz, 0, __sz);
2125 }
2126 else
2127 __invalidate_iterators_past(__n);
2128 pointer __p = __get_pointer();
2129 for (; __first != __last; ++__first, ++__p)
2130 traits_type::assign(*__p, *__first);
2131 traits_type::assign(*__p, value_type());
2132 __set_size(__n);
2133 return *this;
2134}
2135
2136template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002137basic_string<_CharT, _Traits, _Allocator>&
2138basic_string<_CharT, _Traits, _Allocator>::assign(const basic_string& __str, size_type __pos, size_type __n)
2139{
2140 size_type __sz = __str.size();
2141 if (__pos > __sz)
2142 this->__throw_out_of_range();
Howard Hinnant0949eed2011-06-30 21:18:19 +00002143 return assign(__str.data() + __pos, _VSTD::min(__n, __sz - __pos));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002144}
2145
2146template <class _CharT, class _Traits, class _Allocator>
Marshall Clow6ac8de02016-09-24 22:45:42 +00002147template <class _Tp>
2148typename enable_if
2149<
2150 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
2151 basic_string<_CharT, _Traits, _Allocator>&
2152>::type
2153basic_string<_CharT, _Traits, _Allocator>::assign(const _Tp & __t, size_type __pos, size_type __n)
Marshall Clow1e00d6d2016-07-21 05:31:24 +00002154{
Marshall Clow6ac8de02016-09-24 22:45:42 +00002155 __self_view __sv = __t;
Marshall Clow1e00d6d2016-07-21 05:31:24 +00002156 size_type __sz = __sv.size();
2157 if (__pos > __sz)
2158 this->__throw_out_of_range();
2159 return assign(__sv.data() + __pos, _VSTD::min(__n, __sz - __pos));
2160}
2161
2162
2163template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002164basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002165basic_string<_CharT, _Traits, _Allocator>::assign(const value_type* __s)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002166{
Alp Tokerec34c482014-05-15 11:27:39 +00002167 _LIBCPP_ASSERT(__s != nullptr, "string::assign received nullptr");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002168 return assign(__s, traits_type::length(__s));
2169}
2170
2171// append
2172
2173template <class _CharT, class _Traits, class _Allocator>
2174basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002175basic_string<_CharT, _Traits, _Allocator>::append(const value_type* __s, size_type __n)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002176{
Alp Tokerec34c482014-05-15 11:27:39 +00002177 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::append received nullptr");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002178 size_type __cap = capacity();
2179 size_type __sz = size();
2180 if (__cap - __sz >= __n)
2181 {
2182 if (__n)
2183 {
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002184 value_type* __p = _VSTD::__to_raw_pointer(__get_pointer());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002185 traits_type::copy(__p + __sz, __s, __n);
2186 __sz += __n;
2187 __set_size(__sz);
2188 traits_type::assign(__p[__sz], value_type());
2189 }
2190 }
2191 else
2192 __grow_by_and_replace(__cap, __sz + __n - __cap, __sz, __sz, 0, __n, __s);
2193 return *this;
2194}
2195
2196template <class _CharT, class _Traits, class _Allocator>
2197basic_string<_CharT, _Traits, _Allocator>&
2198basic_string<_CharT, _Traits, _Allocator>::append(size_type __n, value_type __c)
2199{
2200 if (__n)
2201 {
2202 size_type __cap = capacity();
2203 size_type __sz = size();
2204 if (__cap - __sz < __n)
2205 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2206 pointer __p = __get_pointer();
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002207 traits_type::assign(_VSTD::__to_raw_pointer(__p) + __sz, __n, __c);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002208 __sz += __n;
2209 __set_size(__sz);
2210 traits_type::assign(__p[__sz], value_type());
2211 }
2212 return *this;
2213}
2214
2215template <class _CharT, class _Traits, class _Allocator>
2216void
2217basic_string<_CharT, _Traits, _Allocator>::push_back(value_type __c)
2218{
Howard Hinnant15467182013-04-30 21:44:48 +00002219 bool __is_short = !__is_long();
2220 size_type __cap;
2221 size_type __sz;
2222 if (__is_short)
2223 {
2224 __cap = __min_cap - 1;
2225 __sz = __get_short_size();
2226 }
2227 else
2228 {
2229 __cap = __get_long_cap() - 1;
2230 __sz = __get_long_size();
2231 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002232 if (__sz == __cap)
Howard Hinnant15467182013-04-30 21:44:48 +00002233 {
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002234 __grow_by(__cap, 1, __sz, __sz, 0);
Howard Hinnant15467182013-04-30 21:44:48 +00002235 __is_short = !__is_long();
2236 }
2237 pointer __p;
2238 if (__is_short)
2239 {
2240 __p = __get_short_pointer() + __sz;
2241 __set_short_size(__sz+1);
2242 }
2243 else
2244 {
2245 __p = __get_long_pointer() + __sz;
2246 __set_long_size(__sz+1);
2247 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002248 traits_type::assign(*__p, __c);
2249 traits_type::assign(*++__p, value_type());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002250}
2251
Marshall Clowac655ef2016-09-07 03:32:06 +00002252template <class _Tp>
Marshall Clowd979eed2016-09-05 01:54:30 +00002253bool __ptr_in_range (const _Tp* __p, const _Tp* __first, const _Tp* __last)
2254{
2255 return __first <= __p && __p < __last;
2256}
2257
Marshall Clowac655ef2016-09-07 03:32:06 +00002258template <class _Tp1, class _Tp2>
Eric Fiselier0e5ebbc2016-12-23 23:37:52 +00002259bool __ptr_in_range (const _Tp1*, const _Tp2*, const _Tp2*)
Marshall Clowac655ef2016-09-07 03:32:06 +00002260{
2261 return false;
2262}
2263
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002264template <class _CharT, class _Traits, class _Allocator>
2265template<class _ForwardIterator>
Eric Fiselier026d38e2016-10-31 02:46:25 +00002266basic_string<_CharT, _Traits, _Allocator>&
2267basic_string<_CharT, _Traits, _Allocator>::__append_forward_unsafe(
2268 _ForwardIterator __first, _ForwardIterator __last)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002269{
Eric Fiselier026d38e2016-10-31 02:46:25 +00002270 static_assert(__is_forward_iterator<_ForwardIterator>::value,
2271 "function requires a ForwardIterator");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002272 size_type __sz = size();
2273 size_type __cap = capacity();
Howard Hinnant0949eed2011-06-30 21:18:19 +00002274 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002275 if (__n)
2276 {
Eric Fiselier622c7d52017-04-15 06:49:02 +00002277 typedef typename iterator_traits<_ForwardIterator>::reference _CharRef;
2278 _CharRef __tmp_ref = *__first;
2279 if (__ptr_in_range(_VSTD::addressof(__tmp_ref), data(), data() + size()))
Marshall Clowd979eed2016-09-05 01:54:30 +00002280 {
2281 const basic_string __temp (__first, __last, __alloc());
2282 append(__temp.data(), __temp.size());
2283 }
2284 else
2285 {
2286 if (__cap - __sz < __n)
2287 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2288 pointer __p = __get_pointer() + __sz;
2289 for (; __first != __last; ++__p, ++__first)
2290 traits_type::assign(*__p, *__first);
2291 traits_type::assign(*__p, value_type());
2292 __set_size(__sz + __n);
2293 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002294 }
2295 return *this;
2296}
2297
2298template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002299inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002300basic_string<_CharT, _Traits, _Allocator>&
2301basic_string<_CharT, _Traits, _Allocator>::append(const basic_string& __str)
2302{
2303 return append(__str.data(), __str.size());
2304}
2305
2306template <class _CharT, class _Traits, class _Allocator>
2307basic_string<_CharT, _Traits, _Allocator>&
2308basic_string<_CharT, _Traits, _Allocator>::append(const basic_string& __str, size_type __pos, size_type __n)
2309{
2310 size_type __sz = __str.size();
2311 if (__pos > __sz)
2312 this->__throw_out_of_range();
Howard Hinnant0949eed2011-06-30 21:18:19 +00002313 return append(__str.data() + __pos, _VSTD::min(__n, __sz - __pos));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002314}
2315
2316template <class _CharT, class _Traits, class _Allocator>
Marshall Clow42a87db2016-10-03 23:40:48 +00002317template <class _Tp>
Marshall Clow6ac8de02016-09-24 22:45:42 +00002318 typename enable_if
2319 <
2320 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
2321 basic_string<_CharT, _Traits, _Allocator>&
2322 >::type
2323basic_string<_CharT, _Traits, _Allocator>::append(const _Tp & __t, size_type __pos, size_type __n)
Marshall Clow1e00d6d2016-07-21 05:31:24 +00002324{
Marshall Clow6ac8de02016-09-24 22:45:42 +00002325 __self_view __sv = __t;
Marshall Clow1e00d6d2016-07-21 05:31:24 +00002326 size_type __sz = __sv.size();
2327 if (__pos > __sz)
2328 this->__throw_out_of_range();
2329 return append(__sv.data() + __pos, _VSTD::min(__n, __sz - __pos));
2330}
2331
2332template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002333basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002334basic_string<_CharT, _Traits, _Allocator>::append(const value_type* __s)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002335{
Alp Tokerec34c482014-05-15 11:27:39 +00002336 _LIBCPP_ASSERT(__s != nullptr, "string::append received nullptr");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002337 return append(__s, traits_type::length(__s));
2338}
2339
2340// insert
2341
2342template <class _CharT, class _Traits, class _Allocator>
2343basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002344basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, const value_type* __s, size_type __n)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002345{
Alp Tokerec34c482014-05-15 11:27:39 +00002346 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::insert received nullptr");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002347 size_type __sz = size();
2348 if (__pos > __sz)
2349 this->__throw_out_of_range();
2350 size_type __cap = capacity();
2351 if (__cap - __sz >= __n)
2352 {
2353 if (__n)
2354 {
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002355 value_type* __p = _VSTD::__to_raw_pointer(__get_pointer());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002356 size_type __n_move = __sz - __pos;
2357 if (__n_move != 0)
2358 {
2359 if (__p + __pos <= __s && __s < __p + __sz)
2360 __s += __n;
2361 traits_type::move(__p + __pos + __n, __p + __pos, __n_move);
2362 }
2363 traits_type::move(__p + __pos, __s, __n);
2364 __sz += __n;
2365 __set_size(__sz);
2366 traits_type::assign(__p[__sz], value_type());
2367 }
2368 }
2369 else
2370 __grow_by_and_replace(__cap, __sz + __n - __cap, __sz, __pos, 0, __n, __s);
2371 return *this;
2372}
2373
2374template <class _CharT, class _Traits, class _Allocator>
2375basic_string<_CharT, _Traits, _Allocator>&
2376basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, size_type __n, value_type __c)
2377{
2378 size_type __sz = size();
2379 if (__pos > __sz)
2380 this->__throw_out_of_range();
2381 if (__n)
2382 {
2383 size_type __cap = capacity();
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002384 value_type* __p;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002385 if (__cap - __sz >= __n)
2386 {
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002387 __p = _VSTD::__to_raw_pointer(__get_pointer());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002388 size_type __n_move = __sz - __pos;
2389 if (__n_move != 0)
2390 traits_type::move(__p + __pos + __n, __p + __pos, __n_move);
2391 }
2392 else
2393 {
2394 __grow_by(__cap, __sz + __n - __cap, __sz, __pos, 0, __n);
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002395 __p = _VSTD::__to_raw_pointer(__get_long_pointer());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002396 }
2397 traits_type::assign(__p + __pos, __n, __c);
2398 __sz += __n;
2399 __set_size(__sz);
2400 traits_type::assign(__p[__sz], value_type());
2401 }
2402 return *this;
2403}
2404
2405template <class _CharT, class _Traits, class _Allocator>
2406template<class _InputIterator>
2407typename enable_if
2408<
Marshall Clowdf9db312016-01-13 21:54:34 +00002409 __is_exactly_input_iterator<_InputIterator>::value
2410 || !__libcpp_string_gets_noexcept_iterator<_InputIterator>::value,
2411 typename basic_string<_CharT, _Traits, _Allocator>::iterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002412>::type
2413basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, _InputIterator __first, _InputIterator __last)
2414{
Howard Hinnant499cea12013-08-23 17:37:05 +00002415#if _LIBCPP_DEBUG_LEVEL >= 2
2416 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2417 "string::insert(iterator, range) called with an iterator not"
2418 " referring to this string");
2419#endif
Marshall Clowd979eed2016-09-05 01:54:30 +00002420 const basic_string __temp(__first, __last, __alloc());
Marshall Clowdf9db312016-01-13 21:54:34 +00002421 return insert(__pos, __temp.data(), __temp.data() + __temp.size());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002422}
2423
2424template <class _CharT, class _Traits, class _Allocator>
2425template<class _ForwardIterator>
2426typename enable_if
2427<
Marshall Clowdf9db312016-01-13 21:54:34 +00002428 __is_forward_iterator<_ForwardIterator>::value
2429 && __libcpp_string_gets_noexcept_iterator<_ForwardIterator>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002430 typename basic_string<_CharT, _Traits, _Allocator>::iterator
2431>::type
2432basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, _ForwardIterator __first, _ForwardIterator __last)
2433{
Howard Hinnant499cea12013-08-23 17:37:05 +00002434#if _LIBCPP_DEBUG_LEVEL >= 2
2435 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2436 "string::insert(iterator, range) called with an iterator not"
2437 " referring to this string");
2438#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002439 size_type __ip = static_cast<size_type>(__pos - begin());
Howard Hinnant0949eed2011-06-30 21:18:19 +00002440 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002441 if (__n)
2442 {
Eric Fiselier622c7d52017-04-15 06:49:02 +00002443 typedef typename iterator_traits<_ForwardIterator>::reference _CharRef;
2444 _CharRef __tmp_char = *__first;
2445 if (__ptr_in_range(_VSTD::addressof(__tmp_char), data(), data() + size()))
Marshall Clowd979eed2016-09-05 01:54:30 +00002446 {
2447 const basic_string __temp(__first, __last, __alloc());
2448 return insert(__pos, __temp.data(), __temp.data() + __temp.size());
2449 }
2450
2451 size_type __sz = size();
2452 size_type __cap = capacity();
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002453 value_type* __p;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002454 if (__cap - __sz >= __n)
2455 {
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002456 __p = _VSTD::__to_raw_pointer(__get_pointer());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002457 size_type __n_move = __sz - __ip;
2458 if (__n_move != 0)
2459 traits_type::move(__p + __ip + __n, __p + __ip, __n_move);
2460 }
2461 else
2462 {
2463 __grow_by(__cap, __sz + __n - __cap, __sz, __ip, 0, __n);
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002464 __p = _VSTD::__to_raw_pointer(__get_long_pointer());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002465 }
2466 __sz += __n;
2467 __set_size(__sz);
2468 traits_type::assign(__p[__sz], value_type());
2469 for (__p += __ip; __first != __last; ++__p, ++__first)
2470 traits_type::assign(*__p, *__first);
2471 }
2472 return begin() + __ip;
2473}
2474
2475template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002476inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002477basic_string<_CharT, _Traits, _Allocator>&
2478basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const basic_string& __str)
2479{
2480 return insert(__pos1, __str.data(), __str.size());
2481}
2482
2483template <class _CharT, class _Traits, class _Allocator>
2484basic_string<_CharT, _Traits, _Allocator>&
2485basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const basic_string& __str,
2486 size_type __pos2, size_type __n)
2487{
2488 size_type __str_sz = __str.size();
2489 if (__pos2 > __str_sz)
2490 this->__throw_out_of_range();
Howard Hinnant0949eed2011-06-30 21:18:19 +00002491 return insert(__pos1, __str.data() + __pos2, _VSTD::min(__n, __str_sz - __pos2));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002492}
2493
2494template <class _CharT, class _Traits, class _Allocator>
Marshall Clow6ac8de02016-09-24 22:45:42 +00002495template <class _Tp>
2496typename enable_if
2497<
2498 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
2499 basic_string<_CharT, _Traits, _Allocator>&
2500>::type
2501basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const _Tp& __t,
Marshall Clow1e00d6d2016-07-21 05:31:24 +00002502 size_type __pos2, size_type __n)
2503{
Marshall Clow6ac8de02016-09-24 22:45:42 +00002504 __self_view __sv = __t;
Marshall Clow1e00d6d2016-07-21 05:31:24 +00002505 size_type __str_sz = __sv.size();
2506 if (__pos2 > __str_sz)
2507 this->__throw_out_of_range();
2508 return insert(__pos1, __sv.data() + __pos2, _VSTD::min(__n, __str_sz - __pos2));
2509}
2510
2511template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002512basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002513basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, const value_type* __s)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002514{
Alp Tokerec34c482014-05-15 11:27:39 +00002515 _LIBCPP_ASSERT(__s != nullptr, "string::insert received nullptr");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002516 return insert(__pos, __s, traits_type::length(__s));
2517}
2518
2519template <class _CharT, class _Traits, class _Allocator>
2520typename basic_string<_CharT, _Traits, _Allocator>::iterator
2521basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, value_type __c)
2522{
2523 size_type __ip = static_cast<size_type>(__pos - begin());
2524 size_type __sz = size();
2525 size_type __cap = capacity();
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002526 value_type* __p;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002527 if (__cap == __sz)
2528 {
2529 __grow_by(__cap, 1, __sz, __ip, 0, 1);
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002530 __p = _VSTD::__to_raw_pointer(__get_long_pointer());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002531 }
2532 else
2533 {
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002534 __p = _VSTD::__to_raw_pointer(__get_pointer());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002535 size_type __n_move = __sz - __ip;
2536 if (__n_move != 0)
2537 traits_type::move(__p + __ip + 1, __p + __ip, __n_move);
2538 }
2539 traits_type::assign(__p[__ip], __c);
2540 traits_type::assign(__p[++__sz], value_type());
2541 __set_size(__sz);
2542 return begin() + static_cast<difference_type>(__ip);
2543}
2544
2545template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002546inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002547typename basic_string<_CharT, _Traits, _Allocator>::iterator
2548basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, size_type __n, value_type __c)
2549{
Howard Hinnant499cea12013-08-23 17:37:05 +00002550#if _LIBCPP_DEBUG_LEVEL >= 2
2551 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2552 "string::insert(iterator, n, value) called with an iterator not"
2553 " referring to this string");
2554#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002555 difference_type __p = __pos - begin();
2556 insert(static_cast<size_type>(__p), __n, __c);
2557 return begin() + __p;
2558}
2559
2560// replace
2561
2562template <class _CharT, class _Traits, class _Allocator>
2563basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002564basic_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 +00002565 _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002566{
Alp Tokerec34c482014-05-15 11:27:39 +00002567 _LIBCPP_ASSERT(__n2 == 0 || __s != nullptr, "string::replace received nullptr");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002568 size_type __sz = size();
2569 if (__pos > __sz)
2570 this->__throw_out_of_range();
Howard Hinnant0949eed2011-06-30 21:18:19 +00002571 __n1 = _VSTD::min(__n1, __sz - __pos);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002572 size_type __cap = capacity();
2573 if (__cap - __sz + __n1 >= __n2)
2574 {
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002575 value_type* __p = _VSTD::__to_raw_pointer(__get_pointer());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002576 if (__n1 != __n2)
2577 {
2578 size_type __n_move = __sz - __pos - __n1;
2579 if (__n_move != 0)
2580 {
2581 if (__n1 > __n2)
2582 {
2583 traits_type::move(__p + __pos, __s, __n2);
2584 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
2585 goto __finish;
2586 }
2587 if (__p + __pos < __s && __s < __p + __sz)
2588 {
2589 if (__p + __pos + __n1 <= __s)
2590 __s += __n2 - __n1;
2591 else // __p + __pos < __s < __p + __pos + __n1
2592 {
2593 traits_type::move(__p + __pos, __s, __n1);
2594 __pos += __n1;
2595 __s += __n2;
2596 __n2 -= __n1;
2597 __n1 = 0;
2598 }
2599 }
2600 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
2601 }
2602 }
2603 traits_type::move(__p + __pos, __s, __n2);
2604__finish:
Eric Fiselier3b7c1342017-03-09 01:54:13 +00002605// __sz += __n2 - __n1; in this and the below function below can cause unsigned integer overflow,
2606// but this is a safe operation, so we disable the check.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002607 __sz += __n2 - __n1;
2608 __set_size(__sz);
2609 __invalidate_iterators_past(__sz);
2610 traits_type::assign(__p[__sz], value_type());
2611 }
2612 else
2613 __grow_by_and_replace(__cap, __sz - __n1 + __n2 - __cap, __sz, __pos, __n1, __n2, __s);
2614 return *this;
2615}
2616
2617template <class _CharT, class _Traits, class _Allocator>
2618basic_string<_CharT, _Traits, _Allocator>&
2619basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos, size_type __n1, size_type __n2, value_type __c)
Eric Fiselier3b7c1342017-03-09 01:54:13 +00002620 _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002621{
2622 size_type __sz = size();
2623 if (__pos > __sz)
2624 this->__throw_out_of_range();
Howard Hinnant0949eed2011-06-30 21:18:19 +00002625 __n1 = _VSTD::min(__n1, __sz - __pos);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002626 size_type __cap = capacity();
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002627 value_type* __p;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002628 if (__cap - __sz + __n1 >= __n2)
2629 {
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002630 __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 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
2636 }
2637 }
2638 else
2639 {
2640 __grow_by(__cap, __sz - __n1 + __n2 - __cap, __sz, __pos, __n1, __n2);
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002641 __p = _VSTD::__to_raw_pointer(__get_long_pointer());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002642 }
2643 traits_type::assign(__p + __pos, __n2, __c);
2644 __sz += __n2 - __n1;
2645 __set_size(__sz);
2646 __invalidate_iterators_past(__sz);
2647 traits_type::assign(__p[__sz], value_type());
2648 return *this;
2649}
2650
2651template <class _CharT, class _Traits, class _Allocator>
2652template<class _InputIterator>
2653typename enable_if
2654<
2655 __is_input_iterator<_InputIterator>::value,
2656 basic_string<_CharT, _Traits, _Allocator>&
2657>::type
Howard Hinnant7b2cb482010-11-17 21:11:40 +00002658basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002659 _InputIterator __j1, _InputIterator __j2)
2660{
Marshall Clowd979eed2016-09-05 01:54:30 +00002661 const basic_string __temp(__j1, __j2, __alloc());
Marshall Clowdf9db312016-01-13 21:54:34 +00002662 return this->replace(__i1, __i2, __temp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002663}
2664
2665template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002666inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002667basic_string<_CharT, _Traits, _Allocator>&
2668basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const basic_string& __str)
2669{
2670 return replace(__pos1, __n1, __str.data(), __str.size());
2671}
2672
2673template <class _CharT, class _Traits, class _Allocator>
2674basic_string<_CharT, _Traits, _Allocator>&
2675basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const basic_string& __str,
2676 size_type __pos2, size_type __n2)
2677{
2678 size_type __str_sz = __str.size();
2679 if (__pos2 > __str_sz)
2680 this->__throw_out_of_range();
Howard Hinnant0949eed2011-06-30 21:18:19 +00002681 return replace(__pos1, __n1, __str.data() + __pos2, _VSTD::min(__n2, __str_sz - __pos2));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002682}
2683
2684template <class _CharT, class _Traits, class _Allocator>
Marshall Clow6ac8de02016-09-24 22:45:42 +00002685template <class _Tp>
2686typename enable_if
2687<
2688 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
2689 basic_string<_CharT, _Traits, _Allocator>&
2690>::type
2691basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const _Tp& __t,
Marshall Clow1e00d6d2016-07-21 05:31:24 +00002692 size_type __pos2, size_type __n2)
2693{
Marshall Clow6ac8de02016-09-24 22:45:42 +00002694 __self_view __sv = __t;
Marshall Clow1e00d6d2016-07-21 05:31:24 +00002695 size_type __str_sz = __sv.size();
2696 if (__pos2 > __str_sz)
2697 this->__throw_out_of_range();
2698 return replace(__pos1, __n1, __sv.data() + __pos2, _VSTD::min(__n2, __str_sz - __pos2));
2699}
2700
2701template <class _CharT, class _Traits, class _Allocator>
2702basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002703basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos, size_type __n1, const value_type* __s)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002704{
Alp Tokerec34c482014-05-15 11:27:39 +00002705 _LIBCPP_ASSERT(__s != nullptr, "string::replace received nullptr");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002706 return replace(__pos, __n1, __s, traits_type::length(__s));
2707}
2708
2709template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002710inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002711basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant7b2cb482010-11-17 21:11:40 +00002712basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, const basic_string& __str)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002713{
2714 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1),
2715 __str.data(), __str.size());
2716}
2717
2718template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002719inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002720basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002721basic_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 +00002722{
2723 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __s, __n);
2724}
2725
2726template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002727inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002728basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002729basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, const value_type* __s)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002730{
2731 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __s);
2732}
2733
2734template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002735inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002736basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant7b2cb482010-11-17 21:11:40 +00002737basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, size_type __n, value_type __c)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002738{
2739 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __n, __c);
2740}
2741
2742// erase
2743
2744template <class _CharT, class _Traits, class _Allocator>
2745basic_string<_CharT, _Traits, _Allocator>&
2746basic_string<_CharT, _Traits, _Allocator>::erase(size_type __pos, size_type __n)
2747{
2748 size_type __sz = size();
2749 if (__pos > __sz)
2750 this->__throw_out_of_range();
2751 if (__n)
2752 {
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002753 value_type* __p = _VSTD::__to_raw_pointer(__get_pointer());
Howard Hinnant0949eed2011-06-30 21:18:19 +00002754 __n = _VSTD::min(__n, __sz - __pos);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002755 size_type __n_move = __sz - __pos - __n;
2756 if (__n_move != 0)
2757 traits_type::move(__p + __pos, __p + __pos + __n, __n_move);
2758 __sz -= __n;
2759 __set_size(__sz);
2760 __invalidate_iterators_past(__sz);
2761 traits_type::assign(__p[__sz], value_type());
2762 }
2763 return *this;
2764}
2765
2766template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002767inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002768typename basic_string<_CharT, _Traits, _Allocator>::iterator
2769basic_string<_CharT, _Traits, _Allocator>::erase(const_iterator __pos)
2770{
Howard Hinnant499cea12013-08-23 17:37:05 +00002771#if _LIBCPP_DEBUG_LEVEL >= 2
2772 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2773 "string::erase(iterator) called with an iterator not"
2774 " referring to this string");
2775#endif
2776 _LIBCPP_ASSERT(__pos != end(),
2777 "string::erase(iterator) called with a non-dereferenceable iterator");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002778 iterator __b = begin();
2779 size_type __r = static_cast<size_type>(__pos - __b);
2780 erase(__r, 1);
Howard Hinnantec3773c2011-12-01 20:21:04 +00002781 return __b + static_cast<difference_type>(__r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002782}
2783
2784template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002785inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002786typename basic_string<_CharT, _Traits, _Allocator>::iterator
2787basic_string<_CharT, _Traits, _Allocator>::erase(const_iterator __first, const_iterator __last)
2788{
Howard Hinnant499cea12013-08-23 17:37:05 +00002789#if _LIBCPP_DEBUG_LEVEL >= 2
2790 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__first) == this,
2791 "string::erase(iterator, iterator) called with an iterator not"
2792 " referring to this string");
2793#endif
2794 _LIBCPP_ASSERT(__first <= __last, "string::erase(first, last) called with invalid range");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002795 iterator __b = begin();
2796 size_type __r = static_cast<size_type>(__first - __b);
2797 erase(__r, static_cast<size_type>(__last - __first));
Howard Hinnantec3773c2011-12-01 20:21:04 +00002798 return __b + static_cast<difference_type>(__r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002799}
2800
2801template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002802inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002803void
2804basic_string<_CharT, _Traits, _Allocator>::pop_back()
2805{
Howard Hinnant499cea12013-08-23 17:37:05 +00002806 _LIBCPP_ASSERT(!empty(), "string::pop_back(): string is already empty");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002807 size_type __sz;
2808 if (__is_long())
2809 {
2810 __sz = __get_long_size() - 1;
2811 __set_long_size(__sz);
2812 traits_type::assign(*(__get_long_pointer() + __sz), value_type());
2813 }
2814 else
2815 {
2816 __sz = __get_short_size() - 1;
2817 __set_short_size(__sz);
2818 traits_type::assign(*(__get_short_pointer() + __sz), value_type());
2819 }
2820 __invalidate_iterators_past(__sz);
2821}
2822
2823template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002824inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002825void
Howard Hinnanta6119a82011-05-29 19:57:12 +00002826basic_string<_CharT, _Traits, _Allocator>::clear() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002827{
2828 __invalidate_all_iterators();
2829 if (__is_long())
2830 {
2831 traits_type::assign(*__get_long_pointer(), value_type());
2832 __set_long_size(0);
2833 }
2834 else
2835 {
2836 traits_type::assign(*__get_short_pointer(), value_type());
2837 __set_short_size(0);
2838 }
2839}
2840
2841template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002842inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002843void
2844basic_string<_CharT, _Traits, _Allocator>::__erase_to_end(size_type __pos)
2845{
2846 if (__is_long())
2847 {
2848 traits_type::assign(*(__get_long_pointer() + __pos), value_type());
2849 __set_long_size(__pos);
2850 }
2851 else
2852 {
2853 traits_type::assign(*(__get_short_pointer() + __pos), value_type());
2854 __set_short_size(__pos);
2855 }
2856 __invalidate_iterators_past(__pos);
2857}
2858
2859template <class _CharT, class _Traits, class _Allocator>
2860void
2861basic_string<_CharT, _Traits, _Allocator>::resize(size_type __n, value_type __c)
2862{
2863 size_type __sz = size();
2864 if (__n > __sz)
2865 append(__n - __sz, __c);
2866 else
2867 __erase_to_end(__n);
2868}
2869
2870template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002871inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002872typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnanta6119a82011-05-29 19:57:12 +00002873basic_string<_CharT, _Traits, _Allocator>::max_size() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002874{
Howard Hinnante32b5e22010-11-17 17:55:08 +00002875 size_type __m = __alloc_traits::max_size(__alloc());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002876#if _LIBCPP_BIG_ENDIAN
Marshall Clow09f85502013-10-31 17:23:08 +00002877 return (__m <= ~__long_mask ? __m : __m/2) - __alignment;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002878#else
Marshall Clow09f85502013-10-31 17:23:08 +00002879 return __m - __alignment;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002880#endif
2881}
2882
2883template <class _CharT, class _Traits, class _Allocator>
2884void
2885basic_string<_CharT, _Traits, _Allocator>::reserve(size_type __res_arg)
2886{
2887 if (__res_arg > max_size())
2888 this->__throw_length_error();
2889 size_type __cap = capacity();
2890 size_type __sz = size();
Howard Hinnant0949eed2011-06-30 21:18:19 +00002891 __res_arg = _VSTD::max(__res_arg, __sz);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002892 __res_arg = __recommend(__res_arg);
2893 if (__res_arg != __cap)
2894 {
2895 pointer __new_data, __p;
2896 bool __was_long, __now_long;
2897 if (__res_arg == __min_cap - 1)
2898 {
2899 __was_long = true;
2900 __now_long = false;
2901 __new_data = __get_short_pointer();
2902 __p = __get_long_pointer();
2903 }
2904 else
2905 {
2906 if (__res_arg > __cap)
Howard Hinnante32b5e22010-11-17 17:55:08 +00002907 __new_data = __alloc_traits::allocate(__alloc(), __res_arg+1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002908 else
2909 {
2910 #ifndef _LIBCPP_NO_EXCEPTIONS
2911 try
2912 {
Howard Hinnant324bb032010-08-22 00:02:43 +00002913 #endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnante32b5e22010-11-17 17:55:08 +00002914 __new_data = __alloc_traits::allocate(__alloc(), __res_arg+1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002915 #ifndef _LIBCPP_NO_EXCEPTIONS
2916 }
2917 catch (...)
2918 {
2919 return;
2920 }
Howard Hinnant324bb032010-08-22 00:02:43 +00002921 #else // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002922 if (__new_data == nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002923 return;
Howard Hinnant324bb032010-08-22 00:02:43 +00002924 #endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002925 }
2926 __now_long = true;
2927 __was_long = __is_long();
2928 __p = __get_pointer();
2929 }
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002930 traits_type::copy(_VSTD::__to_raw_pointer(__new_data),
2931 _VSTD::__to_raw_pointer(__p), size()+1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002932 if (__was_long)
Howard Hinnante32b5e22010-11-17 17:55:08 +00002933 __alloc_traits::deallocate(__alloc(), __p, __cap+1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002934 if (__now_long)
2935 {
2936 __set_long_cap(__res_arg+1);
2937 __set_long_size(__sz);
2938 __set_long_pointer(__new_data);
2939 }
2940 else
2941 __set_short_size(__sz);
2942 __invalidate_all_iterators();
2943 }
2944}
2945
2946template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002947inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002948typename basic_string<_CharT, _Traits, _Allocator>::const_reference
Marshall Clow1e00d6d2016-07-21 05:31:24 +00002949basic_string<_CharT, _Traits, _Allocator>::operator[](size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002950{
Howard Hinnant499cea12013-08-23 17:37:05 +00002951 _LIBCPP_ASSERT(__pos <= size(), "string index out of bounds");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002952 return *(data() + __pos);
2953}
2954
2955template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002956inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002957typename basic_string<_CharT, _Traits, _Allocator>::reference
Marshall Clow1e00d6d2016-07-21 05:31:24 +00002958basic_string<_CharT, _Traits, _Allocator>::operator[](size_type __pos) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002959{
Howard Hinnant499cea12013-08-23 17:37:05 +00002960 _LIBCPP_ASSERT(__pos <= size(), "string index out of bounds");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002961 return *(__get_pointer() + __pos);
2962}
2963
2964template <class _CharT, class _Traits, class _Allocator>
2965typename basic_string<_CharT, _Traits, _Allocator>::const_reference
2966basic_string<_CharT, _Traits, _Allocator>::at(size_type __n) const
2967{
2968 if (__n >= size())
2969 this->__throw_out_of_range();
2970 return (*this)[__n];
2971}
2972
2973template <class _CharT, class _Traits, class _Allocator>
2974typename basic_string<_CharT, _Traits, _Allocator>::reference
2975basic_string<_CharT, _Traits, _Allocator>::at(size_type __n)
2976{
2977 if (__n >= size())
2978 this->__throw_out_of_range();
2979 return (*this)[__n];
2980}
2981
2982template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002983inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002984typename basic_string<_CharT, _Traits, _Allocator>::reference
2985basic_string<_CharT, _Traits, _Allocator>::front()
2986{
Howard Hinnant499cea12013-08-23 17:37:05 +00002987 _LIBCPP_ASSERT(!empty(), "string::front(): string is empty");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002988 return *__get_pointer();
2989}
2990
2991template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002992inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002993typename basic_string<_CharT, _Traits, _Allocator>::const_reference
2994basic_string<_CharT, _Traits, _Allocator>::front() const
2995{
Howard Hinnant499cea12013-08-23 17:37:05 +00002996 _LIBCPP_ASSERT(!empty(), "string::front(): string is empty");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002997 return *data();
2998}
2999
3000template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003001inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003002typename basic_string<_CharT, _Traits, _Allocator>::reference
3003basic_string<_CharT, _Traits, _Allocator>::back()
3004{
Howard Hinnant499cea12013-08-23 17:37:05 +00003005 _LIBCPP_ASSERT(!empty(), "string::back(): string is empty");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003006 return *(__get_pointer() + size() - 1);
3007}
3008
3009template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003010inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003011typename basic_string<_CharT, _Traits, _Allocator>::const_reference
3012basic_string<_CharT, _Traits, _Allocator>::back() const
3013{
Howard Hinnant499cea12013-08-23 17:37:05 +00003014 _LIBCPP_ASSERT(!empty(), "string::back(): string is empty");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003015 return *(data() + size() - 1);
3016}
3017
3018template <class _CharT, class _Traits, class _Allocator>
3019typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00003020basic_string<_CharT, _Traits, _Allocator>::copy(value_type* __s, size_type __n, size_type __pos) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003021{
3022 size_type __sz = size();
3023 if (__pos > __sz)
3024 this->__throw_out_of_range();
Howard Hinnant0949eed2011-06-30 21:18:19 +00003025 size_type __rlen = _VSTD::min(__n, __sz - __pos);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003026 traits_type::copy(__s, data() + __pos, __rlen);
3027 return __rlen;
3028}
3029
3030template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003031inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003032basic_string<_CharT, _Traits, _Allocator>
3033basic_string<_CharT, _Traits, _Allocator>::substr(size_type __pos, size_type __n) const
3034{
3035 return basic_string(*this, __pos, __n, __alloc());
3036}
3037
3038template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003039inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003040void
3041basic_string<_CharT, _Traits, _Allocator>::swap(basic_string& __str)
Marshall Clow7d914d12015-07-13 20:04:56 +00003042#if _LIBCPP_STD_VER >= 14
Eric Fiselier47257c42016-12-28 05:53:01 +00003043 _NOEXCEPT_DEBUG
Marshall Clow7d914d12015-07-13 20:04:56 +00003044#else
Eric Fiselier47257c42016-12-28 05:53:01 +00003045 _NOEXCEPT_DEBUG_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow7d914d12015-07-13 20:04:56 +00003046 __is_nothrow_swappable<allocator_type>::value)
3047#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003048{
Howard Hinnant499cea12013-08-23 17:37:05 +00003049#if _LIBCPP_DEBUG_LEVEL >= 2
3050 if (!__is_long())
3051 __get_db()->__invalidate_all(this);
3052 if (!__str.__is_long())
3053 __get_db()->__invalidate_all(&__str);
3054 __get_db()->swap(this, &__str);
3055#endif
Eric Fiselier47257c42016-12-28 05:53:01 +00003056 _LIBCPP_ASSERT(
3057 __alloc_traits::propagate_on_container_swap::value ||
3058 __alloc_traits::is_always_equal::value ||
3059 __alloc() == __str.__alloc(), "swapping non-equal allocators");
Howard Hinnant0949eed2011-06-30 21:18:19 +00003060 _VSTD::swap(__r_.first(), __str.__r_.first());
Marshall Clow7d914d12015-07-13 20:04:56 +00003061 __swap_allocator(__alloc(), __str.__alloc());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003062}
3063
3064// find
3065
3066template <class _Traits>
3067struct _LIBCPP_HIDDEN __traits_eq
3068{
3069 typedef typename _Traits::char_type char_type;
Howard Hinnanta6119a82011-05-29 19:57:12 +00003070 _LIBCPP_INLINE_VISIBILITY
3071 bool operator()(const char_type& __x, const char_type& __y) _NOEXCEPT
3072 {return _Traits::eq(__x, __y);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003073};
3074
3075template<class _CharT, class _Traits, class _Allocator>
3076typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00003077basic_string<_CharT, _Traits, _Allocator>::find(const value_type* __s,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003078 size_type __pos,
3079 size_type __n) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003080{
Alp Tokerec34c482014-05-15 11:27:39 +00003081 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find(): received nullptr");
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003082 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clow360f3192014-06-02 02:22:49 +00003083 (data(), size(), __s, __pos, __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003084}
3085
3086template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003087inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003088typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnanta6119a82011-05-29 19:57:12 +00003089basic_string<_CharT, _Traits, _Allocator>::find(const basic_string& __str,
3090 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003091{
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003092 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clow360f3192014-06-02 02:22:49 +00003093 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003094}
3095
3096template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003097inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003098typename basic_string<_CharT, _Traits, _Allocator>::size_type
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003099basic_string<_CharT, _Traits, _Allocator>::find(__self_view __sv,
3100 size_type __pos) const _NOEXCEPT
3101{
3102 return __str_find<value_type, size_type, traits_type, npos>
3103 (data(), size(), __sv.data(), __pos, __sv.size());
3104}
3105
3106template<class _CharT, class _Traits, class _Allocator>
3107inline _LIBCPP_INLINE_VISIBILITY
3108typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00003109basic_string<_CharT, _Traits, _Allocator>::find(const value_type* __s,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003110 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003111{
Alp Tokerec34c482014-05-15 11:27:39 +00003112 _LIBCPP_ASSERT(__s != nullptr, "string::find(): received nullptr");
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003113 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clow360f3192014-06-02 02:22:49 +00003114 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003115}
3116
3117template<class _CharT, class _Traits, class _Allocator>
3118typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnanta6119a82011-05-29 19:57:12 +00003119basic_string<_CharT, _Traits, _Allocator>::find(value_type __c,
3120 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003121{
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003122 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clow360f3192014-06-02 02:22:49 +00003123 (data(), size(), __c, __pos);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003124}
3125
3126// rfind
3127
3128template<class _CharT, class _Traits, class _Allocator>
3129typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00003130basic_string<_CharT, _Traits, _Allocator>::rfind(const value_type* __s,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003131 size_type __pos,
3132 size_type __n) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003133{
Alp Tokerec34c482014-05-15 11:27:39 +00003134 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::rfind(): received nullptr");
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003135 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clow360f3192014-06-02 02:22:49 +00003136 (data(), size(), __s, __pos, __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003137}
3138
3139template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003140inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003141typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnanta6119a82011-05-29 19:57:12 +00003142basic_string<_CharT, _Traits, _Allocator>::rfind(const basic_string& __str,
3143 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003144{
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003145 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clow360f3192014-06-02 02:22:49 +00003146 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003147}
3148
3149template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003150inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003151typename basic_string<_CharT, _Traits, _Allocator>::size_type
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003152basic_string<_CharT, _Traits, _Allocator>::rfind(__self_view __sv,
3153 size_type __pos) const _NOEXCEPT
3154{
3155 return __str_rfind<value_type, size_type, traits_type, npos>
3156 (data(), size(), __sv.data(), __pos, __sv.size());
3157}
3158
3159template<class _CharT, class _Traits, class _Allocator>
3160inline _LIBCPP_INLINE_VISIBILITY
3161typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00003162basic_string<_CharT, _Traits, _Allocator>::rfind(const value_type* __s,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003163 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003164{
Alp Tokerec34c482014-05-15 11:27:39 +00003165 _LIBCPP_ASSERT(__s != nullptr, "string::rfind(): received nullptr");
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003166 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clow360f3192014-06-02 02:22:49 +00003167 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003168}
3169
3170template<class _CharT, class _Traits, class _Allocator>
3171typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnanta6119a82011-05-29 19:57:12 +00003172basic_string<_CharT, _Traits, _Allocator>::rfind(value_type __c,
3173 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003174{
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003175 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clow360f3192014-06-02 02:22:49 +00003176 (data(), size(), __c, __pos);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003177}
3178
3179// find_first_of
3180
3181template<class _CharT, class _Traits, class _Allocator>
3182typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00003183basic_string<_CharT, _Traits, _Allocator>::find_first_of(const value_type* __s,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003184 size_type __pos,
3185 size_type __n) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003186{
Alp Tokerec34c482014-05-15 11:27:39 +00003187 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_first_of(): received nullptr");
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003188 return __str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow8eb5acc2014-02-16 01:57:26 +00003189 (data(), size(), __s, __pos, __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003190}
3191
3192template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003193inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003194typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnanta6119a82011-05-29 19:57:12 +00003195basic_string<_CharT, _Traits, _Allocator>::find_first_of(const basic_string& __str,
3196 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003197{
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003198 return __str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow8eb5acc2014-02-16 01:57:26 +00003199 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003200}
3201
3202template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003203inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003204typename basic_string<_CharT, _Traits, _Allocator>::size_type
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003205basic_string<_CharT, _Traits, _Allocator>::find_first_of(__self_view __sv,
3206 size_type __pos) const _NOEXCEPT
3207{
3208 return __str_find_first_of<value_type, size_type, traits_type, npos>
3209 (data(), size(), __sv.data(), __pos, __sv.size());
3210}
3211
3212template<class _CharT, class _Traits, class _Allocator>
3213inline _LIBCPP_INLINE_VISIBILITY
3214typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00003215basic_string<_CharT, _Traits, _Allocator>::find_first_of(const value_type* __s,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003216 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003217{
Alp Tokerec34c482014-05-15 11:27:39 +00003218 _LIBCPP_ASSERT(__s != nullptr, "string::find_first_of(): received nullptr");
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003219 return __str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow8eb5acc2014-02-16 01:57:26 +00003220 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003221}
3222
3223template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003224inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003225typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnanta6119a82011-05-29 19:57:12 +00003226basic_string<_CharT, _Traits, _Allocator>::find_first_of(value_type __c,
3227 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003228{
3229 return find(__c, __pos);
3230}
3231
3232// find_last_of
3233
3234template<class _CharT, class _Traits, class _Allocator>
3235typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00003236basic_string<_CharT, _Traits, _Allocator>::find_last_of(const value_type* __s,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003237 size_type __pos,
3238 size_type __n) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003239{
Alp Tokerec34c482014-05-15 11:27:39 +00003240 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_last_of(): received nullptr");
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003241 return __str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow8eb5acc2014-02-16 01:57:26 +00003242 (data(), size(), __s, __pos, __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003243}
3244
3245template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003246inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003247typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnanta6119a82011-05-29 19:57:12 +00003248basic_string<_CharT, _Traits, _Allocator>::find_last_of(const basic_string& __str,
3249 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003250{
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003251 return __str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow8eb5acc2014-02-16 01:57:26 +00003252 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003253}
3254
3255template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003256inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003257typename basic_string<_CharT, _Traits, _Allocator>::size_type
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003258basic_string<_CharT, _Traits, _Allocator>::find_last_of(__self_view __sv,
3259 size_type __pos) const _NOEXCEPT
3260{
3261 return __str_find_last_of<value_type, size_type, traits_type, npos>
3262 (data(), size(), __sv.data(), __pos, __sv.size());
3263}
3264
3265template<class _CharT, class _Traits, class _Allocator>
3266inline _LIBCPP_INLINE_VISIBILITY
3267typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00003268basic_string<_CharT, _Traits, _Allocator>::find_last_of(const value_type* __s,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003269 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003270{
Alp Tokerec34c482014-05-15 11:27:39 +00003271 _LIBCPP_ASSERT(__s != nullptr, "string::find_last_of(): received nullptr");
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003272 return __str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow8eb5acc2014-02-16 01:57:26 +00003273 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003274}
3275
3276template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003277inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003278typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnanta6119a82011-05-29 19:57:12 +00003279basic_string<_CharT, _Traits, _Allocator>::find_last_of(value_type __c,
3280 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003281{
3282 return rfind(__c, __pos);
3283}
3284
3285// find_first_not_of
3286
3287template<class _CharT, class _Traits, class _Allocator>
3288typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00003289basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const value_type* __s,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003290 size_type __pos,
3291 size_type __n) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003292{
Alp Tokerec34c482014-05-15 11:27:39 +00003293 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_first_not_of(): received nullptr");
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003294 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow8eb5acc2014-02-16 01:57:26 +00003295 (data(), size(), __s, __pos, __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003296}
3297
3298template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003299inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003300typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnanta6119a82011-05-29 19:57:12 +00003301basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const basic_string& __str,
3302 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003303{
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003304 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow8eb5acc2014-02-16 01:57:26 +00003305 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003306}
3307
3308template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003309inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003310typename basic_string<_CharT, _Traits, _Allocator>::size_type
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003311basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(__self_view __sv,
3312 size_type __pos) const _NOEXCEPT
3313{
3314 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
3315 (data(), size(), __sv.data(), __pos, __sv.size());
3316}
3317
3318template<class _CharT, class _Traits, class _Allocator>
3319inline _LIBCPP_INLINE_VISIBILITY
3320typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00003321basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const value_type* __s,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003322 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003323{
Alp Tokerec34c482014-05-15 11:27:39 +00003324 _LIBCPP_ASSERT(__s != nullptr, "string::find_first_not_of(): received nullptr");
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003325 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow8eb5acc2014-02-16 01:57:26 +00003326 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003327}
3328
3329template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003330inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003331typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnanta6119a82011-05-29 19:57:12 +00003332basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(value_type __c,
3333 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003334{
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003335 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow8eb5acc2014-02-16 01:57:26 +00003336 (data(), size(), __c, __pos);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003337}
3338
3339// find_last_not_of
3340
3341template<class _CharT, class _Traits, class _Allocator>
3342typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00003343basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const value_type* __s,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003344 size_type __pos,
3345 size_type __n) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003346{
Alp Tokerec34c482014-05-15 11:27:39 +00003347 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_last_not_of(): received nullptr");
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003348 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow8eb5acc2014-02-16 01:57:26 +00003349 (data(), size(), __s, __pos, __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003350}
3351
3352template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003353inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003354typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnanta6119a82011-05-29 19:57:12 +00003355basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const basic_string& __str,
3356 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003357{
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003358 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow8eb5acc2014-02-16 01:57:26 +00003359 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003360}
3361
3362template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003363inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003364typename basic_string<_CharT, _Traits, _Allocator>::size_type
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003365basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(__self_view __sv,
3366 size_type __pos) const _NOEXCEPT
3367{
3368 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
3369 (data(), size(), __sv.data(), __pos, __sv.size());
3370}
3371
3372template<class _CharT, class _Traits, class _Allocator>
3373inline _LIBCPP_INLINE_VISIBILITY
3374typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00003375basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const value_type* __s,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003376 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003377{
Alp Tokerec34c482014-05-15 11:27:39 +00003378 _LIBCPP_ASSERT(__s != nullptr, "string::find_last_not_of(): received nullptr");
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003379 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow8eb5acc2014-02-16 01:57:26 +00003380 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003381}
3382
3383template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003384inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003385typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnanta6119a82011-05-29 19:57:12 +00003386basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(value_type __c,
3387 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003388{
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003389 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow8eb5acc2014-02-16 01:57:26 +00003390 (data(), size(), __c, __pos);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003391}
3392
3393// compare
3394
3395template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003396inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003397int
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003398basic_string<_CharT, _Traits, _Allocator>::compare(__self_view __sv) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003399{
Howard Hinnantfa06d752011-07-24 21:45:06 +00003400 size_t __lhs_sz = size();
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003401 size_t __rhs_sz = __sv.size();
3402 int __result = traits_type::compare(data(), __sv.data(),
Howard Hinnantfa06d752011-07-24 21:45:06 +00003403 _VSTD::min(__lhs_sz, __rhs_sz));
3404 if (__result != 0)
3405 return __result;
3406 if (__lhs_sz < __rhs_sz)
3407 return -1;
3408 if (__lhs_sz > __rhs_sz)
3409 return 1;
3410 return 0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003411}
3412
3413template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003414inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003415int
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003416basic_string<_CharT, _Traits, _Allocator>::compare(const basic_string& __str) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003417{
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003418 return compare(__self_view(__str));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003419}
3420
3421template <class _CharT, class _Traits, class _Allocator>
3422int
Howard Hinnanta6119a82011-05-29 19:57:12 +00003423basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3424 size_type __n1,
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00003425 const value_type* __s,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003426 size_type __n2) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003427{
Alp Tokerec34c482014-05-15 11:27:39 +00003428 _LIBCPP_ASSERT(__n2 == 0 || __s != nullptr, "string::compare(): received nullptr");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003429 size_type __sz = size();
3430 if (__pos1 > __sz || __n2 == npos)
3431 this->__throw_out_of_range();
Howard Hinnant0949eed2011-06-30 21:18:19 +00003432 size_type __rlen = _VSTD::min(__n1, __sz - __pos1);
3433 int __r = traits_type::compare(data() + __pos1, __s, _VSTD::min(__rlen, __n2));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003434 if (__r == 0)
3435 {
3436 if (__rlen < __n2)
3437 __r = -1;
3438 else if (__rlen > __n2)
3439 __r = 1;
3440 }
3441 return __r;
3442}
3443
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003444template <class _CharT, class _Traits, class _Allocator>
3445inline _LIBCPP_INLINE_VISIBILITY
3446int
3447basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3448 size_type __n1,
3449 __self_view __sv) const
3450{
3451 return compare(__pos1, __n1, __sv.data(), __sv.size());
3452}
3453
3454template <class _CharT, class _Traits, class _Allocator>
3455inline _LIBCPP_INLINE_VISIBILITY
3456int
3457basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3458 size_type __n1,
3459 const basic_string& __str) const
3460{
3461 return compare(__pos1, __n1, __str.data(), __str.size());
3462}
3463
3464template <class _CharT, class _Traits, class _Allocator>
Marshall Clow6ac8de02016-09-24 22:45:42 +00003465template <class _Tp>
3466typename enable_if
3467<
3468 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3469 int
3470>::type
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003471basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3472 size_type __n1,
Marshall Clow6ac8de02016-09-24 22:45:42 +00003473 const _Tp& __t,
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003474 size_type __pos2,
3475 size_type __n2) const
3476{
Marshall Clow6ac8de02016-09-24 22:45:42 +00003477 __self_view __sv = __t;
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003478 return __self_view(*this).substr(__pos1, __n1).compare(__sv.substr(__pos2, __n2));
3479}
3480
3481template <class _CharT, class _Traits, class _Allocator>
3482int
3483basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3484 size_type __n1,
3485 const basic_string& __str,
3486 size_type __pos2,
3487 size_type __n2) const
3488{
3489 return compare(__pos1, __n1, __self_view(__str), __pos2, __n2);
3490}
3491
3492template <class _CharT, class _Traits, class _Allocator>
3493int
3494basic_string<_CharT, _Traits, _Allocator>::compare(const value_type* __s) const _NOEXCEPT
3495{
3496 _LIBCPP_ASSERT(__s != nullptr, "string::compare(): received nullptr");
3497 return compare(0, npos, __s, traits_type::length(__s));
3498}
3499
3500template <class _CharT, class _Traits, class _Allocator>
3501int
3502basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3503 size_type __n1,
3504 const value_type* __s) const
3505{
3506 _LIBCPP_ASSERT(__s != nullptr, "string::compare(): received nullptr");
3507 return compare(__pos1, __n1, __s, traits_type::length(__s));
3508}
3509
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003510// __invariants
3511
3512template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003513inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003514bool
3515basic_string<_CharT, _Traits, _Allocator>::__invariants() const
3516{
3517 if (size() > capacity())
3518 return false;
3519 if (capacity() < __min_cap - 1)
3520 return false;
3521 if (data() == 0)
3522 return false;
3523 if (data()[size()] != value_type(0))
3524 return false;
3525 return true;
3526}
3527
3528// operator==
3529
3530template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003531inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003532bool
3533operator==(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003534 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003535{
Howard Hinnant08dd2532013-04-22 23:55:13 +00003536 size_t __lhs_sz = __lhs.size();
3537 return __lhs_sz == __rhs.size() && _Traits::compare(__lhs.data(),
3538 __rhs.data(),
3539 __lhs_sz) == 0;
3540}
3541
3542template<class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003543inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant08dd2532013-04-22 23:55:13 +00003544bool
3545operator==(const basic_string<char, char_traits<char>, _Allocator>& __lhs,
3546 const basic_string<char, char_traits<char>, _Allocator>& __rhs) _NOEXCEPT
3547{
3548 size_t __lhs_sz = __lhs.size();
3549 if (__lhs_sz != __rhs.size())
3550 return false;
3551 const char* __lp = __lhs.data();
3552 const char* __rp = __rhs.data();
3553 if (__lhs.__is_long())
3554 return char_traits<char>::compare(__lp, __rp, __lhs_sz) == 0;
3555 for (; __lhs_sz != 0; --__lhs_sz, ++__lp, ++__rp)
3556 if (*__lp != *__rp)
3557 return false;
3558 return true;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003559}
3560
3561template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003562inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003563bool
Howard Hinnanta6119a82011-05-29 19:57:12 +00003564operator==(const _CharT* __lhs,
3565 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003566{
Eric Fiselier4f241822015-08-28 03:02:37 +00003567 typedef basic_string<_CharT, _Traits, _Allocator> _String;
3568 _LIBCPP_ASSERT(__lhs != nullptr, "operator==(char*, basic_string): received nullptr");
3569 size_t __lhs_len = _Traits::length(__lhs);
3570 if (__lhs_len != __rhs.size()) return false;
3571 return __rhs.compare(0, _String::npos, __lhs, __lhs_len) == 0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003572}
3573
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003574template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003575inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003576bool
Howard Hinnanta6119a82011-05-29 19:57:12 +00003577operator==(const basic_string<_CharT,_Traits,_Allocator>& __lhs,
3578 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003579{
Eric Fiselier4f241822015-08-28 03:02:37 +00003580 typedef basic_string<_CharT, _Traits, _Allocator> _String;
3581 _LIBCPP_ASSERT(__rhs != nullptr, "operator==(basic_string, char*): received nullptr");
3582 size_t __rhs_len = _Traits::length(__rhs);
3583 if (__rhs_len != __lhs.size()) return false;
3584 return __lhs.compare(0, _String::npos, __rhs, __rhs_len) == 0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003585}
3586
Howard Hinnant324bb032010-08-22 00:02:43 +00003587template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003588inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003589bool
3590operator!=(const basic_string<_CharT,_Traits,_Allocator>& __lhs,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003591 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003592{
3593 return !(__lhs == __rhs);
3594}
3595
3596template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003597inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003598bool
Howard Hinnanta6119a82011-05-29 19:57:12 +00003599operator!=(const _CharT* __lhs,
3600 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003601{
3602 return !(__lhs == __rhs);
3603}
3604
3605template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003606inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003607bool
Howard Hinnanta6119a82011-05-29 19:57:12 +00003608operator!=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
3609 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003610{
3611 return !(__lhs == __rhs);
3612}
3613
3614// operator<
3615
3616template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003617inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003618bool
3619operator< (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003620 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003621{
Howard Hinnant2644a7b2011-07-24 15:07:21 +00003622 return __lhs.compare(__rhs) < 0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003623}
3624
3625template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003626inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003627bool
Howard Hinnanta6119a82011-05-29 19:57:12 +00003628operator< (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
3629 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003630{
Howard Hinnant2644a7b2011-07-24 15:07:21 +00003631 return __lhs.compare(__rhs) < 0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003632}
3633
3634template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003635inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003636bool
Howard Hinnanta6119a82011-05-29 19:57:12 +00003637operator< (const _CharT* __lhs,
3638 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003639{
3640 return __rhs.compare(__lhs) > 0;
3641}
3642
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003643// operator>
3644
3645template<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
3648operator> (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003649 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003650{
3651 return __rhs < __lhs;
3652}
3653
3654template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003655inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003656bool
Howard Hinnanta6119a82011-05-29 19:57:12 +00003657operator> (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
3658 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003659{
3660 return __rhs < __lhs;
3661}
3662
3663template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003664inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003665bool
Howard Hinnanta6119a82011-05-29 19:57:12 +00003666operator> (const _CharT* __lhs,
3667 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003668{
3669 return __rhs < __lhs;
3670}
3671
3672// operator<=
3673
3674template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003675inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003676bool
3677operator<=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003678 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003679{
3680 return !(__rhs < __lhs);
3681}
3682
3683template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003684inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003685bool
Howard Hinnanta6119a82011-05-29 19:57:12 +00003686operator<=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
3687 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003688{
3689 return !(__rhs < __lhs);
3690}
3691
3692template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003693inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003694bool
Howard Hinnanta6119a82011-05-29 19:57:12 +00003695operator<=(const _CharT* __lhs,
3696 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003697{
3698 return !(__rhs < __lhs);
3699}
3700
3701// operator>=
3702
3703template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003704inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003705bool
3706operator>=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003707 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003708{
3709 return !(__lhs < __rhs);
3710}
3711
3712template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003713inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003714bool
Howard Hinnanta6119a82011-05-29 19:57:12 +00003715operator>=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
3716 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003717{
3718 return !(__lhs < __rhs);
3719}
3720
3721template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003722inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003723bool
Howard Hinnanta6119a82011-05-29 19:57:12 +00003724operator>=(const _CharT* __lhs,
3725 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003726{
3727 return !(__lhs < __rhs);
3728}
3729
3730// operator +
3731
3732template<class _CharT, class _Traits, class _Allocator>
3733basic_string<_CharT, _Traits, _Allocator>
3734operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
3735 const basic_string<_CharT, _Traits, _Allocator>& __rhs)
3736{
3737 basic_string<_CharT, _Traits, _Allocator> __r(__lhs.get_allocator());
3738 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = __lhs.size();
3739 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = __rhs.size();
3740 __r.__init(__lhs.data(), __lhs_sz, __lhs_sz + __rhs_sz);
3741 __r.append(__rhs.data(), __rhs_sz);
3742 return __r;
3743}
3744
3745template<class _CharT, class _Traits, class _Allocator>
3746basic_string<_CharT, _Traits, _Allocator>
3747operator+(const _CharT* __lhs , const basic_string<_CharT,_Traits,_Allocator>& __rhs)
3748{
3749 basic_string<_CharT, _Traits, _Allocator> __r(__rhs.get_allocator());
3750 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = _Traits::length(__lhs);
3751 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = __rhs.size();
3752 __r.__init(__lhs, __lhs_sz, __lhs_sz + __rhs_sz);
3753 __r.append(__rhs.data(), __rhs_sz);
3754 return __r;
3755}
3756
3757template<class _CharT, class _Traits, class _Allocator>
3758basic_string<_CharT, _Traits, _Allocator>
3759operator+(_CharT __lhs, const basic_string<_CharT,_Traits,_Allocator>& __rhs)
3760{
3761 basic_string<_CharT, _Traits, _Allocator> __r(__rhs.get_allocator());
3762 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = __rhs.size();
3763 __r.__init(&__lhs, 1, 1 + __rhs_sz);
3764 __r.append(__rhs.data(), __rhs_sz);
3765 return __r;
3766}
3767
3768template<class _CharT, class _Traits, class _Allocator>
3769basic_string<_CharT, _Traits, _Allocator>
3770operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, const _CharT* __rhs)
3771{
3772 basic_string<_CharT, _Traits, _Allocator> __r(__lhs.get_allocator());
3773 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = __lhs.size();
3774 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = _Traits::length(__rhs);
3775 __r.__init(__lhs.data(), __lhs_sz, __lhs_sz + __rhs_sz);
3776 __r.append(__rhs, __rhs_sz);
3777 return __r;
3778}
3779
3780template<class _CharT, class _Traits, class _Allocator>
3781basic_string<_CharT, _Traits, _Allocator>
3782operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, _CharT __rhs)
3783{
3784 basic_string<_CharT, _Traits, _Allocator> __r(__lhs.get_allocator());
3785 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = __lhs.size();
3786 __r.__init(__lhs.data(), __lhs_sz, __lhs_sz + 1);
3787 __r.push_back(__rhs);
3788 return __r;
3789}
3790
Eric Fiselier3e928972017-04-19 00:28:44 +00003791#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003792
3793template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003794inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003795basic_string<_CharT, _Traits, _Allocator>
3796operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, const basic_string<_CharT, _Traits, _Allocator>& __rhs)
3797{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003798 return _VSTD::move(__lhs.append(__rhs));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003799}
3800
3801template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003802inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003803basic_string<_CharT, _Traits, _Allocator>
3804operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, basic_string<_CharT, _Traits, _Allocator>&& __rhs)
3805{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003806 return _VSTD::move(__rhs.insert(0, __lhs));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003807}
3808
3809template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003810inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003811basic_string<_CharT, _Traits, _Allocator>
3812operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, basic_string<_CharT, _Traits, _Allocator>&& __rhs)
3813{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003814 return _VSTD::move(__lhs.append(__rhs));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003815}
3816
3817template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003818inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003819basic_string<_CharT, _Traits, _Allocator>
3820operator+(const _CharT* __lhs , basic_string<_CharT,_Traits,_Allocator>&& __rhs)
3821{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003822 return _VSTD::move(__rhs.insert(0, __lhs));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003823}
3824
3825template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003826inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003827basic_string<_CharT, _Traits, _Allocator>
3828operator+(_CharT __lhs, basic_string<_CharT,_Traits,_Allocator>&& __rhs)
3829{
3830 __rhs.insert(__rhs.begin(), __lhs);
Howard Hinnant0949eed2011-06-30 21:18:19 +00003831 return _VSTD::move(__rhs);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003832}
3833
3834template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003835inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003836basic_string<_CharT, _Traits, _Allocator>
3837operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, const _CharT* __rhs)
3838{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003839 return _VSTD::move(__lhs.append(__rhs));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003840}
3841
3842template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003843inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003844basic_string<_CharT, _Traits, _Allocator>
3845operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, _CharT __rhs)
3846{
3847 __lhs.push_back(__rhs);
Howard Hinnant0949eed2011-06-30 21:18:19 +00003848 return _VSTD::move(__lhs);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003849}
3850
Eric Fiselier3e928972017-04-19 00:28:44 +00003851#endif // _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003852
3853// swap
3854
3855template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003856inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003857void
Howard Hinnanta6119a82011-05-29 19:57:12 +00003858swap(basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnant53f7d4c2011-06-03 18:40:47 +00003859 basic_string<_CharT, _Traits, _Allocator>& __rhs)
3860 _NOEXCEPT_(_NOEXCEPT_(__lhs.swap(__rhs)))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003861{
3862 __lhs.swap(__rhs);
3863}
3864
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003865#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
3866
3867typedef basic_string<char16_t> u16string;
3868typedef basic_string<char32_t> u32string;
3869
Howard Hinnant324bb032010-08-22 00:02:43 +00003870#endif // _LIBCPP_HAS_NO_UNICODE_CHARS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003871
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003872_LIBCPP_FUNC_VIS int stoi (const string& __str, size_t* __idx = 0, int __base = 10);
3873_LIBCPP_FUNC_VIS long stol (const string& __str, size_t* __idx = 0, int __base = 10);
3874_LIBCPP_FUNC_VIS unsigned long stoul (const string& __str, size_t* __idx = 0, int __base = 10);
3875_LIBCPP_FUNC_VIS long long stoll (const string& __str, size_t* __idx = 0, int __base = 10);
3876_LIBCPP_FUNC_VIS unsigned long long stoull(const string& __str, size_t* __idx = 0, int __base = 10);
Howard Hinnanta6a062d2010-06-02 18:20:39 +00003877
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003878_LIBCPP_FUNC_VIS float stof (const string& __str, size_t* __idx = 0);
3879_LIBCPP_FUNC_VIS double stod (const string& __str, size_t* __idx = 0);
3880_LIBCPP_FUNC_VIS long double stold(const string& __str, size_t* __idx = 0);
Howard Hinnanta6a062d2010-06-02 18:20:39 +00003881
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003882_LIBCPP_FUNC_VIS string to_string(int __val);
3883_LIBCPP_FUNC_VIS string to_string(unsigned __val);
3884_LIBCPP_FUNC_VIS string to_string(long __val);
3885_LIBCPP_FUNC_VIS string to_string(unsigned long __val);
3886_LIBCPP_FUNC_VIS string to_string(long long __val);
3887_LIBCPP_FUNC_VIS string to_string(unsigned long long __val);
3888_LIBCPP_FUNC_VIS string to_string(float __val);
3889_LIBCPP_FUNC_VIS string to_string(double __val);
3890_LIBCPP_FUNC_VIS string to_string(long double __val);
Howard Hinnanta6a062d2010-06-02 18:20:39 +00003891
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003892_LIBCPP_FUNC_VIS int stoi (const wstring& __str, size_t* __idx = 0, int __base = 10);
3893_LIBCPP_FUNC_VIS long stol (const wstring& __str, size_t* __idx = 0, int __base = 10);
3894_LIBCPP_FUNC_VIS unsigned long stoul (const wstring& __str, size_t* __idx = 0, int __base = 10);
3895_LIBCPP_FUNC_VIS long long stoll (const wstring& __str, size_t* __idx = 0, int __base = 10);
3896_LIBCPP_FUNC_VIS unsigned long long stoull(const wstring& __str, size_t* __idx = 0, int __base = 10);
Howard Hinnanta6a062d2010-06-02 18:20:39 +00003897
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003898_LIBCPP_FUNC_VIS float stof (const wstring& __str, size_t* __idx = 0);
3899_LIBCPP_FUNC_VIS double stod (const wstring& __str, size_t* __idx = 0);
3900_LIBCPP_FUNC_VIS long double stold(const wstring& __str, size_t* __idx = 0);
Howard Hinnanta6a062d2010-06-02 18:20:39 +00003901
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003902_LIBCPP_FUNC_VIS wstring to_wstring(int __val);
3903_LIBCPP_FUNC_VIS wstring to_wstring(unsigned __val);
3904_LIBCPP_FUNC_VIS wstring to_wstring(long __val);
3905_LIBCPP_FUNC_VIS wstring to_wstring(unsigned long __val);
3906_LIBCPP_FUNC_VIS wstring to_wstring(long long __val);
3907_LIBCPP_FUNC_VIS wstring to_wstring(unsigned long long __val);
3908_LIBCPP_FUNC_VIS wstring to_wstring(float __val);
3909_LIBCPP_FUNC_VIS wstring to_wstring(double __val);
3910_LIBCPP_FUNC_VIS wstring to_wstring(long double __val);
Howard Hinnanta6a062d2010-06-02 18:20:39 +00003911
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003912template<class _CharT, class _Traits, class _Allocator>
3913 const typename basic_string<_CharT, _Traits, _Allocator>::size_type
3914 basic_string<_CharT, _Traits, _Allocator>::npos;
3915
3916template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierc3589a82017-01-04 23:56:00 +00003917struct _LIBCPP_TEMPLATE_VIS hash<basic_string<_CharT, _Traits, _Allocator> >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003918 : public unary_function<basic_string<_CharT, _Traits, _Allocator>, size_t>
3919{
3920 size_t
Howard Hinnanta6119a82011-05-29 19:57:12 +00003921 operator()(const basic_string<_CharT, _Traits, _Allocator>& __val) const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003922};
3923
3924template<class _CharT, class _Traits, class _Allocator>
3925size_t
3926hash<basic_string<_CharT, _Traits, _Allocator> >::operator()(
Howard Hinnanta6119a82011-05-29 19:57:12 +00003927 const basic_string<_CharT, _Traits, _Allocator>& __val) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003928{
Sean Huntaffd9e52011-07-29 23:31:56 +00003929 return __do_string_hash(__val.data(), __val.data() + __val.size());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003930}
3931
Howard Hinnant464aa5c2011-07-18 15:51:59 +00003932template<class _CharT, class _Traits, class _Allocator>
3933basic_ostream<_CharT, _Traits>&
3934operator<<(basic_ostream<_CharT, _Traits>& __os,
3935 const basic_string<_CharT, _Traits, _Allocator>& __str);
3936
3937template<class _CharT, class _Traits, class _Allocator>
3938basic_istream<_CharT, _Traits>&
3939operator>>(basic_istream<_CharT, _Traits>& __is,
3940 basic_string<_CharT, _Traits, _Allocator>& __str);
3941
3942template<class _CharT, class _Traits, class _Allocator>
3943basic_istream<_CharT, _Traits>&
3944getline(basic_istream<_CharT, _Traits>& __is,
3945 basic_string<_CharT, _Traits, _Allocator>& __str, _CharT __dlm);
3946
3947template<class _CharT, class _Traits, class _Allocator>
3948inline _LIBCPP_INLINE_VISIBILITY
3949basic_istream<_CharT, _Traits>&
3950getline(basic_istream<_CharT, _Traits>& __is,
3951 basic_string<_CharT, _Traits, _Allocator>& __str);
3952
Eric Fiselier3e928972017-04-19 00:28:44 +00003953#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant464aa5c2011-07-18 15:51:59 +00003954
3955template<class _CharT, class _Traits, class _Allocator>
3956inline _LIBCPP_INLINE_VISIBILITY
3957basic_istream<_CharT, _Traits>&
3958getline(basic_istream<_CharT, _Traits>&& __is,
3959 basic_string<_CharT, _Traits, _Allocator>& __str, _CharT __dlm);
3960
3961template<class _CharT, class _Traits, class _Allocator>
3962inline _LIBCPP_INLINE_VISIBILITY
3963basic_istream<_CharT, _Traits>&
3964getline(basic_istream<_CharT, _Traits>&& __is,
3965 basic_string<_CharT, _Traits, _Allocator>& __str);
3966
Eric Fiselier3e928972017-04-19 00:28:44 +00003967#endif // _LIBCPP_CXX03_LANG
Howard Hinnant464aa5c2011-07-18 15:51:59 +00003968
Howard Hinnant499cea12013-08-23 17:37:05 +00003969#if _LIBCPP_DEBUG_LEVEL >= 2
3970
3971template<class _CharT, class _Traits, class _Allocator>
3972bool
3973basic_string<_CharT, _Traits, _Allocator>::__dereferenceable(const const_iterator* __i) const
3974{
3975 return this->data() <= _VSTD::__to_raw_pointer(__i->base()) &&
3976 _VSTD::__to_raw_pointer(__i->base()) < this->data() + this->size();
3977}
3978
3979template<class _CharT, class _Traits, class _Allocator>
3980bool
3981basic_string<_CharT, _Traits, _Allocator>::__decrementable(const const_iterator* __i) const
3982{
3983 return this->data() < _VSTD::__to_raw_pointer(__i->base()) &&
3984 _VSTD::__to_raw_pointer(__i->base()) <= this->data() + this->size();
3985}
3986
3987template<class _CharT, class _Traits, class _Allocator>
3988bool
3989basic_string<_CharT, _Traits, _Allocator>::__addable(const const_iterator* __i, ptrdiff_t __n) const
3990{
3991 const value_type* __p = _VSTD::__to_raw_pointer(__i->base()) + __n;
3992 return this->data() <= __p && __p <= this->data() + this->size();
3993}
3994
3995template<class _CharT, class _Traits, class _Allocator>
3996bool
3997basic_string<_CharT, _Traits, _Allocator>::__subscriptable(const const_iterator* __i, ptrdiff_t __n) const
3998{
3999 const value_type* __p = _VSTD::__to_raw_pointer(__i->base()) + __n;
4000 return this->data() <= __p && __p < this->data() + this->size();
4001}
4002
4003#endif // _LIBCPP_DEBUG_LEVEL >= 2
4004
Marshall Clow15234322013-07-23 17:05:24 +00004005#if _LIBCPP_STD_VER > 11
4006// Literal suffixes for basic_string [basic.string.literals]
Marshall Clow8d9dd7a2013-10-05 21:18:32 +00004007inline namespace literals
Marshall Clow15234322013-07-23 17:05:24 +00004008{
4009 inline namespace string_literals
4010 {
Howard Hinnantab61b2c2013-08-07 19:39:48 +00004011 inline _LIBCPP_INLINE_VISIBILITY
4012 basic_string<char> operator "" s( const char *__str, size_t __len )
4013 {
4014 return basic_string<char> (__str, __len);
4015 }
Marshall Clow15234322013-07-23 17:05:24 +00004016
Howard Hinnantab61b2c2013-08-07 19:39:48 +00004017 inline _LIBCPP_INLINE_VISIBILITY
4018 basic_string<wchar_t> operator "" s( const wchar_t *__str, size_t __len )
4019 {
4020 return basic_string<wchar_t> (__str, __len);
4021 }
Marshall Clow15234322013-07-23 17:05:24 +00004022
Howard Hinnantab61b2c2013-08-07 19:39:48 +00004023 inline _LIBCPP_INLINE_VISIBILITY
4024 basic_string<char16_t> operator "" s( const char16_t *__str, size_t __len )
4025 {
4026 return basic_string<char16_t> (__str, __len);
4027 }
Marshall Clow15234322013-07-23 17:05:24 +00004028
Howard Hinnantab61b2c2013-08-07 19:39:48 +00004029 inline _LIBCPP_INLINE_VISIBILITY
4030 basic_string<char32_t> operator "" s( const char32_t *__str, size_t __len )
4031 {
4032 return basic_string<char32_t> (__str, __len);
4033 }
Marshall Clow15234322013-07-23 17:05:24 +00004034 }
4035}
4036#endif
4037
Eric Fiselier833d6442016-09-15 22:27:07 +00004038_LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_string<char>)
4039_LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_string<wchar_t>)
Howard Hinnant499cea12013-08-23 17:37:05 +00004040_LIBCPP_EXTERN_TEMPLATE(string operator+<char, char_traits<char>, allocator<char> >(char const*, string const&))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004041
4042_LIBCPP_END_NAMESPACE_STD
4043
4044#endif // _LIBCPP_STRING