/* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */ #ifndef MPT_FORMAT_SIMPLE_HPP #define MPT_FORMAT_SIMPLE_HPP #include "mpt/base/namespace.hpp" #include "mpt/base/pointer.hpp" #include "mpt/format/default_formatter.hpp" #include "mpt/format/simple_floatingpoint.hpp" #include "mpt/format/simple_integer.hpp" #include "mpt/format/simple_spec.hpp" #include "mpt/string/utility.hpp" #include #include namespace mpt { inline namespace MPT_INLINE_NS { template struct format : format_simple_base { template static inline Tstring val(const T & x) { return mpt::default_formatter::format(x); } template static inline Tstring fmt(const T & x, const format_simple_spec & f) { return mpt::format_simple(x, f); } template static inline Tstring dec(const T & x) { static_assert(std::numeric_limits::is_integer); return mpt::format_simple(x, format_simple_spec().BaseDec().FillOff()); } template static inline Tstring dec0(const T & x) { static_assert(std::numeric_limits::is_integer); return mpt::format_simple(x, format_simple_spec().BaseDec().FillNul().Width(width)); } template static inline Tstring dec(unsigned int g, char s, const T & x) { static_assert(std::numeric_limits::is_integer); return mpt::format_simple(x, format_simple_spec().BaseDec().FillOff().Group(g).GroupSep(s)); } template static inline Tstring dec0(unsigned int g, char s, const T & x) { static_assert(std::numeric_limits::is_integer); return mpt::format_simple(x, format_simple_spec().BaseDec().FillNul().Width(width).Group(g).GroupSep(s)); } template static inline Tstring hex(const T & x) { static_assert(std::numeric_limits::is_integer); return mpt::format_simple(x, format_simple_spec().BaseHex().CaseLow().FillOff()); } template static inline Tstring HEX(const T & x) { static_assert(std::numeric_limits::is_integer); return mpt::format_simple(x, format_simple_spec().BaseHex().CaseUpp().FillOff()); } template static inline Tstring hex0(const T & x) { static_assert(std::numeric_limits::is_integer); return mpt::format_simple(x, format_simple_spec().BaseHex().CaseLow().FillNul().Width(width)); } template static inline Tstring HEX0(const T & x) { static_assert(std::numeric_limits::is_integer); return mpt::format_simple(x, format_simple_spec().BaseHex().CaseUpp().FillNul().Width(width)); } template static inline Tstring hex(unsigned int g, char s, const T & x) { static_assert(std::numeric_limits::is_integer); return mpt::format_simple(x, format_simple_spec().BaseHex().CaseLow().FillOff().Group(g).GroupSep(s)); } template static inline Tstring HEX(unsigned int g, char s, const T & x) { static_assert(std::numeric_limits::is_integer); return mpt::format_simple(x, format_simple_spec().BaseHex().CaseUpp().FillOff().Group(g).GroupSep(s)); } template static inline Tstring hex0(unsigned int g, char s, const T & x) { static_assert(std::numeric_limits::is_integer); return mpt::format_simple(x, format_simple_spec().BaseHex().CaseLow().FillNul().Width(width).Group(g).GroupSep(s)); } template static inline Tstring HEX0(unsigned int g, char s, const T & x) { static_assert(std::numeric_limits::is_integer); return mpt::format_simple(x, format_simple_spec().BaseHex().CaseUpp().FillNul().Width(width).Group(g).GroupSep(s)); } template static inline Tstring flt(const T & x, int precision = -1) { static_assert(std::is_floating_point::value); return mpt::format_simple(x, format_simple_spec().NotaNrm().FillOff().Precision(precision)); } template static inline Tstring fix(const T & x, int precision = -1) { static_assert(std::is_floating_point::value); return mpt::format_simple(x, format_simple_spec().NotaFix().FillOff().Precision(precision)); } template static inline Tstring sci(const T & x, int precision = -1) { static_assert(std::is_floating_point::value); return mpt::format_simple(x, format_simple_spec().NotaSci().FillOff().Precision(precision)); } template static inline Tstring ptr(const T & x) { static_assert(std::is_pointer::value || std::is_same::value || std::is_same::value, ""); return hex0(mpt::pointer_cast(x)); } template static inline Tstring PTR(const T & x) { static_assert(std::is_pointer::value || std::is_same::value || std::is_same::value, ""); return HEX0(mpt::pointer_cast(x)); } static inline Tstring pad_left(std::size_t width_, const Tstring & str) { typedef mpt::string_traits traits; typename traits::size_type width = static_cast(width_); return traits::pad(str, width, 0); } static inline Tstring pad_right(std::size_t width_, const Tstring & str) { typedef mpt::string_traits traits; typename traits::size_type width = static_cast(width_); return traits::pad(str, 0, width); } static inline Tstring left(std::size_t width_, const Tstring & str) { typedef mpt::string_traits traits; typename traits::size_type width = static_cast(width_); return (traits::length(str) < width) ? traits::pad(str, 0, width - traits::length(str)) : str; } static inline Tstring right(std::size_t width_, const Tstring & str) { typedef mpt::string_traits traits; typename traits::size_type width = static_cast(width_); return (traits::length(str) < width) ? traits::pad(str, width - traits::length(str), 0) : str; } static inline Tstring center(std::size_t width_, const Tstring & str) { typedef mpt::string_traits traits; typename traits::size_type width = static_cast(width_); return (traits::length(str) < width) ? traits::pad(str, (width - traits::length(str)) / 2, (width - traits::length(str) + 1) / 2) : str; } }; // struct format } // namespace MPT_INLINE_NS } // namespace mpt #endif // MPT_FORMAT_SIMPLE_HPP