Skip to content

Commit 8ca1427

Browse files
committed
Rename and format strToInt64 -> StrToInt64
1 parent 56ed2aa commit 8ca1427

5 files changed

Lines changed: 84 additions & 81 deletions

File tree

src/mastercore_parse_string.cpp

Lines changed: 50 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -8,54 +8,57 @@
88

99
namespace mastercore
1010
{
11-
int64_t strToInt64(std::string strAmount, bool divisible)
12-
{
13-
int64_t Amount = 0;
11+
int64_t StrToInt64(const std::string& str, bool divisible)
12+
{
13+
// copy original, so it remains unchanged
14+
std::string strAmount (str);
15+
int64_t nAmount = 0;
16+
17+
// check for a negative (minus sign) and invalidate if present
18+
size_t negSignPos = strAmount.find("-");
19+
if (negSignPos != std::string::npos) return 0;
1420

15-
//check for a negative (minus sign) and invalidate if present
16-
size_t negSignPos = strAmount.find("-");
17-
if (negSignPos!=std::string::npos) return 0;
21+
// convert the string into a usable int64
22+
if (divisible) {
23+
// check for existance of decimal point
24+
size_t pos = strAmount.find(".");
25+
if (pos == std::string::npos) {
26+
// no decimal point but divisible so pad 8 zeros on right
27+
strAmount += "00000000";
28+
} else {
29+
// check for existence of second decimal point, if so invalidate amount
30+
size_t posSecond = strAmount.find(".", pos + 1);
31+
if (posSecond != std::string::npos) return 0;
32+
33+
if ((strAmount.size() - pos) < 9) {
34+
// there are decimals either exact or not enough, pad as needed
35+
std::string strRightOfDecimal = strAmount.substr(pos + 1);
36+
unsigned int zerosToPad = 8 - strRightOfDecimal.size();
37+
38+
// do we need to pad?
39+
if (zerosToPad > 0)
40+
{
41+
for (unsigned int it = 0; it != zerosToPad; it++) {
42+
strAmount += "0";
43+
}
44+
}
45+
} else {
46+
// there are too many decimals, truncate after 8
47+
strAmount = strAmount.substr(0, pos + 9);
48+
}
49+
}
50+
strAmount.erase(std::remove(strAmount.begin(), strAmount.end(), '.'), strAmount.end());
51+
try {
52+
nAmount = boost::lexical_cast<int64_t>(strAmount);
53+
} catch (const boost::bad_lexical_cast &e) {}
54+
} else {
55+
size_t pos = strAmount.find(".");
56+
std::string newStrAmount = strAmount.substr(0, pos);
57+
try {
58+
nAmount = boost::lexical_cast<int64_t>(newStrAmount);
59+
} catch (const boost::bad_lexical_cast &e) {}
60+
}
1861

19-
//convert the string into a usable int64
20-
if (divisible)
21-
{
22-
//check for existance of decimal point
23-
size_t pos = strAmount.find(".");
24-
if (pos==std::string::npos)
25-
{ //no decimal point but divisible so pad 8 zeros on right
26-
strAmount+="00000000";
27-
}
28-
else
29-
{
30-
size_t posSecond = strAmount.find(".", pos+1); //check for existence of second decimal point, if so invalidate amount
31-
if (posSecond!=std::string::npos) return 0;
32-
if ((strAmount.size()-pos)<9)
33-
{ //there are decimals either exact or not enough, pad as needed
34-
std::string strRightOfDecimal = strAmount.substr(pos+1);
35-
unsigned int zerosToPad = 8-strRightOfDecimal.size();
36-
if (zerosToPad>0) //do we need to pad?
37-
{
38-
for(unsigned int it = 0; it != zerosToPad; it++)
39-
{
40-
strAmount+="0";
41-
}
42-
}
43-
}
44-
else
45-
{ //there are too many decimals, truncate after 8
46-
strAmount = strAmount.substr(0,pos+9);
47-
}
48-
}
49-
strAmount.erase(std::remove(strAmount.begin(), strAmount.end(), '.'), strAmount.end());
50-
try { Amount = boost::lexical_cast<int64_t>(strAmount); } catch(const boost::bad_lexical_cast &e) { }
51-
}
52-
else
53-
{
54-
size_t pos = strAmount.find(".");
55-
std::string newStrAmount = strAmount.substr(0,pos);
56-
try { Amount = boost::lexical_cast<int64_t>(newStrAmount); } catch(const boost::bad_lexical_cast &e) { }
57-
}
58-
return Amount;
62+
return nAmount;
5963
}
60-
6164
} // namespace mastercore

src/mastercore_parse_string.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ namespace mastercore
1414
// Characters after decimal mark are ignored for indivisible
1515
// amounts.
1616
// Any minus sign invalidates.
17-
int64_t strToInt64(std::string strAmount, bool divisible);
17+
int64_t StrToInt64(const std::string& str, bool divisible);
1818
}
1919

2020
#endif // _MASTERCOIN_PARSE_STRRING

src/mastercore_rpc.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -222,13 +222,13 @@ if (fHelp || params.size() < 4 || params.size() > 6)
222222

223223
string strAmount = params[3].get_str();
224224
int64_t Amount = 0, additional = 0;
225-
Amount = strToInt64(strAmount, divisible);
225+
Amount = StrToInt64(strAmount, divisible);
226226

227227
if (0 >= Amount)
228228
throw JSONRPCError(RPC_TYPE_ERROR, "Invalid amount");
229229

230230
std::string strAdditional = (params.size() > 5) ? (params[5].get_str()): "0";
231-
additional = strToInt64(strAdditional, true);
231+
additional = StrToInt64(strAdditional, true);
232232

233233
int n = params.size();
234234
printf("#: %d, additional= %ld\n", n, additional);
@@ -285,7 +285,7 @@ if (fHelp || params.size() < 3 || params.size() > 4)
285285

286286
string strAmount = params[2].get_str();
287287
int64_t Amount = 0;
288-
Amount = strToInt64(strAmount, divisible);
288+
Amount = StrToInt64(strAmount, divisible);
289289

290290
if (0 >= Amount)
291291
throw JSONRPCError(RPC_TYPE_ERROR, "Invalid amount");
@@ -329,7 +329,7 @@ if (fHelp || params.size() < 2 || params.size() > 5)
329329
int64_t referenceAmount = 0;
330330

331331
if (params.size() > 4)
332-
referenceAmount = strToInt64(params[4].get_str(), true);
332+
referenceAmount = StrToInt64(params[4].get_str(), true);
333333

334334
//some sanity checking of the data supplied?
335335
uint256 newTX;
@@ -1093,11 +1093,11 @@ Value trade_MP(const Array& params, bool fHelp) {
10931093

10941094
std::string strAmountSale = params[1].get_str();
10951095
int64_t Amount_Sale = 0;
1096-
Amount_Sale = strToInt64(strAmountSale, divisible_sale);
1096+
Amount_Sale = StrToInt64(strAmountSale, divisible_sale);
10971097

10981098
std::string strAmountWant = params[3].get_str();
10991099
int64_t Amount_Want = 0;
1100-
Amount_Want = strToInt64(strAmountWant, divisible_want);
1100+
Amount_Want = StrToInt64(strAmountWant, divisible_want);
11011101

11021102
if (0 >= Amount_Sale)
11031103
throw JSONRPCError(RPC_TYPE_ERROR, "Invalid amount (Sale)");

src/qt/sendmpdialog.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ void SendMPDialog::sendMPTransaction()
293293
}
294294

295295
// use strToInt64 function to get the amount, using divisibility of the property
296-
int64_t sendAmount = strToInt64(strAmount, divisible);
296+
int64_t sendAmount = StrToInt64(strAmount, divisible);
297297
if (0>=sendAmount)
298298
{
299299
QMessageBox::critical( this, "Unable to send transaction",

src/test/mastercore_strtoint64_tests.cpp

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -11,67 +11,67 @@ BOOST_AUTO_TEST_SUITE(mastercore_strtoint64_tests)
1111
BOOST_AUTO_TEST_CASE(mastercore_strtoint64_invidisible)
1212
{
1313
// zero amount
14-
BOOST_CHECK(strToInt64("0", false) == 0);
14+
BOOST_CHECK(StrToInt64("0", false) == 0);
1515
// big num
16-
BOOST_CHECK(strToInt64("4000000000000000", false) == 4000000000000000);
16+
BOOST_CHECK(StrToInt64("4000000000000000", false) == 4000000000000000);
1717
// max int64
18-
BOOST_CHECK(strToInt64("9223372036854775807", false) == 9223372036854775807);
18+
BOOST_CHECK(StrToInt64("9223372036854775807", false) == 9223372036854775807);
1919
}
2020

2121
BOOST_AUTO_TEST_CASE(mastercore_strtoint64_invidisible_truncate)
2222
{
2323
// ignore any char after decimal mark
24-
BOOST_CHECK(strToInt64("8.76543210123456878901", false) == 8);
25-
BOOST_CHECK(strToInt64("8.765432101.2345687890", false) == 8);
26-
BOOST_CHECK(strToInt64("2345.AbCdEhf71z1.23", false) == 2345);
24+
BOOST_CHECK(StrToInt64("8.76543210123456878901", false) == 8);
25+
BOOST_CHECK(StrToInt64("8.765432101.2345687890", false) == 8);
26+
BOOST_CHECK(StrToInt64("2345.AbCdEhf71z1.23", false) == 2345);
2727
}
2828

2929
BOOST_AUTO_TEST_CASE(mastercore_strtoint64_invidisible_invalid)
3030
{
3131
// invalid, number is negative
32-
BOOST_CHECK(strToInt64("-4", false) == 0);
32+
BOOST_CHECK(StrToInt64("-4", false) == 0);
3333
// invalid, number is over max int64
34-
BOOST_CHECK(strToInt64("9223372036854775808", false) == 0);
34+
BOOST_CHECK(StrToInt64("9223372036854775808", false) == 0);
3535
// invalid, minus sign in string
36-
BOOST_CHECK(strToInt64("2345.AbCdEFG71z88-1.23", false) == 0);
36+
BOOST_CHECK(StrToInt64("2345.AbCdEFG71z88-1.23", false) == 0);
3737
}
3838

3939
BOOST_AUTO_TEST_CASE(mastercore_strtoint64_divisible)
4040
{
4141
// range 0 to max int64
42-
BOOST_CHECK(strToInt64("0.000", true) == 0);
43-
BOOST_CHECK(strToInt64("92233720368.54775807", true) == 9223372036854775807);
42+
BOOST_CHECK(StrToInt64("0.000", true) == 0);
43+
BOOST_CHECK(StrToInt64("92233720368.54775807", true) == 9223372036854775807);
4444
// check padding
45-
BOOST_CHECK(strToInt64("0.00000004", true) == 4);
46-
BOOST_CHECK(strToInt64("0.0000004", true) == 40);
47-
BOOST_CHECK(strToInt64("0.0004", true) == 40000);
48-
BOOST_CHECK(strToInt64("0.4", true) == 40000000);
49-
BOOST_CHECK(strToInt64("4.0", true) == 400000000);
45+
BOOST_CHECK(StrToInt64("0.00000004", true) == 4);
46+
BOOST_CHECK(StrToInt64("0.0000004", true) == 40);
47+
BOOST_CHECK(StrToInt64("0.0004", true) == 40000);
48+
BOOST_CHECK(StrToInt64("0.4", true) == 40000000);
49+
BOOST_CHECK(StrToInt64("4.0", true) == 400000000);
5050
// truncate after 8 digits
51-
BOOST_CHECK(strToInt64("40.00000000000099", true) == 4000000000);
52-
BOOST_CHECK(strToInt64("92233720368.54775807000", true) == 9223372036854775807);
51+
BOOST_CHECK(StrToInt64("40.00000000000099", true) == 4000000000);
52+
BOOST_CHECK(StrToInt64("92233720368.54775807000", true) == 9223372036854775807);
5353
}
5454

5555
BOOST_AUTO_TEST_CASE(mastercore_strtoint64_divisible_truncate)
5656
{
5757
// truncate after 8 digits
58-
BOOST_CHECK(strToInt64("40.00000000000099", true) == 4000000000);
59-
BOOST_CHECK(strToInt64("92233720368.54775807000", true) == 9223372036854775807);
60-
BOOST_CHECK(strToInt64("92233720368.54775807000", true) == 9223372036854775807);
58+
BOOST_CHECK(StrToInt64("40.00000000000099", true) == 4000000000);
59+
BOOST_CHECK(StrToInt64("92233720368.54775807000", true) == 9223372036854775807);
60+
BOOST_CHECK(StrToInt64("92233720368.54775807000", true) == 9223372036854775807);
6161
}
6262

6363
BOOST_AUTO_TEST_CASE(mastercore_strtoint64_divisible_invalid)
6464
{
6565
// invalid, number is over max int64
66-
BOOST_CHECK(strToInt64("92233720368.54775808", true) == 0);
66+
BOOST_CHECK(StrToInt64("92233720368.54775808", true) == 0);
6767
// invalid, more than one decimal mark in string
68-
BOOST_CHECK(strToInt64("1234..12345678", true) == 0);
68+
BOOST_CHECK(StrToInt64("1234..12345678", true) == 0);
6969
// invalid, alpha chars in string
70-
BOOST_CHECK(strToInt64("1234.12345A", true) == 0);
70+
BOOST_CHECK(StrToInt64("1234.12345A", true) == 0);
7171
// invalid, number is negative
72-
BOOST_CHECK(strToInt64("-4.0", true) == 0);
72+
BOOST_CHECK(StrToInt64("-4.0", true) == 0);
7373
// invalid, minus sign in string
74-
BOOST_CHECK(strToInt64("4.1234-5678", true) == 0);
74+
BOOST_CHECK(StrToInt64("4.1234-5678", true) == 0);
7575
}
7676

7777
BOOST_AUTO_TEST_SUITE_END()

0 commit comments

Comments
 (0)