88 lines · c
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-exception6//7//===----------------------------------------------------------------------===//8 9#ifndef _LIBCPP___LOCALE_DIR_PAD_AND_OUTPUT_H10#define _LIBCPP___LOCALE_DIR_PAD_AND_OUTPUT_H11 12#include <__config>13 14#if _LIBCPP_HAS_LOCALIZATION15 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_header22# endif23 24_LIBCPP_BEGIN_NAMESPACE_STD25 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 else34 __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(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 else57 __ns = 0;58 streamsize __np = __op - __ob;59 if (__np > 0) {60 if (__s.__sbuf_->sputn(__ob, __np) != __np) {61 __s.__sbuf_ = nullptr;62 return __s;63 }64 }65 if (__ns > 0) {66 basic_string<_CharT, _Traits> __sp(__ns, __fl);67 if (__s.__sbuf_->sputn(__sp.data(), __ns) != __ns) {68 __s.__sbuf_ = nullptr;69 return __s;70 }71 }72 __np = __oe - __op;73 if (__np > 0) {74 if (__s.__sbuf_->sputn(__op, __np) != __np) {75 __s.__sbuf_ = nullptr;76 return __s;77 }78 }79 __iob.width(0);80 return __s;81}82 83_LIBCPP_END_NAMESPACE_STD84 85#endif // _LIBCPP_HAS_LOCALIZATION86 87#endif // _LIBCPP___LOCALE_DIR_PAD_AND_OUTPUT_H88