| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -5,6 +5,7 @@ | |||
| 5 | 5 | #ifndef V8_INSPECTOR_PROTOCOL_ENCODING_ENCODING_H_ | |
| 6 | 6 | #define V8_INSPECTOR_PROTOCOL_ENCODING_ENCODING_H_ | |
| 7 | 7 | ||
| 8 | + #include <algorithm> | ||
| 8 | 9 | #include <cstddef> | |
| 9 | 10 | #include <cstdint> | |
| 10 | 11 | #include <cstring> | |
@@ -14,6 +15,19 @@ | |||
| 14 | 15 | #include <vector> | |
| 15 | 16 | ||
| 16 | 17 | 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. | ||
| 17 | 31 | ||
| 18 | 32 | // ============================================================================= | |
| 19 | 33 | // span - sequence of bytes | |
@@ -72,6 +86,22 @@ inline span<uint8_t> SpanFrom(const std::string& v) { | |||
| 72 | 86 | return span<uint8_t>(reinterpret_cast<const uint8_t*>(v.data()), v.size()); | |
| 73 | 87 | } | |
| 74 | 88 | ||
| 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 | + | ||
| 75 | 105 | // ============================================================================= | |
| 76 | 106 | // Status and Error codes | |
| 77 | 107 | // ============================================================================= | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -121,6 +121,28 @@ TEST(SpanFromTest, FromConstCharAndLiteral) { | |||
| 121 | 121 | EXPECT_EQ(3u, SpanFrom("foo").size()); | |
| 122 | 122 | } | |
| 123 | 123 | ||
| 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 | + | ||
| 124 | 146 | // ============================================================================= | |
| 125 | 147 | // Status and Error codes | |
| 126 | 148 | // ============================================================================= | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -9,6 +9,7 @@ | |||
| 9 | 9 | #ifndef {{"_".join(config.protocol.namespace)}}_encoding_h | |
| 10 | 10 | #define {{"_".join(config.protocol.namespace)}}_encoding_h | |
| 11 | 11 | ||
| 12 | + #include <algorithm> | ||
| 12 | 13 | #include <cstddef> | |
| 13 | 14 | #include <cstdint> | |
| 14 | 15 | #include <cstring> | |
@@ -23,6 +24,19 @@ namespace {{namespace}} { | |||
| 23 | 24 | ||
| 24 | 25 | // ===== encoding/encoding.h ===== | |
| 25 | 26 | ||
| 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. | ||
| 26 | 40 | ||
| 27 | 41 | // ============================================================================= | |
| 28 | 42 | // span - sequence of bytes | |
@@ -81,6 +95,22 @@ inline span<uint8_t> SpanFrom(const std::string& v) { | |||
| 81 | 95 | return span<uint8_t>(reinterpret_cast<const uint8_t*>(v.data()), v.size()); | |
| 82 | 96 | } | |
| 83 | 97 | ||
| 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 | + | ||
| 84 | 114 | // ============================================================================= | |
| 85 | 115 | // Status and Error codes | |
| 86 | 116 | // ============================================================================= | |
| Back | FazBrowse Home | New Git URL |
0 commit comments