Skip to content

Commit cf029ca

Browse files
cola119richardlau
authored andcommitted
tools: update inspector_protocol to 912eb68
PR-URL: #51293 Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Luigi Pinca <[email protected]>
1 parent af11944 commit cf029ca

File tree

3 files changed

+82
-0
lines changed

3 files changed

+82
-0
lines changed

tools/inspector_protocol/encoding/encoding.h

+30
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#ifndef V8_INSPECTOR_PROTOCOL_ENCODING_ENCODING_H_
66
#define V8_INSPECTOR_PROTOCOL_ENCODING_ENCODING_H_
77

8+
#include <algorithm>
89
#include <cstddef>
910
#include <cstdint>
1011
#include <cstring>
@@ -14,6 +15,19 @@
1415
#include <vector>
1516

1617
namespace v8_inspector_protocol_encoding {
18+
// This library is designed to be portable. The only allowed dependency
19+
// are the C/C++ standard libraries, up to C++11. We support both 32 bit
20+
// and 64 architectures.
21+
//
22+
// Types used below:
23+
// uint8_t: a byte, e.g. for raw bytes or UTF8 characters
24+
// uint16_t: two bytes, e.g. for UTF16 characters
25+
// For input parameters:
26+
// span<uint8_t>: pointer to bytes and length
27+
// span<uint16_t>: pointer to UTF16 chars and length
28+
// For output parameters:
29+
// std::vector<uint8_t> - Owned segment of bytes / utf8 characters and length.
30+
// std::string - Same, for compatibility, even though char is signed.
1731

1832
// =============================================================================
1933
// span - sequence of bytes
@@ -72,6 +86,22 @@ inline span<uint8_t> SpanFrom(const std::string& v) {
7286
return span<uint8_t>(reinterpret_cast<const uint8_t*>(v.data()), v.size());
7387
}
7488

89+
// Less than / equality comparison functions for sorting / searching for byte
90+
// spans. These are similar to absl::string_view's < and == operators.
91+
inline bool SpanLessThan(span<uint8_t> x, span<uint8_t> y) noexcept {
92+
auto min_size = std::min(x.size(), y.size());
93+
const int r = min_size == 0 ? 0 : memcmp(x.data(), y.data(), min_size);
94+
return (r < 0) || (r == 0 && x.size() < y.size());
95+
}
96+
97+
inline bool SpanEquals(span<uint8_t> x, span<uint8_t> y) noexcept {
98+
auto len = x.size();
99+
if (len != y.size())
100+
return false;
101+
return x.data() == y.data() || len == 0 ||
102+
std::memcmp(x.data(), y.data(), len) == 0;
103+
}
104+
75105
// =============================================================================
76106
// Status and Error codes
77107
// =============================================================================

tools/inspector_protocol/encoding/encoding_test.cc

+22
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,28 @@ TEST(SpanFromTest, FromConstCharAndLiteral) {
121121
EXPECT_EQ(3u, SpanFrom("foo").size());
122122
}
123123

124+
TEST(SpanComparisons, ByteWiseLexicographicalOrder) {
125+
// Compare the empty span.
126+
EXPECT_FALSE(SpanLessThan(span<uint8_t>(), span<uint8_t>()));
127+
EXPECT_TRUE(SpanEquals(span<uint8_t>(), span<uint8_t>()));
128+
129+
// Compare message with itself.
130+
std::string msg = "Hello, world";
131+
EXPECT_FALSE(SpanLessThan(SpanFrom(msg), SpanFrom(msg)));
132+
EXPECT_TRUE(SpanEquals(SpanFrom(msg), SpanFrom(msg)));
133+
134+
// Compare message and copy.
135+
EXPECT_FALSE(SpanLessThan(SpanFrom(msg), SpanFrom(std::string(msg))));
136+
EXPECT_TRUE(SpanEquals(SpanFrom(msg), SpanFrom(std::string(msg))));
137+
138+
// Compare two messages. |lesser_msg| < |msg| because of the first
139+
// byte ('A' < 'H').
140+
std::string lesser_msg = "A lesser message.";
141+
EXPECT_TRUE(SpanLessThan(SpanFrom(lesser_msg), SpanFrom(msg)));
142+
EXPECT_FALSE(SpanLessThan(SpanFrom(msg), SpanFrom(lesser_msg)));
143+
EXPECT_FALSE(SpanEquals(SpanFrom(msg), SpanFrom(lesser_msg)));
144+
}
145+
124146
// =============================================================================
125147
// Status and Error codes
126148
// =============================================================================

tools/inspector_protocol/lib/encoding_h.template

+30
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#ifndef {{"_".join(config.protocol.namespace)}}_encoding_h
1010
#define {{"_".join(config.protocol.namespace)}}_encoding_h
1111

12+
#include <algorithm>
1213
#include <cstddef>
1314
#include <cstdint>
1415
#include <cstring>
@@ -23,6 +24,19 @@ namespace {{namespace}} {
2324

2425
// ===== encoding/encoding.h =====
2526

27+
// This library is designed to be portable. The only allowed dependency
28+
// are the C/C++ standard libraries, up to C++11. We support both 32 bit
29+
// and 64 architectures.
30+
//
31+
// Types used below:
32+
// uint8_t: a byte, e.g. for raw bytes or UTF8 characters
33+
// uint16_t: two bytes, e.g. for UTF16 characters
34+
// For input parameters:
35+
// span<uint8_t>: pointer to bytes and length
36+
// span<uint16_t>: pointer to UTF16 chars and length
37+
// For output parameters:
38+
// std::vector<uint8_t> - Owned segment of bytes / utf8 characters and length.
39+
// std::string - Same, for compatibility, even though char is signed.
2640

2741
// =============================================================================
2842
// span - sequence of bytes
@@ -81,6 +95,22 @@ inline span<uint8_t> SpanFrom(const std::string& v) {
8195
return span<uint8_t>(reinterpret_cast<const uint8_t*>(v.data()), v.size());
8296
}
8397

98+
// Less than / equality comparison functions for sorting / searching for byte
99+
// spans. These are similar to absl::string_view's < and == operators.
100+
inline bool SpanLessThan(span<uint8_t> x, span<uint8_t> y) noexcept {
101+
auto min_size = std::min(x.size(), y.size());
102+
const int r = min_size == 0 ? 0 : memcmp(x.data(), y.data(), min_size);
103+
return (r < 0) || (r == 0 && x.size() < y.size());
104+
}
105+
106+
inline bool SpanEquals(span<uint8_t> x, span<uint8_t> y) noexcept {
107+
auto len = x.size();
108+
if (len != y.size())
109+
return false;
110+
return x.data() == y.data() || len == 0 ||
111+
std::memcmp(x.data(), y.data(), len) == 0;
112+
}
113+
84114
// =============================================================================
85115
// Status and Error codes
86116
// =============================================================================

0 commit comments

Comments
 (0)