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#include "config.h"
27#include "WKString.h"
28#include "WKStringPrivate.h"
29
30#include "WKAPICast.h"
31#include <JavaScriptCore/InitializeThreading.h>
32#include <JavaScriptCore/OpaqueJSString.h>
33#include <wtf/unicode/UTF8Conversion.h>
34
35WKTypeID WKStringGetTypeID()
36{
37 return WebKit::toAPI(API::String::APIType);
38}
39
40WKStringRef WKStringCreateWithUTF8CString(const char* string)
41{
42 return WebKit::toAPI(&API::String::create(WTF::String::fromUTF8(string)).leakRef());
43}
44
45bool WKStringIsEmpty(WKStringRef stringRef)
46{
47 return WebKit::toImpl(stringRef)->stringView().isEmpty();
48}
49
50size_t WKStringGetLength(WKStringRef stringRef)
51{
52 return WebKit::toImpl(stringRef)->stringView().length();
53}
54
55size_t WKStringGetCharacters(WKStringRef stringRef, WKChar* buffer, size_t bufferLength)
56{
57 static_assert(sizeof(WKChar) == sizeof(UChar), "Size of WKChar must match size of UChar");
58
59 unsigned unsignedBufferLength = std::min<size_t>(bufferLength, std::numeric_limits<unsigned>::max());
60 auto substring = WebKit::toImpl(stringRef)->stringView().substring(0, unsignedBufferLength);
61
62 substring.getCharactersWithUpconvert(reinterpret_cast<UChar*>(buffer));
63 return substring.length();
64}
65
66size_t WKStringGetMaximumUTF8CStringSize(WKStringRef stringRef)
67{
68 return WebKit::toImpl(stringRef)->stringView().length() * 3 + 1;
69}
70
71enum StrictType { NonStrict = false, Strict = true };
72
73template <StrictType strict>
74size_t WKStringGetUTF8CStringImpl(WKStringRef stringRef, char* buffer, size_t bufferSize)
75{
76 if (!bufferSize)
77 return 0;
78
79 auto stringView = WebKit::toImpl(stringRef)->stringView();
80
81 char* p = buffer;
82
83 if (stringView.is8Bit()) {
84 const LChar* characters = stringView.characters8();
85 if (!WTF::Unicode::convertLatin1ToUTF8(&characters, characters + stringView.length(), &p, p + bufferSize - 1))
86 return 0;
87 } else {
88 const UChar* characters = stringView.characters16();
89 auto result = WTF::Unicode::convertUTF16ToUTF8(&characters, characters + stringView.length(), &p, p + bufferSize - 1, strict);
90 if (result != WTF::Unicode::ConversionOK && result != WTF::Unicode::TargetExhausted)
91 return 0;
92 }
93
94 *p++ = '\0';
95 return p - buffer;
96}
97
98size_t WKStringGetUTF8CString(WKStringRef stringRef, char* buffer, size_t bufferSize)
99{
100 return WKStringGetUTF8CStringImpl<StrictType::Strict>(stringRef, buffer, bufferSize);
101}
102
103size_t WKStringGetUTF8CStringNonStrict(WKStringRef stringRef, char* buffer, size_t bufferSize)
104{
105 return WKStringGetUTF8CStringImpl<StrictType::NonStrict>(stringRef, buffer, bufferSize);
106}
107
108bool WKStringIsEqual(WKStringRef aRef, WKStringRef bRef)
109{
110 return WebKit::toImpl(aRef)->stringView() == WebKit::toImpl(bRef)->stringView();
111}
112
113bool WKStringIsEqualToUTF8CString(WKStringRef aRef, const char* b)
114{
115 // FIXME: Should we add a fast path that avoids memory allocation when the string is all ASCII?
116 // FIXME: We can do even the general case more efficiently if we write a function in StringView that understands UTF-8 C strings.
117 return WebKit::toImpl(aRef)->stringView() == WTF::String::fromUTF8(b);
118}
119
120bool WKStringIsEqualToUTF8CStringIgnoringCase(WKStringRef aRef, const char* b)
121{
122 // FIXME: Should we add a fast path that avoids memory allocation when the string is all ASCII?
123 // FIXME: We can do even the general case more efficiently if we write a function in StringView that understands UTF-8 C strings.
124 return equalIgnoringASCIICase(WebKit::toImpl(aRef)->stringView(), WTF::String::fromUTF8(b));
125}
126
127WKStringRef WKStringCreateWithJSString(JSStringRef jsStringRef)
128{
129 auto apiString = jsStringRef ? API::String::create(jsStringRef->string()) : API::String::createNull();
130
131 return WebKit::toAPI(&apiString.leakRef());
132}
133
134JSStringRef WKStringCopyJSString(WKStringRef stringRef)
135{
136 JSC::initializeThreading();
137 return OpaqueJSString::tryCreate(WebKit::toImpl(stringRef)->string()).leakRef();
138}
139