From b9066c6331100dbecad8ad80012ba0e98246717b Mon Sep 17 00:00:00 2001 From: Werner Almesberger Date: Wed, 23 May 2012 16:44:44 -0300 Subject: [PATCH] b2/util.c: new function unique_n for strings limited by length and not NUL --- b2/util.c | 18 ++++++++++++++++++ b2/util.h | 1 + 2 files changed, 19 insertions(+) diff --git a/b2/util.c b/b2/util.c index 8b40791..15701bd 100644 --- a/b2/util.c +++ b/b2/util.c @@ -38,3 +38,21 @@ const char *unique(const char *s) } return u; } + + +const char *unique_n(const char *s, int n) +{ + char *tmp, *u; + + if (!tree) + tree = g_tree_new(comp); + tmp = stralloc_n(s, n); + u = g_tree_lookup(tree, tmp); + if (u) { + free(tmp); + return u; + } else { + g_tree_insert(tree, tmp, tmp); + return tmp; + } +} diff --git a/b2/util.h b/b2/util.h index b8084d6..4ec841a 100644 --- a/b2/util.h +++ b/b2/util.h @@ -50,5 +50,6 @@ static inline char *stralloc_n(const char *s, int n) const char *unique(const char *s); +const char *unique_n(const char *s, int n); #endif /* !UTIL_H */