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 */