master
 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 <ios>
17
18#  if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
19#    pragma GCC system_header
20#  endif
21
22_LIBCPP_BEGIN_NAMESPACE_STD
23
24template <class _CharT, class _OutputIterator>
25_LIBCPP_HIDE_FROM_ABI _OutputIterator __pad_and_output(
26    _OutputIterator __s, const _CharT* __ob, const _CharT* __op, const _CharT* __oe, ios_base& __iob, _CharT __fl) {
27  streamsize __sz = __oe - __ob;
28  streamsize __ns = __iob.width();
29  if (__ns > __sz)
30    __ns -= __sz;
31  else
32    __ns = 0;
33  for (; __ob < __op; ++__ob, ++__s)
34    *__s = *__ob;
35  for (; __ns; --__ns, ++__s)
36    *__s = __fl;
37  for (; __ob < __oe; ++__ob, ++__s)
38    *__s = *__ob;
39  __iob.width(0);
40  return __s;
41}
42
43template <class _CharT, class _Traits>
44_LIBCPP_HIDE_FROM_ABI ostreambuf_iterator<_CharT, _Traits> __pad_and_output(
45    ostreambuf_iterator<_CharT, _Traits> __s,
46    const _CharT* __ob,
47    const _CharT* __op,
48    const _CharT* __oe,
49    ios_base& __iob,
50    _CharT __fl) {
51  if (__s.__sbuf_ == nullptr)
52    return __s;
53  streamsize __sz = __oe - __ob;
54  streamsize __ns = __iob.width();
55  if (__ns > __sz)
56    __ns -= __sz;
57  else
58    __ns = 0;
59  streamsize __np = __op - __ob;
60  if (__np > 0) {
61    if (__s.__sbuf_->sputn(__ob, __np) != __np) {
62      __s.__sbuf_ = nullptr;
63      return __s;
64    }
65  }
66  if (__ns > 0) {
67    basic_string<_CharT, _Traits> __sp(__ns, __fl);
68    if (__s.__sbuf_->sputn(__sp.data(), __ns) != __ns) {
69      __s.__sbuf_ = nullptr;
70      return __s;
71    }
72  }
73  __np = __oe - __op;
74  if (__np > 0) {
75    if (__s.__sbuf_->sputn(__op, __np) != __np) {
76      __s.__sbuf_ = nullptr;
77      return __s;
78    }
79  }
80  __iob.width(0);
81  return __s;
82}
83
84_LIBCPP_END_NAMESPACE_STD
85
86#endif // _LIBCPP_HAS_LOCALIZATION
87
88#endif // _LIBCPP___LOCALE_DIR_PAD_AND_OUTPUT_H