1//===----------------------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef _LIBCPP___LOCALE_DIR_PAD_AND_OUTPUT_H
10#define _LIBCPP___LOCALE_DIR_PAD_AND_OUTPUT_H
11
12#include <__config>
13
14#if _LIBCPP_HAS_LOCALIZATION
15
16# include <__algorithm/copy.h>
17# include <__algorithm/fill_n.h>
18# include <ios>
19
20# if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
21# pragma GCC system_header
22# endif
23
24_LIBCPP_BEGIN_NAMESPACE_STD
25
26template <class _CharT, class _OutputIterator>
27_LIBCPP_HIDE_FROM_ABI _OutputIterator __pad_and_output(
28 _OutputIterator __s, const _CharT* __ob, const _CharT* __op, const _CharT* __oe, ios_base& __iob, _CharT __fl) {
29 streamsize __sz = __oe - __ob;
30 streamsize __ns = __iob.width();
31 if (__ns > __sz)
32 __ns -= __sz;
33 else
34 __ns = 0;
35 __s = std::copy(__ob, __op, __s);
36 __s = std::fill_n(__s, __ns, __fl);
37 __s = std::copy(__op, __oe, __s);
38 __iob.width(wide: 0);
39 return __s;
40}
41
42template <class _CharT, class _Traits>
43_LIBCPP_HIDE_FROM_ABI ostreambuf_iterator<_CharT, _Traits> __pad_and_output(
44 ostreambuf_iterator<_CharT, _Traits> __s,
45 const _CharT* __ob,
46 const _CharT* __op,
47 const _CharT* __oe,
48 ios_base& __iob,
49 _CharT __fl) {
50 if (__s.__sbuf_ == nullptr)
51 return __s;
52 streamsize __sz = __oe - __ob;
53 streamsize __ns = __iob.width();
54 if (__ns > __sz)
55 __ns -= __sz;
56 else
57 __ns = 0;
58 __s = std::copy(__ob, __op, __s);
59 if (__ns > 0) {
60 basic_string<_CharT, _Traits> __sp(__ns, __fl);
61 if (__s.__sbuf_->sputn(__sp.data(), __ns) != __ns) {
62 __s.__sbuf_ = nullptr;
63 return __s;
64 }
65 }
66 __s = std::copy(__op, __oe, __s);
67 __iob.width(wide: 0);
68 return __s;
69}
70
71_LIBCPP_END_NAMESPACE_STD
72
73#endif // _LIBCPP_HAS_LOCALIZATION
74
75#endif // _LIBCPP___LOCALE_DIR_PAD_AND_OUTPUT_H
76