19
19
20
20
#include < algorithm>
21
21
#include " winerror_cross_platform.h"
22
- #include < sstream>
23
22
#include < cstring> // for memmove, memcpy
24
23
#include " ratpak.h"
25
24
@@ -85,7 +84,7 @@ namespace
85
84
86
85
if (ullOperand <= UINT32_MAX)
87
86
{
88
- *pulResult = ( uint32_t ) ullOperand;
87
+ *pulResult = static_cast < uint32_t >( ullOperand) ;
89
88
hr = S_OK;
90
89
}
91
90
@@ -130,7 +129,7 @@ void* zmalloc(size_t a)
130
129
131
130
void _dupnum (_In_ PNUMBER dest, _In_ const NUMBER* const src)
132
131
{
133
- memcpy (dest, src, ( int )( sizeof (NUMBER) + (( src) ->cdigit ) * ( sizeof (MANTTYPE)) ));
132
+ memcpy (dest, src, sizeof (NUMBER) + src->cdigit * sizeof (MANTTYPE));
134
133
}
135
134
136
135
// -----------------------------------------------------------------------------
@@ -148,10 +147,12 @@ void _dupnum(_In_ PNUMBER dest, _In_ const NUMBER* const src)
148
147
void _destroynum (_Frees_ptr_opt_ PNUMBER pnum)
149
148
150
149
{
151
- if (pnum ! = nullptr )
150
+ if (pnum = = nullptr )
152
151
{
153
- free (pnum) ;
152
+ return ;
154
153
}
154
+
155
+ free (pnum);
155
156
}
156
157
157
158
// -----------------------------------------------------------------------------
@@ -170,12 +171,13 @@ void _destroynum(_Frees_ptr_opt_ PNUMBER pnum)
170
171
void _destroyrat (_Frees_ptr_opt_ PRAT prat)
171
172
172
173
{
173
- if (prat ! = nullptr )
174
+ if (prat = = nullptr )
174
175
{
175
- destroynum (prat->pp );
176
- destroynum (prat->pq );
177
- free (prat);
176
+ return ;
178
177
}
178
+ destroynum (prat->pp );
179
+ destroynum (prat->pq );
180
+ free (prat);
179
181
}
180
182
181
183
// -----------------------------------------------------------------------------
@@ -200,7 +202,7 @@ PNUMBER _createnum(_In_ uint32_t size)
200
202
if (SUCCEEDED (Calc_ULongAdd (size, 1 , &cbAlloc)) && SUCCEEDED (Calc_ULongMult (cbAlloc, sizeof (MANTTYPE), &cbAlloc))
201
203
&& SUCCEEDED (Calc_ULongAdd (cbAlloc, sizeof (NUMBER), &cbAlloc)))
202
204
{
203
- pnumret = ( PNUMBER) zmalloc (cbAlloc);
205
+ pnumret = static_cast < PNUMBER>( zmalloc (cbAlloc) );
204
206
if (pnumret == nullptr )
205
207
{
206
208
throw (CALC_E_OUTOFMEMORY);
@@ -227,12 +229,9 @@ PNUMBER _createnum(_In_ uint32_t size)
227
229
//
228
230
// -----------------------------------------------------------------------------
229
231
230
- PRAT _createrat (void )
231
-
232
+ PRAT _createrat ()
232
233
{
233
- PRAT prat = nullptr ;
234
-
235
- prat = (PRAT)zmalloc (sizeof (RAT));
234
+ PRAT prat = static_cast <PRAT>(zmalloc (sizeof (RAT)));
236
235
237
236
if (prat == nullptr )
238
237
{
@@ -303,16 +302,16 @@ PRAT numtorat(_In_ PNUMBER pin, uint32_t radix)
303
302
PNUMBER nRadixxtonum (_In_ PNUMBER a, uint32_t radix, int32_t precision)
304
303
305
304
{
306
- PNUMBER sum = i32tonum (0 , radix);
307
- PNUMBER powofnRadix = i32tonum (BASEX, radix);
305
+ PNUMBER sum = Ui32tonum (0 , radix);
306
+ PNUMBER powofnRadix = Ui32tonum (BASEX, radix);
308
307
309
308
// A large penalty is paid for conversion of digits no one will see anyway.
310
309
// limit the digits to the minimum of the existing precision or the
311
310
// requested precision.
312
- uint32_t cdigits = precision + 1 ;
313
- if (cdigits > ( uint32_t ) a->cdigit )
311
+ int32_t cdigits = precision + 1 ;
312
+ if (cdigits > a->cdigit )
314
313
{
315
- cdigits = ( uint32_t ) a->cdigit ;
314
+ cdigits = a->cdigit ;
316
315
}
317
316
318
317
// scale by the internal base to the internal exponent offset of the LSD
@@ -322,7 +321,7 @@ PNUMBER nRadixxtonum(_In_ PNUMBER a, uint32_t radix, int32_t precision)
322
321
for (MANTTYPE* ptr = &(a->mant [a->cdigit - 1 ]); cdigits > 0 ; ptr--, cdigits--)
323
322
{
324
323
// Loop over all the bits from MSB to LSB
325
- for (uint32_t bitmask = BASEX / 2 ; bitmask > 0 ; bitmask /= 2 )
324
+ for (auto bitmask = BASEX >> 1 ; bitmask > 0 ; bitmask >>= 1 )
326
325
{
327
326
addnum (&sum, sum, radix);
328
327
if (*ptr & bitmask)
@@ -356,8 +355,8 @@ PNUMBER nRadixxtonum(_In_ PNUMBER a, uint32_t radix, int32_t precision)
356
355
357
356
PNUMBER numtonRadixx (_In_ PNUMBER a, uint32_t radix)
358
357
{
359
- PNUMBER pnumret = i32tonum (0 , BASEX); // pnumret is the number in internal form.
360
- PNUMBER num_radix = i32tonum (radix, BASEX);
358
+ PNUMBER pnumret = Ui32tonum (0 , BASEX); // pnumret is the number in internal form.
359
+ PNUMBER num_radix = Ui32tonum (radix, BASEX);
361
360
MANTTYPE* ptrdigit = a->mant ; // pointer to digit being worked on.
362
361
363
362
// Digits are in reverse order, back over them LSD first.
@@ -370,7 +369,7 @@ PNUMBER numtonRadixx(_In_ PNUMBER a, uint32_t radix)
370
369
// WARNING:
371
370
// This should just smack in each digit into a 'special' thisdigit.
372
371
// and not do the overhead of recreating the number type each time.
373
- thisdigit = i32tonum (*ptrdigit--, BASEX);
372
+ thisdigit = Ui32tonum (*ptrdigit--, BASEX);
374
373
addnum (&pnumret, thisdigit, BASEX);
375
374
destroynum (thisdigit);
376
375
}
@@ -611,12 +610,12 @@ wchar_t NormalizeCharDigit(wchar_t c, uint32_t radix)
611
610
612
611
PNUMBER StringToNumber (wstring_view numberString, uint32_t radix, int32_t precision)
613
612
{
614
- int32_t expSign = 1L ; // expSign is exponent sign ( +/- 1 )
615
- int32_t expValue = 0L ; // expValue is exponent mantissa, should be unsigned
613
+ int32_t expSign = 1 ; // expSign is exponent sign ( +/- 1 )
614
+ int32_t expValue = 0 ; // expValue is exponent mantissa, should be unsigned
616
615
617
616
PNUMBER pnumret = nullptr ;
618
617
createnum (pnumret, static_cast <uint32_t >(numberString.length ()));
619
- pnumret->sign = 1L ;
618
+ pnumret->sign = 1 ;
620
619
pnumret->cdigit = 0 ;
621
620
pnumret->exp = 0 ;
622
621
MANTTYPE* pmant = pnumret->mant + numberString.length () - 1 ;
@@ -824,7 +823,7 @@ PNUMBER i32tonum(int32_t ini32, uint32_t radix)
824
823
825
824
do
826
825
{
827
- *pmant++ = ( MANTTYPE) (ini32 % radix);
826
+ *pmant++ = static_cast < MANTTYPE> (ini32 % radix);
828
827
ini32 /= radix;
829
828
pnumret->cdigit ++;
830
829
} while (ini32);
@@ -859,7 +858,7 @@ PNUMBER Ui32tonum(uint32_t ini32, uint32_t radix)
859
858
860
859
do
861
860
{
862
- *pmant++ = ( MANTTYPE) (ini32 % radix);
861
+ *pmant++ = static_cast < MANTTYPE> (ini32 % radix);
863
862
ini32 /= radix;
864
863
pnumret->cdigit ++;
865
864
} while (ini32);
@@ -972,7 +971,7 @@ uint64_t rattoUi64(_In_ PRAT prat, uint32_t radix, int32_t precision)
972
971
destroyrat (prat32);
973
972
destroyrat (pint);
974
973
975
- return ((( uint64_t )hi << 32 ) | lo) ;
974
+ return (static_cast < uint64_t >(hi) << 32 ) | lo;
976
975
}
977
976
978
977
// -----------------------------------------------------------------------------
@@ -992,17 +991,16 @@ int32_t numtoi32(_In_ PNUMBER pnum, uint32_t radix)
992
991
{
993
992
int32_t lret = 0 ;
994
993
995
- MANTTYPE* pmant = pnum->mant ;
996
- pmant += pnum->cdigit - 1 ;
994
+ MANTTYPE* pmant = pnum->mant + pnum->cdigit - 1 ;
997
995
998
- int32_t expt = pnum->exp ;
999
- for (int32_t length = pnum->cdigit ; length > 0 && length + expt > 0 ; length--)
996
+ auto expt = pnum->exp ;
997
+ for (auto length = pnum->cdigit ; length > 0 && length + expt > 0 ; length--)
1000
998
{
1001
999
lret *= radix;
1002
1000
lret += *(pmant--);
1003
1001
}
1004
1002
1005
- while ( expt-- > 0 )
1003
+ for (; expt > 0 ; --expt )
1006
1004
{
1007
1005
lret *= radix;
1008
1006
}
@@ -1025,37 +1023,34 @@ int32_t numtoi32(_In_ PNUMBER pnum, uint32_t radix)
1025
1023
1026
1024
bool stripzeroesnum (_Inout_ PNUMBER pnum, int32_t starting)
1027
1025
{
1028
- bool fstrip = false ;
1029
1026
// point pmant to the LeastCalculatedDigit
1030
1027
MANTTYPE* pmant = pnum->mant ;
1031
1028
int32_t cdigits = pnum->cdigit ;
1032
1029
// point pmant to the LSD
1033
1030
if (cdigits > starting)
1034
1031
{
1035
- pmant += cdigits - starting;
1032
+ pmant = pmant + cdigits - starting;
1036
1033
cdigits = starting;
1037
1034
}
1038
1035
1039
1036
// Check we haven't gone too far, and we are still looking at zeros.
1040
- while ((cdigits > 0 ) && !(*pmant))
1037
+ if (cdigits < 1 || *pmant != 0 )
1038
+ return false ;
1039
+
1040
+ do
1041
1041
{
1042
1042
// move to next significant digit and keep track of digits we can
1043
1043
// ignore later.
1044
1044
pmant++;
1045
1045
cdigits--;
1046
- fstrip = true ;
1047
- }
1048
-
1049
- // If there are zeros to remove.
1050
- if (fstrip)
1051
- {
1052
- // Remove them.
1053
- memmove (pnum->mant , pmant, (int )(cdigits * sizeof (MANTTYPE)));
1054
- // And adjust exponent and digit count accordingly.
1055
- pnum->exp += (pnum->cdigit - cdigits);
1056
- pnum->cdigit = cdigits;
1057
- }
1058
- return (fstrip);
1046
+ } while (cdigits > 0 && *pmant == 0 );
1047
+
1048
+ // Remove the zeros
1049
+ memmove (pnum->mant , pmant, cdigits * sizeof (MANTTYPE));
1050
+ // And adjust exponent and digit count accordingly.
1051
+ pnum->exp += pnum->cdigit - cdigits;
1052
+ pnum->cdigit = cdigits;
1053
+ return true ;
1059
1054
}
1060
1055
1061
1056
// -----------------------------------------------------------------------------
@@ -1099,7 +1094,7 @@ wstring NumberToString(_Inout_ PNUMBER& pnum, NumberFormat format, uint32_t radi
1099
1094
if (!zernum (pnum) && (pnum->cdigit >= precision || (length - exponent > precision && exponent >= -MAX_ZEROS_AFTER_DECIMAL)))
1100
1095
{
1101
1096
// Otherwise round.
1102
- round = i32tonum (radix, radix);
1097
+ round = Ui32tonum (radix, radix);
1103
1098
divnum (&round, num_two, radix, precision);
1104
1099
1105
1100
// Make round number exponent one below the LSD for the number.
@@ -1202,7 +1197,7 @@ wstring NumberToString(_Inout_ PNUMBER& pnum, NumberFormat format, uint32_t radi
1202
1197
result = L' -' ;
1203
1198
}
1204
1199
1205
- if (exponent <= 0 && !useSciForm)
1200
+ if (exponent < 1 && !useSciForm)
1206
1201
{
1207
1202
result += L' 0' ;
1208
1203
result += g_decimalSeparator;
@@ -1217,26 +1212,24 @@ wstring NumberToString(_Inout_ PNUMBER& pnum, NumberFormat format, uint32_t radi
1217
1212
1218
1213
while (length > 0 )
1219
1214
{
1220
- exponent--;
1221
1215
result += DIGITS[*pmant--];
1222
1216
length--;
1223
1217
1224
1218
// Be more regular in using a decimal point.
1225
- if (exponent == 0 )
1219
+ if (-- exponent == 0 )
1226
1220
{
1227
1221
result += g_decimalSeparator;
1228
1222
}
1229
1223
}
1230
1224
1231
- while (exponent > 0 )
1225
+ if (exponent > 0 )
1232
1226
{
1233
- result += L' 0' ;
1234
- exponent--;
1235
- // Be more regular in using a decimal point.
1236
- if (exponent == 0 )
1227
+ do
1237
1228
{
1238
- result += g_decimalSeparator;
1239
- }
1229
+ result += L' 0' ;
1230
+ } while (--exponent > 0 );
1231
+ // Be more regular in using a decimal point.
1232
+ result += g_decimalSeparator;
1240
1233
}
1241
1234
1242
1235
if (useSciForm)
@@ -1439,15 +1432,16 @@ PNUMBER i32prodnum(int32_t start, int32_t stop, uint32_t radix)
1439
1432
1440
1433
lret = i32tonum (1 , radix);
1441
1434
1442
- while ( start <= stop)
1435
+ for (; start <= stop; start++ )
1443
1436
{
1444
- if (start)
1437
+ if (! start)
1445
1438
{
1446
- tmp = i32tonum (start, radix);
1447
- mulnum (&lret, tmp, radix);
1448
- destroynum (tmp);
1439
+ continue ;
1449
1440
}
1450
- start++;
1441
+
1442
+ tmp = i32tonum (start, radix);
1443
+ mulnum (&lret, tmp, radix);
1444
+ destroynum (tmp);
1451
1445
}
1452
1446
return (lret);
1453
1447
}
@@ -1470,15 +1464,14 @@ void numpowi32(_Inout_ PNUMBER* proot, int32_t power, uint32_t radix, int32_t pr
1470
1464
{
1471
1465
PNUMBER lret = i32tonum (1 , radix);
1472
1466
1473
- while ( power > 0 )
1467
+ for (; power > 0 ; power >>= 1 )
1474
1468
{
1475
1469
if (power & 1 )
1476
1470
{
1477
1471
mulnum (&lret, *proot, radix);
1478
1472
}
1479
1473
mulnum (proot, *proot, radix);
1480
1474
TRIMNUM (*proot, precision);
1481
- power >>= 1 ;
1482
1475
}
1483
1476
destroynum (*proot);
1484
1477
*proot = lret;
@@ -1511,9 +1504,7 @@ void ratpowi32(_Inout_ PRAT* proot, int32_t power, int32_t precision)
1511
1504
}
1512
1505
else
1513
1506
{
1514
- PRAT lret = nullptr ;
1515
-
1516
- lret = i32torat (1 );
1507
+ PRAT lret = i32torat (1 );
1517
1508
1518
1509
while (power > 0 )
1519
1510
{
0 commit comments