common/iso4217: make find_iso4217 a little more usable.

We often have the currency as a tal string.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell 2020-12-12 09:26:55 +10:30 committed by Christian Decker
parent d971e3de98
commit 0f2c9cf5d5
3 changed files with 14 additions and 10 deletions

View file

@ -1,6 +1,6 @@
#include <ccan/array_size/array_size.h> #include <ccan/array_size/array_size.h>
#include <ccan/mem/mem.h>
#include <common/iso4217.h> #include <common/iso4217.h>
#include <string.h>
/* Wikipedia leads me to: https://www.currency-iso.org/en/home/tables/table-a1.html /* Wikipedia leads me to: https://www.currency-iso.org/en/home/tables/table-a1.html
@ -191,10 +191,12 @@ static const struct iso4217_name_and_divisor iso4217[] = {
{ "ZWL", 2 }, { "ZWL", 2 },
}; };
const struct iso4217_name_and_divisor *find_iso4217(const char *prefix) const struct iso4217_name_and_divisor *find_iso4217(const utf8 *prefix,
size_t len)
{ {
for (size_t i = 0; i < ARRAY_SIZE(iso4217); i++) { for (size_t i = 0; i < ARRAY_SIZE(iso4217); i++) {
if (memcmp(iso4217[i].name, prefix, ISO4217_NAMELEN) == 0) if (memeq(iso4217[i].name, strlen(iso4217[i].name),
prefix, len))
return &iso4217[i]; return &iso4217[i];
} }
return NULL; return NULL;

View file

@ -1,6 +1,7 @@
#ifndef LIGHTNING_COMMON_ISO4217_H #ifndef LIGHTNING_COMMON_ISO4217_H
#define LIGHTNING_COMMON_ISO4217_H #define LIGHTNING_COMMON_ISO4217_H
#include "config.h" #include "config.h"
#include <wire/wire.h>
/* BOLT-offers #12: /* BOLT-offers #12:
* *
@ -15,5 +16,6 @@ struct iso4217_name_and_divisor {
#define ISO4217_NAMELEN 3 #define ISO4217_NAMELEN 3
const struct iso4217_name_and_divisor *find_iso4217(const char *prefix); const struct iso4217_name_and_divisor *find_iso4217(const utf8 *prefix,
size_t len);
#endif /* LIGHTNING_COMMON_ISO4217_H */ #endif /* LIGHTNING_COMMON_ISO4217_H */

View file

@ -112,14 +112,14 @@ static bool print_amount(const struct bitcoin_blkid *chains,
minor_unit = 11; minor_unit = 11;
} else { } else {
const struct iso4217_name_and_divisor *iso; const struct iso4217_name_and_divisor *iso;
currency = tal_strndup(tmpctx, iso4217, tal_bytelen(iso4217)); iso = find_iso4217(iso4217, tal_bytelen(iso4217));
iso = find_iso4217(currency); if (iso) {
if (iso)
minor_unit = iso->minor_unit; minor_unit = iso->minor_unit;
else { currency = iso->name;
} else {
minor_unit = 0; minor_unit = 0;
currency = tal_fmt(tmpctx, "%s (UNKNOWN CURRENCY)", currency = tal_fmt(tmpctx, "%.*s (UNKNOWN CURRENCY)",
currency); (int)tal_bytelen(iso4217), iso4217);
ok = false; ok = false;
} }
} }