2 * Copyright (C) 2009 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #include "WebCommon.h"
36 #if WEBKIT_IMPLEMENTATION
37 #include <wtf/Forward.h>
48 class WebCStringPrivate;
51 // A single-byte string container with unspecified encoding. It is
52 // inexpensive to copy a WebCString object.
54 // WARNING: It is not safe to pass a WebCString across threads!!!
58 ~WebCString() { reset(); }
60 WebCString() : m_private(0) { }
62 WebCString(const char* data, size_t len) : m_private(0)
67 WebCString(const WebCString& s) : m_private(0) { assign(s); }
69 WebCString& operator=(const WebCString& s)
75 // Returns 0 if both strings are equals, a value greater than zero if the
76 // first character that does not match has a greater value in this string
77 // than in |other|, or a value less than zero to indicate the opposite.
78 WEBKIT_EXPORT int compare(const WebCString& other) const;
80 WEBKIT_EXPORT void reset();
81 WEBKIT_EXPORT void assign(const WebCString&);
82 WEBKIT_EXPORT void assign(const char* data, size_t len);
84 WEBKIT_EXPORT size_t length() const;
85 WEBKIT_EXPORT const char* data() const;
87 bool isEmpty() const { return !length(); }
88 bool isNull() const { return !m_private; }
90 WEBKIT_EXPORT WebString utf16() const;
92 WEBKIT_EXPORT static WebCString fromUTF16(const WebUChar* data, size_t length);
93 WEBKIT_EXPORT static WebCString fromUTF16(const WebUChar* data);
95 #if WEBKIT_IMPLEMENTATION
96 WebCString(const WTF::CString&);
97 WebCString& operator=(const WTF::CString&);
98 operator WTF::CString() const;
100 WebCString(const std::string& s) : m_private(0)
102 assign(s.data(), s.length());
105 WebCString& operator=(const std::string& s)
107 assign(s.data(), s.length());
111 operator std::string() const
113 size_t len = length();
114 return len ? std::string(data(), len) : std::string();
117 template <class UTF16String>
118 static WebCString fromUTF16(const UTF16String& s)
120 return fromUTF16(s.data(), s.length());
125 void assign(WebCStringPrivate*);
126 WebCStringPrivate* m_private;
129 inline bool operator<(const WebCString& a, const WebCString& b)
131 return a.compare(b) < 0;
134 } // namespace WebKit