]>
git.saurik.com Git - bison.git/blob - src/uniqstr.c
   1 /* Keep a unique copy of strings. 
   3    Copyright (C) 2002-2005, 2009-2010 Free Software Foundation, Inc. 
   5    This file is part of Bison, the GNU Compiler Compiler. 
   7    This program is free software: you can redistribute it and/or modify 
   8    it under the terms of the GNU General Public License as published by 
   9    the Free Software Foundation, either version 3 of the License, or 
  10    (at your option) any later version. 
  12    This program is distributed in the hope that it will be useful, 
  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. 
  17    You should have received a copy of the GNU General Public License 
  18    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */ 
  30 /*-----------------------. 
  31 | A uniqstr hash table.  | 
  32 `-----------------------*/ 
  34 /* Initial capacity of uniqstr hash table.  */ 
  35 #define HT_INITIAL_CAPACITY 257 
  37 static struct hash_table 
*uniqstrs_table 
= NULL
; 
  39 /*-------------------------------------. 
  40 | Create the uniqstr for S if needed.  | 
  41 `-------------------------------------*/ 
  44 uniqstr_new (char const *str
) 
  46   uniqstr res 
= hash_lookup (uniqstrs_table
, str
); 
  49       /* First insertion in the hash. */ 
  51       if (!hash_insert (uniqstrs_table
, res
)) 
  58 uniqstr_vsprintf (char const *format
, ...) 
  62   va_start (args
, format
); 
  63   length 
= vsnprintf (NULL
, 0, format
, args
); 
  67   va_start (args
, format
); 
  68   vsprintf (res
, format
, args
); 
  70   return uniqstr_new (res
); 
  73 /*------------------------------. 
  74 | Abort if S is not a uniqstr.  | 
  75 `------------------------------*/ 
  78 uniqstr_assert (char const *str
) 
  80   if (!hash_lookup (uniqstrs_table
, str
)) 
  83              "not a uniqstr: %s", quotearg (str
)); 
  89 /*--------------------. 
  90 | Print the uniqstr.  | 
  91 `--------------------*/ 
  94 uniqstr_print (uniqstr ustr
) 
  96   fprintf (stderr
, "%s\n", ustr
); 
 101 uniqstr_print_processor (void *ustr
, void *null ATTRIBUTE_UNUSED
) 
 103   return uniqstr_print (ustr
); 
 107 /*-----------------------. 
 108 | A uniqstr hash table.  | 
 109 `-----------------------*/ 
 112 hash_compare_uniqstr (void const *m1
, void const *m2
) 
 114   return strcmp (m1
, m2
) == 0; 
 118 hash_uniqstr (void const *m
, size_t tablesize
) 
 120   return hash_string (m
, tablesize
); 
 123 /*----------------------------. 
 124 | Create the uniqstrs table.  | 
 125 `----------------------------*/ 
 130   uniqstrs_table 
= hash_initialize (HT_INITIAL_CAPACITY
, 
 133                                     hash_compare_uniqstr
, 
 138 /*-------------------------------------. 
 139 | Perform a task on all the uniqstrs.  | 
 140 `-------------------------------------*/ 
 143 uniqstrs_do (Hash_processor processor
, void *processor_data
) 
 145   hash_do_for_each (uniqstrs_table
, processor
, processor_data
); 
 154 uniqstrs_print (void) 
 156   uniqstrs_do (uniqstr_print_processor
, NULL
); 
 160 /*--------------------. 
 161 | Free the uniqstrs.  | 
 162 `--------------------*/ 
 167   hash_free (uniqstrs_table
);