bool to_currency(Currency& currency, std::string const& code) { if (code.empty () || !code.compare (systemCurrencyCode())) { currency = beast::zero; return true; } static const int CURRENCY_CODE_LENGTH = 3; if (code.size () == CURRENCY_CODE_LENGTH) { Blob codeBlob (CURRENCY_CODE_LENGTH); std::transform (code.begin (), code.end (), codeBlob.begin (), [](auto c) { return ::toupper(static_cast<unsigned char>(c)); }); Serializer s; s.addZeros (96 / 8); s.addRaw (codeBlob); s.addZeros (16 / 8); s.addZeros (24 / 8); s.get160 (currency, 0); return true; } if (40 == code.size ()) return currency.SetHex (code); return false; }
std::string to_string(Currency const& currency) { static Currency const sIsoBits ("FFFFFFFFFFFFFFFFFFFFFFFF000000FFFFFFFFFF"); // Characters we are willing to allow in the ASCII representation of a // three-letter currency code. static std::string const allowed_characters = "abcdefghijklmnopqrstuvwxyz" "ABCDEFGHIJKLMNOPQRSTUVWXYZ" "0123456789" "<>(){}[]|?!@#$%^&*"; if (currency == zero) return systemCurrencyCode(); if (currency == vbcCurrency()) return systemCurrencyCodeVBC(); if (currency == noCurrency()) return "1"; if ((currency & sIsoBits).isZero ()) { // The offset of the 3 character ISO code in the currency descriptor int const isoOffset = 12; std::string const iso( currency.data () + isoOffset, currency.data () + isoOffset + 3); // Specifying the system currency code using ISO-style representation // is not allowed. if ((iso != systemCurrencyCode()) && (iso != systemCurrencyCodeVBC()) && (iso.find_first_not_of (allowed_characters) == std::string::npos)) { return iso; } } return strHex (currency.begin (), currency.size ()); }