vis

a vi-like editor based on Plan 9's structural regular expressions

git clone https://9o.is/git/vis.git

commit cc2fd27b88a22dd31ea13f644160df4b8f28d477
parent 72a801898acdc01ff1a9189c29ee1fed4d50b269
Author: Marc André Tanner <mat@brain-dump.org>
Date:   Thu, 15 Dec 2016 20:21:27 +0100

map: add map_leaf utility function

Tests whether the given prefix can be extended to exactly one map element
i.e. true iff the prefix map contains exactly one element.

Diffstat:
Mmap.c | 13+++++++++++++
Mmap.h | 3+++
2 files changed, 16 insertions(+), 0 deletions(-)

diff --git a/map.c b/map.c @@ -78,6 +78,19 @@ bool map_contains(const Map *map, const char *prefix) return !map_empty(map_prefix(map, prefix)); } +static bool leaf(const char *key, void *value, void *data) +{ + int *nodes = data; + return (*nodes)++ < 1; +} + +bool map_leaf(const Map *map, const char *prefix) +{ + int nodes = 0; + map_iterate(map_prefix(map, prefix), leaf, &nodes); + return nodes == 1; +} + bool map_put(Map *map, const char *k, const void *value) { size_t len = strlen(k); diff --git a/map.h b/map.h @@ -17,6 +17,9 @@ void *map_closest(const Map*, const char *prefix); /* check whether the map contains the given prefix, i.e. whether it can * be extended to match a key of an element stored in the map. */ bool map_contains(const Map*, const char *prefix); +/* Test whether the given prefix can be extended to exactly one map element + * i.e. true iff the prefix map contains exactly one element. */ +bool map_leaf(const Map*, const char *prefix); /* Place a member in the map. This returns false if we run out of memory * (errno = ENOMEM), or if that key already appears in the map (errno = EEXIST). */ bool map_put(Map*, const char *key, const void *value);