Debian Codegraph @

ncrystal_core/include/NCrystal/NCFmt.hh

raw symbols SCIP
1 #cxx . . $ `<file>/ncrystal_core/include/NCrystal/NCFmt.hh`/ifndef NCrystal_Fmt_hh
2 #define NCrystal_Fmt_hhcxx . . $ `ncrystal_core/include/NCrystal/NCFmt.hh:2:9`!
3
4 ////////////////////////////////////////////////////////////////////////////////
5 // //
6 // This file is part of NCrystal (see https://mctools.github.io/ncrystal/) //
7 // //
8 // Copyright 2015-2022 NCrystal developers //
9 // //
10 // Licensed under the Apache License, Version 2.0 (the "License"); //
11 // you may not use this file except in compliance with the License. //
12 // You may obtain a copy of the License at //
13 // //
14 // http://www.apache.org/licenses/LICENSE-2.0 //
15 // //
16 // Unless required by applicable law or agreed to in writing, software //
17 // distributed under the License is distributed on an "AS IS" BASIS, //
18 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. //
19 // See the License for the specific language governing permissions and //
20 // limitations under the License. //
21 // //
22 ////////////////////////////////////////////////////////////////////////////////
23
24 // Utilities for formatting floating point values as strings. In particular
25 // efficient (malloc-free) std::ostream-compatbile formatting of floating point
26 // numbers. To support this, a short-string class is also provided.
27
28 #ifndef NCrystal_Defs_hhcxx . . $ `ncrystal_core/include/NCrystal/NCDefs.hh:2:9`!
29 # include "NCrystal/NCDefs.hh"cxx . . $ `<file>/ncrystal_core/include/NCrystal/NCDefs.hh`/
30 #endif
31
32 namespace NCrystalcxx . . $ NCrystal/ {
33
34 class StrViewcxx . . $ NCrystal/StrView#;//Internal class (cf. NCStrView.hh)
35
36 template <unsigned NMAX>
37 class NCRYSTAL_APIcxx . . $ `ncrystal_core/include/NCrystal/ncapi.h:51:13`! ShortStrcxx . . $ NCrystal/ShortStr# {
38 // Simple short-string class with a given maximum string length (including
39 // null char), and no memory allocations.
40 public:
41 using size_typecxx . . $ NCrystal/ShortStr#size_type# = unsigned;
42 static_assert(NMAX>=1,"");
43 static constexpr size_typecxx . . $ NCrystal/ShortStr#size_type# max_strlencxx . . $ NCrystal/ShortStr#max_strlen. = static_cast<size_typecxx . . $ NCrystal/ShortStr#size_type#>(NMAX - 1);
44 static constexpr size_typecxx . . $ NCrystal/ShortStr#size_type# bufsizecxx . . $ NCrystal/ShortStr#bufsize. = static_cast<size_typecxx . . $ NCrystal/ShortStr#size_type#>(NMAX);
45
46 ShortStrcxx . . $ NCrystal/ShortStr#`ShortStr<NMAX>`(85c52e162fed56f9).( const char * cstr );
47 ShortStrcxx . . $ NCrystal/ShortStr#`ShortStr<NMAX>`(b51963283f2467a4).( const char * cstr, unsigned str_len );
48 ShortStrcxx . . $ NCrystal/ShortStr#`ShortStr<NMAX>`(c41de784c457f93d).( StrViewcxx . . $ NCrystal/StrView# );
49
50 //Empty string:
51 ShortStrcxx . . $ NCrystal/ShortStr#`ShortStr<NMAX>`(2478d1cda8d6d56b).( NullOptTypecxx . . $ NCrystal/NullOptType# ) : m_sizecxx . . $ NCrystal/ShortStr#m_size.(0) { m_datacxx . . $ NCrystal/ShortStr#m_data.[0] = '\0'; }
52 ShortStrcxx . . $ NCrystal/ShortStr#`ShortStr<NMAX>`(49f6e7a06ebc5aa8).() : ShortStr( NullOptcxx . . $ NCrystal/NullOpt. ) {}
53
54 //Special (and rather unsafe!!) constructor which allows direct access to
55 //internal data members. This allows code to directly write to the ShortStr
56 //buffers, but is not generally recommended:
57 struct NCRYSTAL_APIcxx . . $ `ncrystal_core/include/NCrystal/ncapi.h:51:13`! delayed_init_tcxx . . $ NCrystal/ShortStr#delayed_init_t# {};
58 ShortStrcxx . . $ NCrystal/ShortStr#`ShortStr<NMAX>`(e80e4b21e2387729).( delayed_init_tcxx . . $ NCrystal/ShortStr#delayed_init_t#, char** c_handle, size_typecxx . . $ NCrystal/ShortStr#size_type#** s_handle )
59 {
60 *c_handle = &m_datacxx . . $ NCrystal/ShortStr#m_data.[0];
61 *s_handle = &m_sizecxx . . $ NCrystal/ShortStr#m_size.;
62 }
63
64 constexpr const char * datacxx . . $ NCrystal/ShortStr#data(ba32b354807b2b81).() const noexcept;
65 constexpr size_typecxx . . $ NCrystal/ShortStr#size_type# sizecxx . . $ NCrystal/ShortStr#size(d22c8608b6eae7b5).() const noexcept;
66 constexpr bool emptycxx . . $ NCrystal/ShortStr#empty(3482b152b9333168).() const noexcept;
67 constexpr StrViewcxx . . $ NCrystal/StrView# to_viewcxx . . $ NCrystal/ShortStr#to_view(360acda2ac68fa3b).() const noexcept;
68 stdcxx . . $ std/::stringcxx . . $ std/string# to_stringcxx . . $ NCrystal/ShortStr#to_string(a9df4fea84f883e).() const noexcept;
69
70 ShortStrcxx . . $ NCrystal/ShortStr#`ShortStr<NMAX>`(2ad13adf64a3fd96).( const ShortStr& ) = default;
71 ShortStr& operatorcxx . . $ NCrystal/ShortStr#`operator=`(4e5cdd53fb3b278a).=( const ShortStr& ) = default;
72 ShortStrcxx . . $ NCrystal/ShortStr#`ShortStr<NMAX>`(9f3d5eba8a6831f).( ShortStr&& ) = default;
73 ShortStr& operatorcxx . . $ NCrystal/ShortStr#`operator=`(ae64006c3728aafb).=( ShortStr&& ) = default;
74
75 //The following can be used to create constexpr objects from string literals
76 //(i.e. constexpr auto sv = ShortStr::make("foo")): For now it is NOT
77 //constexpr, due to limitations in C++11. This could be revisited if needed.
78 template<size_typecxx . . $ NCrystal/ShortStr#size_type# N>
79 static ShortStr makecxx . . $ NCrystal/ShortStr#make(da84415af24da3fd).( const char (&cdata)[N] ) noexcept
80 {
81 return ShortStr( &cdata[0], ( cdata[N-1]=='\0' ? N-1 : N ) );
82 }
83
84 private:
85 using TDatacxx . . $ NCrystal/ShortStr#TData# = stdcxx . . $ std/::arraycxx . . $ std/array#<char,NMAX>;
86 size_typecxx . . $ NCrystal/ShortStr#size_type# m_sizecxx . . $ NCrystal/ShortStr#m_size.;
87 TDatacxx . . $ NCrystal/ShortStr#TData# m_datacxx . . $ NCrystal/ShortStr#m_data.;
88 };
89
90 template <unsigned NMAX>
91 NCRYSTAL_APIcxx . . $ `ncrystal_core/include/NCrystal/ncapi.h:51:13`! stdcxx . . $ std/::ostreamcxx . . $ std/ostream#& operatorcxx . . $ NCrystal/`operator<<`(ebdf616d4e3f4793).<<(stdcxx . . $ std/::ostreamcxx . . $ std/ostream#&, const ShortStrcxx . . $ NCrystal/ShortStr#<NMAX>&);
92
93 ///////////////////////////////////////////////////////
94 // String <-> FP conversions
95
96 namespace detailcxx . . $ NCrystal/detail/ {
97 Optionalcxx . . $ NCrystal/Optional#<double> raw_str2dblcxx . . $ NCrystal/detail/raw_str2dbl(615c75223d680c66).( const char *, stdcxx . . $ std/::size_tcxx . . $ std/size_t# );
98 Optionalcxx . . $ NCrystal/Optional#<stdcxx . . $ std/::int64_tcxx . . $ std/int64_t#> raw_str2int64cxx . . $ NCrystal/detail/raw_str2int64(576867b20f7390a8).( const char *, stdcxx . . $ std/::size_tcxx . . $ std/size_t# );
99 }
100
101 //Buffer length needed to store any FP converted to string (NOT including any null terminator):
102 namespace detailcxx . . $ NCrystal/detail/ {
103 NCRYSTAL_APIcxx . . $ `ncrystal_core/include/NCrystal/ncapi.h:51:13`! inline constexpr unsigned ncconstexpr_log10ceilcxx . . $ NCrystal/detail/ncconstexpr_log10ceil(2141b656ce2cc063).( unsigned val )
104 {
105 return val < 10u ? 1u : 1u + ncconstexpr_log10ceilcxx . . $ NCrystal/detail/ncconstexpr_log10ceil(2141b656ce2cc063).( val / 10u );
106 }
107
108 template<typename T>
109 NCRYSTAL_APIcxx . . $ `ncrystal_core/include/NCrystal/ncapi.h:51:13`! inline constexpr unsigned ncconstexpr_max_size_fp_strbufcxx . . $ NCrystal/detail/ncconstexpr_max_size_fp_strbuf(db52760e79bf9054).()
110 {
111 return 4u + stdcxx . . $ std/::numeric_limitscxx . . $ std/numeric_limits#<T>::max_digits10
112 + ncconstexpr_maxcxx . . $ NCrystal/ncconstexpr_max(da21754e2b185302).<unsigned>(2u, ncconstexpr_log10ceilcxx . . $ NCrystal/detail/ncconstexpr_log10ceil(2141b656ce2cc063).(stdcxx . . $ std/::numeric_limitscxx . . $ std/numeric_limits#<T>::max_exponent10));
113 }
114 static constexpr unsigned max_size_strbuf_doublecxx . . $ NCrystal/detail/max_size_strbuf_double. = ncconstexpr_max_size_fp_strbufcxx . . $ NCrystal/detail/ncconstexpr_max_size_fp_strbuf(db52760e79bf9054).<double>();//Normally 24
115 static constexpr unsigned max_size_strbuf_floatcxx . . $ NCrystal/detail/max_size_strbuf_float. = ncconstexpr_max_size_fp_strbufcxx . . $ NCrystal/detail/ncconstexpr_max_size_fp_strbuf(db52760e79bf9054).<float>();//Normally 15
116 }
117
118 //Short strings which are just large enough to hold non-lossy encoded floats:
119 using ShortStrDblcxx . . $ NCrystal/ShortStrDbl# = ShortStrcxx . . $ NCrystal/ShortStr#< detailcxx . . $ NCrystal/detail/::max_size_strbuf_doublecxx . . $ NCrystal/detail/max_size_strbuf_double. + 1 >;
120 using ShortStrFltcxx . . $ NCrystal/ShortStrFlt# = ShortStrcxx . . $ NCrystal/ShortStr#< detailcxx . . $ NCrystal/detail/::max_size_strbuf_floatcxx . . $ NCrystal/detail/max_size_strbuf_float. + 1 >;
121
122 //Encode in shortest form which preserves value (unless fmtstr is set):
123 ShortStrDblcxx . . $ NCrystal/ShortStrDbl# dbl2shortstrcxx . . $ NCrystal/dbl2shortstr(ac3770af9fe2595c).( double value, const char * fmtstr = nullptr );
124
125 //Utilities for printing double's to ostreams, either via a format string
126 //(e.g. "%.3g"), or without. The latter prints the shortest/nicest string
127 //which can be re-encoded as a double without loss of precision (in the sense
128 //that if reencoded as a double it will get the original value):
129 //
130 //This means you can do "std::cout << fmt(myvalue)" (shortest loss-less) or
131 //"std::cout << fmt(myvalue,"%.2f")" (specific formatting), without having to
132 //bother with iomanip, stream globals (e.g. std::setprecision), or type unsafe
133 //printf. The alias fmtg(myvalue) means the same as fmt(myvalue,"%g").
134 struct NCRYSTAL_APIcxx . . $ `ncrystal_core/include/NCrystal/ncapi.h:51:13`! detail_FmtDblcxx . . $ NCrystal/detail_FmtDbl# { double valcxx . . $ NCrystal/detail_FmtDbl#val.; const char* fmtstrcxx . . $ NCrystal/detail_FmtDbl#fmtstr.; };
135
136 NCRYSTAL_APIcxx . . $ `ncrystal_core/include/NCrystal/ncapi.h:51:13`! detail_FmtDblcxx . . $ NCrystal/detail_FmtDbl# fmtcxx . . $ NCrystal/fmt(b319caf1895dd43d).( double );
137 NCRYSTAL_APIcxx . . $ `ncrystal_core/include/NCrystal/ncapi.h:51:13`! detail_FmtDblcxx . . $ NCrystal/detail_FmtDbl# fmtcxx . . $ NCrystal/fmt(32e9950fa0dfecdb).( double, const char * fmtstr );
138 NCRYSTAL_APIcxx . . $ `ncrystal_core/include/NCrystal/ncapi.h:51:13`! detail_FmtDblcxx . . $ NCrystal/detail_FmtDbl# fmtgcxx . . $ NCrystal/fmtg(b319caf1895dd43d).( double );//same as fmt(..,"%g")
139 NCRYSTAL_APIcxx . . $ `ncrystal_core/include/NCrystal/ncapi.h:51:13`! stdcxx . . $ std/::ostreamcxx . . $ std/ostream#& operatorcxx . . $ NCrystal/`operator<<`(8aceada0d805403c).<<( stdcxx . . $ std/::ostreamcxx . . $ std/ostream#& , const detail_FmtDblcxx . . $ NCrystal/detail_FmtDbl#& );
140
141 //Similar, but will use the detectSimpleRationalNumbers to detect and print
142 //some simple rational numbers as fractions ("1/3" rather than "0.3333...").
143 struct NCRYSTAL_APIcxx . . $ `ncrystal_core/include/NCrystal/ncapi.h:51:13`! detail_FmtDblFraccxx . . $ NCrystal/detail_FmtDblFrac# { double valcxx . . $ NCrystal/detail_FmtDblFrac#val.; const char* fmtstrcxx . . $ NCrystal/detail_FmtDblFrac#fmtstr.; };
144 stdcxx . . $ std/::ostreamcxx . . $ std/ostream#& operatorcxx . . $ NCrystal/`operator<<`(9ae79f4e9392b25).<<( stdcxx . . $ std/::ostreamcxx . . $ std/ostream#& , const detail_FmtDblFraccxx . . $ NCrystal/detail_FmtDblFrac#& );
145
146 NCRYSTAL_APIcxx . . $ `ncrystal_core/include/NCrystal/ncapi.h:51:13`! detail_FmtDblFraccxx . . $ NCrystal/detail_FmtDblFrac# fmt_fraccxx . . $ NCrystal/fmt_frac(3069794e3200ebf3).( double );
147 NCRYSTAL_APIcxx . . $ `ncrystal_core/include/NCrystal/ncapi.h:51:13`! detail_FmtDblFraccxx . . $ NCrystal/detail_FmtDblFrac# fmt_fraccxx . . $ NCrystal/fmt_frac(42d68ff5d83eb699).( double, const char * fmtstr );
148 NCRYSTAL_APIcxx . . $ `ncrystal_core/include/NCrystal/ncapi.h:51:13`! detail_FmtDblFraccxx . . $ NCrystal/detail_FmtDblFrac# fmtg_fraccxx . . $ NCrystal/fmtg_frac(3069794e3200ebf3).( double );//same as fmt_frac(..,"%g")
149
150 //Find (A,B) so that value ~= A/B. This is only possible for a small subset
151 //of typical values (non-negative integers below 1e9) and some simple fractions with
152 //values in (0,1). When not possible, A=B=0. The main use-case is to allow
153 //for pretty-printing double values which were originally input as fractions
154 //(e.g. we can print 1/3 instead of 0.33333333):
155 stdcxx . . $ std/::pair<unsigned,unsigned> detectSimpleRationalNumberscxx . . $ NCrystal/detectSimpleRationalNumbers(268a9c1fa4d0308a).(double value);
156 }
157
158
159 ////////////////////////////
160 // Inline implementations //
161 ////////////////////////////
162
163 namespace NCrystalcxx . . $ NCrystal/ {
164 template <unsigned NMAX>
165 inline constexpr const char * ShortStrcxx . . $ NCrystal/ShortStr#<NMAX>::datacxx . . $ NCrystal/ShortStr#data(ba32b354807b2b81).() const noexcept { return &m_datacxx . . $ NCrystal/ShortStr#m_data.[0]; }
166
167 template <unsigned NMAX>
168 inline constexpr typename ShortStrcxx . . $ NCrystal/ShortStr#<NMAX>::size_typecxx . . $ NCrystal/ShortStr#size_type# ShortStrcxx . . $ NCrystal/ShortStr#<NMAX>::sizecxx . . $ NCrystal/ShortStr#size(d22c8608b6eae7b5).() const noexcept { return m_sizecxx . . $ NCrystal/ShortStr#m_size.; }
169
170 template <unsigned NMAX>
171 inline constexpr bool ShortStrcxx . . $ NCrystal/ShortStr#<NMAX>::emptycxx . . $ NCrystal/ShortStr#empty(3482b152b9333168).() const noexcept { return m_sizecxx . . $ NCrystal/ShortStr#m_size.==0; }
172
173 template <unsigned NMAX>
174 inline stdcxx . . $ std/::stringcxx . . $ std/string# ShortStrcxx . . $ NCrystal/ShortStr#<NMAX>::to_stringcxx . . $ NCrystal/ShortStr#to_string(a9df4fea84f883e).() const noexcept { return stdcxx . . $ std/::stringcxx . . $ std/string#( datacxx . . $ NCrystal/ShortStr#data(ba32b354807b2b81).(), m_sizecxx . . $ NCrystal/ShortStr#m_size. ); }
175
176 template <unsigned NMAX>
177 inline ShortStrcxx . . $ NCrystal/ShortStr#<NMAX>::ShortStrcxx . . $ NCrystal/ShortStr#`ShortStr<NMAX>`(85c52e162fed56f9).( const char * cstr ) : ShortStr( cstr, stdcxx . . $ std/::strlen(cstr) ) {}
178
179 template <unsigned NMAX>
180 inline ShortStrcxx . . $ NCrystal/ShortStr#<NMAX>::ShortStrcxx . . $ NCrystal/ShortStr#`ShortStr<NMAX>`(b51963283f2467a4).( const char * cstr, unsigned str_len )
181 : m_sizecxx . . $ NCrystal/ShortStr#m_size.(str_len)
182 {
183 nc_assertcxx . . $ `ncrystal_core/include/NCrystal/NCException.hh:153:11`!( cstr );
184 if ( str_len+1 > m_datacxx . . $ NCrystal/ShortStr#m_data..sizecxx . . $ std/array#size(c9b1e749b64d5edc).() ) {
185 stdcxx . . $ std/::ostringstreamcxx . . $ std/ostringstream# sscxx . . $ std/__cxx11/basic_ostringstream#basic_ostringstream(49f6e7a06ebc5aa8).;
186 ss <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "String too long for ShortStr<"<<cxx . . $ std/basic_ostream#`operator<<`(b4c055c363850233).NMAX<<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7).">: \"";
187 ss.writecxx . . $ std/basic_ostream#write(3c866fa06c201852).(cstr,str_len);
188 ss <<cxx . . $ std/`operator<<`(dd93bb5737bfac24). '"';
189 NCRYSTAL_THROWcxx . . $ `ncrystal_core/include/NCrystal/NCException.hh:105:9`!(BadInput,ss.str());
190 }
191 stdcxx . . $ std/::memcpy(&m_datacxx . . $ NCrystal/ShortStr#m_data.[0],cstr,str_len);
192 m_datacxx . . $ NCrystal/ShortStr#m_data.[str_len] = '\0';
193 }
194
195 template <unsigned NMAX>
196 inline stdcxx . . $ std/::ostreamcxx . . $ std/ostream#& operatorcxx . . $ NCrystal/`operator<<`(ebdf616d4e3f4793).<<(stdcxx . . $ std/::ostreamcxx . . $ std/ostream#& os, const ShortStrcxx . . $ NCrystal/ShortStr#<NMAX>& str)
197 {
198 os.writecxx . . $ std/basic_ostream#write(3c866fa06c201852).(str.datacxx . . $ NCrystal/ShortStr#data(ba32b354807b2b81).(),str.sizecxx . . $ NCrystal/ShortStr#size(d22c8608b6eae7b5).());
199 return os;
200 }
201
202 inline detail_FmtDblcxx . . $ NCrystal/detail_FmtDbl# fmtcxx . . $ NCrystal/fmt(b319caf1895dd43d).( double val ) { return { val, nullptr }; }
203 inline detail_FmtDblcxx . . $ NCrystal/detail_FmtDbl# fmtcxx . . $ NCrystal/fmt(32e9950fa0dfecdb).( double val, const char * fmtstr ) { return { val, fmtstr }; }
204 inline detail_FmtDblcxx . . $ NCrystal/detail_FmtDbl# fmtgcxx . . $ NCrystal/fmtg(b319caf1895dd43d).( double val ) { return { val, "%g" }; }
205 inline stdcxx . . $ std/::ostreamcxx . . $ std/ostream#& operatorcxx . . $ NCrystal/`operator<<`(8aceada0d805403c).<<( stdcxx . . $ std/::ostreamcxx . . $ std/ostream#& os , const detail_FmtDblcxx . . $ NCrystal/detail_FmtDbl#& fd ) { return os <<cxx . . $ NCrystal/`operator<<`(ebdf616d4e3f4793). dbl2shortstrcxx . . $ NCrystal/dbl2shortstr(ac3770af9fe2595c).( fd.valcxx . . $ NCrystal/detail_FmtDbl#val., fd.fmtstrcxx . . $ NCrystal/detail_FmtDbl#fmtstr. ); }
206
207 inline detail_FmtDblFraccxx . . $ NCrystal/detail_FmtDblFrac# fmt_fraccxx . . $ NCrystal/fmt_frac(3069794e3200ebf3).( double val ) { return { val, nullptr }; }
208 inline detail_FmtDblFraccxx . . $ NCrystal/detail_FmtDblFrac# fmt_fraccxx . . $ NCrystal/fmt_frac(42d68ff5d83eb699).( double val, const char * fmtstr ) { return { val, fmtstr }; }
209 inline detail_FmtDblFraccxx . . $ NCrystal/detail_FmtDblFrac# fmtg_fraccxx . . $ NCrystal/fmtg_frac(3069794e3200ebf3).( double val ) { return { val, "%g" }; }
210 }
211
212
213 #endif