xrpld
Toggle main menu visibility
Loading...
Searching...
No Matches
libxrpl
protocol
UintTypes.cpp
1
#include <xrpl/protocol/UintTypes.h>
2
3
#include <xrpl/basics/strHex.h>
4
#include <xrpl/beast/utility/Zero.h>
5
#include <xrpl/protocol/SystemParameters.h>
6
7
#include <
algorithm
>
8
#include <
cstddef
>
9
#include <
string
>
10
#include <
string_view
>
11
12
namespace
xrpl
{
13
14
// For details on the protocol-level serialization please visit
15
// https://xrpl.org/serialization.html#currency-codes
16
17
namespace
detail
{
18
19
// Characters we are willing to allow in the ASCII representation of a
20
// three-letter currency code.
21
constexpr
std::string_view
kIsoCharSet
=
22
"abcdefghijklmnopqrstuvwxyz"
23
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
24
"0123456789"
25
"<>(){}[]|?!@#$%^&*"
;
26
27
// The location (in bytes) of the 3 digit currency inside a 160-bit value
28
constexpr
std::size_t
kIsoCodeOffset
= 12;
29
30
// The length of an ISO-4217 like code
31
constexpr
std::size_t
kIsoCodeLength
= 3;
32
33
}
// namespace detail
34
35
std::string
36
to_string
(
Currency
const
& currency)
37
{
38
if
(currency ==
beast::kZero
)
39
return
systemCurrencyCode
();
40
41
if
(currency ==
noCurrency
())
42
return
"1"
;
43
44
static
constexpr
Currency
kSIsoBits(
"FFFFFFFFFFFFFFFFFFFFFFFF000000FFFFFFFFFF"
);
45
46
if
((currency & kSIsoBits).isZero())
47
{
48
std::string
const
iso(
49
currency.
data
() +
detail::kIsoCodeOffset
,
50
currency.
data
() +
detail::kIsoCodeOffset
+
detail::kIsoCodeLength
);
51
52
// Specifying the system currency code using ISO-style representation
53
// is not allowed.
54
if
((iso !=
systemCurrencyCode
()) &&
55
(iso.
find_first_not_of
(
detail::kIsoCharSet
) == std::string::npos))
56
{
57
return
iso;
58
}
59
}
60
61
return
strHex
(currency);
62
}
63
64
bool
65
toCurrency
(
Currency
& currency,
std::string
const
& code)
66
{
67
if
(code.
empty
() || code ==
systemCurrencyCode
())
68
{
69
currency =
beast::kZero
;
70
return
true
;
71
}
72
73
// Handle ISO-4217-like 3-digit character codes.
74
if
(code.
size
() ==
detail::kIsoCodeLength
)
75
{
76
if
(code.
find_first_not_of
(
detail::kIsoCharSet
) != std::string::npos)
77
return
false
;
78
79
currency =
beast::kZero
;
80
81
std::ranges::copy
(code, currency.
begin
() +
detail::kIsoCodeOffset
);
82
83
return
true
;
84
}
85
86
return
currency.
parseHex
(code);
87
}
88
89
Currency
90
toCurrency
(
std::string
const
& code)
91
{
92
Currency
currency;
93
if
(!
toCurrency
(currency, code))
94
currency =
noCurrency
();
95
return
currency;
96
}
97
98
Currency
const
&
99
xrpCurrency
()
100
{
101
static
Currency
const
kCurrency(
beast::kZero
);
102
return
kCurrency;
103
}
104
105
Currency
const
&
106
noCurrency
()
107
{
108
static
Currency
const
kCurrency(1);
109
return
kCurrency;
110
}
111
112
Currency
const
&
113
badCurrency
()
114
{
115
static
Currency
const
kCurrency(0x5852500000000000);
116
return
kCurrency;
117
}
118
119
}
// namespace xrpl
algorithm
std::string
std::string_view
xrpl::BaseUInt::data
pointer data()
Definition
base_uint.h:117
xrpl::BaseUInt::begin
iterator begin()
Definition
base_uint.h:128
xrpl::BaseUInt::parseHex
constexpr bool parseHex(std::string_view sv)
Parse a hex string into a base_uint.
Definition
base_uint.h:525
std::ranges::copy
T copy(T... args)
cstddef
std::string::empty
T empty(T... args)
std::string::find_first_not_of
T find_first_not_of(T... args)
beast::kZero
constexpr Zero kZero
Definition
Zero.h:30
xrpl::detail
Definition
base_uint.h:37
xrpl::detail::kIsoCodeLength
constexpr std::size_t kIsoCodeLength
Definition
UintTypes.cpp:31
xrpl::detail::kIsoCodeOffset
constexpr std::size_t kIsoCodeOffset
Definition
UintTypes.cpp:28
xrpl::detail::kIsoCharSet
constexpr std::string_view kIsoCharSet
Definition
UintTypes.cpp:21
xrpl
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition
algorithm.h:5
xrpl::strHex
std::string strHex(FwdIt begin, FwdIt end)
Definition
strHex.h:13
xrpl::Currency
BaseUInt< 160, detail::CurrencyTag > Currency
Currency is a hash representing a specific currency.
Definition
UintTypes.h:42
xrpl::toCurrency
bool toCurrency(Currency &, std::string const &)
Tries to convert a string to a Currency, returns true on success.
Definition
UintTypes.cpp:65
xrpl::xrpCurrency
Currency const & xrpCurrency()
XRP currency.
Definition
UintTypes.cpp:99
xrpl::to_string
std::string to_string(BaseUInt< Bits, Tag > const &a)
Definition
base_uint.h:657
xrpl::noCurrency
Currency const & noCurrency()
A placeholder for empty currencies.
Definition
UintTypes.cpp:106
xrpl::systemCurrencyCode
static std::string const & systemCurrencyCode()
Definition
SystemParameters.h:56
xrpl::badCurrency
Currency const & badCurrency()
We deliberately disallow the currency that looks like "XRP" because too many people were using it ins...
Definition
UintTypes.cpp:113
std::string::size
T size(T... args)
std::size_t
string
string_view
Generated by
1.17.0