• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2009 Google Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
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
13  * distribution.
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.
17  *
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.
29  */
30 
31 #ifndef WebCString_h
32 #define WebCString_h
33 
34 #include "WebCommon.h"
35 
36 #if WEBKIT_IMPLEMENTATION
37 namespace WebCore { class CString; }
38 #else
39 #include <string>
40 #endif
41 
42 namespace WebKit {
43 
44 class WebCStringPrivate;
45 class WebString;
46 
47 // A single-byte string container with unspecified encoding.  It is
48 // inexpensive to copy a WebCString object.
49 //
50 // WARNING: It is not safe to pass a WebCString across threads!!!
51 //
52 class WebCString {
53 public:
~WebCString()54     ~WebCString() { reset(); }
55 
WebCString()56     WebCString() : m_private(0) { }
57 
WebCString(const char * data,size_t len)58     WebCString(const char* data, size_t len) : m_private(0)
59     {
60         assign(data, len);
61     }
62 
WebCString(const WebCString & s)63     WebCString(const WebCString& s) : m_private(0) { assign(s); }
64 
65     WebCString& operator=(const WebCString& s)
66     {
67         assign(s);
68         return *this;
69     }
70 
71     WEBKIT_API void reset();
72     WEBKIT_API void assign(const WebCString&);
73     WEBKIT_API void assign(const char* data, size_t len);
74 
75     WEBKIT_API size_t length() const;
76     WEBKIT_API const char* data() const;
77 
isEmpty()78     bool isEmpty() const { return !length(); }
isNull()79     bool isNull() const { return !m_private; }
80 
81     WEBKIT_API WebString utf16() const;
82 
83     WEBKIT_API static WebCString fromUTF16(const WebUChar* data, size_t length);
84     WEBKIT_API static WebCString fromUTF16(const WebUChar* data);
85 
86 #if WEBKIT_IMPLEMENTATION
87     WebCString(const WebCore::CString&);
88     WebCString& operator=(const WebCore::CString&);
89     operator WebCore::CString() const;
90 #else
WebCString(const std::string & s)91     WebCString(const std::string& s) : m_private(0)
92     {
93         assign(s.data(), s.length());
94     }
95 
96     WebCString& operator=(const std::string& s)
97     {
98         assign(s.data(), s.length());
99         return *this;
100     }
101 
string()102     operator std::string() const
103     {
104         size_t len = length();
105         return len ? std::string(data(), len) : std::string();
106     }
107 
108     template <class UTF16String>
fromUTF16(const UTF16String & s)109     static WebCString fromUTF16(const UTF16String& s)
110     {
111         return fromUTF16(s.data(), s.length());
112     }
113 #endif
114 
115 private:
116     void assign(WebCStringPrivate*);
117     WebCStringPrivate* m_private;
118 };
119 
120 } // namespace WebKit
121 
122 #endif
123