]>
git.saurik.com Git - bison.git/blob - src/uniqstr.c
1 /* Keep a unique copy of strings.
3 Copyright (C) 2002, 2003, 2004, 2005, 2009, 2010 Free Software
6 This file is part of Bison, the GNU Compiler Compiler.
8 This program is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
31 /*-----------------------.
32 | A uniqstr hash table. |
33 `-----------------------*/
35 /* Initial capacity of uniqstr hash table. */
36 #define HT_INITIAL_CAPACITY 257
38 static struct hash_table
*uniqstrs_table
= NULL
;
40 /*-------------------------------------.
41 | Create the uniqstr for S if needed. |
42 `-------------------------------------*/
45 uniqstr_new (char const *str
)
47 uniqstr res
= hash_lookup (uniqstrs_table
, str
);
50 /* First insertion in the hash. */
52 if (!hash_insert (uniqstrs_table
, res
))
59 uniqstr_vsprintf (char const *format
, ...)
63 va_start (args
, format
);
64 length
= vsnprintf (NULL
, 0, format
, args
);
68 va_start (args
, format
);
69 vsprintf (res
, format
, args
);
71 return uniqstr_new (res
);
74 /*------------------------------.
75 | Abort if S is not a uniqstr. |
76 `------------------------------*/
79 uniqstr_assert (char const *str
)
81 if (!hash_lookup (uniqstrs_table
, str
))
84 "not a uniqstr: %s", quotearg (str
));
90 /*--------------------.
91 | Print the uniqstr. |
92 `--------------------*/
95 uniqstr_print (uniqstr ustr
)
97 fprintf (stderr
, "%s\n", ustr
);
102 uniqstr_print_processor (void *ustr
, void *null ATTRIBUTE_UNUSED
)
104 return uniqstr_print (ustr
);
108 /*-----------------------.
109 | A uniqstr hash table. |
110 `-----------------------*/
113 hash_compare_uniqstr (void const *m1
, void const *m2
)
115 return strcmp (m1
, m2
) == 0;
119 hash_uniqstr (void const *m
, size_t tablesize
)
121 return hash_string (m
, tablesize
);
124 /*----------------------------.
125 | Create the uniqstrs table. |
126 `----------------------------*/
131 uniqstrs_table
= hash_initialize (HT_INITIAL_CAPACITY
,
134 hash_compare_uniqstr
,
139 /*-------------------------------------.
140 | Perform a task on all the uniqstrs. |
141 `-------------------------------------*/
144 uniqstrs_do (Hash_processor processor
, void *processor_data
)
146 hash_do_for_each (uniqstrs_table
, processor
, processor_data
);
155 uniqstrs_print (void)
157 uniqstrs_do (uniqstr_print_processor
, NULL
);
161 /*--------------------.
162 | Free the uniqstrs. |
163 `--------------------*/
168 hash_free (uniqstrs_table
);