1/*
2 * This file is part of the KDE libraries
3 * Copyright (C) 2003 Apple Computer, Inc
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library 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 GNU
13 * Library General Public License for more details.
14 *
15 * You should have received a copy of the GNU Library General Public License
16 * along with this library; see the file COPYING.LIB. If not, write to
17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19 *
20 */
21
22#ifndef KJS_IDENTIFIER_H
23#define KJS_IDENTIFIER_H
24
25#include "ustring.h"
26
27#include <wtf/HashFunctions.h>
28#include <wtf/HashTraits.h>
29
30namespace KJS {
31
32 /**
33 * Represents an Identifier for a Javascript object.
34 */
35 class KJS_EXPORT Identifier {
36 friend class PropertyMap;
37 public:
38 /**
39 * Creates an empty identifier
40 */
41 Identifier() { }
42 /**
43 * Creates an identifier with the name of the string
44 * @code
45 * KJS::Identifier method("someJSMethod");
46 * @endcode
47 */
48 Identifier(const char* s) : _ustring(add(s)) { }
49 Identifier(const UChar* s, int length) : _ustring(add(s, length)) { }
50 explicit Identifier(UString::Rep* rep) : _ustring(add(rep)) { }
51 explicit Identifier(const UString& s) : _ustring(add(s.rep())) { }
52
53
54 /**
55 * returns a UString of the identifier
56 */
57 const UString& ustring() const { return _ustring; }
58 KJS_EXTERNAL_EXPORT DOM::DOMString domString() const;
59 /**
60 * returns a QString of the identifier
61 */
62 KJS_EXTERNAL_EXPORT QString qstring() const;
63
64 /**
65 * returns a UChar pointer to the string of the identifier with a size defined by size().
66 */
67 const UChar* data() const { return _ustring.data(); }
68 /**
69 * The size of the UChar string returned.
70 */
71 int size() const { return _ustring.size(); }
72
73 /**
74 * Char * of the identifier's string.
75 */
76 const char* ascii() const { return _ustring.ascii(); }
77
78 static Identifier from(unsigned y) { return Identifier(UString::from(y)); }
79
80 /**
81 * Returns the identfiers state of being unset.
82 */
83 bool isNull() const { return _ustring.isNull(); }
84 /**
85 * Returns that the identifiers string is set, but is empty.
86 */
87 bool isEmpty() const { return _ustring.isEmpty(); }
88
89 uint32_t toStrictUInt32(bool* ok) const { return _ustring.toStrictUInt32(ok); }
90 unsigned toArrayIndex(bool* ok) const { return _ustring.toArrayIndex(ok); }
91 double toDouble() const { return _ustring.toDouble(); }
92
93 friend bool operator==(const Identifier&, const Identifier&);
94 friend bool operator!=(const Identifier&, const Identifier&);
95
96 friend bool operator==(const Identifier&, const char*);
97
98 static void remove(UString::Rep*);
99 static bool equal(const UString::Rep*, const char*);
100 static bool equal(const UString::Rep*, const UChar*, int length);
101
102 private:
103 UString _ustring;
104
105 static bool equal(const Identifier& a, const Identifier& b)
106 { return a._ustring.rep() == b._ustring.rep(); }
107 static bool equal(const Identifier& a, const char* b)
108 { return equal(a._ustring.rep(), b); }
109
110 static PassRefPtr<UString::Rep> add(const char*);
111 static PassRefPtr<UString::Rep> add(const UChar*, int length);
112 static PassRefPtr<UString::Rep> add(UString::Rep* r)
113 {
114 if (r->isIdentifier)
115 return r;
116 return addSlowCase(r);
117 }
118 static PassRefPtr<UString::Rep> addSlowCase(UString::Rep *r);
119 };
120
121 inline bool operator==(const Identifier& a, const Identifier& b)
122 { return Identifier::equal(a, b); }
123
124 inline bool operator!=(const Identifier& a, const Identifier& b)
125 { return !Identifier::equal(a, b); }
126
127 inline bool operator==(const Identifier& a, const char* b)
128 { return Identifier::equal(a, b); }
129
130} // namespace KJS
131
132#endif // KJS_IDENTIFIER_H
133