bearch: Disallow passing Projs to get_irn_ops().
[libfirm] / ir / ident / ident.c
1 /*
2  * This file is part of libFirm.
3  * Copyright (C) 2012 University of Karlsruhe.
4  */
5
6 /**
7  * @file
8  * @brief     Hash table to store names.
9  * @author    Goetz Lindenmaier
10  */
11 #include "config.h"
12
13 #include <assert.h>
14 #include <ctype.h>
15 #include <stdio.h>
16 #include <string.h>
17 #include <stddef.h>
18 #include <stdlib.h>
19
20 #include "ident_t.h"
21 #include "set.h"
22 #include "xmalloc.h"
23 #include "hashptr.h"
24
25 static set *id_set;
26
27 void init_ident(void)
28 {
29         /* it's ok to use memcmp here, we check only strings */
30         id_set = new_set(memcmp, 128);
31 }
32
33 ident *new_id_from_chars(const char *str, size_t len)
34 {
35         unsigned hash   = hash_data((const unsigned char*)str, len);
36         ident   *result = (ident*) set_hinsert0(id_set, str, len, hash);
37         return result;
38 }
39
40 ident *new_id_from_str(const char *str)
41 {
42         assert(str != NULL);
43         return new_id_from_chars(str, strlen(str));
44 }
45
46 const char *get_id_str(ident *id)
47 {
48         struct set_entry *entry = (struct set_entry*) id;
49         return (const char*) entry->dptr;
50 }
51
52 size_t get_id_strlen(ident *id)
53 {
54         struct set_entry *entry = (struct set_entry*) id;
55         return entry->size;
56 }
57
58 void finish_ident(void)
59 {
60         del_set(id_set);
61         id_set = NULL;
62 }
63
64 int id_is_prefix(ident *prefix, ident *id)
65 {
66         size_t prefix_len = get_id_strlen(prefix);
67         if (prefix_len > get_id_strlen(id))
68                 return 0;
69         return 0 == memcmp(get_id_str(prefix), get_id_str(id), prefix_len);
70 }
71
72 int id_is_suffix(ident *suffix, ident *id)
73 {
74         size_t suflen = get_id_strlen(suffix);
75         size_t idlen  = get_id_strlen(id);
76         const char *part;
77
78         if (suflen > idlen)
79                 return 0;
80
81         part = get_id_str(id);
82         part = part + (idlen - suflen);
83
84         return 0 == memcmp(get_id_str(suffix), part, suflen);
85 }
86
87 int id_contains_char(ident *id, char c)
88 {
89         return strchr(get_id_str(id), c) != NULL;
90 }
91
92 ident *id_unique(const char *tag)
93 {
94         static unsigned unique_id = 0;
95         char buf[256];
96
97         snprintf(buf, sizeof(buf), tag, unique_id);
98         unique_id++;
99         return new_id_from_str(buf);
100 }