PragmaTwice commented on code in PR #791:
URL: https://github.com/apache/incubator-kvrocks/pull/791#discussion_r955582212
##########
src/encoding.cc:
##########
@@ -271,3 +271,77 @@ double DecodeDouble(const char *ptr) {
memcpy(&value, &decoded, sizeof(value));
return value;
}
+
+char* EncodeVarint32(char *dst, uint32_t v) {
+ // Operate on characters as unsigneds
+ unsigned char* ptr = reinterpret_cast<unsigned char*>(dst);
+ static const int B = 128;
+ if (v < (1 << 7)) {
+ *(ptr++) = v;
+ } else if (v < (1 << 14)) {
+ *(ptr++) = v | B;
+ *(ptr++) = v >> 7;
+ } else if (v < (1 << 21)) {
+ *(ptr++) = v | B;
+ *(ptr++) = (v >> 7) | B;
+ *(ptr++) = v >> 14;
+ } else if (v < (1 << 28)) {
+ *(ptr++) = v | B;
+ *(ptr++) = (v >> 7) | B;
+ *(ptr++) = (v >> 14) | B;
+ *(ptr++) = v >> 21;
+ } else {
+ *(ptr++) = v | B;
+ *(ptr++) = (v >> 7) | B;
+ *(ptr++) = (v >> 14) | B;
+ *(ptr++) = (v >> 21) | B;
+ *(ptr++) = v >> 28;
+ }
+ return reinterpret_cast<char*>(ptr);
+}
+
+void PutVarint32(std::string *dst, uint32_t v) {
+ char buf[5];
+ char* ptr = EncodeVarint32(buf, v);
+ dst->append(buf, static_cast<size_t>(ptr - buf));
Review Comment:
If you mean the difference between `uint8_t` and `unsigned char`, I think
they are both OK. I do not prefer anyone, I use `uint8_t` just because it is
short. I know `uint8_t` is not guaranteed to be equal to `unsigned char` since
1 byte is not guaranteed to be equal to 8 bits in some platform, but this
function obviously treat char as 8 bits, since the function name is `...32`, so
just use `unsigned char` will still make this function fail in those platform.
--
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]