From 7aa6a8aefbb2013ef3dc87ecbdf5d947d4b413af Mon Sep 17 00:00:00 2001 From: Pieter Wuille Date: Wed, 27 Feb 2019 13:41:41 -0800 Subject: [PATCH] Add ParseRange function to parse args of the form int/[int,int] --- src/rpc/util.cpp | 14 ++++++++++++++ src/rpc/util.h | 3 +++ 2 files changed, 17 insertions(+) diff --git a/src/rpc/util.cpp b/src/rpc/util.cpp index 86695bc1a5fc4..7fb139f93c05f 100644 --- a/src/rpc/util.cpp +++ b/src/rpc/util.cpp @@ -523,3 +523,17 @@ std::string RPCArg::ToString(const bool oneline) const } assert(false); } + +std::pair ParseRange(const UniValue& value) +{ + if (value.isNum()) { + return {0, value.get_int64()}; + } + if (value.isArray() && value.size() == 2 && value[0].isNum() && value[1].isNum()) { + int64_t low = value[0].get_int64(); + int64_t high = value[1].get_int64(); + if (low > high) throw JSONRPCError(RPC_INVALID_PARAMETER, "Range specified as [begin,end] must not have begin after end"); + return {low, high}; + } + throw JSONRPCError(RPC_INVALID_PARAMETER, "Range must be specified as end or as [begin,end]"); +} diff --git a/src/rpc/util.h b/src/rpc/util.h index 06800ad63cbd7..f11998bafaa3b 100644 --- a/src/rpc/util.h +++ b/src/rpc/util.h @@ -38,6 +38,9 @@ unsigned int ParseConfirmTarget(const UniValue& value); RPCErrorCode RPCErrorFromTransactionError(TransactionError terr); UniValue JSONRPCTransactionError(TransactionError terr, const std::string& err_string = ""); +//! Parse a JSON range specified as int64, or [int64, int64] +std::pair ParseRange(const UniValue& value); + struct RPCArg { enum class Type { OBJ,