WebString.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. #ifndef WebString_h
  31. #define WebString_h
  32. #include "WebCommon.h"
  33. #if WEBKIT_IMPLEMENTATION
  34. #include <wtf/Forward.h>
  35. #elif WEBKIT_MANX
  36. #else
  37. #include <base/nullable_string16.h>
  38. #include <base/string16.h>
  39. #endif
  40. namespace WebKit {
  41. class WebCString;
  42. class WebStringPrivate;
  43. // A UTF-16 string container. It is inexpensive to copy a WebString
  44. // object.
  45. //
  46. // WARNING: It is not safe to pass a WebString across threads!!!
  47. //
  48. class WebString {
  49. public:
  50. ~WebString() { reset(); }
  51. WebString() : m_private(0) { }
  52. WebString(const WebUChar* data, size_t len) : m_private(0)
  53. {
  54. assign(data, len);
  55. }
  56. WebString(const WebString& s) : m_private(0) { assign(s); }
  57. WebString& operator=(const WebString& s)
  58. {
  59. assign(s);
  60. return *this;
  61. }
  62. WEBKIT_EXPORT void reset();
  63. WEBKIT_EXPORT void assign(const WebString&);
  64. WEBKIT_EXPORT void assign(const WebUChar* data, size_t len);
  65. WEBKIT_EXPORT bool equals(const WebString&) const;
  66. WEBKIT_EXPORT size_t length() const;
  67. WEBKIT_EXPORT const WebUChar* data() const;
  68. bool isEmpty() const { return !length(); }
  69. bool isNull() const { return !m_private; }
  70. WEBKIT_EXPORT WebCString utf8() const;
  71. WEBKIT_EXPORT static WebString fromUTF8(const char* data, size_t length);
  72. WEBKIT_EXPORT static WebString fromUTF8(const char* data);
  73. template <int N> WebString(const char (&data)[N])
  74. : m_private(0)
  75. {
  76. assign(fromUTF8(data, N - 1));
  77. }
  78. template <int N> WebString& operator=(const char (&data)[N])
  79. {
  80. assign(fromUTF8(data, N - 1));
  81. return *this;
  82. }
  83. #if WEBKIT_IMPLEMENTATION
  84. WebString(const WTF::String&);
  85. WebString& operator=(const WTF::String&);
  86. operator WTF::String() const;
  87. WebString(const WTF::AtomicString&);
  88. WebString& operator=(const WTF::AtomicString&);
  89. operator WTF::AtomicString() const;
  90. #elif WEBKIT_MANX
  91. #else
  92. WebString(const string16& s) : m_private(0)
  93. {
  94. assign(s.data(), s.length());
  95. }
  96. WebString& operator=(const string16& s)
  97. {
  98. assign(s.data(), s.length());
  99. return *this;
  100. }
  101. operator string16() const
  102. {
  103. size_t len = length();
  104. return len ? string16(data(), len) : string16();
  105. }
  106. WebString(const NullableString16& s) : m_private(0)
  107. {
  108. if (s.is_null())
  109. reset();
  110. else
  111. assign(s.string().data(), s.string().length());
  112. }
  113. WebString& operator=(const NullableString16& s)
  114. {
  115. if (s.is_null())
  116. reset();
  117. else
  118. assign(s.string().data(), s.string().length());
  119. return *this;
  120. }
  121. operator NullableString16() const
  122. {
  123. if (!m_private)
  124. return NullableString16(string16(), true);
  125. size_t len = length();
  126. return NullableString16(len ? string16(data(), len) : string16(), false);
  127. }
  128. template <class UTF8String>
  129. static WebString fromUTF8(const UTF8String& s)
  130. {
  131. return fromUTF8(s.data(), s.length());
  132. }
  133. #endif
  134. private:
  135. void assign(WebStringPrivate*);
  136. WebStringPrivate* m_private;
  137. };
  138. inline bool operator==(const WebString& a, const WebString& b)
  139. {
  140. return a.equals(b);
  141. }
  142. inline bool operator!=(const WebString& a, const WebString& b)
  143. {
  144. return !(a == b);
  145. }
  146. } // namespace WebKit
  147. #endif