Files
Nikolas Klauser b101f01382 [libc++] Optimize using std::copy with an ostreambuf_iterator (#181815)
```
Benchmark                                                  old             new    Difference    % Difference
----------------------------------------------  --------------  --------------  ------------  --------------
std::copy(CharT*,_CharT*,_ostreambuf_iterator)         8115.45          329.54      -7785.91         -95.94%
```
2026-02-26 12:01:07 +01:00

54 lines
1.5 KiB
C++

//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
// <algorithm>
// template<class CharT, class Traits>
// constexpr OutIter
// copy(CharT* first, CharT* last, ostreambuf_iterator<CharT, Traits> result);
// UNSUPPORTED: no-localization
#include <algorithm>
#include <cassert>
#include <iterator>
#include <sstream>
#include <type_traits>
#include "stream_types.h"
#include "test_macros.h"
template <class CCharT>
void test() {
using CharT = typename std::remove_cv<CCharT>::type;
{
std::basic_ostringstream<CharT> oss;
CCharT buff[] = {'B', 'a', 'n', 'a', 'n', 'e'};
std::copy(std::begin(buff), std::end(buff), std::ostreambuf_iterator<CharT>(oss));
assert(oss.str() == std::basic_string_view<CharT>(buff, 6));
}
{
failing_streambuf<CharT> fsb(4);
std::basic_ostream<CharT> oss(&fsb);
CCharT buff[] = {'B', 'a', 'n', 'a', 'n', 'e'};
auto res = std::copy(std::begin(buff), std::end(buff), std::ostreambuf_iterator<CharT>(oss));
assert(res.failed());
assert(fsb.str() == std::basic_string_view<CharT>(buff, 4));
}
}
int main(int, char**) {
test<char>();
test<const char>();
#ifndef TEST_HAS_NO_WIDE_CHARACTERS
test<wchar_t>();
test<const wchar_t>();
#endif
return 0;
}