\hypertarget{format_8h_source}{}\doxysection{format.\+h} \label{format_8h_source}\index{kblib/format.h@{kblib/format.h}} \mbox{\hyperlink{format_8h}{Go to the documentation of this file.}} \begin{DoxyCode}{0} \DoxyCodeLine{\Hypertarget{format_8h_source_l00001}00001 \textcolor{comment}{/* *****************************************************************************}} \DoxyCodeLine{\Hypertarget{format_8h_source_l00002}00002 \textcolor{comment}{ * kblib is a general utility library for C++14 and C++17, intended to provide}} \DoxyCodeLine{\Hypertarget{format_8h_source_l00003}00003 \textcolor{comment}{ * performant high-\/level abstractions and more expressive ways to do simple}} \DoxyCodeLine{\Hypertarget{format_8h_source_l00004}00004 \textcolor{comment}{ * things.}} \DoxyCodeLine{\Hypertarget{format_8h_source_l00005}00005 \textcolor{comment}{ *}} \DoxyCodeLine{\Hypertarget{format_8h_source_l00006}00006 \textcolor{comment}{ * Copyright (c) 2021 killerbee}} \DoxyCodeLine{\Hypertarget{format_8h_source_l00007}00007 \textcolor{comment}{ *}} \DoxyCodeLine{\Hypertarget{format_8h_source_l00008}00008 \textcolor{comment}{ * This program is free software: you can redistribute it and/or modify}} \DoxyCodeLine{\Hypertarget{format_8h_source_l00009}00009 \textcolor{comment}{ * it under the terms of the GNU General Public License as published by}} \DoxyCodeLine{\Hypertarget{format_8h_source_l00010}00010 \textcolor{comment}{ * the Free Software Foundation, either version 3 of the License, or}} \DoxyCodeLine{\Hypertarget{format_8h_source_l00011}00011 \textcolor{comment}{ * (at your option) any later version.}} \DoxyCodeLine{\Hypertarget{format_8h_source_l00012}00012 \textcolor{comment}{ *}} \DoxyCodeLine{\Hypertarget{format_8h_source_l00013}00013 \textcolor{comment}{ * This program is distributed in the hope that it will be useful,}} \DoxyCodeLine{\Hypertarget{format_8h_source_l00014}00014 \textcolor{comment}{ * but WITHOUT ANY WARRANTY; without even the implied warranty of}} \DoxyCodeLine{\Hypertarget{format_8h_source_l00015}00015 \textcolor{comment}{ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the}} \DoxyCodeLine{\Hypertarget{format_8h_source_l00016}00016 \textcolor{comment}{ * GNU General Public License for more details.}} \DoxyCodeLine{\Hypertarget{format_8h_source_l00017}00017 \textcolor{comment}{ *}} \DoxyCodeLine{\Hypertarget{format_8h_source_l00018}00018 \textcolor{comment}{ * You should have received a copy of the GNU General Public License}} \DoxyCodeLine{\Hypertarget{format_8h_source_l00019}00019 \textcolor{comment}{ * along with this program. If not, see .}} \DoxyCodeLine{\Hypertarget{format_8h_source_l00020}00020 \textcolor{comment}{ * ****************************************************************************/}} \DoxyCodeLine{\Hypertarget{format_8h_source_l00021}00021 } \DoxyCodeLine{\Hypertarget{format_8h_source_l00032}00032 \textcolor{preprocessor}{\#ifndef KBLIB\_FORMAT\_H}} \DoxyCodeLine{\Hypertarget{format_8h_source_l00033}00033 \textcolor{preprocessor}{\#define KBLIB\_FORMAT\_H}} \DoxyCodeLine{\Hypertarget{format_8h_source_l00034}00034 } \DoxyCodeLine{\Hypertarget{format_8h_source_l00035}00035 \textcolor{preprocessor}{\#include "{}\mbox{\hyperlink{fakestd_8h}{fakestd.h}}"{}}} \DoxyCodeLine{\Hypertarget{format_8h_source_l00036}00036 } \DoxyCodeLine{\Hypertarget{format_8h_source_l00037}00037 \textcolor{preprocessor}{\#include }} \DoxyCodeLine{\Hypertarget{format_8h_source_l00038}00038 \textcolor{preprocessor}{\#include }} \DoxyCodeLine{\Hypertarget{format_8h_source_l00039}00039 } \DoxyCodeLine{\Hypertarget{format_8h_source_l00040}00040 \textcolor{keyword}{namespace }\mbox{\hyperlink{tdecl_8h_a4b5c574c663a392148218cd4410fd7a1}{KBLIB\_NS}} \{} \DoxyCodeLine{\Hypertarget{format_8h_source_l00041}00041 } \DoxyCodeLine{\Hypertarget{format_8h_source_l00049}00049 \textcolor{keyword}{template} <\textcolor{keyword}{typename} Number>} \DoxyCodeLine{\Hypertarget{format_8h_source_l00050}\mbox{\hyperlink{namespacekblib_ab5e75c9314c76b12efd507259206a546}{00050}} \textcolor{keyword}{constexpr} \textcolor{keyword}{auto} \mbox{\hyperlink{namespacekblib_a490acb8189530823cb6150afff35fc1c}{count\_digits}}(Number val)} \DoxyCodeLine{\Hypertarget{format_8h_source_l00051}00051 -\/> \mbox{\hyperlink{namespacekblib_a366272ad8baad19be4ccf8128afb04a3}{enable\_if\_t::value}}, \textcolor{keywordtype}{int}> \{} \DoxyCodeLine{\Hypertarget{format_8h_source_l00052}00052 \textcolor{keywordflow}{if} (val == 0) \{} \DoxyCodeLine{\Hypertarget{format_8h_source_l00053}00053 \textcolor{keywordflow}{return} 1;} \DoxyCodeLine{\Hypertarget{format_8h_source_l00054}00054 \} \textcolor{keywordflow}{else} \{} \DoxyCodeLine{\Hypertarget{format_8h_source_l00055}00055 \textcolor{keywordflow}{return} std::numeric\_limits::digits10;} \DoxyCodeLine{\Hypertarget{format_8h_source_l00056}00056 \}} \DoxyCodeLine{\Hypertarget{format_8h_source_l00057}00057 \}} \DoxyCodeLine{\Hypertarget{format_8h_source_l00058}00058 } \DoxyCodeLine{\Hypertarget{format_8h_source_l00066}00066 \textcolor{keyword}{template} <\textcolor{keyword}{typename} Number>} \DoxyCodeLine{\Hypertarget{format_8h_source_l00067}00067 \textcolor{keyword}{constexpr} \textcolor{keyword}{auto} \mbox{\hyperlink{namespacekblib_a490acb8189530823cb6150afff35fc1c}{count\_digits}}(Number val)} \DoxyCodeLine{\Hypertarget{format_8h_source_l00068}00068 -\/> enable\_if\_t::value} \DoxyCodeLine{\Hypertarget{format_8h_source_l00069}00069 and std::is\_signed::value,} \DoxyCodeLine{\Hypertarget{format_8h_source_l00070}00070 \textcolor{keywordtype}{int}> \{} \DoxyCodeLine{\Hypertarget{format_8h_source_l00071}00071 \textcolor{keywordflow}{if} (val == 0 or val == 1) \{} \DoxyCodeLine{\Hypertarget{format_8h_source_l00072}00072 \textcolor{keywordflow}{return} 1;} \DoxyCodeLine{\Hypertarget{format_8h_source_l00073}00073 \} \textcolor{keywordflow}{else} \{} \DoxyCodeLine{\Hypertarget{format_8h_source_l00074}00074 \textcolor{keywordflow}{return} \textcolor{keyword}{static\_cast<}\textcolor{keywordtype}{int}\textcolor{keyword}{>}(std::ceil(} \DoxyCodeLine{\Hypertarget{format_8h_source_l00075}00075 std::nextafter(std::log10(std::fabs(val)), INFINITY)))} \DoxyCodeLine{\Hypertarget{format_8h_source_l00076}00076 + (val < 0);} \DoxyCodeLine{\Hypertarget{format_8h_source_l00077}00077 \}} \DoxyCodeLine{\Hypertarget{format_8h_source_l00078}00078 \}} \DoxyCodeLine{\Hypertarget{format_8h_source_l00079}00079 } \DoxyCodeLine{\Hypertarget{format_8h_source_l00086}00086 \textcolor{keyword}{template} <\textcolor{keyword}{typename} Number>} \DoxyCodeLine{\Hypertarget{format_8h_source_l00087}00087 \textcolor{keyword}{constexpr} \textcolor{keyword}{auto} \mbox{\hyperlink{namespacekblib_a490acb8189530823cb6150afff35fc1c}{count\_digits}}(Number val)} \DoxyCodeLine{\Hypertarget{format_8h_source_l00088}00088 -\/> enable\_if\_t::value, \textcolor{keywordtype}{int}> \{} \DoxyCodeLine{\Hypertarget{format_8h_source_l00089}00089 \textcolor{keywordflow}{if} (val == 0) \{} \DoxyCodeLine{\Hypertarget{format_8h_source_l00090}00090 \textcolor{keywordflow}{return} 1;} \DoxyCodeLine{\Hypertarget{format_8h_source_l00091}00091 \} \textcolor{keywordflow}{else} \textcolor{keywordflow}{if} (val == \textcolor{keyword}{static\_cast<}Number\textcolor{keyword}{>}(-\/1)) \{} \DoxyCodeLine{\Hypertarget{format_8h_source_l00092}00092 \textcolor{keywordflow}{return} \textcolor{keyword}{static\_cast<}\textcolor{keywordtype}{int}\textcolor{keyword}{>}(std::ceil(std::log10(val)));} \DoxyCodeLine{\Hypertarget{format_8h_source_l00093}00093 \} \textcolor{keywordflow}{else} \{} \DoxyCodeLine{\Hypertarget{format_8h_source_l00094}00094 \textcolor{keywordflow}{return} \textcolor{keyword}{static\_cast<}\textcolor{keywordtype}{int}\textcolor{keyword}{>}(std::ceil(std::log10(val + 1)));} \DoxyCodeLine{\Hypertarget{format_8h_source_l00095}00095 \}} \DoxyCodeLine{\Hypertarget{format_8h_source_l00096}00096 \}} \DoxyCodeLine{\Hypertarget{format_8h_source_l00097}00097 } \DoxyCodeLine{\Hypertarget{format_8h_source_l00106}00106 \textcolor{keyword}{template} <\textcolor{keyword}{typename} Number>} \DoxyCodeLine{\Hypertarget{format_8h_source_l00107}\mbox{\hyperlink{namespacekblib_a490acb8189530823cb6150afff35fc1c}{00107}} \textcolor{keyword}{constexpr} \textcolor{keyword}{auto} \mbox{\hyperlink{namespacekblib_a490acb8189530823cb6150afff35fc1c}{count\_digits}}(Number val, \textcolor{keywordtype}{int} base) -\/> \textcolor{keywordtype}{int} \{} \DoxyCodeLine{\Hypertarget{format_8h_source_l00108}00108 \textcolor{keywordflow}{if} (val == 0) \{} \DoxyCodeLine{\Hypertarget{format_8h_source_l00109}00109 \textcolor{keywordflow}{return} 1;} \DoxyCodeLine{\Hypertarget{format_8h_source_l00110}00110 \} \textcolor{keywordflow}{else} \textcolor{keywordflow}{if} (std::is\_floating\_point::value) \{} \DoxyCodeLine{\Hypertarget{format_8h_source_l00111}00111 \textcolor{keywordflow}{return} \textcolor{keyword}{static\_cast<}\textcolor{keywordtype}{int}\textcolor{keyword}{>}(} \DoxyCodeLine{\Hypertarget{format_8h_source_l00112}00112 std::ceil(std::numeric\_limits::digits * std::logb(base)));} \DoxyCodeLine{\Hypertarget{format_8h_source_l00113}00113 \} \textcolor{keywordflow}{else} \{} \DoxyCodeLine{\Hypertarget{format_8h_source_l00114}00114 \textcolor{keywordflow}{return} \textcolor{keyword}{static\_cast<}\textcolor{keywordtype}{int}\textcolor{keyword}{>}(} \DoxyCodeLine{\Hypertarget{format_8h_source_l00115}00115 std::ceil(std::log(std::abs(val) + 1) / std::log(base)))} \DoxyCodeLine{\Hypertarget{format_8h_source_l00116}00116 + (val < 0);} \DoxyCodeLine{\Hypertarget{format_8h_source_l00117}00117 \}} \DoxyCodeLine{\Hypertarget{format_8h_source_l00118}00118 \}} \DoxyCodeLine{\Hypertarget{format_8h_source_l00119}00119 } \DoxyCodeLine{\Hypertarget{format_8h_source_l00129}00129 \textcolor{keyword}{template} <\textcolor{keyword}{typename} ForwardIt>} \DoxyCodeLine{\Hypertarget{format_8h_source_l00130}\mbox{\hyperlink{namespacekblib_a5a05c4dad317da381751c694a4b69c52}{00130}} \textcolor{keyword}{auto} \mbox{\hyperlink{namespacekblib_a79bdc41eaf5235864ea333ebe30e7c36}{max\_count\_digits}}(ForwardIt first, ForwardIt last) -\/> \textcolor{keywordtype}{int} \{} \DoxyCodeLine{\Hypertarget{format_8h_source_l00131}00131 \textcolor{keywordflow}{if} (first == last) \{} \DoxyCodeLine{\Hypertarget{format_8h_source_l00132}00132 \textcolor{keywordflow}{return} 0;} \DoxyCodeLine{\Hypertarget{format_8h_source_l00133}00133 \}} \DoxyCodeLine{\Hypertarget{format_8h_source_l00134}00134 \textcolor{keywordflow}{return} \mbox{\hyperlink{namespacekblib_a490acb8189530823cb6150afff35fc1c}{count\_digits}}(*\mbox{\hyperlink{namespacekblib_a85f026b89c516de43393ab0259c1c834}{std::max\_element}}(first, last));} \DoxyCodeLine{\Hypertarget{format_8h_source_l00135}00135 \}} \DoxyCodeLine{\Hypertarget{format_8h_source_l00136}00136 } \DoxyCodeLine{\Hypertarget{format_8h_source_l00147}00147 \textcolor{keyword}{template} <\textcolor{keyword}{typename} ForwardIt>} \DoxyCodeLine{\Hypertarget{format_8h_source_l00148}\mbox{\hyperlink{namespacekblib_a79bdc41eaf5235864ea333ebe30e7c36}{00148}} \textcolor{keyword}{auto} \mbox{\hyperlink{namespacekblib_a79bdc41eaf5235864ea333ebe30e7c36}{max\_count\_digits}}(ForwardIt first, ForwardIt last, \textcolor{keywordtype}{int} base) -\/> \textcolor{keywordtype}{int} \{} \DoxyCodeLine{\Hypertarget{format_8h_source_l00149}00149 \textcolor{keywordflow}{if} (first == last) \{} \DoxyCodeLine{\Hypertarget{format_8h_source_l00150}00150 \textcolor{keywordflow}{return} 0;} \DoxyCodeLine{\Hypertarget{format_8h_source_l00151}00151 \}} \DoxyCodeLine{\Hypertarget{format_8h_source_l00152}00152 \textcolor{keywordflow}{return} \mbox{\hyperlink{namespacekblib_a490acb8189530823cb6150afff35fc1c}{count\_digits}}(*\mbox{\hyperlink{namespacekblib_a85f026b89c516de43393ab0259c1c834}{std::max\_element}}(first, last), base);} \DoxyCodeLine{\Hypertarget{format_8h_source_l00153}00153 \}} \DoxyCodeLine{\Hypertarget{format_8h_source_l00154}00154 } \DoxyCodeLine{\Hypertarget{format_8h_source_l00155}00155 \} \textcolor{comment}{// namespace KBLIB\_NS}} \DoxyCodeLine{\Hypertarget{format_8h_source_l00156}00156 } \DoxyCodeLine{\Hypertarget{format_8h_source_l00157}00157 \textcolor{preprocessor}{\#endif }\textcolor{comment}{// KBLIB\_FORMAT\_H}} \end{DoxyCode}