0

Use string_view whenever possible in WebString

Separating out ptr/data pairs is both less safe and less ergnomic. If we
keep values in paired up view types as much as possible, there's less
risk of lengths and data getting mixed up.

This also avoids needing separate overloads for all variations on
string. string_view (or optional<u16string_view> for FromUTF16) is a
sink type that can accept all variations.

Change-Id: I36c44b3f6052a62369ee351492dac7e542ec06ef
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4892056
Reviewed-by: Philip Rogers <pdr@chromium.org>
Owners-Override: Daniel Cheng <dcheng@chromium.org>
Reviewed-by: Daniel Cheng <dcheng@chromium.org>
Commit-Queue: David Benjamin <davidben@chromium.org>
Code-Coverage: findit-for-me@appspot.gserviceaccount.com <findit-for-me@appspot.gserviceaccount.com>
Cr-Commit-Position: refs/heads/main@{#1206605}
This commit is contained in:
David Benjamin
2023-10-06 20:30:51 +00:00
committed by Chromium LUCI CQ
parent 7076cb3b8a
commit c7d22adc6d
15 changed files with 100 additions and 126 deletions

@ -354,8 +354,8 @@ void PasswordGenerationAgentTest::LoadHTMLWithUserGesture(const char* html) {
WebElement PasswordGenerationAgentTest::GetElementById(
base::StringPiece element_id) {
WebDocument document = GetMainFrame()->GetDocument();
WebElement element = document.GetElementById(
blink::WebString::FromUTF8(element_id.data(), element_id.size()));
WebElement element =
document.GetElementById(blink::WebString::FromUTF8(element_id));
CHECK(!element.IsNull());
return element;
}
@ -435,8 +435,8 @@ void PasswordGenerationAgentTest::ExpectAttribute(
const WebElement& element,
base::StringPiece attribute,
base::StringPiece expected_value) {
WebString actual_value = element.GetAttribute(
blink::WebString::FromUTF8(attribute.data(), attribute.size()));
WebString actual_value =
element.GetAttribute(blink::WebString::FromUTF8(attribute));
ASSERT_FALSE(actual_value.IsNull());
EXPECT_EQ(expected_value, actual_value.Ascii());
}

@ -48,7 +48,7 @@ constexpr base::StringPiece kAutocomplete = "autocomplete";
template <const base::StringPiece& string>
const WebString& GetWebString() {
static const base::NoDestructor<WebString> web_string(
WebString::FromUTF8(string.data(), string.length()));
WebString::FromUTF8(string));
return *web_string;
}
@ -77,9 +77,7 @@ void MaybeAppendAriaLabelledByDevtoolsIssue(
base::kWhitespaceUTF16, base::KEEP_WHITESPACE,
base::SPLIT_WANT_NONEMPTY),
[&](const auto& id) {
return element.GetDocument()
.GetElementById(WebString(id.data(), id.length()))
.IsNull();
return element.GetDocument().GetElementById(WebString(id)).IsNull();
})) {
form_issues.emplace_back(
GenericIssueErrorType::kFormAriaLabelledByToNonExistingId,

@ -181,7 +181,7 @@ constexpr base::StringPiece kValue = "value";
template <const base::StringPiece& string>
const WebString& GetWebString() {
static const base::NoDestructor<WebString> web_string(
WebString::FromUTF8(string.data(), string.length()));
WebString::FromUTF8(string));
return *web_string;
}
@ -1720,8 +1720,7 @@ std::vector<WebElement> GetWebElementsFromIdList(const WebDocument& document,
for (const auto& id : base::SplitStringPiece(
id_list_utf16, base::kWhitespaceUTF16, base::KEEP_WHITESPACE,
base::SPLIT_WANT_NONEMPTY)) {
web_elements.push_back(
document.GetElementById(WebString(id.data(), id.length())));
web_elements.push_back(document.GetElementById(WebString(id)));
}
return web_elements;
}
@ -2894,9 +2893,7 @@ void MaybeEmitAriaLabelledByDevtoolsIssue(const WebElement& element,
base::KEEP_WHITESPACE,
base::SPLIT_WANT_NONEMPTY),
[&](const auto& id) {
return element.GetDocument()
.GetElementById(WebString(id.data(), id.length()))
.IsNull();
return element.GetDocument().GetElementById(WebString(id)).IsNull();
})) {
element.GetDocument().GetFrame()->AddGenericIssue(
blink::mojom::GenericIssueErrorType::kFormAriaLabelledByToNonExistingId,

@ -556,8 +556,8 @@ class PrintRenderFrameHelperTestBase : public content::RenderViewTest {
}
void OnPrintPagesInFrame(base::StringPiece frame_name) {
blink::WebFrame* frame = GetMainFrame()->FindFrameByName(
blink::WebString::FromUTF8(frame_name.data(), frame_name.size()));
blink::WebFrame* frame =
GetMainFrame()->FindFrameByName(blink::WebString::FromUTF8(frame_name));
ASSERT_TRUE(frame);
content::RenderFrame* render_frame =
content::RenderFrame::FromWebFrame(frame->ToWebLocalFrame());

@ -103,9 +103,9 @@ bool CanonicalizeCssSelectors(v8::Local<v8::Context> context,
v8::String::Utf8Value selector(isolate, val.As<v8::String>());
// Note: See the TODO in css_natives_handler.cc.
std::string parsed =
blink::CanonicalizeSelector(
blink::WebString::FromUTF8(*selector, selector.length()),
blink::kWebSelectorTypeCompound)
blink::CanonicalizeSelector(blink::WebString::FromUTF8(std::string_view(
*selector, selector.length())),
blink::kWebSelectorTypeCompound)
.Utf8();
if (parsed.empty()) {
*error =

@ -255,8 +255,7 @@ blink::WebString UserScriptSet::GetJsSource(const UserScript::Content& file,
base::StrCat({kUserScriptHead, script_content, kUserScriptTail});
source = blink::WebString::FromUTF8(content);
} else {
source = blink::WebString::FromUTF8(script_content.data(),
script_content.length());
source = blink::WebString::FromUTF8(script_content);
}
script_sources_[url] = source;
return source;
@ -270,9 +269,7 @@ blink::WebString UserScriptSet::GetCssSource(const UserScript::Content& file) {
base::StringPiece script_content = file.GetContent();
return script_sources_
.insert(std::make_pair(
url, blink::WebString::FromUTF8(script_content.data(),
script_content.length())))
.insert(std::make_pair(url, blink::WebString::FromUTF8(script_content)))
.first->second;
}

@ -346,10 +346,8 @@ class PdfViewWebPluginWithoutInitializeTest
static void AddToPluginParams(base::StringPiece name,
base::StringPiece value,
blink::WebPluginParams& params) {
params.attribute_names.push_back(
blink::WebString::FromUTF8(name.data(), name.size()));
params.attribute_values.push_back(
blink::WebString::FromUTF8(value.data(), value.size()));
params.attribute_names.push_back(blink::WebString::FromUTF8(name));
params.attribute_values.push_back(blink::WebString::FromUTF8(value));
}
void SetUpPlugin(base::StringPiece document_url,

@ -34,6 +34,7 @@
#include <cstring>
#include <limits>
#include <string>
#include <string_view>
#include "base/memory/scoped_refptr.h"
#include "base/strings/latin1_string_conversions.h"
@ -53,12 +54,10 @@ namespace blink {
// Use either one of static methods to convert ASCII, Latin1, UTF-8 or
// UTF-16 string into WebString:
//
// * WebString::FromASCII(const std::string& ascii)
// * WebString::FromLatin1(const std::string& latin1)
// * WebString::FromUTF8(const std::string& utf8)
// * WebString::FromUTF16(const char16_t* utf16)
// * WebString::FromUTF16(const std::u16string& utf16)
// * WebString::FromUTF16(const absl::optional<std::u16string>& utf16)
// * WebString::FromASCII(std::string_view ascii)
// * WebString::FromLatin1(std::string_view latin1)
// * WebString::FromUTF8(std::string_view utf8)
// * WebString::FromUTF16(absl::optional<std::u16string_view> utf16)
//
// Similarly, use either of following methods to convert WebString to
// ASCII, Latin1, UTF-8 or UTF-16:
@ -98,7 +97,7 @@ class BLINK_PLATFORM_EXPORT WebString {
~WebString();
WebString();
WebString(const WebUChar* data, size_t len);
explicit WebString(std::u16string_view s);
WebString(const WebString&);
WebString(WebString&&);
@ -109,9 +108,10 @@ class BLINK_PLATFORM_EXPORT WebString {
void Reset();
bool Equals(const WebString&) const;
bool Equals(const char* characters, size_t len) const;
bool Equals(std::string_view characters) const;
bool Equals(const char* characters) const {
return Equals(characters, characters ? std::strlen(characters) : 0);
return Equals(
characters ? std::string_view(characters) : std::string_view());
}
size_t length() const;
@ -124,18 +124,13 @@ class BLINK_PLATFORM_EXPORT WebString {
WebString Substring(size_t pos,
size_t len = std::numeric_limits<size_t>::max()) const;
static WebString FromUTF8(const char* data, size_t length);
static WebString FromUTF8(const std::string& s) {
return FromUTF8(s.data(), s.length());
}
static WebString FromUTF8(std::string_view s);
std::u16string Utf16() const {
return base::Latin1OrUTF16ToUTF16(length(), Data8(), Data16());
}
static WebString FromUTF16(const char16_t*);
static WebString FromUTF16(const std::u16string&);
static WebString FromUTF16(const absl::optional<std::u16string>&);
static WebString FromUTF16(absl::optional<std::u16string_view>);
static absl::optional<std::u16string> ToOptionalString16(const WebString& s) {
return s.IsNull() ? absl::nullopt : absl::make_optional(s.Utf16());
@ -143,11 +138,7 @@ class BLINK_PLATFORM_EXPORT WebString {
std::string Latin1() const;
static WebString FromLatin1(const WebLChar* data, size_t length);
static WebString FromLatin1(const std::string& s) {
return FromLatin1(reinterpret_cast<const WebLChar*>(s.data()), s.length());
}
static WebString FromLatin1(std::string_view s);
// This asserts if the string contains non-ascii characters.
// Use this rather than calling base::UTF16ToASCII() which always incurs
@ -158,14 +149,15 @@ class BLINK_PLATFORM_EXPORT WebString {
bool ContainsOnlyASCII() const;
// Does same as FromLatin1 but asserts if the given string has non-ascii char.
static WebString FromASCII(const std::string&);
static WebString FromASCII(std::string_view);
template <int N>
WebString(const char (&data)[N]) : WebString(FromUTF8(data, N - 1)) {}
WebString(const char (&data)[N])
: WebString(FromUTF8(std::string_view(data, N - 1))) {}
template <int N>
WebString& operator=(const char (&data)[N]) {
*this = FromUTF8(data, N - 1);
*this = FromUTF8(std::string_view(data, N - 1));
return *this;
}

@ -196,9 +196,8 @@ TEST(V8ScriptValueSerializerForModulesTest, RoundTripRTCCertificate) {
// Make a certificate with the existing key above.
rtc::scoped_refptr<rtc::RTCCertificate> web_certificate =
certificate_generator->FromPEM(
WebString::FromUTF8(kEcdsaPrivateKey, sizeof(kEcdsaPrivateKey)),
WebString::FromUTF8(kEcdsaCertificate, sizeof(kEcdsaCertificate)));
certificate_generator->FromPEM(WebString::FromUTF8(kEcdsaPrivateKey),
WebString::FromUTF8(kEcdsaCertificate));
ASSERT_TRUE(web_certificate);
RTCCertificate* certificate =
MakeGarbageCollected<RTCCertificate>(std::move(web_certificate));

@ -1181,8 +1181,8 @@ TEST_F(WebViewTest, FinishComposingTextDoesNotAssert) {
std::string composition_text("hello");
WebVector<ui::ImeTextSpan> empty_ime_text_spans;
active_input_method_controller->SetComposition(
WebString::FromUTF8(composition_text.c_str()), empty_ime_text_spans,
WebRange(), 5, 5);
WebString::FromUTF8(composition_text), empty_ime_text_spans, WebRange(),
5, 5);
// Do arbitrary change to make layout dirty.
Document& document = *web_view->MainFrameImpl()->GetFrame()->GetDocument();
@ -1239,8 +1239,8 @@ TEST_F(WebViewTest, FinishComposingTextCursorPositionChange) {
->GetActiveWebInputMethodController();
WebVector<ui::ImeTextSpan> empty_ime_text_spans;
active_input_method_controller->SetComposition(
WebString::FromUTF8(composition_text.c_str()), empty_ime_text_spans,
WebRange(), 3, 3);
WebString::FromUTF8(composition_text), empty_ime_text_spans, WebRange(),
3, 3);
WebTextInputInfo info = active_input_method_controller->TextInputInfo();
EXPECT_EQ("hello", info.value.Utf8());
@ -1258,8 +1258,8 @@ TEST_F(WebViewTest, FinishComposingTextCursorPositionChange) {
EXPECT_EQ(-1, info.composition_end);
active_input_method_controller->SetComposition(
WebString::FromUTF8(composition_text.c_str()), empty_ime_text_spans,
WebRange(), 3, 3);
WebString::FromUTF8(composition_text), empty_ime_text_spans, WebRange(),
3, 3);
info = active_input_method_controller->TextInputInfo();
EXPECT_EQ("helhellolo", info.value.Utf8());
EXPECT_EQ(6, info.selection_start);
@ -1305,8 +1305,8 @@ TEST_F(WebViewTest, SetCompositionForNewCaretPositions) {
// Caret is on the left of composing text.
active_input_method_controller->SetComposition(
WebString::FromUTF8(composition_text.c_str()), empty_ime_text_spans,
WebRange(), 0, 0);
WebString::FromUTF8(composition_text), empty_ime_text_spans, WebRange(),
0, 0);
info = active_input_method_controller->TextInputInfo();
EXPECT_EQ("helloABCworld", info.value.Utf8());
EXPECT_EQ(5, info.selection_start);
@ -1316,8 +1316,8 @@ TEST_F(WebViewTest, SetCompositionForNewCaretPositions) {
// Caret is on the right of composing text.
active_input_method_controller->SetComposition(
WebString::FromUTF8(composition_text.c_str()), empty_ime_text_spans,
WebRange(), 3, 3);
WebString::FromUTF8(composition_text), empty_ime_text_spans, WebRange(),
3, 3);
info = active_input_method_controller->TextInputInfo();
EXPECT_EQ("helloABCworld", info.value.Utf8());
EXPECT_EQ(8, info.selection_start);
@ -1327,8 +1327,8 @@ TEST_F(WebViewTest, SetCompositionForNewCaretPositions) {
// Caret is between composing text and left boundary.
active_input_method_controller->SetComposition(
WebString::FromUTF8(composition_text.c_str()), empty_ime_text_spans,
WebRange(), -2, -2);
WebString::FromUTF8(composition_text), empty_ime_text_spans, WebRange(),
-2, -2);
info = active_input_method_controller->TextInputInfo();
EXPECT_EQ("helloABCworld", info.value.Utf8());
EXPECT_EQ(3, info.selection_start);
@ -1338,8 +1338,8 @@ TEST_F(WebViewTest, SetCompositionForNewCaretPositions) {
// Caret is between composing text and right boundary.
active_input_method_controller->SetComposition(
WebString::FromUTF8(composition_text.c_str()), empty_ime_text_spans,
WebRange(), 5, 5);
WebString::FromUTF8(composition_text), empty_ime_text_spans, WebRange(),
5, 5);
info = active_input_method_controller->TextInputInfo();
EXPECT_EQ("helloABCworld", info.value.Utf8());
EXPECT_EQ(10, info.selection_start);
@ -1349,8 +1349,8 @@ TEST_F(WebViewTest, SetCompositionForNewCaretPositions) {
// Caret is on the left boundary.
active_input_method_controller->SetComposition(
WebString::FromUTF8(composition_text.c_str()), empty_ime_text_spans,
WebRange(), -5, -5);
WebString::FromUTF8(composition_text), empty_ime_text_spans, WebRange(),
-5, -5);
info = active_input_method_controller->TextInputInfo();
EXPECT_EQ("helloABCworld", info.value.Utf8());
EXPECT_EQ(0, info.selection_start);
@ -1360,8 +1360,8 @@ TEST_F(WebViewTest, SetCompositionForNewCaretPositions) {
// Caret is on the right boundary.
active_input_method_controller->SetComposition(
WebString::FromUTF8(composition_text.c_str()), empty_ime_text_spans,
WebRange(), 8, 8);
WebString::FromUTF8(composition_text), empty_ime_text_spans, WebRange(),
8, 8);
info = active_input_method_controller->TextInputInfo();
EXPECT_EQ("helloABCworld", info.value.Utf8());
EXPECT_EQ(13, info.selection_start);
@ -1371,8 +1371,8 @@ TEST_F(WebViewTest, SetCompositionForNewCaretPositions) {
// Caret exceeds the left boundary.
active_input_method_controller->SetComposition(
WebString::FromUTF8(composition_text.c_str()), empty_ime_text_spans,
WebRange(), -100, -100);
WebString::FromUTF8(composition_text), empty_ime_text_spans, WebRange(),
-100, -100);
info = active_input_method_controller->TextInputInfo();
EXPECT_EQ("helloABCworld", info.value.Utf8());
EXPECT_EQ(0, info.selection_start);
@ -1382,8 +1382,8 @@ TEST_F(WebViewTest, SetCompositionForNewCaretPositions) {
// Caret exceeds the right boundary.
active_input_method_controller->SetComposition(
WebString::FromUTF8(composition_text.c_str()), empty_ime_text_spans,
WebRange(), 100, 100);
WebString::FromUTF8(composition_text), empty_ime_text_spans, WebRange(),
100, 100);
info = active_input_method_controller->TextInputInfo();
EXPECT_EQ("helloABCworld", info.value.Utf8());
EXPECT_EQ(13, info.selection_start);
@ -1641,8 +1641,8 @@ TEST_F(WebViewTest, InsertNewLinePlacementAfterFinishComposingText) {
std::string composition_text("\n");
active_input_method_controller->CommitText(
WebString::FromUTF8(composition_text.c_str()), empty_ime_text_spans,
WebRange(), 0);
WebString::FromUTF8(composition_text), empty_ime_text_spans, WebRange(),
0);
info = active_input_method_controller->TextInputInfo();
EXPECT_EQ(5, info.selection_start);
EXPECT_EQ(5, info.selection_end);
@ -1759,8 +1759,7 @@ TEST_F(WebViewTest, SetCompositionFromExistingTextInTextArea) {
std::string new_line_text("\n");
WebVector<ui::ImeTextSpan> empty_ime_text_spans;
active_input_method_controller->CommitText(
WebString::FromUTF8(new_line_text.c_str()), empty_ime_text_spans,
WebRange(), 0);
WebString::FromUTF8(new_line_text), empty_ime_text_spans, WebRange(), 0);
WebTextInputInfo info = active_input_method_controller->TextInputInfo();
EXPECT_EQ("0123456789abcdefghijklmnopq\nrstuvwxyz", info.value.Utf8());
@ -1775,8 +1774,8 @@ TEST_F(WebViewTest, SetCompositionFromExistingTextInTextArea) {
std::string composition_text("yolo");
active_input_method_controller->CommitText(
WebString::FromUTF8(composition_text.c_str()), empty_ime_text_spans,
WebRange(), 0);
WebString::FromUTF8(composition_text), empty_ime_text_spans, WebRange(),
0);
info = active_input_method_controller->TextInputInfo();
EXPECT_EQ("0123456789abcdefghijklmnopq\nrsyoloxyz", info.value.Utf8());
EXPECT_EQ(34, info.selection_start);
@ -1817,11 +1816,11 @@ TEST_F(WebViewTest, SetEditableSelectionOffsetsKeepsComposition) {
->FrameWidget()
->GetActiveWebInputMethodController();
active_input_method_controller->CommitText(
WebString::FromUTF8(composition_text_first.c_str()), empty_ime_text_spans,
WebString::FromUTF8(composition_text_first), empty_ime_text_spans,
WebRange(), 0);
active_input_method_controller->SetComposition(
WebString::FromUTF8(composition_text_second.c_str()),
empty_ime_text_spans, WebRange(), 5, 5);
WebString::FromUTF8(composition_text_second), empty_ime_text_spans,
WebRange(), 5, 5);
WebTextInputInfo info = active_input_method_controller->TextInputInfo();
EXPECT_EQ("hello world", info.value.Utf8());
@ -3993,8 +3992,8 @@ TEST_F(WebViewTest, FinishComposingTextDoesntTriggerAutofillTextChange) {
WebVector<ui::ImeTextSpan> empty_ime_text_spans;
active_input_method_controller->SetComposition(
WebString::FromUTF8(composition_text.c_str()), empty_ime_text_spans,
WebRange(), 0, static_cast<int>(composition_text.length()));
WebString::FromUTF8(composition_text), empty_ime_text_spans, WebRange(),
0, static_cast<int>(composition_text.length()));
WebTextInputInfo info = active_input_method_controller->TextInputInfo();
EXPECT_EQ(0, info.selection_start);
@ -4849,8 +4848,8 @@ TEST_F(WebViewTest, CompositionIsUserGesture) {
EXPECT_EQ(0, client.TextChanges());
EXPECT_TRUE(
frame->FrameWidget()->GetActiveWebInputMethodController()->SetComposition(
WebString::FromUTF8(std::string("hello").c_str()),
WebVector<ui::ImeTextSpan>(), WebRange(), 3, 3));
WebString::FromUTF8("hello"), WebVector<ui::ImeTextSpan>(),
WebRange(), 3, 3));
EXPECT_TRUE(frame->HasTransientUserActivation());
EXPECT_EQ(1, client.TextChanges());
EXPECT_TRUE(frame->HasMarkedText());
@ -5280,8 +5279,7 @@ TEST_F(WebViewTest, PasswordFieldEditingIsUserGesture) {
EXPECT_EQ(0, client.TextChanges());
EXPECT_TRUE(
frame->FrameWidget()->GetActiveWebInputMethodController()->CommitText(
WebString::FromUTF8(std::string("hello").c_str()),
empty_ime_text_spans, WebRange(), 0));
WebString::FromUTF8("hello"), empty_ime_text_spans, WebRange(), 0));
EXPECT_TRUE(frame->HasTransientUserActivation());
EXPECT_EQ(1, client.TextChanges());
frame->SetAutofillClient(nullptr);

@ -79,7 +79,7 @@ class LocalFrameClientImplTest : public testing::Test {
WebString UserAgent() {
// The test always returns the same user agent .
std::string user_agent = GetLocalFrameClient().UserAgent().Utf8();
return WebString::FromUTF8(user_agent.c_str(), user_agent.length());
return WebString::FromUTF8(user_agent);
}
WebLocalFrameImpl* MainFrame() { return helper_.LocalMainFrame(); }

@ -2,6 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <string_view>
#include "third_party/blink/public/platform/web_icon_sizes_parser.h"
#include "third_party/blink/public/platform/web_string.h"
#include "third_party/blink/renderer/platform/testing/blink_fuzzer_test_support.h"
@ -12,8 +14,8 @@ namespace blink {
// Fuzzer for blink::MHTMLParser.
int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
static BlinkFuzzerTestSupport test_support = BlinkFuzzerTestSupport();
WebString string =
WebString::FromUTF8(reinterpret_cast<const char*>(data), size);
WebString string = WebString::FromUTF8(
std::string_view(reinterpret_cast<const char*>(data), size));
WebIconSizesParser::ParseIconSizes(string);
return 0;
}

@ -55,10 +55,10 @@ WebString::WebString(WebString&&) = default;
WebString& WebString::operator=(const WebString&) = default;
WebString& WebString::operator=(WebString&&) = default;
WebString::WebString(const WebUChar* data, size_t len)
WebString::WebString(std::u16string_view s)
: impl_(StringImpl::Create8BitIfPossible(
data,
base::checked_cast<wtf_size_t>(len))) {}
s.data(),
base::checked_cast<wtf_size_t>(s.length()))) {}
void WebString::Reset() {
impl_ = nullptr;
@ -89,30 +89,24 @@ WebString WebString::Substring(size_t pos, size_t len) const {
base::checked_cast<wtf_size_t>(len)));
}
WebString WebString::FromUTF8(const char* data, size_t length) {
return String::FromUTF8(data, length);
WebString WebString::FromUTF8(std::string_view s) {
return String::FromUTF8(s.data(), s.length());
}
WebString WebString::FromUTF16(const char16_t* s) {
return WebString(s, std::char_traits<char16_t>::length(s));
}
WebString WebString::FromUTF16(const std::u16string& s) {
return WebString(s.data(), s.length());
}
WebString WebString::FromUTF16(const absl::optional<std::u16string>& s) {
if (!s.has_value())
WebString WebString::FromUTF16(absl::optional<std::u16string_view> s) {
if (!s.has_value()) {
return WebString();
return WebString(s->data(), s->length());
}
return WebString(*s);
}
std::string WebString::Latin1() const {
return String(impl_).Latin1();
}
WebString WebString::FromLatin1(const WebLChar* data, size_t length) {
return String(data, base::checked_cast<wtf_size_t>(length));
WebString WebString::FromLatin1(std::string_view s) {
return String(reinterpret_cast<const WebLChar*>(s.data()),
base::checked_cast<wtf_size_t>(s.length()));
}
std::string WebString::Ascii() const {
@ -134,7 +128,7 @@ bool WebString::ContainsOnlyASCII() const {
return String(impl_).ContainsOnlyASCIIOrEmpty();
}
WebString WebString::FromASCII(const std::string& s) {
WebString WebString::FromASCII(std::string_view s) {
DCHECK(base::IsStringASCII(s));
return FromLatin1(s);
}
@ -143,8 +137,9 @@ bool WebString::Equals(const WebString& s) const {
return Equal(impl_.get(), s.impl_.get());
}
bool WebString::Equals(const char* characters, size_t length) const {
return Equal(impl_.get(), characters, base::checked_cast<wtf_size_t>(length));
bool WebString::Equals(std::string_view characters) const {
return Equal(impl_.get(), characters.data(),
base::checked_cast<wtf_size_t>(characters.length()));
}
bool WebString::operator<(const WebString& other) const {

@ -12,17 +12,16 @@ namespace blink {
TEST(WebStringTest, UTF8ConversionRoundTrip) {
// Valid characters.
for (WebUChar uchar = 0; uchar <= 0xD7FF; ++uchar) {
WebString utf16_string(&uchar, 1);
WebString utf16_string(std::u16string_view(&uchar, 1));
std::string utf8_string(utf16_string.Utf8());
WebString utf16_new_string =
WebString::FromUTF8(utf8_string.data(), utf8_string.length());
WebString utf16_new_string = WebString::FromUTF8(utf8_string);
EXPECT_FALSE(utf16_new_string.IsNull());
EXPECT_TRUE(utf16_string == utf16_new_string);
}
// Unpaired surrogates.
for (WebUChar uchar = 0xD800; uchar <= 0xDFFF; ++uchar) {
WebString utf16_string(&uchar, 1);
WebString utf16_string(std::u16string_view(&uchar, 1));
// Conversion with Strict mode results in an empty string.
std::string utf8_string(
@ -32,16 +31,14 @@ TEST(WebStringTest, UTF8ConversionRoundTrip) {
// Unpaired surrogates can't be converted back in Lenient mode.
utf8_string = utf16_string.Utf8(WebString::UTF8ConversionMode::kLenient);
EXPECT_FALSE(utf8_string.empty());
WebString utf16_new_string =
WebString::FromUTF8(utf8_string.data(), utf8_string.length());
WebString utf16_new_string = WebString::FromUTF8(utf8_string);
EXPECT_TRUE(utf16_new_string.IsNull());
// Round-trip works with StrictReplacingErrorsWithFFFD mode.
utf8_string = utf16_string.Utf8(
WebString::UTF8ConversionMode::kStrictReplacingErrorsWithFFFD);
EXPECT_FALSE(utf8_string.empty());
utf16_new_string =
WebString::FromUTF8(utf8_string.data(), utf8_string.length());
utf16_new_string = WebString::FromUTF8(utf8_string);
EXPECT_FALSE(utf16_new_string.IsNull());
EXPECT_FALSE(utf16_string == utf16_new_string);
}

@ -135,9 +135,10 @@ TEST(WebVectorTest, EmplaceBackArgumentForwarding) {
WebVector<WebString> vector;
vector.reserve(1);
WebUChar buffer[] = {'H', 'e', 'l', 'l', 'o', ' ', 'b', 'l', 'i', 'n', 'k'};
vector.emplace_back(buffer, std::size(buffer));
std::u16string_view view(buffer, std::size(buffer));
vector.emplace_back(view);
ASSERT_EQ(1U, vector.size());
EXPECT_EQ(WebString(buffer, std::size(buffer)), vector[0]);
EXPECT_EQ(WebString(view), vector[0]);
}
TEST(WebVectorTest, EmplaceBackElementPlacement) {