1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
|
/*
* Copyright (C) 2010 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef APIString_h
#define APIString_h
#include "APIObject.h"
#include <JavaScriptCore/InitializeThreading.h>
#include <JavaScriptCore/JSStringRef.h>
#include <JavaScriptCore/OpaqueJSString.h>
#include <wtf/PassRefPtr.h>
#include <wtf/text/StringView.h>
#include <wtf/text/WTFString.h>
#include <wtf/unicode/UTF8.h>
namespace API {
class String final : public ObjectImpl<Object::Type::String> {
public:
static PassRefPtr<String> createNull()
{
return adoptRef(new String);
}
static PassRefPtr<String> create(const WTF::String& string)
{
return adoptRef(new String(string));
}
static PassRefPtr<String> create(JSStringRef jsStringRef)
{
return adoptRef(new String(jsStringRef->string()));
}
static PassRefPtr<String> createFromUTF8String(const char* string)
{
return adoptRef(new String(WTF::String::fromUTF8(string)));
}
virtual ~String()
{
}
bool isNull() const { return m_string.isNull(); }
bool isEmpty() const { return m_string.isEmpty(); }
size_t length() const { return m_string.length(); }
size_t getCharacters(UChar* buffer, size_t bufferLength) const
{
unsigned unsignedBufferLength = std::min<size_t>(bufferLength, std::numeric_limits<unsigned>::max());
StringView substring = StringView(m_string).substring(0, unsignedBufferLength);
substring.getCharactersWithUpconvert(buffer);
return substring.length();
}
size_t maximumUTF8CStringSize() const { return m_string.length() * 3 + 1; }
size_t getUTF8CString(char* buffer, size_t bufferSize)
{
if (!bufferSize)
return 0;
char* p = buffer;
WTF::Unicode::ConversionResult result;
if (m_string.is8Bit()) {
const LChar* characters = m_string.characters8();
result = WTF::Unicode::convertLatin1ToUTF8(&characters, characters + m_string.length(), &p, p + bufferSize - 1);
} else {
const UChar* characters = m_string.characters16();
result = WTF::Unicode::convertUTF16ToUTF8(&characters, characters + m_string.length(), &p, p + bufferSize - 1, /* strict */ true);
}
if (result != WTF::Unicode::conversionOK && result != WTF::Unicode::targetExhausted)
return 0;
*p++ = '\0';
return p - buffer;
}
bool equal(String* other) { return m_string == other->m_string; }
bool equalToUTF8String(const char* other) { return m_string == WTF::String::fromUTF8(other); }
bool equalToUTF8StringIgnoringCase(const char* other) { return equalIgnoringCase(m_string, other); }
const WTF::String& string() const { return m_string; }
JSStringRef createJSString() const
{
JSC::initializeThreading();
return OpaqueJSString::create(m_string).leakRef();
}
private:
String()
: m_string()
{
}
String(const WTF::String& string)
: m_string(!string.impl() ? WTF::String(StringImpl::empty()) : string)
{
}
WTF::String m_string;
};
} // namespace WebKit
#endif // APIString_h
|