1 | /*
|
---|
2 | * Copyright (C) 2003, 2006, 2007 Apple Inc. All rights reserved.
|
---|
3 | *
|
---|
4 | * This library is free software; you can redistribute it and/or
|
---|
5 | * modify it under the terms of the GNU Library General Public
|
---|
6 | * License as published by the Free Software Foundation; either
|
---|
7 | * version 2 of the License, or (at your option) any later version.
|
---|
8 | *
|
---|
9 | * This library is distributed in the hope that it will be useful,
|
---|
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
12 | * Library General Public License for more details.
|
---|
13 | *
|
---|
14 | * You should have received a copy of the GNU Library General Public License
|
---|
15 | * along with this library; see the file COPYING.LIB. If not, write to
|
---|
16 | * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
---|
17 | * Boston, MA 02110-1301, USA.
|
---|
18 | *
|
---|
19 | */
|
---|
20 |
|
---|
21 | #ifndef KJS_IDENTIFIER_H
|
---|
22 | #define KJS_IDENTIFIER_H
|
---|
23 |
|
---|
24 | #include "JSGlobalData.h"
|
---|
25 | #include "ustring.h"
|
---|
26 |
|
---|
27 | namespace KJS {
|
---|
28 |
|
---|
29 | class Identifier {
|
---|
30 | friend class PropertyMap;
|
---|
31 | public:
|
---|
32 | Identifier() { }
|
---|
33 | Identifier(const char* s) : _ustring(add(s)) { } // Only to be used with string literals.
|
---|
34 | Identifier(const UChar* s, int length) : _ustring(add(s, length)) { }
|
---|
35 | explicit Identifier(UString::Rep* rep) : _ustring(add(rep)) { }
|
---|
36 | explicit Identifier(const UString& s) : _ustring(add(s.rep())) { }
|
---|
37 |
|
---|
38 | Identifier(JSGlobalData* globalData, const char* s) : _ustring(add(globalData, s)) { } // Only to be used with string literals.
|
---|
39 |
|
---|
40 | // Special constructor for cases where we overwrite an object in place.
|
---|
41 | Identifier(PlacementNewAdoptType) : _ustring(PlacementNewAdopt) { }
|
---|
42 |
|
---|
43 | const UString& ustring() const { return _ustring; }
|
---|
44 | DOM::DOMString domString() const;
|
---|
45 |
|
---|
46 | const UChar* data() const { return _ustring.data(); }
|
---|
47 | int size() const { return _ustring.size(); }
|
---|
48 |
|
---|
49 | const char* ascii() const { return _ustring.ascii(); }
|
---|
50 |
|
---|
51 | static Identifier from(unsigned y) { return Identifier(UString::from(y)); }
|
---|
52 |
|
---|
53 | bool isNull() const { return _ustring.isNull(); }
|
---|
54 | bool isEmpty() const { return _ustring.isEmpty(); }
|
---|
55 |
|
---|
56 | uint32_t toUInt32(bool* ok) const { return _ustring.toUInt32(ok); }
|
---|
57 | uint32_t toUInt32(bool* ok, bool tolerateEmptyString) const { return _ustring.toUInt32(ok, tolerateEmptyString); };
|
---|
58 | uint32_t toStrictUInt32(bool* ok) const { return _ustring.toStrictUInt32(ok); }
|
---|
59 | unsigned toArrayIndex(bool* ok) const { return _ustring.toArrayIndex(ok); }
|
---|
60 | double toDouble() const { return _ustring.toDouble(); }
|
---|
61 |
|
---|
62 | friend bool operator==(const Identifier&, const Identifier&);
|
---|
63 | friend bool operator!=(const Identifier&, const Identifier&);
|
---|
64 |
|
---|
65 | friend bool operator==(const Identifier&, const char*);
|
---|
66 |
|
---|
67 | static void remove(UString::Rep*);
|
---|
68 |
|
---|
69 | static bool equal(const UString::Rep*, const char*);
|
---|
70 | static bool equal(const UString::Rep*, const UChar*, int length);
|
---|
71 | static bool equal(const UString::Rep* a, const UString::Rep* b) { return KJS::equal(a, b); }
|
---|
72 |
|
---|
73 | static PassRefPtr<UString::Rep> add(const char*);
|
---|
74 |
|
---|
75 | static void initializeIdentifierThreading();
|
---|
76 |
|
---|
77 | private:
|
---|
78 | UString _ustring;
|
---|
79 |
|
---|
80 | static bool equal(const Identifier& a, const Identifier& b)
|
---|
81 | { return a._ustring.rep() == b._ustring.rep(); }
|
---|
82 | static bool equal(const Identifier& a, const char* b)
|
---|
83 | { return equal(a._ustring.rep(), b); }
|
---|
84 |
|
---|
85 | static PassRefPtr<UString::Rep> add(JSGlobalData*, const char*);
|
---|
86 | static PassRefPtr<UString::Rep> add(const UChar*, int length);
|
---|
87 | static PassRefPtr<UString::Rep> add(UString::Rep* r)
|
---|
88 | {
|
---|
89 | if (r->identifierTable)
|
---|
90 | return r;
|
---|
91 | return addSlowCase(r);
|
---|
92 | }
|
---|
93 | static PassRefPtr<UString::Rep> addSlowCase(UString::Rep *r);
|
---|
94 | };
|
---|
95 |
|
---|
96 | inline bool operator==(const Identifier& a, const Identifier& b)
|
---|
97 | { return Identifier::equal(a, b); }
|
---|
98 |
|
---|
99 | inline bool operator!=(const Identifier& a, const Identifier& b)
|
---|
100 | { return !Identifier::equal(a, b); }
|
---|
101 |
|
---|
102 | inline bool operator==(const Identifier& a, const char* b)
|
---|
103 | { return Identifier::equal(a, b); }
|
---|
104 |
|
---|
105 | IdentifierTable* createIdentifierTable();
|
---|
106 | void deleteIdentifierTable(IdentifierTable*);
|
---|
107 |
|
---|
108 | } // namespace KJS
|
---|
109 |
|
---|
110 | #endif // KJS_IDENTIFIER_H
|
---|