File indexing completed on 2024-05-12 15:43:22

0001 /*
0002  *  This file is part of the KDE libraries
0003  *  Copyright (C) 2003 Apple Computer, Inc
0004  *
0005  *  This library is free software; you can redistribute it and/or
0006  *  modify it under the terms of the GNU Library General Public
0007  *  License as published by the Free Software Foundation; either
0008  *  version 2 of the License, or (at your option) any later version.
0009  *
0010  *  This library is distributed in the hope that it will be useful,
0011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
0012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0013  *  Library General Public License for more details.
0014  *
0015  *  You should have received a copy of the GNU Library General Public License
0016  *  along with this library; see the file COPYING.LIB.  If not, write to
0017  *  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0018  *  Boston, MA 02110-1301, USA.
0019  *
0020  */
0021 
0022 #ifndef KJS_IDENTIFIER_H
0023 #define KJS_IDENTIFIER_H
0024 
0025 #include "ustring.h"
0026 
0027 #include <wtf/HashFunctions.h>
0028 #include <wtf/HashTraits.h>
0029 
0030 namespace KJS
0031 {
0032 
0033 /**
0034  * Represents an Identifier for a Javascript object.
0035  */
0036 class KJS_EXPORT Identifier
0037 {
0038     friend class PropertyMap;
0039 public:
0040     /**
0041     * Creates an empty identifier
0042     */
0043     Identifier() { }
0044     /**
0045     * Creates an identifier with the name of the string
0046     * @code
0047     * KJS::Identifier method("someJSMethod");
0048     * @endcode
0049     */
0050     Identifier(const char *s) : _ustring(add(s)) { }
0051     Identifier(const UChar *s, int length) : _ustring(add(s, length)) { }
0052     explicit Identifier(UString::Rep *rep) : _ustring(add(rep)) { }
0053     explicit Identifier(const UString &s) : _ustring(add(s.rep())) { }
0054 
0055     /**
0056     * returns a UString of the identifier
0057     */
0058     const UString &ustring() const
0059     {
0060         return _ustring;
0061     }
0062     KJS_EXTERNAL_EXPORT DOM::DOMString domString() const;
0063     /**
0064     * returns a QString of the identifier
0065     */
0066     KJS_EXTERNAL_EXPORT QString qstring() const;
0067 
0068     /**
0069     * returns a UChar pointer to the string of the identifier with a size defined by size().
0070     */
0071     const UChar *data() const
0072     {
0073         return _ustring.data();
0074     }
0075     /**
0076     * The size of the UChar string returned.
0077     */
0078     int size() const
0079     {
0080         return _ustring.size();
0081     }
0082 
0083     /**
0084     * Char * of the identifier's string.
0085     */
0086     const char *ascii() const
0087     {
0088         return _ustring.ascii();
0089     }
0090 
0091     static Identifier from(unsigned y)
0092     {
0093         return Identifier(UString::from(y));
0094     }
0095 
0096     /**
0097     * Returns the identfiers state of being unset.
0098     */
0099     bool isNull() const
0100     {
0101         return _ustring.isNull();
0102     }
0103     /**
0104     * Returns that the identifiers string is set, but is empty.
0105     */
0106     bool isEmpty() const
0107     {
0108         return _ustring.isEmpty();
0109     }
0110 
0111     uint32_t toStrictUInt32(bool *ok) const
0112     {
0113         return _ustring.toStrictUInt32(ok);
0114     }
0115     unsigned toArrayIndex(bool *ok) const
0116     {
0117         return _ustring.toArrayIndex(ok);
0118     }
0119     double toDouble() const
0120     {
0121         return _ustring.toDouble();
0122     }
0123 
0124     friend bool operator==(const Identifier &, const Identifier &);
0125     friend bool operator!=(const Identifier &, const Identifier &);
0126 
0127     friend bool operator==(const Identifier &, const char *);
0128 
0129     static void remove(UString::Rep *);
0130     static bool equal(const UString::Rep *, const char *);
0131     static bool equal(const UString::Rep *, const UChar *, int length);
0132 
0133 private:
0134     UString _ustring;
0135 
0136     static bool equal(const Identifier &a, const Identifier &b)
0137     {
0138         return a._ustring.rep() == b._ustring.rep();
0139     }
0140     static bool equal(const Identifier &a, const char *b)
0141     {
0142         return equal(a._ustring.rep(), b);
0143     }
0144 
0145     static PassRefPtr<UString::Rep> add(const char *);
0146     static PassRefPtr<UString::Rep> add(const UChar *, int length);
0147     static PassRefPtr<UString::Rep> add(UString::Rep *r)
0148     {
0149         if (r->isIdentifier) {
0150             return r;
0151         }
0152         return addSlowCase(r);
0153     }
0154     static PassRefPtr<UString::Rep> addSlowCase(UString::Rep *r);
0155 };
0156 
0157 inline bool operator==(const Identifier &a, const Identifier &b)
0158 {
0159     return Identifier::equal(a, b);
0160 }
0161 
0162 inline bool operator!=(const Identifier &a, const Identifier &b)
0163 {
0164     return !Identifier::equal(a, b);
0165 }
0166 
0167 inline bool operator==(const Identifier &a, const char *b)
0168 {
0169     return Identifier::equal(a, b);
0170 }
0171 
0172 } // namespace KJS
0173 
0174 #endif // KJS_IDENTIFIER_H