diff options
Diffstat (limited to 'Source/WebKit2/Shared/API/c/WKString.cpp')
-rw-r--r-- | Source/WebKit2/Shared/API/c/WKString.cpp | 74 |
1 files changed, 60 insertions, 14 deletions
diff --git a/Source/WebKit2/Shared/API/c/WKString.cpp b/Source/WebKit2/Shared/API/c/WKString.cpp index 5f13b9ca7..650367045 100644 --- a/Source/WebKit2/Shared/API/c/WKString.cpp +++ b/Source/WebKit2/Shared/API/c/WKString.cpp @@ -28,6 +28,9 @@ #include "WKStringPrivate.h" #include "WKAPICast.h" +#include <JavaScriptCore/InitializeThreading.h> +#include <JavaScriptCore/JSStringRef.h> +#include <JavaScriptCore/OpaqueJSString.h> using namespace WebKit; @@ -38,58 +41,101 @@ WKTypeID WKStringGetTypeID() WKStringRef WKStringCreateWithUTF8CString(const char* string) { - RefPtr<API::String> apiString = API::String::createFromUTF8String(string); - return toAPI(apiString.release().leakRef()); + return toAPI(&API::String::create(WTF::String::fromUTF8(string)).leakRef()); } bool WKStringIsEmpty(WKStringRef stringRef) { - return toImpl(stringRef)->isEmpty(); + return toImpl(stringRef)->stringView().isEmpty(); } size_t WKStringGetLength(WKStringRef stringRef) { - return toImpl(stringRef)->length(); + return toImpl(stringRef)->stringView().length(); } size_t WKStringGetCharacters(WKStringRef stringRef, WKChar* buffer, size_t bufferLength) { - COMPILE_ASSERT(sizeof(WKChar) == sizeof(UChar), WKStringGetCharacters_sizeof_WKChar_matches_UChar); - return (toImpl(stringRef)->getCharacters(static_cast<UChar*>(buffer), bufferLength)); + static_assert(sizeof(WKChar) == sizeof(UChar), "Size of WKChar must match size of UChar"); + + unsigned unsignedBufferLength = std::min<size_t>(bufferLength, std::numeric_limits<unsigned>::max()); + auto substring = toImpl(stringRef)->stringView().substring(0, unsignedBufferLength); + + substring.getCharactersWithUpconvert(reinterpret_cast<UChar*>(buffer)); + return substring.length(); } size_t WKStringGetMaximumUTF8CStringSize(WKStringRef stringRef) { - return toImpl(stringRef)->maximumUTF8CStringSize(); + return toImpl(stringRef)->stringView().length() * 3 + 1; +} + +enum StrictType { NonStrict = false, Strict = true }; + +template <StrictType strict> +size_t WKStringGetUTF8CStringImpl(WKStringRef stringRef, char* buffer, size_t bufferSize) +{ + if (!bufferSize) + return 0; + + auto stringView = toImpl(stringRef)->stringView(); + + char* p = buffer; + WTF::Unicode::ConversionResult result; + + if (stringView.is8Bit()) { + const LChar* characters = stringView.characters8(); + result = WTF::Unicode::convertLatin1ToUTF8(&characters, characters + stringView.length(), &p, p + bufferSize - 1); + } else { + const UChar* characters = stringView.characters16(); + result = WTF::Unicode::convertUTF16ToUTF8(&characters, characters + stringView.length(), &p, p + bufferSize - 1, strict); + } + + if (result != WTF::Unicode::conversionOK && result != WTF::Unicode::targetExhausted) + return 0; + + *p++ = '\0'; + return p - buffer; } size_t WKStringGetUTF8CString(WKStringRef stringRef, char* buffer, size_t bufferSize) { - return toImpl(stringRef)->getUTF8CString(buffer, bufferSize); + return WKStringGetUTF8CStringImpl<StrictType::Strict>(stringRef, buffer, bufferSize); +} + +size_t WKStringGetUTF8CStringNonStrict(WKStringRef stringRef, char* buffer, size_t bufferSize) +{ + return WKStringGetUTF8CStringImpl<StrictType::NonStrict>(stringRef, buffer, bufferSize); } bool WKStringIsEqual(WKStringRef aRef, WKStringRef bRef) { - return toImpl(aRef)->equal(toImpl(bRef)); + return toImpl(aRef)->stringView() == toImpl(bRef)->stringView(); } bool WKStringIsEqualToUTF8CString(WKStringRef aRef, const char* b) { - return toImpl(aRef)->equalToUTF8String(b); + // FIXME: Should we add a fast path that avoids memory allocation when the string is all ASCII? + // FIXME: We can do even the general case more efficiently if we write a function in StringView that understands UTF-8 C strings. + return toImpl(aRef)->stringView() == WTF::String::fromUTF8(b); } bool WKStringIsEqualToUTF8CStringIgnoringCase(WKStringRef aRef, const char* b) { - return toImpl(aRef)->equalToUTF8StringIgnoringCase(b); + // FIXME: Should we add a fast path that avoids memory allocation when the string is all ASCII? + // FIXME: We can do even the general case more efficiently if we write a function in StringView that understands UTF-8 C strings. + return equalIgnoringASCIICase(toImpl(aRef)->stringView(), WTF::String::fromUTF8(b)); } WKStringRef WKStringCreateWithJSString(JSStringRef jsStringRef) { - RefPtr<API::String> apiString = API::String::create(jsStringRef); - return toAPI(apiString.release().leakRef()); + auto apiString = jsStringRef ? API::String::create(jsStringRef->string()) : API::String::createNull(); + + return toAPI(&apiString.leakRef()); } JSStringRef WKStringCopyJSString(WKStringRef stringRef) { - return toImpl(stringRef)->createJSString(); + JSC::initializeThreading(); + return OpaqueJSString::create(toImpl(stringRef)->string()).leakRef(); } |