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 <cstddef> 24 #include <cstdint> 25 26 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 27 # pragma GCC system_header 28 #endif 29 30 _LIBCPP_BEGIN_NAMESPACE_STD 31 32 #if _LIBCPP_STD_VER > 17 33 34 namespace __format_spec { 35 36 template <__formatter::__char_type _CharT> 37 class _LIBCPP_TEMPLATE_VIS __formatter_pointer : public __parser_pointer<_CharT> { 38 public: 39 _LIBCPP_HIDE_FROM_ABI auto format(const void* __ptr, auto& __ctx) -> decltype(__ctx.out()) { 40 _LIBCPP_ASSERT(this->__alignment != _Flags::_Alignment::__default, 41 "The call to parse should have updated the alignment"); 42 if (this->__width_needs_substitution()) 43 this->__substitute_width_arg_id(__ctx.arg(this->__width)); 44 45 // This code looks a lot like the code to format a hexadecimal integral, 46 // but that code isn't public. Making that code public requires some 47 // refactoring. 48 // TODO FMT Remove code duplication. 49 char __buffer[2 + 2 * sizeof(uintptr_t)]; 50 __buffer[0] = '0'; 51 __buffer[1] = 'x'; 52 char* __last = __to_buffer(__buffer + 2, _VSTD::end(__buffer), reinterpret_cast<uintptr_t>(__ptr), 16); 53 54 unsigned __size = __last - __buffer; 55 if (__size >= this->__width) 56 return _VSTD::copy(__buffer, __last, __ctx.out()); 57 58 return __formatter::__write(__ctx.out(), __buffer, __last, __size, this->__width, this->__fill, this->__alignment); 59 } 60 }; 61 62 } // namespace __format_spec 63 64 // [format.formatter.spec]/2.4 65 // For each charT, the pointer type specializations template<> 66 // - struct formatter<nullptr_t, charT>; 67 // - template<> struct formatter<void*, charT>; 68 // - template<> struct formatter<const void*, charT>; 69 template <__formatter::__char_type _CharT> 70 struct _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FORMAT formatter<nullptr_t, _CharT> 71 : public __format_spec::__formatter_pointer<_CharT> {}; 72 template <__formatter::__char_type _CharT> 73 struct _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FORMAT formatter<void*, _CharT> 74 : public __format_spec::__formatter_pointer<_CharT> {}; 75 template <__formatter::__char_type _CharT> 76 struct _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FORMAT formatter<const void*, _CharT> 77 : public __format_spec::__formatter_pointer<_CharT> {}; 78 79 #endif //_LIBCPP_STD_VER > 17 80 81 _LIBCPP_END_NAMESPACE_STD 82 83 #endif // _LIBCPP___FORMAT_FORMATTER_POINTER_H 84