aboutsummaryrefslogtreecommitdiff
path: root/src/rpc/util.cpp
diff options
context:
space:
mode:
authorPieter Wuille <pieter.wuille@gmail.com>2019-02-27 13:41:41 -0800
committerPieter Wuille <pieter.wuille@gmail.com>2019-02-28 13:00:09 -0800
commit7aa6a8aefbb2013ef3dc87ecbdf5d947d4b413af (patch)
treef0eec090a9f4d18a90e5508a24e1df53bca3e1de /src/rpc/util.cpp
parent29c24b05fb71a5577af9f58298493c1f2d489069 (diff)
downloadbitcoin-7aa6a8aefbb2013ef3dc87ecbdf5d947d4b413af.tar.xz
Add ParseRange function to parse args of the form int/[int,int]
Diffstat (limited to 'src/rpc/util.cpp')
-rw-r--r--src/rpc/util.cpp14
1 files changed, 14 insertions, 0 deletions
diff --git a/src/rpc/util.cpp b/src/rpc/util.cpp
index 86695bc1a5..7fb139f93c 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<int64_t, int64_t> 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]");
+}