| 1 |
/* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */ |
| 2 |
|
| 3 |
#ifndef MPT_FORMAT_SIMPLE_SPEC_HPP |
| 4 |
#define MPT_FORMAT_SIMPLE_SPEC_HPP |
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
#include "mpt/base/macros.hpp" |
| 9 |
#include "mpt/base/namespace.hpp" |
| 10 |
#include "mpt/string_transcode/transcode.hpp" |
| 11 |
|
| 12 |
#include <utility> |
| 13 |
|
| 14 |
#include <cstddef> |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
namespace mpt { |
| 19 |
inline namespace MPT_INLINE_NS { |
| 20 |
|
| 21 |
|
| 22 |
struct format_simple_base { |
| 23 |
|
| 24 |
enum FormatFlagsEnum { |
| 25 |
BaseDec = 0x0001, // base 10 (integers only) // int+float |
| 26 |
BaseHex = 0x0002, // base 16 (integers only) // int+float |
| 27 |
CaseLow = 0x0010, // lower case hex digits // int+float |
| 28 |
CaseUpp = 0x0020, // upper case hex digits // int+float |
| 29 |
FillOff = 0x0100, // do not fill up width // int+float |
| 30 |
FillNul = 0x0400, // fill up width with zeros // int+float |
| 31 |
NotaNrm = 0x1000, // float: normal/default notation // float |
| 32 |
NotaFix = 0x2000, // float: fixed point notation // float |
| 33 |
NotaSci = 0x4000, // float: scientific notation // float |
| 34 |
}; |
| 35 |
|
| 36 |
}; // struct format_simple_base |
| 37 |
|
| 38 |
using format_simple_flags = unsigned int; |
| 39 |
|
| 40 |
static_assert(sizeof(format_simple_flags) >= sizeof(format_simple_base::FormatFlagsEnum)); |
| 41 |
|
| 42 |
|
| 43 |
template <typename Tstring> |
| 44 |
class format_simple_spec { |
| 45 |
private: |
| 46 |
format_simple_flags flags; |
| 47 |
std::size_t width; // int+float |
| 48 |
int precision; // float |
| 49 |
unsigned int group; // int |
| 50 |
Tstring group_sep; // int |
| 51 |
public: |
| 52 |
MPT_CONSTEXPRINLINE format_simple_spec() noexcept |
| 53 |
: flags(0) |
| 54 |
, width(0) |
| 55 |
, precision(-1) |
| 56 |
, group(0) |
| 57 |
, group_sep() { } |
| 58 |
MPT_CONSTEXPRINLINE format_simple_flags GetFlags() const noexcept { |
| 59 |
return flags; |
| 60 |
} |
| 61 |
MPT_CONSTEXPRINLINE std::size_t GetWidth() const noexcept { |
| 62 |
return width; |
| 63 |
} |
| 64 |
MPT_CONSTEXPRINLINE int GetPrecision() const noexcept { |
| 65 |
return precision; |
| 66 |
} |
| 67 |
MPT_CONSTEXPRINLINE unsigned int GetGroup() const noexcept { |
| 68 |
return group; |
| 69 |
} |
| 70 |
MPT_CONSTEXPRINLINE Tstring GetGroupSep() const { |
| 71 |
return group_sep; |
| 72 |
} |
| 73 |
MPT_CONSTEXPRINLINE format_simple_spec & SetFlags(format_simple_flags f) noexcept { |
| 74 |
flags = f; |
| 75 |
return *this; |
| 76 |
} |
| 77 |
MPT_CONSTEXPRINLINE format_simple_spec & SetWidth(std::size_t w) noexcept { |
| 78 |
width = w; |
| 79 |
return *this; |
| 80 |
} |
| 81 |
MPT_CONSTEXPRINLINE format_simple_spec & SetPrecision(int p) noexcept { |
| 82 |
precision = p; |
| 83 |
return *this; |
| 84 |
} |
| 85 |
MPT_CONSTEXPRINLINE format_simple_spec & SetGroup(unsigned int g) noexcept { |
| 86 |
group = g; |
| 87 |
return *this; |
| 88 |
} |
| 89 |
MPT_CONSTEXPRINLINE format_simple_spec & SetGroupSep(Tstring s) { |
| 90 |
group_sep = std::move(s); |
| 91 |
return *this; |
| 92 |
} |
| 93 |
|
| 94 |
public: |
| 95 |
MPT_CONSTEXPRINLINE format_simple_spec & BaseDec() noexcept { |
| 96 |
flags &= ~(format_simple_base::BaseDec | format_simple_base::BaseHex); |
| 97 |
flags |= format_simple_base::BaseDec; |
| 98 |
return *this; |
| 99 |
} |
| 100 |
MPT_CONSTEXPRINLINE format_simple_spec & BaseHex() noexcept { |
| 101 |
flags &= ~(format_simple_base::BaseDec | format_simple_base::BaseHex); |
| 102 |
flags |= format_simple_base::BaseHex; |
| 103 |
return *this; |
| 104 |
} |
| 105 |
MPT_CONSTEXPRINLINE format_simple_spec & CaseLow() noexcept { |
| 106 |
flags &= ~(format_simple_base::CaseLow | format_simple_base::CaseUpp); |
| 107 |
flags |= format_simple_base::CaseLow; |
| 108 |
return *this; |
| 109 |
} |
| 110 |
MPT_CONSTEXPRINLINE format_simple_spec & CaseUpp() noexcept { |
| 111 |
flags &= ~(format_simple_base::CaseLow | format_simple_base::CaseUpp); |
| 112 |
flags |= format_simple_base::CaseUpp; |
| 113 |
return *this; |
| 114 |
} |
| 115 |
MPT_CONSTEXPRINLINE format_simple_spec & FillOff() noexcept { |
| 116 |
flags &= ~(format_simple_base::FillOff | format_simple_base::FillNul); |
| 117 |
flags |= format_simple_base::FillOff; |
| 118 |
return *this; |
| 119 |
} |
| 120 |
MPT_CONSTEXPRINLINE format_simple_spec & FillNul() noexcept { |
| 121 |
flags &= ~(format_simple_base::FillOff | format_simple_base::FillNul); |
| 122 |
flags |= format_simple_base::FillNul; |
| 123 |
return *this; |
| 124 |
} |
| 125 |
MPT_CONSTEXPRINLINE format_simple_spec & NotaNrm() noexcept { |
| 126 |
flags &= ~(format_simple_base::NotaNrm | format_simple_base::NotaFix | format_simple_base::NotaSci); |
| 127 |
flags |= format_simple_base::NotaNrm; |
| 128 |
return *this; |
| 129 |
} |
| 130 |
MPT_CONSTEXPRINLINE format_simple_spec & NotaFix() noexcept { |
| 131 |
flags &= ~(format_simple_base::NotaNrm | format_simple_base::NotaFix | format_simple_base::NotaSci); |
| 132 |
flags |= format_simple_base::NotaFix; |
| 133 |
return *this; |
| 134 |
} |
| 135 |
MPT_CONSTEXPRINLINE format_simple_spec & NotaSci() noexcept { |
| 136 |
flags &= ~(format_simple_base::NotaNrm | format_simple_base::NotaFix | format_simple_base::NotaSci); |
| 137 |
flags |= format_simple_base::NotaSci; |
| 138 |
return *this; |
| 139 |
} |
| 140 |
MPT_CONSTEXPRINLINE format_simple_spec & Width(std::size_t w) noexcept { |
| 141 |
width = w; |
| 142 |
return *this; |
| 143 |
} |
| 144 |
MPT_CONSTEXPRINLINE format_simple_spec & Prec(int p) noexcept { |
| 145 |
precision = p; |
| 146 |
return *this; |
| 147 |
} |
| 148 |
MPT_CONSTEXPRINLINE format_simple_spec & Group(unsigned int g) noexcept { |
| 149 |
group = g; |
| 150 |
return *this; |
| 151 |
} |
| 152 |
MPT_CONSTEXPRINLINE format_simple_spec & GroupSep(Tstring s) { |
| 153 |
group_sep = std::move(s); |
| 154 |
return *this; |
| 155 |
} |
| 156 |
|
| 157 |
public: |
| 158 |
MPT_CONSTEXPRINLINE format_simple_spec & Dec() noexcept { |
| 159 |
return BaseDec(); |
| 160 |
} |
| 161 |
MPT_CONSTEXPRINLINE format_simple_spec & Hex() noexcept { |
| 162 |
return BaseHex(); |
| 163 |
} |
| 164 |
MPT_CONSTEXPRINLINE format_simple_spec & Low() noexcept { |
| 165 |
return CaseLow(); |
| 166 |
} |
| 167 |
MPT_CONSTEXPRINLINE format_simple_spec & Upp() noexcept { |
| 168 |
return CaseUpp(); |
| 169 |
} |
| 170 |
MPT_CONSTEXPRINLINE format_simple_spec & Off() noexcept { |
| 171 |
return FillOff(); |
| 172 |
} |
| 173 |
MPT_CONSTEXPRINLINE format_simple_spec & Nul() noexcept { |
| 174 |
return FillNul(); |
| 175 |
} |
| 176 |
MPT_CONSTEXPRINLINE format_simple_spec & Nrm() noexcept { |
| 177 |
return NotaNrm(); |
| 178 |
} |
| 179 |
MPT_CONSTEXPRINLINE format_simple_spec & Fix() noexcept { |
| 180 |
return NotaFix(); |
| 181 |
} |
| 182 |
MPT_CONSTEXPRINLINE format_simple_spec & Sci() noexcept { |
| 183 |
return NotaSci(); |
| 184 |
} |
| 185 |
|
| 186 |
public: |
| 187 |
MPT_CONSTEXPRINLINE format_simple_spec & Decimal() noexcept { |
| 188 |
return BaseDec(); |
| 189 |
} |
| 190 |
MPT_CONSTEXPRINLINE format_simple_spec & Hexadecimal() noexcept { |
| 191 |
return BaseHex(); |
| 192 |
} |
| 193 |
MPT_CONSTEXPRINLINE format_simple_spec & Lower() noexcept { |
| 194 |
return CaseLow(); |
| 195 |
} |
| 196 |
MPT_CONSTEXPRINLINE format_simple_spec & Upper() noexcept { |
| 197 |
return CaseUpp(); |
| 198 |
} |
| 199 |
MPT_CONSTEXPRINLINE format_simple_spec & FillNone() noexcept { |
| 200 |
return FillOff(); |
| 201 |
} |
| 202 |
MPT_CONSTEXPRINLINE format_simple_spec & FillZero() noexcept { |
| 203 |
return FillNul(); |
| 204 |
} |
| 205 |
MPT_CONSTEXPRINLINE format_simple_spec & FloatNormal() noexcept { |
| 206 |
return NotaNrm(); |
| 207 |
} |
| 208 |
MPT_CONSTEXPRINLINE format_simple_spec & FloatFixed() noexcept { |
| 209 |
return NotaFix(); |
| 210 |
} |
| 211 |
MPT_CONSTEXPRINLINE format_simple_spec & FloatScientific() noexcept { |
| 212 |
return NotaSci(); |
| 213 |
} |
| 214 |
MPT_CONSTEXPRINLINE format_simple_spec & Precision(int p) noexcept { |
| 215 |
return Prec(p); |
| 216 |
} |
| 217 |
}; |
| 218 |
|
| 219 |
|
| 220 |
template <typename Tdststring, typename Tsrcstring> |
| 221 |
inline format_simple_spec<Tdststring> transcode_format_simple_spec(const format_simple_spec<Tsrcstring> & src) { |
| 222 |
format_simple_spec<Tdststring> dst; |
| 223 |
dst.SetFlags(src.GetFlags()); |
| 224 |
dst.SetWidth(src.GetWidth()); |
| 225 |
dst.SetPrecision(src.GetPrecision()); |
| 226 |
dst.SetGroup(src.GetGroup()); |
| 227 |
dst.SetGroupSep(mpt::transcode<Tdststring>(src.GetGroupSep())); |
| 228 |
return dst; |
| 229 |
} |
| 230 |
|
| 231 |
template <typename Tdststring, typename Tsrcstring, typename Tencoding, std::enable_if_t<std::is_same<Tdststring, std::string>::value, bool> = true> |
| 232 |
inline format_simple_spec<Tdststring> transcode_format_simple_spec(Tencoding to, const format_simple_spec<Tsrcstring> & src) { |
| 233 |
format_simple_spec<Tdststring> dst; |
| 234 |
dst.SetFlags(src.GetFlags()); |
| 235 |
dst.SetWidth(src.GetWidth()); |
| 236 |
dst.SetPrecision(src.GetPrecision()); |
| 237 |
dst.SetGroup(src.GetGroup()); |
| 238 |
dst.SetGroupSep(mpt::transcode<Tdststring>(to, src.GetGroupSep())); |
| 239 |
return dst; |
| 240 |
} |
| 241 |
|
| 242 |
template <typename Tdststring, typename Tsrcstring, typename Tencoding, std::enable_if_t<std::is_same<typename mpt::make_string_type<Tsrcstring>::type, std::string>::value, bool> = true> |
| 243 |
inline format_simple_spec<Tdststring> transcode_format_simple_spec(Tencoding from, const format_simple_spec<Tsrcstring> & src) { |
| 244 |
format_simple_spec<Tdststring> dst; |
| 245 |
dst.SetFlags(src.GetFlags()); |
| 246 |
dst.SetWidth(src.GetWidth()); |
| 247 |
dst.SetPrecision(src.GetPrecision()); |
| 248 |
dst.SetGroup(src.GetGroup()); |
| 249 |
dst.SetGroupSep(mpt::transcode<Tdststring>(from, src.GetGroupSep())); |
| 250 |
return dst; |
| 251 |
} |
| 252 |
|
| 253 |
template <typename Tdststring, typename Tsrcstring, typename Tto, typename Tfrom> |
| 254 |
inline format_simple_spec<Tdststring> transcode_format_simple_spec(Tto to, Tfrom from, const format_simple_spec<Tsrcstring> & src) { |
| 255 |
format_simple_spec<Tdststring> dst; |
| 256 |
dst.SetFlags(src.GetFlags()); |
| 257 |
dst.SetWidth(src.GetWidth()); |
| 258 |
dst.SetPrecision(src.GetPrecision()); |
| 259 |
dst.SetGroup(src.GetGroup()); |
| 260 |
dst.SetGroupSep(mpt::transcode<Tdststring>(to, from, src.GetGroupSep())); |
| 261 |
return dst; |
| 262 |
} |
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
} // namespace MPT_INLINE_NS |
| 268 |
} // namespace mpt |
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
#endif // MPT_FORMAT_SIMPLE_SPEC_HPP |