0

Spanification: Add String{Impl}::SpanUint16()

We sometimes want span<uint16_t> for 16bit strings.
This CL has no behavior changes.

Bug: 351564777
Change-Id: I20e6a75a60466cc89192da454d4c732690c804b7
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/6437946
Reviewed-by: Fredrik Söderquist <fs@opera.com>
Commit-Queue: Kent Tamura <tkent@chromium.org>
Auto-Submit: Kent Tamura <tkent@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1444975}
This commit is contained in:
Kent Tamura
2025-04-09 15:05:15 -07:00
committed by Chromium LUCI CQ
parent 7547186d54
commit 4f3b5c4026
7 changed files with 26 additions and 28 deletions

@ -518,14 +518,10 @@ protocol::Response InspectorLayerTreeAgent::snapshotCommandLog(
const String& json = snapshot->SnapshotCommandLog()->ToJSONString(); const String& json = snapshot->SnapshotCommandLog()->ToJSONString();
std::vector<uint8_t> cbor; std::vector<uint8_t> cbor;
if (json.Is8Bit()) { if (json.Is8Bit()) {
crdtp::json::ConvertJSONToCBOR( crdtp::json::ConvertJSONToCBOR(crdtp::span<uint8_t>(json.Span8()), &cbor);
crdtp::span<uint8_t>(json.Characters8(), json.length()), &cbor);
} else { } else {
crdtp::json::ConvertJSONToCBOR( crdtp::json::ConvertJSONToCBOR(crdtp::span<uint16_t>(json.SpanUint16()),
crdtp::span<uint16_t>( &cbor);
reinterpret_cast<const uint16_t*>(json.Characters16()),
json.length()),
&cbor);
} }
auto log_value = protocol::Value::parseBinary(cbor.data(), cbor.size()); auto log_value = protocol::Value::parseBinary(cbor.data(), cbor.size());
*command_log = protocol::ValueConversions< *command_log = protocol::ValueConversions<

@ -18,14 +18,10 @@ using protocol::Value;
static std::unique_ptr<protocol::Value> ParseJSON(const String& string) { static std::unique_ptr<protocol::Value> ParseJSON(const String& string) {
std::vector<uint8_t> cbor; std::vector<uint8_t> cbor;
if (string.Is8Bit()) { if (string.Is8Bit()) {
crdtp::json::ConvertJSONToCBOR( crdtp::json::ConvertJSONToCBOR(crdtp::span<uint8_t>(string.Span8()), &cbor);
crdtp::span<uint8_t>(string.Characters8(), string.length()), &cbor);
} else { } else {
crdtp::json::ConvertJSONToCBOR( crdtp::json::ConvertJSONToCBOR(crdtp::span<uint16_t>(string.SpanUint16()),
crdtp::span<uint16_t>( &cbor);
reinterpret_cast<const uint16_t*>(string.Characters16()),
string.length()),
&cbor);
} }
return protocol::Value::parseBinary(cbor.data(), cbor.size()); return protocol::Value::parseBinary(cbor.data(), cbor.size());
} }

@ -165,18 +165,11 @@ void ProtocolTypeTraits<WTF::String>::Serialize(const String& value,
return; return;
} }
if (value.Is8Bit()) { if (value.Is8Bit()) {
crdtp::cbor::EncodeFromLatin1( crdtp::cbor::EncodeFromLatin1(crdtp::span<uint8_t>(value.Span8()), bytes);
crdtp::span<uint8_t>(
reinterpret_cast<const uint8_t*>(value.Characters8()),
value.length()),
bytes);
return; return;
} }
crdtp::cbor::EncodeFromUTF16( crdtp::cbor::EncodeFromUTF16(crdtp::span<uint16_t>(value.SpanUint16()),
crdtp::span<uint16_t>( bytes);
reinterpret_cast<const uint16_t*>(value.Characters16()),
value.length()),
bytes);
} }
// static // static

@ -400,7 +400,9 @@ class PLATFORM_EXPORT ParkableString final {
// Causes the string to be unparked. Note that the pointer must not be // Causes the string to be unparked. Note that the pointer must not be
// cached. // cached.
const LChar* Characters8() const { return ToString().Characters8(); } const LChar* Characters8() const { return ToString().Characters8(); }
const UChar* Characters16() const { return ToString().Characters16(); } const base::span<const uint16_t> SpanUint16() const {
return ToString().SpanUint16();
}
private: private:
scoped_refptr<ParkableStringImpl> impl_; scoped_refptr<ParkableStringImpl> impl_;

@ -207,7 +207,7 @@ class StringResource16 final : public StringResource16Base {
size_t length() const override { return GetStringImpl()->length(); } size_t length() const override { return GetStringImpl()->length(); }
const uint16_t* data() const override { const uint16_t* data() const override {
return reinterpret_cast<const uint16_t*>(GetStringImpl()->Characters16()); return GetStringImpl()->SpanUint16().data();
} }
}; };
@ -230,8 +230,7 @@ class ParkableStringResource16 final : public StringResource16Base {
size_t length() const override { return GetParkableString().length(); } size_t length() const override { return GetParkableString().length(); }
const uint16_t* data() const override { const uint16_t* data() const override {
return reinterpret_cast<const uint16_t*>( return GetParkableString().SpanUint16().data();
GetParkableString().Characters16());
} }
}; };

@ -201,6 +201,10 @@ class WTF_EXPORT StringImpl {
DCHECK(!Is8Bit()); DCHECK(!Is8Bit());
return CharacterBuffer<UChar>(); return CharacterBuffer<UChar>();
} }
ALWAYS_INLINE base::span<const uint16_t> SpanUint16() const {
DCHECK(!Is8Bit());
return CharacterBuffer<uint16_t>();
}
ALWAYS_INLINE const void* Bytes() const { ALWAYS_INLINE const void* Bytes() const {
return reinterpret_cast<const void*>(this + 1); return reinterpret_cast<const void*>(this + 1);
} }

@ -144,6 +144,14 @@ class WTF_EXPORT String {
return impl_->Span16(); return impl_->Span16();
} }
base::span<const uint16_t> SpanUint16() const {
if (!impl_) {
return {};
}
DCHECK(!impl_->Is8Bit());
return impl_->SpanUint16();
}
// This exposes the underlying representation of the string. Use with // This exposes the underlying representation of the string. Use with
// care. When interpreting the string as a sequence of code units // care. When interpreting the string as a sequence of code units
// Span8()/Span16() should be used. // Span8()/Span16() should be used.