]> git.saurik.com Git - bison.git/blame - src/uniqstr.c
gnulib: update
[bison.git] / src / uniqstr.c
CommitLineData
ac72f3e9
PE
1/* Keep a unique copy of strings.
2
3209eb1c 3 Copyright (C) 2002-2005, 2009-2015 Free Software Foundation, Inc.
5bab03fc
PE
4
5 This file is part of Bison, the GNU Compiler Compiler.
6
f16b0819 7 This program is free software: you can redistribute it and/or modify
5bab03fc 8 it under the terms of the GNU General Public License as published by
f16b0819
PE
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
5bab03fc 11
f16b0819 12 This program is distributed in the hope that it will be useful,
5bab03fc
PE
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
f16b0819 18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
5bab03fc 19
2cec9080 20#include <config.h>
5bab03fc
PE
21#include "system.h"
22
23#include <error.h>
24#include <hash.h>
25#include <quotearg.h>
10659d0e 26#include <stdarg.h>
5bab03fc
PE
27
28#include "uniqstr.h"
29
30/*-----------------------.
31| A uniqstr hash table. |
32`-----------------------*/
33
34/* Initial capacity of uniqstr hash table. */
35#define HT_INITIAL_CAPACITY 257
36
37static struct hash_table *uniqstrs_table = NULL;
38
39/*-------------------------------------.
40| Create the uniqstr for S if needed. |
41`-------------------------------------*/
42
43uniqstr
a737b216 44uniqstr_new (char const *str)
5bab03fc 45{
a737b216 46 uniqstr res = hash_lookup (uniqstrs_table, str);
5bab03fc
PE
47 if (!res)
48 {
49 /* First insertion in the hash. */
a737b216 50 res = xstrdup (str);
b5c212b6
AD
51 if (!hash_insert (uniqstrs_table, res))
52 xalloc_die ();
5bab03fc
PE
53 }
54 return res;
55}
56
10659d0e
JD
57uniqstr
58uniqstr_vsprintf (char const *format, ...)
59{
60 va_list args;
61 size_t length;
62 va_start (args, format);
63 length = vsnprintf (NULL, 0, format, args);
64 va_end (args);
65
66 char res[length + 1];
67 va_start (args, format);
68 vsprintf (res, format, args);
69 va_end (args);
70 return uniqstr_new (res);
71}
5bab03fc
PE
72
73/*------------------------------.
74| Abort if S is not a uniqstr. |
75`------------------------------*/
76
77void
a737b216 78uniqstr_assert (char const *str)
5bab03fc 79{
1a9646fc
AD
80 uniqstr s = hash_lookup (uniqstrs_table, str);
81 if (!s || s != str)
5bab03fc 82 {
04098407 83 error (0, 0,
e9690142 84 "not a uniqstr: %s", quotearg (str));
5bab03fc
PE
85 abort ();
86 }
87}
88
89
90/*--------------------.
91| Print the uniqstr. |
92`--------------------*/
93
ac72f3e9 94static inline bool
a737b216 95uniqstr_print (uniqstr ustr)
5bab03fc 96{
a737b216 97 fprintf (stderr, "%s\n", ustr);
5bab03fc
PE
98 return true;
99}
100
ac72f3e9 101static bool
a737b216 102uniqstr_print_processor (void *ustr, void *null ATTRIBUTE_UNUSED)
ac72f3e9 103{
a737b216 104 return uniqstr_print (ustr);
ac72f3e9
PE
105}
106
55439a1c
AD
107
108int
c0ef22ab 109uniqstr_cmp (uniqstr l, uniqstr r)
55439a1c
AD
110{
111 return (l == r ? 0
112 : !l ? -1
113 : !r ? +1
114 : strcmp (l, r));
115}
116
117
5bab03fc
PE
118/*-----------------------.
119| A uniqstr hash table. |
120`-----------------------*/
121
122static bool
ac72f3e9 123hash_compare_uniqstr (void const *m1, void const *m2)
5bab03fc 124{
f518dbaf 125 return STREQ (m1, m2);
5bab03fc
PE
126}
127
233a88ad
PE
128static size_t
129hash_uniqstr (void const *m, size_t tablesize)
5bab03fc
PE
130{
131 return hash_string (m, tablesize);
132}
133
55439a1c 134
5bab03fc
PE
135/*----------------------------.
136| Create the uniqstrs table. |
137`----------------------------*/
138
139void
140uniqstrs_new (void)
141{
142 uniqstrs_table = hash_initialize (HT_INITIAL_CAPACITY,
e9690142
JD
143 NULL,
144 hash_uniqstr,
145 hash_compare_uniqstr,
146 free);
5bab03fc
PE
147}
148
149
150/*-------------------------------------.
151| Perform a task on all the uniqstrs. |
152`-------------------------------------*/
153
154static void
ac72f3e9 155uniqstrs_do (Hash_processor processor, void *processor_data)
5bab03fc 156{
ac72f3e9 157 hash_do_for_each (uniqstrs_table, processor, processor_data);
5bab03fc
PE
158}
159
160
161/*-----------------.
162| Print them all. |
163`-----------------*/
164
165void
166uniqstrs_print (void)
167{
ac72f3e9 168 uniqstrs_do (uniqstr_print_processor, NULL);
5bab03fc
PE
169}
170
171
172/*--------------------.
173| Free the uniqstrs. |
174`--------------------*/
175
176void
177uniqstrs_free (void)
178{
179 hash_free (uniqstrs_table);
180}