dom_string.h 5.07 KB
Newer Older
George Staikos's avatar
George Staikos committed
1 2 3
/*
 * This file is part of the DOM implementation for KDE.
 *
4
 * Copyright 1999 Lars Knoll (knoll@kde.org)
George Staikos's avatar
George Staikos committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this library; see the file COPYING.LIB.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 *
 */
#ifndef _DOM_DOMString_h_
#define _DOM_DOMString_h_

Allen Winter's avatar
Allen Winter committed
25
#include <khtml_export.h>
George Staikos's avatar
George Staikos committed
26
#include <kdebug.h>
27
#include <QtCore/QString>
28
#include <limits.h>
George Staikos's avatar
George Staikos committed
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55

namespace DOM {

class DOMStringImpl;

/**
 * This class implements the basic string we use in the DOM. We do not use
 * QString for 2 reasons: Memory overhead, and the missing explicit sharing
 * of strings we need for the DOM.
 *
 * All DOMStrings are explicitly shared (they behave like pointers), meaning
 * that modifications to one instance will also modify all others. If you
 * wish to get a DOMString that is independent, use copy().
 */
class KHTML_EXPORT DOMString
{
    friend class CharacterDataImpl;
    friend KHTML_EXPORT bool operator==( const DOMString &a, const char *b );
public:
    /**
     * default constructor. Gives an empty DOMString
     */
    DOMString() : impl(0) {}

    DOMString(const QChar *str, uint len);
    DOMString(const QString &);
    DOMString(const char *str);
56 57 58 59
    /**
     * @since 4.2
     */
    DOMString(const char *str, uint len);
George Staikos's avatar
George Staikos committed
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
    DOMString(DOMStringImpl *i);

    virtual ~DOMString();

    // assign and copy
    DOMString(const DOMString &str);
    DOMString &operator =(const DOMString &str);

    /**
     * append str to this string
     */
    DOMString &operator += (const DOMString &str);
    /**
     * add two DOMString's
     */
    DOMString operator + (const DOMString &str);

    void insert(DOMString str, uint pos);

    /**
     * The character at position i of the DOMString. If i >= length(), the
     * character returned will be 0.
     */
    const QChar &operator [](unsigned int i) const;

    int find(const QChar c, int start = 0) const;
86 87 88
    int reverseFind(const QChar c, int start = -1) const;

    DOMString substring(unsigned pos, unsigned len = UINT_MAX) const;
George Staikos's avatar
George Staikos committed
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107

    uint length() const;
    void truncate( unsigned int len );
    void remove(unsigned int pos, int len=1);
    /**
     * Splits the string into two. The original string gets truncated to pos, and the rest is returned.
     */
    DOMString split(unsigned int pos);

    /**
     * Returns a lowercase version of the string
     */
    DOMString lower() const;
    /**
     * Returns an uppercase version of the string
     */
    DOMString upper() const;

    QChar *unicode() const;
108 109
    // for WebCore API compatibility
    inline QChar *characters() const { return unicode(); }
George Staikos's avatar
George Staikos committed
110 111 112
    QString string() const;

    int toInt() const;
113
    int toInt(bool* ok) const;
114
    float toFloat(bool* ok = 0) const;
George Staikos's avatar
George Staikos committed
115 116
    bool percentage(int &_percentage) const;

117 118
    static DOMString number(float f);

George Staikos's avatar
George Staikos committed
119 120 121 122 123
    DOMString copy() const;

    bool isNull()  const { return (impl == 0); }
    bool isEmpty()  const;

124
    bool endsWith(const DOMString& str) const;
125
    bool startsWith(const DOMString& str) const;
126

127 128 129 130 131 132 133
    /**
     * Returns a string with Space Characters removed from the start and the end.
     * Space Characters as defined in
     * http://dev.w3.org/html5/spec-LC/common-microsyntaxes.html#space-character
     */
    DOMString trimSpaces() const;

George Staikos's avatar
George Staikos committed
134 135 136 137 138 139
    /**
     * @internal get a handle to the imlementation of the DOMString
     * Use at own risk!!!
     */
    DOMStringImpl *implementation() const { return impl; }

Dirk Mueller's avatar
Dirk Mueller committed
140 141 142 143 144
    static DOMString format(const char* format, ...)
#if defined(__GNUC__)
    __attribute__ ((format (printf, 1, 2)))
#endif
     ;
145

George Staikos's avatar
George Staikos committed
146 147 148 149
protected:
    DOMStringImpl *impl;
};

150
inline QDebug operator<<(QDebug stream, const DOMString &string) {
151
	return (stream << (string.implementation() ? string.string() : QString::fromLatin1("null")));
George Staikos's avatar
George Staikos committed
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
}

KHTML_EXPORT bool operator==( const DOMString &a, const DOMString &b );
KHTML_EXPORT bool operator==( const DOMString &a, const QString &b );
KHTML_EXPORT bool operator==( const DOMString &a, const char *b );
inline bool operator!=( const DOMString &a, const DOMString &b ) { return !(a==b); }
inline bool operator!=( const DOMString &a, const QString &b ) { return !(a==b); }
inline bool operator!=( const DOMString &a, const char *b )  { return !(a==b); }
inline bool strcmp( const DOMString &a, const DOMString &b ) { return a != b; }

// returns false when equal, true otherwise (ignoring case)
KHTML_EXPORT bool strcasecmp( const DOMString &a, const DOMString &b );
KHTML_EXPORT bool strcasecmp( const DOMString& a, const char* b );

}
#endif