@@ -371,21 +371,27 @@ STAmount operator- (STAmount const& v1, STAmount const& v2)
371
371
372
372
// ------------------------------------------------------------------------------
373
373
374
- std::uint64_t STAmount::uRateOne =
375
- getRate (STAmount (1 ), STAmount (1 ));
374
+ std::uint64_t const STAmount::uRateOne = getRate (STAmount (1 ), STAmount (1 ));
376
375
377
376
// Note: mIsNative and mIssue.currency must be set already!
378
377
bool
379
- STAmount::setValue (std::string const & sAmount )
378
+ STAmount::setValue (std::string const & amount )
380
379
{
381
- static boost::regex reNumber (
382
- " \\ `([+-]?)(\\ d*)(\\ .(\\ d*))?([eE]([+-]?)(\\ d+))?\\ '" );
383
- boost::smatch smMatch;
380
+ static boost::regex const reNumber (
381
+ " ^" // the beginning of the string
382
+ " ([-+]?)" // (optional) + or - character
383
+ " (0|[1-9][0-9]*)" // a number (no leading zeroes, unless 0)
384
+ " (\\ .([0-9]+))?" // (optional) period followed by any number
385
+ " ([eE]([+-]?)([0-9]+))?" // (optional) E, optional + or -, any number
386
+ " $" ,
387
+ boost::regex_constants::optimize);
384
388
385
- if (!boost::regex_match (sAmount , smMatch, reNumber))
389
+ boost::smatch match;
390
+
391
+ if (!boost::regex_match (amount, match, reNumber))
386
392
{
387
- WriteLog (lsWARNING, STAmount)
388
- << " Number not valid: \" " << sAmount << " \" " ;
393
+ WriteLog (lsWARNING, STAmount) <<
394
+ " Number not valid: \" " << amount << " \" " ;
389
395
return false ;
390
396
}
391
397
@@ -401,113 +407,56 @@ STAmount::setValue (std::string const& sAmount)
401
407
402
408
try
403
409
{
404
- if ((smMatch[2 ].length () + smMatch[4 ].length ()) > 32 )
410
+ // CHECKME: Why 32? Shouldn't this be 16?
411
+ if ((match[2 ].length () + match[4 ].length ()) > 32 )
405
412
{
406
- WriteLog (lsWARNING, STAmount) << " Overlong number: " << sAmount ;
413
+ WriteLog (lsWARNING, STAmount) << " Overlong number: " << amount ;
407
414
return false ;
408
415
}
409
416
410
- mIsNegative = (smMatch[1 ].matched && (smMatch[1 ] == " -" ));
417
+ mIsNegative = (match[1 ].matched && (match[1 ] == " -" ));
418
+
419
+ // Can't specify XRP using fractional representation
420
+ if (mIsNative && match[3 ].matched )
421
+ return false ;
411
422
412
- if (!smMatch [4 ].matched ) // integer only
423
+ if (!match [4 ].matched ) // integer only
413
424
{
414
- mValue = beast::lexicalCast <std::uint64_t > (std::string (smMatch [2 ]));
425
+ mValue = beast::lexicalCastThrow <std::uint64_t > (std::string (match [2 ]));
415
426
mOffset = 0 ;
416
427
}
417
428
else
418
429
{
419
430
// integer and fraction
420
- mValue = beast::lexicalCast <std::uint64_t > (smMatch [2 ] + smMatch [4 ]);
421
- mOffset = - (smMatch [4 ].length ());
431
+ mValue = beast::lexicalCastThrow <std::uint64_t > (match [2 ] + match [4 ]);
432
+ mOffset = -(match [4 ].length ());
422
433
}
423
434
424
- if (smMatch [5 ].matched )
435
+ if (match [5 ].matched )
425
436
{
426
437
// we have an exponent
427
- if (smMatch [6 ].matched && (smMatch [6 ] == " -" ))
428
- mOffset -= beast::lexicalCast <int > (std::string (smMatch [7 ]));
438
+ if (match [6 ].matched && (match [6 ] == " -" ))
439
+ mOffset -= beast::lexicalCastThrow <int > (std::string (match [7 ]));
429
440
else
430
- mOffset += beast::lexicalCast <int > (std::string (smMatch [7 ]));
441
+ mOffset += beast::lexicalCastThrow <int > (std::string (match [7 ]));
431
442
}
432
- }
433
- catch (...)
434
- {
435
- WriteLog (lsWARNING, STAmount) << " Number not parsed: \" " << sAmount << " \" " ;
436
- return false ;
437
- }
438
-
439
- WriteLog (lsTRACE, STAmount) << " Float \" " << sAmount << " \" parsed to " << mValue << " : " << mOffset ;
440
-
441
- if (mIsNative )
442
- {
443
- if (smMatch[3 ].matched )
444
- mOffset -= SYSTEM_CURRENCY_PRECISION;
445
443
446
- while (mOffset > 0 )
447
- {
448
- mValue *= 10 ;
449
- --mOffset ;
450
- }
451
-
452
- while (mOffset < 0 )
453
- {
454
- mValue /= 10 ;
455
- ++mOffset ;
456
- }
457
- }
458
- else
459
444
canonicalize ();
460
445
461
- return true ;
462
- }
463
-
464
- // Not meant to be the ultimate parser. For use by RPC which is supposed to be sane and trusted.
465
- // Native has special handling:
466
- // - Integer values are in base units.
467
- // - Float values are in float units.
468
- // - To avoid a mistake float value for native are specified with a "^" in place of a "."
469
- // <-- bValid: true = valid
470
- bool STAmount::setFullValue (std::string const & sAmount , std::string const & sCurrency , std::string const & sIssuer )
471
- {
472
- //
473
- // Figure out the currency.
474
- //
475
- if (!to_currency (mIssue .currency , sCurrency ))
476
- {
477
- WriteLog (lsINFO, STAmount) << " Currency malformed: " << sCurrency ;
478
-
479
- return false ;
480
- }
481
-
482
- mIsNative = !mIssue .currency ;
483
-
484
- //
485
- // Figure out the issuer.
486
- //
487
- RippleAddress naIssuerID;
488
-
489
- // Issuer must be "" or a valid account string.
490
- if (!naIssuerID.setAccountID (sIssuer ))
491
- {
492
- WriteLog (lsINFO, STAmount) << " Issuer malformed: " << sIssuer ;
493
-
494
- return false ;
446
+ WriteLog (lsTRACE, STAmount) <<
447
+ " Canonicalized \" " << amount << " \" to " << mValue << " : " << mOffset ;
495
448
}
496
-
497
- mIssue .account = naIssuerID.getAccountID ();
498
-
499
- // Stamps not must have an issuer.
500
- if (mIsNative && !isXRP (*this ))
449
+ catch (...)
501
450
{
502
- WriteLog (lsINFO, STAmount) << " Issuer specified for XRP: " << sIssuer ;
503
-
504
451
return false ;
505
452
}
506
453
507
- return setValue ( sAmount ) ;
454
+ return true ;
508
455
}
509
456
510
- void STAmount::setIssue (Issue const & issue) {
457
+ void
458
+ STAmount::setIssue (Issue const & issue)
459
+ {
511
460
mIssue = std::move (issue);
512
461
mIsNative = isXRP (*this );
513
462
}
@@ -836,7 +785,7 @@ void STAmount::canonicalize ()
836
785
--mOffset ;
837
786
}
838
787
839
- if (mValue > cMaxNative )
788
+ if (mValue > cMaxNativeN )
840
789
throw std::runtime_error (" Native currency amount out of range" );
841
790
842
791
return ;
@@ -870,7 +819,6 @@ void STAmount::canonicalize ()
870
819
{
871
820
mValue = 0 ;
872
821
mOffset = 0 ;
873
- mIsNegative = false ;
874
822
}
875
823
876
824
if (mOffset > cMaxOffset)
0 commit comments