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 26#ifndef APIString_h 27#define APIString_h 28 29#include "APIObject.h" 30#include <JavaScriptCore/InitializeThreading.h> 31#include <JavaScriptCore/JSStringRef.h> 32#include <JavaScriptCore/OpaqueJSString.h> 33#include <wtf/PassRefPtr.h> 34#include <wtf/text/StringView.h> 35#include <wtf/text/WTFString.h> 36#include <wtf/unicode/UTF8.h> 37 38namespace API { 39 40class String final : public ObjectImpl<Object::Type::String> { 41public: 42 static PassRefPtr<String> createNull() 43 { 44 return adoptRef(new String); 45 } 46 47 static PassRefPtr<String> create(const WTF::String& string) 48 { 49 return adoptRef(new String(string)); 50 } 51 52 static PassRefPtr<String> create(JSStringRef jsStringRef) 53 { 54 return adoptRef(new String(jsStringRef->string())); 55 } 56 57 static PassRefPtr<String> createFromUTF8String(const char* string) 58 { 59 return adoptRef(new String(WTF::String::fromUTF8(string))); 60 } 61 62 virtual ~String() 63 { 64 } 65 66 bool isNull() const { return m_string.isNull(); } 67 bool isEmpty() const { return m_string.isEmpty(); } 68 69 size_t length() const { return m_string.length(); } 70 size_t getCharacters(UChar* buffer, size_t bufferLength) const 71 { 72 unsigned unsignedBufferLength = std::min<size_t>(bufferLength, std::numeric_limits<unsigned>::max()); 73 StringView substring = StringView(m_string).substring(0, unsignedBufferLength); 74 substring.getCharactersWithUpconvert(buffer); 75 return substring.length(); 76 } 77 78 size_t maximumUTF8CStringSize() const { return m_string.length() * 3 + 1; } 79 size_t getUTF8CString(char* buffer, size_t bufferSize) 80 { 81 if (!bufferSize) 82 return 0; 83 char* p = buffer; 84 WTF::Unicode::ConversionResult result; 85 if (m_string.is8Bit()) { 86 const LChar* characters = m_string.characters8(); 87 result = WTF::Unicode::convertLatin1ToUTF8(&characters, characters + m_string.length(), &p, p + bufferSize - 1); 88 } else { 89 const UChar* characters = m_string.characters16(); 90 result = WTF::Unicode::convertUTF16ToUTF8(&characters, characters + m_string.length(), &p, p + bufferSize - 1, /* strict */ true); 91 } 92 if (result != WTF::Unicode::conversionOK && result != WTF::Unicode::targetExhausted) 93 return 0; 94 *p++ = '\0'; 95 return p - buffer; 96 } 97 98 bool equal(String* other) { return m_string == other->m_string; } 99 bool equalToUTF8String(const char* other) { return m_string == WTF::String::fromUTF8(other); } 100 bool equalToUTF8StringIgnoringCase(const char* other) { return equalIgnoringCase(m_string, other); } 101 102 const WTF::String& string() const { return m_string; } 103 104 JSStringRef createJSString() const 105 { 106 JSC::initializeThreading(); 107 return OpaqueJSString::create(m_string).leakRef(); 108 } 109 110private: 111 String() 112 : m_string() 113 { 114 } 115 116 String(const WTF::String& string) 117 : m_string(!string.impl() ? WTF::String(StringImpl::empty()) : string) 118 { 119 } 120 121 WTF::String m_string; 122}; 123 124} // namespace WebKit 125 126#endif // APIString_h 127