193ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)/*
293ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles) * Copyright (C) 2009 Google Inc. All rights reserved.
393ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles) *
493ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles) * Redistribution and use in source and binary forms, with or without
593ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles) * modification, are permitted provided that the following conditions are
693ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles) * met:
793ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles) *
893ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles) *     * Redistributions of source code must retain the above copyright
993ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles) * notice, this list of conditions and the following disclaimer.
1093ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles) *     * Redistributions in binary form must reproduce the above
1193ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles) * copyright notice, this list of conditions and the following disclaimer
1293ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles) * in the documentation and/or other materials provided with the
1393ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles) * distribution.
1493ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles) *     * Neither the name of Google Inc. nor the names of its
1593ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles) * contributors may be used to endorse or promote products derived from
1693ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles) * this software without specific prior written permission.
1793ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles) *
1893ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles) * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1993ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles) * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
2093ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles) * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
2193ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles) * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2293ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles) * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2393ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles) * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2493ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles) * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2593ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles) * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2693ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles) * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2793ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles) * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2893ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles) * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2993ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles) */
3093ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)
3193ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)#ifndef WebString_h
3293ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)#define WebString_h
3393ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)
3493ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)#include "WebCommon.h"
355267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)#include "WebPrivatePtr.h"
3602772c6a72f1ee0b226341a4f4439970c29fc861Ben Murdoch#include <string>
3793ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)
381e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)#if INSIDE_BLINK
39c0e19a689c8ac22cdc96b291a8d33a5d3b0b34a4Torne (Richard Coles)#include "wtf/Forward.h"
4093ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)#else
415267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)#include <base/strings/latin1_string_conversions.h>
42521d96ec04ace82590870fb04353ec4f82bb150fTorne (Richard Coles)#include <base/strings/nullable_string16.h>
43521d96ec04ace82590870fb04353ec4f82bb150fTorne (Richard Coles)#include <base/strings/string16.h>
4493ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)#endif
4593ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)
465267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)namespace WTF {
475267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)class StringImpl;
485267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)}
495267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)
5051b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)namespace blink {
5193ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)
5293ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)class WebCString;
5393ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)
5493ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)// A UTF-16 string container.  It is inexpensive to copy a WebString
5593ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)// object.
5693ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)//
5793ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)// WARNING: It is not safe to pass a WebString across threads!!!
5893ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)//
5993ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)class WebString {
6093ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)public:
6193ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)    ~WebString() { reset(); }
6293ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)
635267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)    WebString() { }
6493ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)
655267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)    WebString(const WebUChar* data, size_t len)
6693ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)    {
6793ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)        assign(data, len);
6893ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)    }
6993ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)
705267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)    WebString(const WebString& s) { assign(s); }
7193ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)
7293ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)    WebString& operator=(const WebString& s)
7393ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)    {
7493ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)        assign(s);
7593ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)        return *this;
7693ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)    }
7793ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)
78521d96ec04ace82590870fb04353ec4f82bb150fTorne (Richard Coles)    BLINK_COMMON_EXPORT void reset();
79521d96ec04ace82590870fb04353ec4f82bb150fTorne (Richard Coles)    BLINK_COMMON_EXPORT void assign(const WebString&);
80521d96ec04ace82590870fb04353ec4f82bb150fTorne (Richard Coles)    BLINK_COMMON_EXPORT void assign(const WebUChar* data, size_t len);
8193ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)
82521d96ec04ace82590870fb04353ec4f82bb150fTorne (Richard Coles)    BLINK_COMMON_EXPORT bool equals(const WebString&) const;
8393ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)
84521d96ec04ace82590870fb04353ec4f82bb150fTorne (Richard Coles)    BLINK_COMMON_EXPORT size_t length() const;
8593ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)
8693ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)    // Caller must check bounds.
87521d96ec04ace82590870fb04353ec4f82bb150fTorne (Richard Coles)    BLINK_COMMON_EXPORT WebUChar at(unsigned) const;
8893ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)
8993ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)    bool isEmpty() const { return !length(); }
905267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)    bool isNull() const { return m_private.isNull(); }
9193ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)
9202772c6a72f1ee0b226341a4f4439970c29fc861Ben Murdoch    BLINK_COMMON_EXPORT std::string utf8() const;
9393ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)
94521d96ec04ace82590870fb04353ec4f82bb150fTorne (Richard Coles)    BLINK_COMMON_EXPORT static WebString fromUTF8(const char* data, size_t length);
95521d96ec04ace82590870fb04353ec4f82bb150fTorne (Richard Coles)    BLINK_COMMON_EXPORT static WebString fromUTF8(const char* data);
9693ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)
9702772c6a72f1ee0b226341a4f4439970c29fc861Ben Murdoch    static WebString fromUTF8(const std::string& s)
9802772c6a72f1ee0b226341a4f4439970c29fc861Ben Murdoch    {
9902772c6a72f1ee0b226341a4f4439970c29fc861Ben Murdoch        return fromUTF8(s.data(), s.length());
10002772c6a72f1ee0b226341a4f4439970c29fc861Ben Murdoch    }
10102772c6a72f1ee0b226341a4f4439970c29fc861Ben Murdoch
1028abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)    BLINK_COMMON_EXPORT std::string latin1() const;
1038abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)
1048abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)    BLINK_COMMON_EXPORT static WebString fromLatin1(const WebLChar* data, size_t length);
1058abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)
1068abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)    static WebString fromLatin1(const std::string& s)
1078abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)    {
1088abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)        return fromLatin1(reinterpret_cast<const WebLChar*>(s.data()), s.length());
1098abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)    }
1108abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)
11193ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)    template <int N> WebString(const char (&data)[N])
11293ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)    {
11393ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)        assign(fromUTF8(data, N - 1));
11493ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)    }
11593ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)
11693ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)    template <int N> WebString& operator=(const char (&data)[N])
11793ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)    {
11893ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)        assign(fromUTF8(data, N - 1));
11993ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)        return *this;
12093ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)    }
12193ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)
1221e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)#if INSIDE_BLINK
123521d96ec04ace82590870fb04353ec4f82bb150fTorne (Richard Coles)    BLINK_COMMON_EXPORT WebString(const WTF::String&);
124521d96ec04ace82590870fb04353ec4f82bb150fTorne (Richard Coles)    BLINK_COMMON_EXPORT WebString& operator=(const WTF::String&);
125521d96ec04ace82590870fb04353ec4f82bb150fTorne (Richard Coles)    BLINK_COMMON_EXPORT operator WTF::String() const;
12693ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)
127521d96ec04ace82590870fb04353ec4f82bb150fTorne (Richard Coles)    BLINK_COMMON_EXPORT WebString(const WTF::AtomicString&);
128521d96ec04ace82590870fb04353ec4f82bb150fTorne (Richard Coles)    BLINK_COMMON_EXPORT WebString& operator=(const WTF::AtomicString&);
129521d96ec04ace82590870fb04353ec4f82bb150fTorne (Richard Coles)    BLINK_COMMON_EXPORT operator WTF::AtomicString() const;
13093ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)#else
131521d96ec04ace82590870fb04353ec4f82bb150fTorne (Richard Coles)    WebString(const base::string16& s)
13293ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)    {
13393ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)        assign(s.data(), s.length());
13493ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)    }
13593ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)
136521d96ec04ace82590870fb04353ec4f82bb150fTorne (Richard Coles)    WebString& operator=(const base::string16& s)
13793ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)    {
13893ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)        assign(s.data(), s.length());
13993ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)        return *this;
14093ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)    }
14193ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)
142521d96ec04ace82590870fb04353ec4f82bb150fTorne (Richard Coles)    operator base::string16() const
14393ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)    {
1445267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)        return base::Latin1OrUTF16ToUTF16(length(), data8(), data16());
14593ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)    }
14693ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)
147521d96ec04ace82590870fb04353ec4f82bb150fTorne (Richard Coles)    WebString(const base::NullableString16& s)
14893ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)    {
14993ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)        if (s.is_null())
15093ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)            reset();
15193ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)        else
15293ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)            assign(s.string().data(), s.string().length());
15393ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)    }
15493ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)
155521d96ec04ace82590870fb04353ec4f82bb150fTorne (Richard Coles)    WebString& operator=(const base::NullableString16& s)
15693ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)    {
15793ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)        if (s.is_null())
15893ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)            reset();
15993ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)        else
16093ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)            assign(s.string().data(), s.string().length());
16193ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)        return *this;
16293ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)    }
16393ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)
164521d96ec04ace82590870fb04353ec4f82bb150fTorne (Richard Coles)    operator base::NullableString16() const
16593ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)    {
166521d96ec04ace82590870fb04353ec4f82bb150fTorne (Richard Coles)        return base::NullableString16(operator base::string16(), m_private.isNull());
16793ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)    }
16893ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)#endif
16993ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)
17093ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)private:
171521d96ec04ace82590870fb04353ec4f82bb150fTorne (Richard Coles)    BLINK_COMMON_EXPORT bool is8Bit() const;
172521d96ec04ace82590870fb04353ec4f82bb150fTorne (Richard Coles)    BLINK_COMMON_EXPORT const WebLChar* data8() const;
173521d96ec04ace82590870fb04353ec4f82bb150fTorne (Richard Coles)    BLINK_COMMON_EXPORT const WebUChar* data16() const;
1745267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)
175521d96ec04ace82590870fb04353ec4f82bb150fTorne (Richard Coles)    BLINK_COMMON_EXPORT void assign(WTF::StringImpl*);
17693ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)
1775267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)    WebPrivatePtr<WTF::StringImpl> m_private;
17893ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)};
17993ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)
18093ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)inline bool operator==(const WebString& a, const WebString& b)
18193ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles){
18293ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)    return a.equals(b);
18393ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)}
18493ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)
18593ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)inline bool operator!=(const WebString& a, const WebString& b)
18693ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles){
18793ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)    return !(a == b);
18893ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)}
18993ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)
19051b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)} // namespace blink
19193ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)
19293ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)#endif
193