Changeset View
Changeset View
Standalone View
Standalone View
src/utilstrencodings.h
Show First 20 Lines • Show All 136 Lines • ▼ Show 20 Lines | |||||
* Parse number as fixed point according to JSON number syntax. | * Parse number as fixed point according to JSON number syntax. | ||||
* See http://json.org/number.gif | * See http://json.org/number.gif | ||||
* @returns true on success, false on error. | * @returns true on success, false on error. | ||||
* @note The result must be in the range (-10^18,10^18), otherwise an overflow | * @note The result must be in the range (-10^18,10^18), otherwise an overflow | ||||
* error will trigger. | * error will trigger. | ||||
*/ | */ | ||||
bool ParseFixedPoint(const std::string &val, int decimals, int64_t *amount_out); | bool ParseFixedPoint(const std::string &val, int decimals, int64_t *amount_out); | ||||
/** Convert from one power-of-2 number base to another. */ | |||||
template <int frombits, int tobits, bool pad, typename O, typename I> | |||||
bool ConvertBits(O &out, I it, I end) { | |||||
size_t acc = 0; | |||||
size_t bits = 0; | |||||
constexpr size_t maxv = (1 << tobits) - 1; | |||||
constexpr size_t max_acc = (1 << (frombits + tobits - 1)) - 1; | |||||
while (it != end) { | |||||
acc = ((acc << frombits) | *it) & max_acc; | |||||
bits += frombits; | |||||
while (bits >= tobits) { | |||||
bits -= tobits; | |||||
out.push_back((acc >> bits) & maxv); | |||||
} | |||||
++it; | |||||
} | |||||
if (pad) { | |||||
if (bits) out.push_back((acc << (tobits - bits)) & maxv); | |||||
} else if (bits >= frombits || ((acc << (tobits - bits)) & maxv)) { | |||||
return false; | |||||
} | |||||
return true; | |||||
} | |||||
#endif // BITCOIN_UTILSTRENCODINGS_H | #endif // BITCOIN_UTILSTRENCODINGS_H |