1 /* classes: src_files */
3 /* Copyright (C) 1995, 1996 Tom Lord
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU Library General Public License as published by
7 * the Free Software Foundation; either version 2, or (at your option)
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public License
16 * along with this software; see the file COPYING. If not, write to
17 * the Free Software Foundation, 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
33 rx_init_string(struct rx_string *thisone, char first)
36 rx_init_string(thisone, first)
37 struct rx_string *thisone;
43 tmp = (char *) malloc (INITSIZE);
48 thisone->contents = tmp;
49 thisone->contents[0] = first;
50 thisone->reallen = INITSIZE;
57 rx_free_string (struct rx_string *junk)
61 struct rx_string *junk;
64 free (junk->contents);
65 junk->len = junk->reallen = 0;
72 rx_adjoin_string (struct rx_string *str, char c)
75 rx_adjoin_string (str, c)
76 struct rx_string *str;
81 return rx_init_string (str, c);
83 if (str->len == str->reallen)
86 temp = (char *) realloc (str->contents, str->reallen + EXPANDSIZE);
92 str->reallen += EXPANDSIZE;
95 str->contents[str->len++] = c;
102 rx_copy_string (struct rx_string *to, struct rx_string *from)
105 rx_copy_string (to, from)
106 struct rx_string *to;
107 struct rx_string *from;
114 tmp = (char *) malloc (from->reallen);
122 to->reallen = from->reallen;
125 memcpy (to->contents, from->contents, from->reallen);
133 rx_compare_rx_strings (struct rx_string *a, struct rx_string *b)
136 rx_compare_rx_strings (a, b)
141 if (a->len != b->len)
145 return !memcmp (a->contents, b->contents, a->len);
147 return 1; /* trivial case: "" == "" */
153 rx_string_hash (unsigned long seed, struct rx_string *str)
156 rx_string_hash (seed, str)
158 struct rx_string *str;
162 unsigned long result;
167 string = str->contents;
175 result += (result<<3) + c;
194 n = (struct rexp_node *) malloc (sizeof (*n));
195 rx_bzero ((char *)n, sizeof (*n));
206 /* free_rexp_node assumes that the bitset passed to rx_mk_r_cset
207 * can be freed using rx_free_cset.
212 rx_mk_r_cset (int type, int size, rx_Bitset b)
215 rx_mk_r_cset (type, size, b)
221 struct rexp_node * n;
222 n = rexp_node (type);
226 n->params.cset_size = size;
234 rx_mk_r_int (int type, int intval)
237 rx_mk_r_int (type, intval)
242 struct rexp_node * n;
243 n = rexp_node (type);
245 n->params.intval = intval;
252 rx_mk_r_str (int type, char c)
255 rx_mk_r_str (type, c)
261 n = rexp_node (type);
263 rx_init_string (&(n->params.cstr), c);
270 rx_mk_r_binop (int type, struct rexp_node * a, struct rexp_node * b)
273 rx_mk_r_binop (type, a, b)
275 struct rexp_node * a;
276 struct rexp_node * b;
279 struct rexp_node * n = rexp_node (type);
282 n->params.pair.left = a;
283 n->params.pair.right = b;
291 rx_mk_r_monop (int type, struct rexp_node * a)
294 rx_mk_r_monop (type, a)
296 struct rexp_node * a;
299 return rx_mk_r_binop (type, a, 0);
305 rx_free_rexp (struct rexp_node * node)
309 struct rexp_node * node;
312 if (node && !--node->refs)
314 if (node->params.cset)
315 rx_free_cset (node->params.cset);
316 if (node->params.cstr.reallen)
317 rx_free_string (&(node->params.cstr));
318 rx_free_rexp (node->params.pair.left);
319 rx_free_rexp (node->params.pair.right);
320 rx_free_rexp (node->simplified);
327 rx_save_rexp (struct rexp_node * node)
331 struct rexp_node * node;
341 rx_copy_rexp (int cset_size, struct rexp_node *node)
344 rx_copy_rexp (cset_size, node)
346 struct rexp_node *node;
354 n = rexp_node (node->type);
358 if (node->params.cset)
360 n->params.cset = rx_copy_cset (cset_size,
369 if (node->params.cstr.reallen)
370 if (rx_copy_string (&(n->params.cstr), &(node->params.cstr)))
376 n->params.intval = node->params.intval;
377 n->params.intval2 = node->params.intval2;
378 n->params.pair.left = rx_copy_rexp (cset_size, node->params.pair.left);
379 n->params.pair.right = rx_copy_rexp (cset_size, node->params.pair.right);
380 if ( (node->params.pair.left && !n->params.pair.left)
381 || (node->params.pair.right && !n->params.pair.right))
388 n->observed = node->observed;
397 rx_shallow_copy_rexp (int cset_size, struct rexp_node *node)
400 rx_shallow_copy_rexp (cset_size, node)
402 struct rexp_node *node;
410 n = rexp_node (node->type);
414 if (node->params.cset)
416 n->params.cset = rx_copy_cset (cset_size,
425 if (node->params.cstr.reallen)
426 if (rx_copy_string (&(n->params.cstr), &(node->params.cstr)))
432 n->params.intval = node->params.intval;
433 n->params.intval2 = node->params.intval2;
434 n->params.pair.left = node->params.pair.left;
435 rx_save_rexp (node->params.pair.left);
436 n->params.pair.right = node->params.pair.right;
437 rx_save_rexp (node->params.pair.right);
440 n->observed = node->observed;
450 rx_rexp_equal (struct rexp_node * a, struct rexp_node * b)
454 struct rexp_node * a;
455 struct rexp_node * b;
463 if ((a == 0) || (b == 0))
466 if (a->type != b->type)
472 ret = ( (a->params.cset_size == b->params.cset_size)
473 && rx_bitset_is_equal (a->params.cset_size,
479 ret = rx_compare_rx_strings (&(a->params.cstr), &(b->params.cstr));
483 ret = (a->params.intval == b->params.intval);
488 ret = ( rx_rexp_equal (a->params.pair.left, b->params.pair.left)
489 && rx_rexp_equal (a->params.pair.right, b->params.pair.right));
494 ret = rx_rexp_equal (a->params.pair.left, b->params.pair.left);
497 ret = ( (a->params.intval == b->params.intval)
498 && (a->params.intval2 == b->params.intval2)
499 && rx_rexp_equal (a->params.pair.left, b->params.pair.left));
502 ret = ( (a->params.intval == b->params.intval)
503 && rx_rexp_equal (a->params.pair.left, b->params.pair.left));
507 ret = (a->params.intval == b->params.intval);
521 rx_rexp_hash (struct rexp_node * node, unsigned long seed)
524 rx_rexp_hash (node, seed)
525 struct rexp_node * node;
532 seed = rx_rexp_hash (node->params.pair.left, seed);
533 seed = rx_rexp_hash (node->params.pair.right, seed);
534 seed = rx_bitset_hash (node->params.cset_size, node->params.cset);
535 seed = rx_string_hash (seed, &(node->params.cstr));
536 seed += (seed << 3) + node->params.intval;
537 seed += (seed << 3) + node->params.intval2;
538 seed += (seed << 3) + node->type;
539 seed += (seed << 3) + node->id;
541 seed += (seed << 3) + node->len;
542 seed += (seed << 3) + node->observed;