blob: f86028869f47d3bbcbb2a97ec74fb2023c3077b4 [file] [log] [blame]
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001// -*- C++ -*-
2//===--------------------------- string -----------------------------------===//
3//
Howard Hinnantf5256e12010-05-11 21:36:01 +00004// The LLVM Compiler Infrastructure
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005//
6// This file is distributed under the University of Illinois Open Source
7// License. See LICENSE.TXT for details.
8//
9//===----------------------------------------------------------------------===//
10
11#ifndef _LIBCPP_STRING
12#define _LIBCPP_STRING
13
14/*
15 string synopsis
16
17namespace std
18{
19
20template <class stateT>
21class fpos
22{
23private:
24 stateT st;
25public:
26 fpos(streamoff = streamoff());
27
28 operator streamoff() const;
29
30 stateT state() const;
31 void state(stateT);
32
33 fpos& operator+=(streamoff);
34 fpos operator+ (streamoff) const;
35 fpos& operator-=(streamoff);
36 fpos operator- (streamoff) const;
37};
38
39template <class stateT> streamoff operator-(const fpos<stateT>& x, const fpos<stateT>& y);
40
41template <class stateT> bool operator==(const fpos<stateT>& x, const fpos<stateT>& y);
42template <class stateT> bool operator!=(const fpos<stateT>& x, const fpos<stateT>& y);
43
44template <class charT>
45struct char_traits
46{
47 typedef charT char_type;
48 typedef ... int_type;
49 typedef streamoff off_type;
50 typedef streampos pos_type;
51 typedef mbstate_t state_type;
52
Howard Hinnanta6119a82011-05-29 19:57:12 +000053 static void assign(char_type& c1, const char_type& c2) noexcept;
Howard Hinnant03d71812012-07-20 19:09:12 +000054 static constexpr bool eq(char_type c1, char_type c2) noexcept;
55 static constexpr bool lt(char_type c1, char_type c2) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000056
57 static int compare(const char_type* s1, const char_type* s2, size_t n);
58 static size_t length(const char_type* s);
59 static const char_type* find(const char_type* s, size_t n, const char_type& a);
60 static char_type* move(char_type* s1, const char_type* s2, size_t n);
61 static char_type* copy(char_type* s1, const char_type* s2, size_t n);
62 static char_type* assign(char_type* s, size_t n, char_type a);
63
Howard Hinnant03d71812012-07-20 19:09:12 +000064 static constexpr int_type not_eof(int_type c) noexcept;
65 static constexpr char_type to_char_type(int_type c) noexcept;
66 static constexpr int_type to_int_type(char_type c) noexcept;
67 static constexpr bool eq_int_type(int_type c1, int_type c2) noexcept;
68 static constexpr int_type eof() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000069};
70
71template <> struct char_traits<char>;
72template <> struct char_traits<wchar_t>;
73
Howard Hinnant324bb032010-08-22 00:02:43 +000074template<class charT, class traits = char_traits<charT>, class Allocator = allocator<charT> >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000075class basic_string
76{
Howard Hinnant324bb032010-08-22 00:02:43 +000077public:
78// types:
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000079 typedef traits traits_type;
80 typedef typename traits_type::char_type value_type;
81 typedef Allocator allocator_type;
82 typedef typename allocator_type::size_type size_type;
83 typedef typename allocator_type::difference_type difference_type;
84 typedef typename allocator_type::reference reference;
85 typedef typename allocator_type::const_reference const_reference;
86 typedef typename allocator_type::pointer pointer;
87 typedef typename allocator_type::const_pointer const_pointer;
88 typedef implementation-defined iterator;
89 typedef implementation-defined const_iterator;
90 typedef std::reverse_iterator<iterator> reverse_iterator;
91 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
92
93 static const size_type npos = -1;
94
Howard Hinnant53f7d4c2011-06-03 18:40:47 +000095 basic_string()
96 noexcept(is_nothrow_default_constructible<allocator_type>::value);
97 explicit basic_string(const allocator_type& a);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000098 basic_string(const basic_string& str);
Howard Hinnant53f7d4c2011-06-03 18:40:47 +000099 basic_string(basic_string&& str)
100 noexcept(is_nothrow_move_constructible<allocator_type>::value);
Marshall Clowf4f7d8f2016-04-07 18:13:41 +0000101 basic_string(const basic_string& str, size_type pos,
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000102 const allocator_type& a = allocator_type());
Marshall Clowf4f7d8f2016-04-07 18:13:41 +0000103 basic_string(const basic_string& str, size_type pos, size_type n,
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000104 const Allocator& a = Allocator());
Marshall Clowdb7fa112016-11-14 18:22:19 +0000105 template<class T>
106 basic_string(const T& t, size_type pos, size_type n, const Allocator& a = Allocator()); // C++17
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000107 explicit basic_string(const basic_string_view<charT, traits> sv, const Allocator& a = Allocator());
Howard Hinnant9dcdcde2013-06-28 16:59:19 +0000108 basic_string(const value_type* s, const allocator_type& a = allocator_type());
109 basic_string(const value_type* s, size_type n, const allocator_type& a = allocator_type());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000110 basic_string(size_type n, value_type c, const allocator_type& a = allocator_type());
111 template<class InputIterator>
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000112 basic_string(InputIterator begin, InputIterator end,
113 const allocator_type& a = allocator_type());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000114 basic_string(initializer_list<value_type>, const Allocator& = Allocator());
115 basic_string(const basic_string&, const Allocator&);
116 basic_string(basic_string&&, const Allocator&);
117
118 ~basic_string();
119
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000120 operator basic_string_view<charT, traits>() const noexcept;
121
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000122 basic_string& operator=(const basic_string& str);
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000123 basic_string& operator=(basic_string_view<charT, traits> sv);
Howard Hinnant53f7d4c2011-06-03 18:40:47 +0000124 basic_string& operator=(basic_string&& str)
125 noexcept(
Marshall Clowaf961ed2015-08-18 18:57:00 +0000126 allocator_type::propagate_on_container_move_assignment::value ||
127 allocator_type::is_always_equal::value ); // C++17
Howard Hinnant9dcdcde2013-06-28 16:59:19 +0000128 basic_string& operator=(const value_type* s);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000129 basic_string& operator=(value_type c);
130 basic_string& operator=(initializer_list<value_type>);
131
Howard Hinnanta6119a82011-05-29 19:57:12 +0000132 iterator begin() noexcept;
133 const_iterator begin() const noexcept;
134 iterator end() noexcept;
135 const_iterator end() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000136
Howard Hinnanta6119a82011-05-29 19:57:12 +0000137 reverse_iterator rbegin() noexcept;
138 const_reverse_iterator rbegin() const noexcept;
139 reverse_iterator rend() noexcept;
140 const_reverse_iterator rend() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000141
Howard Hinnanta6119a82011-05-29 19:57:12 +0000142 const_iterator cbegin() const noexcept;
143 const_iterator cend() const noexcept;
144 const_reverse_iterator crbegin() const noexcept;
145 const_reverse_iterator crend() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000146
Howard Hinnanta6119a82011-05-29 19:57:12 +0000147 size_type size() const noexcept;
148 size_type length() const noexcept;
149 size_type max_size() const noexcept;
150 size_type capacity() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000151
152 void resize(size_type n, value_type c);
153 void resize(size_type n);
154
155 void reserve(size_type res_arg = 0);
156 void shrink_to_fit();
Howard Hinnanta6119a82011-05-29 19:57:12 +0000157 void clear() noexcept;
158 bool empty() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000159
160 const_reference operator[](size_type pos) const;
161 reference operator[](size_type pos);
162
163 const_reference at(size_type n) const;
164 reference at(size_type n);
165
166 basic_string& operator+=(const basic_string& str);
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000167 basic_string& operator+=(basic_string_view<charT, traits> sv);
Howard Hinnant9dcdcde2013-06-28 16:59:19 +0000168 basic_string& operator+=(const value_type* s);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000169 basic_string& operator+=(value_type c);
170 basic_string& operator+=(initializer_list<value_type>);
171
172 basic_string& append(const basic_string& str);
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000173 basic_string& append(basic_string_view<charT, traits> sv);
Marshall Clowa93b5e22014-03-04 19:17:19 +0000174 basic_string& append(const basic_string& str, size_type pos, size_type n=npos); //C++14
Marshall Clow6ac8de02016-09-24 22:45:42 +0000175 template <class T>
176 basic_string& append(const T& t, size_type pos, size_type n=npos); // C++17
Howard Hinnant9dcdcde2013-06-28 16:59:19 +0000177 basic_string& append(const value_type* s, size_type n);
178 basic_string& append(const value_type* s);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000179 basic_string& append(size_type n, value_type c);
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000180 template<class InputIterator>
181 basic_string& append(InputIterator first, InputIterator last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000182 basic_string& append(initializer_list<value_type>);
183
184 void push_back(value_type c);
185 void pop_back();
186 reference front();
187 const_reference front() const;
188 reference back();
189 const_reference back() const;
190
191 basic_string& assign(const basic_string& str);
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000192 basic_string& assign(basic_string_view<charT, traits> sv);
Howard Hinnanta6119a82011-05-29 19:57:12 +0000193 basic_string& assign(basic_string&& str);
Marshall Clowa93b5e22014-03-04 19:17:19 +0000194 basic_string& assign(const basic_string& str, size_type pos, size_type n=npos); // C++14
Marshall Clow6ac8de02016-09-24 22:45:42 +0000195 template <class T>
196 basic_string& assign(const T& t, size_type pos, size_type n=npos); // C++17
Howard Hinnant9dcdcde2013-06-28 16:59:19 +0000197 basic_string& assign(const value_type* s, size_type n);
198 basic_string& assign(const value_type* s);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000199 basic_string& assign(size_type n, value_type c);
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000200 template<class InputIterator>
201 basic_string& assign(InputIterator first, InputIterator last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000202 basic_string& assign(initializer_list<value_type>);
203
204 basic_string& insert(size_type pos1, const basic_string& str);
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000205 basic_string& insert(size_type pos1, basic_string_view<charT, traits> sv);
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000206 basic_string& insert(size_type pos1, const basic_string& str,
207 size_type pos2, size_type n);
Marshall Clow6ac8de02016-09-24 22:45:42 +0000208 template <class T>
209 basic_string& insert(size_type pos1, const T& t, size_type pos2, size_type n); // C++17
Marshall Clowa93b5e22014-03-04 19:17:19 +0000210 basic_string& insert(size_type pos, const value_type* s, size_type n=npos); //C++14
Howard Hinnant9dcdcde2013-06-28 16:59:19 +0000211 basic_string& insert(size_type pos, const value_type* s);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000212 basic_string& insert(size_type pos, size_type n, value_type c);
213 iterator insert(const_iterator p, value_type c);
214 iterator insert(const_iterator p, size_type n, value_type c);
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000215 template<class InputIterator>
216 iterator insert(const_iterator p, InputIterator first, InputIterator last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000217 iterator insert(const_iterator p, initializer_list<value_type>);
218
219 basic_string& erase(size_type pos = 0, size_type n = npos);
220 iterator erase(const_iterator position);
221 iterator erase(const_iterator first, const_iterator last);
222
223 basic_string& replace(size_type pos1, size_type n1, const basic_string& str);
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000224 basic_string& replace(size_type pos1, size_type n1, basic_string_view<charT, traits> sv);
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000225 basic_string& replace(size_type pos1, size_type n1, const basic_string& str,
Marshall Clowa93b5e22014-03-04 19:17:19 +0000226 size_type pos2, size_type n2=npos); // C++14
Marshall Clow6ac8de02016-09-24 22:45:42 +0000227 template <class T>
228 basic_string& replace(size_type pos1, size_type n1, const T& t,
229 size_type pos2, size_type n); // C++17
Howard Hinnant9dcdcde2013-06-28 16:59:19 +0000230 basic_string& replace(size_type pos, size_type n1, const value_type* s, size_type n2);
231 basic_string& replace(size_type pos, size_type n1, const value_type* s);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000232 basic_string& replace(size_type pos, size_type n1, size_type n2, value_type c);
Howard Hinnant7b2cb482010-11-17 21:11:40 +0000233 basic_string& replace(const_iterator i1, const_iterator i2, const basic_string& str);
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000234 basic_string& replace(const_iterator i1, const_iterator i2, basic_string_view<charT, traits> sv);
Howard Hinnant9dcdcde2013-06-28 16:59:19 +0000235 basic_string& replace(const_iterator i1, const_iterator i2, const value_type* s, size_type n);
236 basic_string& replace(const_iterator i1, const_iterator i2, const value_type* s);
Howard Hinnant7b2cb482010-11-17 21:11:40 +0000237 basic_string& replace(const_iterator i1, const_iterator i2, size_type n, value_type c);
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000238 template<class InputIterator>
Howard Hinnant7b2cb482010-11-17 21:11:40 +0000239 basic_string& replace(const_iterator i1, const_iterator i2, InputIterator j1, InputIterator j2);
240 basic_string& replace(const_iterator i1, const_iterator i2, initializer_list<value_type>);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000241
Howard Hinnant9dcdcde2013-06-28 16:59:19 +0000242 size_type copy(value_type* s, size_type n, size_type pos = 0) const;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000243 basic_string substr(size_type pos = 0, size_type n = npos) const;
244
Howard Hinnant53f7d4c2011-06-03 18:40:47 +0000245 void swap(basic_string& str)
Marshall Clow7d914d12015-07-13 20:04:56 +0000246 noexcept(allocator_traits<allocator_type>::propagate_on_container_swap::value ||
247 allocator_traits<allocator_type>::is_always_equal::value); // C++17
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000248
Howard Hinnant9dcdcde2013-06-28 16:59:19 +0000249 const value_type* c_str() const noexcept;
250 const value_type* data() const noexcept;
Marshall Clowf532a702016-03-08 15:44:30 +0000251 value_type* data() noexcept; // C++17
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000252
Howard Hinnanta6119a82011-05-29 19:57:12 +0000253 allocator_type get_allocator() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000254
Howard Hinnanta6119a82011-05-29 19:57:12 +0000255 size_type find(const basic_string& str, size_type pos = 0) const noexcept;
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000256 size_type find(basic_string_view<charT, traits> sv, size_type pos = 0) const noexcept;
Howard Hinnant9dcdcde2013-06-28 16:59:19 +0000257 size_type find(const value_type* s, size_type pos, size_type n) const noexcept;
258 size_type find(const value_type* s, size_type pos = 0) const noexcept;
Howard Hinnanta6119a82011-05-29 19:57:12 +0000259 size_type find(value_type c, size_type pos = 0) const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000260
Howard Hinnanta6119a82011-05-29 19:57:12 +0000261 size_type rfind(const basic_string& str, size_type pos = npos) const noexcept;
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000262 size_type ffind(basic_string_view<charT, traits> sv, size_type pos = 0) const noexcept;
Howard Hinnant9dcdcde2013-06-28 16:59:19 +0000263 size_type rfind(const value_type* s, size_type pos, size_type n) const noexcept;
264 size_type rfind(const value_type* s, size_type pos = npos) const noexcept;
Howard Hinnanta6119a82011-05-29 19:57:12 +0000265 size_type rfind(value_type c, size_type pos = npos) const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000266
Howard Hinnanta6119a82011-05-29 19:57:12 +0000267 size_type find_first_of(const basic_string& str, size_type pos = 0) const noexcept;
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000268 size_type find_first_of(basic_string_view<charT, traits> sv, size_type pos = 0) const noexcept;
Howard Hinnant9dcdcde2013-06-28 16:59:19 +0000269 size_type find_first_of(const value_type* s, size_type pos, size_type n) const noexcept;
270 size_type find_first_of(const value_type* s, size_type pos = 0) const noexcept;
Howard Hinnanta6119a82011-05-29 19:57:12 +0000271 size_type find_first_of(value_type c, size_type pos = 0) const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000272
Howard Hinnanta6119a82011-05-29 19:57:12 +0000273 size_type find_last_of(const basic_string& str, size_type pos = npos) const noexcept;
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000274 size_type find_last_of(basic_string_view<charT, traits> sv, size_type pos = 0) const noexcept;
Howard Hinnant9dcdcde2013-06-28 16:59:19 +0000275 size_type find_last_of(const value_type* s, size_type pos, size_type n) const noexcept;
276 size_type find_last_of(const value_type* s, size_type pos = npos) const noexcept;
Howard Hinnanta6119a82011-05-29 19:57:12 +0000277 size_type find_last_of(value_type c, size_type pos = npos) const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000278
Howard Hinnanta6119a82011-05-29 19:57:12 +0000279 size_type find_first_not_of(const basic_string& str, size_type pos = 0) const noexcept;
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000280 size_type find_first_not_of(basic_string_view<charT, traits> sv, size_type pos = 0) const noexcept;
Howard Hinnant9dcdcde2013-06-28 16:59:19 +0000281 size_type find_first_not_of(const value_type* s, size_type pos, size_type n) const noexcept;
282 size_type find_first_not_of(const value_type* s, size_type pos = 0) const noexcept;
Howard Hinnanta6119a82011-05-29 19:57:12 +0000283 size_type find_first_not_of(value_type c, size_type pos = 0) const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000284
Howard Hinnanta6119a82011-05-29 19:57:12 +0000285 size_type find_last_not_of(const basic_string& str, size_type pos = npos) const noexcept;
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000286 size_type find_last_not_of(basic_string_view<charT, traits> sv, size_type pos = 0) const noexcept;
Howard Hinnant9dcdcde2013-06-28 16:59:19 +0000287 size_type find_last_not_of(const value_type* s, size_type pos, size_type n) const noexcept;
288 size_type find_last_not_of(const value_type* s, size_type pos = npos) const noexcept;
Howard Hinnanta6119a82011-05-29 19:57:12 +0000289 size_type find_last_not_of(value_type c, size_type pos = npos) const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000290
Howard Hinnanta6119a82011-05-29 19:57:12 +0000291 int compare(const basic_string& str) const noexcept;
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000292 int compare(basic_string_view<charT, traits> sv) const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000293 int compare(size_type pos1, size_type n1, const basic_string& str) const;
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000294 int compare(size_type pos1, size_type n1, basic_string_view<charT, traits> sv) const;
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000295 int compare(size_type pos1, size_type n1, const basic_string& str,
Marshall Clowa93b5e22014-03-04 19:17:19 +0000296 size_type pos2, size_type n2=npos) const; // C++14
Marshall Clow6ac8de02016-09-24 22:45:42 +0000297 template <class T>
298 int compare(size_type pos1, size_type n1, const T& t,
299 size_type pos2, size_type n2=npos) const; // C++17
Howard Hinnant9dcdcde2013-06-28 16:59:19 +0000300 int compare(const value_type* s) const noexcept;
301 int compare(size_type pos1, size_type n1, const value_type* s) const;
302 int compare(size_type pos1, size_type n1, const value_type* s, size_type n2) const;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000303
304 bool __invariants() const;
305};
306
307template<class charT, class traits, class Allocator>
308basic_string<charT, traits, Allocator>
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000309operator+(const basic_string<charT, traits, Allocator>& lhs,
310 const basic_string<charT, traits, Allocator>& rhs);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000311
312template<class charT, class traits, class Allocator>
313basic_string<charT, traits, Allocator>
314operator+(const charT* lhs , const basic_string<charT,traits,Allocator>&rhs);
315
316template<class charT, class traits, class Allocator>
317basic_string<charT, traits, Allocator>
318operator+(charT lhs, const basic_string<charT,traits,Allocator>& rhs);
319
320template<class charT, class traits, class Allocator>
321basic_string<charT, traits, Allocator>
322operator+(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs);
323
324template<class charT, class traits, class Allocator>
325basic_string<charT, traits, Allocator>
326operator+(const basic_string<charT, traits, Allocator>& lhs, charT rhs);
327
328template<class charT, class traits, class Allocator>
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000329bool operator==(const basic_string<charT, traits, Allocator>& lhs,
Howard Hinnanta6119a82011-05-29 19:57:12 +0000330 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000331
332template<class charT, class traits, class Allocator>
Howard Hinnanta6119a82011-05-29 19:57:12 +0000333bool operator==(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000334
335template<class charT, class traits, class Allocator>
Howard Hinnanta6119a82011-05-29 19:57:12 +0000336bool operator==(const basic_string<charT,traits,Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000337
Howard Hinnant324bb032010-08-22 00:02:43 +0000338template<class charT, class traits, class Allocator>
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000339bool operator!=(const basic_string<charT,traits,Allocator>& lhs,
Howard Hinnanta6119a82011-05-29 19:57:12 +0000340 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000341
342template<class charT, class traits, class Allocator>
Howard Hinnanta6119a82011-05-29 19:57:12 +0000343bool operator!=(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000344
345template<class charT, class traits, class Allocator>
Howard Hinnanta6119a82011-05-29 19:57:12 +0000346bool operator!=(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000347
348template<class charT, class traits, class Allocator>
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000349bool operator< (const basic_string<charT, traits, Allocator>& lhs,
Howard Hinnanta6119a82011-05-29 19:57:12 +0000350 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000351
352template<class charT, class traits, class Allocator>
Howard Hinnanta6119a82011-05-29 19:57:12 +0000353bool operator< (const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000354
355template<class charT, class traits, class Allocator>
Howard Hinnanta6119a82011-05-29 19:57:12 +0000356bool operator< (const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000357
358template<class charT, class traits, class Allocator>
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000359bool operator> (const basic_string<charT, traits, Allocator>& lhs,
Howard Hinnanta6119a82011-05-29 19:57:12 +0000360 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000361
362template<class charT, class traits, class Allocator>
Howard Hinnanta6119a82011-05-29 19:57:12 +0000363bool operator> (const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000364
365template<class charT, class traits, class Allocator>
Howard Hinnanta6119a82011-05-29 19:57:12 +0000366bool operator> (const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000367
368template<class charT, class traits, class Allocator>
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000369bool operator<=(const basic_string<charT, traits, Allocator>& lhs,
Howard Hinnanta6119a82011-05-29 19:57:12 +0000370 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000371
372template<class charT, class traits, class Allocator>
Howard Hinnanta6119a82011-05-29 19:57:12 +0000373bool operator<=(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000374
375template<class charT, class traits, class Allocator>
Howard Hinnanta6119a82011-05-29 19:57:12 +0000376bool operator<=(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000377
378template<class charT, class traits, class Allocator>
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000379bool operator>=(const basic_string<charT, traits, Allocator>& lhs,
Howard Hinnanta6119a82011-05-29 19:57:12 +0000380 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000381
382template<class charT, class traits, class Allocator>
Howard Hinnanta6119a82011-05-29 19:57:12 +0000383bool operator>=(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000384
385template<class charT, class traits, class Allocator>
Howard Hinnanta6119a82011-05-29 19:57:12 +0000386bool operator>=(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000387
388template<class charT, class traits, class Allocator>
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000389void swap(basic_string<charT, traits, Allocator>& lhs,
Howard Hinnant53f7d4c2011-06-03 18:40:47 +0000390 basic_string<charT, traits, Allocator>& rhs)
391 noexcept(noexcept(lhs.swap(rhs)));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000392
393template<class charT, class traits, class Allocator>
394basic_istream<charT, traits>&
395operator>>(basic_istream<charT, traits>& is, basic_string<charT, traits, Allocator>& str);
396
397template<class charT, class traits, class Allocator>
398basic_ostream<charT, traits>&
399operator<<(basic_ostream<charT, traits>& os, const basic_string<charT, traits, Allocator>& str);
400
401template<class charT, class traits, class Allocator>
Howard Hinnant324bb032010-08-22 00:02:43 +0000402basic_istream<charT, traits>&
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000403getline(basic_istream<charT, traits>& is, basic_string<charT, traits, Allocator>& str,
404 charT delim);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000405
406template<class charT, class traits, class Allocator>
407basic_istream<charT, traits>&
408getline(basic_istream<charT, traits>& is, basic_string<charT, traits, Allocator>& str);
409
410typedef basic_string<char> string;
411typedef basic_string<wchar_t> wstring;
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000412typedef basic_string<char16_t> u16string;
413typedef basic_string<char32_t> u32string;
414
415int stoi (const string& str, size_t* idx = 0, int base = 10);
416long stol (const string& str, size_t* idx = 0, int base = 10);
417unsigned long stoul (const string& str, size_t* idx = 0, int base = 10);
418long long stoll (const string& str, size_t* idx = 0, int base = 10);
419unsigned long long stoull(const string& str, size_t* idx = 0, int base = 10);
420
421float stof (const string& str, size_t* idx = 0);
422double stod (const string& str, size_t* idx = 0);
423long double stold(const string& str, size_t* idx = 0);
424
425string to_string(int val);
426string to_string(unsigned val);
427string to_string(long val);
428string to_string(unsigned long val);
429string to_string(long long val);
430string to_string(unsigned long long val);
431string to_string(float val);
432string to_string(double val);
433string to_string(long double val);
434
435int stoi (const wstring& str, size_t* idx = 0, int base = 10);
436long stol (const wstring& str, size_t* idx = 0, int base = 10);
437unsigned long stoul (const wstring& str, size_t* idx = 0, int base = 10);
438long long stoll (const wstring& str, size_t* idx = 0, int base = 10);
439unsigned long long stoull(const wstring& str, size_t* idx = 0, int base = 10);
440
441float stof (const wstring& str, size_t* idx = 0);
442double stod (const wstring& str, size_t* idx = 0);
443long double stold(const wstring& str, size_t* idx = 0);
444
445wstring to_wstring(int val);
446wstring to_wstring(unsigned val);
447wstring to_wstring(long val);
448wstring to_wstring(unsigned long val);
449wstring to_wstring(long long val);
450wstring to_wstring(unsigned long long val);
451wstring to_wstring(float val);
452wstring to_wstring(double val);
453wstring to_wstring(long double val);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000454
455template <> struct hash<string>;
456template <> struct hash<u16string>;
457template <> struct hash<u32string>;
458template <> struct hash<wstring>;
459
Marshall Clow15234322013-07-23 17:05:24 +0000460basic_string<char> operator "" s( const char *str, size_t len ); // C++14
461basic_string<wchar_t> operator "" s( const wchar_t *str, size_t len ); // C++14
462basic_string<char16_t> operator "" s( const char16_t *str, size_t len ); // C++14
463basic_string<char32_t> operator "" s( const char32_t *str, size_t len ); // C++14
464
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000465} // std
466
467*/
468
469#include <__config>
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000470#include <string_view>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000471#include <iosfwd>
472#include <cstring>
Howard Hinnantadff4892010-05-24 17:49:41 +0000473#include <cstdio> // For EOF.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000474#include <cwchar>
475#include <algorithm>
476#include <iterator>
477#include <utility>
478#include <memory>
479#include <stdexcept>
480#include <type_traits>
481#include <initializer_list>
482#include <__functional_base>
483#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
484#include <cstdint>
485#endif
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000486
Howard Hinnant66c6f972011-11-29 16:45:27 +0000487#include <__undef_min_max>
488
Eric Fiselierb9536102014-08-10 23:53:08 +0000489#include <__debug>
490
Howard Hinnant08e17472011-10-17 20:05:10 +0000491#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000492#pragma GCC system_header
Howard Hinnant08e17472011-10-17 20:05:10 +0000493#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000494
495_LIBCPP_BEGIN_NAMESPACE_STD
496
497// fpos
498
499template <class _StateT>
Eric Fiselierc3589a82017-01-04 23:56:00 +0000500class _LIBCPP_TEMPLATE_VIS fpos
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000501{
502private:
503 _StateT __st_;
504 streamoff __off_;
505public:
506 _LIBCPP_INLINE_VISIBILITY fpos(streamoff __off = streamoff()) : __st_(), __off_(__off) {}
507
508 _LIBCPP_INLINE_VISIBILITY operator streamoff() const {return __off_;}
509
510 _LIBCPP_INLINE_VISIBILITY _StateT state() const {return __st_;}
511 _LIBCPP_INLINE_VISIBILITY void state(_StateT __st) {__st_ = __st;}
512
513 _LIBCPP_INLINE_VISIBILITY fpos& operator+=(streamoff __off) {__off_ += __off; return *this;}
514 _LIBCPP_INLINE_VISIBILITY fpos operator+ (streamoff __off) const {fpos __t(*this); __t += __off; return __t;}
515 _LIBCPP_INLINE_VISIBILITY fpos& operator-=(streamoff __off) {__off_ -= __off; return *this;}
516 _LIBCPP_INLINE_VISIBILITY fpos operator- (streamoff __off) const {fpos __t(*this); __t -= __off; return __t;}
517};
518
519template <class _StateT>
520inline _LIBCPP_INLINE_VISIBILITY
521streamoff operator-(const fpos<_StateT>& __x, const fpos<_StateT>& __y)
522 {return streamoff(__x) - streamoff(__y);}
523
524template <class _StateT>
525inline _LIBCPP_INLINE_VISIBILITY
526bool operator==(const fpos<_StateT>& __x, const fpos<_StateT>& __y)
527 {return streamoff(__x) == streamoff(__y);}
528
529template <class _StateT>
530inline _LIBCPP_INLINE_VISIBILITY
531bool operator!=(const fpos<_StateT>& __x, const fpos<_StateT>& __y)
532 {return streamoff(__x) != streamoff(__y);}
533
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000534// basic_string
535
536template<class _CharT, class _Traits, class _Allocator>
537basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant2b1b2d42011-06-14 19:58:17 +0000538operator+(const basic_string<_CharT, _Traits, _Allocator>& __x,
539 const basic_string<_CharT, _Traits, _Allocator>& __y);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000540
541template<class _CharT, class _Traits, class _Allocator>
542basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant2b1b2d42011-06-14 19:58:17 +0000543operator+(const _CharT* __x, const basic_string<_CharT,_Traits,_Allocator>& __y);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000544
545template<class _CharT, class _Traits, class _Allocator>
546basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant2b1b2d42011-06-14 19:58:17 +0000547operator+(_CharT __x, const basic_string<_CharT,_Traits,_Allocator>& __y);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000548
549template<class _CharT, class _Traits, class _Allocator>
550basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant2b1b2d42011-06-14 19:58:17 +0000551operator+(const basic_string<_CharT, _Traits, _Allocator>& __x, const _CharT* __y);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000552
553template<class _CharT, class _Traits, class _Allocator>
554basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant2b1b2d42011-06-14 19:58:17 +0000555operator+(const basic_string<_CharT, _Traits, _Allocator>& __x, _CharT __y);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000556
557template <bool>
Eric Fiselierc3589a82017-01-04 23:56:00 +0000558class _LIBCPP_TEMPLATE_VIS __basic_string_common
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000559{
560protected:
Marshall Clow14c09a22016-08-25 15:09:01 +0000561 _LIBCPP_NORETURN void __throw_length_error() const;
562 _LIBCPP_NORETURN void __throw_out_of_range() const;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000563};
564
565template <bool __b>
566void
567__basic_string_common<__b>::__throw_length_error() const
568{
Marshall Clow14c09a22016-08-25 15:09:01 +0000569 _VSTD::__throw_length_error("basic_string");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000570}
571
572template <bool __b>
573void
574__basic_string_common<__b>::__throw_out_of_range() const
575{
Marshall Clow14c09a22016-08-25 15:09:01 +0000576 _VSTD::__throw_out_of_range("basic_string");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000577}
578
Howard Hinnante9df0a52013-08-01 18:17:34 +0000579#ifdef _LIBCPP_MSVC
Howard Hinnant78b68282011-10-22 20:59:45 +0000580#pragma warning( push )
581#pragma warning( disable: 4231 )
Howard Hinnante9df0a52013-08-01 18:17:34 +0000582#endif // _LIBCPP_MSVC
Eric Fiselier833d6442016-09-15 22:27:07 +0000583_LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS __basic_string_common<true>)
Howard Hinnante9df0a52013-08-01 18:17:34 +0000584#ifdef _LIBCPP_MSVC
Howard Hinnant78b68282011-10-22 20:59:45 +0000585#pragma warning( pop )
Howard Hinnante9df0a52013-08-01 18:17:34 +0000586#endif // _LIBCPP_MSVC
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000587
Marshall Clowdf9db312016-01-13 21:54:34 +0000588#ifdef _LIBCPP_NO_EXCEPTIONS
589template <class _Iter>
590struct __libcpp_string_gets_noexcept_iterator_impl : public true_type {};
591#elif defined(_LIBCPP_HAS_NO_NOEXCEPT)
592template <class _Iter>
593struct __libcpp_string_gets_noexcept_iterator_impl : public false_type {};
594#else
595template <class _Iter, bool = __is_forward_iterator<_Iter>::value>
596struct __libcpp_string_gets_noexcept_iterator_impl : public _LIBCPP_BOOL_CONSTANT((
597 noexcept(++(declval<_Iter&>())) &&
598 is_nothrow_assignable<_Iter&, _Iter>::value &&
599 noexcept(declval<_Iter>() == declval<_Iter>()) &&
600 noexcept(*declval<_Iter>())
601)) {};
602
603template <class _Iter>
604struct __libcpp_string_gets_noexcept_iterator_impl<_Iter, false> : public false_type {};
605#endif
606
607
608template <class _Iter>
609struct __libcpp_string_gets_noexcept_iterator
610 : public _LIBCPP_BOOL_CONSTANT(__libcpp_is_trivial_iterator<_Iter>::value || __libcpp_string_gets_noexcept_iterator_impl<_Iter>::value) {};
611
Marshall Clow6ac8de02016-09-24 22:45:42 +0000612template <class _CharT, class _Traits, class _Tp>
613struct __can_be_converted_to_string_view : public _LIBCPP_BOOL_CONSTANT(
614 ( is_convertible<const _Tp&, basic_string_view<_CharT, _Traits> >::value &&
615 !is_convertible<const _Tp&, const _CharT*>::value)) {};
616
Evgeniy Stepanov4f01aa82015-10-13 23:48:28 +0000617#ifdef _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant15467182013-04-30 21:44:48 +0000618
619template <class _CharT, size_t = sizeof(_CharT)>
620struct __padding
621{
622 unsigned char __xx[sizeof(_CharT)-1];
623};
624
625template <class _CharT>
626struct __padding<_CharT, 1>
627{
628};
629
Evgeniy Stepanov4f01aa82015-10-13 23:48:28 +0000630#endif // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant15467182013-04-30 21:44:48 +0000631
Howard Hinnant324bb032010-08-22 00:02:43 +0000632template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierc3589a82017-01-04 23:56:00 +0000633class _LIBCPP_TEMPLATE_VIS basic_string
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000634 : private __basic_string_common<true>
635{
636public:
637 typedef basic_string __self;
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000638 typedef basic_string_view<_CharT, _Traits> __self_view;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000639 typedef _Traits traits_type;
640 typedef typename traits_type::char_type value_type;
641 typedef _Allocator allocator_type;
Howard Hinnante32b5e22010-11-17 17:55:08 +0000642 typedef allocator_traits<allocator_type> __alloc_traits;
643 typedef typename __alloc_traits::size_type size_type;
644 typedef typename __alloc_traits::difference_type difference_type;
Howard Hinnant53f7d4c2011-06-03 18:40:47 +0000645 typedef value_type& reference;
646 typedef const value_type& const_reference;
Howard Hinnante32b5e22010-11-17 17:55:08 +0000647 typedef typename __alloc_traits::pointer pointer;
648 typedef typename __alloc_traits::const_pointer const_pointer;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000649
Howard Hinnant499cea12013-08-23 17:37:05 +0000650 static_assert(is_pod<value_type>::value, "Character type of basic_string must be a POD");
651 static_assert((is_same<_CharT, value_type>::value),
652 "traits_type::char_type must be the same type as CharT");
653 static_assert((is_same<typename allocator_type::value_type, value_type>::value),
654 "Allocator::value_type must be same type as value_type");
655#if defined(_LIBCPP_RAW_ITERATORS)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000656 typedef pointer iterator;
657 typedef const_pointer const_iterator;
Howard Hinnant324bb032010-08-22 00:02:43 +0000658#else // defined(_LIBCPP_RAW_ITERATORS)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000659 typedef __wrap_iter<pointer> iterator;
660 typedef __wrap_iter<const_pointer> const_iterator;
Howard Hinnant324bb032010-08-22 00:02:43 +0000661#endif // defined(_LIBCPP_RAW_ITERATORS)
Howard Hinnant0949eed2011-06-30 21:18:19 +0000662 typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
663 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000664
665private:
Howard Hinnant15467182013-04-30 21:44:48 +0000666
Evgeniy Stepanov4f01aa82015-10-13 23:48:28 +0000667#ifdef _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant15467182013-04-30 21:44:48 +0000668
669 struct __long
670 {
671 pointer __data_;
672 size_type __size_;
673 size_type __cap_;
674 };
675
676#if _LIBCPP_BIG_ENDIAN
677 enum {__short_mask = 0x01};
678 enum {__long_mask = 0x1ul};
679#else // _LIBCPP_BIG_ENDIAN
680 enum {__short_mask = 0x80};
681 enum {__long_mask = ~(size_type(~0) >> 1)};
682#endif // _LIBCPP_BIG_ENDIAN
683
684 enum {__min_cap = (sizeof(__long) - 1)/sizeof(value_type) > 2 ?
685 (sizeof(__long) - 1)/sizeof(value_type) : 2};
686
687 struct __short
688 {
689 value_type __data_[__min_cap];
690 struct
691 : __padding<value_type>
692 {
693 unsigned char __size_;
694 };
695 };
696
697#else
698
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000699 struct __long
700 {
701 size_type __cap_;
702 size_type __size_;
703 pointer __data_;
704 };
705
706#if _LIBCPP_BIG_ENDIAN
707 enum {__short_mask = 0x80};
708 enum {__long_mask = ~(size_type(~0) >> 1)};
Howard Hinnant324bb032010-08-22 00:02:43 +0000709#else // _LIBCPP_BIG_ENDIAN
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000710 enum {__short_mask = 0x01};
Howard Hinnantec3773c2011-12-01 20:21:04 +0000711 enum {__long_mask = 0x1ul};
Howard Hinnant324bb032010-08-22 00:02:43 +0000712#endif // _LIBCPP_BIG_ENDIAN
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000713
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000714 enum {__min_cap = (sizeof(__long) - 1)/sizeof(value_type) > 2 ?
715 (sizeof(__long) - 1)/sizeof(value_type) : 2};
716
717 struct __short
718 {
719 union
720 {
721 unsigned char __size_;
Howard Hinnant9c0df142012-10-30 19:06:59 +0000722 value_type __lx;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000723 };
724 value_type __data_[__min_cap];
725 };
726
Evgeniy Stepanov4f01aa82015-10-13 23:48:28 +0000727#endif // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant15467182013-04-30 21:44:48 +0000728
Howard Hinnant499cea12013-08-23 17:37:05 +0000729 union __ulx{__long __lx; __short __lxx;};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000730
Howard Hinnant499cea12013-08-23 17:37:05 +0000731 enum {__n_words = sizeof(__ulx) / sizeof(size_type)};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000732
733 struct __raw
734 {
735 size_type __words[__n_words];
736 };
737
738 struct __rep
739 {
740 union
741 {
742 __long __l;
743 __short __s;
744 __raw __r;
745 };
746 };
747
748 __compressed_pair<__rep, allocator_type> __r_;
749
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000750public:
751 static const size_type npos = -1;
752
Howard Hinnant53f7d4c2011-06-03 18:40:47 +0000753 _LIBCPP_INLINE_VISIBILITY basic_string()
754 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
Marshall Clow7b193f72015-06-03 19:56:43 +0000755
756 _LIBCPP_INLINE_VISIBILITY explicit basic_string(const allocator_type& __a)
757#if _LIBCPP_STD_VER <= 14
758 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value);
759#else
760 _NOEXCEPT;
761#endif
762
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000763 basic_string(const basic_string& __str);
764 basic_string(const basic_string& __str, const allocator_type& __a);
Marshall Clow7b193f72015-06-03 19:56:43 +0000765
Howard Hinnant73d21a42010-09-04 23:28:19 +0000766#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant9f193f22011-01-26 00:06:59 +0000767 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant53f7d4c2011-06-03 18:40:47 +0000768 basic_string(basic_string&& __str)
Marshall Clow7b193f72015-06-03 19:56:43 +0000769#if _LIBCPP_STD_VER <= 14
Howard Hinnant53f7d4c2011-06-03 18:40:47 +0000770 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
Marshall Clow7b193f72015-06-03 19:56:43 +0000771#else
772 _NOEXCEPT;
773#endif
774
Howard Hinnant9f193f22011-01-26 00:06:59 +0000775 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000776 basic_string(basic_string&& __str, const allocator_type& __a);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000777#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
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>
Eric Fiselier0eaf2e82017-02-17 01:17:10 +0000795 basic_string(const _Tp& __t, size_type __pos, size_type __n,
Marshall Clowdb7fa112016-11-14 18:22:19 +0000796 const allocator_type& __a = allocator_type(),
797 typename enable_if<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value, void>::type* = 0);
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000798 _LIBCPP_INLINE_VISIBILITY explicit
799 basic_string(__self_view __sv);
800 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier0eaf2e82017-02-17 01:17:10 +0000801 basic_string(__self_view __sv, const _Allocator& __a);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000802 template<class _InputIterator>
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000803 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000804 basic_string(_InputIterator __first, _InputIterator __last);
805 template<class _InputIterator>
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000806 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000807 basic_string(_InputIterator __first, _InputIterator __last, const allocator_type& __a);
Howard Hinnante3e32912011-08-12 21:56:02 +0000808#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000809 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier0eaf2e82017-02-17 01:17:10 +0000810 basic_string(initializer_list<_CharT> __il);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000811 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier0eaf2e82017-02-17 01:17:10 +0000812 basic_string(initializer_list<_CharT> __il, const _Allocator& __a);
Howard Hinnante3e32912011-08-12 21:56:02 +0000813#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000814
Eric Fiselier51eb1d52016-10-31 03:42:50 +0000815 inline ~basic_string();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000816
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000817 _LIBCPP_INLINE_VISIBILITY
818 operator __self_view() const _NOEXCEPT { return __self_view(data(), size()); }
819
Howard Hinnante32b5e22010-11-17 17:55:08 +0000820 basic_string& operator=(const basic_string& __str);
Eric Fiselierf472d6c2017-01-23 21:24:58 +0000821
822#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier37b2be92017-01-17 22:10:32 +0000823 template <class = void>
Eric Fiselierf472d6c2017-01-23 21:24:58 +0000824#endif
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000825 _LIBCPP_INLINE_VISIBILITY
826 basic_string& operator=(__self_view __sv) {return assign(__sv);}
Howard Hinnant73d21a42010-09-04 23:28:19 +0000827#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000828 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant53f7d4c2011-06-03 18:40:47 +0000829 basic_string& operator=(basic_string&& __str)
Marshall Clowaf961ed2015-08-18 18:57:00 +0000830 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000831#endif
Howard Hinnant9dcdcde2013-06-28 16:59:19 +0000832 _LIBCPP_INLINE_VISIBILITY basic_string& operator=(const value_type* __s) {return assign(__s);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000833 basic_string& operator=(value_type __c);
Howard Hinnante3e32912011-08-12 21:56:02 +0000834#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000835 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000836 basic_string& operator=(initializer_list<value_type> __il) {return assign(__il.begin(), __il.size());}
Howard Hinnante3e32912011-08-12 21:56:02 +0000837#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000838
Howard Hinnant499cea12013-08-23 17:37:05 +0000839#if _LIBCPP_DEBUG_LEVEL >= 2
840 _LIBCPP_INLINE_VISIBILITY
841 iterator begin() _NOEXCEPT
842 {return iterator(this, __get_pointer());}
843 _LIBCPP_INLINE_VISIBILITY
844 const_iterator begin() const _NOEXCEPT
845 {return const_iterator(this, __get_pointer());}
846 _LIBCPP_INLINE_VISIBILITY
847 iterator end() _NOEXCEPT
848 {return iterator(this, __get_pointer() + size());}
849 _LIBCPP_INLINE_VISIBILITY
850 const_iterator end() const _NOEXCEPT
851 {return const_iterator(this, __get_pointer() + size());}
852#else
Howard Hinnanta6119a82011-05-29 19:57:12 +0000853 _LIBCPP_INLINE_VISIBILITY
854 iterator begin() _NOEXCEPT
855 {return iterator(__get_pointer());}
856 _LIBCPP_INLINE_VISIBILITY
857 const_iterator begin() const _NOEXCEPT
Howard Hinnant9dcdcde2013-06-28 16:59:19 +0000858 {return const_iterator(__get_pointer());}
Howard Hinnanta6119a82011-05-29 19:57:12 +0000859 _LIBCPP_INLINE_VISIBILITY
860 iterator end() _NOEXCEPT
861 {return iterator(__get_pointer() + size());}
862 _LIBCPP_INLINE_VISIBILITY
863 const_iterator end() const _NOEXCEPT
Howard Hinnant9dcdcde2013-06-28 16:59:19 +0000864 {return const_iterator(__get_pointer() + size());}
Howard Hinnant499cea12013-08-23 17:37:05 +0000865#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnanta6119a82011-05-29 19:57:12 +0000866 _LIBCPP_INLINE_VISIBILITY
867 reverse_iterator rbegin() _NOEXCEPT
868 {return reverse_iterator(end());}
869 _LIBCPP_INLINE_VISIBILITY
870 const_reverse_iterator rbegin() const _NOEXCEPT
871 {return const_reverse_iterator(end());}
872 _LIBCPP_INLINE_VISIBILITY
873 reverse_iterator rend() _NOEXCEPT
874 {return reverse_iterator(begin());}
875 _LIBCPP_INLINE_VISIBILITY
876 const_reverse_iterator rend() const _NOEXCEPT
877 {return const_reverse_iterator(begin());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000878
Howard Hinnanta6119a82011-05-29 19:57:12 +0000879 _LIBCPP_INLINE_VISIBILITY
880 const_iterator cbegin() const _NOEXCEPT
881 {return begin();}
882 _LIBCPP_INLINE_VISIBILITY
883 const_iterator cend() const _NOEXCEPT
884 {return end();}
885 _LIBCPP_INLINE_VISIBILITY
886 const_reverse_iterator crbegin() const _NOEXCEPT
887 {return rbegin();}
888 _LIBCPP_INLINE_VISIBILITY
889 const_reverse_iterator crend() const _NOEXCEPT
890 {return rend();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000891
Howard Hinnanta6119a82011-05-29 19:57:12 +0000892 _LIBCPP_INLINE_VISIBILITY size_type size() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000893 {return __is_long() ? __get_long_size() : __get_short_size();}
Howard Hinnanta6119a82011-05-29 19:57:12 +0000894 _LIBCPP_INLINE_VISIBILITY size_type length() const _NOEXCEPT {return size();}
895 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT;
896 _LIBCPP_INLINE_VISIBILITY size_type capacity() const _NOEXCEPT
Eric Fiseliere2d48922015-08-28 07:02:42 +0000897 {return (__is_long() ? __get_long_cap()
898 : static_cast<size_type>(__min_cap)) - 1;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000899
900 void resize(size_type __n, value_type __c);
901 _LIBCPP_INLINE_VISIBILITY void resize(size_type __n) {resize(__n, value_type());}
902
903 void reserve(size_type res_arg = 0);
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000904 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant53f7d4c2011-06-03 18:40:47 +0000905 void shrink_to_fit() _NOEXCEPT {reserve();}
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000906 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta6119a82011-05-29 19:57:12 +0000907 void clear() _NOEXCEPT;
908 _LIBCPP_INLINE_VISIBILITY bool empty() const _NOEXCEPT {return size() == 0;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000909
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000910 _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_type __pos) const _NOEXCEPT;
911 _LIBCPP_INLINE_VISIBILITY reference operator[](size_type __pos) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000912
913 const_reference at(size_type __n) const;
914 reference at(size_type __n);
915
916 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(const basic_string& __str) {return append(__str);}
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000917 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(__self_view __sv) {return append(__sv);}
918 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(const value_type* __s) {return append(__s);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000919 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(value_type __c) {push_back(__c); return *this;}
Howard Hinnante3e32912011-08-12 21:56:02 +0000920#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000921 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(initializer_list<value_type> __il) {return append(__il);}
Howard Hinnante3e32912011-08-12 21:56:02 +0000922#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000923
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000924 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000925 basic_string& append(const basic_string& __str);
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000926 _LIBCPP_INLINE_VISIBILITY
927 basic_string& append(__self_view __sv) { return append(__sv.data(), __sv.size()); }
Marshall Clowa93b5e22014-03-04 19:17:19 +0000928 basic_string& append(const basic_string& __str, size_type __pos, size_type __n=npos);
Marshall Clow42a87db2016-10-03 23:40:48 +0000929 template <class _Tp>
Marshall Clow6ac8de02016-09-24 22:45:42 +0000930 typename enable_if
931 <
932 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
933 basic_string&
934 >::type
935 append(const _Tp& __t, size_type __pos, size_type __n=npos);
Howard Hinnant9dcdcde2013-06-28 16:59:19 +0000936 basic_string& append(const value_type* __s, size_type __n);
937 basic_string& append(const value_type* __s);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000938 basic_string& append(size_type __n, value_type __c);
Eric Fiselier026d38e2016-10-31 02:46:25 +0000939 template <class _ForwardIterator>
Eric Fiselierd5b0db52016-10-31 03:40:29 +0000940 inline basic_string& __append_forward_unsafe(_ForwardIterator, _ForwardIterator);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000941 template<class _InputIterator>
942 typename enable_if
943 <
Marshall Clowdf9db312016-01-13 21:54:34 +0000944 __is_exactly_input_iterator<_InputIterator>::value
945 || !__libcpp_string_gets_noexcept_iterator<_InputIterator>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000946 basic_string&
947 >::type
Eric Fiselier026d38e2016-10-31 02:46:25 +0000948 _LIBCPP_INLINE_VISIBILITY
949 append(_InputIterator __first, _InputIterator __last) {
950 const basic_string __temp (__first, __last, __alloc());
951 append(__temp.data(), __temp.size());
952 return *this;
953 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000954 template<class _ForwardIterator>
955 typename enable_if
956 <
Marshall Clowdf9db312016-01-13 21:54:34 +0000957 __is_forward_iterator<_ForwardIterator>::value
958 && __libcpp_string_gets_noexcept_iterator<_ForwardIterator>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000959 basic_string&
960 >::type
Eric Fiselier026d38e2016-10-31 02:46:25 +0000961 _LIBCPP_INLINE_VISIBILITY
962 append(_ForwardIterator __first, _ForwardIterator __last) {
963 return __append_forward_unsafe(__first, __last);
964 }
965
Howard Hinnante3e32912011-08-12 21:56:02 +0000966#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000967 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000968 basic_string& append(initializer_list<value_type> __il) {return append(__il.begin(), __il.size());}
Howard Hinnante3e32912011-08-12 21:56:02 +0000969#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000970
971 void push_back(value_type __c);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000972 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000973 void pop_back();
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000974 _LIBCPP_INLINE_VISIBILITY reference front();
975 _LIBCPP_INLINE_VISIBILITY const_reference front() const;
976 _LIBCPP_INLINE_VISIBILITY reference back();
977 _LIBCPP_INLINE_VISIBILITY const_reference back() const;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000978
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000979 _LIBCPP_INLINE_VISIBILITY
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000980 basic_string& assign(__self_view __sv) { return assign(__sv.data(), __sv.size()); }
981 _LIBCPP_INLINE_VISIBILITY
Marshall Clowf40ec902016-03-09 18:08:29 +0000982 basic_string& assign(const basic_string& __str) { return *this = __str; }
Howard Hinnanta6119a82011-05-29 19:57:12 +0000983#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
984 _LIBCPP_INLINE_VISIBILITY
985 basic_string& assign(basic_string&& str)
Marshall Clow7ed093b2015-10-05 16:17:34 +0000986 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
Howard Hinnant0949eed2011-06-30 21:18:19 +0000987 {*this = _VSTD::move(str); return *this;}
Howard Hinnanta6119a82011-05-29 19:57:12 +0000988#endif
Marshall Clowa93b5e22014-03-04 19:17:19 +0000989 basic_string& assign(const basic_string& __str, size_type __pos, size_type __n=npos);
Marshall Clow42a87db2016-10-03 23:40:48 +0000990 template <class _Tp>
Eric Fiselier25380e42017-02-17 01:53:16 +0000991 inline typename enable_if
Marshall Clow6ac8de02016-09-24 22:45:42 +0000992 <
993 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
994 basic_string&
995 >::type
996 assign(const _Tp & __t, size_type pos, size_type n=npos);
Howard Hinnant9dcdcde2013-06-28 16:59:19 +0000997 basic_string& assign(const value_type* __s, size_type __n);
998 basic_string& assign(const value_type* __s);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000999 basic_string& assign(size_type __n, value_type __c);
1000 template<class _InputIterator>
Eric Fiselier25380e42017-02-17 01:53:16 +00001001 inline typename enable_if
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001002 <
Marshall Clowdf9db312016-01-13 21:54:34 +00001003 __is_exactly_input_iterator<_InputIterator>::value
1004 || !__libcpp_string_gets_noexcept_iterator<_InputIterator>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001005 basic_string&
1006 >::type
1007 assign(_InputIterator __first, _InputIterator __last);
1008 template<class _ForwardIterator>
Eric Fiselier25380e42017-02-17 01:53:16 +00001009 inline typename enable_if
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001010 <
Marshall Clowdf9db312016-01-13 21:54:34 +00001011 __is_forward_iterator<_ForwardIterator>::value
1012 && __libcpp_string_gets_noexcept_iterator<_ForwardIterator>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001013 basic_string&
1014 >::type
1015 assign(_ForwardIterator __first, _ForwardIterator __last);
Howard Hinnante3e32912011-08-12 21:56:02 +00001016#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant8d7a9552010-09-23 17:31:07 +00001017 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001018 basic_string& assign(initializer_list<value_type> __il) {return assign(__il.begin(), __il.size());}
Howard Hinnante3e32912011-08-12 21:56:02 +00001019#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001020
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001021 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001022 basic_string& insert(size_type __pos1, const basic_string& __str);
Marshall Clow1e00d6d2016-07-21 05:31:24 +00001023 _LIBCPP_INLINE_VISIBILITY
1024 basic_string& insert(size_type __pos1, __self_view __sv) { return insert(__pos1, __sv.data(), __sv.size()); }
Marshall Clow6ac8de02016-09-24 22:45:42 +00001025 template <class _Tp>
Eric Fiselier25380e42017-02-17 01:53:16 +00001026 inline typename enable_if
Marshall Clow6ac8de02016-09-24 22:45:42 +00001027 <
1028 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1029 basic_string&
1030 >::type
1031 insert(size_type __pos1, const _Tp& __t, size_type __pos2, size_type __n=npos);
Marshall Clowa93b5e22014-03-04 19:17:19 +00001032 basic_string& insert(size_type __pos1, const basic_string& __str, size_type __pos2, size_type __n=npos);
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001033 basic_string& insert(size_type __pos, const value_type* __s, size_type __n);
1034 basic_string& insert(size_type __pos, const value_type* __s);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001035 basic_string& insert(size_type __pos, size_type __n, value_type __c);
1036 iterator insert(const_iterator __pos, value_type __c);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001037 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001038 iterator insert(const_iterator __pos, size_type __n, value_type __c);
1039 template<class _InputIterator>
Eric Fiselier25380e42017-02-17 01:53:16 +00001040 inline typename enable_if
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001041 <
Marshall Clowdf9db312016-01-13 21:54:34 +00001042 __is_exactly_input_iterator<_InputIterator>::value
1043 || !__libcpp_string_gets_noexcept_iterator<_InputIterator>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001044 iterator
1045 >::type
1046 insert(const_iterator __pos, _InputIterator __first, _InputIterator __last);
1047 template<class _ForwardIterator>
Eric Fiselier25380e42017-02-17 01:53:16 +00001048 inline typename enable_if
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001049 <
Marshall Clowdf9db312016-01-13 21:54:34 +00001050 __is_forward_iterator<_ForwardIterator>::value
1051 && __libcpp_string_gets_noexcept_iterator<_ForwardIterator>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001052 iterator
1053 >::type
1054 insert(const_iterator __pos, _ForwardIterator __first, _ForwardIterator __last);
Howard Hinnante3e32912011-08-12 21:56:02 +00001055#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant8d7a9552010-09-23 17:31:07 +00001056 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001057 iterator insert(const_iterator __pos, initializer_list<value_type> __il)
1058 {return insert(__pos, __il.begin(), __il.end());}
Howard Hinnante3e32912011-08-12 21:56:02 +00001059#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001060
1061 basic_string& erase(size_type __pos = 0, size_type __n = npos);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001062 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001063 iterator erase(const_iterator __pos);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001064 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001065 iterator erase(const_iterator __first, const_iterator __last);
1066
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001067 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001068 basic_string& replace(size_type __pos1, size_type __n1, const basic_string& __str);
Marshall Clow1e00d6d2016-07-21 05:31:24 +00001069 _LIBCPP_INLINE_VISIBILITY
1070 basic_string& replace(size_type __pos1, size_type __n1, __self_view __sv) { return replace(__pos1, __n1, __sv.data(), __sv.size()); }
Marshall Clowa93b5e22014-03-04 19:17:19 +00001071 basic_string& replace(size_type __pos1, size_type __n1, const basic_string& __str, size_type __pos2, size_type __n2=npos);
Marshall Clow6ac8de02016-09-24 22:45:42 +00001072 template <class _Tp>
Eric Fiselier25380e42017-02-17 01:53:16 +00001073 inline typename enable_if
Marshall Clow6ac8de02016-09-24 22:45:42 +00001074 <
1075 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1076 basic_string&
1077 >::type
1078 replace(size_type __pos1, size_type __n1, const _Tp& __t, size_type __pos2, size_type __n2=npos);
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001079 basic_string& replace(size_type __pos, size_type __n1, const value_type* __s, size_type __n2);
1080 basic_string& replace(size_type __pos, size_type __n1, const value_type* __s);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001081 basic_string& replace(size_type __pos, size_type __n1, size_type __n2, value_type __c);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001082 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7b2cb482010-11-17 21:11:40 +00001083 basic_string& replace(const_iterator __i1, const_iterator __i2, const basic_string& __str);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001084 _LIBCPP_INLINE_VISIBILITY
Marshall Clow1e00d6d2016-07-21 05:31:24 +00001085 basic_string& replace(const_iterator __i1, const_iterator __i2, __self_view __sv) { return replace(__i1 - begin(), __i2 - __i1, __sv); }
1086 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001087 basic_string& replace(const_iterator __i1, const_iterator __i2, const value_type* __s, size_type __n);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001088 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001089 basic_string& replace(const_iterator __i1, const_iterator __i2, const value_type* __s);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001090 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7b2cb482010-11-17 21:11:40 +00001091 basic_string& replace(const_iterator __i1, const_iterator __i2, size_type __n, value_type __c);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001092 template<class _InputIterator>
Eric Fiselier25380e42017-02-17 01:53:16 +00001093 inline typename enable_if
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001094 <
1095 __is_input_iterator<_InputIterator>::value,
1096 basic_string&
1097 >::type
Howard Hinnant7b2cb482010-11-17 21:11:40 +00001098 replace(const_iterator __i1, const_iterator __i2, _InputIterator __j1, _InputIterator __j2);
Howard Hinnante3e32912011-08-12 21:56:02 +00001099#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant8d7a9552010-09-23 17:31:07 +00001100 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7b2cb482010-11-17 21:11:40 +00001101 basic_string& replace(const_iterator __i1, const_iterator __i2, initializer_list<value_type> __il)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001102 {return replace(__i1, __i2, __il.begin(), __il.end());}
Howard Hinnante3e32912011-08-12 21:56:02 +00001103#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001104
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001105 size_type copy(value_type* __s, size_type __n, size_type __pos = 0) const;
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001106 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001107 basic_string substr(size_type __pos = 0, size_type __n = npos) const;
1108
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001109 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant53f7d4c2011-06-03 18:40:47 +00001110 void swap(basic_string& __str)
Marshall Clow7d914d12015-07-13 20:04:56 +00001111#if _LIBCPP_STD_VER >= 14
Eric Fiselier47257c42016-12-28 05:53:01 +00001112 _NOEXCEPT_DEBUG;
Marshall Clow7d914d12015-07-13 20:04:56 +00001113#else
Eric Fiselier47257c42016-12-28 05:53:01 +00001114 _NOEXCEPT_DEBUG_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow7d914d12015-07-13 20:04:56 +00001115 __is_nothrow_swappable<allocator_type>::value);
1116#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001117
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001118 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001119 const value_type* c_str() const _NOEXCEPT {return data();}
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001120 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001121 const value_type* data() const _NOEXCEPT {return _VSTD::__to_raw_pointer(__get_pointer());}
Marshall Clowf532a702016-03-08 15:44:30 +00001122#if _LIBCPP_STD_VER > 14
1123 _LIBCPP_INLINE_VISIBILITY
1124 value_type* data() _NOEXCEPT {return _VSTD::__to_raw_pointer(__get_pointer());}
1125#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001126
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001127 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta6119a82011-05-29 19:57:12 +00001128 allocator_type get_allocator() const _NOEXCEPT {return __alloc();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001129
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001130 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta6119a82011-05-29 19:57:12 +00001131 size_type find(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
Marshall Clow1e00d6d2016-07-21 05:31:24 +00001132 _LIBCPP_INLINE_VISIBILITY
1133 size_type find(__self_view __sv, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001134 size_type find(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001135 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001136 size_type find(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnanta6119a82011-05-29 19:57:12 +00001137 size_type find(value_type __c, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001138
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001139 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta6119a82011-05-29 19:57:12 +00001140 size_type rfind(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
Marshall Clow1e00d6d2016-07-21 05:31:24 +00001141 _LIBCPP_INLINE_VISIBILITY
1142 size_type rfind(__self_view __sv, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001143 size_type rfind(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001144 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001145 size_type rfind(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnanta6119a82011-05-29 19:57:12 +00001146 size_type rfind(value_type __c, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001147
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001148 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta6119a82011-05-29 19:57:12 +00001149 size_type find_first_of(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
Marshall Clow1e00d6d2016-07-21 05:31:24 +00001150 _LIBCPP_INLINE_VISIBILITY
1151 size_type find_first_of(__self_view __sv, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001152 size_type find_first_of(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001153 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001154 size_type find_first_of(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001155 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta6119a82011-05-29 19:57:12 +00001156 size_type find_first_of(value_type __c, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001157
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001158 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta6119a82011-05-29 19:57:12 +00001159 size_type find_last_of(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
Marshall Clow1e00d6d2016-07-21 05:31:24 +00001160 _LIBCPP_INLINE_VISIBILITY
1161 size_type find_last_of(__self_view __sv, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001162 size_type find_last_of(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001163 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001164 size_type find_last_of(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001165 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta6119a82011-05-29 19:57:12 +00001166 size_type find_last_of(value_type __c, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001167
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001168 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta6119a82011-05-29 19:57:12 +00001169 size_type find_first_not_of(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
Marshall Clow1e00d6d2016-07-21 05:31:24 +00001170 _LIBCPP_INLINE_VISIBILITY
1171 size_type find_first_not_of(__self_view __sv, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001172 size_type find_first_not_of(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnanta6119a82011-05-29 19:57:12 +00001173 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001174 size_type find_first_not_of(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnanta6119a82011-05-29 19:57:12 +00001175 _LIBCPP_INLINE_VISIBILITY
1176 size_type find_first_not_of(value_type __c, size_type __pos = 0) const _NOEXCEPT;
1177
1178 _LIBCPP_INLINE_VISIBILITY
1179 size_type find_last_not_of(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
Marshall Clow1e00d6d2016-07-21 05:31:24 +00001180 _LIBCPP_INLINE_VISIBILITY
1181 size_type find_last_not_of(__self_view __sv, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001182 size_type find_last_not_of(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnanta6119a82011-05-29 19:57:12 +00001183 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001184 size_type find_last_not_of(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnanta6119a82011-05-29 19:57:12 +00001185 _LIBCPP_INLINE_VISIBILITY
1186 size_type find_last_not_of(value_type __c, size_type __pos = npos) const _NOEXCEPT;
1187
1188 _LIBCPP_INLINE_VISIBILITY
1189 int compare(const basic_string& __str) const _NOEXCEPT;
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001190 _LIBCPP_INLINE_VISIBILITY
Marshall Clow1e00d6d2016-07-21 05:31:24 +00001191 int compare(__self_view __sv) const _NOEXCEPT;
1192 _LIBCPP_INLINE_VISIBILITY
1193 int compare(size_type __pos1, size_type __n1, __self_view __sv) const;
1194 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001195 int compare(size_type __pos1, size_type __n1, const basic_string& __str) const;
Marshall Clowa93b5e22014-03-04 19:17:19 +00001196 int compare(size_type __pos1, size_type __n1, const basic_string& __str, size_type __pos2, size_type __n2=npos) const;
Marshall Clow6ac8de02016-09-24 22:45:42 +00001197 template <class _Tp>
Eric Fiselier9dbc0532016-10-14 05:29:46 +00001198 inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow6ac8de02016-09-24 22:45:42 +00001199 typename enable_if
1200 <
1201 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1202 int
1203 >::type
1204 compare(size_type __pos1, size_type __n1, const _Tp& __t, size_type __pos2, size_type __n2=npos) const;
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001205 int compare(const value_type* __s) const _NOEXCEPT;
1206 int compare(size_type __pos1, size_type __n1, const value_type* __s) const;
1207 int compare(size_type __pos1, size_type __n1, const value_type* __s, size_type __n2) const;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001208
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001209 _LIBCPP_INLINE_VISIBILITY bool __invariants() const;
Howard Hinnant08dd2532013-04-22 23:55:13 +00001210
1211 _LIBCPP_INLINE_VISIBILITY
1212 bool __is_long() const _NOEXCEPT
1213 {return bool(__r_.first().__s.__size_ & __short_mask);}
1214
Howard Hinnant499cea12013-08-23 17:37:05 +00001215#if _LIBCPP_DEBUG_LEVEL >= 2
1216
1217 bool __dereferenceable(const const_iterator* __i) const;
1218 bool __decrementable(const const_iterator* __i) const;
1219 bool __addable(const const_iterator* __i, ptrdiff_t __n) const;
1220 bool __subscriptable(const const_iterator* __i, ptrdiff_t __n) const;
1221
1222#endif // _LIBCPP_DEBUG_LEVEL >= 2
1223
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001224private:
Howard Hinnanta6119a82011-05-29 19:57:12 +00001225 _LIBCPP_INLINE_VISIBILITY
1226 allocator_type& __alloc() _NOEXCEPT
1227 {return __r_.second();}
1228 _LIBCPP_INLINE_VISIBILITY
1229 const allocator_type& __alloc() const _NOEXCEPT
1230 {return __r_.second();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001231
Evgeniy Stepanov4f01aa82015-10-13 23:48:28 +00001232#ifdef _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant15467182013-04-30 21:44:48 +00001233
Howard Hinnanta6119a82011-05-29 19:57:12 +00001234 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta6119a82011-05-29 19:57:12 +00001235 void __set_short_size(size_type __s) _NOEXCEPT
Howard Hinnant15467182013-04-30 21:44:48 +00001236# if _LIBCPP_BIG_ENDIAN
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001237 {__r_.first().__s.__size_ = (unsigned char)(__s << 1);}
Howard Hinnant15467182013-04-30 21:44:48 +00001238# else
1239 {__r_.first().__s.__size_ = (unsigned char)(__s);}
1240# endif
1241
Howard Hinnanta6119a82011-05-29 19:57:12 +00001242 _LIBCPP_INLINE_VISIBILITY
1243 size_type __get_short_size() const _NOEXCEPT
Howard Hinnant15467182013-04-30 21:44:48 +00001244# if _LIBCPP_BIG_ENDIAN
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001245 {return __r_.first().__s.__size_ >> 1;}
Howard Hinnant15467182013-04-30 21:44:48 +00001246# else
1247 {return __r_.first().__s.__size_;}
1248# endif
1249
Evgeniy Stepanov4f01aa82015-10-13 23:48:28 +00001250#else // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant15467182013-04-30 21:44:48 +00001251
1252 _LIBCPP_INLINE_VISIBILITY
1253 void __set_short_size(size_type __s) _NOEXCEPT
1254# if _LIBCPP_BIG_ENDIAN
1255 {__r_.first().__s.__size_ = (unsigned char)(__s);}
1256# else
1257 {__r_.first().__s.__size_ = (unsigned char)(__s << 1);}
1258# endif
1259
1260 _LIBCPP_INLINE_VISIBILITY
1261 size_type __get_short_size() const _NOEXCEPT
1262# if _LIBCPP_BIG_ENDIAN
1263 {return __r_.first().__s.__size_;}
1264# else
1265 {return __r_.first().__s.__size_ >> 1;}
1266# endif
1267
Evgeniy Stepanov4f01aa82015-10-13 23:48:28 +00001268#endif // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant15467182013-04-30 21:44:48 +00001269
Howard Hinnanta6119a82011-05-29 19:57:12 +00001270 _LIBCPP_INLINE_VISIBILITY
1271 void __set_long_size(size_type __s) _NOEXCEPT
1272 {__r_.first().__l.__size_ = __s;}
1273 _LIBCPP_INLINE_VISIBILITY
1274 size_type __get_long_size() const _NOEXCEPT
1275 {return __r_.first().__l.__size_;}
1276 _LIBCPP_INLINE_VISIBILITY
1277 void __set_size(size_type __s) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001278 {if (__is_long()) __set_long_size(__s); else __set_short_size(__s);}
1279
Howard Hinnanta6119a82011-05-29 19:57:12 +00001280 _LIBCPP_INLINE_VISIBILITY
1281 void __set_long_cap(size_type __s) _NOEXCEPT
1282 {__r_.first().__l.__cap_ = __long_mask | __s;}
1283 _LIBCPP_INLINE_VISIBILITY
1284 size_type __get_long_cap() const _NOEXCEPT
Howard Hinnantec3773c2011-12-01 20:21:04 +00001285 {return __r_.first().__l.__cap_ & size_type(~__long_mask);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001286
Howard Hinnanta6119a82011-05-29 19:57:12 +00001287 _LIBCPP_INLINE_VISIBILITY
1288 void __set_long_pointer(pointer __p) _NOEXCEPT
1289 {__r_.first().__l.__data_ = __p;}
1290 _LIBCPP_INLINE_VISIBILITY
1291 pointer __get_long_pointer() _NOEXCEPT
1292 {return __r_.first().__l.__data_;}
1293 _LIBCPP_INLINE_VISIBILITY
1294 const_pointer __get_long_pointer() const _NOEXCEPT
1295 {return __r_.first().__l.__data_;}
1296 _LIBCPP_INLINE_VISIBILITY
1297 pointer __get_short_pointer() _NOEXCEPT
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001298 {return pointer_traits<pointer>::pointer_to(__r_.first().__s.__data_[0]);}
Howard Hinnanta6119a82011-05-29 19:57:12 +00001299 _LIBCPP_INLINE_VISIBILITY
1300 const_pointer __get_short_pointer() const _NOEXCEPT
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001301 {return pointer_traits<const_pointer>::pointer_to(__r_.first().__s.__data_[0]);}
Howard Hinnanta6119a82011-05-29 19:57:12 +00001302 _LIBCPP_INLINE_VISIBILITY
1303 pointer __get_pointer() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001304 {return __is_long() ? __get_long_pointer() : __get_short_pointer();}
Howard Hinnanta6119a82011-05-29 19:57:12 +00001305 _LIBCPP_INLINE_VISIBILITY
1306 const_pointer __get_pointer() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001307 {return __is_long() ? __get_long_pointer() : __get_short_pointer();}
1308
Howard Hinnanta6119a82011-05-29 19:57:12 +00001309 _LIBCPP_INLINE_VISIBILITY
1310 void __zero() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001311 {
1312 size_type (&__a)[__n_words] = __r_.first().__r.__words;
1313 for (unsigned __i = 0; __i < __n_words; ++__i)
1314 __a[__i] = 0;
1315 }
1316
1317 template <size_type __a> static
Howard Hinnanta6119a82011-05-29 19:57:12 +00001318 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7f764502013-08-14 18:00:20 +00001319 size_type __align_it(size_type __s) _NOEXCEPT
Eric Fiseliere2d48922015-08-28 07:02:42 +00001320 {return (__s + (__a-1)) & ~(__a-1);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001321 enum {__alignment = 16};
Howard Hinnanta6119a82011-05-29 19:57:12 +00001322 static _LIBCPP_INLINE_VISIBILITY
1323 size_type __recommend(size_type __s) _NOEXCEPT
Eric Fiseliere2d48922015-08-28 07:02:42 +00001324 {return (__s < __min_cap ? static_cast<size_type>(__min_cap) :
Howard Hinnant7f764502013-08-14 18:00:20 +00001325 __align_it<sizeof(value_type) < __alignment ?
Howard Hinnanta6119a82011-05-29 19:57:12 +00001326 __alignment/sizeof(value_type) : 1 > (__s+1)) - 1;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001327
Eric Fiselier6dbed462016-09-16 00:00:48 +00001328 inline _LIBCPP_EXTERN_TEMPLATE_INLINE_VISIBILITY
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001329 void __init(const value_type* __s, size_type __sz, size_type __reserve);
Eric Fiselier6dbed462016-09-16 00:00:48 +00001330 inline _LIBCPP_EXTERN_TEMPLATE_INLINE_VISIBILITY
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001331 void __init(const value_type* __s, size_type __sz);
Eric Fiselier6dbed462016-09-16 00:00:48 +00001332 inline _LIBCPP_EXTERN_TEMPLATE_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001333 void __init(size_type __n, value_type __c);
Howard Hinnant324bb032010-08-22 00:02:43 +00001334
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001335 template <class _InputIterator>
Eric Fiselier6dbed462016-09-16 00:00:48 +00001336 inline _LIBCPP_EXTERN_TEMPLATE_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001337 typename enable_if
1338 <
Marshall Clowdf9db312016-01-13 21:54:34 +00001339 __is_exactly_input_iterator<_InputIterator>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001340 void
1341 >::type
1342 __init(_InputIterator __first, _InputIterator __last);
1343
1344 template <class _ForwardIterator>
Eric Fiselier6dbed462016-09-16 00:00:48 +00001345 inline _LIBCPP_EXTERN_TEMPLATE_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001346 typename enable_if
1347 <
1348 __is_forward_iterator<_ForwardIterator>::value,
1349 void
1350 >::type
1351 __init(_ForwardIterator __first, _ForwardIterator __last);
1352
1353 void __grow_by(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
Howard Hinnant324bb032010-08-22 00:02:43 +00001354 size_type __n_copy, size_type __n_del, size_type __n_add = 0);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001355 void __grow_by_and_replace(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
1356 size_type __n_copy, size_type __n_del,
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001357 size_type __n_add, const value_type* __p_new_stuff);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001358
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001359 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001360 void __erase_to_end(size_type __pos);
1361
Howard Hinnante32b5e22010-11-17 17:55:08 +00001362 _LIBCPP_INLINE_VISIBILITY
1363 void __copy_assign_alloc(const basic_string& __str)
1364 {__copy_assign_alloc(__str, integral_constant<bool,
1365 __alloc_traits::propagate_on_container_copy_assignment::value>());}
1366
1367 _LIBCPP_INLINE_VISIBILITY
1368 void __copy_assign_alloc(const basic_string& __str, true_type)
1369 {
Marshall Clow9247fd22017-01-31 03:40:52 +00001370 if (__alloc() == __str.__alloc())
1371 __alloc() = __str.__alloc();
1372 else
Howard Hinnante32b5e22010-11-17 17:55:08 +00001373 {
Marshall Clow9247fd22017-01-31 03:40:52 +00001374 if (!__str.__is_long())
1375 {
1376 clear();
1377 shrink_to_fit();
1378 __alloc() = __str.__alloc();
1379 }
1380 else
1381 {
1382 allocator_type __a = __str.__alloc();
1383 pointer __p = __alloc_traits::allocate(__a, __str.__get_long_cap());
1384 clear();
1385 shrink_to_fit();
1386 __alloc() = _VSTD::move(__a);
1387 __set_long_pointer(__p);
1388 __set_long_cap(__str.__get_long_cap());
1389 __set_long_size(__str.size());
1390 }
Howard Hinnante32b5e22010-11-17 17:55:08 +00001391 }
Howard Hinnante32b5e22010-11-17 17:55:08 +00001392 }
1393
1394 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantec3773c2011-12-01 20:21:04 +00001395 void __copy_assign_alloc(const basic_string&, false_type) _NOEXCEPT
Howard Hinnante32b5e22010-11-17 17:55:08 +00001396 {}
1397
1398#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001399 _LIBCPP_INLINE_VISIBILITY
Marshall Clowaf961ed2015-08-18 18:57:00 +00001400 void __move_assign(basic_string& __str, false_type)
1401 _NOEXCEPT_(__alloc_traits::is_always_equal::value);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001402 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant53f7d4c2011-06-03 18:40:47 +00001403 void __move_assign(basic_string& __str, true_type)
Marshall Clowaf961ed2015-08-18 18:57:00 +00001404#if _LIBCPP_STD_VER > 14
1405 _NOEXCEPT;
1406#else
Howard Hinnant53f7d4c2011-06-03 18:40:47 +00001407 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnante32b5e22010-11-17 17:55:08 +00001408#endif
Marshall Clowaf961ed2015-08-18 18:57:00 +00001409#endif
Howard Hinnante32b5e22010-11-17 17:55:08 +00001410
1411 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3fdbbd22011-08-17 20:36:18 +00001412 void
Howard Hinnant9cbee432011-09-02 20:42:31 +00001413 __move_assign_alloc(basic_string& __str)
Howard Hinnant3fdbbd22011-08-17 20:36:18 +00001414 _NOEXCEPT_(
1415 !__alloc_traits::propagate_on_container_move_assignment::value ||
1416 is_nothrow_move_assignable<allocator_type>::value)
1417 {__move_assign_alloc(__str, integral_constant<bool,
1418 __alloc_traits::propagate_on_container_move_assignment::value>());}
1419
1420 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9cbee432011-09-02 20:42:31 +00001421 void __move_assign_alloc(basic_string& __c, true_type)
Howard Hinnant3fdbbd22011-08-17 20:36:18 +00001422 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
1423 {
1424 __alloc() = _VSTD::move(__c.__alloc());
1425 }
1426
1427 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantec3773c2011-12-01 20:21:04 +00001428 void __move_assign_alloc(basic_string&, false_type)
Howard Hinnant3fdbbd22011-08-17 20:36:18 +00001429 _NOEXCEPT
1430 {}
1431
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001432 _LIBCPP_INLINE_VISIBILITY void __invalidate_all_iterators();
1433 _LIBCPP_INLINE_VISIBILITY void __invalidate_iterators_past(size_type);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001434
1435 friend basic_string operator+<>(const basic_string&, const basic_string&);
1436 friend basic_string operator+<>(const value_type*, const basic_string&);
1437 friend basic_string operator+<>(value_type, const basic_string&);
1438 friend basic_string operator+<>(const basic_string&, const value_type*);
1439 friend basic_string operator+<>(const basic_string&, value_type);
1440};
1441
1442template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001443inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001444void
1445basic_string<_CharT, _Traits, _Allocator>::__invalidate_all_iterators()
1446{
Howard Hinnant499cea12013-08-23 17:37:05 +00001447#if _LIBCPP_DEBUG_LEVEL >= 2
1448 __get_db()->__invalidate_all(this);
1449#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001450}
1451
1452template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001453inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001454void
Howard Hinnantec3773c2011-12-01 20:21:04 +00001455basic_string<_CharT, _Traits, _Allocator>::__invalidate_iterators_past(size_type
Howard Hinnant499cea12013-08-23 17:37:05 +00001456#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantec3773c2011-12-01 20:21:04 +00001457 __pos
1458#endif
1459 )
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001460{
Howard Hinnant499cea12013-08-23 17:37:05 +00001461#if _LIBCPP_DEBUG_LEVEL >= 2
1462 __c_node* __c = __get_db()->__find_c_and_lock(this);
1463 if (__c)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001464 {
Howard Hinnant499cea12013-08-23 17:37:05 +00001465 const_pointer __new_last = __get_pointer() + __pos;
1466 for (__i_node** __p = __c->end_; __p != __c->beg_; )
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001467 {
Howard Hinnant499cea12013-08-23 17:37:05 +00001468 --__p;
1469 const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_);
1470 if (__i->base() > __new_last)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001471 {
Howard Hinnant499cea12013-08-23 17:37:05 +00001472 (*__p)->__c_ = nullptr;
1473 if (--__c->end_ != __p)
1474 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001475 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001476 }
Howard Hinnant499cea12013-08-23 17:37:05 +00001477 __get_db()->unlock();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001478 }
Howard Hinnant499cea12013-08-23 17:37:05 +00001479#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001480}
1481
1482template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001483inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant53f7d4c2011-06-03 18:40:47 +00001484basic_string<_CharT, _Traits, _Allocator>::basic_string()
Marshall Clowc912c0c2015-06-04 02:05:41 +00001485 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001486{
Howard Hinnant499cea12013-08-23 17:37:05 +00001487#if _LIBCPP_DEBUG_LEVEL >= 2
1488 __get_db()->__insert_c(this);
1489#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001490 __zero();
1491}
1492
1493template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001494inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001495basic_string<_CharT, _Traits, _Allocator>::basic_string(const allocator_type& __a)
Eric Fiselier692177d2015-07-18 20:40:46 +00001496#if _LIBCPP_STD_VER <= 14
1497 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value)
1498#else
1499 _NOEXCEPT
1500#endif
1501: __r_(__a)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001502{
Howard Hinnant499cea12013-08-23 17:37:05 +00001503#if _LIBCPP_DEBUG_LEVEL >= 2
1504 __get_db()->__insert_c(this);
1505#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001506 __zero();
1507}
1508
1509template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier6dbed462016-09-16 00:00:48 +00001510void basic_string<_CharT, _Traits, _Allocator>::__init(const value_type* __s,
1511 size_type __sz,
1512 size_type __reserve)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001513{
1514 if (__reserve > max_size())
1515 this->__throw_length_error();
1516 pointer __p;
1517 if (__reserve < __min_cap)
1518 {
1519 __set_short_size(__sz);
1520 __p = __get_short_pointer();
1521 }
1522 else
1523 {
1524 size_type __cap = __recommend(__reserve);
Howard Hinnante32b5e22010-11-17 17:55:08 +00001525 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001526 __set_long_pointer(__p);
1527 __set_long_cap(__cap+1);
1528 __set_long_size(__sz);
1529 }
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001530 traits_type::copy(_VSTD::__to_raw_pointer(__p), __s, __sz);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001531 traits_type::assign(__p[__sz], value_type());
1532}
1533
1534template <class _CharT, class _Traits, class _Allocator>
1535void
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001536basic_string<_CharT, _Traits, _Allocator>::__init(const value_type* __s, size_type __sz)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001537{
1538 if (__sz > max_size())
1539 this->__throw_length_error();
1540 pointer __p;
1541 if (__sz < __min_cap)
1542 {
1543 __set_short_size(__sz);
1544 __p = __get_short_pointer();
1545 }
1546 else
1547 {
1548 size_type __cap = __recommend(__sz);
Howard Hinnante32b5e22010-11-17 17:55:08 +00001549 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001550 __set_long_pointer(__p);
1551 __set_long_cap(__cap+1);
1552 __set_long_size(__sz);
1553 }
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001554 traits_type::copy(_VSTD::__to_raw_pointer(__p), __s, __sz);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001555 traits_type::assign(__p[__sz], value_type());
1556}
1557
1558template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001559inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier0eaf2e82017-02-17 01:17:10 +00001560basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001561{
Howard Hinnant499cea12013-08-23 17:37:05 +00001562 _LIBCPP_ASSERT(__s != nullptr, "basic_string(const char*) detected nullptr");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001563 __init(__s, traits_type::length(__s));
Howard Hinnant499cea12013-08-23 17:37:05 +00001564#if _LIBCPP_DEBUG_LEVEL >= 2
1565 __get_db()->__insert_c(this);
1566#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001567}
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, const _Allocator& __a)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001572 : __r_(__a)
1573{
Howard Hinnant499cea12013-08-23 17:37:05 +00001574 _LIBCPP_ASSERT(__s != nullptr, "basic_string(const char*, allocator) detected nullptr");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001575 __init(__s, traits_type::length(__s));
Howard Hinnant499cea12013-08-23 17:37:05 +00001576#if _LIBCPP_DEBUG_LEVEL >= 2
1577 __get_db()->__insert_c(this);
1578#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001579}
1580
1581template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001582inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier0eaf2e82017-02-17 01:17:10 +00001583basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, size_type __n)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001584{
Howard Hinnant499cea12013-08-23 17:37:05 +00001585 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "basic_string(const char*, n) detected nullptr");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001586 __init(__s, __n);
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, const _Allocator& __a)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001595 : __r_(__a)
1596{
Howard Hinnant499cea12013-08-23 17:37:05 +00001597 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "basic_string(const char*, n, allocator) detected nullptr");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001598 __init(__s, __n);
Howard Hinnant499cea12013-08-23 17:37:05 +00001599#if _LIBCPP_DEBUG_LEVEL >= 2
1600 __get_db()->__insert_c(this);
1601#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001602}
1603
1604template <class _CharT, class _Traits, class _Allocator>
1605basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str)
Howard Hinnante32b5e22010-11-17 17:55:08 +00001606 : __r_(__alloc_traits::select_on_container_copy_construction(__str.__alloc()))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001607{
1608 if (!__str.__is_long())
1609 __r_.first().__r = __str.__r_.first().__r;
1610 else
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001611 __init(_VSTD::__to_raw_pointer(__str.__get_long_pointer()), __str.__get_long_size());
Howard Hinnant499cea12013-08-23 17:37:05 +00001612#if _LIBCPP_DEBUG_LEVEL >= 2
1613 __get_db()->__insert_c(this);
1614#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001615}
1616
1617template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier0eaf2e82017-02-17 01:17:10 +00001618basic_string<_CharT, _Traits, _Allocator>::basic_string(
1619 const basic_string& __str, const allocator_type& __a)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001620 : __r_(__a)
1621{
1622 if (!__str.__is_long())
1623 __r_.first().__r = __str.__r_.first().__r;
1624 else
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001625 __init(_VSTD::__to_raw_pointer(__str.__get_long_pointer()), __str.__get_long_size());
Howard Hinnant499cea12013-08-23 17:37:05 +00001626#if _LIBCPP_DEBUG_LEVEL >= 2
1627 __get_db()->__insert_c(this);
1628#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001629}
1630
Howard Hinnant73d21a42010-09-04 23:28:19 +00001631#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001632
1633template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001634inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant53f7d4c2011-06-03 18:40:47 +00001635basic_string<_CharT, _Traits, _Allocator>::basic_string(basic_string&& __str)
Marshall Clow7b193f72015-06-03 19:56:43 +00001636#if _LIBCPP_STD_VER <= 14
Howard Hinnant53f7d4c2011-06-03 18:40:47 +00001637 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Marshall Clow7b193f72015-06-03 19:56:43 +00001638#else
1639 _NOEXCEPT
1640#endif
Howard Hinnant0949eed2011-06-30 21:18:19 +00001641 : __r_(_VSTD::move(__str.__r_))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001642{
1643 __str.__zero();
Howard Hinnant499cea12013-08-23 17:37:05 +00001644#if _LIBCPP_DEBUG_LEVEL >= 2
1645 __get_db()->__insert_c(this);
1646 if (__is_long())
1647 __get_db()->swap(this, &__str);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001648#endif
1649}
1650
1651template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001652inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001653basic_string<_CharT, _Traits, _Allocator>::basic_string(basic_string&& __str, const allocator_type& __a)
Howard Hinnante32b5e22010-11-17 17:55:08 +00001654 : __r_(__a)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001655{
Marshall Clowd5549cc2014-07-17 15:32:20 +00001656 if (__str.__is_long() && __a != __str.__alloc()) // copy, not move
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001657 __init(_VSTD::__to_raw_pointer(__str.__get_long_pointer()), __str.__get_long_size());
Marshall Clowd5549cc2014-07-17 15:32:20 +00001658 else
1659 {
1660 __r_.first().__r = __str.__r_.first().__r;
1661 __str.__zero();
1662 }
Howard Hinnant499cea12013-08-23 17:37:05 +00001663#if _LIBCPP_DEBUG_LEVEL >= 2
1664 __get_db()->__insert_c(this);
1665 if (__is_long())
1666 __get_db()->swap(this, &__str);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001667#endif
1668}
1669
Howard Hinnant73d21a42010-09-04 23:28:19 +00001670#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001671
1672template <class _CharT, class _Traits, class _Allocator>
1673void
1674basic_string<_CharT, _Traits, _Allocator>::__init(size_type __n, value_type __c)
1675{
1676 if (__n > max_size())
1677 this->__throw_length_error();
1678 pointer __p;
1679 if (__n < __min_cap)
1680 {
1681 __set_short_size(__n);
1682 __p = __get_short_pointer();
1683 }
1684 else
1685 {
1686 size_type __cap = __recommend(__n);
Howard Hinnante32b5e22010-11-17 17:55:08 +00001687 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001688 __set_long_pointer(__p);
1689 __set_long_cap(__cap+1);
1690 __set_long_size(__n);
1691 }
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001692 traits_type::assign(_VSTD::__to_raw_pointer(__p), __n, __c);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001693 traits_type::assign(__p[__n], value_type());
1694}
1695
1696template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001697inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier0eaf2e82017-02-17 01:17:10 +00001698basic_string<_CharT, _Traits, _Allocator>::basic_string(size_type __n, _CharT __c)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001699{
1700 __init(__n, __c);
Howard Hinnant499cea12013-08-23 17:37:05 +00001701#if _LIBCPP_DEBUG_LEVEL >= 2
1702 __get_db()->__insert_c(this);
1703#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001704}
1705
1706template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001707inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier0eaf2e82017-02-17 01:17:10 +00001708basic_string<_CharT, _Traits, _Allocator>::basic_string(size_type __n, _CharT __c, const _Allocator& __a)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001709 : __r_(__a)
1710{
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
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001717template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier0eaf2e82017-02-17 01:17:10 +00001718basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str,
1719 size_type __pos, size_type __n,
1720 const _Allocator& __a)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001721 : __r_(__a)
1722{
1723 size_type __str_sz = __str.size();
1724 if (__pos > __str_sz)
1725 this->__throw_out_of_range();
Howard Hinnant0949eed2011-06-30 21:18:19 +00001726 __init(__str.data() + __pos, _VSTD::min(__n, __str_sz - __pos));
Howard Hinnant499cea12013-08-23 17:37:05 +00001727#if _LIBCPP_DEBUG_LEVEL >= 2
1728 __get_db()->__insert_c(this);
1729#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001730}
1731
1732template <class _CharT, class _Traits, class _Allocator>
Marshall Clowf4f7d8f2016-04-07 18:13:41 +00001733inline _LIBCPP_INLINE_VISIBILITY
1734basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str, size_type __pos,
Eric Fiselier0eaf2e82017-02-17 01:17:10 +00001735 const _Allocator& __a)
Marshall Clowf4f7d8f2016-04-07 18:13:41 +00001736 : __r_(__a)
1737{
1738 size_type __str_sz = __str.size();
1739 if (__pos > __str_sz)
1740 this->__throw_out_of_range();
1741 __init(__str.data() + __pos, __str_sz - __pos);
1742#if _LIBCPP_DEBUG_LEVEL >= 2
1743 __get_db()->__insert_c(this);
1744#endif
1745}
1746
1747template <class _CharT, class _Traits, class _Allocator>
Marshall Clowdb7fa112016-11-14 18:22:19 +00001748template <class _Tp>
1749basic_string<_CharT, _Traits, _Allocator>::basic_string(
1750 const _Tp& __t, size_type __pos, size_type __n, const allocator_type& __a,
1751 typename enable_if<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value, void>::type *)
1752 : __r_(__a)
1753{
1754 __self_view __sv = __self_view(__t).substr(__pos, __n);
1755 __init(__sv.data(), __sv.size());
1756#if _LIBCPP_DEBUG_LEVEL >= 2
1757 __get_db()->__insert_c(this);
Eric Fiselier0eaf2e82017-02-17 01:17:10 +00001758#endif
Marshall Clowdb7fa112016-11-14 18:22:19 +00001759}
1760
1761template <class _CharT, class _Traits, class _Allocator>
Marshall Clow1e00d6d2016-07-21 05:31:24 +00001762inline _LIBCPP_INLINE_VISIBILITY
1763basic_string<_CharT, _Traits, _Allocator>::basic_string(__self_view __sv)
1764{
1765 __init(__sv.data(), __sv.size());
1766#if _LIBCPP_DEBUG_LEVEL >= 2
1767 __get_db()->__insert_c(this);
1768#endif
1769}
1770
1771template <class _CharT, class _Traits, class _Allocator>
1772inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier0eaf2e82017-02-17 01:17:10 +00001773basic_string<_CharT, _Traits, _Allocator>::basic_string(__self_view __sv, const _Allocator& __a)
Marshall Clow1e00d6d2016-07-21 05:31:24 +00001774 : __r_(__a)
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>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001783template <class _InputIterator>
1784typename enable_if
1785<
Marshall Clowdf9db312016-01-13 21:54:34 +00001786 __is_exactly_input_iterator<_InputIterator>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001787 void
1788>::type
1789basic_string<_CharT, _Traits, _Allocator>::__init(_InputIterator __first, _InputIterator __last)
1790{
1791 __zero();
1792#ifndef _LIBCPP_NO_EXCEPTIONS
1793 try
1794 {
Howard Hinnant324bb032010-08-22 00:02:43 +00001795#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001796 for (; __first != __last; ++__first)
1797 push_back(*__first);
1798#ifndef _LIBCPP_NO_EXCEPTIONS
1799 }
1800 catch (...)
1801 {
1802 if (__is_long())
Howard Hinnante32b5e22010-11-17 17:55:08 +00001803 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001804 throw;
1805 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001806#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001807}
1808
1809template <class _CharT, class _Traits, class _Allocator>
1810template <class _ForwardIterator>
1811typename enable_if
1812<
1813 __is_forward_iterator<_ForwardIterator>::value,
1814 void
1815>::type
1816basic_string<_CharT, _Traits, _Allocator>::__init(_ForwardIterator __first, _ForwardIterator __last)
1817{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001818 size_type __sz = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001819 if (__sz > max_size())
1820 this->__throw_length_error();
1821 pointer __p;
1822 if (__sz < __min_cap)
1823 {
1824 __set_short_size(__sz);
1825 __p = __get_short_pointer();
1826 }
1827 else
1828 {
1829 size_type __cap = __recommend(__sz);
Howard Hinnante32b5e22010-11-17 17:55:08 +00001830 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001831 __set_long_pointer(__p);
1832 __set_long_cap(__cap+1);
1833 __set_long_size(__sz);
1834 }
Eric Fiselierb9919752014-10-27 19:28:20 +00001835 for (; __first != __last; ++__first, (void) ++__p)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001836 traits_type::assign(*__p, *__first);
1837 traits_type::assign(*__p, value_type());
1838}
1839
1840template <class _CharT, class _Traits, class _Allocator>
1841template<class _InputIterator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001842inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001843basic_string<_CharT, _Traits, _Allocator>::basic_string(_InputIterator __first, _InputIterator __last)
1844{
1845 __init(__first, __last);
Howard Hinnant499cea12013-08-23 17:37:05 +00001846#if _LIBCPP_DEBUG_LEVEL >= 2
1847 __get_db()->__insert_c(this);
1848#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001849}
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 const allocator_type& __a)
1856 : __r_(__a)
1857{
1858 __init(__first, __last);
Howard Hinnant499cea12013-08-23 17:37:05 +00001859#if _LIBCPP_DEBUG_LEVEL >= 2
1860 __get_db()->__insert_c(this);
1861#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001862}
1863
Howard Hinnante3e32912011-08-12 21:56:02 +00001864#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
1865
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001866template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001867inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier0eaf2e82017-02-17 01:17:10 +00001868basic_string<_CharT, _Traits, _Allocator>::basic_string(
1869 initializer_list<_CharT> __il)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001870{
1871 __init(__il.begin(), __il.end());
Howard Hinnant499cea12013-08-23 17:37:05 +00001872#if _LIBCPP_DEBUG_LEVEL >= 2
1873 __get_db()->__insert_c(this);
1874#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001875}
1876
1877template <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, const _Allocator& __a)
1881
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001882 : __r_(__a)
1883{
1884 __init(__il.begin(), __il.end());
Howard Hinnant499cea12013-08-23 17:37:05 +00001885#if _LIBCPP_DEBUG_LEVEL >= 2
1886 __get_db()->__insert_c(this);
1887#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001888}
1889
Howard Hinnante3e32912011-08-12 21:56:02 +00001890#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
1891
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001892template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001893basic_string<_CharT, _Traits, _Allocator>::~basic_string()
1894{
Howard Hinnant499cea12013-08-23 17:37:05 +00001895#if _LIBCPP_DEBUG_LEVEL >= 2
1896 __get_db()->__erase_c(this);
1897#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001898 if (__is_long())
Howard Hinnante32b5e22010-11-17 17:55:08 +00001899 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001900}
1901
1902template <class _CharT, class _Traits, class _Allocator>
1903void
1904basic_string<_CharT, _Traits, _Allocator>::__grow_by_and_replace
1905 (size_type __old_cap, size_type __delta_cap, size_type __old_sz,
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001906 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 +00001907{
1908 size_type __ms = max_size();
1909 if (__delta_cap > __ms - __old_cap - 1)
1910 this->__throw_length_error();
1911 pointer __old_p = __get_pointer();
1912 size_type __cap = __old_cap < __ms / 2 - __alignment ?
Howard Hinnant0949eed2011-06-30 21:18:19 +00001913 __recommend(_VSTD::max(__old_cap + __delta_cap, 2 * __old_cap)) :
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001914 __ms - 1;
Howard Hinnante32b5e22010-11-17 17:55:08 +00001915 pointer __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001916 __invalidate_all_iterators();
1917 if (__n_copy != 0)
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001918 traits_type::copy(_VSTD::__to_raw_pointer(__p),
1919 _VSTD::__to_raw_pointer(__old_p), __n_copy);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001920 if (__n_add != 0)
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001921 traits_type::copy(_VSTD::__to_raw_pointer(__p) + __n_copy, __p_new_stuff, __n_add);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001922 size_type __sec_cp_sz = __old_sz - __n_del - __n_copy;
1923 if (__sec_cp_sz != 0)
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001924 traits_type::copy(_VSTD::__to_raw_pointer(__p) + __n_copy + __n_add,
1925 _VSTD::__to_raw_pointer(__old_p) + __n_copy + __n_del, __sec_cp_sz);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001926 if (__old_cap+1 != __min_cap)
Howard Hinnante32b5e22010-11-17 17:55:08 +00001927 __alloc_traits::deallocate(__alloc(), __old_p, __old_cap+1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001928 __set_long_pointer(__p);
1929 __set_long_cap(__cap+1);
1930 __old_sz = __n_copy + __n_add + __sec_cp_sz;
1931 __set_long_size(__old_sz);
1932 traits_type::assign(__p[__old_sz], value_type());
1933}
1934
1935template <class _CharT, class _Traits, class _Allocator>
1936void
1937basic_string<_CharT, _Traits, _Allocator>::__grow_by(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
1938 size_type __n_copy, size_type __n_del, size_type __n_add)
1939{
1940 size_type __ms = max_size();
Marshall Clowecc8d7b2013-11-06 14:24:38 +00001941 if (__delta_cap > __ms - __old_cap)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001942 this->__throw_length_error();
1943 pointer __old_p = __get_pointer();
1944 size_type __cap = __old_cap < __ms / 2 - __alignment ?
Howard Hinnant0949eed2011-06-30 21:18:19 +00001945 __recommend(_VSTD::max(__old_cap + __delta_cap, 2 * __old_cap)) :
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001946 __ms - 1;
Howard Hinnante32b5e22010-11-17 17:55:08 +00001947 pointer __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001948 __invalidate_all_iterators();
1949 if (__n_copy != 0)
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001950 traits_type::copy(_VSTD::__to_raw_pointer(__p),
1951 _VSTD::__to_raw_pointer(__old_p), __n_copy);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001952 size_type __sec_cp_sz = __old_sz - __n_del - __n_copy;
1953 if (__sec_cp_sz != 0)
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001954 traits_type::copy(_VSTD::__to_raw_pointer(__p) + __n_copy + __n_add,
1955 _VSTD::__to_raw_pointer(__old_p) + __n_copy + __n_del,
1956 __sec_cp_sz);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001957 if (__old_cap+1 != __min_cap)
Howard Hinnante32b5e22010-11-17 17:55:08 +00001958 __alloc_traits::deallocate(__alloc(), __old_p, __old_cap+1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001959 __set_long_pointer(__p);
1960 __set_long_cap(__cap+1);
1961}
1962
1963// assign
1964
1965template <class _CharT, class _Traits, class _Allocator>
1966basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001967basic_string<_CharT, _Traits, _Allocator>::assign(const value_type* __s, size_type __n)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001968{
Alp Tokerec34c482014-05-15 11:27:39 +00001969 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::assign received nullptr");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001970 size_type __cap = capacity();
1971 if (__cap >= __n)
1972 {
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001973 value_type* __p = _VSTD::__to_raw_pointer(__get_pointer());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001974 traits_type::move(__p, __s, __n);
1975 traits_type::assign(__p[__n], value_type());
1976 __set_size(__n);
1977 __invalidate_iterators_past(__n);
1978 }
1979 else
1980 {
1981 size_type __sz = size();
1982 __grow_by_and_replace(__cap, __n - __cap, __sz, 0, __sz, __n, __s);
1983 }
1984 return *this;
1985}
1986
1987template <class _CharT, class _Traits, class _Allocator>
1988basic_string<_CharT, _Traits, _Allocator>&
1989basic_string<_CharT, _Traits, _Allocator>::assign(size_type __n, value_type __c)
1990{
1991 size_type __cap = capacity();
1992 if (__cap < __n)
1993 {
1994 size_type __sz = size();
1995 __grow_by(__cap, __n - __cap, __sz, 0, __sz);
1996 }
1997 else
1998 __invalidate_iterators_past(__n);
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001999 value_type* __p = _VSTD::__to_raw_pointer(__get_pointer());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002000 traits_type::assign(__p, __n, __c);
2001 traits_type::assign(__p[__n], value_type());
2002 __set_size(__n);
2003 return *this;
2004}
2005
2006template <class _CharT, class _Traits, class _Allocator>
2007basic_string<_CharT, _Traits, _Allocator>&
2008basic_string<_CharT, _Traits, _Allocator>::operator=(value_type __c)
2009{
2010 pointer __p;
2011 if (__is_long())
2012 {
2013 __p = __get_long_pointer();
2014 __set_long_size(1);
2015 }
2016 else
2017 {
2018 __p = __get_short_pointer();
2019 __set_short_size(1);
2020 }
2021 traits_type::assign(*__p, __c);
2022 traits_type::assign(*++__p, value_type());
2023 __invalidate_iterators_past(1);
2024 return *this;
2025}
2026
2027template <class _CharT, class _Traits, class _Allocator>
Howard Hinnante32b5e22010-11-17 17:55:08 +00002028basic_string<_CharT, _Traits, _Allocator>&
2029basic_string<_CharT, _Traits, _Allocator>::operator=(const basic_string& __str)
2030{
2031 if (this != &__str)
2032 {
2033 __copy_assign_alloc(__str);
Marshall Clowf40ec902016-03-09 18:08:29 +00002034 assign(__str.data(), __str.size());
Howard Hinnante32b5e22010-11-17 17:55:08 +00002035 }
2036 return *this;
2037}
2038
2039#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
2040
2041template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002042inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnante32b5e22010-11-17 17:55:08 +00002043void
2044basic_string<_CharT, _Traits, _Allocator>::__move_assign(basic_string& __str, false_type)
Marshall Clowaf961ed2015-08-18 18:57:00 +00002045 _NOEXCEPT_(__alloc_traits::is_always_equal::value)
Howard Hinnante32b5e22010-11-17 17:55:08 +00002046{
2047 if (__alloc() != __str.__alloc())
2048 assign(__str);
2049 else
2050 __move_assign(__str, true_type());
2051}
2052
2053template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002054inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnante32b5e22010-11-17 17:55:08 +00002055void
2056basic_string<_CharT, _Traits, _Allocator>::__move_assign(basic_string& __str, true_type)
Marshall Clowaf961ed2015-08-18 18:57:00 +00002057#if _LIBCPP_STD_VER > 14
2058 _NOEXCEPT
2059#else
Howard Hinnant53f7d4c2011-06-03 18:40:47 +00002060 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Marshall Clowaf961ed2015-08-18 18:57:00 +00002061#endif
Howard Hinnante32b5e22010-11-17 17:55:08 +00002062{
2063 clear();
2064 shrink_to_fit();
Howard Hinnant3fdbbd22011-08-17 20:36:18 +00002065 __r_.first() = __str.__r_.first();
2066 __move_assign_alloc(__str);
Howard Hinnante32b5e22010-11-17 17:55:08 +00002067 __str.__zero();
2068}
2069
2070template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002071inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnante32b5e22010-11-17 17:55:08 +00002072basic_string<_CharT, _Traits, _Allocator>&
2073basic_string<_CharT, _Traits, _Allocator>::operator=(basic_string&& __str)
Marshall Clowaf961ed2015-08-18 18:57:00 +00002074 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
Howard Hinnante32b5e22010-11-17 17:55:08 +00002075{
2076 __move_assign(__str, integral_constant<bool,
2077 __alloc_traits::propagate_on_container_move_assignment::value>());
2078 return *this;
2079}
2080
2081#endif
2082
2083template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002084template<class _InputIterator>
2085typename enable_if
2086<
Marshall Clowdf9db312016-01-13 21:54:34 +00002087 __is_exactly_input_iterator <_InputIterator>::value
2088 || !__libcpp_string_gets_noexcept_iterator<_InputIterator>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002089 basic_string<_CharT, _Traits, _Allocator>&
2090>::type
2091basic_string<_CharT, _Traits, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
2092{
Marshall Clowd979eed2016-09-05 01:54:30 +00002093 const basic_string __temp(__first, __last, __alloc());
Marshall Clowdf9db312016-01-13 21:54:34 +00002094 assign(__temp.data(), __temp.size());
Argyrios Kyrtzidis1dc6f7a2012-10-13 02:03:45 +00002095 return *this;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002096}
2097
2098template <class _CharT, class _Traits, class _Allocator>
2099template<class _ForwardIterator>
2100typename enable_if
2101<
Marshall Clowdf9db312016-01-13 21:54:34 +00002102 __is_forward_iterator<_ForwardIterator>::value
2103 && __libcpp_string_gets_noexcept_iterator<_ForwardIterator>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002104 basic_string<_CharT, _Traits, _Allocator>&
2105>::type
2106basic_string<_CharT, _Traits, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
2107{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002108 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002109 size_type __cap = capacity();
2110 if (__cap < __n)
2111 {
2112 size_type __sz = size();
2113 __grow_by(__cap, __n - __cap, __sz, 0, __sz);
2114 }
2115 else
2116 __invalidate_iterators_past(__n);
2117 pointer __p = __get_pointer();
2118 for (; __first != __last; ++__first, ++__p)
2119 traits_type::assign(*__p, *__first);
2120 traits_type::assign(*__p, value_type());
2121 __set_size(__n);
2122 return *this;
2123}
2124
2125template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002126basic_string<_CharT, _Traits, _Allocator>&
2127basic_string<_CharT, _Traits, _Allocator>::assign(const basic_string& __str, size_type __pos, size_type __n)
2128{
2129 size_type __sz = __str.size();
2130 if (__pos > __sz)
2131 this->__throw_out_of_range();
Howard Hinnant0949eed2011-06-30 21:18:19 +00002132 return assign(__str.data() + __pos, _VSTD::min(__n, __sz - __pos));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002133}
2134
2135template <class _CharT, class _Traits, class _Allocator>
Marshall Clow6ac8de02016-09-24 22:45:42 +00002136template <class _Tp>
2137typename enable_if
2138<
2139 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
2140 basic_string<_CharT, _Traits, _Allocator>&
2141>::type
2142basic_string<_CharT, _Traits, _Allocator>::assign(const _Tp & __t, size_type __pos, size_type __n)
Marshall Clow1e00d6d2016-07-21 05:31:24 +00002143{
Marshall Clow6ac8de02016-09-24 22:45:42 +00002144 __self_view __sv = __t;
Marshall Clow1e00d6d2016-07-21 05:31:24 +00002145 size_type __sz = __sv.size();
2146 if (__pos > __sz)
2147 this->__throw_out_of_range();
2148 return assign(__sv.data() + __pos, _VSTD::min(__n, __sz - __pos));
2149}
2150
2151
2152template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002153basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002154basic_string<_CharT, _Traits, _Allocator>::assign(const value_type* __s)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002155{
Alp Tokerec34c482014-05-15 11:27:39 +00002156 _LIBCPP_ASSERT(__s != nullptr, "string::assign received nullptr");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002157 return assign(__s, traits_type::length(__s));
2158}
2159
2160// append
2161
2162template <class _CharT, class _Traits, class _Allocator>
2163basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002164basic_string<_CharT, _Traits, _Allocator>::append(const value_type* __s, size_type __n)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002165{
Alp Tokerec34c482014-05-15 11:27:39 +00002166 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::append received nullptr");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002167 size_type __cap = capacity();
2168 size_type __sz = size();
2169 if (__cap - __sz >= __n)
2170 {
2171 if (__n)
2172 {
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002173 value_type* __p = _VSTD::__to_raw_pointer(__get_pointer());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002174 traits_type::copy(__p + __sz, __s, __n);
2175 __sz += __n;
2176 __set_size(__sz);
2177 traits_type::assign(__p[__sz], value_type());
2178 }
2179 }
2180 else
2181 __grow_by_and_replace(__cap, __sz + __n - __cap, __sz, __sz, 0, __n, __s);
2182 return *this;
2183}
2184
2185template <class _CharT, class _Traits, class _Allocator>
2186basic_string<_CharT, _Traits, _Allocator>&
2187basic_string<_CharT, _Traits, _Allocator>::append(size_type __n, value_type __c)
2188{
2189 if (__n)
2190 {
2191 size_type __cap = capacity();
2192 size_type __sz = size();
2193 if (__cap - __sz < __n)
2194 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2195 pointer __p = __get_pointer();
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002196 traits_type::assign(_VSTD::__to_raw_pointer(__p) + __sz, __n, __c);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002197 __sz += __n;
2198 __set_size(__sz);
2199 traits_type::assign(__p[__sz], value_type());
2200 }
2201 return *this;
2202}
2203
2204template <class _CharT, class _Traits, class _Allocator>
2205void
2206basic_string<_CharT, _Traits, _Allocator>::push_back(value_type __c)
2207{
Howard Hinnant15467182013-04-30 21:44:48 +00002208 bool __is_short = !__is_long();
2209 size_type __cap;
2210 size_type __sz;
2211 if (__is_short)
2212 {
2213 __cap = __min_cap - 1;
2214 __sz = __get_short_size();
2215 }
2216 else
2217 {
2218 __cap = __get_long_cap() - 1;
2219 __sz = __get_long_size();
2220 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002221 if (__sz == __cap)
Howard Hinnant15467182013-04-30 21:44:48 +00002222 {
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002223 __grow_by(__cap, 1, __sz, __sz, 0);
Howard Hinnant15467182013-04-30 21:44:48 +00002224 __is_short = !__is_long();
2225 }
2226 pointer __p;
2227 if (__is_short)
2228 {
2229 __p = __get_short_pointer() + __sz;
2230 __set_short_size(__sz+1);
2231 }
2232 else
2233 {
2234 __p = __get_long_pointer() + __sz;
2235 __set_long_size(__sz+1);
2236 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002237 traits_type::assign(*__p, __c);
2238 traits_type::assign(*++__p, value_type());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002239}
2240
Marshall Clowac655ef2016-09-07 03:32:06 +00002241template <class _Tp>
Marshall Clowd979eed2016-09-05 01:54:30 +00002242bool __ptr_in_range (const _Tp* __p, const _Tp* __first, const _Tp* __last)
2243{
2244 return __first <= __p && __p < __last;
2245}
2246
Marshall Clowac655ef2016-09-07 03:32:06 +00002247template <class _Tp1, class _Tp2>
Eric Fiselier0e5ebbc2016-12-23 23:37:52 +00002248bool __ptr_in_range (const _Tp1*, const _Tp2*, const _Tp2*)
Marshall Clowac655ef2016-09-07 03:32:06 +00002249{
2250 return false;
2251}
2252
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002253template <class _CharT, class _Traits, class _Allocator>
2254template<class _ForwardIterator>
Eric Fiselier026d38e2016-10-31 02:46:25 +00002255basic_string<_CharT, _Traits, _Allocator>&
2256basic_string<_CharT, _Traits, _Allocator>::__append_forward_unsafe(
2257 _ForwardIterator __first, _ForwardIterator __last)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002258{
Eric Fiselier026d38e2016-10-31 02:46:25 +00002259 static_assert(__is_forward_iterator<_ForwardIterator>::value,
2260 "function requires a ForwardIterator");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002261 size_type __sz = size();
2262 size_type __cap = capacity();
Howard Hinnant0949eed2011-06-30 21:18:19 +00002263 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002264 if (__n)
2265 {
Marshall Clowd979eed2016-09-05 01:54:30 +00002266 if ( __ptr_in_range(&*__first, data(), data() + size()))
2267 {
2268 const basic_string __temp (__first, __last, __alloc());
2269 append(__temp.data(), __temp.size());
2270 }
2271 else
2272 {
2273 if (__cap - __sz < __n)
2274 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2275 pointer __p = __get_pointer() + __sz;
2276 for (; __first != __last; ++__p, ++__first)
2277 traits_type::assign(*__p, *__first);
2278 traits_type::assign(*__p, value_type());
2279 __set_size(__sz + __n);
2280 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002281 }
2282 return *this;
2283}
2284
2285template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002286inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002287basic_string<_CharT, _Traits, _Allocator>&
2288basic_string<_CharT, _Traits, _Allocator>::append(const basic_string& __str)
2289{
2290 return append(__str.data(), __str.size());
2291}
2292
2293template <class _CharT, class _Traits, class _Allocator>
2294basic_string<_CharT, _Traits, _Allocator>&
2295basic_string<_CharT, _Traits, _Allocator>::append(const basic_string& __str, size_type __pos, size_type __n)
2296{
2297 size_type __sz = __str.size();
2298 if (__pos > __sz)
2299 this->__throw_out_of_range();
Howard Hinnant0949eed2011-06-30 21:18:19 +00002300 return append(__str.data() + __pos, _VSTD::min(__n, __sz - __pos));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002301}
2302
2303template <class _CharT, class _Traits, class _Allocator>
Marshall Clow42a87db2016-10-03 23:40:48 +00002304template <class _Tp>
Marshall Clow6ac8de02016-09-24 22:45:42 +00002305 typename enable_if
2306 <
2307 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
2308 basic_string<_CharT, _Traits, _Allocator>&
2309 >::type
2310basic_string<_CharT, _Traits, _Allocator>::append(const _Tp & __t, size_type __pos, size_type __n)
Marshall Clow1e00d6d2016-07-21 05:31:24 +00002311{
Marshall Clow6ac8de02016-09-24 22:45:42 +00002312 __self_view __sv = __t;
Marshall Clow1e00d6d2016-07-21 05:31:24 +00002313 size_type __sz = __sv.size();
2314 if (__pos > __sz)
2315 this->__throw_out_of_range();
2316 return append(__sv.data() + __pos, _VSTD::min(__n, __sz - __pos));
2317}
2318
2319template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002320basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002321basic_string<_CharT, _Traits, _Allocator>::append(const value_type* __s)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002322{
Alp Tokerec34c482014-05-15 11:27:39 +00002323 _LIBCPP_ASSERT(__s != nullptr, "string::append received nullptr");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002324 return append(__s, traits_type::length(__s));
2325}
2326
2327// insert
2328
2329template <class _CharT, class _Traits, class _Allocator>
2330basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002331basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, const value_type* __s, size_type __n)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002332{
Alp Tokerec34c482014-05-15 11:27:39 +00002333 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::insert received nullptr");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002334 size_type __sz = size();
2335 if (__pos > __sz)
2336 this->__throw_out_of_range();
2337 size_type __cap = capacity();
2338 if (__cap - __sz >= __n)
2339 {
2340 if (__n)
2341 {
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002342 value_type* __p = _VSTD::__to_raw_pointer(__get_pointer());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002343 size_type __n_move = __sz - __pos;
2344 if (__n_move != 0)
2345 {
2346 if (__p + __pos <= __s && __s < __p + __sz)
2347 __s += __n;
2348 traits_type::move(__p + __pos + __n, __p + __pos, __n_move);
2349 }
2350 traits_type::move(__p + __pos, __s, __n);
2351 __sz += __n;
2352 __set_size(__sz);
2353 traits_type::assign(__p[__sz], value_type());
2354 }
2355 }
2356 else
2357 __grow_by_and_replace(__cap, __sz + __n - __cap, __sz, __pos, 0, __n, __s);
2358 return *this;
2359}
2360
2361template <class _CharT, class _Traits, class _Allocator>
2362basic_string<_CharT, _Traits, _Allocator>&
2363basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, size_type __n, value_type __c)
2364{
2365 size_type __sz = size();
2366 if (__pos > __sz)
2367 this->__throw_out_of_range();
2368 if (__n)
2369 {
2370 size_type __cap = capacity();
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002371 value_type* __p;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002372 if (__cap - __sz >= __n)
2373 {
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002374 __p = _VSTD::__to_raw_pointer(__get_pointer());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002375 size_type __n_move = __sz - __pos;
2376 if (__n_move != 0)
2377 traits_type::move(__p + __pos + __n, __p + __pos, __n_move);
2378 }
2379 else
2380 {
2381 __grow_by(__cap, __sz + __n - __cap, __sz, __pos, 0, __n);
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002382 __p = _VSTD::__to_raw_pointer(__get_long_pointer());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002383 }
2384 traits_type::assign(__p + __pos, __n, __c);
2385 __sz += __n;
2386 __set_size(__sz);
2387 traits_type::assign(__p[__sz], value_type());
2388 }
2389 return *this;
2390}
2391
2392template <class _CharT, class _Traits, class _Allocator>
2393template<class _InputIterator>
2394typename enable_if
2395<
Marshall Clowdf9db312016-01-13 21:54:34 +00002396 __is_exactly_input_iterator<_InputIterator>::value
2397 || !__libcpp_string_gets_noexcept_iterator<_InputIterator>::value,
2398 typename basic_string<_CharT, _Traits, _Allocator>::iterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002399>::type
2400basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, _InputIterator __first, _InputIterator __last)
2401{
Howard Hinnant499cea12013-08-23 17:37:05 +00002402#if _LIBCPP_DEBUG_LEVEL >= 2
2403 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2404 "string::insert(iterator, range) called with an iterator not"
2405 " referring to this string");
2406#endif
Marshall Clowd979eed2016-09-05 01:54:30 +00002407 const basic_string __temp(__first, __last, __alloc());
Marshall Clowdf9db312016-01-13 21:54:34 +00002408 return insert(__pos, __temp.data(), __temp.data() + __temp.size());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002409}
2410
2411template <class _CharT, class _Traits, class _Allocator>
2412template<class _ForwardIterator>
2413typename enable_if
2414<
Marshall Clowdf9db312016-01-13 21:54:34 +00002415 __is_forward_iterator<_ForwardIterator>::value
2416 && __libcpp_string_gets_noexcept_iterator<_ForwardIterator>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002417 typename basic_string<_CharT, _Traits, _Allocator>::iterator
2418>::type
2419basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, _ForwardIterator __first, _ForwardIterator __last)
2420{
Howard Hinnant499cea12013-08-23 17:37:05 +00002421#if _LIBCPP_DEBUG_LEVEL >= 2
2422 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2423 "string::insert(iterator, range) called with an iterator not"
2424 " referring to this string");
2425#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002426 size_type __ip = static_cast<size_type>(__pos - begin());
Howard Hinnant0949eed2011-06-30 21:18:19 +00002427 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002428 if (__n)
2429 {
Marshall Clowd979eed2016-09-05 01:54:30 +00002430 if ( __ptr_in_range(&*__first, data(), data() + size()))
2431 {
2432 const basic_string __temp(__first, __last, __alloc());
2433 return insert(__pos, __temp.data(), __temp.data() + __temp.size());
2434 }
2435
2436 size_type __sz = size();
2437 size_type __cap = capacity();
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002438 value_type* __p;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002439 if (__cap - __sz >= __n)
2440 {
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002441 __p = _VSTD::__to_raw_pointer(__get_pointer());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002442 size_type __n_move = __sz - __ip;
2443 if (__n_move != 0)
2444 traits_type::move(__p + __ip + __n, __p + __ip, __n_move);
2445 }
2446 else
2447 {
2448 __grow_by(__cap, __sz + __n - __cap, __sz, __ip, 0, __n);
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002449 __p = _VSTD::__to_raw_pointer(__get_long_pointer());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002450 }
2451 __sz += __n;
2452 __set_size(__sz);
2453 traits_type::assign(__p[__sz], value_type());
2454 for (__p += __ip; __first != __last; ++__p, ++__first)
2455 traits_type::assign(*__p, *__first);
2456 }
2457 return begin() + __ip;
2458}
2459
2460template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002461inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002462basic_string<_CharT, _Traits, _Allocator>&
2463basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const basic_string& __str)
2464{
2465 return insert(__pos1, __str.data(), __str.size());
2466}
2467
2468template <class _CharT, class _Traits, class _Allocator>
2469basic_string<_CharT, _Traits, _Allocator>&
2470basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const basic_string& __str,
2471 size_type __pos2, size_type __n)
2472{
2473 size_type __str_sz = __str.size();
2474 if (__pos2 > __str_sz)
2475 this->__throw_out_of_range();
Howard Hinnant0949eed2011-06-30 21:18:19 +00002476 return insert(__pos1, __str.data() + __pos2, _VSTD::min(__n, __str_sz - __pos2));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002477}
2478
2479template <class _CharT, class _Traits, class _Allocator>
Marshall Clow6ac8de02016-09-24 22:45:42 +00002480template <class _Tp>
2481typename enable_if
2482<
2483 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
2484 basic_string<_CharT, _Traits, _Allocator>&
2485>::type
2486basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const _Tp& __t,
Marshall Clow1e00d6d2016-07-21 05:31:24 +00002487 size_type __pos2, size_type __n)
2488{
Marshall Clow6ac8de02016-09-24 22:45:42 +00002489 __self_view __sv = __t;
Marshall Clow1e00d6d2016-07-21 05:31:24 +00002490 size_type __str_sz = __sv.size();
2491 if (__pos2 > __str_sz)
2492 this->__throw_out_of_range();
2493 return insert(__pos1, __sv.data() + __pos2, _VSTD::min(__n, __str_sz - __pos2));
2494}
2495
2496template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002497basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002498basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, const value_type* __s)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002499{
Alp Tokerec34c482014-05-15 11:27:39 +00002500 _LIBCPP_ASSERT(__s != nullptr, "string::insert received nullptr");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002501 return insert(__pos, __s, traits_type::length(__s));
2502}
2503
2504template <class _CharT, class _Traits, class _Allocator>
2505typename basic_string<_CharT, _Traits, _Allocator>::iterator
2506basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, value_type __c)
2507{
2508 size_type __ip = static_cast<size_type>(__pos - begin());
2509 size_type __sz = size();
2510 size_type __cap = capacity();
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002511 value_type* __p;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002512 if (__cap == __sz)
2513 {
2514 __grow_by(__cap, 1, __sz, __ip, 0, 1);
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002515 __p = _VSTD::__to_raw_pointer(__get_long_pointer());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002516 }
2517 else
2518 {
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002519 __p = _VSTD::__to_raw_pointer(__get_pointer());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002520 size_type __n_move = __sz - __ip;
2521 if (__n_move != 0)
2522 traits_type::move(__p + __ip + 1, __p + __ip, __n_move);
2523 }
2524 traits_type::assign(__p[__ip], __c);
2525 traits_type::assign(__p[++__sz], value_type());
2526 __set_size(__sz);
2527 return begin() + static_cast<difference_type>(__ip);
2528}
2529
2530template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002531inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002532typename basic_string<_CharT, _Traits, _Allocator>::iterator
2533basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, size_type __n, value_type __c)
2534{
Howard Hinnant499cea12013-08-23 17:37:05 +00002535#if _LIBCPP_DEBUG_LEVEL >= 2
2536 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2537 "string::insert(iterator, n, value) called with an iterator not"
2538 " referring to this string");
2539#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002540 difference_type __p = __pos - begin();
2541 insert(static_cast<size_type>(__p), __n, __c);
2542 return begin() + __p;
2543}
2544
2545// replace
2546
2547template <class _CharT, class _Traits, class _Allocator>
2548basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002549basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos, size_type __n1, const value_type* __s, size_type __n2)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002550{
Alp Tokerec34c482014-05-15 11:27:39 +00002551 _LIBCPP_ASSERT(__n2 == 0 || __s != nullptr, "string::replace received nullptr");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002552 size_type __sz = size();
2553 if (__pos > __sz)
2554 this->__throw_out_of_range();
Howard Hinnant0949eed2011-06-30 21:18:19 +00002555 __n1 = _VSTD::min(__n1, __sz - __pos);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002556 size_type __cap = capacity();
2557 if (__cap - __sz + __n1 >= __n2)
2558 {
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002559 value_type* __p = _VSTD::__to_raw_pointer(__get_pointer());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002560 if (__n1 != __n2)
2561 {
2562 size_type __n_move = __sz - __pos - __n1;
2563 if (__n_move != 0)
2564 {
2565 if (__n1 > __n2)
2566 {
2567 traits_type::move(__p + __pos, __s, __n2);
2568 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
2569 goto __finish;
2570 }
2571 if (__p + __pos < __s && __s < __p + __sz)
2572 {
2573 if (__p + __pos + __n1 <= __s)
2574 __s += __n2 - __n1;
2575 else // __p + __pos < __s < __p + __pos + __n1
2576 {
2577 traits_type::move(__p + __pos, __s, __n1);
2578 __pos += __n1;
2579 __s += __n2;
2580 __n2 -= __n1;
2581 __n1 = 0;
2582 }
2583 }
2584 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
2585 }
2586 }
2587 traits_type::move(__p + __pos, __s, __n2);
2588__finish:
2589 __sz += __n2 - __n1;
2590 __set_size(__sz);
2591 __invalidate_iterators_past(__sz);
2592 traits_type::assign(__p[__sz], value_type());
2593 }
2594 else
2595 __grow_by_and_replace(__cap, __sz - __n1 + __n2 - __cap, __sz, __pos, __n1, __n2, __s);
2596 return *this;
2597}
2598
2599template <class _CharT, class _Traits, class _Allocator>
2600basic_string<_CharT, _Traits, _Allocator>&
2601basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos, size_type __n1, size_type __n2, value_type __c)
2602{
2603 size_type __sz = size();
2604 if (__pos > __sz)
2605 this->__throw_out_of_range();
Howard Hinnant0949eed2011-06-30 21:18:19 +00002606 __n1 = _VSTD::min(__n1, __sz - __pos);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002607 size_type __cap = capacity();
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002608 value_type* __p;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002609 if (__cap - __sz + __n1 >= __n2)
2610 {
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002611 __p = _VSTD::__to_raw_pointer(__get_pointer());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002612 if (__n1 != __n2)
2613 {
2614 size_type __n_move = __sz - __pos - __n1;
2615 if (__n_move != 0)
2616 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
2617 }
2618 }
2619 else
2620 {
2621 __grow_by(__cap, __sz - __n1 + __n2 - __cap, __sz, __pos, __n1, __n2);
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002622 __p = _VSTD::__to_raw_pointer(__get_long_pointer());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002623 }
2624 traits_type::assign(__p + __pos, __n2, __c);
2625 __sz += __n2 - __n1;
2626 __set_size(__sz);
2627 __invalidate_iterators_past(__sz);
2628 traits_type::assign(__p[__sz], value_type());
2629 return *this;
2630}
2631
2632template <class _CharT, class _Traits, class _Allocator>
2633template<class _InputIterator>
2634typename enable_if
2635<
2636 __is_input_iterator<_InputIterator>::value,
2637 basic_string<_CharT, _Traits, _Allocator>&
2638>::type
Howard Hinnant7b2cb482010-11-17 21:11:40 +00002639basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002640 _InputIterator __j1, _InputIterator __j2)
2641{
Marshall Clowd979eed2016-09-05 01:54:30 +00002642 const basic_string __temp(__j1, __j2, __alloc());
Marshall Clowdf9db312016-01-13 21:54:34 +00002643 return this->replace(__i1, __i2, __temp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002644}
2645
2646template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002647inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002648basic_string<_CharT, _Traits, _Allocator>&
2649basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const basic_string& __str)
2650{
2651 return replace(__pos1, __n1, __str.data(), __str.size());
2652}
2653
2654template <class _CharT, class _Traits, class _Allocator>
2655basic_string<_CharT, _Traits, _Allocator>&
2656basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const basic_string& __str,
2657 size_type __pos2, size_type __n2)
2658{
2659 size_type __str_sz = __str.size();
2660 if (__pos2 > __str_sz)
2661 this->__throw_out_of_range();
Howard Hinnant0949eed2011-06-30 21:18:19 +00002662 return replace(__pos1, __n1, __str.data() + __pos2, _VSTD::min(__n2, __str_sz - __pos2));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002663}
2664
2665template <class _CharT, class _Traits, class _Allocator>
Marshall Clow6ac8de02016-09-24 22:45:42 +00002666template <class _Tp>
2667typename enable_if
2668<
2669 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
2670 basic_string<_CharT, _Traits, _Allocator>&
2671>::type
2672basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const _Tp& __t,
Marshall Clow1e00d6d2016-07-21 05:31:24 +00002673 size_type __pos2, size_type __n2)
2674{
Marshall Clow6ac8de02016-09-24 22:45:42 +00002675 __self_view __sv = __t;
Marshall Clow1e00d6d2016-07-21 05:31:24 +00002676 size_type __str_sz = __sv.size();
2677 if (__pos2 > __str_sz)
2678 this->__throw_out_of_range();
2679 return replace(__pos1, __n1, __sv.data() + __pos2, _VSTD::min(__n2, __str_sz - __pos2));
2680}
2681
2682template <class _CharT, class _Traits, class _Allocator>
2683basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002684basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos, size_type __n1, const value_type* __s)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002685{
Alp Tokerec34c482014-05-15 11:27:39 +00002686 _LIBCPP_ASSERT(__s != nullptr, "string::replace received nullptr");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002687 return replace(__pos, __n1, __s, traits_type::length(__s));
2688}
2689
2690template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002691inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002692basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant7b2cb482010-11-17 21:11:40 +00002693basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, const basic_string& __str)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002694{
2695 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1),
2696 __str.data(), __str.size());
2697}
2698
2699template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002700inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002701basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002702basic_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 +00002703{
2704 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __s, __n);
2705}
2706
2707template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002708inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002709basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002710basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, const value_type* __s)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002711{
2712 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __s);
2713}
2714
2715template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002716inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002717basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant7b2cb482010-11-17 21:11:40 +00002718basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, size_type __n, value_type __c)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002719{
2720 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __n, __c);
2721}
2722
2723// erase
2724
2725template <class _CharT, class _Traits, class _Allocator>
2726basic_string<_CharT, _Traits, _Allocator>&
2727basic_string<_CharT, _Traits, _Allocator>::erase(size_type __pos, size_type __n)
2728{
2729 size_type __sz = size();
2730 if (__pos > __sz)
2731 this->__throw_out_of_range();
2732 if (__n)
2733 {
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002734 value_type* __p = _VSTD::__to_raw_pointer(__get_pointer());
Howard Hinnant0949eed2011-06-30 21:18:19 +00002735 __n = _VSTD::min(__n, __sz - __pos);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002736 size_type __n_move = __sz - __pos - __n;
2737 if (__n_move != 0)
2738 traits_type::move(__p + __pos, __p + __pos + __n, __n_move);
2739 __sz -= __n;
2740 __set_size(__sz);
2741 __invalidate_iterators_past(__sz);
2742 traits_type::assign(__p[__sz], value_type());
2743 }
2744 return *this;
2745}
2746
2747template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002748inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002749typename basic_string<_CharT, _Traits, _Allocator>::iterator
2750basic_string<_CharT, _Traits, _Allocator>::erase(const_iterator __pos)
2751{
Howard Hinnant499cea12013-08-23 17:37:05 +00002752#if _LIBCPP_DEBUG_LEVEL >= 2
2753 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2754 "string::erase(iterator) called with an iterator not"
2755 " referring to this string");
2756#endif
2757 _LIBCPP_ASSERT(__pos != end(),
2758 "string::erase(iterator) called with a non-dereferenceable iterator");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002759 iterator __b = begin();
2760 size_type __r = static_cast<size_type>(__pos - __b);
2761 erase(__r, 1);
Howard Hinnantec3773c2011-12-01 20:21:04 +00002762 return __b + static_cast<difference_type>(__r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002763}
2764
2765template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002766inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002767typename basic_string<_CharT, _Traits, _Allocator>::iterator
2768basic_string<_CharT, _Traits, _Allocator>::erase(const_iterator __first, const_iterator __last)
2769{
Howard Hinnant499cea12013-08-23 17:37:05 +00002770#if _LIBCPP_DEBUG_LEVEL >= 2
2771 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__first) == this,
2772 "string::erase(iterator, iterator) called with an iterator not"
2773 " referring to this string");
2774#endif
2775 _LIBCPP_ASSERT(__first <= __last, "string::erase(first, last) called with invalid range");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002776 iterator __b = begin();
2777 size_type __r = static_cast<size_type>(__first - __b);
2778 erase(__r, static_cast<size_type>(__last - __first));
Howard Hinnantec3773c2011-12-01 20:21:04 +00002779 return __b + static_cast<difference_type>(__r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002780}
2781
2782template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002783inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002784void
2785basic_string<_CharT, _Traits, _Allocator>::pop_back()
2786{
Howard Hinnant499cea12013-08-23 17:37:05 +00002787 _LIBCPP_ASSERT(!empty(), "string::pop_back(): string is already empty");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002788 size_type __sz;
2789 if (__is_long())
2790 {
2791 __sz = __get_long_size() - 1;
2792 __set_long_size(__sz);
2793 traits_type::assign(*(__get_long_pointer() + __sz), value_type());
2794 }
2795 else
2796 {
2797 __sz = __get_short_size() - 1;
2798 __set_short_size(__sz);
2799 traits_type::assign(*(__get_short_pointer() + __sz), value_type());
2800 }
2801 __invalidate_iterators_past(__sz);
2802}
2803
2804template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002805inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002806void
Howard Hinnanta6119a82011-05-29 19:57:12 +00002807basic_string<_CharT, _Traits, _Allocator>::clear() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002808{
2809 __invalidate_all_iterators();
2810 if (__is_long())
2811 {
2812 traits_type::assign(*__get_long_pointer(), value_type());
2813 __set_long_size(0);
2814 }
2815 else
2816 {
2817 traits_type::assign(*__get_short_pointer(), value_type());
2818 __set_short_size(0);
2819 }
2820}
2821
2822template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002823inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002824void
2825basic_string<_CharT, _Traits, _Allocator>::__erase_to_end(size_type __pos)
2826{
2827 if (__is_long())
2828 {
2829 traits_type::assign(*(__get_long_pointer() + __pos), value_type());
2830 __set_long_size(__pos);
2831 }
2832 else
2833 {
2834 traits_type::assign(*(__get_short_pointer() + __pos), value_type());
2835 __set_short_size(__pos);
2836 }
2837 __invalidate_iterators_past(__pos);
2838}
2839
2840template <class _CharT, class _Traits, class _Allocator>
2841void
2842basic_string<_CharT, _Traits, _Allocator>::resize(size_type __n, value_type __c)
2843{
2844 size_type __sz = size();
2845 if (__n > __sz)
2846 append(__n - __sz, __c);
2847 else
2848 __erase_to_end(__n);
2849}
2850
2851template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002852inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002853typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnanta6119a82011-05-29 19:57:12 +00002854basic_string<_CharT, _Traits, _Allocator>::max_size() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002855{
Howard Hinnante32b5e22010-11-17 17:55:08 +00002856 size_type __m = __alloc_traits::max_size(__alloc());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002857#if _LIBCPP_BIG_ENDIAN
Marshall Clow09f85502013-10-31 17:23:08 +00002858 return (__m <= ~__long_mask ? __m : __m/2) - __alignment;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002859#else
Marshall Clow09f85502013-10-31 17:23:08 +00002860 return __m - __alignment;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002861#endif
2862}
2863
2864template <class _CharT, class _Traits, class _Allocator>
2865void
2866basic_string<_CharT, _Traits, _Allocator>::reserve(size_type __res_arg)
2867{
2868 if (__res_arg > max_size())
2869 this->__throw_length_error();
2870 size_type __cap = capacity();
2871 size_type __sz = size();
Howard Hinnant0949eed2011-06-30 21:18:19 +00002872 __res_arg = _VSTD::max(__res_arg, __sz);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002873 __res_arg = __recommend(__res_arg);
2874 if (__res_arg != __cap)
2875 {
2876 pointer __new_data, __p;
2877 bool __was_long, __now_long;
2878 if (__res_arg == __min_cap - 1)
2879 {
2880 __was_long = true;
2881 __now_long = false;
2882 __new_data = __get_short_pointer();
2883 __p = __get_long_pointer();
2884 }
2885 else
2886 {
2887 if (__res_arg > __cap)
Howard Hinnante32b5e22010-11-17 17:55:08 +00002888 __new_data = __alloc_traits::allocate(__alloc(), __res_arg+1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002889 else
2890 {
2891 #ifndef _LIBCPP_NO_EXCEPTIONS
2892 try
2893 {
Howard Hinnant324bb032010-08-22 00:02:43 +00002894 #endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnante32b5e22010-11-17 17:55:08 +00002895 __new_data = __alloc_traits::allocate(__alloc(), __res_arg+1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002896 #ifndef _LIBCPP_NO_EXCEPTIONS
2897 }
2898 catch (...)
2899 {
2900 return;
2901 }
Howard Hinnant324bb032010-08-22 00:02:43 +00002902 #else // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002903 if (__new_data == nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002904 return;
Howard Hinnant324bb032010-08-22 00:02:43 +00002905 #endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002906 }
2907 __now_long = true;
2908 __was_long = __is_long();
2909 __p = __get_pointer();
2910 }
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002911 traits_type::copy(_VSTD::__to_raw_pointer(__new_data),
2912 _VSTD::__to_raw_pointer(__p), size()+1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002913 if (__was_long)
Howard Hinnante32b5e22010-11-17 17:55:08 +00002914 __alloc_traits::deallocate(__alloc(), __p, __cap+1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002915 if (__now_long)
2916 {
2917 __set_long_cap(__res_arg+1);
2918 __set_long_size(__sz);
2919 __set_long_pointer(__new_data);
2920 }
2921 else
2922 __set_short_size(__sz);
2923 __invalidate_all_iterators();
2924 }
2925}
2926
2927template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002928inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002929typename basic_string<_CharT, _Traits, _Allocator>::const_reference
Marshall Clow1e00d6d2016-07-21 05:31:24 +00002930basic_string<_CharT, _Traits, _Allocator>::operator[](size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002931{
Howard Hinnant499cea12013-08-23 17:37:05 +00002932 _LIBCPP_ASSERT(__pos <= size(), "string index out of bounds");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002933 return *(data() + __pos);
2934}
2935
2936template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002937inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002938typename basic_string<_CharT, _Traits, _Allocator>::reference
Marshall Clow1e00d6d2016-07-21 05:31:24 +00002939basic_string<_CharT, _Traits, _Allocator>::operator[](size_type __pos) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002940{
Howard Hinnant499cea12013-08-23 17:37:05 +00002941 _LIBCPP_ASSERT(__pos <= size(), "string index out of bounds");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002942 return *(__get_pointer() + __pos);
2943}
2944
2945template <class _CharT, class _Traits, class _Allocator>
2946typename basic_string<_CharT, _Traits, _Allocator>::const_reference
2947basic_string<_CharT, _Traits, _Allocator>::at(size_type __n) const
2948{
2949 if (__n >= size())
2950 this->__throw_out_of_range();
2951 return (*this)[__n];
2952}
2953
2954template <class _CharT, class _Traits, class _Allocator>
2955typename basic_string<_CharT, _Traits, _Allocator>::reference
2956basic_string<_CharT, _Traits, _Allocator>::at(size_type __n)
2957{
2958 if (__n >= size())
2959 this->__throw_out_of_range();
2960 return (*this)[__n];
2961}
2962
2963template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002964inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002965typename basic_string<_CharT, _Traits, _Allocator>::reference
2966basic_string<_CharT, _Traits, _Allocator>::front()
2967{
Howard Hinnant499cea12013-08-23 17:37:05 +00002968 _LIBCPP_ASSERT(!empty(), "string::front(): string is empty");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002969 return *__get_pointer();
2970}
2971
2972template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002973inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002974typename basic_string<_CharT, _Traits, _Allocator>::const_reference
2975basic_string<_CharT, _Traits, _Allocator>::front() const
2976{
Howard Hinnant499cea12013-08-23 17:37:05 +00002977 _LIBCPP_ASSERT(!empty(), "string::front(): string is empty");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002978 return *data();
2979}
2980
2981template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002982inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002983typename basic_string<_CharT, _Traits, _Allocator>::reference
2984basic_string<_CharT, _Traits, _Allocator>::back()
2985{
Howard Hinnant499cea12013-08-23 17:37:05 +00002986 _LIBCPP_ASSERT(!empty(), "string::back(): string is empty");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002987 return *(__get_pointer() + size() - 1);
2988}
2989
2990template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002991inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002992typename basic_string<_CharT, _Traits, _Allocator>::const_reference
2993basic_string<_CharT, _Traits, _Allocator>::back() const
2994{
Howard Hinnant499cea12013-08-23 17:37:05 +00002995 _LIBCPP_ASSERT(!empty(), "string::back(): string is empty");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002996 return *(data() + size() - 1);
2997}
2998
2999template <class _CharT, class _Traits, class _Allocator>
3000typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00003001basic_string<_CharT, _Traits, _Allocator>::copy(value_type* __s, size_type __n, size_type __pos) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003002{
3003 size_type __sz = size();
3004 if (__pos > __sz)
3005 this->__throw_out_of_range();
Howard Hinnant0949eed2011-06-30 21:18:19 +00003006 size_type __rlen = _VSTD::min(__n, __sz - __pos);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003007 traits_type::copy(__s, data() + __pos, __rlen);
3008 return __rlen;
3009}
3010
3011template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003012inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003013basic_string<_CharT, _Traits, _Allocator>
3014basic_string<_CharT, _Traits, _Allocator>::substr(size_type __pos, size_type __n) const
3015{
3016 return basic_string(*this, __pos, __n, __alloc());
3017}
3018
3019template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003020inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003021void
3022basic_string<_CharT, _Traits, _Allocator>::swap(basic_string& __str)
Marshall Clow7d914d12015-07-13 20:04:56 +00003023#if _LIBCPP_STD_VER >= 14
Eric Fiselier47257c42016-12-28 05:53:01 +00003024 _NOEXCEPT_DEBUG
Marshall Clow7d914d12015-07-13 20:04:56 +00003025#else
Eric Fiselier47257c42016-12-28 05:53:01 +00003026 _NOEXCEPT_DEBUG_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow7d914d12015-07-13 20:04:56 +00003027 __is_nothrow_swappable<allocator_type>::value)
3028#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003029{
Howard Hinnant499cea12013-08-23 17:37:05 +00003030#if _LIBCPP_DEBUG_LEVEL >= 2
3031 if (!__is_long())
3032 __get_db()->__invalidate_all(this);
3033 if (!__str.__is_long())
3034 __get_db()->__invalidate_all(&__str);
3035 __get_db()->swap(this, &__str);
3036#endif
Eric Fiselier47257c42016-12-28 05:53:01 +00003037 _LIBCPP_ASSERT(
3038 __alloc_traits::propagate_on_container_swap::value ||
3039 __alloc_traits::is_always_equal::value ||
3040 __alloc() == __str.__alloc(), "swapping non-equal allocators");
Howard Hinnant0949eed2011-06-30 21:18:19 +00003041 _VSTD::swap(__r_.first(), __str.__r_.first());
Marshall Clow7d914d12015-07-13 20:04:56 +00003042 __swap_allocator(__alloc(), __str.__alloc());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003043}
3044
3045// find
3046
3047template <class _Traits>
3048struct _LIBCPP_HIDDEN __traits_eq
3049{
3050 typedef typename _Traits::char_type char_type;
Howard Hinnanta6119a82011-05-29 19:57:12 +00003051 _LIBCPP_INLINE_VISIBILITY
3052 bool operator()(const char_type& __x, const char_type& __y) _NOEXCEPT
3053 {return _Traits::eq(__x, __y);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003054};
3055
3056template<class _CharT, class _Traits, class _Allocator>
3057typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00003058basic_string<_CharT, _Traits, _Allocator>::find(const value_type* __s,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003059 size_type __pos,
3060 size_type __n) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003061{
Alp Tokerec34c482014-05-15 11:27:39 +00003062 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find(): received nullptr");
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003063 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clow360f3192014-06-02 02:22:49 +00003064 (data(), size(), __s, __pos, __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003065}
3066
3067template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003068inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003069typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnanta6119a82011-05-29 19:57:12 +00003070basic_string<_CharT, _Traits, _Allocator>::find(const basic_string& __str,
3071 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003072{
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003073 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clow360f3192014-06-02 02:22:49 +00003074 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003075}
3076
3077template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003078inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003079typename basic_string<_CharT, _Traits, _Allocator>::size_type
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003080basic_string<_CharT, _Traits, _Allocator>::find(__self_view __sv,
3081 size_type __pos) const _NOEXCEPT
3082{
3083 return __str_find<value_type, size_type, traits_type, npos>
3084 (data(), size(), __sv.data(), __pos, __sv.size());
3085}
3086
3087template<class _CharT, class _Traits, class _Allocator>
3088inline _LIBCPP_INLINE_VISIBILITY
3089typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00003090basic_string<_CharT, _Traits, _Allocator>::find(const value_type* __s,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003091 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003092{
Alp Tokerec34c482014-05-15 11:27:39 +00003093 _LIBCPP_ASSERT(__s != nullptr, "string::find(): received nullptr");
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003094 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clow360f3192014-06-02 02:22:49 +00003095 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003096}
3097
3098template<class _CharT, class _Traits, class _Allocator>
3099typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnanta6119a82011-05-29 19:57:12 +00003100basic_string<_CharT, _Traits, _Allocator>::find(value_type __c,
3101 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003102{
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003103 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clow360f3192014-06-02 02:22:49 +00003104 (data(), size(), __c, __pos);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003105}
3106
3107// rfind
3108
3109template<class _CharT, class _Traits, class _Allocator>
3110typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00003111basic_string<_CharT, _Traits, _Allocator>::rfind(const value_type* __s,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003112 size_type __pos,
3113 size_type __n) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003114{
Alp Tokerec34c482014-05-15 11:27:39 +00003115 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::rfind(): received nullptr");
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003116 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clow360f3192014-06-02 02:22:49 +00003117 (data(), size(), __s, __pos, __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003118}
3119
3120template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003121inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003122typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnanta6119a82011-05-29 19:57:12 +00003123basic_string<_CharT, _Traits, _Allocator>::rfind(const basic_string& __str,
3124 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003125{
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003126 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clow360f3192014-06-02 02:22:49 +00003127 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003128}
3129
3130template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003131inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003132typename basic_string<_CharT, _Traits, _Allocator>::size_type
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003133basic_string<_CharT, _Traits, _Allocator>::rfind(__self_view __sv,
3134 size_type __pos) const _NOEXCEPT
3135{
3136 return __str_rfind<value_type, size_type, traits_type, npos>
3137 (data(), size(), __sv.data(), __pos, __sv.size());
3138}
3139
3140template<class _CharT, class _Traits, class _Allocator>
3141inline _LIBCPP_INLINE_VISIBILITY
3142typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00003143basic_string<_CharT, _Traits, _Allocator>::rfind(const value_type* __s,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003144 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003145{
Alp Tokerec34c482014-05-15 11:27:39 +00003146 _LIBCPP_ASSERT(__s != nullptr, "string::rfind(): received nullptr");
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003147 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clow360f3192014-06-02 02:22:49 +00003148 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003149}
3150
3151template<class _CharT, class _Traits, class _Allocator>
3152typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnanta6119a82011-05-29 19:57:12 +00003153basic_string<_CharT, _Traits, _Allocator>::rfind(value_type __c,
3154 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003155{
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003156 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clow360f3192014-06-02 02:22:49 +00003157 (data(), size(), __c, __pos);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003158}
3159
3160// find_first_of
3161
3162template<class _CharT, class _Traits, class _Allocator>
3163typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00003164basic_string<_CharT, _Traits, _Allocator>::find_first_of(const value_type* __s,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003165 size_type __pos,
3166 size_type __n) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003167{
Alp Tokerec34c482014-05-15 11:27:39 +00003168 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_first_of(): received nullptr");
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003169 return __str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow8eb5acc2014-02-16 01:57:26 +00003170 (data(), size(), __s, __pos, __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003171}
3172
3173template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003174inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003175typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnanta6119a82011-05-29 19:57:12 +00003176basic_string<_CharT, _Traits, _Allocator>::find_first_of(const basic_string& __str,
3177 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003178{
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003179 return __str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow8eb5acc2014-02-16 01:57:26 +00003180 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003181}
3182
3183template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003184inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003185typename basic_string<_CharT, _Traits, _Allocator>::size_type
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003186basic_string<_CharT, _Traits, _Allocator>::find_first_of(__self_view __sv,
3187 size_type __pos) const _NOEXCEPT
3188{
3189 return __str_find_first_of<value_type, size_type, traits_type, npos>
3190 (data(), size(), __sv.data(), __pos, __sv.size());
3191}
3192
3193template<class _CharT, class _Traits, class _Allocator>
3194inline _LIBCPP_INLINE_VISIBILITY
3195typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00003196basic_string<_CharT, _Traits, _Allocator>::find_first_of(const value_type* __s,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003197 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003198{
Alp Tokerec34c482014-05-15 11:27:39 +00003199 _LIBCPP_ASSERT(__s != nullptr, "string::find_first_of(): received nullptr");
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003200 return __str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow8eb5acc2014-02-16 01:57:26 +00003201 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003202}
3203
3204template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003205inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003206typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnanta6119a82011-05-29 19:57:12 +00003207basic_string<_CharT, _Traits, _Allocator>::find_first_of(value_type __c,
3208 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003209{
3210 return find(__c, __pos);
3211}
3212
3213// find_last_of
3214
3215template<class _CharT, class _Traits, class _Allocator>
3216typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00003217basic_string<_CharT, _Traits, _Allocator>::find_last_of(const value_type* __s,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003218 size_type __pos,
3219 size_type __n) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003220{
Alp Tokerec34c482014-05-15 11:27:39 +00003221 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_last_of(): received nullptr");
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003222 return __str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow8eb5acc2014-02-16 01:57:26 +00003223 (data(), size(), __s, __pos, __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003224}
3225
3226template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003227inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003228typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnanta6119a82011-05-29 19:57:12 +00003229basic_string<_CharT, _Traits, _Allocator>::find_last_of(const basic_string& __str,
3230 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003231{
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003232 return __str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow8eb5acc2014-02-16 01:57:26 +00003233 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003234}
3235
3236template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003237inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003238typename basic_string<_CharT, _Traits, _Allocator>::size_type
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003239basic_string<_CharT, _Traits, _Allocator>::find_last_of(__self_view __sv,
3240 size_type __pos) const _NOEXCEPT
3241{
3242 return __str_find_last_of<value_type, size_type, traits_type, npos>
3243 (data(), size(), __sv.data(), __pos, __sv.size());
3244}
3245
3246template<class _CharT, class _Traits, class _Allocator>
3247inline _LIBCPP_INLINE_VISIBILITY
3248typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00003249basic_string<_CharT, _Traits, _Allocator>::find_last_of(const value_type* __s,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003250 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003251{
Alp Tokerec34c482014-05-15 11:27:39 +00003252 _LIBCPP_ASSERT(__s != nullptr, "string::find_last_of(): received nullptr");
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003253 return __str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow8eb5acc2014-02-16 01:57:26 +00003254 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003255}
3256
3257template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003258inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003259typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnanta6119a82011-05-29 19:57:12 +00003260basic_string<_CharT, _Traits, _Allocator>::find_last_of(value_type __c,
3261 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003262{
3263 return rfind(__c, __pos);
3264}
3265
3266// find_first_not_of
3267
3268template<class _CharT, class _Traits, class _Allocator>
3269typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00003270basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const value_type* __s,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003271 size_type __pos,
3272 size_type __n) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003273{
Alp Tokerec34c482014-05-15 11:27:39 +00003274 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_first_not_of(): received nullptr");
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003275 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow8eb5acc2014-02-16 01:57:26 +00003276 (data(), size(), __s, __pos, __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003277}
3278
3279template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003280inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003281typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnanta6119a82011-05-29 19:57:12 +00003282basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const basic_string& __str,
3283 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003284{
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003285 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow8eb5acc2014-02-16 01:57:26 +00003286 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003287}
3288
3289template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003290inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003291typename basic_string<_CharT, _Traits, _Allocator>::size_type
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003292basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(__self_view __sv,
3293 size_type __pos) const _NOEXCEPT
3294{
3295 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
3296 (data(), size(), __sv.data(), __pos, __sv.size());
3297}
3298
3299template<class _CharT, class _Traits, class _Allocator>
3300inline _LIBCPP_INLINE_VISIBILITY
3301typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00003302basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const value_type* __s,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003303 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003304{
Alp Tokerec34c482014-05-15 11:27:39 +00003305 _LIBCPP_ASSERT(__s != nullptr, "string::find_first_not_of(): received nullptr");
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003306 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow8eb5acc2014-02-16 01:57:26 +00003307 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003308}
3309
3310template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003311inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003312typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnanta6119a82011-05-29 19:57:12 +00003313basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(value_type __c,
3314 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003315{
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003316 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow8eb5acc2014-02-16 01:57:26 +00003317 (data(), size(), __c, __pos);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003318}
3319
3320// find_last_not_of
3321
3322template<class _CharT, class _Traits, class _Allocator>
3323typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00003324basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const value_type* __s,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003325 size_type __pos,
3326 size_type __n) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003327{
Alp Tokerec34c482014-05-15 11:27:39 +00003328 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_last_not_of(): received nullptr");
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003329 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow8eb5acc2014-02-16 01:57:26 +00003330 (data(), size(), __s, __pos, __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003331}
3332
3333template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003334inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003335typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnanta6119a82011-05-29 19:57:12 +00003336basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const basic_string& __str,
3337 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003338{
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003339 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow8eb5acc2014-02-16 01:57:26 +00003340 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003341}
3342
3343template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003344inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003345typename basic_string<_CharT, _Traits, _Allocator>::size_type
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003346basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(__self_view __sv,
3347 size_type __pos) const _NOEXCEPT
3348{
3349 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
3350 (data(), size(), __sv.data(), __pos, __sv.size());
3351}
3352
3353template<class _CharT, class _Traits, class _Allocator>
3354inline _LIBCPP_INLINE_VISIBILITY
3355typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00003356basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const value_type* __s,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003357 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003358{
Alp Tokerec34c482014-05-15 11:27:39 +00003359 _LIBCPP_ASSERT(__s != nullptr, "string::find_last_not_of(): received nullptr");
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003360 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow8eb5acc2014-02-16 01:57:26 +00003361 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003362}
3363
3364template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003365inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003366typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnanta6119a82011-05-29 19:57:12 +00003367basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(value_type __c,
3368 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003369{
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003370 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow8eb5acc2014-02-16 01:57:26 +00003371 (data(), size(), __c, __pos);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003372}
3373
3374// compare
3375
3376template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003377inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003378int
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003379basic_string<_CharT, _Traits, _Allocator>::compare(__self_view __sv) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003380{
Howard Hinnantfa06d752011-07-24 21:45:06 +00003381 size_t __lhs_sz = size();
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003382 size_t __rhs_sz = __sv.size();
3383 int __result = traits_type::compare(data(), __sv.data(),
Howard Hinnantfa06d752011-07-24 21:45:06 +00003384 _VSTD::min(__lhs_sz, __rhs_sz));
3385 if (__result != 0)
3386 return __result;
3387 if (__lhs_sz < __rhs_sz)
3388 return -1;
3389 if (__lhs_sz > __rhs_sz)
3390 return 1;
3391 return 0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003392}
3393
3394template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003395inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003396int
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003397basic_string<_CharT, _Traits, _Allocator>::compare(const basic_string& __str) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003398{
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003399 return compare(__self_view(__str));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003400}
3401
3402template <class _CharT, class _Traits, class _Allocator>
3403int
Howard Hinnanta6119a82011-05-29 19:57:12 +00003404basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3405 size_type __n1,
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00003406 const value_type* __s,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003407 size_type __n2) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003408{
Alp Tokerec34c482014-05-15 11:27:39 +00003409 _LIBCPP_ASSERT(__n2 == 0 || __s != nullptr, "string::compare(): received nullptr");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003410 size_type __sz = size();
3411 if (__pos1 > __sz || __n2 == npos)
3412 this->__throw_out_of_range();
Howard Hinnant0949eed2011-06-30 21:18:19 +00003413 size_type __rlen = _VSTD::min(__n1, __sz - __pos1);
3414 int __r = traits_type::compare(data() + __pos1, __s, _VSTD::min(__rlen, __n2));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003415 if (__r == 0)
3416 {
3417 if (__rlen < __n2)
3418 __r = -1;
3419 else if (__rlen > __n2)
3420 __r = 1;
3421 }
3422 return __r;
3423}
3424
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003425template <class _CharT, class _Traits, class _Allocator>
3426inline _LIBCPP_INLINE_VISIBILITY
3427int
3428basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3429 size_type __n1,
3430 __self_view __sv) const
3431{
3432 return compare(__pos1, __n1, __sv.data(), __sv.size());
3433}
3434
3435template <class _CharT, class _Traits, class _Allocator>
3436inline _LIBCPP_INLINE_VISIBILITY
3437int
3438basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3439 size_type __n1,
3440 const basic_string& __str) const
3441{
3442 return compare(__pos1, __n1, __str.data(), __str.size());
3443}
3444
3445template <class _CharT, class _Traits, class _Allocator>
Marshall Clow6ac8de02016-09-24 22:45:42 +00003446template <class _Tp>
3447typename enable_if
3448<
3449 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3450 int
3451>::type
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003452basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3453 size_type __n1,
Marshall Clow6ac8de02016-09-24 22:45:42 +00003454 const _Tp& __t,
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003455 size_type __pos2,
3456 size_type __n2) const
3457{
Marshall Clow6ac8de02016-09-24 22:45:42 +00003458 __self_view __sv = __t;
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003459 return __self_view(*this).substr(__pos1, __n1).compare(__sv.substr(__pos2, __n2));
3460}
3461
3462template <class _CharT, class _Traits, class _Allocator>
3463int
3464basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3465 size_type __n1,
3466 const basic_string& __str,
3467 size_type __pos2,
3468 size_type __n2) const
3469{
3470 return compare(__pos1, __n1, __self_view(__str), __pos2, __n2);
3471}
3472
3473template <class _CharT, class _Traits, class _Allocator>
3474int
3475basic_string<_CharT, _Traits, _Allocator>::compare(const value_type* __s) const _NOEXCEPT
3476{
3477 _LIBCPP_ASSERT(__s != nullptr, "string::compare(): received nullptr");
3478 return compare(0, npos, __s, traits_type::length(__s));
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 value_type* __s) const
3486{
3487 _LIBCPP_ASSERT(__s != nullptr, "string::compare(): received nullptr");
3488 return compare(__pos1, __n1, __s, traits_type::length(__s));
3489}
3490
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003491// __invariants
3492
3493template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003494inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003495bool
3496basic_string<_CharT, _Traits, _Allocator>::__invariants() const
3497{
3498 if (size() > capacity())
3499 return false;
3500 if (capacity() < __min_cap - 1)
3501 return false;
3502 if (data() == 0)
3503 return false;
3504 if (data()[size()] != value_type(0))
3505 return false;
3506 return true;
3507}
3508
3509// operator==
3510
3511template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003512inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003513bool
3514operator==(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003515 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003516{
Howard Hinnant08dd2532013-04-22 23:55:13 +00003517 size_t __lhs_sz = __lhs.size();
3518 return __lhs_sz == __rhs.size() && _Traits::compare(__lhs.data(),
3519 __rhs.data(),
3520 __lhs_sz) == 0;
3521}
3522
3523template<class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003524inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant08dd2532013-04-22 23:55:13 +00003525bool
3526operator==(const basic_string<char, char_traits<char>, _Allocator>& __lhs,
3527 const basic_string<char, char_traits<char>, _Allocator>& __rhs) _NOEXCEPT
3528{
3529 size_t __lhs_sz = __lhs.size();
3530 if (__lhs_sz != __rhs.size())
3531 return false;
3532 const char* __lp = __lhs.data();
3533 const char* __rp = __rhs.data();
3534 if (__lhs.__is_long())
3535 return char_traits<char>::compare(__lp, __rp, __lhs_sz) == 0;
3536 for (; __lhs_sz != 0; --__lhs_sz, ++__lp, ++__rp)
3537 if (*__lp != *__rp)
3538 return false;
3539 return true;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003540}
3541
3542template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003543inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003544bool
Howard Hinnanta6119a82011-05-29 19:57:12 +00003545operator==(const _CharT* __lhs,
3546 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003547{
Eric Fiselier4f241822015-08-28 03:02:37 +00003548 typedef basic_string<_CharT, _Traits, _Allocator> _String;
3549 _LIBCPP_ASSERT(__lhs != nullptr, "operator==(char*, basic_string): received nullptr");
3550 size_t __lhs_len = _Traits::length(__lhs);
3551 if (__lhs_len != __rhs.size()) return false;
3552 return __rhs.compare(0, _String::npos, __lhs, __lhs_len) == 0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003553}
3554
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003555template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003556inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003557bool
Howard Hinnanta6119a82011-05-29 19:57:12 +00003558operator==(const basic_string<_CharT,_Traits,_Allocator>& __lhs,
3559 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003560{
Eric Fiselier4f241822015-08-28 03:02:37 +00003561 typedef basic_string<_CharT, _Traits, _Allocator> _String;
3562 _LIBCPP_ASSERT(__rhs != nullptr, "operator==(basic_string, char*): received nullptr");
3563 size_t __rhs_len = _Traits::length(__rhs);
3564 if (__rhs_len != __lhs.size()) return false;
3565 return __lhs.compare(0, _String::npos, __rhs, __rhs_len) == 0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003566}
3567
Howard Hinnant324bb032010-08-22 00:02:43 +00003568template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003569inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003570bool
3571operator!=(const basic_string<_CharT,_Traits,_Allocator>& __lhs,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003572 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003573{
3574 return !(__lhs == __rhs);
3575}
3576
3577template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003578inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003579bool
Howard Hinnanta6119a82011-05-29 19:57:12 +00003580operator!=(const _CharT* __lhs,
3581 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003582{
3583 return !(__lhs == __rhs);
3584}
3585
3586template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003587inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003588bool
Howard Hinnanta6119a82011-05-29 19:57:12 +00003589operator!=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
3590 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003591{
3592 return !(__lhs == __rhs);
3593}
3594
3595// operator<
3596
3597template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003598inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003599bool
3600operator< (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003601 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003602{
Howard Hinnant2644a7b2011-07-24 15:07:21 +00003603 return __lhs.compare(__rhs) < 0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003604}
3605
3606template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003607inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003608bool
Howard Hinnanta6119a82011-05-29 19:57:12 +00003609operator< (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
3610 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003611{
Howard Hinnant2644a7b2011-07-24 15:07:21 +00003612 return __lhs.compare(__rhs) < 0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003613}
3614
3615template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003616inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003617bool
Howard Hinnanta6119a82011-05-29 19:57:12 +00003618operator< (const _CharT* __lhs,
3619 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003620{
3621 return __rhs.compare(__lhs) > 0;
3622}
3623
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003624// operator>
3625
3626template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003627inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003628bool
3629operator> (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003630 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003631{
3632 return __rhs < __lhs;
3633}
3634
3635template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003636inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003637bool
Howard Hinnanta6119a82011-05-29 19:57:12 +00003638operator> (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
3639 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003640{
3641 return __rhs < __lhs;
3642}
3643
3644template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003645inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003646bool
Howard Hinnanta6119a82011-05-29 19:57:12 +00003647operator> (const _CharT* __lhs,
3648 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003649{
3650 return __rhs < __lhs;
3651}
3652
3653// operator<=
3654
3655template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003656inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003657bool
3658operator<=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003659 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003660{
3661 return !(__rhs < __lhs);
3662}
3663
3664template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003665inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003666bool
Howard Hinnanta6119a82011-05-29 19:57:12 +00003667operator<=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
3668 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003669{
3670 return !(__rhs < __lhs);
3671}
3672
3673template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003674inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003675bool
Howard Hinnanta6119a82011-05-29 19:57:12 +00003676operator<=(const _CharT* __lhs,
3677 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003678{
3679 return !(__rhs < __lhs);
3680}
3681
3682// operator>=
3683
3684template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003685inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003686bool
3687operator>=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003688 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003689{
3690 return !(__lhs < __rhs);
3691}
3692
3693template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003694inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003695bool
Howard Hinnanta6119a82011-05-29 19:57:12 +00003696operator>=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
3697 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003698{
3699 return !(__lhs < __rhs);
3700}
3701
3702template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003703inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003704bool
Howard Hinnanta6119a82011-05-29 19:57:12 +00003705operator>=(const _CharT* __lhs,
3706 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003707{
3708 return !(__lhs < __rhs);
3709}
3710
3711// operator +
3712
3713template<class _CharT, class _Traits, class _Allocator>
3714basic_string<_CharT, _Traits, _Allocator>
3715operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
3716 const basic_string<_CharT, _Traits, _Allocator>& __rhs)
3717{
3718 basic_string<_CharT, _Traits, _Allocator> __r(__lhs.get_allocator());
3719 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = __lhs.size();
3720 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = __rhs.size();
3721 __r.__init(__lhs.data(), __lhs_sz, __lhs_sz + __rhs_sz);
3722 __r.append(__rhs.data(), __rhs_sz);
3723 return __r;
3724}
3725
3726template<class _CharT, class _Traits, class _Allocator>
3727basic_string<_CharT, _Traits, _Allocator>
3728operator+(const _CharT* __lhs , const basic_string<_CharT,_Traits,_Allocator>& __rhs)
3729{
3730 basic_string<_CharT, _Traits, _Allocator> __r(__rhs.get_allocator());
3731 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = _Traits::length(__lhs);
3732 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = __rhs.size();
3733 __r.__init(__lhs, __lhs_sz, __lhs_sz + __rhs_sz);
3734 __r.append(__rhs.data(), __rhs_sz);
3735 return __r;
3736}
3737
3738template<class _CharT, class _Traits, class _Allocator>
3739basic_string<_CharT, _Traits, _Allocator>
3740operator+(_CharT __lhs, const basic_string<_CharT,_Traits,_Allocator>& __rhs)
3741{
3742 basic_string<_CharT, _Traits, _Allocator> __r(__rhs.get_allocator());
3743 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = __rhs.size();
3744 __r.__init(&__lhs, 1, 1 + __rhs_sz);
3745 __r.append(__rhs.data(), __rhs_sz);
3746 return __r;
3747}
3748
3749template<class _CharT, class _Traits, class _Allocator>
3750basic_string<_CharT, _Traits, _Allocator>
3751operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, const _CharT* __rhs)
3752{
3753 basic_string<_CharT, _Traits, _Allocator> __r(__lhs.get_allocator());
3754 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = __lhs.size();
3755 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = _Traits::length(__rhs);
3756 __r.__init(__lhs.data(), __lhs_sz, __lhs_sz + __rhs_sz);
3757 __r.append(__rhs, __rhs_sz);
3758 return __r;
3759}
3760
3761template<class _CharT, class _Traits, class _Allocator>
3762basic_string<_CharT, _Traits, _Allocator>
3763operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, _CharT __rhs)
3764{
3765 basic_string<_CharT, _Traits, _Allocator> __r(__lhs.get_allocator());
3766 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = __lhs.size();
3767 __r.__init(__lhs.data(), __lhs_sz, __lhs_sz + 1);
3768 __r.push_back(__rhs);
3769 return __r;
3770}
3771
Howard Hinnant73d21a42010-09-04 23:28:19 +00003772#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003773
3774template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003775inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003776basic_string<_CharT, _Traits, _Allocator>
3777operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, const basic_string<_CharT, _Traits, _Allocator>& __rhs)
3778{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003779 return _VSTD::move(__lhs.append(__rhs));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003780}
3781
3782template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003783inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003784basic_string<_CharT, _Traits, _Allocator>
3785operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, basic_string<_CharT, _Traits, _Allocator>&& __rhs)
3786{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003787 return _VSTD::move(__rhs.insert(0, __lhs));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003788}
3789
3790template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003791inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003792basic_string<_CharT, _Traits, _Allocator>
3793operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, basic_string<_CharT, _Traits, _Allocator>&& __rhs)
3794{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003795 return _VSTD::move(__lhs.append(__rhs));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003796}
3797
3798template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003799inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003800basic_string<_CharT, _Traits, _Allocator>
3801operator+(const _CharT* __lhs , basic_string<_CharT,_Traits,_Allocator>&& __rhs)
3802{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003803 return _VSTD::move(__rhs.insert(0, __lhs));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003804}
3805
3806template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003807inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003808basic_string<_CharT, _Traits, _Allocator>
3809operator+(_CharT __lhs, basic_string<_CharT,_Traits,_Allocator>&& __rhs)
3810{
3811 __rhs.insert(__rhs.begin(), __lhs);
Howard Hinnant0949eed2011-06-30 21:18:19 +00003812 return _VSTD::move(__rhs);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003813}
3814
3815template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003816inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003817basic_string<_CharT, _Traits, _Allocator>
3818operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, const _CharT* __rhs)
3819{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003820 return _VSTD::move(__lhs.append(__rhs));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003821}
3822
3823template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003824inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003825basic_string<_CharT, _Traits, _Allocator>
3826operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, _CharT __rhs)
3827{
3828 __lhs.push_back(__rhs);
Howard Hinnant0949eed2011-06-30 21:18:19 +00003829 return _VSTD::move(__lhs);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003830}
3831
Howard Hinnant73d21a42010-09-04 23:28:19 +00003832#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003833
3834// swap
3835
3836template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003837inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003838void
Howard Hinnanta6119a82011-05-29 19:57:12 +00003839swap(basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnant53f7d4c2011-06-03 18:40:47 +00003840 basic_string<_CharT, _Traits, _Allocator>& __rhs)
3841 _NOEXCEPT_(_NOEXCEPT_(__lhs.swap(__rhs)))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003842{
3843 __lhs.swap(__rhs);
3844}
3845
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003846#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
3847
3848typedef basic_string<char16_t> u16string;
3849typedef basic_string<char32_t> u32string;
3850
Howard Hinnant324bb032010-08-22 00:02:43 +00003851#endif // _LIBCPP_HAS_NO_UNICODE_CHARS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003852
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003853_LIBCPP_FUNC_VIS int stoi (const string& __str, size_t* __idx = 0, int __base = 10);
3854_LIBCPP_FUNC_VIS long stol (const string& __str, size_t* __idx = 0, int __base = 10);
3855_LIBCPP_FUNC_VIS unsigned long stoul (const string& __str, size_t* __idx = 0, int __base = 10);
3856_LIBCPP_FUNC_VIS long long stoll (const string& __str, size_t* __idx = 0, int __base = 10);
3857_LIBCPP_FUNC_VIS unsigned long long stoull(const string& __str, size_t* __idx = 0, int __base = 10);
Howard Hinnanta6a062d2010-06-02 18:20:39 +00003858
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003859_LIBCPP_FUNC_VIS float stof (const string& __str, size_t* __idx = 0);
3860_LIBCPP_FUNC_VIS double stod (const string& __str, size_t* __idx = 0);
3861_LIBCPP_FUNC_VIS long double stold(const string& __str, size_t* __idx = 0);
Howard Hinnanta6a062d2010-06-02 18:20:39 +00003862
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003863_LIBCPP_FUNC_VIS string to_string(int __val);
3864_LIBCPP_FUNC_VIS string to_string(unsigned __val);
3865_LIBCPP_FUNC_VIS string to_string(long __val);
3866_LIBCPP_FUNC_VIS string to_string(unsigned long __val);
3867_LIBCPP_FUNC_VIS string to_string(long long __val);
3868_LIBCPP_FUNC_VIS string to_string(unsigned long long __val);
3869_LIBCPP_FUNC_VIS string to_string(float __val);
3870_LIBCPP_FUNC_VIS string to_string(double __val);
3871_LIBCPP_FUNC_VIS string to_string(long double __val);
Howard Hinnanta6a062d2010-06-02 18:20:39 +00003872
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003873_LIBCPP_FUNC_VIS int stoi (const wstring& __str, size_t* __idx = 0, int __base = 10);
3874_LIBCPP_FUNC_VIS long stol (const wstring& __str, size_t* __idx = 0, int __base = 10);
3875_LIBCPP_FUNC_VIS unsigned long stoul (const wstring& __str, size_t* __idx = 0, int __base = 10);
3876_LIBCPP_FUNC_VIS long long stoll (const wstring& __str, size_t* __idx = 0, int __base = 10);
3877_LIBCPP_FUNC_VIS unsigned long long stoull(const wstring& __str, size_t* __idx = 0, int __base = 10);
Howard Hinnanta6a062d2010-06-02 18:20:39 +00003878
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003879_LIBCPP_FUNC_VIS float stof (const wstring& __str, size_t* __idx = 0);
3880_LIBCPP_FUNC_VIS double stod (const wstring& __str, size_t* __idx = 0);
3881_LIBCPP_FUNC_VIS long double stold(const wstring& __str, size_t* __idx = 0);
Howard Hinnanta6a062d2010-06-02 18:20:39 +00003882
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003883_LIBCPP_FUNC_VIS wstring to_wstring(int __val);
3884_LIBCPP_FUNC_VIS wstring to_wstring(unsigned __val);
3885_LIBCPP_FUNC_VIS wstring to_wstring(long __val);
3886_LIBCPP_FUNC_VIS wstring to_wstring(unsigned long __val);
3887_LIBCPP_FUNC_VIS wstring to_wstring(long long __val);
3888_LIBCPP_FUNC_VIS wstring to_wstring(unsigned long long __val);
3889_LIBCPP_FUNC_VIS wstring to_wstring(float __val);
3890_LIBCPP_FUNC_VIS wstring to_wstring(double __val);
3891_LIBCPP_FUNC_VIS wstring to_wstring(long double __val);
Howard Hinnanta6a062d2010-06-02 18:20:39 +00003892
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003893template<class _CharT, class _Traits, class _Allocator>
3894 const typename basic_string<_CharT, _Traits, _Allocator>::size_type
3895 basic_string<_CharT, _Traits, _Allocator>::npos;
3896
3897template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierc3589a82017-01-04 23:56:00 +00003898struct _LIBCPP_TEMPLATE_VIS hash<basic_string<_CharT, _Traits, _Allocator> >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003899 : public unary_function<basic_string<_CharT, _Traits, _Allocator>, size_t>
3900{
3901 size_t
Howard Hinnanta6119a82011-05-29 19:57:12 +00003902 operator()(const basic_string<_CharT, _Traits, _Allocator>& __val) const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003903};
3904
3905template<class _CharT, class _Traits, class _Allocator>
3906size_t
3907hash<basic_string<_CharT, _Traits, _Allocator> >::operator()(
Howard Hinnanta6119a82011-05-29 19:57:12 +00003908 const basic_string<_CharT, _Traits, _Allocator>& __val) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003909{
Sean Huntaffd9e52011-07-29 23:31:56 +00003910 return __do_string_hash(__val.data(), __val.data() + __val.size());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003911}
3912
Howard Hinnant464aa5c2011-07-18 15:51:59 +00003913template<class _CharT, class _Traits, class _Allocator>
3914basic_ostream<_CharT, _Traits>&
3915operator<<(basic_ostream<_CharT, _Traits>& __os,
3916 const basic_string<_CharT, _Traits, _Allocator>& __str);
3917
3918template<class _CharT, class _Traits, class _Allocator>
3919basic_istream<_CharT, _Traits>&
3920operator>>(basic_istream<_CharT, _Traits>& __is,
3921 basic_string<_CharT, _Traits, _Allocator>& __str);
3922
3923template<class _CharT, class _Traits, class _Allocator>
3924basic_istream<_CharT, _Traits>&
3925getline(basic_istream<_CharT, _Traits>& __is,
3926 basic_string<_CharT, _Traits, _Allocator>& __str, _CharT __dlm);
3927
3928template<class _CharT, class _Traits, class _Allocator>
3929inline _LIBCPP_INLINE_VISIBILITY
3930basic_istream<_CharT, _Traits>&
3931getline(basic_istream<_CharT, _Traits>& __is,
3932 basic_string<_CharT, _Traits, _Allocator>& __str);
3933
3934#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
3935
3936template<class _CharT, class _Traits, class _Allocator>
3937inline _LIBCPP_INLINE_VISIBILITY
3938basic_istream<_CharT, _Traits>&
3939getline(basic_istream<_CharT, _Traits>&& __is,
3940 basic_string<_CharT, _Traits, _Allocator>& __str, _CharT __dlm);
3941
3942template<class _CharT, class _Traits, class _Allocator>
3943inline _LIBCPP_INLINE_VISIBILITY
3944basic_istream<_CharT, _Traits>&
3945getline(basic_istream<_CharT, _Traits>&& __is,
3946 basic_string<_CharT, _Traits, _Allocator>& __str);
3947
3948#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
3949
Howard Hinnant499cea12013-08-23 17:37:05 +00003950#if _LIBCPP_DEBUG_LEVEL >= 2
3951
3952template<class _CharT, class _Traits, class _Allocator>
3953bool
3954basic_string<_CharT, _Traits, _Allocator>::__dereferenceable(const const_iterator* __i) const
3955{
3956 return this->data() <= _VSTD::__to_raw_pointer(__i->base()) &&
3957 _VSTD::__to_raw_pointer(__i->base()) < this->data() + this->size();
3958}
3959
3960template<class _CharT, class _Traits, class _Allocator>
3961bool
3962basic_string<_CharT, _Traits, _Allocator>::__decrementable(const const_iterator* __i) const
3963{
3964 return this->data() < _VSTD::__to_raw_pointer(__i->base()) &&
3965 _VSTD::__to_raw_pointer(__i->base()) <= this->data() + this->size();
3966}
3967
3968template<class _CharT, class _Traits, class _Allocator>
3969bool
3970basic_string<_CharT, _Traits, _Allocator>::__addable(const const_iterator* __i, ptrdiff_t __n) const
3971{
3972 const value_type* __p = _VSTD::__to_raw_pointer(__i->base()) + __n;
3973 return this->data() <= __p && __p <= this->data() + this->size();
3974}
3975
3976template<class _CharT, class _Traits, class _Allocator>
3977bool
3978basic_string<_CharT, _Traits, _Allocator>::__subscriptable(const const_iterator* __i, ptrdiff_t __n) const
3979{
3980 const value_type* __p = _VSTD::__to_raw_pointer(__i->base()) + __n;
3981 return this->data() <= __p && __p < this->data() + this->size();
3982}
3983
3984#endif // _LIBCPP_DEBUG_LEVEL >= 2
3985
Marshall Clow15234322013-07-23 17:05:24 +00003986#if _LIBCPP_STD_VER > 11
3987// Literal suffixes for basic_string [basic.string.literals]
Marshall Clow8d9dd7a2013-10-05 21:18:32 +00003988inline namespace literals
Marshall Clow15234322013-07-23 17:05:24 +00003989{
3990 inline namespace string_literals
3991 {
Howard Hinnantab61b2c2013-08-07 19:39:48 +00003992 inline _LIBCPP_INLINE_VISIBILITY
3993 basic_string<char> operator "" s( const char *__str, size_t __len )
3994 {
3995 return basic_string<char> (__str, __len);
3996 }
Marshall Clow15234322013-07-23 17:05:24 +00003997
Howard Hinnantab61b2c2013-08-07 19:39:48 +00003998 inline _LIBCPP_INLINE_VISIBILITY
3999 basic_string<wchar_t> operator "" s( const wchar_t *__str, size_t __len )
4000 {
4001 return basic_string<wchar_t> (__str, __len);
4002 }
Marshall Clow15234322013-07-23 17:05:24 +00004003
Howard Hinnantab61b2c2013-08-07 19:39:48 +00004004 inline _LIBCPP_INLINE_VISIBILITY
4005 basic_string<char16_t> operator "" s( const char16_t *__str, size_t __len )
4006 {
4007 return basic_string<char16_t> (__str, __len);
4008 }
Marshall Clow15234322013-07-23 17:05:24 +00004009
Howard Hinnantab61b2c2013-08-07 19:39:48 +00004010 inline _LIBCPP_INLINE_VISIBILITY
4011 basic_string<char32_t> operator "" s( const char32_t *__str, size_t __len )
4012 {
4013 return basic_string<char32_t> (__str, __len);
4014 }
Marshall Clow15234322013-07-23 17:05:24 +00004015 }
4016}
4017#endif
4018
Eric Fiselier833d6442016-09-15 22:27:07 +00004019_LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_string<char>)
4020_LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_string<wchar_t>)
Howard Hinnant499cea12013-08-23 17:37:05 +00004021_LIBCPP_EXTERN_TEMPLATE(string operator+<char, char_traits<char>, allocator<char> >(char const*, string const&))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004022
4023_LIBCPP_END_NAMESPACE_STD
4024
4025#endif // _LIBCPP_STRING