xref: /freebsd/contrib/llvm-project/libcxx/include/string (revision 81ad626541db97eb356e2c1d4a20eb2a26a766ab)
10b57cec5SDimitry Andric// -*- C++ -*-
2349cc55cSDimitry Andric//===----------------------------------------------------------------------===//
30b57cec5SDimitry Andric//
40b57cec5SDimitry Andric// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
50b57cec5SDimitry Andric// See https://llvm.org/LICENSE.txt for license information.
60b57cec5SDimitry Andric// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
70b57cec5SDimitry Andric//
80b57cec5SDimitry Andric//===----------------------------------------------------------------------===//
90b57cec5SDimitry Andric
100b57cec5SDimitry Andric#ifndef _LIBCPP_STRING
110b57cec5SDimitry Andric#define _LIBCPP_STRING
120b57cec5SDimitry Andric
130b57cec5SDimitry Andric/*
140b57cec5SDimitry Andric    string synopsis
150b57cec5SDimitry Andric
160b57cec5SDimitry Andricnamespace std
170b57cec5SDimitry Andric{
180b57cec5SDimitry Andric
190b57cec5SDimitry Andrictemplate <class stateT>
200b57cec5SDimitry Andricclass fpos
210b57cec5SDimitry Andric{
220b57cec5SDimitry Andricprivate:
230b57cec5SDimitry Andric    stateT st;
240b57cec5SDimitry Andricpublic:
250b57cec5SDimitry Andric    fpos(streamoff = streamoff());
260b57cec5SDimitry Andric
270b57cec5SDimitry Andric    operator streamoff() const;
280b57cec5SDimitry Andric
290b57cec5SDimitry Andric    stateT state() const;
300b57cec5SDimitry Andric    void state(stateT);
310b57cec5SDimitry Andric
320b57cec5SDimitry Andric    fpos& operator+=(streamoff);
330b57cec5SDimitry Andric    fpos  operator+ (streamoff) const;
340b57cec5SDimitry Andric    fpos& operator-=(streamoff);
350b57cec5SDimitry Andric    fpos  operator- (streamoff) const;
360b57cec5SDimitry Andric};
370b57cec5SDimitry Andric
380b57cec5SDimitry Andrictemplate <class stateT> streamoff operator-(const fpos<stateT>& x, const fpos<stateT>& y);
390b57cec5SDimitry Andric
400b57cec5SDimitry Andrictemplate <class stateT> bool operator==(const fpos<stateT>& x, const fpos<stateT>& y);
410b57cec5SDimitry Andrictemplate <class stateT> bool operator!=(const fpos<stateT>& x, const fpos<stateT>& y);
420b57cec5SDimitry Andric
430b57cec5SDimitry Andrictemplate <class charT>
440b57cec5SDimitry Andricstruct char_traits
450b57cec5SDimitry Andric{
460b57cec5SDimitry Andric    typedef charT     char_type;
470b57cec5SDimitry Andric    typedef ...       int_type;
480b57cec5SDimitry Andric    typedef streamoff off_type;
490b57cec5SDimitry Andric    typedef streampos pos_type;
500b57cec5SDimitry Andric    typedef mbstate_t state_type;
510b57cec5SDimitry Andric
520b57cec5SDimitry Andric    static void assign(char_type& c1, const char_type& c2) noexcept;
530b57cec5SDimitry Andric    static constexpr bool eq(char_type c1, char_type c2) noexcept;
540b57cec5SDimitry Andric    static constexpr bool lt(char_type c1, char_type c2) noexcept;
550b57cec5SDimitry Andric
560b57cec5SDimitry Andric    static int              compare(const char_type* s1, const char_type* s2, size_t n);
570b57cec5SDimitry Andric    static size_t           length(const char_type* s);
580b57cec5SDimitry Andric    static const char_type* find(const char_type* s, size_t n, const char_type& a);
590b57cec5SDimitry Andric    static char_type*       move(char_type* s1, const char_type* s2, size_t n);
600b57cec5SDimitry Andric    static char_type*       copy(char_type* s1, const char_type* s2, size_t n);
610b57cec5SDimitry Andric    static char_type*       assign(char_type* s, size_t n, char_type a);
620b57cec5SDimitry Andric
630b57cec5SDimitry Andric    static constexpr int_type  not_eof(int_type c) noexcept;
640b57cec5SDimitry Andric    static constexpr char_type to_char_type(int_type c) noexcept;
650b57cec5SDimitry Andric    static constexpr int_type  to_int_type(char_type c) noexcept;
660b57cec5SDimitry Andric    static constexpr bool      eq_int_type(int_type c1, int_type c2) noexcept;
670b57cec5SDimitry Andric    static constexpr int_type  eof() noexcept;
680b57cec5SDimitry Andric};
690b57cec5SDimitry Andric
700b57cec5SDimitry Andrictemplate <> struct char_traits<char>;
710b57cec5SDimitry Andrictemplate <> struct char_traits<wchar_t>;
72fe6060f1SDimitry Andrictemplate <> struct char_traits<char8_t>;  // C++20
73fe6060f1SDimitry Andrictemplate <> struct char_traits<char16_t>;
74fe6060f1SDimitry Andrictemplate <> struct char_traits<char32_t>;
750b57cec5SDimitry Andric
760b57cec5SDimitry Andrictemplate<class charT, class traits = char_traits<charT>, class Allocator = allocator<charT> >
770b57cec5SDimitry Andricclass basic_string
780b57cec5SDimitry Andric{
790b57cec5SDimitry Andricpublic:
800b57cec5SDimitry Andric// types:
810b57cec5SDimitry Andric    typedef traits traits_type;
820b57cec5SDimitry Andric    typedef typename traits_type::char_type value_type;
830b57cec5SDimitry Andric    typedef Allocator allocator_type;
840b57cec5SDimitry Andric    typedef typename allocator_type::size_type size_type;
850b57cec5SDimitry Andric    typedef typename allocator_type::difference_type difference_type;
860b57cec5SDimitry Andric    typedef typename allocator_type::reference reference;
870b57cec5SDimitry Andric    typedef typename allocator_type::const_reference const_reference;
880b57cec5SDimitry Andric    typedef typename allocator_type::pointer pointer;
890b57cec5SDimitry Andric    typedef typename allocator_type::const_pointer const_pointer;
900b57cec5SDimitry Andric    typedef implementation-defined iterator;
910b57cec5SDimitry Andric    typedef implementation-defined const_iterator;
920b57cec5SDimitry Andric    typedef std::reverse_iterator<iterator> reverse_iterator;
930b57cec5SDimitry Andric    typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
940b57cec5SDimitry Andric
950b57cec5SDimitry Andric    static const size_type npos = -1;
960b57cec5SDimitry Andric
970b57cec5SDimitry Andric    basic_string()
98*81ad6265SDimitry Andric        noexcept(is_nothrow_default_constructible<allocator_type>::value);                      // constexpr since C++20
99*81ad6265SDimitry Andric    explicit basic_string(const allocator_type& a);                                             // constexpr since C++20
100*81ad6265SDimitry Andric    basic_string(const basic_string& str);                                                      // constexpr since C++20
1010b57cec5SDimitry Andric    basic_string(basic_string&& str)
102*81ad6265SDimitry Andric        noexcept(is_nothrow_move_constructible<allocator_type>::value);                         // constexpr since C++20
1030b57cec5SDimitry Andric    basic_string(const basic_string& str, size_type pos,
104*81ad6265SDimitry Andric                 const allocator_type& a = allocator_type());                                   // constexpr since C++20
1050b57cec5SDimitry Andric    basic_string(const basic_string& str, size_type pos, size_type n,
106*81ad6265SDimitry Andric                 const Allocator& a = Allocator());                                             // constexpr since C++20
1070b57cec5SDimitry Andric    template<class T>
108*81ad6265SDimitry Andric        basic_string(const T& t, size_type pos, size_type n, const Allocator& a = Allocator()); // C++17, constexpr since C++20
1090b57cec5SDimitry Andric    template <class T>
110*81ad6265SDimitry Andric        explicit basic_string(const T& t, const Allocator& a = Allocator());                    // C++17, constexpr since C++20
111*81ad6265SDimitry Andric    basic_string(const value_type* s, const allocator_type& a = allocator_type());              // constexpr since C++20
112*81ad6265SDimitry Andric    basic_string(const value_type* s, size_type n, const allocator_type& a = allocator_type()); // constexpr since C++20
113fe6060f1SDimitry Andric    basic_string(nullptr_t) = delete; // C++2b
114*81ad6265SDimitry Andric    basic_string(size_type n, value_type c, const allocator_type& a = allocator_type());        // constexpr since C++20
1150b57cec5SDimitry Andric    template<class InputIterator>
1160b57cec5SDimitry Andric        basic_string(InputIterator begin, InputIterator end,
117*81ad6265SDimitry Andric                     const allocator_type& a = allocator_type());                               // constexpr since C++20
118*81ad6265SDimitry Andric    basic_string(initializer_list<value_type>, const Allocator& = Allocator());                 // constexpr since C++20
119*81ad6265SDimitry Andric    basic_string(const basic_string&, const Allocator&);                                        // constexpr since C++20
120*81ad6265SDimitry Andric    basic_string(basic_string&&, const Allocator&);                                             // constexpr since C++20
1210b57cec5SDimitry Andric
122*81ad6265SDimitry Andric    ~basic_string();                                                                            // constexpr since C++20
1230b57cec5SDimitry Andric
124*81ad6265SDimitry Andric    operator basic_string_view<charT, traits>() const noexcept;                                 // constexpr since C++20
1250b57cec5SDimitry Andric
126*81ad6265SDimitry Andric    basic_string& operator=(const basic_string& str);                                           // constexpr since C++20
1270b57cec5SDimitry Andric    template <class T>
128*81ad6265SDimitry Andric        basic_string& operator=(const T& t);                                                    // C++17, constexpr since C++20
1290b57cec5SDimitry Andric    basic_string& operator=(basic_string&& str)
1300b57cec5SDimitry Andric        noexcept(
1310b57cec5SDimitry Andric             allocator_type::propagate_on_container_move_assignment::value ||
132*81ad6265SDimitry Andric             allocator_type::is_always_equal::value );                                          // C++17, constexpr since C++20
133*81ad6265SDimitry Andric    basic_string& operator=(const value_type* s);                                               // constexpr since C++20
134fe6060f1SDimitry Andric    basic_string& operator=(nullptr_t) = delete; // C++2b
135*81ad6265SDimitry Andric    basic_string& operator=(value_type c);                                                      // constexpr since C++20
136*81ad6265SDimitry Andric    basic_string& operator=(initializer_list<value_type>);                                      // constexpr since C++20
1370b57cec5SDimitry Andric
138*81ad6265SDimitry Andric    iterator       begin() noexcept;                                                            // constexpr since C++20
139*81ad6265SDimitry Andric    const_iterator begin() const noexcept;                                                      // constexpr since C++20
140*81ad6265SDimitry Andric    iterator       end() noexcept;                                                              // constexpr since C++20
141*81ad6265SDimitry Andric    const_iterator end() const noexcept;                                                        // constexpr since C++20
1420b57cec5SDimitry Andric
143*81ad6265SDimitry Andric    reverse_iterator       rbegin() noexcept;                                                   // constexpr since C++20
144*81ad6265SDimitry Andric    const_reverse_iterator rbegin() const noexcept;                                             // constexpr since C++20
145*81ad6265SDimitry Andric    reverse_iterator       rend() noexcept;                                                     // constexpr since C++20
146*81ad6265SDimitry Andric    const_reverse_iterator rend() const noexcept;                                               // constexpr since C++20
1470b57cec5SDimitry Andric
148*81ad6265SDimitry Andric    const_iterator         cbegin() const noexcept;                                             // constexpr since C++20
149*81ad6265SDimitry Andric    const_iterator         cend() const noexcept;                                               // constexpr since C++20
150*81ad6265SDimitry Andric    const_reverse_iterator crbegin() const noexcept;                                            // constexpr since C++20
151*81ad6265SDimitry Andric    const_reverse_iterator crend() const noexcept;                                              // constexpr since C++20
1520b57cec5SDimitry Andric
153*81ad6265SDimitry Andric    size_type size() const noexcept;                                                            // constexpr since C++20
154*81ad6265SDimitry Andric    size_type length() const noexcept;                                                          // constexpr since C++20
155*81ad6265SDimitry Andric    size_type max_size() const noexcept;                                                        // constexpr since C++20
156*81ad6265SDimitry Andric    size_type capacity() const noexcept;                                                        // constexpr since C++20
1570b57cec5SDimitry Andric
158*81ad6265SDimitry Andric    void resize(size_type n, value_type c);                                                     // constexpr since C++20
159*81ad6265SDimitry Andric    void resize(size_type n);                                                                   // constexpr since C++20
1600b57cec5SDimitry Andric
16104eeddc0SDimitry Andric    template<class Operation>
16204eeddc0SDimitry Andric    constexpr void resize_and_overwrite(size_type n, Operation op); // since C++23
16304eeddc0SDimitry Andric
164*81ad6265SDimitry Andric    void reserve(size_type res_arg);                                                            // constexpr since C++20
165e8d8bef9SDimitry Andric    void reserve(); // deprecated in C++20
166*81ad6265SDimitry Andric    void shrink_to_fit();                                                                       // constexpr since C++20
167*81ad6265SDimitry Andric    void clear() noexcept;                                                                      // constexpr since C++20
168*81ad6265SDimitry Andric    bool empty() const noexcept;                                                                // constexpr since C++20
1690b57cec5SDimitry Andric
170*81ad6265SDimitry Andric    const_reference operator[](size_type pos) const;                                            // constexpr since C++20
171*81ad6265SDimitry Andric    reference       operator[](size_type pos);                                                  // constexpr since C++20
1720b57cec5SDimitry Andric
173*81ad6265SDimitry Andric    const_reference at(size_type n) const;                                                      // constexpr since C++20
174*81ad6265SDimitry Andric    reference       at(size_type n);                                                            // constexpr since C++20
1750b57cec5SDimitry Andric
176*81ad6265SDimitry Andric    basic_string& operator+=(const basic_string& str);                                          // constexpr since C++20
1770b57cec5SDimitry Andric    template <class T>
178*81ad6265SDimitry Andric        basic_string& operator+=(const T& t);                                                   // C++17, constexpr since C++20
179*81ad6265SDimitry Andric    basic_string& operator+=(const value_type* s);                                              // constexpr since C++20
180*81ad6265SDimitry Andric    basic_string& operator+=(value_type c);                                                     // constexpr since C++20
181*81ad6265SDimitry Andric    basic_string& operator+=(initializer_list<value_type>);                                     // constexpr since C++20
1820b57cec5SDimitry Andric
183*81ad6265SDimitry Andric    basic_string& append(const basic_string& str);                                              // constexpr since C++20
1840b57cec5SDimitry Andric    template <class T>
185*81ad6265SDimitry Andric        basic_string& append(const T& t);                                                       // C++17, constexpr since C++20
186*81ad6265SDimitry Andric    basic_string& append(const basic_string& str, size_type pos, size_type n=npos);             // C++14, constexpr since C++20
1870b57cec5SDimitry Andric    template <class T>
188*81ad6265SDimitry Andric        basic_string& append(const T& t, size_type pos, size_type n=npos);                      // C++17, constexpr since C++20
189*81ad6265SDimitry Andric    basic_string& append(const value_type* s, size_type n);                                     // constexpr since C++20
190*81ad6265SDimitry Andric    basic_string& append(const value_type* s);                                                  // constexpr since C++20
191*81ad6265SDimitry Andric    basic_string& append(size_type n, value_type c);                                            // constexpr since C++20
1920b57cec5SDimitry Andric    template<class InputIterator>
193*81ad6265SDimitry Andric        basic_string& append(InputIterator first, InputIterator last);                          // constexpr since C++20
194*81ad6265SDimitry Andric    basic_string& append(initializer_list<value_type>);                                         // constexpr since C++20
1950b57cec5SDimitry Andric
196*81ad6265SDimitry Andric    void push_back(value_type c);                                                               // constexpr since C++20
197*81ad6265SDimitry Andric    void pop_back();                                                                            // constexpr since C++20
198*81ad6265SDimitry Andric    reference       front();                                                                    // constexpr since C++20
199*81ad6265SDimitry Andric    const_reference front() const;                                                              // constexpr since C++20
200*81ad6265SDimitry Andric    reference       back();                                                                     // constexpr since C++20
201*81ad6265SDimitry Andric    const_reference back() const;                                                               // constexpr since C++20
2020b57cec5SDimitry Andric
203*81ad6265SDimitry Andric    basic_string& assign(const basic_string& str);                                              // constexpr since C++20
2040b57cec5SDimitry Andric    template <class T>
205*81ad6265SDimitry Andric        basic_string& assign(const T& t);                                                       // C++17, constexpr since C++20
206*81ad6265SDimitry Andric    basic_string& assign(basic_string&& str);                                                   // constexpr since C++20
207*81ad6265SDimitry Andric    basic_string& assign(const basic_string& str, size_type pos, size_type n=npos);             // C++14, constexpr since C++20
2080b57cec5SDimitry Andric    template <class T>
209*81ad6265SDimitry Andric        basic_string& assign(const T& t, size_type pos, size_type n=npos);                      // C++17, constexpr since C++20
210*81ad6265SDimitry Andric    basic_string& assign(const value_type* s, size_type n);                                     // constexpr since C++20
211*81ad6265SDimitry Andric    basic_string& assign(const value_type* s);                                                  // constexpr since C++20
212*81ad6265SDimitry Andric    basic_string& assign(size_type n, value_type c);                                            // constexpr since C++20
2130b57cec5SDimitry Andric    template<class InputIterator>
214*81ad6265SDimitry Andric        basic_string& assign(InputIterator first, InputIterator last);                          // constexpr since C++20
215*81ad6265SDimitry Andric    basic_string& assign(initializer_list<value_type>);                                         // constexpr since C++20
2160b57cec5SDimitry Andric
217*81ad6265SDimitry Andric    basic_string& insert(size_type pos1, const basic_string& str);                              // constexpr since C++20
2180b57cec5SDimitry Andric    template <class T>
219*81ad6265SDimitry Andric        basic_string& insert(size_type pos1, const T& t);                                       // constexpr since C++20
2200b57cec5SDimitry Andric    basic_string& insert(size_type pos1, const basic_string& str,
221*81ad6265SDimitry Andric                         size_type pos2, size_type n);                                          // constexpr since C++20
2220b57cec5SDimitry Andric    template <class T>
223*81ad6265SDimitry Andric        basic_string& insert(size_type pos1, const T& t, size_type pos2, size_type n);          // C++17, constexpr since C++20
224*81ad6265SDimitry Andric    basic_string& insert(size_type pos, const value_type* s, size_type n=npos);                 // C++14, constexpr since C++20
225*81ad6265SDimitry Andric    basic_string& insert(size_type pos, const value_type* s);                                   // constexpr since C++20
226*81ad6265SDimitry Andric    basic_string& insert(size_type pos, size_type n, value_type c);                             // constexpr since C++20
227*81ad6265SDimitry Andric    iterator      insert(const_iterator p, value_type c);                                       // constexpr since C++20
228*81ad6265SDimitry Andric    iterator      insert(const_iterator p, size_type n, value_type c);                          // constexpr since C++20
2290b57cec5SDimitry Andric    template<class InputIterator>
230*81ad6265SDimitry Andric        iterator insert(const_iterator p, InputIterator first, InputIterator last);             // constexpr since C++20
231*81ad6265SDimitry Andric    iterator      insert(const_iterator p, initializer_list<value_type>);                       // constexpr since C++20
2320b57cec5SDimitry Andric
233*81ad6265SDimitry Andric    basic_string& erase(size_type pos = 0, size_type n = npos);                                 // constexpr since C++20
234*81ad6265SDimitry Andric    iterator      erase(const_iterator position);                                               // constexpr since C++20
235*81ad6265SDimitry Andric    iterator      erase(const_iterator first, const_iterator last);                             // constexpr since C++20
2360b57cec5SDimitry Andric
237*81ad6265SDimitry Andric    basic_string& replace(size_type pos1, size_type n1, const basic_string& str);               // constexpr since C++20
2380b57cec5SDimitry Andric    template <class T>
239*81ad6265SDimitry Andric    basic_string& replace(size_type pos1, size_type n1, const T& t);                            // C++17, constexpr since C++20
2400b57cec5SDimitry Andric    basic_string& replace(size_type pos1, size_type n1, const basic_string& str,
241*81ad6265SDimitry Andric                          size_type pos2, size_type n2=npos);                                   // C++14, constexpr since C++20
2420b57cec5SDimitry Andric    template <class T>
2430b57cec5SDimitry Andric        basic_string& replace(size_type pos1, size_type n1, const T& t,
244*81ad6265SDimitry Andric                              size_type pos2, size_type n);                                     // C++17, constexpr since C++20
245*81ad6265SDimitry Andric    basic_string& replace(size_type pos, size_type n1, const value_type* s, size_type n2);      // constexpr since C++20
246*81ad6265SDimitry Andric    basic_string& replace(size_type pos, size_type n1, const value_type* s);                    // constexpr since C++20
247*81ad6265SDimitry Andric    basic_string& replace(size_type pos, size_type n1, size_type n2, value_type c);             // constexpr since C++20
248*81ad6265SDimitry Andric    basic_string& replace(const_iterator i1, const_iterator i2, const basic_string& str);       // constexpr since C++20
2490b57cec5SDimitry Andric    template <class T>
250*81ad6265SDimitry Andric        basic_string& replace(const_iterator i1, const_iterator i2, const T& t);                // C++17, constexpr since C++20
251*81ad6265SDimitry Andric    basic_string& replace(const_iterator i1, const_iterator i2, const value_type* s, size_type n); // constexpr since C++20
252*81ad6265SDimitry Andric    basic_string& replace(const_iterator i1, const_iterator i2, const value_type* s);           // constexpr since C++20
253*81ad6265SDimitry Andric    basic_string& replace(const_iterator i1, const_iterator i2, size_type n, value_type c);     // constexpr since C++20
2540b57cec5SDimitry Andric    template<class InputIterator>
255*81ad6265SDimitry Andric        basic_string& replace(const_iterator i1, const_iterator i2, InputIterator j1, InputIterator j2); // constexpr since C++20
256*81ad6265SDimitry Andric    basic_string& replace(const_iterator i1, const_iterator i2, initializer_list<value_type>);  // constexpr since C++20
2570b57cec5SDimitry Andric
258*81ad6265SDimitry Andric    size_type copy(value_type* s, size_type n, size_type pos = 0) const;                        // constexpr since C++20
259*81ad6265SDimitry Andric    basic_string substr(size_type pos = 0, size_type n = npos) const;                           // constexpr since C++20
2600b57cec5SDimitry Andric
2610b57cec5SDimitry Andric    void swap(basic_string& str)
2620b57cec5SDimitry Andric        noexcept(allocator_traits<allocator_type>::propagate_on_container_swap::value ||
263*81ad6265SDimitry Andric                 allocator_traits<allocator_type>::is_always_equal::value);                     // C++17, constexpr since C++20
2640b57cec5SDimitry Andric
265*81ad6265SDimitry Andric    const value_type* c_str() const noexcept;                                                   // constexpr since C++20
266*81ad6265SDimitry Andric    const value_type* data() const noexcept;                                                    // constexpr since C++20
267*81ad6265SDimitry Andric          value_type* data()       noexcept;                                                    // C++17, constexpr since C++20
2680b57cec5SDimitry Andric
269*81ad6265SDimitry Andric    allocator_type get_allocator() const noexcept;                                              // constexpr since C++20
2700b57cec5SDimitry Andric
271*81ad6265SDimitry Andric    size_type find(const basic_string& str, size_type pos = 0) const noexcept;                  // constexpr since C++20
2720b57cec5SDimitry Andric    template <class T>
273*81ad6265SDimitry Andric        size_type find(const T& t, size_type pos = 0) const noexcept;                           // C++17, noexcept as an extension, constexpr since C++20
274*81ad6265SDimitry Andric    size_type find(const value_type* s, size_type pos, size_type n) const noexcept;             // constexpr since C++20
275*81ad6265SDimitry Andric    size_type find(const value_type* s, size_type pos = 0) const noexcept;                      // constexpr since C++20
276*81ad6265SDimitry Andric    size_type find(value_type c, size_type pos = 0) const noexcept;                             // constexpr since C++20
2770b57cec5SDimitry Andric
278*81ad6265SDimitry Andric    size_type rfind(const basic_string& str, size_type pos = npos) const noexcept;              // constexpr since C++20
2790b57cec5SDimitry Andric    template <class T>
280*81ad6265SDimitry Andric        size_type rfind(const T& t, size_type pos = npos) const noexcept;                       // C++17, noexcept as an extension, constexpr since C++20
281*81ad6265SDimitry Andric    size_type rfind(const value_type* s, size_type pos, size_type n) const noexcept;            // constexpr since C++20
282*81ad6265SDimitry Andric    size_type rfind(const value_type* s, size_type pos = npos) const noexcept;                  // constexpr since C++20
283*81ad6265SDimitry Andric    size_type rfind(value_type c, size_type pos = npos) const noexcept;                         // constexpr since C++20
2840b57cec5SDimitry Andric
285*81ad6265SDimitry Andric    size_type find_first_of(const basic_string& str, size_type pos = 0) const noexcept;         // constexpr since C++20
2860b57cec5SDimitry Andric    template <class T>
287*81ad6265SDimitry Andric        size_type find_first_of(const T& t, size_type pos = 0) const noexcept;                  // C++17, noexcept as an extension, constexpr since C++20
288*81ad6265SDimitry Andric    size_type find_first_of(const value_type* s, size_type pos, size_type n) const noexcept;    // constexpr since C++20
289*81ad6265SDimitry Andric    size_type find_first_of(const value_type* s, size_type pos = 0) const noexcept;             // constexpr since C++20
290*81ad6265SDimitry Andric    size_type find_first_of(value_type c, size_type pos = 0) const noexcept;                    // constexpr since C++20
2910b57cec5SDimitry Andric
292*81ad6265SDimitry Andric    size_type find_last_of(const basic_string& str, size_type pos = npos) const noexcept;       // constexpr since C++20
2930b57cec5SDimitry Andric    template <class T>
294*81ad6265SDimitry Andric        size_type find_last_of(const T& t, size_type pos = npos) const noexcept noexcept;       // C++17, noexcept as an extension, constexpr since C++20
295*81ad6265SDimitry Andric    size_type find_last_of(const value_type* s, size_type pos, size_type n) const noexcept;     // constexpr since C++20
296*81ad6265SDimitry Andric    size_type find_last_of(const value_type* s, size_type pos = npos) const noexcept;           // constexpr since C++20
297*81ad6265SDimitry Andric    size_type find_last_of(value_type c, size_type pos = npos) const noexcept;                  // constexpr since C++20
2980b57cec5SDimitry Andric
299*81ad6265SDimitry Andric    size_type find_first_not_of(const basic_string& str, size_type pos = 0) const noexcept;     // constexpr since C++20
3000b57cec5SDimitry Andric    template <class T>
301*81ad6265SDimitry Andric        size_type find_first_not_of(const T& t, size_type pos = 0) const noexcept;              // C++17, noexcept as an extension, constexpr since C++20
302*81ad6265SDimitry Andric    size_type find_first_not_of(const value_type* s, size_type pos, size_type n) const noexcept; // constexpr since C++20
303*81ad6265SDimitry Andric    size_type find_first_not_of(const value_type* s, size_type pos = 0) const noexcept;         // constexpr since C++20
304*81ad6265SDimitry Andric    size_type find_first_not_of(value_type c, size_type pos = 0) const noexcept;                // constexpr since C++20
3050b57cec5SDimitry Andric
306*81ad6265SDimitry Andric    size_type find_last_not_of(const basic_string& str, size_type pos = npos) const noexcept;   // constexpr since C++20
3070b57cec5SDimitry Andric    template <class T>
308*81ad6265SDimitry Andric        size_type find_last_not_of(const T& t, size_type pos = npos) const noexcept;            // C++17, noexcept as an extension, constexpr since C++20
309*81ad6265SDimitry Andric    size_type find_last_not_of(const value_type* s, size_type pos, size_type n) const noexcept; // constexpr since C++20
310*81ad6265SDimitry Andric    size_type find_last_not_of(const value_type* s, size_type pos = npos) const noexcept;       // constexpr since C++20
311*81ad6265SDimitry Andric    size_type find_last_not_of(value_type c, size_type pos = npos) const noexcept;              // constexpr since C++20
3120b57cec5SDimitry Andric
313*81ad6265SDimitry Andric    int compare(const basic_string& str) const noexcept;                                        // constexpr since C++20
3140b57cec5SDimitry Andric    template <class T>
315*81ad6265SDimitry Andric        int compare(const T& t) const noexcept;                                                 // C++17, noexcept as an extension, constexpr since C++20
316*81ad6265SDimitry Andric    int compare(size_type pos1, size_type n1, const basic_string& str) const;                   // constexpr since C++20
3170b57cec5SDimitry Andric    template <class T>
318*81ad6265SDimitry Andric        int compare(size_type pos1, size_type n1, const T& t) const;                            // C++17, constexpr since C++20
3190b57cec5SDimitry Andric    int compare(size_type pos1, size_type n1, const basic_string& str,
320*81ad6265SDimitry Andric                size_type pos2, size_type n2=npos) const;                                       // C++14, constexpr since C++20
3210b57cec5SDimitry Andric    template <class T>
3220b57cec5SDimitry Andric        int compare(size_type pos1, size_type n1, const T& t,
323*81ad6265SDimitry Andric                    size_type pos2, size_type n2=npos) const;                                   // C++17, constexpr since C++20
324*81ad6265SDimitry Andric    int compare(const value_type* s) const noexcept;                                            // constexpr since C++20
325*81ad6265SDimitry Andric    int compare(size_type pos1, size_type n1, const value_type* s) const;                       // constexpr since C++20
326*81ad6265SDimitry Andric    int compare(size_type pos1, size_type n1, const value_type* s, size_type n2) const;         // constexpr since C++20
3270b57cec5SDimitry Andric
328*81ad6265SDimitry Andric    constexpr bool starts_with(basic_string_view<charT, traits> sv) const noexcept;             // C++20
329*81ad6265SDimitry Andric    constexpr bool starts_with(charT c) const noexcept;                                         // C++20
330*81ad6265SDimitry Andric    constexpr bool starts_with(const charT* s) const;                                           // C++20
331*81ad6265SDimitry Andric    constexpr bool ends_with(basic_string_view<charT, traits> sv) const noexcept;               // C++20
332*81ad6265SDimitry Andric    constexpr bool ends_with(charT c) const noexcept;                                           // C++20
333*81ad6265SDimitry Andric    constexpr bool ends_with(const charT* s) const;                                             // C++20
334e8d8bef9SDimitry Andric
335e8d8bef9SDimitry Andric    constexpr bool contains(basic_string_view<charT, traits> sv) const noexcept;                // C++2b
336e8d8bef9SDimitry Andric    constexpr bool contains(charT c) const noexcept;                                            // C++2b
337e8d8bef9SDimitry Andric    constexpr bool contains(const charT* s) const;                                              // C++2b
3380b57cec5SDimitry Andric};
3390b57cec5SDimitry Andric
3400b57cec5SDimitry Andrictemplate<class InputIterator,
3410b57cec5SDimitry Andric         class Allocator = allocator<typename iterator_traits<InputIterator>::value_type>>
3420b57cec5SDimitry Andricbasic_string(InputIterator, InputIterator, Allocator = Allocator())
3430b57cec5SDimitry Andric   -> basic_string<typename iterator_traits<InputIterator>::value_type,
3440b57cec5SDimitry Andric                  char_traits<typename iterator_traits<InputIterator>::value_type>,
3450b57cec5SDimitry Andric                  Allocator>;   // C++17
3460b57cec5SDimitry Andric
3470b57cec5SDimitry Andrictemplate<class charT, class traits, class Allocator>
3480b57cec5SDimitry Andricbasic_string<charT, traits, Allocator>
3490b57cec5SDimitry Andricoperator+(const basic_string<charT, traits, Allocator>& lhs,
350*81ad6265SDimitry Andric          const basic_string<charT, traits, Allocator>& rhs);                                   // constexpr since C++20
3510b57cec5SDimitry Andric
3520b57cec5SDimitry Andrictemplate<class charT, class traits, class Allocator>
3530b57cec5SDimitry Andricbasic_string<charT, traits, Allocator>
354*81ad6265SDimitry Andricoperator+(const charT* lhs , const basic_string<charT,traits,Allocator>&rhs);                   // constexpr since C++20
3550b57cec5SDimitry Andric
3560b57cec5SDimitry Andrictemplate<class charT, class traits, class Allocator>
3570b57cec5SDimitry Andricbasic_string<charT, traits, Allocator>
358*81ad6265SDimitry Andricoperator+(charT lhs, const basic_string<charT,traits,Allocator>& rhs);                          // constexpr since C++20
3590b57cec5SDimitry Andric
3600b57cec5SDimitry Andrictemplate<class charT, class traits, class Allocator>
3610b57cec5SDimitry Andricbasic_string<charT, traits, Allocator>
362*81ad6265SDimitry Andricoperator+(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs);                 // constexpr since C++20
3630b57cec5SDimitry Andric
3640b57cec5SDimitry Andrictemplate<class charT, class traits, class Allocator>
3650b57cec5SDimitry Andricbasic_string<charT, traits, Allocator>
366*81ad6265SDimitry Andricoperator+(const basic_string<charT, traits, Allocator>& lhs, charT rhs);                        // constexpr since C++20
3670b57cec5SDimitry Andric
3680b57cec5SDimitry Andrictemplate<class charT, class traits, class Allocator>
3690b57cec5SDimitry Andricbool operator==(const basic_string<charT, traits, Allocator>& lhs,
370*81ad6265SDimitry Andric                const basic_string<charT, traits, Allocator>& rhs) noexcept;                    // constexpr since C++20
3710b57cec5SDimitry Andric
3720b57cec5SDimitry Andrictemplate<class charT, class traits, class Allocator>
373*81ad6265SDimitry Andricbool operator==(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;  // constexpr since C++20
3740b57cec5SDimitry Andric
3750b57cec5SDimitry Andrictemplate<class charT, class traits, class Allocator>
376*81ad6265SDimitry Andricbool operator==(const basic_string<charT,traits,Allocator>& lhs, const charT* rhs) noexcept;    // constexpr since C++20
3770b57cec5SDimitry Andric
3780b57cec5SDimitry Andrictemplate<class charT, class traits, class Allocator>
3790b57cec5SDimitry Andricbool operator!=(const basic_string<charT,traits,Allocator>& lhs,
380*81ad6265SDimitry Andric                const basic_string<charT, traits, Allocator>& rhs) noexcept;                    // constexpr since C++20
3810b57cec5SDimitry Andric
3820b57cec5SDimitry Andrictemplate<class charT, class traits, class Allocator>
383*81ad6265SDimitry Andricbool operator!=(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;  // constexpr since C++20
3840b57cec5SDimitry Andric
3850b57cec5SDimitry Andrictemplate<class charT, class traits, class Allocator>
386*81ad6265SDimitry Andricbool operator!=(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept;  // constexpr since C++20
3870b57cec5SDimitry Andric
3880b57cec5SDimitry Andrictemplate<class charT, class traits, class Allocator>
3890b57cec5SDimitry Andricbool operator< (const basic_string<charT, traits, Allocator>& lhs,
390*81ad6265SDimitry Andric                const basic_string<charT, traits, Allocator>& rhs) noexcept;                    // constexpr since C++20
3910b57cec5SDimitry Andric
3920b57cec5SDimitry Andrictemplate<class charT, class traits, class Allocator>
393*81ad6265SDimitry Andricbool operator< (const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept;  // constexpr since C++20
3940b57cec5SDimitry Andric
3950b57cec5SDimitry Andrictemplate<class charT, class traits, class Allocator>
396*81ad6265SDimitry Andricbool operator< (const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;  // constexpr since C++20
3970b57cec5SDimitry Andric
3980b57cec5SDimitry Andrictemplate<class charT, class traits, class Allocator>
3990b57cec5SDimitry Andricbool operator> (const basic_string<charT, traits, Allocator>& lhs,
400*81ad6265SDimitry Andric                const basic_string<charT, traits, Allocator>& rhs) noexcept;                    // constexpr since C++20
4010b57cec5SDimitry Andric
4020b57cec5SDimitry Andrictemplate<class charT, class traits, class Allocator>
403*81ad6265SDimitry Andricbool operator> (const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept;  // constexpr since C++20
4040b57cec5SDimitry Andric
4050b57cec5SDimitry Andrictemplate<class charT, class traits, class Allocator>
406*81ad6265SDimitry Andricbool operator> (const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;  // constexpr since C++20
4070b57cec5SDimitry Andric
4080b57cec5SDimitry Andrictemplate<class charT, class traits, class Allocator>
4090b57cec5SDimitry Andricbool operator<=(const basic_string<charT, traits, Allocator>& lhs,
410*81ad6265SDimitry Andric                const basic_string<charT, traits, Allocator>& rhs) noexcept;                    // constexpr since C++20
4110b57cec5SDimitry Andric
4120b57cec5SDimitry Andrictemplate<class charT, class traits, class Allocator>
413*81ad6265SDimitry Andricbool operator<=(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept;  // constexpr since C++20
4140b57cec5SDimitry Andric
4150b57cec5SDimitry Andrictemplate<class charT, class traits, class Allocator>
416*81ad6265SDimitry Andricbool operator<=(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;  // constexpr since C++20
4170b57cec5SDimitry Andric
4180b57cec5SDimitry Andrictemplate<class charT, class traits, class Allocator>
4190b57cec5SDimitry Andricbool operator>=(const basic_string<charT, traits, Allocator>& lhs,
420*81ad6265SDimitry Andric                const basic_string<charT, traits, Allocator>& rhs) noexcept;                    // constexpr since C++20
4210b57cec5SDimitry Andric
4220b57cec5SDimitry Andrictemplate<class charT, class traits, class Allocator>
423*81ad6265SDimitry Andricbool operator>=(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept;  // constexpr since C++20
4240b57cec5SDimitry Andric
4250b57cec5SDimitry Andrictemplate<class charT, class traits, class Allocator>
426*81ad6265SDimitry Andricbool operator>=(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;  // constexpr since C++20
4270b57cec5SDimitry Andric
4280b57cec5SDimitry Andrictemplate<class charT, class traits, class Allocator>
4290b57cec5SDimitry Andricvoid swap(basic_string<charT, traits, Allocator>& lhs,
4300b57cec5SDimitry Andric          basic_string<charT, traits, Allocator>& rhs)
431*81ad6265SDimitry Andric            noexcept(noexcept(lhs.swap(rhs)));                                                  // constexpr since C++20
4320b57cec5SDimitry Andric
4330b57cec5SDimitry Andrictemplate<class charT, class traits, class Allocator>
4340b57cec5SDimitry Andricbasic_istream<charT, traits>&
4350b57cec5SDimitry Andricoperator>>(basic_istream<charT, traits>& is, basic_string<charT, traits, Allocator>& str);
4360b57cec5SDimitry Andric
4370b57cec5SDimitry Andrictemplate<class charT, class traits, class Allocator>
4380b57cec5SDimitry Andricbasic_ostream<charT, traits>&
4390b57cec5SDimitry Andricoperator<<(basic_ostream<charT, traits>& os, const basic_string<charT, traits, Allocator>& str);
4400b57cec5SDimitry Andric
4410b57cec5SDimitry Andrictemplate<class charT, class traits, class Allocator>
4420b57cec5SDimitry Andricbasic_istream<charT, traits>&
4430b57cec5SDimitry Andricgetline(basic_istream<charT, traits>& is, basic_string<charT, traits, Allocator>& str,
4440b57cec5SDimitry Andric        charT delim);
4450b57cec5SDimitry Andric
4460b57cec5SDimitry Andrictemplate<class charT, class traits, class Allocator>
4470b57cec5SDimitry Andricbasic_istream<charT, traits>&
4480b57cec5SDimitry Andricgetline(basic_istream<charT, traits>& is, basic_string<charT, traits, Allocator>& str);
4490b57cec5SDimitry Andric
4500b57cec5SDimitry Andrictemplate<class charT, class traits, class Allocator, class U>
4515ffd83dbSDimitry Andrictypename basic_string<charT, traits, Allocator>::size_type
4525ffd83dbSDimitry Andricerase(basic_string<charT, traits, Allocator>& c, const U& value);    // C++20
4530b57cec5SDimitry Andrictemplate<class charT, class traits, class Allocator, class Predicate>
4545ffd83dbSDimitry Andrictypename basic_string<charT, traits, Allocator>::size_type
4555ffd83dbSDimitry Andricerase_if(basic_string<charT, traits, Allocator>& c, Predicate pred); // C++20
4560b57cec5SDimitry Andric
4570b57cec5SDimitry Andrictypedef basic_string<char>    string;
4580b57cec5SDimitry Andrictypedef basic_string<wchar_t> wstring;
459fe6060f1SDimitry Andrictypedef basic_string<char8_t> u8string; // C++20
4600b57cec5SDimitry Andrictypedef basic_string<char16_t> u16string;
4610b57cec5SDimitry Andrictypedef basic_string<char32_t> u32string;
4620b57cec5SDimitry Andric
463e8d8bef9SDimitry Andricint                stoi  (const string& str, size_t* idx = nullptr, int base = 10);
464e8d8bef9SDimitry Andriclong               stol  (const string& str, size_t* idx = nullptr, int base = 10);
465e8d8bef9SDimitry Andricunsigned long      stoul (const string& str, size_t* idx = nullptr, int base = 10);
466e8d8bef9SDimitry Andriclong long          stoll (const string& str, size_t* idx = nullptr, int base = 10);
467e8d8bef9SDimitry Andricunsigned long long stoull(const string& str, size_t* idx = nullptr, int base = 10);
4680b57cec5SDimitry Andric
469e8d8bef9SDimitry Andricfloat       stof (const string& str, size_t* idx = nullptr);
470e8d8bef9SDimitry Andricdouble      stod (const string& str, size_t* idx = nullptr);
471e8d8bef9SDimitry Andriclong double stold(const string& str, size_t* idx = nullptr);
4720b57cec5SDimitry Andric
4730b57cec5SDimitry Andricstring to_string(int val);
4740b57cec5SDimitry Andricstring to_string(unsigned val);
4750b57cec5SDimitry Andricstring to_string(long val);
4760b57cec5SDimitry Andricstring to_string(unsigned long val);
4770b57cec5SDimitry Andricstring to_string(long long val);
4780b57cec5SDimitry Andricstring to_string(unsigned long long val);
4790b57cec5SDimitry Andricstring to_string(float val);
4800b57cec5SDimitry Andricstring to_string(double val);
4810b57cec5SDimitry Andricstring to_string(long double val);
4820b57cec5SDimitry Andric
483e8d8bef9SDimitry Andricint                stoi  (const wstring& str, size_t* idx = nullptr, int base = 10);
484e8d8bef9SDimitry Andriclong               stol  (const wstring& str, size_t* idx = nullptr, int base = 10);
485e8d8bef9SDimitry Andricunsigned long      stoul (const wstring& str, size_t* idx = nullptr, int base = 10);
486e8d8bef9SDimitry Andriclong long          stoll (const wstring& str, size_t* idx = nullptr, int base = 10);
487e8d8bef9SDimitry Andricunsigned long long stoull(const wstring& str, size_t* idx = nullptr, int base = 10);
4880b57cec5SDimitry Andric
489e8d8bef9SDimitry Andricfloat       stof (const wstring& str, size_t* idx = nullptr);
490e8d8bef9SDimitry Andricdouble      stod (const wstring& str, size_t* idx = nullptr);
491e8d8bef9SDimitry Andriclong double stold(const wstring& str, size_t* idx = nullptr);
4920b57cec5SDimitry Andric
4930b57cec5SDimitry Andricwstring to_wstring(int val);
4940b57cec5SDimitry Andricwstring to_wstring(unsigned val);
4950b57cec5SDimitry Andricwstring to_wstring(long val);
4960b57cec5SDimitry Andricwstring to_wstring(unsigned long val);
4970b57cec5SDimitry Andricwstring to_wstring(long long val);
4980b57cec5SDimitry Andricwstring to_wstring(unsigned long long val);
4990b57cec5SDimitry Andricwstring to_wstring(float val);
5000b57cec5SDimitry Andricwstring to_wstring(double val);
5010b57cec5SDimitry Andricwstring to_wstring(long double val);
5020b57cec5SDimitry Andric
5030b57cec5SDimitry Andrictemplate <> struct hash<string>;
504fe6060f1SDimitry Andrictemplate <> struct hash<u8string>; // C++20
5050b57cec5SDimitry Andrictemplate <> struct hash<u16string>;
5060b57cec5SDimitry Andrictemplate <> struct hash<u32string>;
5070b57cec5SDimitry Andrictemplate <> struct hash<wstring>;
5080b57cec5SDimitry Andric
509*81ad6265SDimitry Andricbasic_string<char>     operator "" s( const char *str,     size_t len );           // C++14, constexpr since C++20
510*81ad6265SDimitry Andricbasic_string<wchar_t>  operator "" s( const wchar_t *str,  size_t len );           // C++14, constexpr since C++20
511*81ad6265SDimitry Andricconstexpr basic_string<char8_t>  operator "" s( const char8_t *str,  size_t len ); // C++20
512*81ad6265SDimitry Andricbasic_string<char16_t> operator "" s( const char16_t *str, size_t len );           // C++14, constexpr since C++20
513*81ad6265SDimitry Andricbasic_string<char32_t> operator "" s( const char32_t *str, size_t len );           // C++14, constexpr since C++20
5140b57cec5SDimitry Andric
5150b57cec5SDimitry Andric}  // std
5160b57cec5SDimitry Andric
5170b57cec5SDimitry Andric*/
5180b57cec5SDimitry Andric
519*81ad6265SDimitry Andric#include <__algorithm/max.h>
520*81ad6265SDimitry Andric#include <__algorithm/min.h>
521*81ad6265SDimitry Andric#include <__algorithm/remove.h>
522*81ad6265SDimitry Andric#include <__algorithm/remove_if.h>
523*81ad6265SDimitry Andric#include <__assert> // all public C++ headers provide the assertion handler
5240b57cec5SDimitry Andric#include <__config>
525fe6060f1SDimitry Andric#include <__debug>
526*81ad6265SDimitry Andric#include <__format/enable_insertable.h>
527*81ad6265SDimitry Andric#include <__functional/hash.h>
528*81ad6265SDimitry Andric#include <__functional/unary_function.h>
529*81ad6265SDimitry Andric#include <__ios/fpos.h>
530*81ad6265SDimitry Andric#include <__iterator/distance.h>
531*81ad6265SDimitry Andric#include <__iterator/iterator_traits.h>
532*81ad6265SDimitry Andric#include <__iterator/reverse_iterator.h>
533fe6060f1SDimitry Andric#include <__iterator/wrap_iter.h>
534*81ad6265SDimitry Andric#include <__memory/allocate_at_least.h>
535*81ad6265SDimitry Andric#include <__string/char_traits.h>
536*81ad6265SDimitry Andric#include <__string/extern_template_lists.h>
537*81ad6265SDimitry Andric#include <__utility/auto_cast.h>
538*81ad6265SDimitry Andric#include <__utility/move.h>
539*81ad6265SDimitry Andric#include <__utility/swap.h>
540*81ad6265SDimitry Andric#include <__utility/unreachable.h>
541*81ad6265SDimitry Andric#include <climits>
542*81ad6265SDimitry Andric#include <cstdint>
543fe6060f1SDimitry Andric#include <cstdio>  // EOF
54469ade1e0SDimitry Andric#include <cstdlib>
545fe6060f1SDimitry Andric#include <cstring>
546fe6060f1SDimitry Andric#include <iosfwd>
547*81ad6265SDimitry Andric#include <limits>
5480b57cec5SDimitry Andric#include <memory>
5490b57cec5SDimitry Andric#include <stdexcept>
550fe6060f1SDimitry Andric#include <string_view>
5510b57cec5SDimitry Andric#include <type_traits>
5520b57cec5SDimitry Andric#include <version>
553fe6060f1SDimitry Andric
554349cc55cSDimitry Andric#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
555349cc55cSDimitry Andric#  include <cwchar>
556349cc55cSDimitry Andric#endif
557349cc55cSDimitry Andric
558*81ad6265SDimitry Andric#ifndef _LIBCPP_REMOVE_TRANSITIVE_INCLUDES
559*81ad6265SDimitry Andric#  include <algorithm>
560*81ad6265SDimitry Andric#  include <functional>
561*81ad6265SDimitry Andric#  include <iterator>
562*81ad6265SDimitry Andric#  include <new>
563*81ad6265SDimitry Andric#  include <typeinfo>
564*81ad6265SDimitry Andric#  include <utility>
565*81ad6265SDimitry Andric#  include <vector>
5660b57cec5SDimitry Andric#endif
5670b57cec5SDimitry Andric
568*81ad6265SDimitry Andric// standard-mandated includes
569*81ad6265SDimitry Andric
570*81ad6265SDimitry Andric// [iterator.range]
571*81ad6265SDimitry Andric#include <__iterator/access.h>
572*81ad6265SDimitry Andric#include <__iterator/data.h>
573*81ad6265SDimitry Andric#include <__iterator/empty.h>
574*81ad6265SDimitry Andric#include <__iterator/reverse_access.h>
575*81ad6265SDimitry Andric#include <__iterator/size.h>
576*81ad6265SDimitry Andric
577*81ad6265SDimitry Andric// [string.syn]
578*81ad6265SDimitry Andric#include <compare>
579*81ad6265SDimitry Andric#include <initializer_list>
580*81ad6265SDimitry Andric
5810b57cec5SDimitry Andric#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
5820b57cec5SDimitry Andric#  pragma GCC system_header
5830b57cec5SDimitry Andric#endif
5840b57cec5SDimitry Andric
5850b57cec5SDimitry Andric_LIBCPP_PUSH_MACROS
5860b57cec5SDimitry Andric#include <__undef_macros>
5870b57cec5SDimitry Andric
5880b57cec5SDimitry Andric
5890b57cec5SDimitry Andric_LIBCPP_BEGIN_NAMESPACE_STD
5900b57cec5SDimitry Andric
5910b57cec5SDimitry Andric// basic_string
5920b57cec5SDimitry Andric
5930b57cec5SDimitry Andrictemplate<class _CharT, class _Traits, class _Allocator>
5940b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>
595*81ad6265SDimitry Andric_LIBCPP_CONSTEXPR_AFTER_CXX17
5960b57cec5SDimitry Andricoperator+(const basic_string<_CharT, _Traits, _Allocator>& __x,
5970b57cec5SDimitry Andric          const basic_string<_CharT, _Traits, _Allocator>& __y);
5980b57cec5SDimitry Andric
5990b57cec5SDimitry Andrictemplate<class _CharT, class _Traits, class _Allocator>
600*81ad6265SDimitry Andric_LIBCPP_CONSTEXPR_AFTER_CXX17
6010b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>
6020b57cec5SDimitry Andricoperator+(const _CharT* __x, const basic_string<_CharT,_Traits,_Allocator>& __y);
6030b57cec5SDimitry Andric
6040b57cec5SDimitry Andrictemplate<class _CharT, class _Traits, class _Allocator>
605*81ad6265SDimitry Andric_LIBCPP_CONSTEXPR_AFTER_CXX17
6060b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>
6070b57cec5SDimitry Andricoperator+(_CharT __x, const basic_string<_CharT,_Traits,_Allocator>& __y);
6080b57cec5SDimitry Andric
6090b57cec5SDimitry Andrictemplate<class _CharT, class _Traits, class _Allocator>
610*81ad6265SDimitry Andricinline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
6110b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>
6120b57cec5SDimitry Andricoperator+(const basic_string<_CharT, _Traits, _Allocator>& __x, const _CharT* __y);
6130b57cec5SDimitry Andric
6140b57cec5SDimitry Andrictemplate<class _CharT, class _Traits, class _Allocator>
615*81ad6265SDimitry Andric_LIBCPP_CONSTEXPR_AFTER_CXX17
6160b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>
6170b57cec5SDimitry Andricoperator+(const basic_string<_CharT, _Traits, _Allocator>& __x, _CharT __y);
6180b57cec5SDimitry Andric
619*81ad6265SDimitry Andricextern template _LIBCPP_FUNC_VIS string operator+<char, char_traits<char>, allocator<char> >(char const*, string const&);
6200b57cec5SDimitry Andric
6210b57cec5SDimitry Andrictemplate <class _Iter>
622fe6060f1SDimitry Andricstruct __string_is_trivial_iterator : public false_type {};
623fe6060f1SDimitry Andric
624fe6060f1SDimitry Andrictemplate <class _Tp>
625fe6060f1SDimitry Andricstruct __string_is_trivial_iterator<_Tp*>
626fe6060f1SDimitry Andric    : public is_arithmetic<_Tp> {};
6270b57cec5SDimitry Andric
6280b57cec5SDimitry Andrictemplate <class _Iter>
629fe6060f1SDimitry Andricstruct __string_is_trivial_iterator<__wrap_iter<_Iter> >
630fe6060f1SDimitry Andric    : public __string_is_trivial_iterator<_Iter> {};
6310b57cec5SDimitry Andric
6320b57cec5SDimitry Andrictemplate <class _CharT, class _Traits, class _Tp>
6335ffd83dbSDimitry Andricstruct __can_be_converted_to_string_view : public _BoolConstant<
6345ffd83dbSDimitry Andric      is_convertible<const _Tp&, basic_string_view<_CharT, _Traits> >::value &&
6355ffd83dbSDimitry Andric     !is_convertible<const _Tp&, const _CharT*>::value
6365ffd83dbSDimitry Andric    > {};
6370b57cec5SDimitry Andric
638fe6060f1SDimitry Andric#ifndef _LIBCPP_HAS_NO_CHAR8_T
639e8d8bef9SDimitry Andrictypedef basic_string<char8_t> u8string;
640e8d8bef9SDimitry Andric#endif
641e8d8bef9SDimitry Andrictypedef basic_string<char16_t> u16string;
642e8d8bef9SDimitry Andrictypedef basic_string<char32_t> u32string;
643*81ad6265SDimitry Andric
644*81ad6265SDimitry Andricstruct __uninitialized_size_tag {};
645e8d8bef9SDimitry Andric
6460b57cec5SDimitry Andrictemplate<class _CharT, class _Traits, class _Allocator>
647e8d8bef9SDimitry Andricclass
648e8d8bef9SDimitry Andric    _LIBCPP_TEMPLATE_VIS
649fe6060f1SDimitry Andric#ifndef _LIBCPP_HAS_NO_CHAR8_T
650e8d8bef9SDimitry Andric    _LIBCPP_PREFERRED_NAME(u8string)
651e8d8bef9SDimitry Andric#endif
652e8d8bef9SDimitry Andric    _LIBCPP_PREFERRED_NAME(u16string)
653e8d8bef9SDimitry Andric    _LIBCPP_PREFERRED_NAME(u32string)
654e8d8bef9SDimitry Andric    basic_string
6550b57cec5SDimitry Andric{
6560b57cec5SDimitry Andricpublic:
6570b57cec5SDimitry Andric    typedef basic_string                                 __self;
6580b57cec5SDimitry Andric    typedef basic_string_view<_CharT, _Traits>           __self_view;
6590b57cec5SDimitry Andric    typedef _Traits                                      traits_type;
6600b57cec5SDimitry Andric    typedef _CharT                                       value_type;
6610b57cec5SDimitry Andric    typedef _Allocator                                   allocator_type;
6620b57cec5SDimitry Andric    typedef allocator_traits<allocator_type>             __alloc_traits;
6630b57cec5SDimitry Andric    typedef typename __alloc_traits::size_type           size_type;
6640b57cec5SDimitry Andric    typedef typename __alloc_traits::difference_type     difference_type;
6650b57cec5SDimitry Andric    typedef value_type&                                  reference;
6660b57cec5SDimitry Andric    typedef const value_type&                            const_reference;
6670b57cec5SDimitry Andric    typedef typename __alloc_traits::pointer             pointer;
6680b57cec5SDimitry Andric    typedef typename __alloc_traits::const_pointer       const_pointer;
6690b57cec5SDimitry Andric
6700b57cec5SDimitry Andric    static_assert((!is_array<value_type>::value), "Character type of basic_string must not be an array");
6710b57cec5SDimitry Andric    static_assert(( is_standard_layout<value_type>::value), "Character type of basic_string must be standard-layout");
6720b57cec5SDimitry Andric    static_assert(( is_trivial<value_type>::value), "Character type of basic_string must be trivial");
6730b57cec5SDimitry Andric    static_assert(( is_same<_CharT, typename traits_type::char_type>::value),
6740b57cec5SDimitry Andric                  "traits_type::char_type must be the same type as CharT");
6750b57cec5SDimitry Andric    static_assert(( is_same<typename allocator_type::value_type, value_type>::value),
6760b57cec5SDimitry Andric                  "Allocator::value_type must be same type as value_type");
6770b57cec5SDimitry Andric
6780b57cec5SDimitry Andric    typedef __wrap_iter<pointer>                         iterator;
6790b57cec5SDimitry Andric    typedef __wrap_iter<const_pointer>                   const_iterator;
680*81ad6265SDimitry Andric    typedef std::reverse_iterator<iterator>              reverse_iterator;
681*81ad6265SDimitry Andric    typedef std::reverse_iterator<const_iterator>        const_reverse_iterator;
6820b57cec5SDimitry Andric
6830b57cec5SDimitry Andricprivate:
684*81ad6265SDimitry Andric    static_assert(CHAR_BIT == 8, "This implementation assumes that one byte contains 8 bits");
6850b57cec5SDimitry Andric
6860b57cec5SDimitry Andric#ifdef _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
6870b57cec5SDimitry Andric
6880b57cec5SDimitry Andric    struct __long
6890b57cec5SDimitry Andric    {
6900b57cec5SDimitry Andric        pointer   __data_;
6910b57cec5SDimitry Andric        size_type __size_;
692*81ad6265SDimitry Andric        size_type __cap_ : sizeof(size_type) * CHAR_BIT - 1;
693*81ad6265SDimitry Andric        size_type __is_long_ : 1;
6940b57cec5SDimitry Andric    };
6950b57cec5SDimitry Andric
6960b57cec5SDimitry Andric    enum {__min_cap = (sizeof(__long) - 1)/sizeof(value_type) > 2 ?
6970b57cec5SDimitry Andric                      (sizeof(__long) - 1)/sizeof(value_type) : 2};
6980b57cec5SDimitry Andric
6990b57cec5SDimitry Andric    struct __short
7000b57cec5SDimitry Andric    {
7010b57cec5SDimitry Andric        value_type __data_[__min_cap];
702*81ad6265SDimitry Andric        unsigned char __padding_[sizeof(value_type) - 1];
703*81ad6265SDimitry Andric        unsigned char __size_ : 7;
704*81ad6265SDimitry Andric        unsigned char __is_long_ : 1;
7050b57cec5SDimitry Andric    };
7060b57cec5SDimitry Andric
707*81ad6265SDimitry Andric// The __endian_factor is required because the field we use to store the size
708*81ad6265SDimitry Andric// has one fewer bit than it would if it were not a bitfield.
709*81ad6265SDimitry Andric//
710*81ad6265SDimitry Andric// If the LSB is used to store the short-flag in the short string representation,
711*81ad6265SDimitry Andric// we have to multiply the size by two when it is stored and divide it by two when
712*81ad6265SDimitry Andric// it is loaded to make sure that we always store an even number. In the long string
713*81ad6265SDimitry Andric// representation, we can ignore this because we can assume that we always allocate
714*81ad6265SDimitry Andric// an even amount of value_types.
715*81ad6265SDimitry Andric//
716*81ad6265SDimitry Andric// If the MSB is used for the short-flag, the max_size() is numeric_limits<size_type>::max() / 2.
717*81ad6265SDimitry Andric// This does not impact the short string representation, since we never need the MSB
718*81ad6265SDimitry Andric// for representing the size of a short string anyway.
719*81ad6265SDimitry Andric
720*81ad6265SDimitry Andric#ifdef _LIBCPP_BIG_ENDIAN
721*81ad6265SDimitry Andric    static const size_type __endian_factor = 2;
7220b57cec5SDimitry Andric#else
723*81ad6265SDimitry Andric    static const size_type __endian_factor = 1;
724*81ad6265SDimitry Andric#endif
7250b57cec5SDimitry Andric
726*81ad6265SDimitry Andric#else // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
727*81ad6265SDimitry Andric
728*81ad6265SDimitry Andric#ifdef _LIBCPP_BIG_ENDIAN
729*81ad6265SDimitry Andric    static const size_type __endian_factor = 1;
730*81ad6265SDimitry Andric#else
731*81ad6265SDimitry Andric    static const size_type __endian_factor = 2;
732*81ad6265SDimitry Andric#endif
733*81ad6265SDimitry Andric
734*81ad6265SDimitry Andric    // Attribute 'packed' is used to keep the layout compatible with the
735*81ad6265SDimitry Andric    // previous definition that did not use bit fields. This is because on
736*81ad6265SDimitry Andric    // some platforms bit fields have a default size rather than the actual
737*81ad6265SDimitry Andric    // size used, e.g., it is 4 bytes on AIX. See D128285 for details.
7380b57cec5SDimitry Andric    struct __long
7390b57cec5SDimitry Andric    {
740*81ad6265SDimitry Andric        struct _LIBCPP_PACKED {
741*81ad6265SDimitry Andric            size_type __is_long_ : 1;
742*81ad6265SDimitry Andric            size_type __cap_ : sizeof(size_type) * CHAR_BIT - 1;
743*81ad6265SDimitry Andric        };
7440b57cec5SDimitry Andric        size_type __size_;
7450b57cec5SDimitry Andric        pointer   __data_;
7460b57cec5SDimitry Andric    };
7470b57cec5SDimitry Andric
7480b57cec5SDimitry Andric    enum {__min_cap = (sizeof(__long) - 1)/sizeof(value_type) > 2 ?
7490b57cec5SDimitry Andric                      (sizeof(__long) - 1)/sizeof(value_type) : 2};
7500b57cec5SDimitry Andric
7510b57cec5SDimitry Andric    struct __short
7520b57cec5SDimitry Andric    {
753*81ad6265SDimitry Andric        struct _LIBCPP_PACKED {
754*81ad6265SDimitry Andric            unsigned char __is_long_ : 1;
755*81ad6265SDimitry Andric            unsigned char __size_ : 7;
7560b57cec5SDimitry Andric        };
757*81ad6265SDimitry Andric        char __padding_[sizeof(value_type) - 1];
7580b57cec5SDimitry Andric        value_type __data_[__min_cap];
7590b57cec5SDimitry Andric    };
7600b57cec5SDimitry Andric
7610b57cec5SDimitry Andric#endif // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
7620b57cec5SDimitry Andric
763*81ad6265SDimitry Andric    static_assert(sizeof(__short) == (sizeof(value_type) * (__min_cap + 1)), "__short has an unexpected size.");
764*81ad6265SDimitry Andric
7650b57cec5SDimitry Andric    union __ulx{__long __lx; __short __lxx;};
7660b57cec5SDimitry Andric
7670b57cec5SDimitry Andric    enum {__n_words = sizeof(__ulx) / sizeof(size_type)};
7680b57cec5SDimitry Andric
7690b57cec5SDimitry Andric    struct __raw
7700b57cec5SDimitry Andric    {
7710b57cec5SDimitry Andric        size_type __words[__n_words];
7720b57cec5SDimitry Andric    };
7730b57cec5SDimitry Andric
7740b57cec5SDimitry Andric    struct __rep
7750b57cec5SDimitry Andric    {
7760b57cec5SDimitry Andric        union
7770b57cec5SDimitry Andric        {
7780b57cec5SDimitry Andric            __long  __l;
7790b57cec5SDimitry Andric            __short __s;
7800b57cec5SDimitry Andric            __raw   __r;
7810b57cec5SDimitry Andric        };
7820b57cec5SDimitry Andric    };
7830b57cec5SDimitry Andric
7840b57cec5SDimitry Andric    __compressed_pair<__rep, allocator_type> __r_;
7850b57cec5SDimitry Andric
786*81ad6265SDimitry Andric    // Construct a string with the given allocator and enough storage to hold `__size` characters, but
787*81ad6265SDimitry Andric    // don't initialize the characters. The contents of the string, including the null terminator, must be
788*81ad6265SDimitry Andric    // initialized separately.
789*81ad6265SDimitry Andric    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
790*81ad6265SDimitry Andric    explicit basic_string(__uninitialized_size_tag, size_type __size, const allocator_type& __a)
791*81ad6265SDimitry Andric            : __r_(__default_init_tag(), __a) {
792*81ad6265SDimitry Andric        if (__size > max_size())
793*81ad6265SDimitry Andric            __throw_length_error();
794*81ad6265SDimitry Andric        if (__fits_in_sso(__size)) {
795*81ad6265SDimitry Andric            __zero();
796*81ad6265SDimitry Andric            __set_short_size(__size);
797*81ad6265SDimitry Andric        } else {
798*81ad6265SDimitry Andric            auto __capacity = __recommend(__size) + 1;
799*81ad6265SDimitry Andric            auto __allocation = __alloc_traits::allocate(__alloc(), __capacity);
800*81ad6265SDimitry Andric            __begin_lifetime(__allocation, __capacity);
801*81ad6265SDimitry Andric            __set_long_cap(__capacity);
802*81ad6265SDimitry Andric            __set_long_pointer(__allocation);
803*81ad6265SDimitry Andric            __set_long_size(__size);
804*81ad6265SDimitry Andric        }
805*81ad6265SDimitry Andric        std::__debug_db_insert_c(this);
806*81ad6265SDimitry Andric    }
807*81ad6265SDimitry Andric
8080b57cec5SDimitry Andricpublic:
809fe6060f1SDimitry Andric    _LIBCPP_TEMPLATE_DATA_VIS
8100b57cec5SDimitry Andric    static const size_type npos = -1;
8110b57cec5SDimitry Andric
812*81ad6265SDimitry Andric    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 basic_string()
8130b57cec5SDimitry Andric        _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
8140b57cec5SDimitry Andric
815*81ad6265SDimitry Andric    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 explicit basic_string(const allocator_type& __a)
8160b57cec5SDimitry Andric#if _LIBCPP_STD_VER <= 14
8170b57cec5SDimitry Andric        _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value);
8180b57cec5SDimitry Andric#else
8190b57cec5SDimitry Andric        _NOEXCEPT;
8200b57cec5SDimitry Andric#endif
8210b57cec5SDimitry Andric
822*81ad6265SDimitry Andric    _LIBCPP_CONSTEXPR_AFTER_CXX17 basic_string(const basic_string& __str);
823*81ad6265SDimitry Andric    _LIBCPP_CONSTEXPR_AFTER_CXX17 basic_string(const basic_string& __str, const allocator_type& __a);
8240b57cec5SDimitry Andric
8250b57cec5SDimitry Andric#ifndef _LIBCPP_CXX03_LANG
826*81ad6265SDimitry Andric    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
8270b57cec5SDimitry Andric    basic_string(basic_string&& __str)
8280b57cec5SDimitry Andric#if _LIBCPP_STD_VER <= 14
8290b57cec5SDimitry Andric        _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
8300b57cec5SDimitry Andric#else
8310b57cec5SDimitry Andric        _NOEXCEPT;
8320b57cec5SDimitry Andric#endif
8330b57cec5SDimitry Andric
834*81ad6265SDimitry Andric    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
8350b57cec5SDimitry Andric    basic_string(basic_string&& __str, const allocator_type& __a);
8360b57cec5SDimitry Andric#endif // _LIBCPP_CXX03_LANG
8370b57cec5SDimitry Andric
838349cc55cSDimitry Andric    template <class = __enable_if_t<__is_allocator<_Allocator>::value, nullptr_t> >
839*81ad6265SDimitry Andric    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
840480093f4SDimitry Andric    basic_string(const _CharT* __s) : __r_(__default_init_tag(), __default_init_tag()) {
8410b57cec5SDimitry Andric      _LIBCPP_ASSERT(__s != nullptr, "basic_string(const char*) detected nullptr");
8420b57cec5SDimitry Andric      __init(__s, traits_type::length(__s));
843*81ad6265SDimitry Andric      std::__debug_db_insert_c(this);
8440b57cec5SDimitry Andric    }
8450b57cec5SDimitry Andric
846349cc55cSDimitry Andric    template <class = __enable_if_t<__is_allocator<_Allocator>::value, nullptr_t> >
847*81ad6265SDimitry Andric        _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
8480b57cec5SDimitry Andric        basic_string(const _CharT* __s, const _Allocator& __a);
8490b57cec5SDimitry Andric
850fe6060f1SDimitry Andric#if _LIBCPP_STD_VER > 20
851fe6060f1SDimitry Andric    basic_string(nullptr_t) = delete;
852fe6060f1SDimitry Andric#endif
853fe6060f1SDimitry Andric
854*81ad6265SDimitry Andric    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
8550b57cec5SDimitry Andric    basic_string(const _CharT* __s, size_type __n);
856*81ad6265SDimitry Andric    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
8570b57cec5SDimitry Andric    basic_string(const _CharT* __s, size_type __n, const _Allocator& __a);
858*81ad6265SDimitry Andric    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
8590b57cec5SDimitry Andric    basic_string(size_type __n, _CharT __c);
8600b57cec5SDimitry Andric
861349cc55cSDimitry Andric    template <class = __enable_if_t<__is_allocator<_Allocator>::value, nullptr_t> >
862*81ad6265SDimitry Andric        _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
8630b57cec5SDimitry Andric        basic_string(size_type __n, _CharT __c, const _Allocator& __a);
8640b57cec5SDimitry Andric
865*81ad6265SDimitry Andric    _LIBCPP_CONSTEXPR_AFTER_CXX17
8660b57cec5SDimitry Andric    basic_string(const basic_string& __str, size_type __pos, size_type __n,
8670b57cec5SDimitry Andric                 const _Allocator& __a = _Allocator());
868*81ad6265SDimitry Andric    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
8690b57cec5SDimitry Andric    basic_string(const basic_string& __str, size_type __pos,
8700b57cec5SDimitry Andric                 const _Allocator& __a = _Allocator());
8710b57cec5SDimitry Andric
872349cc55cSDimitry Andric    template<class _Tp, class = __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value> >
873*81ad6265SDimitry Andric        _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_AFTER_CXX17
8740b57cec5SDimitry Andric        basic_string(const _Tp& __t, size_type __pos, size_type __n,
8750b57cec5SDimitry Andric                     const allocator_type& __a = allocator_type());
8760b57cec5SDimitry Andric
877349cc55cSDimitry Andric    template<class _Tp, class = __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value &&
8785ffd83dbSDimitry Andric                                          !__is_same_uncvref<_Tp, basic_string>::value> >
879*81ad6265SDimitry Andric        _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_AFTER_CXX17
8800b57cec5SDimitry Andric        explicit basic_string(const _Tp& __t);
8810b57cec5SDimitry Andric
882349cc55cSDimitry Andric    template<class _Tp, class = __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value> >
883*81ad6265SDimitry Andric        _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_AFTER_CXX17
8840b57cec5SDimitry Andric        explicit basic_string(const _Tp& __t, const allocator_type& __a);
8850b57cec5SDimitry Andric
886349cc55cSDimitry Andric    template<class _InputIterator, class = __enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value> >
887*81ad6265SDimitry Andric        _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
8880b57cec5SDimitry Andric        basic_string(_InputIterator __first, _InputIterator __last);
889349cc55cSDimitry Andric    template<class _InputIterator, class = __enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value> >
890*81ad6265SDimitry Andric        _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
8910b57cec5SDimitry Andric        basic_string(_InputIterator __first, _InputIterator __last, const allocator_type& __a);
8920b57cec5SDimitry Andric#ifndef _LIBCPP_CXX03_LANG
893*81ad6265SDimitry Andric    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
8940b57cec5SDimitry Andric    basic_string(initializer_list<_CharT> __il);
895*81ad6265SDimitry Andric    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
8960b57cec5SDimitry Andric    basic_string(initializer_list<_CharT> __il, const _Allocator& __a);
8970b57cec5SDimitry Andric#endif // _LIBCPP_CXX03_LANG
8980b57cec5SDimitry Andric
899*81ad6265SDimitry Andric    inline _LIBCPP_CONSTEXPR_AFTER_CXX17 ~basic_string();
9000b57cec5SDimitry Andric
901*81ad6265SDimitry Andric    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
9020b57cec5SDimitry Andric    operator __self_view() const _NOEXCEPT { return __self_view(data(), size()); }
9030b57cec5SDimitry Andric
904*81ad6265SDimitry Andric    _LIBCPP_CONSTEXPR_AFTER_CXX17 basic_string& operator=(const basic_string& __str);
9050b57cec5SDimitry Andric
906*81ad6265SDimitry Andric    template <class _Tp, class = __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value &&
907*81ad6265SDimitry Andric                                               !__is_same_uncvref<_Tp, basic_string>::value> >
908*81ad6265SDimitry Andric    _LIBCPP_CONSTEXPR_AFTER_CXX17 basic_string& operator=(const _Tp& __t) {
909*81ad6265SDimitry Andric      __self_view __sv = __t;
910*81ad6265SDimitry Andric      return assign(__sv);
911*81ad6265SDimitry Andric    }
9120b57cec5SDimitry Andric
9130b57cec5SDimitry Andric#ifndef _LIBCPP_CXX03_LANG
914*81ad6265SDimitry Andric    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
9150b57cec5SDimitry Andric    basic_string& operator=(basic_string&& __str)
9160b57cec5SDimitry Andric        _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value));
917*81ad6265SDimitry Andric     _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
9180b57cec5SDimitry Andric    basic_string& operator=(initializer_list<value_type> __il) {return assign(__il.begin(), __il.size());}
9190b57cec5SDimitry Andric#endif
920*81ad6265SDimitry Andric    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
921*81ad6265SDimitry Andric    basic_string& operator=(const value_type* __s) {return assign(__s);}
922fe6060f1SDimitry Andric#if _LIBCPP_STD_VER > 20
923fe6060f1SDimitry Andric    basic_string& operator=(nullptr_t) = delete;
924fe6060f1SDimitry Andric#endif
925*81ad6265SDimitry Andric    _LIBCPP_CONSTEXPR_AFTER_CXX17 basic_string& operator=(value_type __c);
9260b57cec5SDimitry Andric
927*81ad6265SDimitry Andric    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
9280b57cec5SDimitry Andric    iterator begin() _NOEXCEPT
9290b57cec5SDimitry Andric        {return iterator(this, __get_pointer());}
930*81ad6265SDimitry Andric    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
9310b57cec5SDimitry Andric    const_iterator begin() const _NOEXCEPT
9320b57cec5SDimitry Andric        {return const_iterator(this, __get_pointer());}
933*81ad6265SDimitry Andric    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
9340b57cec5SDimitry Andric    iterator end() _NOEXCEPT
9350b57cec5SDimitry Andric        {return iterator(this, __get_pointer() + size());}
936*81ad6265SDimitry Andric    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
9370b57cec5SDimitry Andric    const_iterator end() const _NOEXCEPT
9380b57cec5SDimitry Andric        {return const_iterator(this, __get_pointer() + size());}
939*81ad6265SDimitry Andric
940*81ad6265SDimitry Andric    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
9410b57cec5SDimitry Andric    reverse_iterator rbegin() _NOEXCEPT
9420b57cec5SDimitry Andric        {return reverse_iterator(end());}
943*81ad6265SDimitry Andric    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
9440b57cec5SDimitry Andric    const_reverse_iterator rbegin() const _NOEXCEPT
9450b57cec5SDimitry Andric        {return const_reverse_iterator(end());}
946*81ad6265SDimitry Andric    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
9470b57cec5SDimitry Andric    reverse_iterator rend() _NOEXCEPT
9480b57cec5SDimitry Andric        {return reverse_iterator(begin());}
949*81ad6265SDimitry Andric    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
9500b57cec5SDimitry Andric    const_reverse_iterator rend() const _NOEXCEPT
9510b57cec5SDimitry Andric        {return const_reverse_iterator(begin());}
9520b57cec5SDimitry Andric
953*81ad6265SDimitry Andric    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
9540b57cec5SDimitry Andric    const_iterator cbegin() const _NOEXCEPT
9550b57cec5SDimitry Andric        {return begin();}
956*81ad6265SDimitry Andric    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
9570b57cec5SDimitry Andric    const_iterator cend() const _NOEXCEPT
9580b57cec5SDimitry Andric        {return end();}
959*81ad6265SDimitry Andric    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
9600b57cec5SDimitry Andric    const_reverse_iterator crbegin() const _NOEXCEPT
9610b57cec5SDimitry Andric        {return rbegin();}
962*81ad6265SDimitry Andric    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
9630b57cec5SDimitry Andric    const_reverse_iterator crend() const _NOEXCEPT
9640b57cec5SDimitry Andric        {return rend();}
9650b57cec5SDimitry Andric
966*81ad6265SDimitry Andric    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 size_type size() const _NOEXCEPT
9670b57cec5SDimitry Andric        {return __is_long() ? __get_long_size() : __get_short_size();}
968*81ad6265SDimitry Andric    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 size_type length() const _NOEXCEPT {return size();}
969*81ad6265SDimitry Andric    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 size_type max_size() const _NOEXCEPT;
970*81ad6265SDimitry Andric    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 size_type capacity() const _NOEXCEPT {
971*81ad6265SDimitry Andric        return (__is_long() ? __get_long_cap() : static_cast<size_type>(__min_cap)) - 1;
972*81ad6265SDimitry Andric    }
9730b57cec5SDimitry Andric
974*81ad6265SDimitry Andric    _LIBCPP_CONSTEXPR_AFTER_CXX17 void resize(size_type __n, value_type __c);
975*81ad6265SDimitry Andric    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 void resize(size_type __n) { resize(__n, value_type()); }
9760b57cec5SDimitry Andric
977*81ad6265SDimitry Andric    _LIBCPP_CONSTEXPR_AFTER_CXX17 void reserve(size_type __requested_capacity);
97804eeddc0SDimitry Andric
97904eeddc0SDimitry Andric#if _LIBCPP_STD_VER > 20
98004eeddc0SDimitry Andric    template <class _Op>
98104eeddc0SDimitry Andric    _LIBCPP_HIDE_FROM_ABI constexpr
98204eeddc0SDimitry Andric    void resize_and_overwrite(size_type __n, _Op __op) {
98304eeddc0SDimitry Andric      __resize_default_init(__n);
984*81ad6265SDimitry Andric      __erase_to_end(std::move(__op)(data(), _LIBCPP_AUTO_CAST(__n)));
98504eeddc0SDimitry Andric    }
98604eeddc0SDimitry Andric#endif
98704eeddc0SDimitry Andric
988*81ad6265SDimitry Andric    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 void __resize_default_init(size_type __n);
9890b57cec5SDimitry Andric
990*81ad6265SDimitry Andric    _LIBCPP_DEPRECATED_IN_CXX20 _LIBCPP_HIDE_FROM_ABI void reserve() _NOEXCEPT { shrink_to_fit(); }
991*81ad6265SDimitry Andric    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 void shrink_to_fit() _NOEXCEPT;
992*81ad6265SDimitry Andric    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 void clear() _NOEXCEPT;
993*81ad6265SDimitry Andric
994*81ad6265SDimitry Andric    _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
9950b57cec5SDimitry Andric    bool empty() const _NOEXCEPT {return size() == 0;}
9960b57cec5SDimitry Andric
997*81ad6265SDimitry Andric    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
998*81ad6265SDimitry Andric    const_reference operator[](size_type __pos) const _NOEXCEPT;
999*81ad6265SDimitry Andric    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 reference operator[](size_type __pos) _NOEXCEPT;
10000b57cec5SDimitry Andric
1001*81ad6265SDimitry Andric    _LIBCPP_CONSTEXPR_AFTER_CXX17 const_reference at(size_type __n) const;
1002*81ad6265SDimitry Andric    _LIBCPP_CONSTEXPR_AFTER_CXX17 reference       at(size_type __n);
10030b57cec5SDimitry Andric
1004*81ad6265SDimitry Andric    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 basic_string& operator+=(const basic_string& __str) {
1005*81ad6265SDimitry Andric        return append(__str);
1006*81ad6265SDimitry Andric    }
10070b57cec5SDimitry Andric
10080b57cec5SDimitry Andric    template <class _Tp>
1009*81ad6265SDimitry Andric    _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_AFTER_CXX17
1010349cc55cSDimitry Andric    __enable_if_t
10110b57cec5SDimitry Andric        <
10125ffd83dbSDimitry Andric            __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
10135ffd83dbSDimitry Andric            && !__is_same_uncvref<_Tp, basic_string >::value,
10140b57cec5SDimitry Andric            basic_string&
10155ffd83dbSDimitry Andric        >
1016*81ad6265SDimitry Andric    operator+=(const _Tp& __t) {
1017*81ad6265SDimitry Andric        __self_view __sv = __t; return append(__sv);
1018*81ad6265SDimitry Andric    }
1019*81ad6265SDimitry Andric
1020*81ad6265SDimitry Andric    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 basic_string& operator+=(const value_type* __s) {
1021*81ad6265SDimitry Andric        return append(__s);
1022*81ad6265SDimitry Andric    }
1023*81ad6265SDimitry Andric
1024*81ad6265SDimitry Andric    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 basic_string& operator+=(value_type __c) {
1025*81ad6265SDimitry Andric        push_back(__c);
1026*81ad6265SDimitry Andric        return *this;
1027*81ad6265SDimitry Andric    }
1028*81ad6265SDimitry Andric
10290b57cec5SDimitry Andric#ifndef _LIBCPP_CXX03_LANG
1030*81ad6265SDimitry Andric    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
1031*81ad6265SDimitry Andric    basic_string& operator+=(initializer_list<value_type> __il) { return append(__il); }
10320b57cec5SDimitry Andric#endif // _LIBCPP_CXX03_LANG
10330b57cec5SDimitry Andric
1034*81ad6265SDimitry Andric    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
10350b57cec5SDimitry Andric    basic_string& append(const basic_string& __str);
10360b57cec5SDimitry Andric
10370b57cec5SDimitry Andric    template <class _Tp>
1038*81ad6265SDimitry Andric    _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_AFTER_CXX17
1039349cc55cSDimitry Andric    __enable_if_t<
10405ffd83dbSDimitry Andric            __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
10415ffd83dbSDimitry Andric            && !__is_same_uncvref<_Tp, basic_string>::value,
10420b57cec5SDimitry Andric            basic_string&
10435ffd83dbSDimitry Andric        >
10440b57cec5SDimitry Andric                  append(const _Tp& __t) { __self_view __sv = __t; return append(__sv.data(), __sv.size()); }
1045*81ad6265SDimitry Andric    _LIBCPP_CONSTEXPR_AFTER_CXX17 basic_string& append(const basic_string& __str, size_type __pos, size_type __n=npos);
10460b57cec5SDimitry Andric
10470b57cec5SDimitry Andric    template <class _Tp>
1048*81ad6265SDimitry Andric    _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_AFTER_CXX17
1049349cc55cSDimitry Andric    __enable_if_t
10500b57cec5SDimitry Andric        <
10515ffd83dbSDimitry Andric            __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
10525ffd83dbSDimitry Andric            && !__is_same_uncvref<_Tp, basic_string>::value,
10530b57cec5SDimitry Andric            basic_string&
10545ffd83dbSDimitry Andric        >
10550b57cec5SDimitry Andric                  append(const _Tp& __t, size_type __pos, size_type __n=npos);
1056*81ad6265SDimitry Andric    _LIBCPP_CONSTEXPR_AFTER_CXX17 basic_string& append(const value_type* __s, size_type __n);
1057*81ad6265SDimitry Andric    _LIBCPP_CONSTEXPR_AFTER_CXX17 basic_string& append(const value_type* __s);
1058*81ad6265SDimitry Andric    _LIBCPP_CONSTEXPR_AFTER_CXX17 basic_string& append(size_type __n, value_type __c);
10590b57cec5SDimitry Andric
1060*81ad6265SDimitry Andric    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
10610b57cec5SDimitry Andric    void __append_default_init(size_type __n);
10620b57cec5SDimitry Andric
10630b57cec5SDimitry Andric    template<class _InputIterator>
10640b57cec5SDimitry Andric    _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
1065349cc55cSDimitry Andric    __enable_if_t
10660b57cec5SDimitry Andric        <
1067fe6060f1SDimitry Andric            __is_exactly_cpp17_input_iterator<_InputIterator>::value,
10680b57cec5SDimitry Andric            basic_string&
10695ffd83dbSDimitry Andric        >
1070*81ad6265SDimitry Andric    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
10710b57cec5SDimitry Andric    append(_InputIterator __first, _InputIterator __last) {
10720b57cec5SDimitry Andric      const basic_string __temp(__first, __last, __alloc());
10730b57cec5SDimitry Andric      append(__temp.data(), __temp.size());
10740b57cec5SDimitry Andric      return *this;
10750b57cec5SDimitry Andric    }
10760b57cec5SDimitry Andric    template<class _ForwardIterator>
10770b57cec5SDimitry Andric    _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
1078349cc55cSDimitry Andric    __enable_if_t
10790b57cec5SDimitry Andric        <
1080fe6060f1SDimitry Andric            __is_cpp17_forward_iterator<_ForwardIterator>::value,
10810b57cec5SDimitry Andric            basic_string&
10825ffd83dbSDimitry Andric        >
1083*81ad6265SDimitry Andric    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
1084fe6060f1SDimitry Andric    append(_ForwardIterator __first, _ForwardIterator __last);
10850b57cec5SDimitry Andric
10860b57cec5SDimitry Andric#ifndef _LIBCPP_CXX03_LANG
1087*81ad6265SDimitry Andric    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
10880b57cec5SDimitry Andric    basic_string& append(initializer_list<value_type> __il) {return append(__il.begin(), __il.size());}
10890b57cec5SDimitry Andric#endif // _LIBCPP_CXX03_LANG
10900b57cec5SDimitry Andric
1091*81ad6265SDimitry Andric    _LIBCPP_CONSTEXPR_AFTER_CXX17 void push_back(value_type __c);
1092*81ad6265SDimitry Andric    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 void pop_back();
1093*81ad6265SDimitry Andric    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 reference       front() _NOEXCEPT;
1094*81ad6265SDimitry Andric    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 const_reference front() const _NOEXCEPT;
1095*81ad6265SDimitry Andric    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 reference       back() _NOEXCEPT;
1096*81ad6265SDimitry Andric    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 const_reference back() const _NOEXCEPT;
10970b57cec5SDimitry Andric
10980b57cec5SDimitry Andric    template <class _Tp>
1099*81ad6265SDimitry Andric    _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_AFTER_CXX17
1100349cc55cSDimitry Andric    __enable_if_t
11010b57cec5SDimitry Andric        <
11020b57cec5SDimitry Andric            __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
11030b57cec5SDimitry Andric            basic_string&
11045ffd83dbSDimitry Andric        >
11050b57cec5SDimitry Andric                 assign(const _Tp & __t) { __self_view __sv = __t; return assign(__sv.data(), __sv.size()); }
1106*81ad6265SDimitry Andric    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
11070b57cec5SDimitry Andric    basic_string& assign(const basic_string& __str) { return *this = __str; }
11080b57cec5SDimitry Andric#ifndef _LIBCPP_CXX03_LANG
1109*81ad6265SDimitry Andric    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
11100b57cec5SDimitry Andric    basic_string& assign(basic_string&& __str)
11110b57cec5SDimitry Andric        _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
1112*81ad6265SDimitry Andric        {*this = std::move(__str); return *this;}
11130b57cec5SDimitry Andric#endif
1114*81ad6265SDimitry Andric    _LIBCPP_CONSTEXPR_AFTER_CXX17 basic_string& assign(const basic_string& __str, size_type __pos, size_type __n=npos);
11150b57cec5SDimitry Andric    template <class _Tp>
1116*81ad6265SDimitry Andric    _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_AFTER_CXX17
1117349cc55cSDimitry Andric    __enable_if_t
11180b57cec5SDimitry Andric        <
11195ffd83dbSDimitry Andric            __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
11205ffd83dbSDimitry Andric            && !__is_same_uncvref<_Tp, basic_string>::value,
11210b57cec5SDimitry Andric            basic_string&
11225ffd83dbSDimitry Andric        >
11230b57cec5SDimitry Andric                  assign(const _Tp & __t, size_type __pos, size_type __n=npos);
1124*81ad6265SDimitry Andric    _LIBCPP_CONSTEXPR_AFTER_CXX17 basic_string& assign(const value_type* __s, size_type __n);
1125*81ad6265SDimitry Andric    _LIBCPP_CONSTEXPR_AFTER_CXX17 basic_string& assign(const value_type* __s);
1126*81ad6265SDimitry Andric    _LIBCPP_CONSTEXPR_AFTER_CXX17 basic_string& assign(size_type __n, value_type __c);
11270b57cec5SDimitry Andric    template<class _InputIterator>
1128*81ad6265SDimitry Andric    _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_AFTER_CXX17
1129349cc55cSDimitry Andric    __enable_if_t
11300b57cec5SDimitry Andric        <
1131fe6060f1SDimitry Andric            __is_exactly_cpp17_input_iterator<_InputIterator>::value,
11320b57cec5SDimitry Andric            basic_string&
11335ffd83dbSDimitry Andric        >
11340b57cec5SDimitry Andric        assign(_InputIterator __first, _InputIterator __last);
11350b57cec5SDimitry Andric    template<class _ForwardIterator>
1136*81ad6265SDimitry Andric    _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_AFTER_CXX17
1137349cc55cSDimitry Andric    __enable_if_t
11380b57cec5SDimitry Andric        <
1139fe6060f1SDimitry Andric            __is_cpp17_forward_iterator<_ForwardIterator>::value,
11400b57cec5SDimitry Andric            basic_string&
11415ffd83dbSDimitry Andric        >
11420b57cec5SDimitry Andric        assign(_ForwardIterator __first, _ForwardIterator __last);
11430b57cec5SDimitry Andric#ifndef _LIBCPP_CXX03_LANG
1144*81ad6265SDimitry Andric    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
11450b57cec5SDimitry Andric    basic_string& assign(initializer_list<value_type> __il) {return assign(__il.begin(), __il.size());}
11460b57cec5SDimitry Andric#endif // _LIBCPP_CXX03_LANG
11470b57cec5SDimitry Andric
1148*81ad6265SDimitry Andric    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
11490b57cec5SDimitry Andric    basic_string& insert(size_type __pos1, const basic_string& __str);
11500b57cec5SDimitry Andric
11510b57cec5SDimitry Andric    template <class _Tp>
1152*81ad6265SDimitry Andric    _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_AFTER_CXX17
1153349cc55cSDimitry Andric    __enable_if_t
11540b57cec5SDimitry Andric        <
11550b57cec5SDimitry Andric            __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
11560b57cec5SDimitry Andric            basic_string&
11575ffd83dbSDimitry Andric        >
11580b57cec5SDimitry Andric                 insert(size_type __pos1, const _Tp& __t)
11590b57cec5SDimitry Andric    { __self_view __sv = __t; return insert(__pos1, __sv.data(), __sv.size()); }
11600b57cec5SDimitry Andric
11610b57cec5SDimitry Andric    template <class _Tp>
1162*81ad6265SDimitry Andric    _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_AFTER_CXX17
1163349cc55cSDimitry Andric    __enable_if_t
11640b57cec5SDimitry Andric        <
11655ffd83dbSDimitry Andric            __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value,
11660b57cec5SDimitry Andric            basic_string&
11675ffd83dbSDimitry Andric        >
11680b57cec5SDimitry Andric                  insert(size_type __pos1, const _Tp& __t, size_type __pos2, size_type __n=npos);
1169*81ad6265SDimitry Andric    _LIBCPP_CONSTEXPR_AFTER_CXX17
11700b57cec5SDimitry Andric    basic_string& insert(size_type __pos1, const basic_string& __str, size_type __pos2, size_type __n=npos);
1171*81ad6265SDimitry Andric    _LIBCPP_CONSTEXPR_AFTER_CXX17 basic_string& insert(size_type __pos, const value_type* __s, size_type __n);
1172*81ad6265SDimitry Andric    _LIBCPP_CONSTEXPR_AFTER_CXX17 basic_string& insert(size_type __pos, const value_type* __s);
1173*81ad6265SDimitry Andric    _LIBCPP_CONSTEXPR_AFTER_CXX17 basic_string& insert(size_type __pos, size_type __n, value_type __c);
1174*81ad6265SDimitry Andric    _LIBCPP_CONSTEXPR_AFTER_CXX17 iterator      insert(const_iterator __pos, value_type __c);
1175*81ad6265SDimitry Andric    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
11760b57cec5SDimitry Andric    iterator      insert(const_iterator __pos, size_type __n, value_type __c);
11770b57cec5SDimitry Andric    template<class _InputIterator>
1178*81ad6265SDimitry Andric    _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_AFTER_CXX17
1179349cc55cSDimitry Andric    __enable_if_t
11800b57cec5SDimitry Andric        <
1181fe6060f1SDimitry Andric            __is_exactly_cpp17_input_iterator<_InputIterator>::value,
11820b57cec5SDimitry Andric            iterator
11835ffd83dbSDimitry Andric        >
11840b57cec5SDimitry Andric        insert(const_iterator __pos, _InputIterator __first, _InputIterator __last);
11850b57cec5SDimitry Andric    template<class _ForwardIterator>
1186*81ad6265SDimitry Andric    _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_AFTER_CXX17
1187349cc55cSDimitry Andric    __enable_if_t
11880b57cec5SDimitry Andric        <
1189fe6060f1SDimitry Andric            __is_cpp17_forward_iterator<_ForwardIterator>::value,
11900b57cec5SDimitry Andric            iterator
11915ffd83dbSDimitry Andric        >
11920b57cec5SDimitry Andric        insert(const_iterator __pos, _ForwardIterator __first, _ForwardIterator __last);
11930b57cec5SDimitry Andric#ifndef _LIBCPP_CXX03_LANG
1194*81ad6265SDimitry Andric    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
11950b57cec5SDimitry Andric    iterator insert(const_iterator __pos, initializer_list<value_type> __il)
11960b57cec5SDimitry Andric                    {return insert(__pos, __il.begin(), __il.end());}
11970b57cec5SDimitry Andric#endif // _LIBCPP_CXX03_LANG
11980b57cec5SDimitry Andric
1199*81ad6265SDimitry Andric    _LIBCPP_CONSTEXPR_AFTER_CXX17 basic_string& erase(size_type __pos = 0, size_type __n = npos);
1200*81ad6265SDimitry Andric    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
12010b57cec5SDimitry Andric    iterator      erase(const_iterator __pos);
1202*81ad6265SDimitry Andric    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
12030b57cec5SDimitry Andric    iterator      erase(const_iterator __first, const_iterator __last);
12040b57cec5SDimitry Andric
1205*81ad6265SDimitry Andric    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
12060b57cec5SDimitry Andric    basic_string& replace(size_type __pos1, size_type __n1, const basic_string& __str);
12070b57cec5SDimitry Andric
12080b57cec5SDimitry Andric    template <class _Tp>
1209*81ad6265SDimitry Andric    _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_AFTER_CXX17
1210349cc55cSDimitry Andric    __enable_if_t
12110b57cec5SDimitry Andric        <
12120b57cec5SDimitry Andric            __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
12130b57cec5SDimitry Andric            basic_string&
12145ffd83dbSDimitry Andric        >
12150b57cec5SDimitry Andric                  replace(size_type __pos1, size_type __n1, const _Tp& __t) { __self_view __sv = __t; return replace(__pos1, __n1, __sv.data(), __sv.size()); }
1216*81ad6265SDimitry Andric    _LIBCPP_CONSTEXPR_AFTER_CXX17
12170b57cec5SDimitry Andric    basic_string& replace(size_type __pos1, size_type __n1, const basic_string& __str, size_type __pos2, size_type __n2=npos);
12180b57cec5SDimitry Andric    template <class _Tp>
1219*81ad6265SDimitry Andric    _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_AFTER_CXX17
1220349cc55cSDimitry Andric    __enable_if_t
12210b57cec5SDimitry Andric        <
12225ffd83dbSDimitry Andric            __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value  && !__is_same_uncvref<_Tp, basic_string>::value,
12230b57cec5SDimitry Andric            basic_string&
12245ffd83dbSDimitry Andric        >
12250b57cec5SDimitry Andric                  replace(size_type __pos1, size_type __n1, const _Tp& __t, size_type __pos2, size_type __n2=npos);
1226*81ad6265SDimitry Andric    _LIBCPP_CONSTEXPR_AFTER_CXX17
12270b57cec5SDimitry Andric    basic_string& replace(size_type __pos, size_type __n1, const value_type* __s, size_type __n2);
1228*81ad6265SDimitry Andric    _LIBCPP_CONSTEXPR_AFTER_CXX17 basic_string& replace(size_type __pos, size_type __n1, const value_type* __s);
1229*81ad6265SDimitry Andric    _LIBCPP_CONSTEXPR_AFTER_CXX17 basic_string& replace(size_type __pos, size_type __n1, size_type __n2, value_type __c);
1230*81ad6265SDimitry Andric    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
12310b57cec5SDimitry Andric    basic_string& replace(const_iterator __i1, const_iterator __i2, const basic_string& __str);
12320b57cec5SDimitry Andric
12330b57cec5SDimitry Andric    template <class _Tp>
1234*81ad6265SDimitry Andric    _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_AFTER_CXX17
1235349cc55cSDimitry Andric    __enable_if_t
12360b57cec5SDimitry Andric        <
12370b57cec5SDimitry Andric            __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
12380b57cec5SDimitry Andric            basic_string&
12395ffd83dbSDimitry Andric        >
12400b57cec5SDimitry Andric                  replace(const_iterator __i1, const_iterator __i2, const _Tp& __t) { __self_view __sv = __t; return replace(__i1 - begin(), __i2 - __i1, __sv); }
12410b57cec5SDimitry Andric
1242*81ad6265SDimitry Andric    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
12430b57cec5SDimitry Andric    basic_string& replace(const_iterator __i1, const_iterator __i2, const value_type* __s, size_type __n);
1244*81ad6265SDimitry Andric    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
12450b57cec5SDimitry Andric    basic_string& replace(const_iterator __i1, const_iterator __i2, const value_type* __s);
1246*81ad6265SDimitry Andric    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
12470b57cec5SDimitry Andric    basic_string& replace(const_iterator __i1, const_iterator __i2, size_type __n, value_type __c);
12480b57cec5SDimitry Andric    template<class _InputIterator>
1249*81ad6265SDimitry Andric    _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_AFTER_CXX17
1250349cc55cSDimitry Andric    __enable_if_t
12510b57cec5SDimitry Andric        <
1252480093f4SDimitry Andric            __is_cpp17_input_iterator<_InputIterator>::value,
12530b57cec5SDimitry Andric            basic_string&
12545ffd83dbSDimitry Andric        >
12550b57cec5SDimitry Andric        replace(const_iterator __i1, const_iterator __i2, _InputIterator __j1, _InputIterator __j2);
12560b57cec5SDimitry Andric#ifndef _LIBCPP_CXX03_LANG
1257*81ad6265SDimitry Andric    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
12580b57cec5SDimitry Andric    basic_string& replace(const_iterator __i1, const_iterator __i2, initializer_list<value_type> __il)
12590b57cec5SDimitry Andric        {return replace(__i1, __i2, __il.begin(), __il.end());}
12600b57cec5SDimitry Andric#endif // _LIBCPP_CXX03_LANG
12610b57cec5SDimitry Andric
1262*81ad6265SDimitry Andric    _LIBCPP_CONSTEXPR_AFTER_CXX17 size_type copy(value_type* __s, size_type __n, size_type __pos = 0) const;
1263*81ad6265SDimitry Andric    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
12640b57cec5SDimitry Andric    basic_string substr(size_type __pos = 0, size_type __n = npos) const;
12650b57cec5SDimitry Andric
1266*81ad6265SDimitry Andric    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
12670b57cec5SDimitry Andric    void swap(basic_string& __str)
12680b57cec5SDimitry Andric#if _LIBCPP_STD_VER >= 14
12690b57cec5SDimitry Andric        _NOEXCEPT;
12700b57cec5SDimitry Andric#else
12710b57cec5SDimitry Andric        _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
12720b57cec5SDimitry Andric                    __is_nothrow_swappable<allocator_type>::value);
12730b57cec5SDimitry Andric#endif
12740b57cec5SDimitry Andric
1275*81ad6265SDimitry Andric    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
12760b57cec5SDimitry Andric    const value_type* c_str() const _NOEXCEPT {return data();}
1277*81ad6265SDimitry Andric    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
1278*81ad6265SDimitry Andric    const value_type* data() const _NOEXCEPT  {return std::__to_address(__get_pointer());}
12790b57cec5SDimitry Andric#if _LIBCPP_STD_VER > 14 || defined(_LIBCPP_BUILDING_LIBRARY)
1280*81ad6265SDimitry Andric    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
1281*81ad6265SDimitry Andric    value_type* data()             _NOEXCEPT  {return std::__to_address(__get_pointer());}
12820b57cec5SDimitry Andric#endif
12830b57cec5SDimitry Andric
1284*81ad6265SDimitry Andric    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
12850b57cec5SDimitry Andric    allocator_type get_allocator() const _NOEXCEPT {return __alloc();}
12860b57cec5SDimitry Andric
1287*81ad6265SDimitry Andric    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
12880b57cec5SDimitry Andric    size_type find(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
12890b57cec5SDimitry Andric
12900b57cec5SDimitry Andric    template <class _Tp>
1291*81ad6265SDimitry Andric    _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_AFTER_CXX17
1292349cc55cSDimitry Andric    __enable_if_t
12930b57cec5SDimitry Andric        <
12940b57cec5SDimitry Andric            __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
12950b57cec5SDimitry Andric            size_type
12965ffd83dbSDimitry Andric        >
1297fe6060f1SDimitry Andric              find(const _Tp& __t, size_type __pos = 0) const _NOEXCEPT;
1298*81ad6265SDimitry Andric    _LIBCPP_CONSTEXPR_AFTER_CXX17
12990b57cec5SDimitry Andric    size_type find(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
1300*81ad6265SDimitry Andric    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
13010b57cec5SDimitry Andric    size_type find(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
1302*81ad6265SDimitry Andric    _LIBCPP_CONSTEXPR_AFTER_CXX17 size_type find(value_type __c, size_type __pos = 0) const _NOEXCEPT;
13030b57cec5SDimitry Andric
1304*81ad6265SDimitry Andric    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
13050b57cec5SDimitry Andric    size_type rfind(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
13060b57cec5SDimitry Andric
13070b57cec5SDimitry Andric    template <class _Tp>
1308*81ad6265SDimitry Andric    _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_AFTER_CXX17
1309349cc55cSDimitry Andric    __enable_if_t
13100b57cec5SDimitry Andric        <
13110b57cec5SDimitry Andric            __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
13120b57cec5SDimitry Andric            size_type
13135ffd83dbSDimitry Andric        >
1314fe6060f1SDimitry Andric              rfind(const _Tp& __t, size_type __pos = npos) const _NOEXCEPT;
1315*81ad6265SDimitry Andric    _LIBCPP_CONSTEXPR_AFTER_CXX17
13160b57cec5SDimitry Andric    size_type rfind(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
1317*81ad6265SDimitry Andric    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
13180b57cec5SDimitry Andric    size_type rfind(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
1319*81ad6265SDimitry Andric    _LIBCPP_CONSTEXPR_AFTER_CXX17 size_type rfind(value_type __c, size_type __pos = npos) const _NOEXCEPT;
13200b57cec5SDimitry Andric
1321*81ad6265SDimitry Andric    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
13220b57cec5SDimitry Andric    size_type find_first_of(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
13230b57cec5SDimitry Andric
13240b57cec5SDimitry Andric    template <class _Tp>
1325*81ad6265SDimitry Andric    _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_AFTER_CXX17
1326349cc55cSDimitry Andric    __enable_if_t
13270b57cec5SDimitry Andric        <
13280b57cec5SDimitry Andric            __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
13290b57cec5SDimitry Andric            size_type
13305ffd83dbSDimitry Andric        >
1331fe6060f1SDimitry Andric              find_first_of(const _Tp& __t, size_type __pos = 0) const _NOEXCEPT;
1332*81ad6265SDimitry Andric    _LIBCPP_CONSTEXPR_AFTER_CXX17
13330b57cec5SDimitry Andric    size_type find_first_of(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
1334*81ad6265SDimitry Andric    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
13350b57cec5SDimitry Andric    size_type find_first_of(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
1336*81ad6265SDimitry Andric    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
13370b57cec5SDimitry Andric    size_type find_first_of(value_type __c, size_type __pos = 0) const _NOEXCEPT;
13380b57cec5SDimitry Andric
1339*81ad6265SDimitry Andric    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
13400b57cec5SDimitry Andric    size_type find_last_of(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
13410b57cec5SDimitry Andric
13420b57cec5SDimitry Andric    template <class _Tp>
1343*81ad6265SDimitry Andric    _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_AFTER_CXX17
1344349cc55cSDimitry Andric    __enable_if_t
13450b57cec5SDimitry Andric        <
13460b57cec5SDimitry Andric            __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
13470b57cec5SDimitry Andric            size_type
13485ffd83dbSDimitry Andric        >
1349fe6060f1SDimitry Andric              find_last_of(const _Tp& __t, size_type __pos = npos) const _NOEXCEPT;
1350*81ad6265SDimitry Andric    _LIBCPP_CONSTEXPR_AFTER_CXX17
13510b57cec5SDimitry Andric    size_type find_last_of(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
1352*81ad6265SDimitry Andric    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
13530b57cec5SDimitry Andric    size_type find_last_of(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
1354*81ad6265SDimitry Andric    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
13550b57cec5SDimitry Andric    size_type find_last_of(value_type __c, size_type __pos = npos) const _NOEXCEPT;
13560b57cec5SDimitry Andric
1357*81ad6265SDimitry Andric    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
13580b57cec5SDimitry Andric    size_type find_first_not_of(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
13590b57cec5SDimitry Andric
13600b57cec5SDimitry Andric    template <class _Tp>
1361*81ad6265SDimitry Andric    _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_AFTER_CXX17
1362349cc55cSDimitry Andric    __enable_if_t
13630b57cec5SDimitry Andric        <
13640b57cec5SDimitry Andric            __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
13650b57cec5SDimitry Andric            size_type
13665ffd83dbSDimitry Andric        >
1367fe6060f1SDimitry Andric              find_first_not_of(const _Tp &__t, size_type __pos = 0) const _NOEXCEPT;
1368*81ad6265SDimitry Andric    _LIBCPP_CONSTEXPR_AFTER_CXX17
13690b57cec5SDimitry Andric    size_type find_first_not_of(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
1370*81ad6265SDimitry Andric    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
13710b57cec5SDimitry Andric    size_type find_first_not_of(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
1372*81ad6265SDimitry Andric    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
13730b57cec5SDimitry Andric    size_type find_first_not_of(value_type __c, size_type __pos = 0) const _NOEXCEPT;
13740b57cec5SDimitry Andric
1375*81ad6265SDimitry Andric    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
13760b57cec5SDimitry Andric    size_type find_last_not_of(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
13770b57cec5SDimitry Andric
13780b57cec5SDimitry Andric    template <class _Tp>
1379*81ad6265SDimitry Andric    _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_AFTER_CXX17
1380349cc55cSDimitry Andric    __enable_if_t
13810b57cec5SDimitry Andric        <
13820b57cec5SDimitry Andric            __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
13830b57cec5SDimitry Andric            size_type
13845ffd83dbSDimitry Andric        >
1385fe6060f1SDimitry Andric              find_last_not_of(const _Tp& __t, size_type __pos = npos) const _NOEXCEPT;
1386*81ad6265SDimitry Andric    _LIBCPP_CONSTEXPR_AFTER_CXX17
13870b57cec5SDimitry Andric    size_type find_last_not_of(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
1388*81ad6265SDimitry Andric    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
13890b57cec5SDimitry Andric    size_type find_last_not_of(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
1390*81ad6265SDimitry Andric    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
13910b57cec5SDimitry Andric    size_type find_last_not_of(value_type __c, size_type __pos = npos) const _NOEXCEPT;
13920b57cec5SDimitry Andric
1393*81ad6265SDimitry Andric    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
13940b57cec5SDimitry Andric    int compare(const basic_string& __str) const _NOEXCEPT;
13950b57cec5SDimitry Andric
13960b57cec5SDimitry Andric    template <class _Tp>
1397*81ad6265SDimitry Andric    _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_AFTER_CXX17
1398349cc55cSDimitry Andric    __enable_if_t
13990b57cec5SDimitry Andric        <
14000b57cec5SDimitry Andric            __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
14010b57cec5SDimitry Andric            int
14025ffd83dbSDimitry Andric        >
1403fe6060f1SDimitry Andric        compare(const _Tp &__t) const _NOEXCEPT;
14040b57cec5SDimitry Andric
14050b57cec5SDimitry Andric    template <class _Tp>
1406*81ad6265SDimitry Andric    _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_AFTER_CXX17
1407349cc55cSDimitry Andric    __enable_if_t
14080b57cec5SDimitry Andric        <
14090b57cec5SDimitry Andric            __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
14100b57cec5SDimitry Andric            int
14115ffd83dbSDimitry Andric        >
14120b57cec5SDimitry Andric         compare(size_type __pos1, size_type __n1, const _Tp& __t) const;
14130b57cec5SDimitry Andric
1414*81ad6265SDimitry Andric    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
14150b57cec5SDimitry Andric    int compare(size_type __pos1, size_type __n1, const basic_string& __str) const;
1416*81ad6265SDimitry Andric    _LIBCPP_CONSTEXPR_AFTER_CXX17
1417*81ad6265SDimitry Andric    int compare(size_type __pos1, size_type __n1, const basic_string& __str, size_type __pos2,
1418*81ad6265SDimitry Andric                size_type __n2 = npos) const;
14190b57cec5SDimitry Andric
14200b57cec5SDimitry Andric    template <class _Tp>
1421*81ad6265SDimitry Andric    inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
1422349cc55cSDimitry Andric        __enable_if_t
14230b57cec5SDimitry Andric        <
14245ffd83dbSDimitry Andric            __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value  && !__is_same_uncvref<_Tp, basic_string>::value,
14250b57cec5SDimitry Andric            int
14265ffd83dbSDimitry Andric        >
14270b57cec5SDimitry Andric        compare(size_type __pos1, size_type __n1, const _Tp& __t, size_type __pos2, size_type __n2=npos) const;
1428*81ad6265SDimitry Andric    _LIBCPP_CONSTEXPR_AFTER_CXX17 int compare(const value_type* __s) const _NOEXCEPT;
1429*81ad6265SDimitry Andric    _LIBCPP_CONSTEXPR_AFTER_CXX17 int compare(size_type __pos1, size_type __n1, const value_type* __s) const;
1430*81ad6265SDimitry Andric    _LIBCPP_CONSTEXPR_AFTER_CXX17
14310b57cec5SDimitry Andric    int compare(size_type __pos1, size_type __n1, const value_type* __s, size_type __n2) const;
14320b57cec5SDimitry Andric
14330b57cec5SDimitry Andric#if _LIBCPP_STD_VER > 17
1434*81ad6265SDimitry Andric    constexpr _LIBCPP_HIDE_FROM_ABI
1435349cc55cSDimitry Andric    bool starts_with(__self_view __sv) const noexcept
14360b57cec5SDimitry Andric    { return __self_view(data(), size()).starts_with(__sv); }
14370b57cec5SDimitry Andric
1438*81ad6265SDimitry Andric    constexpr _LIBCPP_HIDE_FROM_ABI
1439349cc55cSDimitry Andric    bool starts_with(value_type __c) const noexcept
14400b57cec5SDimitry Andric    { return !empty() && _Traits::eq(front(), __c); }
14410b57cec5SDimitry Andric
1442*81ad6265SDimitry Andric    constexpr _LIBCPP_HIDE_FROM_ABI
1443349cc55cSDimitry Andric    bool starts_with(const value_type* __s) const noexcept
14440b57cec5SDimitry Andric    { return starts_with(__self_view(__s)); }
14450b57cec5SDimitry Andric
1446*81ad6265SDimitry Andric    constexpr _LIBCPP_HIDE_FROM_ABI
1447349cc55cSDimitry Andric    bool ends_with(__self_view __sv) const noexcept
14480b57cec5SDimitry Andric    { return __self_view(data(), size()).ends_with( __sv); }
14490b57cec5SDimitry Andric
1450*81ad6265SDimitry Andric    constexpr _LIBCPP_HIDE_FROM_ABI
1451349cc55cSDimitry Andric    bool ends_with(value_type __c) const noexcept
14520b57cec5SDimitry Andric    { return !empty() && _Traits::eq(back(), __c); }
14530b57cec5SDimitry Andric
1454*81ad6265SDimitry Andric    constexpr _LIBCPP_HIDE_FROM_ABI
1455349cc55cSDimitry Andric    bool ends_with(const value_type* __s) const noexcept
14560b57cec5SDimitry Andric    { return ends_with(__self_view(__s)); }
14570b57cec5SDimitry Andric#endif
14580b57cec5SDimitry Andric
1459e8d8bef9SDimitry Andric#if _LIBCPP_STD_VER > 20
1460*81ad6265SDimitry Andric    constexpr _LIBCPP_HIDE_FROM_ABI
1461e8d8bef9SDimitry Andric    bool contains(__self_view __sv) const noexcept
1462e8d8bef9SDimitry Andric    { return __self_view(data(), size()).contains(__sv); }
1463e8d8bef9SDimitry Andric
1464*81ad6265SDimitry Andric    constexpr _LIBCPP_HIDE_FROM_ABI
1465e8d8bef9SDimitry Andric    bool contains(value_type __c) const noexcept
1466e8d8bef9SDimitry Andric    { return __self_view(data(), size()).contains(__c); }
1467e8d8bef9SDimitry Andric
1468*81ad6265SDimitry Andric    constexpr _LIBCPP_HIDE_FROM_ABI
1469e8d8bef9SDimitry Andric    bool contains(const value_type* __s) const
1470e8d8bef9SDimitry Andric    { return __self_view(data(), size()).contains(__s); }
1471e8d8bef9SDimitry Andric#endif
1472e8d8bef9SDimitry Andric
1473*81ad6265SDimitry Andric    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 bool __invariants() const;
14740b57cec5SDimitry Andric
1475*81ad6265SDimitry Andric    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 void __clear_and_shrink() _NOEXCEPT;
14760b57cec5SDimitry Andric
1477*81ad6265SDimitry Andric#ifdef _LIBCPP_ENABLE_DEBUG_MODE
14780b57cec5SDimitry Andric
14790b57cec5SDimitry Andric    bool __dereferenceable(const const_iterator* __i) const;
14800b57cec5SDimitry Andric    bool __decrementable(const const_iterator* __i) const;
14810b57cec5SDimitry Andric    bool __addable(const const_iterator* __i, ptrdiff_t __n) const;
14820b57cec5SDimitry Andric    bool __subscriptable(const const_iterator* __i, ptrdiff_t __n) const;
14830b57cec5SDimitry Andric
1484*81ad6265SDimitry Andric#endif // _LIBCPP_ENABLE_DEBUG_MODE
14850b57cec5SDimitry Andric
14860b57cec5SDimitry Andricprivate:
1487*81ad6265SDimitry Andric    template<class _Alloc>
1488*81ad6265SDimitry Andric    inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
1489*81ad6265SDimitry Andric    bool friend operator==(const basic_string<char, char_traits<char>, _Alloc>& __lhs,
1490*81ad6265SDimitry Andric                           const basic_string<char, char_traits<char>, _Alloc>& __rhs) _NOEXCEPT;
1491*81ad6265SDimitry Andric
1492*81ad6265SDimitry Andric    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 void __shrink_or_extend(size_type __target_capacity);
1493*81ad6265SDimitry Andric
1494*81ad6265SDimitry Andric    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
1495*81ad6265SDimitry Andric    bool __is_long() const _NOEXCEPT {
1496*81ad6265SDimitry Andric        if (__libcpp_is_constant_evaluated())
1497*81ad6265SDimitry Andric            return true;
1498*81ad6265SDimitry Andric        return __r_.first().__s.__is_long_;
1499*81ad6265SDimitry Andric    }
1500*81ad6265SDimitry Andric
1501*81ad6265SDimitry Andric    static _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 void __begin_lifetime(pointer __begin, size_type __n) {
1502*81ad6265SDimitry Andric#if _LIBCPP_STD_VER > 17
1503*81ad6265SDimitry Andric        if (__libcpp_is_constant_evaluated()) {
1504*81ad6265SDimitry Andric            for (size_type __i = 0; __i != __n; ++__i)
1505*81ad6265SDimitry Andric                std::construct_at(std::addressof(__begin[__i]));
1506*81ad6265SDimitry Andric        }
1507*81ad6265SDimitry Andric#else
1508*81ad6265SDimitry Andric        (void)__begin;
1509*81ad6265SDimitry Andric        (void)__n;
1510*81ad6265SDimitry Andric#endif // _LIBCPP_STD_VER > 17
1511*81ad6265SDimitry Andric    }
1512*81ad6265SDimitry Andric
1513*81ad6265SDimitry Andric    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 void __default_init() {
1514*81ad6265SDimitry Andric        __zero();
1515*81ad6265SDimitry Andric        if (__libcpp_is_constant_evaluated()) {
1516*81ad6265SDimitry Andric            size_type __sz = __recommend(0) + 1;
1517*81ad6265SDimitry Andric            pointer __ptr = __alloc_traits::allocate(__alloc(), __sz);
1518*81ad6265SDimitry Andric            __begin_lifetime(__ptr, __sz);
1519*81ad6265SDimitry Andric            __set_long_pointer(__ptr);
1520*81ad6265SDimitry Andric            __set_long_cap(__sz);
1521*81ad6265SDimitry Andric            __set_long_size(0);
1522*81ad6265SDimitry Andric        }
1523*81ad6265SDimitry Andric    }
1524*81ad6265SDimitry Andric
1525*81ad6265SDimitry Andric    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 void __deallocate_constexpr() {
1526*81ad6265SDimitry Andric        if (__libcpp_is_constant_evaluated() && __get_pointer() != nullptr)
1527*81ad6265SDimitry Andric            __alloc_traits::deallocate(__alloc(), __get_pointer(), __get_long_cap());
1528*81ad6265SDimitry Andric    }
1529*81ad6265SDimitry Andric
153004eeddc0SDimitry Andric    _LIBCPP_CONSTEXPR _LIBCPP_HIDE_FROM_ABI static bool __fits_in_sso(size_type __sz) {
153104eeddc0SDimitry Andric        // SSO is disabled during constant evaluation because `__is_long` isn't constexpr friendly
153204eeddc0SDimitry Andric        return !__libcpp_is_constant_evaluated() && (__sz < __min_cap);
153304eeddc0SDimitry Andric    }
153404eeddc0SDimitry Andric
1535*81ad6265SDimitry Andric    template <class _ForwardIterator>
1536*81ad6265SDimitry Andric    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX11
1537*81ad6265SDimitry Andric    iterator __insert_from_safe_copy(size_type __n, size_type __ip, _ForwardIterator __first, _ForwardIterator __last) {
1538*81ad6265SDimitry Andric        size_type __sz = size();
1539*81ad6265SDimitry Andric        size_type __cap = capacity();
1540*81ad6265SDimitry Andric        value_type* __p;
1541*81ad6265SDimitry Andric        if (__cap - __sz >= __n)
1542*81ad6265SDimitry Andric        {
1543*81ad6265SDimitry Andric            __p = std::__to_address(__get_pointer());
1544*81ad6265SDimitry Andric            size_type __n_move = __sz - __ip;
1545*81ad6265SDimitry Andric            if (__n_move != 0)
1546*81ad6265SDimitry Andric                traits_type::move(__p + __ip + __n, __p + __ip, __n_move);
1547*81ad6265SDimitry Andric        }
1548*81ad6265SDimitry Andric        else
1549*81ad6265SDimitry Andric        {
1550*81ad6265SDimitry Andric            __grow_by(__cap, __sz + __n - __cap, __sz, __ip, 0, __n);
1551*81ad6265SDimitry Andric            __p = std::__to_address(__get_long_pointer());
1552*81ad6265SDimitry Andric        }
1553*81ad6265SDimitry Andric        __sz += __n;
1554*81ad6265SDimitry Andric        __set_size(__sz);
1555*81ad6265SDimitry Andric        traits_type::assign(__p[__sz], value_type());
1556*81ad6265SDimitry Andric        for (__p += __ip; __first != __last; ++__p, ++__first)
1557*81ad6265SDimitry Andric            traits_type::assign(*__p, *__first);
15580b57cec5SDimitry Andric
1559*81ad6265SDimitry Andric        return begin() + __ip;
1560*81ad6265SDimitry Andric    }
15610b57cec5SDimitry Andric
1562*81ad6265SDimitry Andric    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX11 allocator_type& __alloc() _NOEXCEPT { return __r_.second(); }
1563*81ad6265SDimitry Andric    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR const allocator_type& __alloc() const _NOEXCEPT { return __r_.second(); }
15640b57cec5SDimitry Andric
1565*81ad6265SDimitry Andric    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
1566*81ad6265SDimitry Andric    void __set_short_size(size_type __s) _NOEXCEPT {
1567*81ad6265SDimitry Andric        _LIBCPP_ASSERT(__s < __min_cap, "__s should never be greater than or equal to the short string capacity");
1568*81ad6265SDimitry Andric        __r_.first().__s.__size_ = __s;
1569*81ad6265SDimitry Andric        __r_.first().__s.__is_long_ = false;
1570*81ad6265SDimitry Andric    }
15710b57cec5SDimitry Andric
1572*81ad6265SDimitry Andric    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
1573*81ad6265SDimitry Andric    size_type __get_short_size() const _NOEXCEPT {
1574*81ad6265SDimitry Andric        _LIBCPP_ASSERT(!__r_.first().__s.__is_long_, "String has to be short when trying to get the short size");
1575*81ad6265SDimitry Andric        return __r_.first().__s.__size_;
1576*81ad6265SDimitry Andric    }
15770b57cec5SDimitry Andric
1578*81ad6265SDimitry Andric    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
15790b57cec5SDimitry Andric    void __set_long_size(size_type __s) _NOEXCEPT
15800b57cec5SDimitry Andric        {__r_.first().__l.__size_ = __s;}
1581*81ad6265SDimitry Andric    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
15820b57cec5SDimitry Andric    size_type __get_long_size() const _NOEXCEPT
15830b57cec5SDimitry Andric        {return __r_.first().__l.__size_;}
1584*81ad6265SDimitry Andric    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
15850b57cec5SDimitry Andric    void __set_size(size_type __s) _NOEXCEPT
15860b57cec5SDimitry Andric        {if (__is_long()) __set_long_size(__s); else __set_short_size(__s);}
15870b57cec5SDimitry Andric
1588*81ad6265SDimitry Andric    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
1589*81ad6265SDimitry Andric    void __set_long_cap(size_type __s) _NOEXCEPT {
1590*81ad6265SDimitry Andric        __r_.first().__l.__cap_ = __s / __endian_factor;
1591*81ad6265SDimitry Andric        __r_.first().__l.__is_long_ = true;
1592*81ad6265SDimitry Andric    }
15930b57cec5SDimitry Andric
1594*81ad6265SDimitry Andric    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
1595*81ad6265SDimitry Andric    size_type __get_long_cap() const _NOEXCEPT {
1596*81ad6265SDimitry Andric        return __r_.first().__l.__cap_ * __endian_factor;
1597*81ad6265SDimitry Andric    }
1598*81ad6265SDimitry Andric
1599*81ad6265SDimitry Andric    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
16000b57cec5SDimitry Andric    void __set_long_pointer(pointer __p) _NOEXCEPT
16010b57cec5SDimitry Andric        {__r_.first().__l.__data_ = __p;}
1602*81ad6265SDimitry Andric    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
16030b57cec5SDimitry Andric    pointer __get_long_pointer() _NOEXCEPT
16040b57cec5SDimitry Andric        {return __r_.first().__l.__data_;}
1605*81ad6265SDimitry Andric    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
16060b57cec5SDimitry Andric    const_pointer __get_long_pointer() const _NOEXCEPT
16070b57cec5SDimitry Andric        {return __r_.first().__l.__data_;}
1608*81ad6265SDimitry Andric    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
16090b57cec5SDimitry Andric    pointer __get_short_pointer() _NOEXCEPT
16100b57cec5SDimitry Andric        {return pointer_traits<pointer>::pointer_to(__r_.first().__s.__data_[0]);}
1611*81ad6265SDimitry Andric    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
16120b57cec5SDimitry Andric    const_pointer __get_short_pointer() const _NOEXCEPT
16130b57cec5SDimitry Andric        {return pointer_traits<const_pointer>::pointer_to(__r_.first().__s.__data_[0]);}
1614*81ad6265SDimitry Andric    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
16150b57cec5SDimitry Andric    pointer __get_pointer() _NOEXCEPT
16160b57cec5SDimitry Andric        {return __is_long() ? __get_long_pointer() : __get_short_pointer();}
1617*81ad6265SDimitry Andric    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
16180b57cec5SDimitry Andric    const_pointer __get_pointer() const _NOEXCEPT
16190b57cec5SDimitry Andric        {return __is_long() ? __get_long_pointer() : __get_short_pointer();}
16200b57cec5SDimitry Andric
1621*81ad6265SDimitry Andric    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
1622*81ad6265SDimitry Andric    void __zero() _NOEXCEPT {
1623*81ad6265SDimitry Andric        __r_.first() = __rep();
16240b57cec5SDimitry Andric    }
16250b57cec5SDimitry Andric
16260b57cec5SDimitry Andric    template <size_type __a> static
1627*81ad6265SDimitry Andric        _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
16280b57cec5SDimitry Andric        size_type __align_it(size_type __s) _NOEXCEPT
16290b57cec5SDimitry Andric            {return (__s + (__a-1)) & ~(__a-1);}
16300b57cec5SDimitry Andric    enum {__alignment = 16};
1631*81ad6265SDimitry Andric    static _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
16320b57cec5SDimitry Andric    size_type __recommend(size_type __s) _NOEXCEPT
16330b57cec5SDimitry Andric    {
1634*81ad6265SDimitry Andric        if (__s < __min_cap) {
1635*81ad6265SDimitry Andric            if (__libcpp_is_constant_evaluated())
1636*81ad6265SDimitry Andric                return static_cast<size_type>(__min_cap);
1637*81ad6265SDimitry Andric            else
1638*81ad6265SDimitry Andric                return static_cast<size_type>(__min_cap) - 1;
1639*81ad6265SDimitry Andric        }
16400b57cec5SDimitry Andric        size_type __guess = __align_it<sizeof(value_type) < __alignment ?
16410b57cec5SDimitry Andric                     __alignment/sizeof(value_type) : 1 > (__s+1) - 1;
16420b57cec5SDimitry Andric        if (__guess == __min_cap) ++__guess;
16430b57cec5SDimitry Andric        return __guess;
16440b57cec5SDimitry Andric    }
16450b57cec5SDimitry Andric
1646*81ad6265SDimitry Andric    inline _LIBCPP_CONSTEXPR_AFTER_CXX17
16470b57cec5SDimitry Andric    void __init(const value_type* __s, size_type __sz, size_type __reserve);
1648*81ad6265SDimitry Andric    inline _LIBCPP_CONSTEXPR_AFTER_CXX17
16490b57cec5SDimitry Andric    void __init(const value_type* __s, size_type __sz);
1650*81ad6265SDimitry Andric    inline _LIBCPP_CONSTEXPR_AFTER_CXX17
16510b57cec5SDimitry Andric    void __init(size_type __n, value_type __c);
16520b57cec5SDimitry Andric
16535ffd83dbSDimitry Andric    // Slow path for the (inlined) copy constructor for 'long' strings.
16545ffd83dbSDimitry Andric    // Always externally instantiated and not inlined.
16555ffd83dbSDimitry Andric    // Requires that __s is zero terminated.
16565ffd83dbSDimitry Andric    // The main reason for this function to exist is because for unstable, we
16575ffd83dbSDimitry Andric    // want to allow inlining of the copy constructor. However, we don't want
16585ffd83dbSDimitry Andric    // to call the __init() functions as those are marked as inline which may
16595ffd83dbSDimitry Andric    // result in over-aggressive inlining by the compiler, where our aim is
16605ffd83dbSDimitry Andric    // to only inline the fast path code directly in the ctor.
1661*81ad6265SDimitry Andric    _LIBCPP_CONSTEXPR_AFTER_CXX17 void __init_copy_ctor_external(const value_type* __s, size_type __sz);
16625ffd83dbSDimitry Andric
16630b57cec5SDimitry Andric    template <class _InputIterator>
1664*81ad6265SDimitry Andric    inline _LIBCPP_CONSTEXPR_AFTER_CXX17
1665349cc55cSDimitry Andric    __enable_if_t
16660b57cec5SDimitry Andric    <
16675ffd83dbSDimitry Andric        __is_exactly_cpp17_input_iterator<_InputIterator>::value
16685ffd83dbSDimitry Andric    >
16690b57cec5SDimitry Andric    __init(_InputIterator __first, _InputIterator __last);
16700b57cec5SDimitry Andric
16710b57cec5SDimitry Andric    template <class _ForwardIterator>
1672*81ad6265SDimitry Andric    inline _LIBCPP_CONSTEXPR_AFTER_CXX17
1673349cc55cSDimitry Andric    __enable_if_t
16740b57cec5SDimitry Andric    <
16755ffd83dbSDimitry Andric        __is_cpp17_forward_iterator<_ForwardIterator>::value
16765ffd83dbSDimitry Andric    >
16770b57cec5SDimitry Andric    __init(_ForwardIterator __first, _ForwardIterator __last);
16780b57cec5SDimitry Andric
1679*81ad6265SDimitry Andric    _LIBCPP_CONSTEXPR_AFTER_CXX17
16800b57cec5SDimitry Andric    void __grow_by(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
16810b57cec5SDimitry Andric                   size_type __n_copy,  size_type __n_del,     size_type __n_add = 0);
1682*81ad6265SDimitry Andric    _LIBCPP_CONSTEXPR_AFTER_CXX17
16830b57cec5SDimitry Andric    void __grow_by_and_replace(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
16840b57cec5SDimitry Andric                               size_type __n_copy,  size_type __n_del,
16850b57cec5SDimitry Andric                               size_type __n_add, const value_type* __p_new_stuff);
16860b57cec5SDimitry Andric
16875ffd83dbSDimitry Andric    // __assign_no_alias is invoked for assignment operations where we
16885ffd83dbSDimitry Andric    // have proof that the input does not alias the current instance.
16895ffd83dbSDimitry Andric    // For example, operator=(basic_string) performs a 'self' check.
16905ffd83dbSDimitry Andric    template <bool __is_short>
1691*81ad6265SDimitry Andric    _LIBCPP_CONSTEXPR_AFTER_CXX17 basic_string& __assign_no_alias(const value_type* __s, size_type __n);
16925ffd83dbSDimitry Andric
1693*81ad6265SDimitry Andric    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
16940b57cec5SDimitry Andric    void __erase_to_end(size_type __pos);
16950b57cec5SDimitry Andric
16965ffd83dbSDimitry Andric    // __erase_external_with_move is invoked for erase() invocations where
16975ffd83dbSDimitry Andric    // `n ~= npos`, likely requiring memory moves on the string data.
1698*81ad6265SDimitry Andric    _LIBCPP_CONSTEXPR_AFTER_CXX17 void __erase_external_with_move(size_type __pos, size_type __n);
16995ffd83dbSDimitry Andric
1700*81ad6265SDimitry Andric    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
17010b57cec5SDimitry Andric    void __copy_assign_alloc(const basic_string& __str)
17020b57cec5SDimitry Andric        {__copy_assign_alloc(__str, integral_constant<bool,
17030b57cec5SDimitry Andric                      __alloc_traits::propagate_on_container_copy_assignment::value>());}
17040b57cec5SDimitry Andric
1705*81ad6265SDimitry Andric    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
17060b57cec5SDimitry Andric    void __copy_assign_alloc(const basic_string& __str, true_type)
17070b57cec5SDimitry Andric        {
17080b57cec5SDimitry Andric            if (__alloc() == __str.__alloc())
17090b57cec5SDimitry Andric                __alloc() = __str.__alloc();
17100b57cec5SDimitry Andric            else
17110b57cec5SDimitry Andric            {
17120b57cec5SDimitry Andric                if (!__str.__is_long())
17130b57cec5SDimitry Andric                {
17140b57cec5SDimitry Andric                    __clear_and_shrink();
17150b57cec5SDimitry Andric                    __alloc() = __str.__alloc();
17160b57cec5SDimitry Andric                }
17170b57cec5SDimitry Andric                else
17180b57cec5SDimitry Andric                {
17190b57cec5SDimitry Andric                    allocator_type __a = __str.__alloc();
1720*81ad6265SDimitry Andric                    auto __allocation = std::__allocate_at_least(__a, __str.__get_long_cap());
1721*81ad6265SDimitry Andric                    __begin_lifetime(__allocation.ptr, __allocation.count);
17220b57cec5SDimitry Andric                    __clear_and_shrink();
1723*81ad6265SDimitry Andric                    __alloc() = std::move(__a);
1724*81ad6265SDimitry Andric                    __set_long_pointer(__allocation.ptr);
1725*81ad6265SDimitry Andric                    __set_long_cap(__allocation.count);
17260b57cec5SDimitry Andric                    __set_long_size(__str.size());
17270b57cec5SDimitry Andric                }
17280b57cec5SDimitry Andric            }
17290b57cec5SDimitry Andric        }
17300b57cec5SDimitry Andric
1731*81ad6265SDimitry Andric    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
17320b57cec5SDimitry Andric    void __copy_assign_alloc(const basic_string&, false_type) _NOEXCEPT
17330b57cec5SDimitry Andric        {}
17340b57cec5SDimitry Andric
17350b57cec5SDimitry Andric#ifndef _LIBCPP_CXX03_LANG
1736*81ad6265SDimitry Andric    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
17370b57cec5SDimitry Andric    void __move_assign(basic_string& __str, false_type)
17380b57cec5SDimitry Andric        _NOEXCEPT_(__alloc_traits::is_always_equal::value);
1739*81ad6265SDimitry Andric    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
17400b57cec5SDimitry Andric    void __move_assign(basic_string& __str, true_type)
17410b57cec5SDimitry Andric#if _LIBCPP_STD_VER > 14
17420b57cec5SDimitry Andric        _NOEXCEPT;
17430b57cec5SDimitry Andric#else
17440b57cec5SDimitry Andric        _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
17450b57cec5SDimitry Andric#endif
17460b57cec5SDimitry Andric#endif
17470b57cec5SDimitry Andric
1748*81ad6265SDimitry Andric    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
17490b57cec5SDimitry Andric    void
17500b57cec5SDimitry Andric    __move_assign_alloc(basic_string& __str)
17510b57cec5SDimitry Andric        _NOEXCEPT_(
17520b57cec5SDimitry Andric            !__alloc_traits::propagate_on_container_move_assignment::value ||
17530b57cec5SDimitry Andric            is_nothrow_move_assignable<allocator_type>::value)
17540b57cec5SDimitry Andric    {__move_assign_alloc(__str, integral_constant<bool,
17550b57cec5SDimitry Andric                      __alloc_traits::propagate_on_container_move_assignment::value>());}
17560b57cec5SDimitry Andric
1757*81ad6265SDimitry Andric    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
17580b57cec5SDimitry Andric    void __move_assign_alloc(basic_string& __c, true_type)
17590b57cec5SDimitry Andric        _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
17600b57cec5SDimitry Andric        {
1761*81ad6265SDimitry Andric            __alloc() = std::move(__c.__alloc());
17620b57cec5SDimitry Andric        }
17630b57cec5SDimitry Andric
1764*81ad6265SDimitry Andric    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
17650b57cec5SDimitry Andric    void __move_assign_alloc(basic_string&, false_type)
17660b57cec5SDimitry Andric        _NOEXCEPT
17670b57cec5SDimitry Andric        {}
17680b57cec5SDimitry Andric
1769*81ad6265SDimitry Andric    _LIBCPP_CONSTEXPR_AFTER_CXX17 basic_string& __assign_external(const value_type* __s);
1770*81ad6265SDimitry Andric    _LIBCPP_CONSTEXPR_AFTER_CXX17 basic_string& __assign_external(const value_type* __s, size_type __n);
17715ffd83dbSDimitry Andric
17725ffd83dbSDimitry Andric    // Assigns the value in __s, guaranteed to be __n < __min_cap in length.
17735ffd83dbSDimitry Andric    inline basic_string& __assign_short(const value_type* __s, size_type __n) {
17745ffd83dbSDimitry Andric      pointer __p = __is_long()
17755ffd83dbSDimitry Andric                        ? (__set_long_size(__n), __get_long_pointer())
17765ffd83dbSDimitry Andric                        : (__set_short_size(__n), __get_short_pointer());
1777*81ad6265SDimitry Andric      traits_type::move(std::__to_address(__p), __s, __n);
17785ffd83dbSDimitry Andric      traits_type::assign(__p[__n], value_type());
17795ffd83dbSDimitry Andric      return *this;
17805ffd83dbSDimitry Andric    }
17815ffd83dbSDimitry Andric
1782*81ad6265SDimitry Andric    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
1783*81ad6265SDimitry Andric    basic_string& __null_terminate_at(value_type* __p, size_type __newsz) {
17840eae32dcSDimitry Andric      __set_size(__newsz);
17850eae32dcSDimitry Andric      __invalidate_iterators_past(__newsz);
17860eae32dcSDimitry Andric      traits_type::assign(__p[__newsz], value_type());
17870eae32dcSDimitry Andric      return *this;
17880eae32dcSDimitry Andric    }
17890eae32dcSDimitry Andric
1790*81ad6265SDimitry Andric    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 void __invalidate_iterators_past(size_type);
17910b57cec5SDimitry Andric
1792fe6060f1SDimitry Andric    template<class _Tp>
1793*81ad6265SDimitry Andric    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
1794fe6060f1SDimitry Andric    bool __addr_in_range(_Tp&& __t) const {
1795*81ad6265SDimitry Andric        // assume that the ranges overlap, because we can't check during constant evaluation
1796*81ad6265SDimitry Andric        if (__libcpp_is_constant_evaluated())
1797*81ad6265SDimitry Andric          return true;
1798*81ad6265SDimitry Andric        const volatile void *__p = std::addressof(__t);
1799fe6060f1SDimitry Andric        return data() <= __p && __p <= data() + size();
1800fe6060f1SDimitry Andric    }
1801fe6060f1SDimitry Andric
180269ade1e0SDimitry Andric    _LIBCPP_NORETURN _LIBCPP_HIDE_FROM_ABI
180369ade1e0SDimitry Andric    void __throw_length_error() const {
1804*81ad6265SDimitry Andric        std::__throw_length_error("basic_string");
180569ade1e0SDimitry Andric    }
180669ade1e0SDimitry Andric
180769ade1e0SDimitry Andric    _LIBCPP_NORETURN _LIBCPP_HIDE_FROM_ABI
180869ade1e0SDimitry Andric    void __throw_out_of_range() const {
1809*81ad6265SDimitry Andric        std::__throw_out_of_range("basic_string");
181069ade1e0SDimitry Andric    }
181169ade1e0SDimitry Andric
1812*81ad6265SDimitry Andric    friend _LIBCPP_CONSTEXPR_AFTER_CXX17 basic_string operator+<>(const basic_string&, const basic_string&);
1813*81ad6265SDimitry Andric    friend _LIBCPP_CONSTEXPR_AFTER_CXX17 basic_string operator+<>(const value_type*, const basic_string&);
1814*81ad6265SDimitry Andric    friend _LIBCPP_CONSTEXPR_AFTER_CXX17 basic_string operator+<>(value_type, const basic_string&);
1815*81ad6265SDimitry Andric    friend _LIBCPP_CONSTEXPR_AFTER_CXX17 basic_string operator+<>(const basic_string&, const value_type*);
1816*81ad6265SDimitry Andric    friend _LIBCPP_CONSTEXPR_AFTER_CXX17 basic_string operator+<>(const basic_string&, value_type);
18170b57cec5SDimitry Andric};
18180b57cec5SDimitry Andric
18195ffd83dbSDimitry Andric// These declarations must appear before any functions are implicitly used
18205ffd83dbSDimitry Andric// so that they have the correct visibility specifier.
1821*81ad6265SDimitry Andric#define _LIBCPP_DECLARE(...) extern template __VA_ARGS__;
18225ffd83dbSDimitry Andric#ifdef _LIBCPP_ABI_STRING_OPTIMIZED_EXTERNAL_INSTANTIATION
1823*81ad6265SDimitry Andric    _LIBCPP_STRING_UNSTABLE_EXTERN_TEMPLATE_LIST(_LIBCPP_DECLARE, char)
1824349cc55cSDimitry Andric#   ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
1825*81ad6265SDimitry Andric        _LIBCPP_STRING_UNSTABLE_EXTERN_TEMPLATE_LIST(_LIBCPP_DECLARE, wchar_t)
1826349cc55cSDimitry Andric#   endif
18275ffd83dbSDimitry Andric#else
1828*81ad6265SDimitry Andric    _LIBCPP_STRING_V1_EXTERN_TEMPLATE_LIST(_LIBCPP_DECLARE, char)
1829349cc55cSDimitry Andric#   ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
1830*81ad6265SDimitry Andric        _LIBCPP_STRING_V1_EXTERN_TEMPLATE_LIST(_LIBCPP_DECLARE, wchar_t)
18315ffd83dbSDimitry Andric#   endif
1832349cc55cSDimitry Andric#endif
1833*81ad6265SDimitry Andric#undef _LIBCPP_DECLARE
18345ffd83dbSDimitry Andric
18355ffd83dbSDimitry Andric
1836349cc55cSDimitry Andric#if _LIBCPP_STD_VER >= 17
18370b57cec5SDimitry Andrictemplate<class _InputIterator,
1838fe6060f1SDimitry Andric         class _CharT = __iter_value_type<_InputIterator>,
18390b57cec5SDimitry Andric         class _Allocator = allocator<_CharT>,
1840349cc55cSDimitry Andric         class = enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value>,
1841349cc55cSDimitry Andric         class = enable_if_t<__is_allocator<_Allocator>::value>
18420b57cec5SDimitry Andric         >
18430b57cec5SDimitry Andricbasic_string(_InputIterator, _InputIterator, _Allocator = _Allocator())
18440b57cec5SDimitry Andric  -> basic_string<_CharT, char_traits<_CharT>, _Allocator>;
18450b57cec5SDimitry Andric
18460b57cec5SDimitry Andrictemplate<class _CharT,
18470b57cec5SDimitry Andric         class _Traits,
18480b57cec5SDimitry Andric         class _Allocator = allocator<_CharT>,
1849349cc55cSDimitry Andric         class = enable_if_t<__is_allocator<_Allocator>::value>
18500b57cec5SDimitry Andric         >
18510b57cec5SDimitry Andricexplicit basic_string(basic_string_view<_CharT, _Traits>, const _Allocator& = _Allocator())
18520b57cec5SDimitry Andric  -> basic_string<_CharT, _Traits, _Allocator>;
18530b57cec5SDimitry Andric
18540b57cec5SDimitry Andrictemplate<class _CharT,
18550b57cec5SDimitry Andric         class _Traits,
18560b57cec5SDimitry Andric         class _Allocator = allocator<_CharT>,
1857349cc55cSDimitry Andric         class = enable_if_t<__is_allocator<_Allocator>::value>,
18580b57cec5SDimitry Andric         class _Sz = typename allocator_traits<_Allocator>::size_type
18590b57cec5SDimitry Andric         >
18600b57cec5SDimitry Andricbasic_string(basic_string_view<_CharT, _Traits>, _Sz, _Sz, const _Allocator& = _Allocator())
18610b57cec5SDimitry Andric  -> basic_string<_CharT, _Traits, _Allocator>;
18620b57cec5SDimitry Andric#endif
18630b57cec5SDimitry Andric
18640b57cec5SDimitry Andrictemplate <class _CharT, class _Traits, class _Allocator>
1865*81ad6265SDimitry Andricinline _LIBCPP_CONSTEXPR_AFTER_CXX17
18660b57cec5SDimitry Andricvoid
1867fe6060f1SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>::__invalidate_iterators_past(size_type __pos)
18680b57cec5SDimitry Andric{
1869*81ad6265SDimitry Andric#ifdef _LIBCPP_ENABLE_DEBUG_MODE
18700eae32dcSDimitry Andric    if (!__libcpp_is_constant_evaluated()) {
18710b57cec5SDimitry Andric        __c_node* __c = __get_db()->__find_c_and_lock(this);
18720b57cec5SDimitry Andric        if (__c)
18730b57cec5SDimitry Andric        {
18740b57cec5SDimitry Andric            const_pointer __new_last = __get_pointer() + __pos;
18750b57cec5SDimitry Andric            for (__i_node** __p = __c->end_; __p != __c->beg_; )
18760b57cec5SDimitry Andric            {
18770b57cec5SDimitry Andric                --__p;
18780b57cec5SDimitry Andric                const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_);
18790b57cec5SDimitry Andric                if (__i->base() > __new_last)
18800b57cec5SDimitry Andric                {
18810b57cec5SDimitry Andric                    (*__p)->__c_ = nullptr;
18820b57cec5SDimitry Andric                    if (--__c->end_ != __p)
1883*81ad6265SDimitry Andric                        std::memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
18840b57cec5SDimitry Andric                }
18850b57cec5SDimitry Andric            }
18860b57cec5SDimitry Andric            __get_db()->unlock();
18870b57cec5SDimitry Andric        }
18880eae32dcSDimitry Andric    }
1889fe6060f1SDimitry Andric#else
1890fe6060f1SDimitry Andric    (void)__pos;
1891*81ad6265SDimitry Andric#endif // _LIBCPP_ENABLE_DEBUG_MODE
18920b57cec5SDimitry Andric}
18930b57cec5SDimitry Andric
18940b57cec5SDimitry Andrictemplate <class _CharT, class _Traits, class _Allocator>
1895*81ad6265SDimitry Andricinline _LIBCPP_CONSTEXPR_AFTER_CXX17
18960b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>::basic_string()
18970b57cec5SDimitry Andric    _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
1898480093f4SDimitry Andric     : __r_(__default_init_tag(), __default_init_tag())
18990b57cec5SDimitry Andric{
1900*81ad6265SDimitry Andric    std::__debug_db_insert_c(this);
1901*81ad6265SDimitry Andric    __default_init();
19020b57cec5SDimitry Andric}
19030b57cec5SDimitry Andric
19040b57cec5SDimitry Andrictemplate <class _CharT, class _Traits, class _Allocator>
1905*81ad6265SDimitry Andricinline _LIBCPP_CONSTEXPR_AFTER_CXX17
19060b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>::basic_string(const allocator_type& __a)
19070b57cec5SDimitry Andric#if _LIBCPP_STD_VER <= 14
19080b57cec5SDimitry Andric        _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value)
19090b57cec5SDimitry Andric#else
19100b57cec5SDimitry Andric        _NOEXCEPT
19110b57cec5SDimitry Andric#endif
1912480093f4SDimitry Andric: __r_(__default_init_tag(), __a)
19130b57cec5SDimitry Andric{
1914*81ad6265SDimitry Andric    std::__debug_db_insert_c(this);
1915*81ad6265SDimitry Andric    __default_init();
19160b57cec5SDimitry Andric}
19170b57cec5SDimitry Andric
19180b57cec5SDimitry Andrictemplate <class _CharT, class _Traits, class _Allocator>
1919*81ad6265SDimitry Andric_LIBCPP_CONSTEXPR_AFTER_CXX17
19200b57cec5SDimitry Andricvoid basic_string<_CharT, _Traits, _Allocator>::__init(const value_type* __s,
19210b57cec5SDimitry Andric                                                       size_type __sz,
19220b57cec5SDimitry Andric                                                       size_type __reserve)
19230b57cec5SDimitry Andric{
1924*81ad6265SDimitry Andric    if (__libcpp_is_constant_evaluated())
1925*81ad6265SDimitry Andric        __zero();
19260b57cec5SDimitry Andric    if (__reserve > max_size())
192704eeddc0SDimitry Andric        __throw_length_error();
19280b57cec5SDimitry Andric    pointer __p;
192904eeddc0SDimitry Andric    if (__fits_in_sso(__reserve))
19300b57cec5SDimitry Andric    {
19310b57cec5SDimitry Andric        __set_short_size(__sz);
19320b57cec5SDimitry Andric        __p = __get_short_pointer();
19330b57cec5SDimitry Andric    }
19340b57cec5SDimitry Andric    else
19350b57cec5SDimitry Andric    {
1936*81ad6265SDimitry Andric        auto __allocation = std::__allocate_at_least(__alloc(), __recommend(__reserve) + 1);
1937*81ad6265SDimitry Andric        __p = __allocation.ptr;
1938*81ad6265SDimitry Andric        __begin_lifetime(__p, __allocation.count);
19390b57cec5SDimitry Andric        __set_long_pointer(__p);
1940*81ad6265SDimitry Andric        __set_long_cap(__allocation.count);
19410b57cec5SDimitry Andric        __set_long_size(__sz);
19420b57cec5SDimitry Andric    }
1943*81ad6265SDimitry Andric    traits_type::copy(std::__to_address(__p), __s, __sz);
19440b57cec5SDimitry Andric    traits_type::assign(__p[__sz], value_type());
19450b57cec5SDimitry Andric}
19460b57cec5SDimitry Andric
19470b57cec5SDimitry Andrictemplate <class _CharT, class _Traits, class _Allocator>
1948*81ad6265SDimitry Andric_LIBCPP_CONSTEXPR_AFTER_CXX17
19490b57cec5SDimitry Andricvoid
19500b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>::__init(const value_type* __s, size_type __sz)
19510b57cec5SDimitry Andric{
1952*81ad6265SDimitry Andric    if (__libcpp_is_constant_evaluated())
1953*81ad6265SDimitry Andric        __zero();
19540b57cec5SDimitry Andric    if (__sz > max_size())
195504eeddc0SDimitry Andric        __throw_length_error();
19560b57cec5SDimitry Andric    pointer __p;
195704eeddc0SDimitry Andric    if (__fits_in_sso(__sz))
19580b57cec5SDimitry Andric    {
19590b57cec5SDimitry Andric        __set_short_size(__sz);
19600b57cec5SDimitry Andric        __p = __get_short_pointer();
19610b57cec5SDimitry Andric    }
19620b57cec5SDimitry Andric    else
19630b57cec5SDimitry Andric    {
1964*81ad6265SDimitry Andric        auto __allocation = std::__allocate_at_least(__alloc(), __recommend(__sz) + 1);
1965*81ad6265SDimitry Andric        __p = __allocation.ptr;
1966*81ad6265SDimitry Andric        __begin_lifetime(__p, __allocation.count);
19670b57cec5SDimitry Andric        __set_long_pointer(__p);
1968*81ad6265SDimitry Andric        __set_long_cap(__allocation.count);
19690b57cec5SDimitry Andric        __set_long_size(__sz);
19700b57cec5SDimitry Andric    }
1971*81ad6265SDimitry Andric    traits_type::copy(std::__to_address(__p), __s, __sz);
19720b57cec5SDimitry Andric    traits_type::assign(__p[__sz], value_type());
19730b57cec5SDimitry Andric}
19740b57cec5SDimitry Andric
19750b57cec5SDimitry Andrictemplate <class _CharT, class _Traits, class _Allocator>
19760b57cec5SDimitry Andrictemplate <class>
1977*81ad6265SDimitry Andric_LIBCPP_CONSTEXPR_AFTER_CXX17
19780b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, const _Allocator& __a)
1979480093f4SDimitry Andric    : __r_(__default_init_tag(), __a)
19800b57cec5SDimitry Andric{
19810b57cec5SDimitry Andric    _LIBCPP_ASSERT(__s != nullptr, "basic_string(const char*, allocator) detected nullptr");
19820b57cec5SDimitry Andric    __init(__s, traits_type::length(__s));
1983*81ad6265SDimitry Andric    std::__debug_db_insert_c(this);
19840b57cec5SDimitry Andric}
19850b57cec5SDimitry Andric
19860b57cec5SDimitry Andrictemplate <class _CharT, class _Traits, class _Allocator>
1987*81ad6265SDimitry Andricinline _LIBCPP_CONSTEXPR_AFTER_CXX17
19880b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, size_type __n)
1989480093f4SDimitry Andric     : __r_(__default_init_tag(), __default_init_tag())
19900b57cec5SDimitry Andric{
19910b57cec5SDimitry Andric    _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "basic_string(const char*, n) detected nullptr");
19920b57cec5SDimitry Andric    __init(__s, __n);
1993*81ad6265SDimitry Andric    std::__debug_db_insert_c(this);
19940b57cec5SDimitry Andric}
19950b57cec5SDimitry Andric
19960b57cec5SDimitry Andrictemplate <class _CharT, class _Traits, class _Allocator>
1997*81ad6265SDimitry Andricinline _LIBCPP_CONSTEXPR_AFTER_CXX17
19980b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, size_type __n, const _Allocator& __a)
1999480093f4SDimitry Andric    : __r_(__default_init_tag(), __a)
20000b57cec5SDimitry Andric{
20010b57cec5SDimitry Andric    _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "basic_string(const char*, n, allocator) detected nullptr");
20020b57cec5SDimitry Andric    __init(__s, __n);
2003*81ad6265SDimitry Andric    std::__debug_db_insert_c(this);
20040b57cec5SDimitry Andric}
20050b57cec5SDimitry Andric
20060b57cec5SDimitry Andrictemplate <class _CharT, class _Traits, class _Allocator>
2007*81ad6265SDimitry Andric_LIBCPP_CONSTEXPR_AFTER_CXX17
20080b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str)
2009480093f4SDimitry Andric    : __r_(__default_init_tag(), __alloc_traits::select_on_container_copy_construction(__str.__alloc()))
20100b57cec5SDimitry Andric{
20110b57cec5SDimitry Andric    if (!__str.__is_long())
20120b57cec5SDimitry Andric        __r_.first().__r = __str.__r_.first().__r;
20130b57cec5SDimitry Andric    else
2014*81ad6265SDimitry Andric        __init_copy_ctor_external(std::__to_address(__str.__get_long_pointer()),
20155ffd83dbSDimitry Andric                                  __str.__get_long_size());
2016*81ad6265SDimitry Andric    std::__debug_db_insert_c(this);
20170b57cec5SDimitry Andric}
20180b57cec5SDimitry Andric
20190b57cec5SDimitry Andrictemplate <class _CharT, class _Traits, class _Allocator>
2020*81ad6265SDimitry Andric_LIBCPP_CONSTEXPR_AFTER_CXX17
20210b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>::basic_string(
20220b57cec5SDimitry Andric    const basic_string& __str, const allocator_type& __a)
2023480093f4SDimitry Andric    : __r_(__default_init_tag(), __a)
20240b57cec5SDimitry Andric{
20250b57cec5SDimitry Andric    if (!__str.__is_long())
20260b57cec5SDimitry Andric        __r_.first().__r = __str.__r_.first().__r;
20270b57cec5SDimitry Andric    else
2028*81ad6265SDimitry Andric        __init_copy_ctor_external(std::__to_address(__str.__get_long_pointer()),
20295ffd83dbSDimitry Andric                                  __str.__get_long_size());
2030*81ad6265SDimitry Andric    std::__debug_db_insert_c(this);
20310b57cec5SDimitry Andric}
20320b57cec5SDimitry Andric
20335ffd83dbSDimitry Andrictemplate <class _CharT, class _Traits, class _Allocator>
2034*81ad6265SDimitry Andric_LIBCPP_CONSTEXPR_AFTER_CXX17
20355ffd83dbSDimitry Andricvoid basic_string<_CharT, _Traits, _Allocator>::__init_copy_ctor_external(
20365ffd83dbSDimitry Andric    const value_type* __s, size_type __sz) {
2037*81ad6265SDimitry Andric  if (__libcpp_is_constant_evaluated())
2038*81ad6265SDimitry Andric    __zero();
20395ffd83dbSDimitry Andric  pointer __p;
204004eeddc0SDimitry Andric  if (__fits_in_sso(__sz)) {
20415ffd83dbSDimitry Andric    __p = __get_short_pointer();
20425ffd83dbSDimitry Andric    __set_short_size(__sz);
20435ffd83dbSDimitry Andric  } else {
20445ffd83dbSDimitry Andric    if (__sz > max_size())
204504eeddc0SDimitry Andric      __throw_length_error();
2046*81ad6265SDimitry Andric    auto __allocation = std::__allocate_at_least(__alloc(), __recommend(__sz) + 1);
2047*81ad6265SDimitry Andric    __p = __allocation.ptr;
2048*81ad6265SDimitry Andric    __begin_lifetime(__p, __allocation.count);
20495ffd83dbSDimitry Andric    __set_long_pointer(__p);
2050*81ad6265SDimitry Andric    __set_long_cap(__allocation.count);
20515ffd83dbSDimitry Andric    __set_long_size(__sz);
20525ffd83dbSDimitry Andric  }
2053*81ad6265SDimitry Andric  traits_type::copy(std::__to_address(__p), __s, __sz + 1);
20545ffd83dbSDimitry Andric}
20555ffd83dbSDimitry Andric
20560b57cec5SDimitry Andric#ifndef _LIBCPP_CXX03_LANG
20570b57cec5SDimitry Andric
20580b57cec5SDimitry Andrictemplate <class _CharT, class _Traits, class _Allocator>
2059*81ad6265SDimitry Andricinline _LIBCPP_CONSTEXPR_AFTER_CXX17
20600b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>::basic_string(basic_string&& __str)
20610b57cec5SDimitry Andric#if _LIBCPP_STD_VER <= 14
20620b57cec5SDimitry Andric        _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
20630b57cec5SDimitry Andric#else
20640b57cec5SDimitry Andric        _NOEXCEPT
20650b57cec5SDimitry Andric#endif
2066*81ad6265SDimitry Andric    : __r_(std::move(__str.__r_))
20670b57cec5SDimitry Andric{
2068*81ad6265SDimitry Andric    __str.__default_init();
2069*81ad6265SDimitry Andric    std::__debug_db_insert_c(this);
2070*81ad6265SDimitry Andric    if (__is_long())
2071*81ad6265SDimitry Andric        std::__debug_db_swap(this, &__str);
20720b57cec5SDimitry Andric}
20730b57cec5SDimitry Andric
20740b57cec5SDimitry Andrictemplate <class _CharT, class _Traits, class _Allocator>
2075*81ad6265SDimitry Andricinline _LIBCPP_CONSTEXPR_AFTER_CXX17
20760b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>::basic_string(basic_string&& __str, const allocator_type& __a)
2077480093f4SDimitry Andric    : __r_(__default_init_tag(), __a)
20780b57cec5SDimitry Andric{
20790b57cec5SDimitry Andric    if (__str.__is_long() && __a != __str.__alloc()) // copy, not move
2080*81ad6265SDimitry Andric        __init(std::__to_address(__str.__get_long_pointer()), __str.__get_long_size());
20810b57cec5SDimitry Andric    else
20820b57cec5SDimitry Andric    {
2083*81ad6265SDimitry Andric        if (__libcpp_is_constant_evaluated()) {
2084*81ad6265SDimitry Andric            __zero();
2085*81ad6265SDimitry Andric            __r_.first().__l = __str.__r_.first().__l;
2086*81ad6265SDimitry Andric        } else {
20870b57cec5SDimitry Andric            __r_.first().__r = __str.__r_.first().__r;
20880b57cec5SDimitry Andric        }
2089*81ad6265SDimitry Andric        __str.__default_init();
2090*81ad6265SDimitry Andric    }
2091*81ad6265SDimitry Andric    std::__debug_db_insert_c(this);
2092*81ad6265SDimitry Andric    if (__is_long())
2093*81ad6265SDimitry Andric        std::__debug_db_swap(this, &__str);
20940b57cec5SDimitry Andric}
20950b57cec5SDimitry Andric
20960b57cec5SDimitry Andric#endif // _LIBCPP_CXX03_LANG
20970b57cec5SDimitry Andric
20980b57cec5SDimitry Andrictemplate <class _CharT, class _Traits, class _Allocator>
2099*81ad6265SDimitry Andric_LIBCPP_CONSTEXPR_AFTER_CXX17
21000b57cec5SDimitry Andricvoid
21010b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>::__init(size_type __n, value_type __c)
21020b57cec5SDimitry Andric{
2103*81ad6265SDimitry Andric    if (__libcpp_is_constant_evaluated())
2104*81ad6265SDimitry Andric        __zero();
21050b57cec5SDimitry Andric    if (__n > max_size())
210604eeddc0SDimitry Andric        __throw_length_error();
21070b57cec5SDimitry Andric    pointer __p;
210804eeddc0SDimitry Andric    if (__fits_in_sso(__n))
21090b57cec5SDimitry Andric    {
21100b57cec5SDimitry Andric        __set_short_size(__n);
21110b57cec5SDimitry Andric        __p = __get_short_pointer();
21120b57cec5SDimitry Andric    }
21130b57cec5SDimitry Andric    else
21140b57cec5SDimitry Andric    {
2115*81ad6265SDimitry Andric        auto __allocation = std::__allocate_at_least(__alloc(), __recommend(__n) + 1);
2116*81ad6265SDimitry Andric        __p = __allocation.ptr;
2117*81ad6265SDimitry Andric        __begin_lifetime(__p, __allocation.count);
21180b57cec5SDimitry Andric        __set_long_pointer(__p);
2119*81ad6265SDimitry Andric        __set_long_cap(__allocation.count);
21200b57cec5SDimitry Andric        __set_long_size(__n);
21210b57cec5SDimitry Andric    }
2122*81ad6265SDimitry Andric    traits_type::assign(std::__to_address(__p), __n, __c);
21230b57cec5SDimitry Andric    traits_type::assign(__p[__n], value_type());
21240b57cec5SDimitry Andric}
21250b57cec5SDimitry Andric
21260b57cec5SDimitry Andrictemplate <class _CharT, class _Traits, class _Allocator>
2127*81ad6265SDimitry Andricinline _LIBCPP_CONSTEXPR_AFTER_CXX17
21280b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>::basic_string(size_type __n, _CharT __c)
2129480093f4SDimitry Andric     : __r_(__default_init_tag(), __default_init_tag())
21300b57cec5SDimitry Andric{
21310b57cec5SDimitry Andric    __init(__n, __c);
2132*81ad6265SDimitry Andric    std::__debug_db_insert_c(this);
21330b57cec5SDimitry Andric}
21340b57cec5SDimitry Andric
21350b57cec5SDimitry Andrictemplate <class _CharT, class _Traits, class _Allocator>
21360b57cec5SDimitry Andrictemplate <class>
2137*81ad6265SDimitry Andric_LIBCPP_CONSTEXPR_AFTER_CXX17
21380b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>::basic_string(size_type __n, _CharT __c, const _Allocator& __a)
2139480093f4SDimitry Andric    : __r_(__default_init_tag(), __a)
21400b57cec5SDimitry Andric{
21410b57cec5SDimitry Andric    __init(__n, __c);
2142*81ad6265SDimitry Andric    std::__debug_db_insert_c(this);
21430b57cec5SDimitry Andric}
21440b57cec5SDimitry Andric
21450b57cec5SDimitry Andrictemplate <class _CharT, class _Traits, class _Allocator>
2146*81ad6265SDimitry Andric_LIBCPP_CONSTEXPR_AFTER_CXX17
21470b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str,
21480b57cec5SDimitry Andric                                                        size_type __pos, size_type __n,
21490b57cec5SDimitry Andric                                                        const _Allocator& __a)
2150480093f4SDimitry Andric    : __r_(__default_init_tag(), __a)
21510b57cec5SDimitry Andric{
21520b57cec5SDimitry Andric    size_type __str_sz = __str.size();
21530b57cec5SDimitry Andric    if (__pos > __str_sz)
215404eeddc0SDimitry Andric        __throw_out_of_range();
2155*81ad6265SDimitry Andric    __init(__str.data() + __pos, std::min(__n, __str_sz - __pos));
2156*81ad6265SDimitry Andric    std::__debug_db_insert_c(this);
21570b57cec5SDimitry Andric}
21580b57cec5SDimitry Andric
21590b57cec5SDimitry Andrictemplate <class _CharT, class _Traits, class _Allocator>
2160*81ad6265SDimitry Andricinline _LIBCPP_CONSTEXPR_AFTER_CXX17
21610b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str, size_type __pos,
21620b57cec5SDimitry Andric                                                        const _Allocator& __a)
2163480093f4SDimitry Andric    : __r_(__default_init_tag(), __a)
21640b57cec5SDimitry Andric{
21650b57cec5SDimitry Andric    size_type __str_sz = __str.size();
21660b57cec5SDimitry Andric    if (__pos > __str_sz)
216704eeddc0SDimitry Andric        __throw_out_of_range();
21680b57cec5SDimitry Andric    __init(__str.data() + __pos, __str_sz - __pos);
2169*81ad6265SDimitry Andric    std::__debug_db_insert_c(this);
21700b57cec5SDimitry Andric}
21710b57cec5SDimitry Andric
21720b57cec5SDimitry Andrictemplate <class _CharT, class _Traits, class _Allocator>
21730b57cec5SDimitry Andrictemplate <class _Tp, class>
2174*81ad6265SDimitry Andric_LIBCPP_CONSTEXPR_AFTER_CXX17
21750b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>::basic_string(
21760b57cec5SDimitry Andric             const _Tp& __t, size_type __pos, size_type __n, const allocator_type& __a)
2177480093f4SDimitry Andric    : __r_(__default_init_tag(), __a)
21780b57cec5SDimitry Andric{
21790b57cec5SDimitry Andric    __self_view __sv0 = __t;
21800b57cec5SDimitry Andric    __self_view __sv = __sv0.substr(__pos, __n);
21810b57cec5SDimitry Andric    __init(__sv.data(), __sv.size());
2182*81ad6265SDimitry Andric    std::__debug_db_insert_c(this);
21830b57cec5SDimitry Andric}
21840b57cec5SDimitry Andric
21850b57cec5SDimitry Andrictemplate <class _CharT, class _Traits, class _Allocator>
21860b57cec5SDimitry Andrictemplate <class _Tp, class>
2187*81ad6265SDimitry Andric_LIBCPP_CONSTEXPR_AFTER_CXX17
21880b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>::basic_string(const _Tp & __t)
2189480093f4SDimitry Andric     : __r_(__default_init_tag(), __default_init_tag())
21900b57cec5SDimitry Andric{
21910b57cec5SDimitry Andric    __self_view __sv = __t;
21920b57cec5SDimitry Andric    __init(__sv.data(), __sv.size());
2193*81ad6265SDimitry Andric    std::__debug_db_insert_c(this);
21940b57cec5SDimitry Andric}
21950b57cec5SDimitry Andric
21960b57cec5SDimitry Andrictemplate <class _CharT, class _Traits, class _Allocator>
21970b57cec5SDimitry Andrictemplate <class _Tp, class>
2198*81ad6265SDimitry Andric_LIBCPP_CONSTEXPR_AFTER_CXX17
21990b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>::basic_string(const _Tp & __t, const _Allocator& __a)
2200480093f4SDimitry Andric    : __r_(__default_init_tag(), __a)
22010b57cec5SDimitry Andric{
22020b57cec5SDimitry Andric    __self_view __sv = __t;
22030b57cec5SDimitry Andric    __init(__sv.data(), __sv.size());
2204*81ad6265SDimitry Andric    std::__debug_db_insert_c(this);
22050b57cec5SDimitry Andric}
22060b57cec5SDimitry Andric
22070b57cec5SDimitry Andrictemplate <class _CharT, class _Traits, class _Allocator>
22080b57cec5SDimitry Andrictemplate <class _InputIterator>
2209*81ad6265SDimitry Andric_LIBCPP_CONSTEXPR_AFTER_CXX17
2210349cc55cSDimitry Andric__enable_if_t
22110b57cec5SDimitry Andric<
22125ffd83dbSDimitry Andric    __is_exactly_cpp17_input_iterator<_InputIterator>::value
22135ffd83dbSDimitry Andric>
22140b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>::__init(_InputIterator __first, _InputIterator __last)
22150b57cec5SDimitry Andric{
2216*81ad6265SDimitry Andric    __default_init();
22170b57cec5SDimitry Andric#ifndef _LIBCPP_NO_EXCEPTIONS
22180b57cec5SDimitry Andric    try
22190b57cec5SDimitry Andric    {
22200b57cec5SDimitry Andric#endif // _LIBCPP_NO_EXCEPTIONS
22210b57cec5SDimitry Andric    for (; __first != __last; ++__first)
22220b57cec5SDimitry Andric        push_back(*__first);
22230b57cec5SDimitry Andric#ifndef _LIBCPP_NO_EXCEPTIONS
22240b57cec5SDimitry Andric    }
22250b57cec5SDimitry Andric    catch (...)
22260b57cec5SDimitry Andric    {
22270b57cec5SDimitry Andric        if (__is_long())
22280b57cec5SDimitry Andric            __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
22290b57cec5SDimitry Andric        throw;
22300b57cec5SDimitry Andric    }
22310b57cec5SDimitry Andric#endif // _LIBCPP_NO_EXCEPTIONS
22320b57cec5SDimitry Andric}
22330b57cec5SDimitry Andric
22340b57cec5SDimitry Andrictemplate <class _CharT, class _Traits, class _Allocator>
22350b57cec5SDimitry Andrictemplate <class _ForwardIterator>
2236*81ad6265SDimitry Andric_LIBCPP_CONSTEXPR_AFTER_CXX17
2237349cc55cSDimitry Andric__enable_if_t
22380b57cec5SDimitry Andric<
22395ffd83dbSDimitry Andric    __is_cpp17_forward_iterator<_ForwardIterator>::value
22405ffd83dbSDimitry Andric>
22410b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>::__init(_ForwardIterator __first, _ForwardIterator __last)
22420b57cec5SDimitry Andric{
2243*81ad6265SDimitry Andric    if (__libcpp_is_constant_evaluated())
2244*81ad6265SDimitry Andric        __zero();
2245*81ad6265SDimitry Andric    size_type __sz = static_cast<size_type>(std::distance(__first, __last));
22460b57cec5SDimitry Andric    if (__sz > max_size())
224704eeddc0SDimitry Andric        __throw_length_error();
22480b57cec5SDimitry Andric    pointer __p;
224904eeddc0SDimitry Andric    if (__fits_in_sso(__sz))
22500b57cec5SDimitry Andric    {
22510b57cec5SDimitry Andric        __set_short_size(__sz);
22520b57cec5SDimitry Andric        __p = __get_short_pointer();
22530b57cec5SDimitry Andric    }
22540b57cec5SDimitry Andric    else
22550b57cec5SDimitry Andric    {
2256*81ad6265SDimitry Andric        auto __allocation = std::__allocate_at_least(__alloc(), __recommend(__sz) + 1);
2257*81ad6265SDimitry Andric        __p = __allocation.ptr;
2258*81ad6265SDimitry Andric        __begin_lifetime(__p, __allocation.count);
22590b57cec5SDimitry Andric        __set_long_pointer(__p);
2260*81ad6265SDimitry Andric        __set_long_cap(__allocation.count);
22610b57cec5SDimitry Andric        __set_long_size(__sz);
22620b57cec5SDimitry Andric    }
2263fe6060f1SDimitry Andric
2264fe6060f1SDimitry Andric#ifndef _LIBCPP_NO_EXCEPTIONS
2265fe6060f1SDimitry Andric    try
2266fe6060f1SDimitry Andric    {
2267fe6060f1SDimitry Andric#endif  // _LIBCPP_NO_EXCEPTIONS
22680b57cec5SDimitry Andric    for (; __first != __last; ++__first, (void) ++__p)
22690b57cec5SDimitry Andric        traits_type::assign(*__p, *__first);
22700b57cec5SDimitry Andric    traits_type::assign(*__p, value_type());
2271fe6060f1SDimitry Andric#ifndef _LIBCPP_NO_EXCEPTIONS
2272fe6060f1SDimitry Andric    }
2273fe6060f1SDimitry Andric    catch (...)
2274fe6060f1SDimitry Andric    {
2275fe6060f1SDimitry Andric        if (__is_long())
2276fe6060f1SDimitry Andric            __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
2277fe6060f1SDimitry Andric        throw;
2278fe6060f1SDimitry Andric    }
2279fe6060f1SDimitry Andric#endif  // _LIBCPP_NO_EXCEPTIONS
22800b57cec5SDimitry Andric}
22810b57cec5SDimitry Andric
22820b57cec5SDimitry Andrictemplate <class _CharT, class _Traits, class _Allocator>
22830b57cec5SDimitry Andrictemplate<class _InputIterator, class>
2284*81ad6265SDimitry Andricinline _LIBCPP_CONSTEXPR_AFTER_CXX17
22850b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>::basic_string(_InputIterator __first, _InputIterator __last)
2286480093f4SDimitry Andric     : __r_(__default_init_tag(), __default_init_tag())
22870b57cec5SDimitry Andric{
22880b57cec5SDimitry Andric    __init(__first, __last);
2289*81ad6265SDimitry Andric    std::__debug_db_insert_c(this);
22900b57cec5SDimitry Andric}
22910b57cec5SDimitry Andric
22920b57cec5SDimitry Andrictemplate <class _CharT, class _Traits, class _Allocator>
22930b57cec5SDimitry Andrictemplate<class _InputIterator, class>
2294*81ad6265SDimitry Andricinline _LIBCPP_CONSTEXPR_AFTER_CXX17
22950b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>::basic_string(_InputIterator __first, _InputIterator __last,
22960b57cec5SDimitry Andric                                                        const allocator_type& __a)
2297480093f4SDimitry Andric    : __r_(__default_init_tag(), __a)
22980b57cec5SDimitry Andric{
22990b57cec5SDimitry Andric    __init(__first, __last);
2300*81ad6265SDimitry Andric    std::__debug_db_insert_c(this);
23010b57cec5SDimitry Andric}
23020b57cec5SDimitry Andric
23030b57cec5SDimitry Andric#ifndef _LIBCPP_CXX03_LANG
23040b57cec5SDimitry Andric
23050b57cec5SDimitry Andrictemplate <class _CharT, class _Traits, class _Allocator>
2306*81ad6265SDimitry Andricinline _LIBCPP_CONSTEXPR_AFTER_CXX17
23070b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>::basic_string(
23080b57cec5SDimitry Andric    initializer_list<_CharT> __il)
2309480093f4SDimitry Andric     : __r_(__default_init_tag(), __default_init_tag())
23100b57cec5SDimitry Andric{
23110b57cec5SDimitry Andric    __init(__il.begin(), __il.end());
2312*81ad6265SDimitry Andric    std::__debug_db_insert_c(this);
23130b57cec5SDimitry Andric}
23140b57cec5SDimitry Andric
23150b57cec5SDimitry Andrictemplate <class _CharT, class _Traits, class _Allocator>
2316*81ad6265SDimitry Andricinline _LIBCPP_CONSTEXPR_AFTER_CXX17
23170b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>::basic_string(
23180b57cec5SDimitry Andric    initializer_list<_CharT> __il, const _Allocator& __a)
2319480093f4SDimitry Andric    : __r_(__default_init_tag(), __a)
23200b57cec5SDimitry Andric{
23210b57cec5SDimitry Andric    __init(__il.begin(), __il.end());
2322*81ad6265SDimitry Andric    std::__debug_db_insert_c(this);
23230b57cec5SDimitry Andric}
23240b57cec5SDimitry Andric
23250b57cec5SDimitry Andric#endif // _LIBCPP_CXX03_LANG
23260b57cec5SDimitry Andric
23270b57cec5SDimitry Andrictemplate <class _CharT, class _Traits, class _Allocator>
2328*81ad6265SDimitry Andric_LIBCPP_CONSTEXPR_AFTER_CXX17
23290b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>::~basic_string()
23300b57cec5SDimitry Andric{
2331*81ad6265SDimitry Andric    std::__debug_db_erase_c(this);
23320b57cec5SDimitry Andric    if (__is_long())
23330b57cec5SDimitry Andric        __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
23340b57cec5SDimitry Andric}
23350b57cec5SDimitry Andric
23360b57cec5SDimitry Andrictemplate <class _CharT, class _Traits, class _Allocator>
2337*81ad6265SDimitry Andric_LIBCPP_CONSTEXPR_AFTER_CXX17
23380b57cec5SDimitry Andricvoid
23390b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>::__grow_by_and_replace
23400b57cec5SDimitry Andric    (size_type __old_cap, size_type __delta_cap, size_type __old_sz,
23410b57cec5SDimitry Andric     size_type __n_copy,  size_type __n_del,     size_type __n_add, const value_type* __p_new_stuff)
23420b57cec5SDimitry Andric{
23430b57cec5SDimitry Andric    size_type __ms = max_size();
23440b57cec5SDimitry Andric    if (__delta_cap > __ms - __old_cap - 1)
234504eeddc0SDimitry Andric        __throw_length_error();
23460b57cec5SDimitry Andric    pointer __old_p = __get_pointer();
23470b57cec5SDimitry Andric    size_type __cap = __old_cap < __ms / 2 - __alignment ?
2348*81ad6265SDimitry Andric                          __recommend(std::max(__old_cap + __delta_cap, 2 * __old_cap)) :
23490b57cec5SDimitry Andric                          __ms - 1;
2350*81ad6265SDimitry Andric    auto __allocation = std::__allocate_at_least(__alloc(), __cap + 1);
2351*81ad6265SDimitry Andric    pointer __p = __allocation.ptr;
2352*81ad6265SDimitry Andric    __begin_lifetime(__p, __allocation.count);
2353*81ad6265SDimitry Andric    std::__debug_db_invalidate_all(this);
23540b57cec5SDimitry Andric    if (__n_copy != 0)
2355*81ad6265SDimitry Andric        traits_type::copy(std::__to_address(__p),
2356*81ad6265SDimitry Andric                          std::__to_address(__old_p), __n_copy);
23570b57cec5SDimitry Andric    if (__n_add != 0)
2358*81ad6265SDimitry Andric        traits_type::copy(std::__to_address(__p) + __n_copy, __p_new_stuff, __n_add);
23590b57cec5SDimitry Andric    size_type __sec_cp_sz = __old_sz - __n_del - __n_copy;
23600b57cec5SDimitry Andric    if (__sec_cp_sz != 0)
2361*81ad6265SDimitry Andric        traits_type::copy(std::__to_address(__p) + __n_copy + __n_add,
2362*81ad6265SDimitry Andric                          std::__to_address(__old_p) + __n_copy + __n_del, __sec_cp_sz);
2363*81ad6265SDimitry Andric    if (__old_cap+1 != __min_cap || __libcpp_is_constant_evaluated())
23640b57cec5SDimitry Andric        __alloc_traits::deallocate(__alloc(), __old_p, __old_cap+1);
23650b57cec5SDimitry Andric    __set_long_pointer(__p);
2366*81ad6265SDimitry Andric    __set_long_cap(__allocation.count);
23670b57cec5SDimitry Andric    __old_sz = __n_copy + __n_add + __sec_cp_sz;
23680b57cec5SDimitry Andric    __set_long_size(__old_sz);
23690b57cec5SDimitry Andric    traits_type::assign(__p[__old_sz], value_type());
23700b57cec5SDimitry Andric}
23710b57cec5SDimitry Andric
23720b57cec5SDimitry Andrictemplate <class _CharT, class _Traits, class _Allocator>
23730b57cec5SDimitry Andricvoid
2374*81ad6265SDimitry Andric_LIBCPP_CONSTEXPR_AFTER_CXX17
23750b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>::__grow_by(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
23760b57cec5SDimitry Andric                                                     size_type __n_copy,  size_type __n_del,     size_type __n_add)
23770b57cec5SDimitry Andric{
23780b57cec5SDimitry Andric    size_type __ms = max_size();
23790b57cec5SDimitry Andric    if (__delta_cap > __ms - __old_cap)
238004eeddc0SDimitry Andric        __throw_length_error();
23810b57cec5SDimitry Andric    pointer __old_p = __get_pointer();
23820b57cec5SDimitry Andric    size_type __cap = __old_cap < __ms / 2 - __alignment ?
2383*81ad6265SDimitry Andric                          __recommend(std::max(__old_cap + __delta_cap, 2 * __old_cap)) :
23840b57cec5SDimitry Andric                          __ms - 1;
2385*81ad6265SDimitry Andric    auto __allocation = std::__allocate_at_least(__alloc(), __cap + 1);
2386*81ad6265SDimitry Andric    pointer __p = __allocation.ptr;
2387*81ad6265SDimitry Andric    __begin_lifetime(__p, __allocation.count);
2388*81ad6265SDimitry Andric    std::__debug_db_invalidate_all(this);
23890b57cec5SDimitry Andric    if (__n_copy != 0)
2390*81ad6265SDimitry Andric        traits_type::copy(std::__to_address(__p),
2391*81ad6265SDimitry Andric                          std::__to_address(__old_p), __n_copy);
23920b57cec5SDimitry Andric    size_type __sec_cp_sz = __old_sz - __n_del - __n_copy;
23930b57cec5SDimitry Andric    if (__sec_cp_sz != 0)
2394*81ad6265SDimitry Andric        traits_type::copy(std::__to_address(__p) + __n_copy + __n_add,
2395*81ad6265SDimitry Andric                          std::__to_address(__old_p) + __n_copy + __n_del,
23960b57cec5SDimitry Andric                          __sec_cp_sz);
2397*81ad6265SDimitry Andric    if (__libcpp_is_constant_evaluated() || __old_cap + 1 != __min_cap)
23980b57cec5SDimitry Andric        __alloc_traits::deallocate(__alloc(), __old_p, __old_cap + 1);
23990b57cec5SDimitry Andric    __set_long_pointer(__p);
2400*81ad6265SDimitry Andric    __set_long_cap(__allocation.count);
24010b57cec5SDimitry Andric}
24020b57cec5SDimitry Andric
24030b57cec5SDimitry Andric// assign
24040b57cec5SDimitry Andric
24050b57cec5SDimitry Andrictemplate <class _CharT, class _Traits, class _Allocator>
24065ffd83dbSDimitry Andrictemplate <bool __is_short>
2407*81ad6265SDimitry Andric_LIBCPP_CONSTEXPR_AFTER_CXX17
24080b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>&
24095ffd83dbSDimitry Andricbasic_string<_CharT, _Traits, _Allocator>::__assign_no_alias(
24105ffd83dbSDimitry Andric    const value_type* __s, size_type __n) {
2411*81ad6265SDimitry Andric  size_type __cap = __is_short ? static_cast<size_type>(__min_cap) : __get_long_cap();
24125ffd83dbSDimitry Andric  if (__n < __cap) {
24135ffd83dbSDimitry Andric    pointer __p = __is_short ? __get_short_pointer() : __get_long_pointer();
24145ffd83dbSDimitry Andric    __is_short ? __set_short_size(__n) : __set_long_size(__n);
2415*81ad6265SDimitry Andric    traits_type::copy(std::__to_address(__p), __s, __n);
24165ffd83dbSDimitry Andric    traits_type::assign(__p[__n], value_type());
24175ffd83dbSDimitry Andric    __invalidate_iterators_past(__n);
24185ffd83dbSDimitry Andric  } else {
24195ffd83dbSDimitry Andric    size_type __sz = __is_short ? __get_short_size() : __get_long_size();
24205ffd83dbSDimitry Andric    __grow_by_and_replace(__cap - 1, __n - __cap + 1, __sz, 0, __sz, __n, __s);
24215ffd83dbSDimitry Andric  }
24225ffd83dbSDimitry Andric  return *this;
24235ffd83dbSDimitry Andric}
24245ffd83dbSDimitry Andric
24255ffd83dbSDimitry Andrictemplate <class _CharT, class _Traits, class _Allocator>
2426*81ad6265SDimitry Andric_LIBCPP_CONSTEXPR_AFTER_CXX17
24275ffd83dbSDimitry Andricbasic_string<_CharT, _Traits, _Allocator>&
24285ffd83dbSDimitry Andricbasic_string<_CharT, _Traits, _Allocator>::__assign_external(
24295ffd83dbSDimitry Andric    const value_type* __s, size_type __n) {
24300b57cec5SDimitry Andric  size_type __cap = capacity();
24315ffd83dbSDimitry Andric  if (__cap >= __n) {
2432*81ad6265SDimitry Andric    value_type* __p = std::__to_address(__get_pointer());
24330b57cec5SDimitry Andric    traits_type::move(__p, __s, __n);
24340eae32dcSDimitry Andric    return __null_terminate_at(__p, __n);
24355ffd83dbSDimitry Andric  } else {
24360b57cec5SDimitry Andric    size_type __sz = size();
24370b57cec5SDimitry Andric    __grow_by_and_replace(__cap, __n - __cap, __sz, 0, __sz, __n, __s);
24380b57cec5SDimitry Andric    return *this;
24390b57cec5SDimitry Andric  }
24400eae32dcSDimitry Andric}
24410b57cec5SDimitry Andric
24420b57cec5SDimitry Andrictemplate <class _CharT, class _Traits, class _Allocator>
2443*81ad6265SDimitry Andric_LIBCPP_CONSTEXPR_AFTER_CXX17
24440b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>&
24455ffd83dbSDimitry Andricbasic_string<_CharT, _Traits, _Allocator>::assign(const value_type* __s, size_type __n)
24465ffd83dbSDimitry Andric{
24475ffd83dbSDimitry Andric    _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::assign received nullptr");
244804eeddc0SDimitry Andric    return (__builtin_constant_p(__n) && __fits_in_sso(__n))
24495ffd83dbSDimitry Andric               ? __assign_short(__s, __n)
24505ffd83dbSDimitry Andric               : __assign_external(__s, __n);
24515ffd83dbSDimitry Andric}
24525ffd83dbSDimitry Andric
24535ffd83dbSDimitry Andrictemplate <class _CharT, class _Traits, class _Allocator>
2454*81ad6265SDimitry Andric_LIBCPP_CONSTEXPR_AFTER_CXX17
24555ffd83dbSDimitry Andricbasic_string<_CharT, _Traits, _Allocator>&
24560b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>::assign(size_type __n, value_type __c)
24570b57cec5SDimitry Andric{
24580b57cec5SDimitry Andric    size_type __cap = capacity();
24590b57cec5SDimitry Andric    if (__cap < __n)
24600b57cec5SDimitry Andric    {
24610b57cec5SDimitry Andric        size_type __sz = size();
24620b57cec5SDimitry Andric        __grow_by(__cap, __n - __cap, __sz, 0, __sz);
24630b57cec5SDimitry Andric    }
2464*81ad6265SDimitry Andric    value_type* __p = std::__to_address(__get_pointer());
24650b57cec5SDimitry Andric    traits_type::assign(__p, __n, __c);
24660eae32dcSDimitry Andric    return __null_terminate_at(__p, __n);
24670b57cec5SDimitry Andric}
24680b57cec5SDimitry Andric
24690b57cec5SDimitry Andrictemplate <class _CharT, class _Traits, class _Allocator>
2470*81ad6265SDimitry Andric_LIBCPP_CONSTEXPR_AFTER_CXX17
24710b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>&
24720b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>::operator=(value_type __c)
24730b57cec5SDimitry Andric{
24740b57cec5SDimitry Andric    pointer __p;
24750b57cec5SDimitry Andric    if (__is_long())
24760b57cec5SDimitry Andric    {
24770b57cec5SDimitry Andric        __p = __get_long_pointer();
24780b57cec5SDimitry Andric        __set_long_size(1);
24790b57cec5SDimitry Andric    }
24800b57cec5SDimitry Andric    else
24810b57cec5SDimitry Andric    {
24820b57cec5SDimitry Andric        __p = __get_short_pointer();
24830b57cec5SDimitry Andric        __set_short_size(1);
24840b57cec5SDimitry Andric    }
24850b57cec5SDimitry Andric    traits_type::assign(*__p, __c);
24860b57cec5SDimitry Andric    traits_type::assign(*++__p, value_type());
24870b57cec5SDimitry Andric    __invalidate_iterators_past(1);
24880b57cec5SDimitry Andric    return *this;
24890b57cec5SDimitry Andric}
24900b57cec5SDimitry Andric
24910b57cec5SDimitry Andrictemplate <class _CharT, class _Traits, class _Allocator>
2492*81ad6265SDimitry Andric_LIBCPP_CONSTEXPR_AFTER_CXX17
24930b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>&
24940b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>::operator=(const basic_string& __str)
24950b57cec5SDimitry Andric{
24965ffd83dbSDimitry Andric  if (this != &__str) {
24970b57cec5SDimitry Andric    __copy_assign_alloc(__str);
24985ffd83dbSDimitry Andric    if (!__is_long()) {
24995ffd83dbSDimitry Andric      if (!__str.__is_long()) {
25005ffd83dbSDimitry Andric        __r_.first().__r = __str.__r_.first().__r;
25015ffd83dbSDimitry Andric      } else {
25025ffd83dbSDimitry Andric        return __assign_no_alias<true>(__str.data(), __str.size());
25035ffd83dbSDimitry Andric      }
25045ffd83dbSDimitry Andric    } else {
25055ffd83dbSDimitry Andric      return __assign_no_alias<false>(__str.data(), __str.size());
25065ffd83dbSDimitry Andric    }
25070b57cec5SDimitry Andric  }
25080b57cec5SDimitry Andric  return *this;
25090b57cec5SDimitry Andric}
25100b57cec5SDimitry Andric
25110b57cec5SDimitry Andric#ifndef _LIBCPP_CXX03_LANG
25120b57cec5SDimitry Andric
25130b57cec5SDimitry Andrictemplate <class _CharT, class _Traits, class _Allocator>
2514*81ad6265SDimitry Andricinline _LIBCPP_CONSTEXPR_AFTER_CXX17
25150b57cec5SDimitry Andricvoid
25160b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>::__move_assign(basic_string& __str, false_type)
25170b57cec5SDimitry Andric    _NOEXCEPT_(__alloc_traits::is_always_equal::value)
25180b57cec5SDimitry Andric{
25190b57cec5SDimitry Andric    if (__alloc() != __str.__alloc())
25200b57cec5SDimitry Andric        assign(__str);
25210b57cec5SDimitry Andric    else
25220b57cec5SDimitry Andric        __move_assign(__str, true_type());
25230b57cec5SDimitry Andric}
25240b57cec5SDimitry Andric
25250b57cec5SDimitry Andrictemplate <class _CharT, class _Traits, class _Allocator>
2526*81ad6265SDimitry Andricinline _LIBCPP_CONSTEXPR_AFTER_CXX17
25270b57cec5SDimitry Andricvoid
25280b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>::__move_assign(basic_string& __str, true_type)
25290b57cec5SDimitry Andric#if _LIBCPP_STD_VER > 14
25300b57cec5SDimitry Andric    _NOEXCEPT
25310b57cec5SDimitry Andric#else
25320b57cec5SDimitry Andric    _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
25330b57cec5SDimitry Andric#endif
25340b57cec5SDimitry Andric{
2535480093f4SDimitry Andric  if (__is_long()) {
2536480093f4SDimitry Andric    __alloc_traits::deallocate(__alloc(), __get_long_pointer(),
2537480093f4SDimitry Andric                               __get_long_cap());
2538480093f4SDimitry Andric#if _LIBCPP_STD_VER <= 14
2539480093f4SDimitry Andric    if (!is_nothrow_move_assignable<allocator_type>::value) {
2540480093f4SDimitry Andric      __set_short_size(0);
2541480093f4SDimitry Andric      traits_type::assign(__get_short_pointer()[0], value_type());
2542480093f4SDimitry Andric    }
2543480093f4SDimitry Andric#endif
2544480093f4SDimitry Andric  }
25450b57cec5SDimitry Andric  __move_assign_alloc(__str);
2546480093f4SDimitry Andric  __r_.first() = __str.__r_.first();
2547*81ad6265SDimitry Andric  if (__libcpp_is_constant_evaluated()) {
2548*81ad6265SDimitry Andric    __str.__default_init();
2549*81ad6265SDimitry Andric  } else {
2550480093f4SDimitry Andric    __str.__set_short_size(0);
2551480093f4SDimitry Andric    traits_type::assign(__str.__get_short_pointer()[0], value_type());
25520b57cec5SDimitry Andric  }
2553*81ad6265SDimitry Andric}
25540b57cec5SDimitry Andric
25550b57cec5SDimitry Andrictemplate <class _CharT, class _Traits, class _Allocator>
2556*81ad6265SDimitry Andricinline _LIBCPP_CONSTEXPR_AFTER_CXX17
25570b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>&
25580b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>::operator=(basic_string&& __str)
25590b57cec5SDimitry Andric    _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
25600b57cec5SDimitry Andric{
25610b57cec5SDimitry Andric    __move_assign(__str, integral_constant<bool,
25620b57cec5SDimitry Andric          __alloc_traits::propagate_on_container_move_assignment::value>());
25630b57cec5SDimitry Andric    return *this;
25640b57cec5SDimitry Andric}
25650b57cec5SDimitry Andric
25660b57cec5SDimitry Andric#endif
25670b57cec5SDimitry Andric
25680b57cec5SDimitry Andrictemplate <class _CharT, class _Traits, class _Allocator>
25690b57cec5SDimitry Andrictemplate<class _InputIterator>
2570*81ad6265SDimitry Andric_LIBCPP_CONSTEXPR_AFTER_CXX17
2571349cc55cSDimitry Andric__enable_if_t
25720b57cec5SDimitry Andric<
2573fe6060f1SDimitry Andric     __is_exactly_cpp17_input_iterator<_InputIterator>::value,
25740b57cec5SDimitry Andric    basic_string<_CharT, _Traits, _Allocator>&
25755ffd83dbSDimitry Andric>
25760b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
25770b57cec5SDimitry Andric{
25780b57cec5SDimitry Andric    const basic_string __temp(__first, __last, __alloc());
25790b57cec5SDimitry Andric    assign(__temp.data(), __temp.size());
25800b57cec5SDimitry Andric    return *this;
25810b57cec5SDimitry Andric}
25820b57cec5SDimitry Andric
25830b57cec5SDimitry Andrictemplate <class _CharT, class _Traits, class _Allocator>
25840b57cec5SDimitry Andrictemplate<class _ForwardIterator>
2585*81ad6265SDimitry Andric_LIBCPP_CONSTEXPR_AFTER_CXX17
2586349cc55cSDimitry Andric__enable_if_t
25870b57cec5SDimitry Andric<
2588fe6060f1SDimitry Andric    __is_cpp17_forward_iterator<_ForwardIterator>::value,
25890b57cec5SDimitry Andric    basic_string<_CharT, _Traits, _Allocator>&
25905ffd83dbSDimitry Andric>
25910b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
25920b57cec5SDimitry Andric{
25930b57cec5SDimitry Andric    size_type __cap = capacity();
2594fe6060f1SDimitry Andric    size_type __n = __string_is_trivial_iterator<_ForwardIterator>::value ?
2595*81ad6265SDimitry Andric        static_cast<size_type>(std::distance(__first, __last)) : 0;
2596fe6060f1SDimitry Andric
2597fe6060f1SDimitry Andric    if (__string_is_trivial_iterator<_ForwardIterator>::value &&
2598fe6060f1SDimitry Andric        (__cap >= __n || !__addr_in_range(*__first)))
2599fe6060f1SDimitry Andric    {
26000b57cec5SDimitry Andric        if (__cap < __n)
26010b57cec5SDimitry Andric        {
26020b57cec5SDimitry Andric            size_type __sz = size();
26030b57cec5SDimitry Andric            __grow_by(__cap, __n - __cap, __sz, 0, __sz);
26040b57cec5SDimitry Andric        }
26050b57cec5SDimitry Andric        pointer __p = __get_pointer();
2606349cc55cSDimitry Andric        for (; __first != __last; ++__p, (void) ++__first)
26070b57cec5SDimitry Andric            traits_type::assign(*__p, *__first);
26080b57cec5SDimitry Andric        traits_type::assign(*__p, value_type());
26090b57cec5SDimitry Andric        __set_size(__n);
2610fe6060f1SDimitry Andric        __invalidate_iterators_past(__n);
2611fe6060f1SDimitry Andric    }
2612fe6060f1SDimitry Andric    else
2613fe6060f1SDimitry Andric    {
2614fe6060f1SDimitry Andric        const basic_string __temp(__first, __last, __alloc());
2615fe6060f1SDimitry Andric        assign(__temp.data(), __temp.size());
2616fe6060f1SDimitry Andric    }
26170b57cec5SDimitry Andric    return *this;
26180b57cec5SDimitry Andric}
26190b57cec5SDimitry Andric
26200b57cec5SDimitry Andrictemplate <class _CharT, class _Traits, class _Allocator>
2621*81ad6265SDimitry Andric_LIBCPP_CONSTEXPR_AFTER_CXX17
26220b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>&
26230b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>::assign(const basic_string& __str, size_type __pos, size_type __n)
26240b57cec5SDimitry Andric{
26250b57cec5SDimitry Andric    size_type __sz = __str.size();
26260b57cec5SDimitry Andric    if (__pos > __sz)
262704eeddc0SDimitry Andric        __throw_out_of_range();
2628*81ad6265SDimitry Andric    return assign(__str.data() + __pos, std::min(__n, __sz - __pos));
26290b57cec5SDimitry Andric}
26300b57cec5SDimitry Andric
26310b57cec5SDimitry Andrictemplate <class _CharT, class _Traits, class _Allocator>
26320b57cec5SDimitry Andrictemplate <class _Tp>
2633*81ad6265SDimitry Andric_LIBCPP_CONSTEXPR_AFTER_CXX17
2634349cc55cSDimitry Andric__enable_if_t
26350b57cec5SDimitry Andric<
26365ffd83dbSDimitry Andric    __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
26375ffd83dbSDimitry Andric    && !__is_same_uncvref<_Tp, basic_string<_CharT, _Traits, _Allocator> >::value,
26380b57cec5SDimitry Andric    basic_string<_CharT, _Traits, _Allocator>&
26395ffd83dbSDimitry Andric>
26400b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>::assign(const _Tp & __t, size_type __pos, size_type __n)
26410b57cec5SDimitry Andric{
26420b57cec5SDimitry Andric    __self_view __sv = __t;
26430b57cec5SDimitry Andric    size_type __sz = __sv.size();
26440b57cec5SDimitry Andric    if (__pos > __sz)
264504eeddc0SDimitry Andric        __throw_out_of_range();
2646*81ad6265SDimitry Andric    return assign(__sv.data() + __pos, std::min(__n, __sz - __pos));
26470b57cec5SDimitry Andric}
26480b57cec5SDimitry Andric
26490b57cec5SDimitry Andric
26500b57cec5SDimitry Andrictemplate <class _CharT, class _Traits, class _Allocator>
2651*81ad6265SDimitry Andric_LIBCPP_CONSTEXPR_AFTER_CXX17
26520b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>&
26535ffd83dbSDimitry Andricbasic_string<_CharT, _Traits, _Allocator>::__assign_external(const value_type* __s) {
26545ffd83dbSDimitry Andric  return __assign_external(__s, traits_type::length(__s));
26555ffd83dbSDimitry Andric}
26565ffd83dbSDimitry Andric
26575ffd83dbSDimitry Andrictemplate <class _CharT, class _Traits, class _Allocator>
2658*81ad6265SDimitry Andric_LIBCPP_CONSTEXPR_AFTER_CXX17
26595ffd83dbSDimitry Andricbasic_string<_CharT, _Traits, _Allocator>&
26600b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>::assign(const value_type* __s)
26610b57cec5SDimitry Andric{
26620b57cec5SDimitry Andric    _LIBCPP_ASSERT(__s != nullptr, "string::assign received nullptr");
2663349cc55cSDimitry Andric    return __builtin_constant_p(*__s)
266404eeddc0SDimitry Andric               ? (__fits_in_sso(traits_type::length(__s))
26655ffd83dbSDimitry Andric                      ? __assign_short(__s, traits_type::length(__s))
26665ffd83dbSDimitry Andric                      : __assign_external(__s, traits_type::length(__s)))
26675ffd83dbSDimitry Andric               : __assign_external(__s);
26680b57cec5SDimitry Andric}
26690b57cec5SDimitry Andric// append
26700b57cec5SDimitry Andric
26710b57cec5SDimitry Andrictemplate <class _CharT, class _Traits, class _Allocator>
2672*81ad6265SDimitry Andric_LIBCPP_CONSTEXPR_AFTER_CXX17
26730b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>&
26740b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>::append(const value_type* __s, size_type __n)
26750b57cec5SDimitry Andric{
26760b57cec5SDimitry Andric    _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::append received nullptr");
26770b57cec5SDimitry Andric    size_type __cap = capacity();
26780b57cec5SDimitry Andric    size_type __sz = size();
26790b57cec5SDimitry Andric    if (__cap - __sz >= __n)
26800b57cec5SDimitry Andric    {
26810b57cec5SDimitry Andric        if (__n)
26820b57cec5SDimitry Andric        {
2683*81ad6265SDimitry Andric            value_type* __p = std::__to_address(__get_pointer());
26840b57cec5SDimitry Andric            traits_type::copy(__p + __sz, __s, __n);
26850b57cec5SDimitry Andric            __sz += __n;
26860b57cec5SDimitry Andric            __set_size(__sz);
26870b57cec5SDimitry Andric            traits_type::assign(__p[__sz], value_type());
26880b57cec5SDimitry Andric        }
26890b57cec5SDimitry Andric    }
26900b57cec5SDimitry Andric    else
26910b57cec5SDimitry Andric        __grow_by_and_replace(__cap, __sz + __n - __cap, __sz, __sz, 0, __n, __s);
26920b57cec5SDimitry Andric    return *this;
26930b57cec5SDimitry Andric}
26940b57cec5SDimitry Andric
26950b57cec5SDimitry Andrictemplate <class _CharT, class _Traits, class _Allocator>
2696*81ad6265SDimitry Andric_LIBCPP_CONSTEXPR_AFTER_CXX17
26970b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>&
26980b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>::append(size_type __n, value_type __c)
26990b57cec5SDimitry Andric{
27000b57cec5SDimitry Andric    if (__n)
27010b57cec5SDimitry Andric    {
27020b57cec5SDimitry Andric        size_type __cap = capacity();
27030b57cec5SDimitry Andric        size_type __sz = size();
27040b57cec5SDimitry Andric        if (__cap - __sz < __n)
27050b57cec5SDimitry Andric            __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
27060b57cec5SDimitry Andric        pointer __p = __get_pointer();
2707*81ad6265SDimitry Andric        traits_type::assign(std::__to_address(__p) + __sz, __n, __c);
27080b57cec5SDimitry Andric        __sz += __n;
27090b57cec5SDimitry Andric        __set_size(__sz);
27100b57cec5SDimitry Andric        traits_type::assign(__p[__sz], value_type());
27110b57cec5SDimitry Andric    }
27120b57cec5SDimitry Andric    return *this;
27130b57cec5SDimitry Andric}
27140b57cec5SDimitry Andric
27150b57cec5SDimitry Andrictemplate <class _CharT, class _Traits, class _Allocator>
2716*81ad6265SDimitry Andric_LIBCPP_CONSTEXPR_AFTER_CXX17 inline void
27170b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>::__append_default_init(size_type __n)
27180b57cec5SDimitry Andric{
27190b57cec5SDimitry Andric    if (__n)
27200b57cec5SDimitry Andric    {
27210b57cec5SDimitry Andric        size_type __cap = capacity();
27220b57cec5SDimitry Andric        size_type __sz = size();
27230b57cec5SDimitry Andric        if (__cap - __sz < __n)
27240b57cec5SDimitry Andric            __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
27250b57cec5SDimitry Andric        pointer __p = __get_pointer();
27260b57cec5SDimitry Andric        __sz += __n;
27270b57cec5SDimitry Andric        __set_size(__sz);
27280b57cec5SDimitry Andric        traits_type::assign(__p[__sz], value_type());
27290b57cec5SDimitry Andric    }
27300b57cec5SDimitry Andric}
27310b57cec5SDimitry Andric
27320b57cec5SDimitry Andrictemplate <class _CharT, class _Traits, class _Allocator>
2733*81ad6265SDimitry Andric_LIBCPP_CONSTEXPR_AFTER_CXX17
27340b57cec5SDimitry Andricvoid
27350b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>::push_back(value_type __c)
27360b57cec5SDimitry Andric{
27370b57cec5SDimitry Andric    bool __is_short = !__is_long();
27380b57cec5SDimitry Andric    size_type __cap;
27390b57cec5SDimitry Andric    size_type __sz;
27400b57cec5SDimitry Andric    if (__is_short)
27410b57cec5SDimitry Andric    {
27420b57cec5SDimitry Andric        __cap = __min_cap - 1;
27430b57cec5SDimitry Andric        __sz = __get_short_size();
27440b57cec5SDimitry Andric    }
27450b57cec5SDimitry Andric    else
27460b57cec5SDimitry Andric    {
27470b57cec5SDimitry Andric        __cap = __get_long_cap() - 1;
27480b57cec5SDimitry Andric        __sz = __get_long_size();
27490b57cec5SDimitry Andric    }
27500b57cec5SDimitry Andric    if (__sz == __cap)
27510b57cec5SDimitry Andric    {
27520b57cec5SDimitry Andric        __grow_by(__cap, 1, __sz, __sz, 0);
2753349cc55cSDimitry Andric        __is_short = false; // the string is always long after __grow_by
27540b57cec5SDimitry Andric    }
2755*81ad6265SDimitry Andric    pointer __p = __get_pointer();
27560b57cec5SDimitry Andric    if (__is_short)
27570b57cec5SDimitry Andric    {
27580b57cec5SDimitry Andric        __p = __get_short_pointer() + __sz;
27590b57cec5SDimitry Andric        __set_short_size(__sz+1);
27600b57cec5SDimitry Andric    }
27610b57cec5SDimitry Andric    else
27620b57cec5SDimitry Andric    {
27630b57cec5SDimitry Andric        __p = __get_long_pointer() + __sz;
27640b57cec5SDimitry Andric        __set_long_size(__sz+1);
27650b57cec5SDimitry Andric    }
27660b57cec5SDimitry Andric    traits_type::assign(*__p, __c);
27670b57cec5SDimitry Andric    traits_type::assign(*++__p, value_type());
27680b57cec5SDimitry Andric}
27690b57cec5SDimitry Andric
27700b57cec5SDimitry Andrictemplate <class _CharT, class _Traits, class _Allocator>
27710b57cec5SDimitry Andrictemplate<class _ForwardIterator>
2772*81ad6265SDimitry Andric_LIBCPP_CONSTEXPR_AFTER_CXX17
2773349cc55cSDimitry Andric__enable_if_t
2774fe6060f1SDimitry Andric<
2775fe6060f1SDimitry Andric    __is_cpp17_forward_iterator<_ForwardIterator>::value,
27760b57cec5SDimitry Andric    basic_string<_CharT, _Traits, _Allocator>&
2777fe6060f1SDimitry Andric>
2778fe6060f1SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>::append(
27790b57cec5SDimitry Andric    _ForwardIterator __first, _ForwardIterator __last)
27800b57cec5SDimitry Andric{
27810b57cec5SDimitry Andric    size_type __sz = size();
27820b57cec5SDimitry Andric    size_type __cap = capacity();
2783*81ad6265SDimitry Andric    size_type __n = static_cast<size_type>(std::distance(__first, __last));
27840b57cec5SDimitry Andric    if (__n)
27850b57cec5SDimitry Andric    {
2786fe6060f1SDimitry Andric        if (__string_is_trivial_iterator<_ForwardIterator>::value &&
2787fe6060f1SDimitry Andric            !__addr_in_range(*__first))
27880b57cec5SDimitry Andric        {
27890b57cec5SDimitry Andric            if (__cap - __sz < __n)
27900b57cec5SDimitry Andric                __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
27910b57cec5SDimitry Andric            pointer __p = __get_pointer() + __sz;
2792349cc55cSDimitry Andric            for (; __first != __last; ++__p, (void) ++__first)
27930b57cec5SDimitry Andric                traits_type::assign(*__p, *__first);
27940b57cec5SDimitry Andric            traits_type::assign(*__p, value_type());
27950b57cec5SDimitry Andric            __set_size(__sz + __n);
27960b57cec5SDimitry Andric        }
2797fe6060f1SDimitry Andric        else
2798fe6060f1SDimitry Andric        {
2799fe6060f1SDimitry Andric            const basic_string __temp(__first, __last, __alloc());
2800fe6060f1SDimitry Andric            append(__temp.data(), __temp.size());
2801fe6060f1SDimitry Andric        }
28020b57cec5SDimitry Andric    }
28030b57cec5SDimitry Andric    return *this;
28040b57cec5SDimitry Andric}
28050b57cec5SDimitry Andric
28060b57cec5SDimitry Andrictemplate <class _CharT, class _Traits, class _Allocator>
2807*81ad6265SDimitry Andricinline _LIBCPP_CONSTEXPR_AFTER_CXX17
28080b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>&
28090b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>::append(const basic_string& __str)
28100b57cec5SDimitry Andric{
28110b57cec5SDimitry Andric    return append(__str.data(), __str.size());
28120b57cec5SDimitry Andric}
28130b57cec5SDimitry Andric
28140b57cec5SDimitry Andrictemplate <class _CharT, class _Traits, class _Allocator>
2815*81ad6265SDimitry Andric_LIBCPP_CONSTEXPR_AFTER_CXX17
28160b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>&
28170b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>::append(const basic_string& __str, size_type __pos, size_type __n)
28180b57cec5SDimitry Andric{
28190b57cec5SDimitry Andric    size_type __sz = __str.size();
28200b57cec5SDimitry Andric    if (__pos > __sz)
282104eeddc0SDimitry Andric        __throw_out_of_range();
2822*81ad6265SDimitry Andric    return append(__str.data() + __pos, std::min(__n, __sz - __pos));
28230b57cec5SDimitry Andric}
28240b57cec5SDimitry Andric
28250b57cec5SDimitry Andrictemplate <class _CharT, class _Traits, class _Allocator>
28260b57cec5SDimitry Andrictemplate <class _Tp>
2827*81ad6265SDimitry Andric_LIBCPP_CONSTEXPR_AFTER_CXX17
2828349cc55cSDimitry Andric    __enable_if_t
28290b57cec5SDimitry Andric    <
28305ffd83dbSDimitry Andric        __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value  && !__is_same_uncvref<_Tp, basic_string<_CharT, _Traits, _Allocator> >::value,
28310b57cec5SDimitry Andric        basic_string<_CharT, _Traits, _Allocator>&
28325ffd83dbSDimitry Andric    >
28330b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>::append(const _Tp & __t, size_type __pos, size_type __n)
28340b57cec5SDimitry Andric{
28350b57cec5SDimitry Andric    __self_view __sv = __t;
28360b57cec5SDimitry Andric    size_type __sz = __sv.size();
28370b57cec5SDimitry Andric    if (__pos > __sz)
283804eeddc0SDimitry Andric        __throw_out_of_range();
2839*81ad6265SDimitry Andric    return append(__sv.data() + __pos, std::min(__n, __sz - __pos));
28400b57cec5SDimitry Andric}
28410b57cec5SDimitry Andric
28420b57cec5SDimitry Andrictemplate <class _CharT, class _Traits, class _Allocator>
2843*81ad6265SDimitry Andric_LIBCPP_CONSTEXPR_AFTER_CXX17
28440b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>&
28450b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>::append(const value_type* __s)
28460b57cec5SDimitry Andric{
28470b57cec5SDimitry Andric    _LIBCPP_ASSERT(__s != nullptr, "string::append received nullptr");
28480b57cec5SDimitry Andric    return append(__s, traits_type::length(__s));
28490b57cec5SDimitry Andric}
28500b57cec5SDimitry Andric
28510b57cec5SDimitry Andric// insert
28520b57cec5SDimitry Andric
28530b57cec5SDimitry Andrictemplate <class _CharT, class _Traits, class _Allocator>
2854*81ad6265SDimitry Andric_LIBCPP_CONSTEXPR_AFTER_CXX17
28550b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>&
28560b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, const value_type* __s, size_type __n)
28570b57cec5SDimitry Andric{
28580b57cec5SDimitry Andric    _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::insert received nullptr");
28590b57cec5SDimitry Andric    size_type __sz = size();
28600b57cec5SDimitry Andric    if (__pos > __sz)
286104eeddc0SDimitry Andric        __throw_out_of_range();
28620b57cec5SDimitry Andric    size_type __cap = capacity();
2863*81ad6265SDimitry Andric    if (__libcpp_is_constant_evaluated()) {
2864*81ad6265SDimitry Andric        if (__cap - __sz >= __n)
2865*81ad6265SDimitry Andric            __grow_by_and_replace(__cap, 0, __sz, __pos, 0, __n, __s);
2866*81ad6265SDimitry Andric        else
2867*81ad6265SDimitry Andric            __grow_by_and_replace(__cap, __sz + __n - __cap, __sz, __pos, 0, __n, __s);
2868*81ad6265SDimitry Andric        return *this;
2869*81ad6265SDimitry Andric    }
28700b57cec5SDimitry Andric    if (__cap - __sz >= __n)
28710b57cec5SDimitry Andric    {
28720b57cec5SDimitry Andric        if (__n)
28730b57cec5SDimitry Andric        {
2874*81ad6265SDimitry Andric            value_type* __p = std::__to_address(__get_pointer());
28750b57cec5SDimitry Andric            size_type __n_move = __sz - __pos;
28760b57cec5SDimitry Andric            if (__n_move != 0)
28770b57cec5SDimitry Andric            {
28780b57cec5SDimitry Andric                if (__p + __pos <= __s && __s < __p + __sz)
28790b57cec5SDimitry Andric                    __s += __n;
28800b57cec5SDimitry Andric                traits_type::move(__p + __pos + __n, __p + __pos, __n_move);
28810b57cec5SDimitry Andric            }
28820b57cec5SDimitry Andric            traits_type::move(__p + __pos, __s, __n);
28830b57cec5SDimitry Andric            __sz += __n;
28840b57cec5SDimitry Andric            __set_size(__sz);
28850b57cec5SDimitry Andric            traits_type::assign(__p[__sz], value_type());
28860b57cec5SDimitry Andric        }
28870b57cec5SDimitry Andric    }
28880b57cec5SDimitry Andric    else
28890b57cec5SDimitry Andric        __grow_by_and_replace(__cap, __sz + __n - __cap, __sz, __pos, 0, __n, __s);
28900b57cec5SDimitry Andric    return *this;
28910b57cec5SDimitry Andric}
28920b57cec5SDimitry Andric
28930b57cec5SDimitry Andrictemplate <class _CharT, class _Traits, class _Allocator>
2894*81ad6265SDimitry Andric_LIBCPP_CONSTEXPR_AFTER_CXX17
28950b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>&
28960b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, size_type __n, value_type __c)
28970b57cec5SDimitry Andric{
28980b57cec5SDimitry Andric    size_type __sz = size();
28990b57cec5SDimitry Andric    if (__pos > __sz)
290004eeddc0SDimitry Andric        __throw_out_of_range();
29010b57cec5SDimitry Andric    if (__n)
29020b57cec5SDimitry Andric    {
29030b57cec5SDimitry Andric        size_type __cap = capacity();
29040b57cec5SDimitry Andric        value_type* __p;
29050b57cec5SDimitry Andric        if (__cap - __sz >= __n)
29060b57cec5SDimitry Andric        {
2907*81ad6265SDimitry Andric            __p = std::__to_address(__get_pointer());
29080b57cec5SDimitry Andric            size_type __n_move = __sz - __pos;
29090b57cec5SDimitry Andric            if (__n_move != 0)
29100b57cec5SDimitry Andric                traits_type::move(__p + __pos + __n, __p + __pos, __n_move);
29110b57cec5SDimitry Andric        }
29120b57cec5SDimitry Andric        else
29130b57cec5SDimitry Andric        {
29140b57cec5SDimitry Andric            __grow_by(__cap, __sz + __n - __cap, __sz, __pos, 0, __n);
2915*81ad6265SDimitry Andric            __p = std::__to_address(__get_long_pointer());
29160b57cec5SDimitry Andric        }
29170b57cec5SDimitry Andric        traits_type::assign(__p + __pos, __n, __c);
29180b57cec5SDimitry Andric        __sz += __n;
29190b57cec5SDimitry Andric        __set_size(__sz);
29200b57cec5SDimitry Andric        traits_type::assign(__p[__sz], value_type());
29210b57cec5SDimitry Andric    }
29220b57cec5SDimitry Andric    return *this;
29230b57cec5SDimitry Andric}
29240b57cec5SDimitry Andric
29250b57cec5SDimitry Andrictemplate <class _CharT, class _Traits, class _Allocator>
29260b57cec5SDimitry Andrictemplate<class _InputIterator>
2927*81ad6265SDimitry Andric_LIBCPP_CONSTEXPR_AFTER_CXX17
2928349cc55cSDimitry Andric__enable_if_t
29290b57cec5SDimitry Andric<
2930fe6060f1SDimitry Andric   __is_exactly_cpp17_input_iterator<_InputIterator>::value,
29310b57cec5SDimitry Andric   typename basic_string<_CharT, _Traits, _Allocator>::iterator
29325ffd83dbSDimitry Andric>
29330b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, _InputIterator __first, _InputIterator __last)
29340b57cec5SDimitry Andric{
29350eae32dcSDimitry Andric  _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
29360b57cec5SDimitry Andric                       "string::insert(iterator, range) called with an iterator not"
29370b57cec5SDimitry Andric                       " referring to this string");
29380b57cec5SDimitry Andric  const basic_string __temp(__first, __last, __alloc());
29390b57cec5SDimitry Andric  return insert(__pos, __temp.data(), __temp.data() + __temp.size());
29400b57cec5SDimitry Andric}
29410b57cec5SDimitry Andric
29420b57cec5SDimitry Andrictemplate <class _CharT, class _Traits, class _Allocator>
29430b57cec5SDimitry Andrictemplate<class _ForwardIterator>
2944*81ad6265SDimitry Andric_LIBCPP_CONSTEXPR_AFTER_CXX17
2945349cc55cSDimitry Andric__enable_if_t
29460b57cec5SDimitry Andric<
2947fe6060f1SDimitry Andric    __is_cpp17_forward_iterator<_ForwardIterator>::value,
29480b57cec5SDimitry Andric    typename basic_string<_CharT, _Traits, _Allocator>::iterator
29495ffd83dbSDimitry Andric>
29500b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, _ForwardIterator __first, _ForwardIterator __last)
29510b57cec5SDimitry Andric{
29520eae32dcSDimitry Andric    _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2953*81ad6265SDimitry Andric                         "string::insert(iterator, range) called with an iterator not referring to this string");
29540eae32dcSDimitry Andric
29550b57cec5SDimitry Andric    size_type __ip = static_cast<size_type>(__pos - begin());
2956*81ad6265SDimitry Andric    size_type __n = static_cast<size_type>(std::distance(__first, __last));
2957*81ad6265SDimitry Andric    if (__n == 0)
2958*81ad6265SDimitry Andric        return begin() + __ip;
2959*81ad6265SDimitry Andric
2960*81ad6265SDimitry Andric    if (__string_is_trivial_iterator<_ForwardIterator>::value && !__addr_in_range(*__first))
29610b57cec5SDimitry Andric    {
2962*81ad6265SDimitry Andric        return __insert_from_safe_copy(__n, __ip, __first, __last);
29630b57cec5SDimitry Andric    }
2964fe6060f1SDimitry Andric    else
2965fe6060f1SDimitry Andric    {
2966fe6060f1SDimitry Andric        const basic_string __temp(__first, __last, __alloc());
2967*81ad6265SDimitry Andric        return __insert_from_safe_copy(__n, __ip, __temp.begin(), __temp.end());
2968fe6060f1SDimitry Andric    }
2969fe6060f1SDimitry Andric}
29700b57cec5SDimitry Andric
29710b57cec5SDimitry Andrictemplate <class _CharT, class _Traits, class _Allocator>
2972*81ad6265SDimitry Andricinline _LIBCPP_CONSTEXPR_AFTER_CXX17
29730b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>&
29740b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const basic_string& __str)
29750b57cec5SDimitry Andric{
29760b57cec5SDimitry Andric    return insert(__pos1, __str.data(), __str.size());
29770b57cec5SDimitry Andric}
29780b57cec5SDimitry Andric
29790b57cec5SDimitry Andrictemplate <class _CharT, class _Traits, class _Allocator>
2980*81ad6265SDimitry Andric_LIBCPP_CONSTEXPR_AFTER_CXX17
29810b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>&
29820b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const basic_string& __str,
29830b57cec5SDimitry Andric                                                  size_type __pos2, size_type __n)
29840b57cec5SDimitry Andric{
29850b57cec5SDimitry Andric    size_type __str_sz = __str.size();
29860b57cec5SDimitry Andric    if (__pos2 > __str_sz)
298704eeddc0SDimitry Andric        __throw_out_of_range();
2988*81ad6265SDimitry Andric    return insert(__pos1, __str.data() + __pos2, std::min(__n, __str_sz - __pos2));
29890b57cec5SDimitry Andric}
29900b57cec5SDimitry Andric
29910b57cec5SDimitry Andrictemplate <class _CharT, class _Traits, class _Allocator>
29920b57cec5SDimitry Andrictemplate <class _Tp>
2993*81ad6265SDimitry Andric_LIBCPP_CONSTEXPR_AFTER_CXX17
2994349cc55cSDimitry Andric__enable_if_t
29950b57cec5SDimitry Andric<
29965ffd83dbSDimitry Andric    __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value  && !__is_same_uncvref<_Tp, basic_string<_CharT, _Traits, _Allocator> >::value,
29970b57cec5SDimitry Andric    basic_string<_CharT, _Traits, _Allocator>&
29985ffd83dbSDimitry Andric>
29990b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const _Tp& __t,
30000b57cec5SDimitry Andric                                                  size_type __pos2, size_type __n)
30010b57cec5SDimitry Andric{
30020b57cec5SDimitry Andric    __self_view __sv = __t;
30030b57cec5SDimitry Andric    size_type __str_sz = __sv.size();
30040b57cec5SDimitry Andric    if (__pos2 > __str_sz)
300504eeddc0SDimitry Andric        __throw_out_of_range();
3006*81ad6265SDimitry Andric    return insert(__pos1, __sv.data() + __pos2, std::min(__n, __str_sz - __pos2));
30070b57cec5SDimitry Andric}
30080b57cec5SDimitry Andric
30090b57cec5SDimitry Andrictemplate <class _CharT, class _Traits, class _Allocator>
3010*81ad6265SDimitry Andric_LIBCPP_CONSTEXPR_AFTER_CXX17
30110b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>&
30120b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, const value_type* __s)
30130b57cec5SDimitry Andric{
30140b57cec5SDimitry Andric    _LIBCPP_ASSERT(__s != nullptr, "string::insert received nullptr");
30150b57cec5SDimitry Andric    return insert(__pos, __s, traits_type::length(__s));
30160b57cec5SDimitry Andric}
30170b57cec5SDimitry Andric
30180b57cec5SDimitry Andrictemplate <class _CharT, class _Traits, class _Allocator>
3019*81ad6265SDimitry Andric_LIBCPP_CONSTEXPR_AFTER_CXX17
30200b57cec5SDimitry Andrictypename basic_string<_CharT, _Traits, _Allocator>::iterator
30210b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, value_type __c)
30220b57cec5SDimitry Andric{
3023d56accc7SDimitry Andric    _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
3024d56accc7SDimitry Andric                         "string::insert(iterator, character) called with an iterator not"
3025d56accc7SDimitry Andric                         " referring to this string");
3026d56accc7SDimitry Andric
30270b57cec5SDimitry Andric    size_type __ip = static_cast<size_type>(__pos - begin());
30280b57cec5SDimitry Andric    size_type __sz = size();
30290b57cec5SDimitry Andric    size_type __cap = capacity();
30300b57cec5SDimitry Andric    value_type* __p;
30310b57cec5SDimitry Andric    if (__cap == __sz)
30320b57cec5SDimitry Andric    {
30330b57cec5SDimitry Andric        __grow_by(__cap, 1, __sz, __ip, 0, 1);
3034*81ad6265SDimitry Andric        __p = std::__to_address(__get_long_pointer());
30350b57cec5SDimitry Andric    }
30360b57cec5SDimitry Andric    else
30370b57cec5SDimitry Andric    {
3038*81ad6265SDimitry Andric        __p = std::__to_address(__get_pointer());
30390b57cec5SDimitry Andric        size_type __n_move = __sz - __ip;
30400b57cec5SDimitry Andric        if (__n_move != 0)
30410b57cec5SDimitry Andric            traits_type::move(__p + __ip + 1, __p + __ip, __n_move);
30420b57cec5SDimitry Andric    }
30430b57cec5SDimitry Andric    traits_type::assign(__p[__ip], __c);
30440b57cec5SDimitry Andric    traits_type::assign(__p[++__sz], value_type());
30450b57cec5SDimitry Andric    __set_size(__sz);
30460b57cec5SDimitry Andric    return begin() + static_cast<difference_type>(__ip);
30470b57cec5SDimitry Andric}
30480b57cec5SDimitry Andric
30490b57cec5SDimitry Andrictemplate <class _CharT, class _Traits, class _Allocator>
3050*81ad6265SDimitry Andricinline _LIBCPP_CONSTEXPR_AFTER_CXX17
30510b57cec5SDimitry Andrictypename basic_string<_CharT, _Traits, _Allocator>::iterator
30520b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, size_type __n, value_type __c)
30530b57cec5SDimitry Andric{
30540eae32dcSDimitry Andric  _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
30550b57cec5SDimitry Andric                       "string::insert(iterator, n, value) called with an iterator not"
30560b57cec5SDimitry Andric                       " referring to this string");
30570b57cec5SDimitry Andric  difference_type __p = __pos - begin();
30580b57cec5SDimitry Andric  insert(static_cast<size_type>(__p), __n, __c);
30590b57cec5SDimitry Andric  return begin() + __p;
30600b57cec5SDimitry Andric}
30610b57cec5SDimitry Andric
30620b57cec5SDimitry Andric// replace
30630b57cec5SDimitry Andric
30640b57cec5SDimitry Andrictemplate <class _CharT, class _Traits, class _Allocator>
3065*81ad6265SDimitry Andric_LIBCPP_CONSTEXPR_AFTER_CXX17
30660b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>&
30670b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos, size_type __n1, const value_type* __s, size_type __n2)
30680b57cec5SDimitry Andric    _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
30690b57cec5SDimitry Andric{
30700b57cec5SDimitry Andric    _LIBCPP_ASSERT(__n2 == 0 || __s != nullptr, "string::replace received nullptr");
30710b57cec5SDimitry Andric    size_type __sz = size();
30720b57cec5SDimitry Andric    if (__pos > __sz)
307304eeddc0SDimitry Andric        __throw_out_of_range();
3074*81ad6265SDimitry Andric    __n1 = std::min(__n1, __sz - __pos);
30750b57cec5SDimitry Andric    size_type __cap = capacity();
30760b57cec5SDimitry Andric    if (__cap - __sz + __n1 >= __n2)
30770b57cec5SDimitry Andric    {
3078*81ad6265SDimitry Andric        if (__libcpp_is_constant_evaluated()) {
3079*81ad6265SDimitry Andric            __grow_by_and_replace(__cap, 0, __sz, __pos, __n1, __n2, __s);
3080*81ad6265SDimitry Andric            return *this;
3081*81ad6265SDimitry Andric        }
3082*81ad6265SDimitry Andric        value_type* __p = std::__to_address(__get_pointer());
30830b57cec5SDimitry Andric        if (__n1 != __n2)
30840b57cec5SDimitry Andric        {
30850b57cec5SDimitry Andric            size_type __n_move = __sz - __pos - __n1;
30860b57cec5SDimitry Andric            if (__n_move != 0)
30870b57cec5SDimitry Andric            {
30880b57cec5SDimitry Andric                if (__n1 > __n2)
30890b57cec5SDimitry Andric                {
30900b57cec5SDimitry Andric                    traits_type::move(__p + __pos, __s, __n2);
30910b57cec5SDimitry Andric                    traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
30920eae32dcSDimitry Andric                    return __null_terminate_at(__p, __sz + (__n2 - __n1));
30930b57cec5SDimitry Andric                }
30940b57cec5SDimitry Andric                if (__p + __pos < __s && __s < __p + __sz)
30950b57cec5SDimitry Andric                {
30960b57cec5SDimitry Andric                    if (__p + __pos + __n1 <= __s)
30970b57cec5SDimitry Andric                        __s += __n2 - __n1;
30980b57cec5SDimitry Andric                    else // __p + __pos < __s < __p + __pos + __n1
30990b57cec5SDimitry Andric                    {
31000b57cec5SDimitry Andric                        traits_type::move(__p + __pos, __s, __n1);
31010b57cec5SDimitry Andric                        __pos += __n1;
31020b57cec5SDimitry Andric                        __s += __n2;
31030b57cec5SDimitry Andric                        __n2 -= __n1;
31040b57cec5SDimitry Andric                        __n1 = 0;
31050b57cec5SDimitry Andric                    }
31060b57cec5SDimitry Andric                }
31070b57cec5SDimitry Andric                traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
31080b57cec5SDimitry Andric            }
31090b57cec5SDimitry Andric        }
31100b57cec5SDimitry Andric        traits_type::move(__p + __pos, __s, __n2);
31110eae32dcSDimitry Andric        return __null_terminate_at(__p, __sz + (__n2 - __n1));
31120b57cec5SDimitry Andric    }
31130b57cec5SDimitry Andric    else
31140b57cec5SDimitry Andric        __grow_by_and_replace(__cap, __sz - __n1 + __n2 - __cap, __sz, __pos, __n1, __n2, __s);
31150b57cec5SDimitry Andric    return *this;
31160b57cec5SDimitry Andric}
31170b57cec5SDimitry Andric
31180b57cec5SDimitry Andrictemplate <class _CharT, class _Traits, class _Allocator>
3119*81ad6265SDimitry Andric_LIBCPP_CONSTEXPR_AFTER_CXX17
31200b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>&
31210b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos, size_type __n1, size_type __n2, value_type __c)
31220b57cec5SDimitry Andric{
31230b57cec5SDimitry Andric    size_type __sz = size();
31240b57cec5SDimitry Andric    if (__pos > __sz)
312504eeddc0SDimitry Andric        __throw_out_of_range();
3126*81ad6265SDimitry Andric    __n1 = std::min(__n1, __sz - __pos);
31270b57cec5SDimitry Andric    size_type __cap = capacity();
31280b57cec5SDimitry Andric    value_type* __p;
31290b57cec5SDimitry Andric    if (__cap - __sz + __n1 >= __n2)
31300b57cec5SDimitry Andric    {
3131*81ad6265SDimitry Andric        __p = std::__to_address(__get_pointer());
31320b57cec5SDimitry Andric        if (__n1 != __n2)
31330b57cec5SDimitry Andric        {
31340b57cec5SDimitry Andric            size_type __n_move = __sz - __pos - __n1;
31350b57cec5SDimitry Andric            if (__n_move != 0)
31360b57cec5SDimitry Andric                traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
31370b57cec5SDimitry Andric        }
31380b57cec5SDimitry Andric    }
31390b57cec5SDimitry Andric    else
31400b57cec5SDimitry Andric    {
31410b57cec5SDimitry Andric        __grow_by(__cap, __sz - __n1 + __n2 - __cap, __sz, __pos, __n1, __n2);
3142*81ad6265SDimitry Andric        __p = std::__to_address(__get_long_pointer());
31430b57cec5SDimitry Andric    }
31440b57cec5SDimitry Andric    traits_type::assign(__p + __pos, __n2, __c);
31450eae32dcSDimitry Andric    return __null_terminate_at(__p, __sz - (__n1 - __n2));
31460b57cec5SDimitry Andric}
31470b57cec5SDimitry Andric
31480b57cec5SDimitry Andrictemplate <class _CharT, class _Traits, class _Allocator>
31490b57cec5SDimitry Andrictemplate<class _InputIterator>
3150*81ad6265SDimitry Andric_LIBCPP_CONSTEXPR_AFTER_CXX17
3151349cc55cSDimitry Andric__enable_if_t
31520b57cec5SDimitry Andric<
3153480093f4SDimitry Andric    __is_cpp17_input_iterator<_InputIterator>::value,
31540b57cec5SDimitry Andric    basic_string<_CharT, _Traits, _Allocator>&
31555ffd83dbSDimitry Andric>
31560b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2,
31570b57cec5SDimitry Andric                                                   _InputIterator __j1, _InputIterator __j2)
31580b57cec5SDimitry Andric{
31590b57cec5SDimitry Andric    const basic_string __temp(__j1, __j2, __alloc());
316004eeddc0SDimitry Andric    return replace(__i1, __i2, __temp);
31610b57cec5SDimitry Andric}
31620b57cec5SDimitry Andric
31630b57cec5SDimitry Andrictemplate <class _CharT, class _Traits, class _Allocator>
3164*81ad6265SDimitry Andricinline _LIBCPP_CONSTEXPR_AFTER_CXX17
31650b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>&
31660b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const basic_string& __str)
31670b57cec5SDimitry Andric{
31680b57cec5SDimitry Andric    return replace(__pos1, __n1, __str.data(), __str.size());
31690b57cec5SDimitry Andric}
31700b57cec5SDimitry Andric
31710b57cec5SDimitry Andrictemplate <class _CharT, class _Traits, class _Allocator>
3172*81ad6265SDimitry Andric_LIBCPP_CONSTEXPR_AFTER_CXX17
31730b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>&
31740b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const basic_string& __str,
31750b57cec5SDimitry Andric                                                   size_type __pos2, size_type __n2)
31760b57cec5SDimitry Andric{
31770b57cec5SDimitry Andric    size_type __str_sz = __str.size();
31780b57cec5SDimitry Andric    if (__pos2 > __str_sz)
317904eeddc0SDimitry Andric        __throw_out_of_range();
3180*81ad6265SDimitry Andric    return replace(__pos1, __n1, __str.data() + __pos2, std::min(__n2, __str_sz - __pos2));
31810b57cec5SDimitry Andric}
31820b57cec5SDimitry Andric
31830b57cec5SDimitry Andrictemplate <class _CharT, class _Traits, class _Allocator>
31840b57cec5SDimitry Andrictemplate <class _Tp>
3185*81ad6265SDimitry Andric_LIBCPP_CONSTEXPR_AFTER_CXX17
3186349cc55cSDimitry Andric__enable_if_t
31870b57cec5SDimitry Andric<
31885ffd83dbSDimitry Andric    __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string<_CharT, _Traits, _Allocator> >::value,
31890b57cec5SDimitry Andric    basic_string<_CharT, _Traits, _Allocator>&
31905ffd83dbSDimitry Andric>
31910b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const _Tp& __t,
31920b57cec5SDimitry Andric                                                   size_type __pos2, size_type __n2)
31930b57cec5SDimitry Andric{
31940b57cec5SDimitry Andric    __self_view __sv = __t;
31950b57cec5SDimitry Andric    size_type __str_sz = __sv.size();
31960b57cec5SDimitry Andric    if (__pos2 > __str_sz)
319704eeddc0SDimitry Andric        __throw_out_of_range();
3198*81ad6265SDimitry Andric    return replace(__pos1, __n1, __sv.data() + __pos2, std::min(__n2, __str_sz - __pos2));
31990b57cec5SDimitry Andric}
32000b57cec5SDimitry Andric
32010b57cec5SDimitry Andrictemplate <class _CharT, class _Traits, class _Allocator>
3202*81ad6265SDimitry Andric_LIBCPP_CONSTEXPR_AFTER_CXX17
32030b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>&
32040b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos, size_type __n1, const value_type* __s)
32050b57cec5SDimitry Andric{
32060b57cec5SDimitry Andric    _LIBCPP_ASSERT(__s != nullptr, "string::replace received nullptr");
32070b57cec5SDimitry Andric    return replace(__pos, __n1, __s, traits_type::length(__s));
32080b57cec5SDimitry Andric}
32090b57cec5SDimitry Andric
32100b57cec5SDimitry Andrictemplate <class _CharT, class _Traits, class _Allocator>
3211*81ad6265SDimitry Andricinline _LIBCPP_CONSTEXPR_AFTER_CXX17
32120b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>&
32130b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, const basic_string& __str)
32140b57cec5SDimitry Andric{
32150b57cec5SDimitry Andric    return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1),
32160b57cec5SDimitry Andric                   __str.data(), __str.size());
32170b57cec5SDimitry Andric}
32180b57cec5SDimitry Andric
32190b57cec5SDimitry Andrictemplate <class _CharT, class _Traits, class _Allocator>
3220*81ad6265SDimitry Andricinline _LIBCPP_CONSTEXPR_AFTER_CXX17
32210b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>&
32220b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, const value_type* __s, size_type __n)
32230b57cec5SDimitry Andric{
32240b57cec5SDimitry Andric    return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __s, __n);
32250b57cec5SDimitry Andric}
32260b57cec5SDimitry Andric
32270b57cec5SDimitry Andrictemplate <class _CharT, class _Traits, class _Allocator>
3228*81ad6265SDimitry Andricinline _LIBCPP_CONSTEXPR_AFTER_CXX17
32290b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>&
32300b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, const value_type* __s)
32310b57cec5SDimitry Andric{
32320b57cec5SDimitry Andric    return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __s);
32330b57cec5SDimitry Andric}
32340b57cec5SDimitry Andric
32350b57cec5SDimitry Andrictemplate <class _CharT, class _Traits, class _Allocator>
3236*81ad6265SDimitry Andricinline _LIBCPP_CONSTEXPR_AFTER_CXX17
32370b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>&
32380b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, size_type __n, value_type __c)
32390b57cec5SDimitry Andric{
32400b57cec5SDimitry Andric    return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __n, __c);
32410b57cec5SDimitry Andric}
32420b57cec5SDimitry Andric
32430b57cec5SDimitry Andric// erase
32440b57cec5SDimitry Andric
32455ffd83dbSDimitry Andric// 'externally instantiated' erase() implementation, called when __n != npos.
32465ffd83dbSDimitry Andric// Does not check __pos against size()
32470b57cec5SDimitry Andrictemplate <class _CharT, class _Traits, class _Allocator>
3248*81ad6265SDimitry Andric_LIBCPP_CONSTEXPR_AFTER_CXX17
32495ffd83dbSDimitry Andricvoid
32505ffd83dbSDimitry Andricbasic_string<_CharT, _Traits, _Allocator>::__erase_external_with_move(
32515ffd83dbSDimitry Andric    size_type __pos, size_type __n)
32520b57cec5SDimitry Andric{
32530b57cec5SDimitry Andric    if (__n)
32540b57cec5SDimitry Andric    {
32555ffd83dbSDimitry Andric        size_type __sz = size();
3256*81ad6265SDimitry Andric        value_type* __p = std::__to_address(__get_pointer());
3257*81ad6265SDimitry Andric        __n = std::min(__n, __sz - __pos);
32580b57cec5SDimitry Andric        size_type __n_move = __sz - __pos - __n;
32590b57cec5SDimitry Andric        if (__n_move != 0)
32600b57cec5SDimitry Andric            traits_type::move(__p + __pos, __p + __pos + __n, __n_move);
32610eae32dcSDimitry Andric        __null_terminate_at(__p, __sz - __n);
32620b57cec5SDimitry Andric    }
32635ffd83dbSDimitry Andric}
32645ffd83dbSDimitry Andric
32655ffd83dbSDimitry Andrictemplate <class _CharT, class _Traits, class _Allocator>
3266*81ad6265SDimitry Andric_LIBCPP_CONSTEXPR_AFTER_CXX17
32675ffd83dbSDimitry Andricbasic_string<_CharT, _Traits, _Allocator>&
32685ffd83dbSDimitry Andricbasic_string<_CharT, _Traits, _Allocator>::erase(size_type __pos,
32695ffd83dbSDimitry Andric                                                 size_type __n) {
327004eeddc0SDimitry Andric  if (__pos > size())
327104eeddc0SDimitry Andric    __throw_out_of_range();
32725ffd83dbSDimitry Andric  if (__n == npos) {
32735ffd83dbSDimitry Andric    __erase_to_end(__pos);
32745ffd83dbSDimitry Andric  } else {
32755ffd83dbSDimitry Andric    __erase_external_with_move(__pos, __n);
32765ffd83dbSDimitry Andric  }
32770b57cec5SDimitry Andric  return *this;
32780b57cec5SDimitry Andric}
32790b57cec5SDimitry Andric
32800b57cec5SDimitry Andrictemplate <class _CharT, class _Traits, class _Allocator>
3281*81ad6265SDimitry Andricinline _LIBCPP_CONSTEXPR_AFTER_CXX17
32820b57cec5SDimitry Andrictypename basic_string<_CharT, _Traits, _Allocator>::iterator
32830b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>::erase(const_iterator __pos)
32840b57cec5SDimitry Andric{
32850eae32dcSDimitry Andric  _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
32860b57cec5SDimitry Andric                       "string::erase(iterator) called with an iterator not"
32870b57cec5SDimitry Andric                       " referring to this string");
32880eae32dcSDimitry Andric
32890eae32dcSDimitry Andric  _LIBCPP_ASSERT(__pos != end(), "string::erase(iterator) called with a non-dereferenceable iterator");
32900b57cec5SDimitry Andric  iterator __b = begin();
32910b57cec5SDimitry Andric  size_type __r = static_cast<size_type>(__pos - __b);
32920b57cec5SDimitry Andric  erase(__r, 1);
32930b57cec5SDimitry Andric  return __b + static_cast<difference_type>(__r);
32940b57cec5SDimitry Andric}
32950b57cec5SDimitry Andric
32960b57cec5SDimitry Andrictemplate <class _CharT, class _Traits, class _Allocator>
3297*81ad6265SDimitry Andricinline _LIBCPP_CONSTEXPR_AFTER_CXX17
32980b57cec5SDimitry Andrictypename basic_string<_CharT, _Traits, _Allocator>::iterator
32990b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>::erase(const_iterator __first, const_iterator __last)
33000b57cec5SDimitry Andric{
33010eae32dcSDimitry Andric  _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__first) == this,
33020b57cec5SDimitry Andric                       "string::erase(iterator,  iterator) called with an iterator not"
33030b57cec5SDimitry Andric                       " referring to this string");
33040eae32dcSDimitry Andric
33050b57cec5SDimitry Andric  _LIBCPP_ASSERT(__first <= __last, "string::erase(first, last) called with invalid range");
33060b57cec5SDimitry Andric  iterator __b = begin();
33070b57cec5SDimitry Andric  size_type __r = static_cast<size_type>(__first - __b);
33080b57cec5SDimitry Andric  erase(__r, static_cast<size_type>(__last - __first));
33090b57cec5SDimitry Andric  return __b + static_cast<difference_type>(__r);
33100b57cec5SDimitry Andric}
33110b57cec5SDimitry Andric
33120b57cec5SDimitry Andrictemplate <class _CharT, class _Traits, class _Allocator>
3313*81ad6265SDimitry Andricinline _LIBCPP_CONSTEXPR_AFTER_CXX17
33140b57cec5SDimitry Andricvoid
33150b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>::pop_back()
33160b57cec5SDimitry Andric{
33170b57cec5SDimitry Andric    _LIBCPP_ASSERT(!empty(), "string::pop_back(): string is already empty");
33180eae32dcSDimitry Andric    __erase_to_end(size() - 1);
33190b57cec5SDimitry Andric}
33200b57cec5SDimitry Andric
33210b57cec5SDimitry Andrictemplate <class _CharT, class _Traits, class _Allocator>
3322*81ad6265SDimitry Andricinline _LIBCPP_CONSTEXPR_AFTER_CXX17
33230b57cec5SDimitry Andricvoid
33240b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>::clear() _NOEXCEPT
33250b57cec5SDimitry Andric{
3326*81ad6265SDimitry Andric    std::__debug_db_invalidate_all(this);
33270b57cec5SDimitry Andric    if (__is_long())
33280b57cec5SDimitry Andric    {
33290b57cec5SDimitry Andric        traits_type::assign(*__get_long_pointer(), value_type());
33300b57cec5SDimitry Andric        __set_long_size(0);
33310b57cec5SDimitry Andric    }
33320b57cec5SDimitry Andric    else
33330b57cec5SDimitry Andric    {
33340b57cec5SDimitry Andric        traits_type::assign(*__get_short_pointer(), value_type());
33350b57cec5SDimitry Andric        __set_short_size(0);
33360b57cec5SDimitry Andric    }
33370b57cec5SDimitry Andric}
33380b57cec5SDimitry Andric
33390b57cec5SDimitry Andrictemplate <class _CharT, class _Traits, class _Allocator>
3340*81ad6265SDimitry Andricinline _LIBCPP_CONSTEXPR_AFTER_CXX17
33410b57cec5SDimitry Andricvoid
33420b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>::__erase_to_end(size_type __pos)
33430b57cec5SDimitry Andric{
3344*81ad6265SDimitry Andric  __null_terminate_at(std::__to_address(__get_pointer()), __pos);
33450b57cec5SDimitry Andric}
33460b57cec5SDimitry Andric
33470b57cec5SDimitry Andrictemplate <class _CharT, class _Traits, class _Allocator>
3348*81ad6265SDimitry Andric_LIBCPP_CONSTEXPR_AFTER_CXX17
33490b57cec5SDimitry Andricvoid
33500b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>::resize(size_type __n, value_type __c)
33510b57cec5SDimitry Andric{
33520b57cec5SDimitry Andric    size_type __sz = size();
33530b57cec5SDimitry Andric    if (__n > __sz)
33540b57cec5SDimitry Andric        append(__n - __sz, __c);
33550b57cec5SDimitry Andric    else
33560b57cec5SDimitry Andric        __erase_to_end(__n);
33570b57cec5SDimitry Andric}
33580b57cec5SDimitry Andric
33590b57cec5SDimitry Andrictemplate <class _CharT, class _Traits, class _Allocator>
3360*81ad6265SDimitry Andric_LIBCPP_CONSTEXPR_AFTER_CXX17 inline void
33610b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>::__resize_default_init(size_type __n)
33620b57cec5SDimitry Andric{
33630b57cec5SDimitry Andric    size_type __sz = size();
33640b57cec5SDimitry Andric    if (__n > __sz) {
33650b57cec5SDimitry Andric       __append_default_init(__n - __sz);
33660b57cec5SDimitry Andric    } else
33670b57cec5SDimitry Andric        __erase_to_end(__n);
33680b57cec5SDimitry Andric}
33690b57cec5SDimitry Andric
33700b57cec5SDimitry Andrictemplate <class _CharT, class _Traits, class _Allocator>
3371*81ad6265SDimitry Andricinline _LIBCPP_CONSTEXPR_AFTER_CXX17
33720b57cec5SDimitry Andrictypename basic_string<_CharT, _Traits, _Allocator>::size_type
33730b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>::max_size() const _NOEXCEPT
33740b57cec5SDimitry Andric{
33750b57cec5SDimitry Andric    size_type __m = __alloc_traits::max_size(__alloc());
3376*81ad6265SDimitry Andric    if (__m <= std::numeric_limits<size_type>::max() / 2) {
33770b57cec5SDimitry Andric        return __m - __alignment;
3378*81ad6265SDimitry Andric    } else {
3379*81ad6265SDimitry Andric        bool __uses_lsb = __endian_factor == 2;
3380*81ad6265SDimitry Andric        return __uses_lsb ? __m - __alignment : (__m / 2) - __alignment;
3381*81ad6265SDimitry Andric    }
33820b57cec5SDimitry Andric}
33830b57cec5SDimitry Andric
33840b57cec5SDimitry Andrictemplate <class _CharT, class _Traits, class _Allocator>
3385*81ad6265SDimitry Andric_LIBCPP_CONSTEXPR_AFTER_CXX17
33860b57cec5SDimitry Andricvoid
3387e8d8bef9SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>::reserve(size_type __requested_capacity)
33880b57cec5SDimitry Andric{
3389e8d8bef9SDimitry Andric    if (__requested_capacity > max_size())
339004eeddc0SDimitry Andric        __throw_length_error();
3391e8d8bef9SDimitry Andric
339204eeddc0SDimitry Andric    // Make sure reserve(n) never shrinks. This is technically only required in C++20
339304eeddc0SDimitry Andric    // and later (since P0966R1), however we provide consistent behavior in all Standard
339404eeddc0SDimitry Andric    // modes because this function is instantiated in the shared library.
339504eeddc0SDimitry Andric    if (__requested_capacity <= capacity())
339604eeddc0SDimitry Andric        return;
3397e8d8bef9SDimitry Andric
3398*81ad6265SDimitry Andric    size_type __target_capacity = std::max(__requested_capacity, size());
3399e8d8bef9SDimitry Andric    __target_capacity = __recommend(__target_capacity);
3400e8d8bef9SDimitry Andric    if (__target_capacity == capacity()) return;
3401e8d8bef9SDimitry Andric
3402e8d8bef9SDimitry Andric    __shrink_or_extend(__target_capacity);
3403e8d8bef9SDimitry Andric}
3404e8d8bef9SDimitry Andric
3405e8d8bef9SDimitry Andrictemplate <class _CharT, class _Traits, class _Allocator>
3406*81ad6265SDimitry Andricinline _LIBCPP_CONSTEXPR_AFTER_CXX17
3407e8d8bef9SDimitry Andricvoid
3408e8d8bef9SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>::shrink_to_fit() _NOEXCEPT
3409e8d8bef9SDimitry Andric{
3410e8d8bef9SDimitry Andric    size_type __target_capacity = __recommend(size());
3411e8d8bef9SDimitry Andric    if (__target_capacity == capacity()) return;
3412e8d8bef9SDimitry Andric
3413e8d8bef9SDimitry Andric    __shrink_or_extend(__target_capacity);
3414e8d8bef9SDimitry Andric}
3415e8d8bef9SDimitry Andric
3416e8d8bef9SDimitry Andrictemplate <class _CharT, class _Traits, class _Allocator>
3417*81ad6265SDimitry Andricinline _LIBCPP_CONSTEXPR_AFTER_CXX17
3418e8d8bef9SDimitry Andricvoid
3419e8d8bef9SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>::__shrink_or_extend(size_type __target_capacity)
3420e8d8bef9SDimitry Andric{
34210b57cec5SDimitry Andric    size_type __cap = capacity();
34220b57cec5SDimitry Andric    size_type __sz = size();
3423e8d8bef9SDimitry Andric
34240b57cec5SDimitry Andric    pointer __new_data, __p;
34250b57cec5SDimitry Andric    bool __was_long, __now_long;
3426*81ad6265SDimitry Andric    if (__fits_in_sso(__target_capacity))
34270b57cec5SDimitry Andric    {
34280b57cec5SDimitry Andric        __was_long = true;
34290b57cec5SDimitry Andric        __now_long = false;
34300b57cec5SDimitry Andric        __new_data = __get_short_pointer();
34310b57cec5SDimitry Andric        __p = __get_long_pointer();
34320b57cec5SDimitry Andric    }
34330b57cec5SDimitry Andric    else
34340b57cec5SDimitry Andric    {
3435*81ad6265SDimitry Andric        if (__target_capacity > __cap) {
3436*81ad6265SDimitry Andric            auto __allocation = std::__allocate_at_least(__alloc(), __target_capacity + 1);
3437*81ad6265SDimitry Andric            __new_data = __allocation.ptr;
3438*81ad6265SDimitry Andric            __target_capacity = __allocation.count - 1;
3439*81ad6265SDimitry Andric        }
34400b57cec5SDimitry Andric        else
34410b57cec5SDimitry Andric        {
34420b57cec5SDimitry Andric        #ifndef _LIBCPP_NO_EXCEPTIONS
34430b57cec5SDimitry Andric            try
34440b57cec5SDimitry Andric            {
34450b57cec5SDimitry Andric        #endif // _LIBCPP_NO_EXCEPTIONS
3446*81ad6265SDimitry Andric                auto __allocation = std::__allocate_at_least(__alloc(), __target_capacity + 1);
3447*81ad6265SDimitry Andric                __new_data = __allocation.ptr;
3448*81ad6265SDimitry Andric                __target_capacity = __allocation.count - 1;
34490b57cec5SDimitry Andric        #ifndef _LIBCPP_NO_EXCEPTIONS
34500b57cec5SDimitry Andric            }
34510b57cec5SDimitry Andric            catch (...)
34520b57cec5SDimitry Andric            {
34530b57cec5SDimitry Andric                return;
34540b57cec5SDimitry Andric            }
34550b57cec5SDimitry Andric        #else  // _LIBCPP_NO_EXCEPTIONS
34560b57cec5SDimitry Andric            if (__new_data == nullptr)
34570b57cec5SDimitry Andric                return;
34580b57cec5SDimitry Andric        #endif // _LIBCPP_NO_EXCEPTIONS
34590b57cec5SDimitry Andric        }
3460*81ad6265SDimitry Andric        __begin_lifetime(__new_data, __target_capacity + 1);
34610b57cec5SDimitry Andric        __now_long = true;
34620b57cec5SDimitry Andric        __was_long = __is_long();
34630b57cec5SDimitry Andric        __p = __get_pointer();
34640b57cec5SDimitry Andric    }
3465*81ad6265SDimitry Andric    traits_type::copy(std::__to_address(__new_data),
3466*81ad6265SDimitry Andric                      std::__to_address(__p), size()+1);
34670b57cec5SDimitry Andric    if (__was_long)
34680b57cec5SDimitry Andric        __alloc_traits::deallocate(__alloc(), __p, __cap+1);
34690b57cec5SDimitry Andric    if (__now_long)
34700b57cec5SDimitry Andric    {
3471e8d8bef9SDimitry Andric        __set_long_cap(__target_capacity+1);
34720b57cec5SDimitry Andric        __set_long_size(__sz);
34730b57cec5SDimitry Andric        __set_long_pointer(__new_data);
34740b57cec5SDimitry Andric    }
34750b57cec5SDimitry Andric    else
34760b57cec5SDimitry Andric        __set_short_size(__sz);
3477*81ad6265SDimitry Andric    std::__debug_db_invalidate_all(this);
34780b57cec5SDimitry Andric}
34790b57cec5SDimitry Andric
34800b57cec5SDimitry Andrictemplate <class _CharT, class _Traits, class _Allocator>
3481*81ad6265SDimitry Andricinline _LIBCPP_CONSTEXPR_AFTER_CXX17
34820b57cec5SDimitry Andrictypename basic_string<_CharT, _Traits, _Allocator>::const_reference
34830b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>::operator[](size_type __pos) const _NOEXCEPT
34840b57cec5SDimitry Andric{
34850b57cec5SDimitry Andric    _LIBCPP_ASSERT(__pos <= size(), "string index out of bounds");
34860b57cec5SDimitry Andric    return *(data() + __pos);
34870b57cec5SDimitry Andric}
34880b57cec5SDimitry Andric
34890b57cec5SDimitry Andrictemplate <class _CharT, class _Traits, class _Allocator>
3490*81ad6265SDimitry Andricinline _LIBCPP_CONSTEXPR_AFTER_CXX17
34910b57cec5SDimitry Andrictypename basic_string<_CharT, _Traits, _Allocator>::reference
34920b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>::operator[](size_type __pos) _NOEXCEPT
34930b57cec5SDimitry Andric{
34940b57cec5SDimitry Andric    _LIBCPP_ASSERT(__pos <= size(), "string index out of bounds");
34950b57cec5SDimitry Andric    return *(__get_pointer() + __pos);
34960b57cec5SDimitry Andric}
34970b57cec5SDimitry Andric
34980b57cec5SDimitry Andrictemplate <class _CharT, class _Traits, class _Allocator>
3499*81ad6265SDimitry Andric_LIBCPP_CONSTEXPR_AFTER_CXX17
35000b57cec5SDimitry Andrictypename basic_string<_CharT, _Traits, _Allocator>::const_reference
35010b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>::at(size_type __n) const
35020b57cec5SDimitry Andric{
35030b57cec5SDimitry Andric    if (__n >= size())
350404eeddc0SDimitry Andric        __throw_out_of_range();
35050b57cec5SDimitry Andric    return (*this)[__n];
35060b57cec5SDimitry Andric}
35070b57cec5SDimitry Andric
35080b57cec5SDimitry Andrictemplate <class _CharT, class _Traits, class _Allocator>
3509*81ad6265SDimitry Andric_LIBCPP_CONSTEXPR_AFTER_CXX17
35100b57cec5SDimitry Andrictypename basic_string<_CharT, _Traits, _Allocator>::reference
35110b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>::at(size_type __n)
35120b57cec5SDimitry Andric{
35130b57cec5SDimitry Andric    if (__n >= size())
351404eeddc0SDimitry Andric        __throw_out_of_range();
35150b57cec5SDimitry Andric    return (*this)[__n];
35160b57cec5SDimitry Andric}
35170b57cec5SDimitry Andric
35180b57cec5SDimitry Andrictemplate <class _CharT, class _Traits, class _Allocator>
3519*81ad6265SDimitry Andricinline _LIBCPP_CONSTEXPR_AFTER_CXX17
35200b57cec5SDimitry Andrictypename basic_string<_CharT, _Traits, _Allocator>::reference
35210b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>::front() _NOEXCEPT
35220b57cec5SDimitry Andric{
35230b57cec5SDimitry Andric    _LIBCPP_ASSERT(!empty(), "string::front(): string is empty");
35240b57cec5SDimitry Andric    return *__get_pointer();
35250b57cec5SDimitry Andric}
35260b57cec5SDimitry Andric
35270b57cec5SDimitry Andrictemplate <class _CharT, class _Traits, class _Allocator>
3528*81ad6265SDimitry Andricinline _LIBCPP_CONSTEXPR_AFTER_CXX17
35290b57cec5SDimitry Andrictypename basic_string<_CharT, _Traits, _Allocator>::const_reference
35300b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>::front() const _NOEXCEPT
35310b57cec5SDimitry Andric{
35320b57cec5SDimitry Andric    _LIBCPP_ASSERT(!empty(), "string::front(): string is empty");
35330b57cec5SDimitry Andric    return *data();
35340b57cec5SDimitry Andric}
35350b57cec5SDimitry Andric
35360b57cec5SDimitry Andrictemplate <class _CharT, class _Traits, class _Allocator>
3537*81ad6265SDimitry Andricinline _LIBCPP_CONSTEXPR_AFTER_CXX17
35380b57cec5SDimitry Andrictypename basic_string<_CharT, _Traits, _Allocator>::reference
35390b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>::back() _NOEXCEPT
35400b57cec5SDimitry Andric{
35410b57cec5SDimitry Andric    _LIBCPP_ASSERT(!empty(), "string::back(): string is empty");
35420b57cec5SDimitry Andric    return *(__get_pointer() + size() - 1);
35430b57cec5SDimitry Andric}
35440b57cec5SDimitry Andric
35450b57cec5SDimitry Andrictemplate <class _CharT, class _Traits, class _Allocator>
3546*81ad6265SDimitry Andricinline _LIBCPP_CONSTEXPR_AFTER_CXX17
35470b57cec5SDimitry Andrictypename basic_string<_CharT, _Traits, _Allocator>::const_reference
35480b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>::back() const _NOEXCEPT
35490b57cec5SDimitry Andric{
35500b57cec5SDimitry Andric    _LIBCPP_ASSERT(!empty(), "string::back(): string is empty");
35510b57cec5SDimitry Andric    return *(data() + size() - 1);
35520b57cec5SDimitry Andric}
35530b57cec5SDimitry Andric
35540b57cec5SDimitry Andrictemplate <class _CharT, class _Traits, class _Allocator>
3555*81ad6265SDimitry Andric_LIBCPP_CONSTEXPR_AFTER_CXX17
35560b57cec5SDimitry Andrictypename basic_string<_CharT, _Traits, _Allocator>::size_type
35570b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>::copy(value_type* __s, size_type __n, size_type __pos) const
35580b57cec5SDimitry Andric{
35590b57cec5SDimitry Andric    size_type __sz = size();
35600b57cec5SDimitry Andric    if (__pos > __sz)
356104eeddc0SDimitry Andric        __throw_out_of_range();
3562*81ad6265SDimitry Andric    size_type __rlen = std::min(__n, __sz - __pos);
35630b57cec5SDimitry Andric    traits_type::copy(__s, data() + __pos, __rlen);
35640b57cec5SDimitry Andric    return __rlen;
35650b57cec5SDimitry Andric}
35660b57cec5SDimitry Andric
35670b57cec5SDimitry Andrictemplate <class _CharT, class _Traits, class _Allocator>
3568*81ad6265SDimitry Andricinline _LIBCPP_CONSTEXPR_AFTER_CXX17
35690b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>
35700b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>::substr(size_type __pos, size_type __n) const
35710b57cec5SDimitry Andric{
35720b57cec5SDimitry Andric    return basic_string(*this, __pos, __n, __alloc());
35730b57cec5SDimitry Andric}
35740b57cec5SDimitry Andric
35750b57cec5SDimitry Andrictemplate <class _CharT, class _Traits, class _Allocator>
3576*81ad6265SDimitry Andricinline _LIBCPP_CONSTEXPR_AFTER_CXX17
35770b57cec5SDimitry Andricvoid
35780b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>::swap(basic_string& __str)
35790b57cec5SDimitry Andric#if _LIBCPP_STD_VER >= 14
35800b57cec5SDimitry Andric        _NOEXCEPT
35810b57cec5SDimitry Andric#else
35820b57cec5SDimitry Andric        _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
35830b57cec5SDimitry Andric                    __is_nothrow_swappable<allocator_type>::value)
35840b57cec5SDimitry Andric#endif
35850b57cec5SDimitry Andric{
35860b57cec5SDimitry Andric    if (!__is_long())
3587*81ad6265SDimitry Andric        std::__debug_db_invalidate_all(this);
35880b57cec5SDimitry Andric    if (!__str.__is_long())
3589*81ad6265SDimitry Andric        std::__debug_db_invalidate_all(&__str);
3590*81ad6265SDimitry Andric    std::__debug_db_swap(this, &__str);
3591*81ad6265SDimitry Andric
35920b57cec5SDimitry Andric    _LIBCPP_ASSERT(
35930b57cec5SDimitry Andric        __alloc_traits::propagate_on_container_swap::value ||
35940b57cec5SDimitry Andric        __alloc_traits::is_always_equal::value ||
35950b57cec5SDimitry Andric        __alloc() == __str.__alloc(), "swapping non-equal allocators");
3596*81ad6265SDimitry Andric    std::swap(__r_.first(), __str.__r_.first());
3597*81ad6265SDimitry Andric    std::__swap_allocator(__alloc(), __str.__alloc());
35980b57cec5SDimitry Andric}
35990b57cec5SDimitry Andric
36000b57cec5SDimitry Andric// find
36010b57cec5SDimitry Andric
36020b57cec5SDimitry Andrictemplate <class _Traits>
36030b57cec5SDimitry Andricstruct _LIBCPP_HIDDEN __traits_eq
36040b57cec5SDimitry Andric{
36050b57cec5SDimitry Andric    typedef typename _Traits::char_type char_type;
3606*81ad6265SDimitry Andric    _LIBCPP_HIDE_FROM_ABI
36070b57cec5SDimitry Andric    bool operator()(const char_type& __x, const char_type& __y) _NOEXCEPT
36080b57cec5SDimitry Andric        {return _Traits::eq(__x, __y);}
36090b57cec5SDimitry Andric};
36100b57cec5SDimitry Andric
36110b57cec5SDimitry Andrictemplate<class _CharT, class _Traits, class _Allocator>
3612*81ad6265SDimitry Andric_LIBCPP_CONSTEXPR_AFTER_CXX17
36130b57cec5SDimitry Andrictypename basic_string<_CharT, _Traits, _Allocator>::size_type
36140b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>::find(const value_type* __s,
36150b57cec5SDimitry Andric                                                size_type __pos,
36160b57cec5SDimitry Andric                                                size_type __n) const _NOEXCEPT
36170b57cec5SDimitry Andric{
36180b57cec5SDimitry Andric    _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find(): received nullptr");
36190b57cec5SDimitry Andric    return __str_find<value_type, size_type, traits_type, npos>
36200b57cec5SDimitry Andric        (data(), size(), __s, __pos, __n);
36210b57cec5SDimitry Andric}
36220b57cec5SDimitry Andric
36230b57cec5SDimitry Andrictemplate<class _CharT, class _Traits, class _Allocator>
3624*81ad6265SDimitry Andricinline _LIBCPP_CONSTEXPR_AFTER_CXX17
36250b57cec5SDimitry Andrictypename basic_string<_CharT, _Traits, _Allocator>::size_type
36260b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>::find(const basic_string& __str,
36270b57cec5SDimitry Andric                                                size_type __pos) const _NOEXCEPT
36280b57cec5SDimitry Andric{
36290b57cec5SDimitry Andric    return __str_find<value_type, size_type, traits_type, npos>
36300b57cec5SDimitry Andric        (data(), size(), __str.data(), __pos, __str.size());
36310b57cec5SDimitry Andric}
36320b57cec5SDimitry Andric
36330b57cec5SDimitry Andrictemplate<class _CharT, class _Traits, class _Allocator>
36340b57cec5SDimitry Andrictemplate <class _Tp>
3635*81ad6265SDimitry Andric_LIBCPP_CONSTEXPR_AFTER_CXX17
3636349cc55cSDimitry Andric__enable_if_t
36370b57cec5SDimitry Andric<
36380b57cec5SDimitry Andric    __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
36390b57cec5SDimitry Andric    typename basic_string<_CharT, _Traits, _Allocator>::size_type
36405ffd83dbSDimitry Andric>
36410b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>::find(const _Tp &__t,
3642fe6060f1SDimitry Andric                                                size_type __pos) const _NOEXCEPT
36430b57cec5SDimitry Andric{
36440b57cec5SDimitry Andric    __self_view __sv = __t;
36450b57cec5SDimitry Andric    return __str_find<value_type, size_type, traits_type, npos>
36460b57cec5SDimitry Andric        (data(), size(), __sv.data(), __pos, __sv.size());
36470b57cec5SDimitry Andric}
36480b57cec5SDimitry Andric
36490b57cec5SDimitry Andrictemplate<class _CharT, class _Traits, class _Allocator>
3650*81ad6265SDimitry Andricinline _LIBCPP_CONSTEXPR_AFTER_CXX17
36510b57cec5SDimitry Andrictypename basic_string<_CharT, _Traits, _Allocator>::size_type
36520b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>::find(const value_type* __s,
36530b57cec5SDimitry Andric                                                size_type __pos) const _NOEXCEPT
36540b57cec5SDimitry Andric{
36550b57cec5SDimitry Andric    _LIBCPP_ASSERT(__s != nullptr, "string::find(): received nullptr");
36560b57cec5SDimitry Andric    return __str_find<value_type, size_type, traits_type, npos>
36570b57cec5SDimitry Andric        (data(), size(), __s, __pos, traits_type::length(__s));
36580b57cec5SDimitry Andric}
36590b57cec5SDimitry Andric
36600b57cec5SDimitry Andrictemplate<class _CharT, class _Traits, class _Allocator>
3661*81ad6265SDimitry Andric_LIBCPP_CONSTEXPR_AFTER_CXX17
36620b57cec5SDimitry Andrictypename basic_string<_CharT, _Traits, _Allocator>::size_type
36630b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>::find(value_type __c,
36640b57cec5SDimitry Andric                                                size_type __pos) const _NOEXCEPT
36650b57cec5SDimitry Andric{
36660b57cec5SDimitry Andric    return __str_find<value_type, size_type, traits_type, npos>
36670b57cec5SDimitry Andric        (data(), size(), __c, __pos);
36680b57cec5SDimitry Andric}
36690b57cec5SDimitry Andric
36700b57cec5SDimitry Andric// rfind
36710b57cec5SDimitry Andric
36720b57cec5SDimitry Andrictemplate<class _CharT, class _Traits, class _Allocator>
3673*81ad6265SDimitry Andric_LIBCPP_CONSTEXPR_AFTER_CXX17
36740b57cec5SDimitry Andrictypename basic_string<_CharT, _Traits, _Allocator>::size_type
36750b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>::rfind(const value_type* __s,
36760b57cec5SDimitry Andric                                                 size_type __pos,
36770b57cec5SDimitry Andric                                                 size_type __n) const _NOEXCEPT
36780b57cec5SDimitry Andric{
36790b57cec5SDimitry Andric    _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::rfind(): received nullptr");
36800b57cec5SDimitry Andric    return __str_rfind<value_type, size_type, traits_type, npos>
36810b57cec5SDimitry Andric        (data(), size(), __s, __pos, __n);
36820b57cec5SDimitry Andric}
36830b57cec5SDimitry Andric
36840b57cec5SDimitry Andrictemplate<class _CharT, class _Traits, class _Allocator>
3685*81ad6265SDimitry Andricinline _LIBCPP_CONSTEXPR_AFTER_CXX17
36860b57cec5SDimitry Andrictypename basic_string<_CharT, _Traits, _Allocator>::size_type
36870b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>::rfind(const basic_string& __str,
36880b57cec5SDimitry Andric                                                 size_type __pos) const _NOEXCEPT
36890b57cec5SDimitry Andric{
36900b57cec5SDimitry Andric    return __str_rfind<value_type, size_type, traits_type, npos>
36910b57cec5SDimitry Andric        (data(), size(), __str.data(), __pos, __str.size());
36920b57cec5SDimitry Andric}
36930b57cec5SDimitry Andric
36940b57cec5SDimitry Andrictemplate<class _CharT, class _Traits, class _Allocator>
36950b57cec5SDimitry Andrictemplate <class _Tp>
3696*81ad6265SDimitry Andric_LIBCPP_CONSTEXPR_AFTER_CXX17
3697349cc55cSDimitry Andric__enable_if_t
36980b57cec5SDimitry Andric<
36990b57cec5SDimitry Andric    __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
37000b57cec5SDimitry Andric    typename basic_string<_CharT, _Traits, _Allocator>::size_type
37015ffd83dbSDimitry Andric>
37020b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>::rfind(const _Tp& __t,
3703fe6060f1SDimitry Andric                                                size_type __pos) const _NOEXCEPT
37040b57cec5SDimitry Andric{
37050b57cec5SDimitry Andric    __self_view __sv = __t;
37060b57cec5SDimitry Andric    return __str_rfind<value_type, size_type, traits_type, npos>
37070b57cec5SDimitry Andric        (data(), size(), __sv.data(), __pos, __sv.size());
37080b57cec5SDimitry Andric}
37090b57cec5SDimitry Andric
37100b57cec5SDimitry Andrictemplate<class _CharT, class _Traits, class _Allocator>
3711*81ad6265SDimitry Andricinline _LIBCPP_CONSTEXPR_AFTER_CXX17
37120b57cec5SDimitry Andrictypename basic_string<_CharT, _Traits, _Allocator>::size_type
37130b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>::rfind(const value_type* __s,
37140b57cec5SDimitry Andric                                                 size_type __pos) const _NOEXCEPT
37150b57cec5SDimitry Andric{
37160b57cec5SDimitry Andric    _LIBCPP_ASSERT(__s != nullptr, "string::rfind(): received nullptr");
37170b57cec5SDimitry Andric    return __str_rfind<value_type, size_type, traits_type, npos>
37180b57cec5SDimitry Andric        (data(), size(), __s, __pos, traits_type::length(__s));
37190b57cec5SDimitry Andric}
37200b57cec5SDimitry Andric
37210b57cec5SDimitry Andrictemplate<class _CharT, class _Traits, class _Allocator>
3722*81ad6265SDimitry Andric_LIBCPP_CONSTEXPR_AFTER_CXX17
37230b57cec5SDimitry Andrictypename basic_string<_CharT, _Traits, _Allocator>::size_type
37240b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>::rfind(value_type __c,
37250b57cec5SDimitry Andric                                                 size_type __pos) const _NOEXCEPT
37260b57cec5SDimitry Andric{
37270b57cec5SDimitry Andric    return __str_rfind<value_type, size_type, traits_type, npos>
37280b57cec5SDimitry Andric        (data(), size(), __c, __pos);
37290b57cec5SDimitry Andric}
37300b57cec5SDimitry Andric
37310b57cec5SDimitry Andric// find_first_of
37320b57cec5SDimitry Andric
37330b57cec5SDimitry Andrictemplate<class _CharT, class _Traits, class _Allocator>
3734*81ad6265SDimitry Andric_LIBCPP_CONSTEXPR_AFTER_CXX17
37350b57cec5SDimitry Andrictypename basic_string<_CharT, _Traits, _Allocator>::size_type
37360b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>::find_first_of(const value_type* __s,
37370b57cec5SDimitry Andric                                                         size_type __pos,
37380b57cec5SDimitry Andric                                                         size_type __n) const _NOEXCEPT
37390b57cec5SDimitry Andric{
37400b57cec5SDimitry Andric    _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_first_of(): received nullptr");
37410b57cec5SDimitry Andric    return __str_find_first_of<value_type, size_type, traits_type, npos>
37420b57cec5SDimitry Andric        (data(), size(), __s, __pos, __n);
37430b57cec5SDimitry Andric}
37440b57cec5SDimitry Andric
37450b57cec5SDimitry Andrictemplate<class _CharT, class _Traits, class _Allocator>
3746*81ad6265SDimitry Andricinline _LIBCPP_CONSTEXPR_AFTER_CXX17
37470b57cec5SDimitry Andrictypename basic_string<_CharT, _Traits, _Allocator>::size_type
37480b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>::find_first_of(const basic_string& __str,
37490b57cec5SDimitry Andric                                                         size_type __pos) const _NOEXCEPT
37500b57cec5SDimitry Andric{
37510b57cec5SDimitry Andric    return __str_find_first_of<value_type, size_type, traits_type, npos>
37520b57cec5SDimitry Andric        (data(), size(), __str.data(), __pos, __str.size());
37530b57cec5SDimitry Andric}
37540b57cec5SDimitry Andric
37550b57cec5SDimitry Andrictemplate<class _CharT, class _Traits, class _Allocator>
37560b57cec5SDimitry Andrictemplate <class _Tp>
3757*81ad6265SDimitry Andric_LIBCPP_CONSTEXPR_AFTER_CXX17
3758349cc55cSDimitry Andric__enable_if_t
37590b57cec5SDimitry Andric<
37600b57cec5SDimitry Andric    __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
37610b57cec5SDimitry Andric    typename basic_string<_CharT, _Traits, _Allocator>::size_type
37625ffd83dbSDimitry Andric>
37630b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>::find_first_of(const _Tp& __t,
3764fe6060f1SDimitry Andric                                                size_type __pos) const _NOEXCEPT
37650b57cec5SDimitry Andric{
37660b57cec5SDimitry Andric    __self_view __sv = __t;
37670b57cec5SDimitry Andric    return __str_find_first_of<value_type, size_type, traits_type, npos>
37680b57cec5SDimitry Andric        (data(), size(), __sv.data(), __pos, __sv.size());
37690b57cec5SDimitry Andric}
37700b57cec5SDimitry Andric
37710b57cec5SDimitry Andrictemplate<class _CharT, class _Traits, class _Allocator>
3772*81ad6265SDimitry Andricinline _LIBCPP_CONSTEXPR_AFTER_CXX17
37730b57cec5SDimitry Andrictypename basic_string<_CharT, _Traits, _Allocator>::size_type
37740b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>::find_first_of(const value_type* __s,
37750b57cec5SDimitry Andric                                                         size_type __pos) const _NOEXCEPT
37760b57cec5SDimitry Andric{
37770b57cec5SDimitry Andric    _LIBCPP_ASSERT(__s != nullptr, "string::find_first_of(): received nullptr");
37780b57cec5SDimitry Andric    return __str_find_first_of<value_type, size_type, traits_type, npos>
37790b57cec5SDimitry Andric        (data(), size(), __s, __pos, traits_type::length(__s));
37800b57cec5SDimitry Andric}
37810b57cec5SDimitry Andric
37820b57cec5SDimitry Andrictemplate<class _CharT, class _Traits, class _Allocator>
3783*81ad6265SDimitry Andricinline _LIBCPP_CONSTEXPR_AFTER_CXX17
37840b57cec5SDimitry Andrictypename basic_string<_CharT, _Traits, _Allocator>::size_type
37850b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>::find_first_of(value_type __c,
37860b57cec5SDimitry Andric                                                         size_type __pos) const _NOEXCEPT
37870b57cec5SDimitry Andric{
37880b57cec5SDimitry Andric    return find(__c, __pos);
37890b57cec5SDimitry Andric}
37900b57cec5SDimitry Andric
37910b57cec5SDimitry Andric// find_last_of
37920b57cec5SDimitry Andric
37930b57cec5SDimitry Andrictemplate<class _CharT, class _Traits, class _Allocator>
3794*81ad6265SDimitry Andricinline _LIBCPP_CONSTEXPR_AFTER_CXX17
37950b57cec5SDimitry Andrictypename basic_string<_CharT, _Traits, _Allocator>::size_type
37960b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>::find_last_of(const value_type* __s,
37970b57cec5SDimitry Andric                                                        size_type __pos,
37980b57cec5SDimitry Andric                                                        size_type __n) const _NOEXCEPT
37990b57cec5SDimitry Andric{
38000b57cec5SDimitry Andric    _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_last_of(): received nullptr");
38010b57cec5SDimitry Andric    return __str_find_last_of<value_type, size_type, traits_type, npos>
38020b57cec5SDimitry Andric        (data(), size(), __s, __pos, __n);
38030b57cec5SDimitry Andric}
38040b57cec5SDimitry Andric
38050b57cec5SDimitry Andrictemplate<class _CharT, class _Traits, class _Allocator>
3806*81ad6265SDimitry Andricinline _LIBCPP_CONSTEXPR_AFTER_CXX17
38070b57cec5SDimitry Andrictypename basic_string<_CharT, _Traits, _Allocator>::size_type
38080b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>::find_last_of(const basic_string& __str,
38090b57cec5SDimitry Andric                                                        size_type __pos) const _NOEXCEPT
38100b57cec5SDimitry Andric{
38110b57cec5SDimitry Andric    return __str_find_last_of<value_type, size_type, traits_type, npos>
38120b57cec5SDimitry Andric        (data(), size(), __str.data(), __pos, __str.size());
38130b57cec5SDimitry Andric}
38140b57cec5SDimitry Andric
38150b57cec5SDimitry Andrictemplate<class _CharT, class _Traits, class _Allocator>
38160b57cec5SDimitry Andrictemplate <class _Tp>
3817*81ad6265SDimitry Andric_LIBCPP_CONSTEXPR_AFTER_CXX17
3818349cc55cSDimitry Andric__enable_if_t
38190b57cec5SDimitry Andric<
38200b57cec5SDimitry Andric    __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
38210b57cec5SDimitry Andric    typename basic_string<_CharT, _Traits, _Allocator>::size_type
38225ffd83dbSDimitry Andric>
38230b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>::find_last_of(const _Tp& __t,
3824fe6060f1SDimitry Andric                                                size_type __pos) const _NOEXCEPT
38250b57cec5SDimitry Andric{
38260b57cec5SDimitry Andric    __self_view __sv = __t;
38270b57cec5SDimitry Andric    return __str_find_last_of<value_type, size_type, traits_type, npos>
38280b57cec5SDimitry Andric        (data(), size(), __sv.data(), __pos, __sv.size());
38290b57cec5SDimitry Andric}
38300b57cec5SDimitry Andric
38310b57cec5SDimitry Andrictemplate<class _CharT, class _Traits, class _Allocator>
3832*81ad6265SDimitry Andricinline _LIBCPP_CONSTEXPR_AFTER_CXX17
38330b57cec5SDimitry Andrictypename basic_string<_CharT, _Traits, _Allocator>::size_type
38340b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>::find_last_of(const value_type* __s,
38350b57cec5SDimitry Andric                                                        size_type __pos) const _NOEXCEPT
38360b57cec5SDimitry Andric{
38370b57cec5SDimitry Andric    _LIBCPP_ASSERT(__s != nullptr, "string::find_last_of(): received nullptr");
38380b57cec5SDimitry Andric    return __str_find_last_of<value_type, size_type, traits_type, npos>
38390b57cec5SDimitry Andric        (data(), size(), __s, __pos, traits_type::length(__s));
38400b57cec5SDimitry Andric}
38410b57cec5SDimitry Andric
38420b57cec5SDimitry Andrictemplate<class _CharT, class _Traits, class _Allocator>
3843*81ad6265SDimitry Andricinline _LIBCPP_CONSTEXPR_AFTER_CXX17
38440b57cec5SDimitry Andrictypename basic_string<_CharT, _Traits, _Allocator>::size_type
38450b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>::find_last_of(value_type __c,
38460b57cec5SDimitry Andric                                                        size_type __pos) const _NOEXCEPT
38470b57cec5SDimitry Andric{
38480b57cec5SDimitry Andric    return rfind(__c, __pos);
38490b57cec5SDimitry Andric}
38500b57cec5SDimitry Andric
38510b57cec5SDimitry Andric// find_first_not_of
38520b57cec5SDimitry Andric
38530b57cec5SDimitry Andrictemplate<class _CharT, class _Traits, class _Allocator>
3854*81ad6265SDimitry Andric_LIBCPP_CONSTEXPR_AFTER_CXX17
38550b57cec5SDimitry Andrictypename basic_string<_CharT, _Traits, _Allocator>::size_type
38560b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const value_type* __s,
38570b57cec5SDimitry Andric                                                             size_type __pos,
38580b57cec5SDimitry Andric                                                             size_type __n) const _NOEXCEPT
38590b57cec5SDimitry Andric{
38600b57cec5SDimitry Andric    _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_first_not_of(): received nullptr");
38610b57cec5SDimitry Andric    return __str_find_first_not_of<value_type, size_type, traits_type, npos>
38620b57cec5SDimitry Andric        (data(), size(), __s, __pos, __n);
38630b57cec5SDimitry Andric}
38640b57cec5SDimitry Andric
38650b57cec5SDimitry Andrictemplate<class _CharT, class _Traits, class _Allocator>
3866*81ad6265SDimitry Andricinline _LIBCPP_CONSTEXPR_AFTER_CXX17
38670b57cec5SDimitry Andrictypename basic_string<_CharT, _Traits, _Allocator>::size_type
38680b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const basic_string& __str,
38690b57cec5SDimitry Andric                                                             size_type __pos) const _NOEXCEPT
38700b57cec5SDimitry Andric{
38710b57cec5SDimitry Andric    return __str_find_first_not_of<value_type, size_type, traits_type, npos>
38720b57cec5SDimitry Andric        (data(), size(), __str.data(), __pos, __str.size());
38730b57cec5SDimitry Andric}
38740b57cec5SDimitry Andric
38750b57cec5SDimitry Andrictemplate<class _CharT, class _Traits, class _Allocator>
38760b57cec5SDimitry Andrictemplate <class _Tp>
3877*81ad6265SDimitry Andric_LIBCPP_CONSTEXPR_AFTER_CXX17
3878349cc55cSDimitry Andric__enable_if_t
38790b57cec5SDimitry Andric<
38800b57cec5SDimitry Andric    __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
38810b57cec5SDimitry Andric    typename basic_string<_CharT, _Traits, _Allocator>::size_type
38825ffd83dbSDimitry Andric>
38830b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const _Tp& __t,
3884fe6060f1SDimitry Andric                                                size_type __pos) const _NOEXCEPT
38850b57cec5SDimitry Andric{
38860b57cec5SDimitry Andric    __self_view __sv = __t;
38870b57cec5SDimitry Andric    return __str_find_first_not_of<value_type, size_type, traits_type, npos>
38880b57cec5SDimitry Andric        (data(), size(), __sv.data(), __pos, __sv.size());
38890b57cec5SDimitry Andric}
38900b57cec5SDimitry Andric
38910b57cec5SDimitry Andrictemplate<class _CharT, class _Traits, class _Allocator>
3892*81ad6265SDimitry Andricinline _LIBCPP_CONSTEXPR_AFTER_CXX17
38930b57cec5SDimitry Andrictypename basic_string<_CharT, _Traits, _Allocator>::size_type
38940b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const value_type* __s,
38950b57cec5SDimitry Andric                                                             size_type __pos) const _NOEXCEPT
38960b57cec5SDimitry Andric{
38970b57cec5SDimitry Andric    _LIBCPP_ASSERT(__s != nullptr, "string::find_first_not_of(): received nullptr");
38980b57cec5SDimitry Andric    return __str_find_first_not_of<value_type, size_type, traits_type, npos>
38990b57cec5SDimitry Andric        (data(), size(), __s, __pos, traits_type::length(__s));
39000b57cec5SDimitry Andric}
39010b57cec5SDimitry Andric
39020b57cec5SDimitry Andrictemplate<class _CharT, class _Traits, class _Allocator>
3903*81ad6265SDimitry Andricinline _LIBCPP_CONSTEXPR_AFTER_CXX17
39040b57cec5SDimitry Andrictypename basic_string<_CharT, _Traits, _Allocator>::size_type
39050b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>::find_first_not_of(value_type __c,
39060b57cec5SDimitry Andric                                                             size_type __pos) const _NOEXCEPT
39070b57cec5SDimitry Andric{
39080b57cec5SDimitry Andric    return __str_find_first_not_of<value_type, size_type, traits_type, npos>
39090b57cec5SDimitry Andric        (data(), size(), __c, __pos);
39100b57cec5SDimitry Andric}
39110b57cec5SDimitry Andric
39120b57cec5SDimitry Andric// find_last_not_of
39130b57cec5SDimitry Andric
39140b57cec5SDimitry Andrictemplate<class _CharT, class _Traits, class _Allocator>
3915*81ad6265SDimitry Andric_LIBCPP_CONSTEXPR_AFTER_CXX17
39160b57cec5SDimitry Andrictypename basic_string<_CharT, _Traits, _Allocator>::size_type
39170b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const value_type* __s,
39180b57cec5SDimitry Andric                                                            size_type __pos,
39190b57cec5SDimitry Andric                                                            size_type __n) const _NOEXCEPT
39200b57cec5SDimitry Andric{
39210b57cec5SDimitry Andric    _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_last_not_of(): received nullptr");
39220b57cec5SDimitry Andric    return __str_find_last_not_of<value_type, size_type, traits_type, npos>
39230b57cec5SDimitry Andric        (data(), size(), __s, __pos, __n);
39240b57cec5SDimitry Andric}
39250b57cec5SDimitry Andric
39260b57cec5SDimitry Andrictemplate<class _CharT, class _Traits, class _Allocator>
3927*81ad6265SDimitry Andricinline _LIBCPP_CONSTEXPR_AFTER_CXX17
39280b57cec5SDimitry Andrictypename basic_string<_CharT, _Traits, _Allocator>::size_type
39290b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const basic_string& __str,
39300b57cec5SDimitry Andric                                                            size_type __pos) const _NOEXCEPT
39310b57cec5SDimitry Andric{
39320b57cec5SDimitry Andric    return __str_find_last_not_of<value_type, size_type, traits_type, npos>
39330b57cec5SDimitry Andric        (data(), size(), __str.data(), __pos, __str.size());
39340b57cec5SDimitry Andric}
39350b57cec5SDimitry Andric
39360b57cec5SDimitry Andrictemplate<class _CharT, class _Traits, class _Allocator>
39370b57cec5SDimitry Andrictemplate <class _Tp>
3938*81ad6265SDimitry Andric_LIBCPP_CONSTEXPR_AFTER_CXX17
3939349cc55cSDimitry Andric__enable_if_t
39400b57cec5SDimitry Andric<
39410b57cec5SDimitry Andric    __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
39420b57cec5SDimitry Andric    typename basic_string<_CharT, _Traits, _Allocator>::size_type
39435ffd83dbSDimitry Andric>
39440b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const _Tp& __t,
3945fe6060f1SDimitry Andric                                                size_type __pos) const _NOEXCEPT
39460b57cec5SDimitry Andric{
39470b57cec5SDimitry Andric    __self_view __sv = __t;
39480b57cec5SDimitry Andric    return __str_find_last_not_of<value_type, size_type, traits_type, npos>
39490b57cec5SDimitry Andric        (data(), size(), __sv.data(), __pos, __sv.size());
39500b57cec5SDimitry Andric}
39510b57cec5SDimitry Andric
39520b57cec5SDimitry Andrictemplate<class _CharT, class _Traits, class _Allocator>
3953*81ad6265SDimitry Andricinline _LIBCPP_CONSTEXPR_AFTER_CXX17
39540b57cec5SDimitry Andrictypename basic_string<_CharT, _Traits, _Allocator>::size_type
39550b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const value_type* __s,
39560b57cec5SDimitry Andric                                                            size_type __pos) const _NOEXCEPT
39570b57cec5SDimitry Andric{
39580b57cec5SDimitry Andric    _LIBCPP_ASSERT(__s != nullptr, "string::find_last_not_of(): received nullptr");
39590b57cec5SDimitry Andric    return __str_find_last_not_of<value_type, size_type, traits_type, npos>
39600b57cec5SDimitry Andric        (data(), size(), __s, __pos, traits_type::length(__s));
39610b57cec5SDimitry Andric}
39620b57cec5SDimitry Andric
39630b57cec5SDimitry Andrictemplate<class _CharT, class _Traits, class _Allocator>
3964*81ad6265SDimitry Andricinline _LIBCPP_CONSTEXPR_AFTER_CXX17
39650b57cec5SDimitry Andrictypename basic_string<_CharT, _Traits, _Allocator>::size_type
39660b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>::find_last_not_of(value_type __c,
39670b57cec5SDimitry Andric                                                            size_type __pos) const _NOEXCEPT
39680b57cec5SDimitry Andric{
39690b57cec5SDimitry Andric    return __str_find_last_not_of<value_type, size_type, traits_type, npos>
39700b57cec5SDimitry Andric        (data(), size(), __c, __pos);
39710b57cec5SDimitry Andric}
39720b57cec5SDimitry Andric
39730b57cec5SDimitry Andric// compare
39740b57cec5SDimitry Andric
39750b57cec5SDimitry Andrictemplate <class _CharT, class _Traits, class _Allocator>
39760b57cec5SDimitry Andrictemplate <class _Tp>
3977*81ad6265SDimitry Andric_LIBCPP_CONSTEXPR_AFTER_CXX17
3978349cc55cSDimitry Andric__enable_if_t
39790b57cec5SDimitry Andric<
39800b57cec5SDimitry Andric    __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
39810b57cec5SDimitry Andric    int
39825ffd83dbSDimitry Andric>
3983fe6060f1SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>::compare(const _Tp& __t) const _NOEXCEPT
39840b57cec5SDimitry Andric{
39850b57cec5SDimitry Andric    __self_view __sv = __t;
39860b57cec5SDimitry Andric    size_t __lhs_sz = size();
39870b57cec5SDimitry Andric    size_t __rhs_sz = __sv.size();
39880b57cec5SDimitry Andric    int __result = traits_type::compare(data(), __sv.data(),
3989*81ad6265SDimitry Andric                                        std::min(__lhs_sz, __rhs_sz));
39900b57cec5SDimitry Andric    if (__result != 0)
39910b57cec5SDimitry Andric        return __result;
39920b57cec5SDimitry Andric    if (__lhs_sz < __rhs_sz)
39930b57cec5SDimitry Andric        return -1;
39940b57cec5SDimitry Andric    if (__lhs_sz > __rhs_sz)
39950b57cec5SDimitry Andric        return 1;
39960b57cec5SDimitry Andric    return 0;
39970b57cec5SDimitry Andric}
39980b57cec5SDimitry Andric
39990b57cec5SDimitry Andrictemplate <class _CharT, class _Traits, class _Allocator>
4000*81ad6265SDimitry Andricinline _LIBCPP_CONSTEXPR_AFTER_CXX17
40010b57cec5SDimitry Andricint
40020b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>::compare(const basic_string& __str) const _NOEXCEPT
40030b57cec5SDimitry Andric{
40040b57cec5SDimitry Andric    return compare(__self_view(__str));
40050b57cec5SDimitry Andric}
40060b57cec5SDimitry Andric
40070b57cec5SDimitry Andrictemplate <class _CharT, class _Traits, class _Allocator>
4008*81ad6265SDimitry Andricinline _LIBCPP_CONSTEXPR_AFTER_CXX17
40090b57cec5SDimitry Andricint
40100b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
40110b57cec5SDimitry Andric                                                   size_type __n1,
40120b57cec5SDimitry Andric                                                   const value_type* __s,
40130b57cec5SDimitry Andric                                                   size_type __n2) const
40140b57cec5SDimitry Andric{
40150b57cec5SDimitry Andric    _LIBCPP_ASSERT(__n2 == 0 || __s != nullptr, "string::compare(): received nullptr");
40160b57cec5SDimitry Andric    size_type __sz = size();
40170b57cec5SDimitry Andric    if (__pos1 > __sz || __n2 == npos)
401804eeddc0SDimitry Andric        __throw_out_of_range();
4019*81ad6265SDimitry Andric    size_type __rlen = std::min(__n1, __sz - __pos1);
4020*81ad6265SDimitry Andric    int __r = traits_type::compare(data() + __pos1, __s, std::min(__rlen, __n2));
40210b57cec5SDimitry Andric    if (__r == 0)
40220b57cec5SDimitry Andric    {
40230b57cec5SDimitry Andric        if (__rlen < __n2)
40240b57cec5SDimitry Andric            __r = -1;
40250b57cec5SDimitry Andric        else if (__rlen > __n2)
40260b57cec5SDimitry Andric            __r = 1;
40270b57cec5SDimitry Andric    }
40280b57cec5SDimitry Andric    return __r;
40290b57cec5SDimitry Andric}
40300b57cec5SDimitry Andric
40310b57cec5SDimitry Andrictemplate <class _CharT, class _Traits, class _Allocator>
40320b57cec5SDimitry Andrictemplate <class _Tp>
4033*81ad6265SDimitry Andric_LIBCPP_CONSTEXPR_AFTER_CXX17
4034349cc55cSDimitry Andric__enable_if_t
40350b57cec5SDimitry Andric<
40360b57cec5SDimitry Andric    __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
40370b57cec5SDimitry Andric    int
40385ffd83dbSDimitry Andric>
40390b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
40400b57cec5SDimitry Andric                                                   size_type __n1,
40410b57cec5SDimitry Andric                                                   const _Tp& __t) const
40420b57cec5SDimitry Andric{
40430b57cec5SDimitry Andric    __self_view __sv = __t;
40440b57cec5SDimitry Andric    return compare(__pos1, __n1, __sv.data(), __sv.size());
40450b57cec5SDimitry Andric}
40460b57cec5SDimitry Andric
40470b57cec5SDimitry Andrictemplate <class _CharT, class _Traits, class _Allocator>
4048*81ad6265SDimitry Andricinline _LIBCPP_CONSTEXPR_AFTER_CXX17
40490b57cec5SDimitry Andricint
40500b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
40510b57cec5SDimitry Andric                                                   size_type __n1,
40520b57cec5SDimitry Andric                                                   const basic_string& __str) const
40530b57cec5SDimitry Andric{
40540b57cec5SDimitry Andric    return compare(__pos1, __n1, __str.data(), __str.size());
40550b57cec5SDimitry Andric}
40560b57cec5SDimitry Andric
40570b57cec5SDimitry Andrictemplate <class _CharT, class _Traits, class _Allocator>
40580b57cec5SDimitry Andrictemplate <class _Tp>
4059*81ad6265SDimitry Andric_LIBCPP_CONSTEXPR_AFTER_CXX17
4060349cc55cSDimitry Andric__enable_if_t
40610b57cec5SDimitry Andric<
40625ffd83dbSDimitry Andric    __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
40635ffd83dbSDimitry Andric    && !__is_same_uncvref<_Tp, basic_string<_CharT, _Traits, _Allocator> >::value,
40640b57cec5SDimitry Andric    int
40655ffd83dbSDimitry Andric>
40660b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
40670b57cec5SDimitry Andric                                                   size_type __n1,
40680b57cec5SDimitry Andric                                                   const _Tp& __t,
40690b57cec5SDimitry Andric                                                   size_type __pos2,
40700b57cec5SDimitry Andric                                                   size_type __n2) const
40710b57cec5SDimitry Andric{
40720b57cec5SDimitry Andric    __self_view __sv = __t;
40730b57cec5SDimitry Andric    return __self_view(*this).substr(__pos1, __n1).compare(__sv.substr(__pos2, __n2));
40740b57cec5SDimitry Andric}
40750b57cec5SDimitry Andric
40760b57cec5SDimitry Andrictemplate <class _CharT, class _Traits, class _Allocator>
4077*81ad6265SDimitry Andric_LIBCPP_CONSTEXPR_AFTER_CXX17
40780b57cec5SDimitry Andricint
40790b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
40800b57cec5SDimitry Andric                                                   size_type __n1,
40810b57cec5SDimitry Andric                                                   const basic_string& __str,
40820b57cec5SDimitry Andric                                                   size_type __pos2,
40830b57cec5SDimitry Andric                                                   size_type __n2) const
40840b57cec5SDimitry Andric{
40850b57cec5SDimitry Andric    return compare(__pos1, __n1, __self_view(__str), __pos2, __n2);
40860b57cec5SDimitry Andric}
40870b57cec5SDimitry Andric
40880b57cec5SDimitry Andrictemplate <class _CharT, class _Traits, class _Allocator>
4089*81ad6265SDimitry Andric_LIBCPP_CONSTEXPR_AFTER_CXX17
40900b57cec5SDimitry Andricint
40910b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>::compare(const value_type* __s) const _NOEXCEPT
40920b57cec5SDimitry Andric{
40930b57cec5SDimitry Andric    _LIBCPP_ASSERT(__s != nullptr, "string::compare(): received nullptr");
40940b57cec5SDimitry Andric    return compare(0, npos, __s, traits_type::length(__s));
40950b57cec5SDimitry Andric}
40960b57cec5SDimitry Andric
40970b57cec5SDimitry Andrictemplate <class _CharT, class _Traits, class _Allocator>
4098*81ad6265SDimitry Andric_LIBCPP_CONSTEXPR_AFTER_CXX17
40990b57cec5SDimitry Andricint
41000b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
41010b57cec5SDimitry Andric                                                   size_type __n1,
41020b57cec5SDimitry Andric                                                   const value_type* __s) const
41030b57cec5SDimitry Andric{
41040b57cec5SDimitry Andric    _LIBCPP_ASSERT(__s != nullptr, "string::compare(): received nullptr");
41050b57cec5SDimitry Andric    return compare(__pos1, __n1, __s, traits_type::length(__s));
41060b57cec5SDimitry Andric}
41070b57cec5SDimitry Andric
41080b57cec5SDimitry Andric// __invariants
41090b57cec5SDimitry Andric
41100b57cec5SDimitry Andrictemplate<class _CharT, class _Traits, class _Allocator>
4111*81ad6265SDimitry Andricinline _LIBCPP_CONSTEXPR_AFTER_CXX17
41120b57cec5SDimitry Andricbool
41130b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>::__invariants() const
41140b57cec5SDimitry Andric{
41150b57cec5SDimitry Andric    if (size() > capacity())
41160b57cec5SDimitry Andric        return false;
41170b57cec5SDimitry Andric    if (capacity() < __min_cap - 1)
41180b57cec5SDimitry Andric        return false;
4119e8d8bef9SDimitry Andric    if (data() == nullptr)
41200b57cec5SDimitry Andric        return false;
4121e8d8bef9SDimitry Andric    if (data()[size()] != value_type())
41220b57cec5SDimitry Andric        return false;
41230b57cec5SDimitry Andric    return true;
41240b57cec5SDimitry Andric}
41250b57cec5SDimitry Andric
41260b57cec5SDimitry Andric// __clear_and_shrink
41270b57cec5SDimitry Andric
41280b57cec5SDimitry Andrictemplate<class _CharT, class _Traits, class _Allocator>
4129*81ad6265SDimitry Andricinline _LIBCPP_CONSTEXPR_AFTER_CXX17
41300b57cec5SDimitry Andricvoid
41310b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>::__clear_and_shrink() _NOEXCEPT
41320b57cec5SDimitry Andric{
41330b57cec5SDimitry Andric    clear();
41340b57cec5SDimitry Andric    if(__is_long())
41350b57cec5SDimitry Andric    {
41360b57cec5SDimitry Andric        __alloc_traits::deallocate(__alloc(), __get_long_pointer(), capacity() + 1);
41370b57cec5SDimitry Andric        __set_long_cap(0);
41380b57cec5SDimitry Andric        __set_short_size(0);
4139e8d8bef9SDimitry Andric        traits_type::assign(*__get_short_pointer(), value_type());
41400b57cec5SDimitry Andric    }
41410b57cec5SDimitry Andric}
41420b57cec5SDimitry Andric
41430b57cec5SDimitry Andric// operator==
41440b57cec5SDimitry Andric
41450b57cec5SDimitry Andrictemplate<class _CharT, class _Traits, class _Allocator>
4146*81ad6265SDimitry Andricinline _LIBCPP_CONSTEXPR_AFTER_CXX17 _LIBCPP_HIDE_FROM_ABI
41470b57cec5SDimitry Andricbool
41480b57cec5SDimitry Andricoperator==(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
41490b57cec5SDimitry Andric           const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
41500b57cec5SDimitry Andric{
41510b57cec5SDimitry Andric    size_t __lhs_sz = __lhs.size();
41520b57cec5SDimitry Andric    return __lhs_sz == __rhs.size() && _Traits::compare(__lhs.data(),
41530b57cec5SDimitry Andric                                                        __rhs.data(),
41540b57cec5SDimitry Andric                                                        __lhs_sz) == 0;
41550b57cec5SDimitry Andric}
41560b57cec5SDimitry Andric
41570b57cec5SDimitry Andrictemplate<class _Allocator>
4158*81ad6265SDimitry Andricinline _LIBCPP_CONSTEXPR_AFTER_CXX17 _LIBCPP_HIDE_FROM_ABI
41590b57cec5SDimitry Andricbool
41600b57cec5SDimitry Andricoperator==(const basic_string<char, char_traits<char>, _Allocator>& __lhs,
41610b57cec5SDimitry Andric           const basic_string<char, char_traits<char>, _Allocator>& __rhs) _NOEXCEPT
41620b57cec5SDimitry Andric{
41630b57cec5SDimitry Andric    size_t __lhs_sz = __lhs.size();
41640b57cec5SDimitry Andric    if (__lhs_sz != __rhs.size())
41650b57cec5SDimitry Andric        return false;
41660b57cec5SDimitry Andric    const char* __lp = __lhs.data();
41670b57cec5SDimitry Andric    const char* __rp = __rhs.data();
41680b57cec5SDimitry Andric    if (__lhs.__is_long())
41690b57cec5SDimitry Andric        return char_traits<char>::compare(__lp, __rp, __lhs_sz) == 0;
41700b57cec5SDimitry Andric    for (; __lhs_sz != 0; --__lhs_sz, ++__lp, ++__rp)
41710b57cec5SDimitry Andric        if (*__lp != *__rp)
41720b57cec5SDimitry Andric            return false;
41730b57cec5SDimitry Andric    return true;
41740b57cec5SDimitry Andric}
41750b57cec5SDimitry Andric
41760b57cec5SDimitry Andrictemplate<class _CharT, class _Traits, class _Allocator>
4177*81ad6265SDimitry Andricinline _LIBCPP_CONSTEXPR_AFTER_CXX17 _LIBCPP_HIDE_FROM_ABI
41780b57cec5SDimitry Andricbool
41790b57cec5SDimitry Andricoperator==(const _CharT* __lhs,
41800b57cec5SDimitry Andric           const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
41810b57cec5SDimitry Andric{
41820b57cec5SDimitry Andric    typedef basic_string<_CharT, _Traits, _Allocator> _String;
41830b57cec5SDimitry Andric    _LIBCPP_ASSERT(__lhs != nullptr, "operator==(char*, basic_string): received nullptr");
41840b57cec5SDimitry Andric    size_t __lhs_len = _Traits::length(__lhs);
41850b57cec5SDimitry Andric    if (__lhs_len != __rhs.size()) return false;
41860b57cec5SDimitry Andric    return __rhs.compare(0, _String::npos, __lhs, __lhs_len) == 0;
41870b57cec5SDimitry Andric}
41880b57cec5SDimitry Andric
41890b57cec5SDimitry Andrictemplate<class _CharT, class _Traits, class _Allocator>
4190*81ad6265SDimitry Andricinline _LIBCPP_CONSTEXPR_AFTER_CXX17 _LIBCPP_HIDE_FROM_ABI
41910b57cec5SDimitry Andricbool
41920b57cec5SDimitry Andricoperator==(const basic_string<_CharT,_Traits,_Allocator>& __lhs,
41930b57cec5SDimitry Andric           const _CharT* __rhs) _NOEXCEPT
41940b57cec5SDimitry Andric{
41950b57cec5SDimitry Andric    typedef basic_string<_CharT, _Traits, _Allocator> _String;
41960b57cec5SDimitry Andric    _LIBCPP_ASSERT(__rhs != nullptr, "operator==(basic_string, char*): received nullptr");
41970b57cec5SDimitry Andric    size_t __rhs_len = _Traits::length(__rhs);
41980b57cec5SDimitry Andric    if (__rhs_len != __lhs.size()) return false;
41990b57cec5SDimitry Andric    return __lhs.compare(0, _String::npos, __rhs, __rhs_len) == 0;
42000b57cec5SDimitry Andric}
42010b57cec5SDimitry Andric
42020b57cec5SDimitry Andrictemplate<class _CharT, class _Traits, class _Allocator>
4203*81ad6265SDimitry Andricinline _LIBCPP_CONSTEXPR_AFTER_CXX17 _LIBCPP_HIDE_FROM_ABI
42040b57cec5SDimitry Andricbool
42050b57cec5SDimitry Andricoperator!=(const basic_string<_CharT,_Traits,_Allocator>& __lhs,
42060b57cec5SDimitry Andric           const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
42070b57cec5SDimitry Andric{
42080b57cec5SDimitry Andric    return !(__lhs == __rhs);
42090b57cec5SDimitry Andric}
42100b57cec5SDimitry Andric
42110b57cec5SDimitry Andrictemplate<class _CharT, class _Traits, class _Allocator>
4212*81ad6265SDimitry Andricinline _LIBCPP_CONSTEXPR_AFTER_CXX17 _LIBCPP_HIDE_FROM_ABI
42130b57cec5SDimitry Andricbool
42140b57cec5SDimitry Andricoperator!=(const _CharT* __lhs,
42150b57cec5SDimitry Andric           const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
42160b57cec5SDimitry Andric{
42170b57cec5SDimitry Andric    return !(__lhs == __rhs);
42180b57cec5SDimitry Andric}
42190b57cec5SDimitry Andric
42200b57cec5SDimitry Andrictemplate<class _CharT, class _Traits, class _Allocator>
4221*81ad6265SDimitry Andricinline _LIBCPP_CONSTEXPR_AFTER_CXX17 _LIBCPP_HIDE_FROM_ABI
42220b57cec5SDimitry Andricbool
42230b57cec5SDimitry Andricoperator!=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
42240b57cec5SDimitry Andric           const _CharT* __rhs) _NOEXCEPT
42250b57cec5SDimitry Andric{
42260b57cec5SDimitry Andric    return !(__lhs == __rhs);
42270b57cec5SDimitry Andric}
42280b57cec5SDimitry Andric
42290b57cec5SDimitry Andric// operator<
42300b57cec5SDimitry Andric
42310b57cec5SDimitry Andrictemplate<class _CharT, class _Traits, class _Allocator>
4232*81ad6265SDimitry Andricinline _LIBCPP_CONSTEXPR_AFTER_CXX17 _LIBCPP_HIDE_FROM_ABI
42330b57cec5SDimitry Andricbool
42340b57cec5SDimitry Andricoperator< (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
42350b57cec5SDimitry Andric           const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
42360b57cec5SDimitry Andric{
42370b57cec5SDimitry Andric    return __lhs.compare(__rhs) < 0;
42380b57cec5SDimitry Andric}
42390b57cec5SDimitry Andric
42400b57cec5SDimitry Andrictemplate<class _CharT, class _Traits, class _Allocator>
4241*81ad6265SDimitry Andricinline _LIBCPP_CONSTEXPR_AFTER_CXX17 _LIBCPP_HIDE_FROM_ABI
42420b57cec5SDimitry Andricbool
42430b57cec5SDimitry Andricoperator< (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
42440b57cec5SDimitry Andric           const _CharT* __rhs) _NOEXCEPT
42450b57cec5SDimitry Andric{
42460b57cec5SDimitry Andric    return __lhs.compare(__rhs) < 0;
42470b57cec5SDimitry Andric}
42480b57cec5SDimitry Andric
42490b57cec5SDimitry Andrictemplate<class _CharT, class _Traits, class _Allocator>
4250*81ad6265SDimitry Andricinline _LIBCPP_CONSTEXPR_AFTER_CXX17 _LIBCPP_HIDE_FROM_ABI
42510b57cec5SDimitry Andricbool
42520b57cec5SDimitry Andricoperator< (const _CharT* __lhs,
42530b57cec5SDimitry Andric           const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
42540b57cec5SDimitry Andric{
42550b57cec5SDimitry Andric    return __rhs.compare(__lhs) > 0;
42560b57cec5SDimitry Andric}
42570b57cec5SDimitry Andric
42580b57cec5SDimitry Andric// operator>
42590b57cec5SDimitry Andric
42600b57cec5SDimitry Andrictemplate<class _CharT, class _Traits, class _Allocator>
4261*81ad6265SDimitry Andricinline _LIBCPP_CONSTEXPR_AFTER_CXX17 _LIBCPP_HIDE_FROM_ABI
42620b57cec5SDimitry Andricbool
42630b57cec5SDimitry Andricoperator> (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
42640b57cec5SDimitry Andric           const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
42650b57cec5SDimitry Andric{
42660b57cec5SDimitry Andric    return __rhs < __lhs;
42670b57cec5SDimitry Andric}
42680b57cec5SDimitry Andric
42690b57cec5SDimitry Andrictemplate<class _CharT, class _Traits, class _Allocator>
4270*81ad6265SDimitry Andricinline _LIBCPP_CONSTEXPR_AFTER_CXX17 _LIBCPP_HIDE_FROM_ABI
42710b57cec5SDimitry Andricbool
42720b57cec5SDimitry Andricoperator> (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
42730b57cec5SDimitry Andric           const _CharT* __rhs) _NOEXCEPT
42740b57cec5SDimitry Andric{
42750b57cec5SDimitry Andric    return __rhs < __lhs;
42760b57cec5SDimitry Andric}
42770b57cec5SDimitry Andric
42780b57cec5SDimitry Andrictemplate<class _CharT, class _Traits, class _Allocator>
4279*81ad6265SDimitry Andricinline _LIBCPP_CONSTEXPR_AFTER_CXX17 _LIBCPP_HIDE_FROM_ABI
42800b57cec5SDimitry Andricbool
42810b57cec5SDimitry Andricoperator> (const _CharT* __lhs,
42820b57cec5SDimitry Andric           const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
42830b57cec5SDimitry Andric{
42840b57cec5SDimitry Andric    return __rhs < __lhs;
42850b57cec5SDimitry Andric}
42860b57cec5SDimitry Andric
42870b57cec5SDimitry Andric// operator<=
42880b57cec5SDimitry Andric
42890b57cec5SDimitry Andrictemplate<class _CharT, class _Traits, class _Allocator>
4290*81ad6265SDimitry Andricinline _LIBCPP_CONSTEXPR_AFTER_CXX17 _LIBCPP_HIDE_FROM_ABI
42910b57cec5SDimitry Andricbool
42920b57cec5SDimitry Andricoperator<=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
42930b57cec5SDimitry Andric           const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
42940b57cec5SDimitry Andric{
42950b57cec5SDimitry Andric    return !(__rhs < __lhs);
42960b57cec5SDimitry Andric}
42970b57cec5SDimitry Andric
42980b57cec5SDimitry Andrictemplate<class _CharT, class _Traits, class _Allocator>
4299*81ad6265SDimitry Andricinline _LIBCPP_CONSTEXPR_AFTER_CXX17 _LIBCPP_HIDE_FROM_ABI
43000b57cec5SDimitry Andricbool
43010b57cec5SDimitry Andricoperator<=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
43020b57cec5SDimitry Andric           const _CharT* __rhs) _NOEXCEPT
43030b57cec5SDimitry Andric{
43040b57cec5SDimitry Andric    return !(__rhs < __lhs);
43050b57cec5SDimitry Andric}
43060b57cec5SDimitry Andric
43070b57cec5SDimitry Andrictemplate<class _CharT, class _Traits, class _Allocator>
4308*81ad6265SDimitry Andricinline _LIBCPP_CONSTEXPR_AFTER_CXX17 _LIBCPP_HIDE_FROM_ABI
43090b57cec5SDimitry Andricbool
43100b57cec5SDimitry Andricoperator<=(const _CharT* __lhs,
43110b57cec5SDimitry Andric           const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
43120b57cec5SDimitry Andric{
43130b57cec5SDimitry Andric    return !(__rhs < __lhs);
43140b57cec5SDimitry Andric}
43150b57cec5SDimitry Andric
43160b57cec5SDimitry Andric// operator>=
43170b57cec5SDimitry Andric
43180b57cec5SDimitry Andrictemplate<class _CharT, class _Traits, class _Allocator>
4319*81ad6265SDimitry Andricinline _LIBCPP_CONSTEXPR_AFTER_CXX17 _LIBCPP_HIDE_FROM_ABI
43200b57cec5SDimitry Andricbool
43210b57cec5SDimitry Andricoperator>=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
43220b57cec5SDimitry Andric           const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
43230b57cec5SDimitry Andric{
43240b57cec5SDimitry Andric    return !(__lhs < __rhs);
43250b57cec5SDimitry Andric}
43260b57cec5SDimitry Andric
43270b57cec5SDimitry Andrictemplate<class _CharT, class _Traits, class _Allocator>
4328*81ad6265SDimitry Andricinline _LIBCPP_CONSTEXPR_AFTER_CXX17 _LIBCPP_HIDE_FROM_ABI
43290b57cec5SDimitry Andricbool
43300b57cec5SDimitry Andricoperator>=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
43310b57cec5SDimitry Andric           const _CharT* __rhs) _NOEXCEPT
43320b57cec5SDimitry Andric{
43330b57cec5SDimitry Andric    return !(__lhs < __rhs);
43340b57cec5SDimitry Andric}
43350b57cec5SDimitry Andric
43360b57cec5SDimitry Andrictemplate<class _CharT, class _Traits, class _Allocator>
4337*81ad6265SDimitry Andricinline _LIBCPP_CONSTEXPR_AFTER_CXX17 _LIBCPP_HIDE_FROM_ABI
43380b57cec5SDimitry Andricbool
43390b57cec5SDimitry Andricoperator>=(const _CharT* __lhs,
43400b57cec5SDimitry Andric           const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
43410b57cec5SDimitry Andric{
43420b57cec5SDimitry Andric    return !(__lhs < __rhs);
43430b57cec5SDimitry Andric}
43440b57cec5SDimitry Andric
43450b57cec5SDimitry Andric// operator +
43460b57cec5SDimitry Andric
43470b57cec5SDimitry Andrictemplate<class _CharT, class _Traits, class _Allocator>
4348*81ad6265SDimitry Andric_LIBCPP_CONSTEXPR_AFTER_CXX17
43490b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>
43500b57cec5SDimitry Andricoperator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
43510b57cec5SDimitry Andric          const basic_string<_CharT, _Traits, _Allocator>& __rhs)
43520b57cec5SDimitry Andric{
4353*81ad6265SDimitry Andric    using _String = basic_string<_CharT, _Traits, _Allocator>;
4354*81ad6265SDimitry Andric    auto __lhs_sz = __lhs.size();
4355*81ad6265SDimitry Andric    auto __rhs_sz = __rhs.size();
4356*81ad6265SDimitry Andric    _String __r(__uninitialized_size_tag(),
4357*81ad6265SDimitry Andric                __lhs_sz + __rhs_sz,
4358*81ad6265SDimitry Andric                _String::__alloc_traits::select_on_container_copy_construction(__lhs.get_allocator()));
4359*81ad6265SDimitry Andric    auto __ptr = std::__to_address(__r.__get_pointer());
4360*81ad6265SDimitry Andric    _Traits::copy(__ptr, __lhs.data(), __lhs_sz);
4361*81ad6265SDimitry Andric    _Traits::copy(__ptr + __lhs_sz, __rhs.data(), __rhs_sz);
4362*81ad6265SDimitry Andric    _Traits::assign(__ptr + __lhs_sz + __rhs_sz, 1, _CharT());
43630b57cec5SDimitry Andric    return __r;
43640b57cec5SDimitry Andric}
43650b57cec5SDimitry Andric
43660b57cec5SDimitry Andrictemplate<class _CharT, class _Traits, class _Allocator>
4367*81ad6265SDimitry Andric_LIBCPP_CONSTEXPR_AFTER_CXX17
43680b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>
43690b57cec5SDimitry Andricoperator+(const _CharT* __lhs , const basic_string<_CharT,_Traits,_Allocator>& __rhs)
43700b57cec5SDimitry Andric{
4371*81ad6265SDimitry Andric    using _String = basic_string<_CharT, _Traits, _Allocator>;
4372*81ad6265SDimitry Andric    auto __lhs_sz = _Traits::length(__lhs);
4373*81ad6265SDimitry Andric    auto __rhs_sz = __rhs.size();
4374*81ad6265SDimitry Andric    _String __r(__uninitialized_size_tag(),
4375*81ad6265SDimitry Andric                __lhs_sz + __rhs_sz,
4376*81ad6265SDimitry Andric                _String::__alloc_traits::select_on_container_copy_construction(__rhs.get_allocator()));
4377*81ad6265SDimitry Andric    auto __ptr = std::__to_address(__r.__get_pointer());
4378*81ad6265SDimitry Andric    _Traits::copy(__ptr, __lhs, __lhs_sz);
4379*81ad6265SDimitry Andric    _Traits::copy(__ptr + __lhs_sz, __rhs.data(), __rhs_sz);
4380*81ad6265SDimitry Andric    _Traits::assign(__ptr + __lhs_sz + __rhs_sz, 1, _CharT());
43810b57cec5SDimitry Andric    return __r;
43820b57cec5SDimitry Andric}
43830b57cec5SDimitry Andric
43840b57cec5SDimitry Andrictemplate<class _CharT, class _Traits, class _Allocator>
4385*81ad6265SDimitry Andric_LIBCPP_CONSTEXPR_AFTER_CXX17
43860b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>
43870b57cec5SDimitry Andricoperator+(_CharT __lhs, const basic_string<_CharT,_Traits,_Allocator>& __rhs)
43880b57cec5SDimitry Andric{
4389*81ad6265SDimitry Andric    using _String = basic_string<_CharT, _Traits, _Allocator>;
4390*81ad6265SDimitry Andric    typename _String::size_type __rhs_sz = __rhs.size();
4391*81ad6265SDimitry Andric    _String __r(__uninitialized_size_tag(),
4392*81ad6265SDimitry Andric                __rhs_sz + 1,
4393*81ad6265SDimitry Andric                _String::__alloc_traits::select_on_container_copy_construction(__rhs.get_allocator()));
4394*81ad6265SDimitry Andric    auto __ptr = std::__to_address(__r.__get_pointer());
4395*81ad6265SDimitry Andric    _Traits::assign(__ptr, 1, __lhs);
4396*81ad6265SDimitry Andric    _Traits::copy(__ptr + 1, __rhs.data(), __rhs_sz);
4397*81ad6265SDimitry Andric    _Traits::assign(__ptr + 1 + __rhs_sz, 1, _CharT());
43980b57cec5SDimitry Andric    return __r;
43990b57cec5SDimitry Andric}
44000b57cec5SDimitry Andric
44010b57cec5SDimitry Andrictemplate<class _CharT, class _Traits, class _Allocator>
4402*81ad6265SDimitry Andricinline _LIBCPP_CONSTEXPR_AFTER_CXX17
44030b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>
44040b57cec5SDimitry Andricoperator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, const _CharT* __rhs)
44050b57cec5SDimitry Andric{
4406*81ad6265SDimitry Andric    using _String = basic_string<_CharT, _Traits, _Allocator>;
4407*81ad6265SDimitry Andric    typename _String::size_type __lhs_sz = __lhs.size();
4408*81ad6265SDimitry Andric    typename _String::size_type __rhs_sz = _Traits::length(__rhs);
4409*81ad6265SDimitry Andric    _String __r(__uninitialized_size_tag(),
4410*81ad6265SDimitry Andric                __lhs_sz + __rhs_sz,
4411*81ad6265SDimitry Andric                _String::__alloc_traits::select_on_container_copy_construction(__lhs.get_allocator()));
4412*81ad6265SDimitry Andric    auto __ptr = std::__to_address(__r.__get_pointer());
4413*81ad6265SDimitry Andric    _Traits::copy(__ptr, __lhs.data(), __lhs_sz);
4414*81ad6265SDimitry Andric    _Traits::copy(__ptr + __lhs_sz, __rhs, __rhs_sz);
4415*81ad6265SDimitry Andric    _Traits::assign(__ptr + __lhs_sz + __rhs_sz, 1, _CharT());
44160b57cec5SDimitry Andric    return __r;
44170b57cec5SDimitry Andric}
44180b57cec5SDimitry Andric
44190b57cec5SDimitry Andrictemplate<class _CharT, class _Traits, class _Allocator>
4420*81ad6265SDimitry Andric_LIBCPP_CONSTEXPR_AFTER_CXX17
44210b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>
44220b57cec5SDimitry Andricoperator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, _CharT __rhs)
44230b57cec5SDimitry Andric{
4424*81ad6265SDimitry Andric    using _String = basic_string<_CharT, _Traits, _Allocator>;
4425*81ad6265SDimitry Andric    typename _String::size_type __lhs_sz = __lhs.size();
4426*81ad6265SDimitry Andric    _String __r(__uninitialized_size_tag(),
4427*81ad6265SDimitry Andric                __lhs_sz + 1,
4428*81ad6265SDimitry Andric                _String::__alloc_traits::select_on_container_copy_construction(__lhs.get_allocator()));
4429*81ad6265SDimitry Andric    auto __ptr = std::__to_address(__r.__get_pointer());
4430*81ad6265SDimitry Andric    _Traits::copy(__ptr, __lhs.data(), __lhs_sz);
4431*81ad6265SDimitry Andric    _Traits::assign(__ptr + __lhs_sz, 1, __rhs);
4432*81ad6265SDimitry Andric    _Traits::assign(__ptr + 1 + __lhs_sz, 1, _CharT());
44330b57cec5SDimitry Andric    return __r;
44340b57cec5SDimitry Andric}
44350b57cec5SDimitry Andric
44360b57cec5SDimitry Andric#ifndef _LIBCPP_CXX03_LANG
44370b57cec5SDimitry Andric
44380b57cec5SDimitry Andrictemplate<class _CharT, class _Traits, class _Allocator>
4439*81ad6265SDimitry Andricinline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
44400b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>
44410b57cec5SDimitry Andricoperator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, const basic_string<_CharT, _Traits, _Allocator>& __rhs)
44420b57cec5SDimitry Andric{
4443*81ad6265SDimitry Andric    return std::move(__lhs.append(__rhs));
44440b57cec5SDimitry Andric}
44450b57cec5SDimitry Andric
44460b57cec5SDimitry Andrictemplate<class _CharT, class _Traits, class _Allocator>
4447*81ad6265SDimitry Andricinline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
44480b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>
44490b57cec5SDimitry Andricoperator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, basic_string<_CharT, _Traits, _Allocator>&& __rhs)
44500b57cec5SDimitry Andric{
4451*81ad6265SDimitry Andric    return std::move(__rhs.insert(0, __lhs));
44520b57cec5SDimitry Andric}
44530b57cec5SDimitry Andric
44540b57cec5SDimitry Andrictemplate<class _CharT, class _Traits, class _Allocator>
4455*81ad6265SDimitry Andricinline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
44560b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>
44570b57cec5SDimitry Andricoperator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, basic_string<_CharT, _Traits, _Allocator>&& __rhs)
44580b57cec5SDimitry Andric{
4459*81ad6265SDimitry Andric    return std::move(__lhs.append(__rhs));
44600b57cec5SDimitry Andric}
44610b57cec5SDimitry Andric
44620b57cec5SDimitry Andrictemplate<class _CharT, class _Traits, class _Allocator>
4463*81ad6265SDimitry Andricinline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
44640b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>
44650b57cec5SDimitry Andricoperator+(const _CharT* __lhs , basic_string<_CharT,_Traits,_Allocator>&& __rhs)
44660b57cec5SDimitry Andric{
4467*81ad6265SDimitry Andric    return std::move(__rhs.insert(0, __lhs));
44680b57cec5SDimitry Andric}
44690b57cec5SDimitry Andric
44700b57cec5SDimitry Andrictemplate<class _CharT, class _Traits, class _Allocator>
4471*81ad6265SDimitry Andricinline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
44720b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>
44730b57cec5SDimitry Andricoperator+(_CharT __lhs, basic_string<_CharT,_Traits,_Allocator>&& __rhs)
44740b57cec5SDimitry Andric{
44750b57cec5SDimitry Andric    __rhs.insert(__rhs.begin(), __lhs);
4476*81ad6265SDimitry Andric    return std::move(__rhs);
44770b57cec5SDimitry Andric}
44780b57cec5SDimitry Andric
44790b57cec5SDimitry Andrictemplate<class _CharT, class _Traits, class _Allocator>
4480*81ad6265SDimitry Andricinline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
44810b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>
44820b57cec5SDimitry Andricoperator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, const _CharT* __rhs)
44830b57cec5SDimitry Andric{
4484*81ad6265SDimitry Andric    return std::move(__lhs.append(__rhs));
44850b57cec5SDimitry Andric}
44860b57cec5SDimitry Andric
44870b57cec5SDimitry Andrictemplate<class _CharT, class _Traits, class _Allocator>
4488*81ad6265SDimitry Andricinline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
44890b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>
44900b57cec5SDimitry Andricoperator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, _CharT __rhs)
44910b57cec5SDimitry Andric{
44920b57cec5SDimitry Andric    __lhs.push_back(__rhs);
4493*81ad6265SDimitry Andric    return std::move(__lhs);
44940b57cec5SDimitry Andric}
44950b57cec5SDimitry Andric
44960b57cec5SDimitry Andric#endif // _LIBCPP_CXX03_LANG
44970b57cec5SDimitry Andric
44980b57cec5SDimitry Andric// swap
44990b57cec5SDimitry Andric
45000b57cec5SDimitry Andrictemplate<class _CharT, class _Traits, class _Allocator>
4501*81ad6265SDimitry Andricinline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
45020b57cec5SDimitry Andricvoid
45030b57cec5SDimitry Andricswap(basic_string<_CharT, _Traits, _Allocator>& __lhs,
45040b57cec5SDimitry Andric     basic_string<_CharT, _Traits, _Allocator>& __rhs)
45050b57cec5SDimitry Andric     _NOEXCEPT_(_NOEXCEPT_(__lhs.swap(__rhs)))
45060b57cec5SDimitry Andric{
45070b57cec5SDimitry Andric    __lhs.swap(__rhs);
45080b57cec5SDimitry Andric}
45090b57cec5SDimitry Andric
4510e8d8bef9SDimitry Andric_LIBCPP_FUNC_VIS int                stoi  (const string& __str, size_t* __idx = nullptr, int __base = 10);
4511e8d8bef9SDimitry Andric_LIBCPP_FUNC_VIS long               stol  (const string& __str, size_t* __idx = nullptr, int __base = 10);
4512e8d8bef9SDimitry Andric_LIBCPP_FUNC_VIS unsigned long      stoul (const string& __str, size_t* __idx = nullptr, int __base = 10);
4513e8d8bef9SDimitry Andric_LIBCPP_FUNC_VIS long long          stoll (const string& __str, size_t* __idx = nullptr, int __base = 10);
4514e8d8bef9SDimitry Andric_LIBCPP_FUNC_VIS unsigned long long stoull(const string& __str, size_t* __idx = nullptr, int __base = 10);
45150b57cec5SDimitry Andric
4516e8d8bef9SDimitry Andric_LIBCPP_FUNC_VIS float       stof (const string& __str, size_t* __idx = nullptr);
4517e8d8bef9SDimitry Andric_LIBCPP_FUNC_VIS double      stod (const string& __str, size_t* __idx = nullptr);
4518e8d8bef9SDimitry Andric_LIBCPP_FUNC_VIS long double stold(const string& __str, size_t* __idx = nullptr);
45190b57cec5SDimitry Andric
45200b57cec5SDimitry Andric_LIBCPP_FUNC_VIS string to_string(int __val);
45210b57cec5SDimitry Andric_LIBCPP_FUNC_VIS string to_string(unsigned __val);
45220b57cec5SDimitry Andric_LIBCPP_FUNC_VIS string to_string(long __val);
45230b57cec5SDimitry Andric_LIBCPP_FUNC_VIS string to_string(unsigned long __val);
45240b57cec5SDimitry Andric_LIBCPP_FUNC_VIS string to_string(long long __val);
45250b57cec5SDimitry Andric_LIBCPP_FUNC_VIS string to_string(unsigned long long __val);
45260b57cec5SDimitry Andric_LIBCPP_FUNC_VIS string to_string(float __val);
45270b57cec5SDimitry Andric_LIBCPP_FUNC_VIS string to_string(double __val);
45280b57cec5SDimitry Andric_LIBCPP_FUNC_VIS string to_string(long double __val);
45290b57cec5SDimitry Andric
4530349cc55cSDimitry Andric#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
4531e8d8bef9SDimitry Andric_LIBCPP_FUNC_VIS int                stoi  (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4532e8d8bef9SDimitry Andric_LIBCPP_FUNC_VIS long               stol  (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4533e8d8bef9SDimitry Andric_LIBCPP_FUNC_VIS unsigned long      stoul (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4534e8d8bef9SDimitry Andric_LIBCPP_FUNC_VIS long long          stoll (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4535e8d8bef9SDimitry Andric_LIBCPP_FUNC_VIS unsigned long long stoull(const wstring& __str, size_t* __idx = nullptr, int __base = 10);
45360b57cec5SDimitry Andric
4537e8d8bef9SDimitry Andric_LIBCPP_FUNC_VIS float       stof (const wstring& __str, size_t* __idx = nullptr);
4538e8d8bef9SDimitry Andric_LIBCPP_FUNC_VIS double      stod (const wstring& __str, size_t* __idx = nullptr);
4539e8d8bef9SDimitry Andric_LIBCPP_FUNC_VIS long double stold(const wstring& __str, size_t* __idx = nullptr);
45400b57cec5SDimitry Andric
45410b57cec5SDimitry Andric_LIBCPP_FUNC_VIS wstring to_wstring(int __val);
45420b57cec5SDimitry Andric_LIBCPP_FUNC_VIS wstring to_wstring(unsigned __val);
45430b57cec5SDimitry Andric_LIBCPP_FUNC_VIS wstring to_wstring(long __val);
45440b57cec5SDimitry Andric_LIBCPP_FUNC_VIS wstring to_wstring(unsigned long __val);
45450b57cec5SDimitry Andric_LIBCPP_FUNC_VIS wstring to_wstring(long long __val);
45460b57cec5SDimitry Andric_LIBCPP_FUNC_VIS wstring to_wstring(unsigned long long __val);
45470b57cec5SDimitry Andric_LIBCPP_FUNC_VIS wstring to_wstring(float __val);
45480b57cec5SDimitry Andric_LIBCPP_FUNC_VIS wstring to_wstring(double __val);
45490b57cec5SDimitry Andric_LIBCPP_FUNC_VIS wstring to_wstring(long double __val);
4550349cc55cSDimitry Andric#endif // _LIBCPP_HAS_NO_WIDE_CHARACTERS
45510b57cec5SDimitry Andric
45520b57cec5SDimitry Andrictemplate<class _CharT, class _Traits, class _Allocator>
4553fe6060f1SDimitry Andric_LIBCPP_TEMPLATE_DATA_VIS
45540b57cec5SDimitry Andricconst typename basic_string<_CharT, _Traits, _Allocator>::size_type
45550b57cec5SDimitry Andric               basic_string<_CharT, _Traits, _Allocator>::npos;
45560b57cec5SDimitry Andric
45570b57cec5SDimitry Andrictemplate <class _CharT, class _Allocator>
45580b57cec5SDimitry Andricstruct _LIBCPP_TEMPLATE_VIS
45590b57cec5SDimitry Andric    hash<basic_string<_CharT, char_traits<_CharT>, _Allocator> >
4560*81ad6265SDimitry Andric    : public __unary_function<basic_string<_CharT, char_traits<_CharT>, _Allocator>, size_t>
45610b57cec5SDimitry Andric{
45620b57cec5SDimitry Andric    size_t
45630b57cec5SDimitry Andric    operator()(const basic_string<_CharT, char_traits<_CharT>, _Allocator>& __val) const _NOEXCEPT
45640b57cec5SDimitry Andric    { return __do_string_hash(__val.data(), __val.data() + __val.size()); }
45650b57cec5SDimitry Andric};
45660b57cec5SDimitry Andric
45670b57cec5SDimitry Andrictemplate<class _CharT, class _Traits, class _Allocator>
45680b57cec5SDimitry Andricbasic_ostream<_CharT, _Traits>&
45690b57cec5SDimitry Andricoperator<<(basic_ostream<_CharT, _Traits>& __os,
45700b57cec5SDimitry Andric           const basic_string<_CharT, _Traits, _Allocator>& __str);
45710b57cec5SDimitry Andric
45720b57cec5SDimitry Andrictemplate<class _CharT, class _Traits, class _Allocator>
45730b57cec5SDimitry Andricbasic_istream<_CharT, _Traits>&
45740b57cec5SDimitry Andricoperator>>(basic_istream<_CharT, _Traits>& __is,
45750b57cec5SDimitry Andric           basic_string<_CharT, _Traits, _Allocator>& __str);
45760b57cec5SDimitry Andric
45770b57cec5SDimitry Andrictemplate<class _CharT, class _Traits, class _Allocator>
45780b57cec5SDimitry Andricbasic_istream<_CharT, _Traits>&
45790b57cec5SDimitry Andricgetline(basic_istream<_CharT, _Traits>& __is,
45800b57cec5SDimitry Andric        basic_string<_CharT, _Traits, _Allocator>& __str, _CharT __dlm);
45810b57cec5SDimitry Andric
45820b57cec5SDimitry Andrictemplate<class _CharT, class _Traits, class _Allocator>
4583*81ad6265SDimitry Andricinline _LIBCPP_HIDE_FROM_ABI
45840b57cec5SDimitry Andricbasic_istream<_CharT, _Traits>&
45850b57cec5SDimitry Andricgetline(basic_istream<_CharT, _Traits>& __is,
45860b57cec5SDimitry Andric        basic_string<_CharT, _Traits, _Allocator>& __str);
45870b57cec5SDimitry Andric
45880b57cec5SDimitry Andrictemplate<class _CharT, class _Traits, class _Allocator>
4589*81ad6265SDimitry Andricinline _LIBCPP_HIDE_FROM_ABI
45900b57cec5SDimitry Andricbasic_istream<_CharT, _Traits>&
45910b57cec5SDimitry Andricgetline(basic_istream<_CharT, _Traits>&& __is,
45920b57cec5SDimitry Andric        basic_string<_CharT, _Traits, _Allocator>& __str, _CharT __dlm);
45930b57cec5SDimitry Andric
45940b57cec5SDimitry Andrictemplate<class _CharT, class _Traits, class _Allocator>
4595*81ad6265SDimitry Andricinline _LIBCPP_HIDE_FROM_ABI
45960b57cec5SDimitry Andricbasic_istream<_CharT, _Traits>&
45970b57cec5SDimitry Andricgetline(basic_istream<_CharT, _Traits>&& __is,
45980b57cec5SDimitry Andric        basic_string<_CharT, _Traits, _Allocator>& __str);
45990b57cec5SDimitry Andric
46000b57cec5SDimitry Andric#if _LIBCPP_STD_VER > 17
46010b57cec5SDimitry Andrictemplate <class _CharT, class _Traits, class _Allocator, class _Up>
4602*81ad6265SDimitry Andricinline _LIBCPP_HIDE_FROM_ABI
46035ffd83dbSDimitry Andric    typename basic_string<_CharT, _Traits, _Allocator>::size_type
46045ffd83dbSDimitry Andric    erase(basic_string<_CharT, _Traits, _Allocator>& __str, const _Up& __v) {
46055ffd83dbSDimitry Andric  auto __old_size = __str.size();
4606*81ad6265SDimitry Andric  __str.erase(std::remove(__str.begin(), __str.end(), __v), __str.end());
46075ffd83dbSDimitry Andric  return __old_size - __str.size();
46085ffd83dbSDimitry Andric}
46090b57cec5SDimitry Andric
46100b57cec5SDimitry Andrictemplate <class _CharT, class _Traits, class _Allocator, class _Predicate>
4611*81ad6265SDimitry Andricinline _LIBCPP_HIDE_FROM_ABI
46125ffd83dbSDimitry Andric    typename basic_string<_CharT, _Traits, _Allocator>::size_type
46135ffd83dbSDimitry Andric    erase_if(basic_string<_CharT, _Traits, _Allocator>& __str,
46145ffd83dbSDimitry Andric             _Predicate __pred) {
46155ffd83dbSDimitry Andric  auto __old_size = __str.size();
4616*81ad6265SDimitry Andric  __str.erase(std::remove_if(__str.begin(), __str.end(), __pred),
46175ffd83dbSDimitry Andric              __str.end());
46185ffd83dbSDimitry Andric  return __old_size - __str.size();
46195ffd83dbSDimitry Andric}
46200b57cec5SDimitry Andric#endif
46210b57cec5SDimitry Andric
4622*81ad6265SDimitry Andric#ifdef _LIBCPP_ENABLE_DEBUG_MODE
46230b57cec5SDimitry Andric
46240b57cec5SDimitry Andrictemplate<class _CharT, class _Traits, class _Allocator>
46250b57cec5SDimitry Andricbool
46260b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>::__dereferenceable(const const_iterator* __i) const
46270b57cec5SDimitry Andric{
4628*81ad6265SDimitry Andric    return data() <= std::__to_address(__i->base()) &&
4629*81ad6265SDimitry Andric           std::__to_address(__i->base()) < data() + size();
46300b57cec5SDimitry Andric}
46310b57cec5SDimitry Andric
46320b57cec5SDimitry Andrictemplate<class _CharT, class _Traits, class _Allocator>
46330b57cec5SDimitry Andricbool
46340b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>::__decrementable(const const_iterator* __i) const
46350b57cec5SDimitry Andric{
4636*81ad6265SDimitry Andric    return data() < std::__to_address(__i->base()) &&
4637*81ad6265SDimitry Andric           std::__to_address(__i->base()) <= data() + size();
46380b57cec5SDimitry Andric}
46390b57cec5SDimitry Andric
46400b57cec5SDimitry Andrictemplate<class _CharT, class _Traits, class _Allocator>
46410b57cec5SDimitry Andricbool
46420b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>::__addable(const const_iterator* __i, ptrdiff_t __n) const
46430b57cec5SDimitry Andric{
4644*81ad6265SDimitry Andric    const value_type* __p = std::__to_address(__i->base()) + __n;
464504eeddc0SDimitry Andric    return data() <= __p && __p <= data() + size();
46460b57cec5SDimitry Andric}
46470b57cec5SDimitry Andric
46480b57cec5SDimitry Andrictemplate<class _CharT, class _Traits, class _Allocator>
46490b57cec5SDimitry Andricbool
46500b57cec5SDimitry Andricbasic_string<_CharT, _Traits, _Allocator>::__subscriptable(const const_iterator* __i, ptrdiff_t __n) const
46510b57cec5SDimitry Andric{
4652*81ad6265SDimitry Andric    const value_type* __p = std::__to_address(__i->base()) + __n;
465304eeddc0SDimitry Andric    return data() <= __p && __p < data() + size();
46540b57cec5SDimitry Andric}
46550b57cec5SDimitry Andric
4656*81ad6265SDimitry Andric#endif // _LIBCPP_ENABLE_DEBUG_MODE
46570b57cec5SDimitry Andric
46580b57cec5SDimitry Andric#if _LIBCPP_STD_VER > 11
46590b57cec5SDimitry Andric// Literal suffixes for basic_string [basic.string.literals]
46600b57cec5SDimitry Andricinline namespace literals
46610b57cec5SDimitry Andric{
46620b57cec5SDimitry Andric  inline namespace string_literals
46630b57cec5SDimitry Andric  {
4664*81ad6265SDimitry Andric    inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
46650b57cec5SDimitry Andric    basic_string<char> operator "" s( const char *__str, size_t __len )
46660b57cec5SDimitry Andric    {
46670b57cec5SDimitry Andric        return basic_string<char> (__str, __len);
46680b57cec5SDimitry Andric    }
46690b57cec5SDimitry Andric
4670349cc55cSDimitry Andric#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
4671*81ad6265SDimitry Andric    inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
46720b57cec5SDimitry Andric    basic_string<wchar_t> operator "" s( const wchar_t *__str, size_t __len )
46730b57cec5SDimitry Andric    {
46740b57cec5SDimitry Andric        return basic_string<wchar_t> (__str, __len);
46750b57cec5SDimitry Andric    }
4676349cc55cSDimitry Andric#endif
46770b57cec5SDimitry Andric
4678fe6060f1SDimitry Andric#ifndef _LIBCPP_HAS_NO_CHAR8_T
4679*81ad6265SDimitry Andric    inline _LIBCPP_HIDE_FROM_ABI constexpr
46800b57cec5SDimitry Andric    basic_string<char8_t> operator "" s(const char8_t *__str, size_t __len) _NOEXCEPT
46810b57cec5SDimitry Andric    {
46820b57cec5SDimitry Andric        return basic_string<char8_t> (__str, __len);
46830b57cec5SDimitry Andric    }
46840b57cec5SDimitry Andric#endif
46850b57cec5SDimitry Andric
4686*81ad6265SDimitry Andric    inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
46870b57cec5SDimitry Andric    basic_string<char16_t> operator "" s( const char16_t *__str, size_t __len )
46880b57cec5SDimitry Andric    {
46890b57cec5SDimitry Andric        return basic_string<char16_t> (__str, __len);
46900b57cec5SDimitry Andric    }
46910b57cec5SDimitry Andric
4692*81ad6265SDimitry Andric    inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
46930b57cec5SDimitry Andric    basic_string<char32_t> operator "" s( const char32_t *__str, size_t __len )
46940b57cec5SDimitry Andric    {
46950b57cec5SDimitry Andric        return basic_string<char32_t> (__str, __len);
46960b57cec5SDimitry Andric    }
46970eae32dcSDimitry Andric  } // namespace string_literals
46980eae32dcSDimitry Andric} // namespace literals
4699*81ad6265SDimitry Andric
4700*81ad6265SDimitry Andric#if _LIBCPP_STD_VER > 17
4701*81ad6265SDimitry Andrictemplate <>
4702*81ad6265SDimitry Andricinline constexpr bool __format::__enable_insertable<std::basic_string<char>> = true;
4703*81ad6265SDimitry Andric#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
4704*81ad6265SDimitry Andrictemplate <>
4705*81ad6265SDimitry Andricinline constexpr bool __format::__enable_insertable<std::basic_string<wchar_t>> = true;
4706*81ad6265SDimitry Andric#endif
4707*81ad6265SDimitry Andric#endif
4708*81ad6265SDimitry Andric
47090b57cec5SDimitry Andric#endif
47100b57cec5SDimitry Andric
47110b57cec5SDimitry Andric_LIBCPP_END_NAMESPACE_STD
47120b57cec5SDimitry Andric
47130b57cec5SDimitry Andric_LIBCPP_POP_MACROS
47140b57cec5SDimitry Andric
47150b57cec5SDimitry Andric#endif // _LIBCPP_STRING
4716