PragmaTwice commented on code in PR #791:
URL: https://github.com/apache/incubator-kvrocks/pull/791#discussion_r954924533
##########
src/encoding.cc:
##########
@@ -271,3 +271,60 @@ double DecodeDouble(const char *ptr) {
memcpy(&value, &decoded, sizeof(value));
return value;
}
+
+uint8_t* EncodeVarint32(uint8_t *dst, uint32_t v) {
+ // Operate on characters as unsigneds
+ uint8_t* ptr = dst;
+ do {
+ *ptr = 0x80 | v;
+ v >>= 7, ++ptr;
+ } while (v != 0);
+ *(ptr - 1) &= 0x7F;
+ return ptr;
+}
+
+void PutVarint32(std::string *dst, uint32_t v) {
+ uint8_t buf[5];
+ uint8_t* ptr = EncodeVarint32(buf, v);
+ dst->append(reinterpret_cast<char*>(buf), static_cast<size_t>(ptr - buf));
+}
+
+const char* GetVarint32PtrFallback(const char *p, const char *limit, uint32_t
*value) {
+ uint32_t result = 0;
+ for (uint32_t shift = 0; shift <= 28 && p < limit; shift += 7) {
+ uint32_t byte = *(reinterpret_cast<const unsigned char*>(p));
+ p++;
+ if (byte & 0x80) {
+ // More bytes are present
+ result |= ((byte & 0x7F) << shift);
+ } else {
+ result |= (byte << shift);
+ *value = result;
+ return p;
+ }
+ }
+ return nullptr;
+}
+
+const char* GetVarint32Ptr(const char *p, const char *limit, uint32_t *value) {
+ if (p < limit) {
+ uint32_t result = *(reinterpret_cast<const unsigned char*>(p));
Review Comment:
```suggestion
uint32_t result = static_cast<unsigned char>(*p);
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]