1 // -*- C++ -*- 2 //===----------------------------------------------------------------------===// 3 // 4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 5 // See https://llvm.org/LICENSE.txt for license information. 6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 7 // 8 //===----------------------------------------------------------------------===// 9 10 #ifndef _LIBCPP___FORMAT_FORMATTER_POINTER_H 11 #define _LIBCPP___FORMAT_FORMATTER_POINTER_H 12 13 #include <__algorithm/copy.h> 14 #include <__assert> 15 #include <__availability> 16 #include <__config> 17 #include <__format/format_error.h> 18 #include <__format/format_fwd.h> 19 #include <__format/formatter.h> 20 #include <__format/formatter_integral.h> 21 #include <__format/parser_std_format_spec.h> 22 #include <__iterator/access.h> 23 #include <cstdint> 24 25 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 26 # pragma GCC system_header 27 #endif 28 29 _LIBCPP_BEGIN_NAMESPACE_STD 30 31 #if _LIBCPP_STD_VER > 17 32 33 namespace __format_spec { 34 35 template <__formatter::__char_type _CharT> 36 class _LIBCPP_TEMPLATE_VIS __formatter_pointer : public __parser_pointer<_CharT> { 37 public: 38 _LIBCPP_HIDE_FROM_ABI auto format(const void* __ptr, auto& __ctx) -> decltype(__ctx.out()) { 39 _LIBCPP_ASSERT(this->__alignment != _Flags::_Alignment::__default, 40 "The call to parse should have updated the alignment"); 41 if (this->__width_needs_substitution()) 42 this->__substitute_width_arg_id(__ctx.arg(this->__width)); 43 44 // This code looks a lot like the code to format a hexadecimal integral, 45 // but that code isn't public. Making that code public requires some 46 // refactoring. 47 // TODO FMT Remove code duplication. 48 char __buffer[2 + 2 * sizeof(uintptr_t)]; 49 __buffer[0] = '0'; 50 __buffer[1] = 'x'; 51 char* __last = __to_buffer(__buffer + 2, _VSTD::end(__buffer), reinterpret_cast<uintptr_t>(__ptr), 16); 52 53 unsigned __size = __last - __buffer; 54 if (__size >= this->__width) 55 return _VSTD::copy(__buffer, __last, __ctx.out()); 56 57 return __formatter::__write(__ctx.out(), __buffer, __last, __size, this->__width, this->__fill, this->__alignment); 58 } 59 }; 60 61 } // namespace __format_spec 62 63 // [format.formatter.spec]/2.4 64 // For each charT, the pointer type specializations template<> 65 // - struct formatter<nullptr_t, charT>; 66 // - template<> struct formatter<void*, charT>; 67 // - template<> struct formatter<const void*, charT>; 68 template <__formatter::__char_type _CharT> 69 struct _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FORMAT formatter<nullptr_t, _CharT> 70 : public __format_spec::__formatter_pointer<_CharT> {}; 71 template <__formatter::__char_type _CharT> 72 struct _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FORMAT formatter<void*, _CharT> 73 : public __format_spec::__formatter_pointer<_CharT> {}; 74 template <__formatter::__char_type _CharT> 75 struct _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FORMAT formatter<const void*, _CharT> 76 : public __format_spec::__formatter_pointer<_CharT> {}; 77 78 #endif //_LIBCPP_STD_VER > 17 79 80 _LIBCPP_END_NAMESPACE_STD 81 82 #endif // _LIBCPP___FORMAT_FORMATTER_POINTER_H 83