tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

cxx20_erase_string.h (1585B)


      1 // Copyright 2021 The Chromium Authors
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #ifndef BASE_CONTAINERS_CXX20_ERASE_STRING_H_
      6 #define BASE_CONTAINERS_CXX20_ERASE_STRING_H_
      7 
      8 #include <algorithm>
      9 #include <iterator>
     10 #include <string>
     11 
     12 namespace base {
     13 
     14 // Erase/EraseIf are based on C++20's uniform container erasure API:
     15 // - https://eel.is/c++draft/libraryindex#:erase
     16 // - https://eel.is/c++draft/libraryindex#:erase_if
     17 // They provide a generic way to erase elements from a container.
     18 // The functions here implement these for the standard containers until those
     19 // functions are available in the C++ standard.
     20 // Note: there is no std::erase for standard associative containers so we don't
     21 // have it either.
     22 
     23 template <typename CharT, typename Traits, typename Allocator, typename Value>
     24 size_t Erase(std::basic_string<CharT, Traits, Allocator>& container,
     25             const Value& value) {
     26  auto it = std::remove(container.begin(), container.end(), value);
     27  size_t removed = std::distance(it, container.end());
     28  container.erase(it, container.end());
     29  return removed;
     30 }
     31 
     32 template <typename CharT, typename Traits, typename Allocator, class Predicate>
     33 size_t EraseIf(std::basic_string<CharT, Traits, Allocator>& container,
     34               Predicate pred) {
     35  auto it = std::remove_if(container.begin(), container.end(), pred);
     36  size_t removed = std::distance(it, container.end());
     37  container.erase(it, container.end());
     38  return removed;
     39 }
     40 
     41 }  // namespace base
     42 
     43 #endif  // BASE_CONTAINERS_CXX20_ERASE_STRING_H_