22 #include "identifier.h"
24 #include <wtf/FastMalloc.h>
25 #include <wtf/HashSet.h>
32 typedef HashSet<UString::Rep *> IdentifierTable;
33 static IdentifierTable *table;
35 static inline IdentifierTable &identifierTable()
38 table =
new IdentifierTable;
43 bool Identifier::equal(
const UString::Rep *r,
const char *s)
46 const UChar *d = r->data();
47 for (
int i = 0; i != length; ++i)
48 if (d[i].uc != (
unsigned char)s[i]) {
51 return s[length] == 0;
54 bool Identifier::equal(
const UString::Rep *r,
const UChar *s,
int length)
56 if (r->len != length) {
59 const UChar *d = r->data();
60 for (
int i = 0; i != length; ++i)
61 if (d[i].uc != s[i].uc) {
67 struct CStringTranslator {
68 static unsigned hash(
const char *c)
70 return UString::Rep::computeHash(c);
73 static bool equal(UString::Rep *r,
const char *s)
75 return Identifier::equal(r, s);
78 static void translate(UString::Rep *&location,
const char *c,
unsigned hash)
80 size_t length = strlen(c);
81 UChar *d =
static_cast<UChar *
>(fastMalloc(
sizeof(UChar) * length));
82 for (
size_t i = 0; i != length; i++) {
86 UString::Rep *r = UString::Rep::create(d,
static_cast<int>(length)).releaseRef();
95 PassRefPtr<UString::Rep> Identifier::add(
const char *c)
98 UString::Rep::null.hash();
99 return &UString::Rep::null;
103 UString::Rep::empty.hash();
104 return &UString::Rep::empty;
107 return *identifierTable().add<
const char *, CStringTranslator>(c).first;
115 struct UCharBufferTranslator {
116 static unsigned hash(
const UCharBuffer &buf)
118 return UString::Rep::computeHash(buf.s, buf.length);
121 static bool equal(UString::Rep *str,
const UCharBuffer &buf)
123 return Identifier::equal(str, buf.s, buf.length);
126 static void translate(UString::Rep *&location,
const UCharBuffer &buf,
unsigned hash)
128 UChar *d =
static_cast<UChar *
>(fastMalloc(
sizeof(UChar) * buf.length));
129 for (
unsigned i = 0; i != buf.length; i++) {
133 UString::Rep *r = UString::Rep::create(d, buf.length).releaseRef();
142 PassRefPtr<UString::Rep> Identifier::add(
const UChar *s,
int length)
145 UString::Rep::empty.hash();
146 return &UString::Rep::empty;
149 UCharBuffer buf = {s,
static_cast<unsigned int>(length)};
150 return *identifierTable().add<UCharBuffer, UCharBufferTranslator>(buf).first;
153 PassRefPtr<UString::Rep> Identifier::addSlowCase(UString::Rep *r)
155 assert(!r->isIdentifier);
158 UString::Rep::empty.hash();
159 return &UString::Rep::empty;
162 UString::Rep *result = *identifierTable().add(r).first;
164 r->isIdentifier =
true;
169 void Identifier::remove(UString::Rep *r)
171 identifierTable().remove(r);