WebString.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. * Copyright (C) 2010 Apple 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
  6. * are met:
  7. * 1. Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * 2. Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
  14. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  15. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  16. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
  17. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  18. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  19. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  20. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  21. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  22. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  23. * THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #ifndef WebString_h
  26. #define WebString_h
  27. #include "APIObject.h"
  28. #include <JavaScriptCore/InitializeThreading.h>
  29. #include <JavaScriptCore/JSStringRef.h>
  30. #include <JavaScriptCore/OpaqueJSString.h>
  31. #include <wtf/PassRefPtr.h>
  32. #include <wtf/text/WTFString.h>
  33. #include <wtf/unicode/UTF8.h>
  34. namespace WebKit {
  35. // WebString - A string type suitable for vending to an API.
  36. class WebString : public TypedAPIObject<APIObject::TypeString> {
  37. public:
  38. static PassRefPtr<WebString> createNull()
  39. {
  40. return adoptRef(new WebString());
  41. }
  42. static PassRefPtr<WebString> create(const String& string)
  43. {
  44. return adoptRef(new WebString(string));
  45. }
  46. static PassRefPtr<WebString> create(JSStringRef jsStringRef)
  47. {
  48. return adoptRef(new WebString(String(jsStringRef->string())));
  49. }
  50. static PassRefPtr<WebString> createFromUTF8String(const char* string)
  51. {
  52. return adoptRef(new WebString(String::fromUTF8(string)));
  53. }
  54. bool isNull() const { return m_string.isNull(); }
  55. bool isEmpty() const { return m_string.isEmpty(); }
  56. size_t length() const { return m_string.length(); }
  57. size_t getCharacters(UChar* buffer, size_t bufferLength) const
  58. {
  59. if (!bufferLength)
  60. return 0;
  61. bufferLength = std::min(bufferLength, static_cast<size_t>(m_string.length()));
  62. memcpy(buffer, m_string.characters(), bufferLength * sizeof(UChar));
  63. return bufferLength;
  64. }
  65. size_t maximumUTF8CStringSize() const { return m_string.length() * 3 + 1; }
  66. size_t getUTF8CString(char* buffer, size_t bufferSize)
  67. {
  68. if (!bufferSize)
  69. return 0;
  70. char* p = buffer;
  71. const UChar* d = m_string.characters();
  72. WTF::Unicode::ConversionResult result = WTF::Unicode::convertUTF16ToUTF8(&d, d + m_string.length(), &p, p + bufferSize - 1, /* strict */ true);
  73. *p++ = '\0';
  74. if (result != WTF::Unicode::conversionOK && result != WTF::Unicode::targetExhausted)
  75. return 0;
  76. return p - buffer;
  77. }
  78. bool equal(WebString* other) { return m_string == other->m_string; }
  79. bool equalToUTF8String(const char* other) { return m_string == String::fromUTF8(other); }
  80. bool equalToUTF8StringIgnoringCase(const char* other) { return equalIgnoringCase(m_string, other); }
  81. const String& string() const { return m_string; }
  82. JSStringRef createJSString() const
  83. {
  84. JSC::initializeThreading();
  85. return OpaqueJSString::create(m_string).leakRef();
  86. }
  87. private:
  88. WebString()
  89. : m_string()
  90. {
  91. }
  92. WebString(const String& string)
  93. : m_string(!string.impl() ? String(StringImpl::empty()) : string)
  94. {
  95. }
  96. String m_string;
  97. };
  98. } // namespace WebKit
  99. #endif // WebString_h