WebKit/chromium/public/WebCString.h
changeset 0 4f2f89ce4247
equal deleted inserted replaced
-1:000000000000 0:4f2f89ce4247
       
     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 WTF { 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:
       
    54     ~WebCString() { reset(); }
       
    55 
       
    56     WebCString() : m_private(0) { }
       
    57 
       
    58     WebCString(const char* data, size_t len) : m_private(0)
       
    59     {
       
    60         assign(data, len);
       
    61     }
       
    62 
       
    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     // Returns 0 if both strings are equals, a value greater than zero if the
       
    72     // first character that does not match has a greater value in this string
       
    73     // than in |other|, or a value less than zero to indicate the opposite.
       
    74     WEBKIT_API int compare(const WebCString& other) const;
       
    75 
       
    76     WEBKIT_API void reset();
       
    77     WEBKIT_API void assign(const WebCString&);
       
    78     WEBKIT_API void assign(const char* data, size_t len);
       
    79 
       
    80     WEBKIT_API size_t length() const;
       
    81     WEBKIT_API const char* data() const;
       
    82 
       
    83     bool isEmpty() const { return !length(); }
       
    84     bool isNull() const { return !m_private; }
       
    85 
       
    86     WEBKIT_API WebString utf16() const;
       
    87 
       
    88     WEBKIT_API static WebCString fromUTF16(const WebUChar* data, size_t length);
       
    89     WEBKIT_API static WebCString fromUTF16(const WebUChar* data);
       
    90 
       
    91 #if WEBKIT_IMPLEMENTATION
       
    92     WebCString(const WTF::CString&);
       
    93     WebCString& operator=(const WTF::CString&);
       
    94     operator WTF::CString() const;
       
    95 #else
       
    96     WebCString(const std::string& s) : m_private(0)
       
    97     {
       
    98         assign(s.data(), s.length());
       
    99     }
       
   100 
       
   101     WebCString& operator=(const std::string& s)
       
   102     {
       
   103         assign(s.data(), s.length());
       
   104         return *this;
       
   105     }
       
   106 
       
   107     operator std::string() const
       
   108     {
       
   109         size_t len = length();
       
   110         return len ? std::string(data(), len) : std::string();
       
   111     }
       
   112 
       
   113     template <class UTF16String>
       
   114     static WebCString fromUTF16(const UTF16String& s)
       
   115     {
       
   116         return fromUTF16(s.data(), s.length());
       
   117     }
       
   118 #endif
       
   119 
       
   120 private:
       
   121     void assign(WebCStringPrivate*);
       
   122     WebCStringPrivate* m_private;
       
   123 };
       
   124 
       
   125 inline bool operator<(const WebCString& a, const WebCString& b)
       
   126 {
       
   127     return a.compare(b) < 0;
       
   128 }
       
   129 
       
   130 } // namespace WebKit
       
   131 
       
   132 #endif