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 #include "WebPrivatePtr.h" 36 37 #if INSIDE_BLINK 38 #include "wtf/Forward.h" 39 #endif 40 #if !INSIDE_BLINK || defined(UNIT_TEST) 41 #include <string> 42 #endif 43 44 namespace WTF { 45 class CString; 46 class CStringBuffer; 47 } 48 49 namespace blink { 50 51 class WebString; 52 53 // A single-byte string container with unspecified encoding. It is 54 // inexpensive to copy a WebCString object. 55 // 56 // WARNING: It is not safe to pass a WebCString across threads!!! 57 // 58 class WebCString { 59 public: ~WebCString()60 ~WebCString() { reset(); } 61 WebCString()62 WebCString() { } 63 WebCString(const char * data,size_t len)64 WebCString(const char* data, size_t len) 65 { 66 assign(data, len); 67 } 68 WebCString(const WebCString & s)69 WebCString(const WebCString& s) { assign(s); } 70 71 WebCString& operator=(const WebCString& s) 72 { 73 assign(s); 74 return *this; 75 } 76 77 // Returns 0 if both strings are equals, a value greater than zero if the 78 // first character that does not match has a greater value in this string 79 // than in |other|, or a value less than zero to indicate the opposite. 80 BLINK_COMMON_EXPORT int compare(const WebCString& other) const; 81 82 BLINK_COMMON_EXPORT void reset(); 83 BLINK_COMMON_EXPORT void assign(const WebCString&); 84 BLINK_COMMON_EXPORT void assign(const char* data, size_t len); 85 86 BLINK_COMMON_EXPORT size_t length() const; 87 BLINK_COMMON_EXPORT const char* data() const; 88 isEmpty()89 bool isEmpty() const { return !length(); } isNull()90 bool isNull() const { return m_private.isNull(); } 91 92 BLINK_COMMON_EXPORT WebString utf16() const; 93 94 #if INSIDE_BLINK 95 BLINK_COMMON_EXPORT WebCString(const WTF::CString&); 96 BLINK_COMMON_EXPORT WebCString& operator=(const WTF::CString&); 97 BLINK_COMMON_EXPORT operator WTF::CString() const; 98 #else WebCString(const std::string & s)99 WebCString(const std::string& s) 100 { 101 assign(s.data(), s.length()); 102 } 103 104 WebCString& operator=(const std::string& s) 105 { 106 assign(s.data(), s.length()); 107 return *this; 108 } 109 #endif 110 #if !INSIDE_BLINK || defined(UNIT_TEST) string()111 operator std::string() const 112 { 113 size_t len = length(); 114 return len ? std::string(data(), len) : std::string(); 115 } 116 117 template <class UTF16String> fromUTF16(const UTF16String & s)118 static WebCString fromUTF16(const UTF16String& s) 119 { 120 return fromUTF16(s.data(), s.length()); 121 } 122 #endif 123 124 private: 125 BLINK_COMMON_EXPORT void assign(WTF::CStringBuffer*); 126 WebPrivatePtr<WTF::CStringBuffer> m_private; 127 }; 128 129 inline bool operator<(const WebCString& a, const WebCString& b) 130 { 131 return a.compare(b) < 0; 132 } 133 134 } // namespace blink 135 136 #endif 137