@@ -37,29 +37,28 @@ namespace MsgFormat
3737{
3838
3939// Enough to the current conversions. If SINT128 is decoded as a full number
40- // instead of two parts, it may be updated to 64 and 63 respectively,
41- // because 2^128 ~ 3.4e38.
42- const int DECODE_BUF_SIZE = 32 ;
43- const int DECODE_BUF_LEN = 31 ;
40+ // instead of two parts, it may be updated to 63, because 2^128 ~ 3.4e38.
41+ static constexpr int DECODE_BUF_LEN = 31 ;
42+ static constexpr int DECODE_BUF_SIZE = DECODE_BUF_LEN + 1 ;
4443
4544// The maximum numeric base we support, using 0..Z
46- const int MAX_RADIX = 36 ;
45+ static constexpr int MAX_RADIX = 36 ;
4746// We don't mess with octal and the like, otherwise DECODE_BUF_* constants have to be enlarged.
48- const int MIN_RADIX = 10 ;
47+ static constexpr int MIN_RADIX = 10 ;
4948// We won't output strings of more than 64K.
50- const FB_SIZE_T MAX_STRING = 1 << 16 ;
49+ static constexpr FB_SIZE_T MAX_STRING = 1 << 16 ;
5150
5251// Generic functions.
53- int decode (uint64_t value, char * const rc, int radix = 10 );
54- int decode (int64_t value, char * const rc, int radix = 10 );
55- int decode (double value, char * const rc, const size_t sz);
56- int adjust_prefix (int radix, int rev, bool is_neg, char * const rc);
52+ int decode (uint64_t value, char * const rc, int radix = 10 ) noexcept ;
53+ int decode (int64_t value, char * const rc, int radix = 10 ) noexcept ;
54+ int decode (double value, char * const rc, const size_t sz) noexcept ;
55+ int adjust_prefix (int radix, int rev, bool is_neg, char * const rc) noexcept ;
5756int MsgPrintHelper (BaseStream& out_stream, const safe_cell& item);
5857
5958
6059
6160// Decode unsigned integer values in any base between 10 and 36.
62- int decode (uint64_t value, char * const rc, int radix)
61+ int decode (uint64_t value, char * const rc, int radix) noexcept
6362{
6463 int rev = DECODE_BUF_LEN;
6564 if (radix < MIN_RADIX || radix > MAX_RADIX)
@@ -79,7 +78,7 @@ int decode(uint64_t value, char* const rc, int radix)
7978 {
8079 while (true )
8180 {
82- int temp = static_cast <int >(value % radix);
81+ const int temp = static_cast <int >(value % radix);
8382 rc[rev--] = static_cast <unsigned char >(temp < 10 ? temp + ' 0' : temp - 10 + ' A' );
8483 value /= radix;
8584 if (!value)
@@ -92,7 +91,7 @@ int decode(uint64_t value, char* const rc, int radix)
9291
9392
9493// Decode signed integer values in any base between 10 and 36.
95- int decode (int64_t value, char * const rc, int radix)
94+ int decode (int64_t value, char * const rc, int radix) noexcept
9695{
9796 if (value >= 0 )
9897 return decode (static_cast <uint64_t >(value), rc, radix);
@@ -106,7 +105,7 @@ int decode(int64_t value, char* const rc, int radix)
106105 {
107106 while (true )
108107 {
109- int64_t temp = (value / 10 ) * 10 - value;
108+ const int64_t temp = (value / 10 ) * 10 - value;
110109 rc[rev--] = static_cast <unsigned char >(temp) + ' 0' ;
111110 value /= 10 ;
112111 if (!value)
@@ -117,7 +116,7 @@ int decode(int64_t value, char* const rc, int radix)
117116 {
118117 while (true )
119118 {
120- int64_t temp = (value / radix) * radix - value;
119+ const int64_t temp = (value / radix) * radix - value;
121120 rc[rev--] = static_cast <unsigned char >(temp < 10 ? temp + ' 0' : temp - 10 + ' A' );
122121 value /= radix;
123122 if (!value)
@@ -131,15 +130,15 @@ int decode(int64_t value, char* const rc, int radix)
131130
132131// Stub that relies on the printf family to write a double using "g"
133132// for smallest representation in text form.
134- int decode (double value, char * const rc, const size_t sz)
133+ int decode (double value, char * const rc, const size_t sz) noexcept
135134{
136135 const int n = snprintf (rc, sz, " %g" , value);
137136 return std::min (n, static_cast <int >(sz - 1 ));
138137}
139138
140139
141140// Sets the radix indicator and returns the length of the adjusted string.
142- int adjust_prefix (int radix, int rev, bool is_neg, char * const rc)
141+ int adjust_prefix (int radix, int rev, bool is_neg, char * const rc) noexcept
143142{
144143 int fwd = 0 ;
145144
@@ -183,21 +182,21 @@ int MsgPrintHelper(BaseStream& out_stream, const safe_cell& item)
183182 case safe_cell::at_int64:
184183 {
185184 char s[DECODE_BUF_SIZE];
186- int n = decode (item.i_value , s);
185+ const int n = decode (item.i_value , s);
187186 return out_stream.write (s, n);
188187 }
189188 case safe_cell::at_uint64:
190189 {
191190 char s[DECODE_BUF_SIZE];
192- int n = decode (static_cast <uint64_t >(item.i_value ), s);
191+ const int n = decode (static_cast <uint64_t >(item.i_value ), s);
193192 return out_stream.write (s, n);
194193 }
195194 case safe_cell::at_int128:
196195 {
197196 // Warning: useless display in real life
198197 char s[DECODE_BUF_SIZE];
199198 int n = decode (item.i128_value .high , s);
200- int n2 = out_stream.write (s, n) + out_stream.write (" ." , 1 );
199+ const int n2 = out_stream.write (s, n) + out_stream.write (" ." , 1 );
201200 n = decode (item.i128_value .low , s);
202201 return n2 + out_stream.write (s, n);
203202 }
@@ -221,9 +220,9 @@ int MsgPrintHelper(BaseStream& out_stream, const safe_cell& item)
221220 }
222221 case safe_cell::at_ptr:
223222 {
224- uint64_t v = reinterpret_cast <uint64_t >(item.p_value );
223+ const uint64_t v = reinterpret_cast <uint64_t >(item.p_value );
225224 char s[DECODE_BUF_SIZE];
226- int n = decode (v, s, 16 );
225+ const int n = decode (v, s, 16 );
227226 return out_stream.write (s, n);
228227 }
229228 default : // safe_cell::at_none and whatever out of range.
@@ -371,7 +370,7 @@ int fb_msg_format(void* handle, USHORT facility, USHORT number, unsigned int bsi
371370 {
372371 const TEXT* rep[5 ];
373372 arg.dump (rep, 5 );
374- total_msg = fb_utils:: snprintf (buffer, bsize, msg, rep[0 ], rep[1 ], rep[2 ], rep[3 ], rep[4 ]);
373+ total_msg = snprintf (buffer, bsize, msg, rep[0 ], rep[1 ], rep[2 ], rep[3 ], rep[4 ]);
375374 }
376375 else
377376 total_msg = MsgPrint (buffer, bsize, msg, arg);
@@ -390,7 +389,7 @@ int fb_msg_format(void* handle, USHORT facility, USHORT number, unsigned int bsi
390389 }
391390 else
392391 {
393- fb_utils:: snprintf (buffer, bsize, " message system code %d" , n);
392+ snprintf (buffer, bsize, " message system code %d" , n);
394393 s += buffer;
395394 }
396395 total_msg = s.copyTo (buffer, bsize);
0 commit comments