From ca4b07c529b75d36b940594e81f2f14f240cdc20 Mon Sep 17 00:00:00 2001 From: darosior Date: Tue, 11 Jun 2019 10:55:45 +0200 Subject: [PATCH] json: add a helper to split a json token given a specific character credits @rustyrussell --- common/json_helpers.c | 16 ++++++++++++++++ common/json_helpers.h | 6 ++++++ 2 files changed, 22 insertions(+) diff --git a/common/json_helpers.c b/common/json_helpers.c index 20cd345ae..c99e3f1f4 100644 --- a/common/json_helpers.c +++ b/common/json_helpers.c @@ -75,3 +75,19 @@ bool json_to_txid(const char *buffer, const jsmntok_t *tok, return bitcoin_txid_from_hex(buffer + tok->start, tok->end - tok->start, txid); } + +bool split_tok(const char *buffer, const jsmntok_t *tok, + char split, + jsmntok_t *a, + jsmntok_t *b) +{ + const char *p = memchr(buffer + tok->start, split, tok->end - tok->start); + if (!p) + return false; + + *a = *b = *tok; + a->end = p - buffer; + b->start = p + 1 - buffer; + + return true; +} diff --git a/common/json_helpers.h b/common/json_helpers.h index 03286469d..9872af9a3 100644 --- a/common/json_helpers.h +++ b/common/json_helpers.h @@ -39,4 +39,10 @@ bool json_to_msat(const char *buffer, const jsmntok_t *tok, /* Extract a bitcoin txid from this */ bool json_to_txid(const char *buffer, const jsmntok_t *tok, struct bitcoin_txid *txid); + +/* Split a json token into 2 tokens given a splitting character */ +bool split_tok(const char *buffer, const jsmntok_t *tok, + char split, + jsmntok_t *a, + jsmntok_t *b); #endif /* LIGHTNING_COMMON_JSON_HELPERS_H */