Created
May 21, 2016 19:28
-
-
Save mlowicki/50006b28fe91e805618247ce77aad4b1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| int VarintLength(uint64_t v) { | |
| int len = 1; | |
| while (v >= 128) { | |
| v >>= 7; | |
| len++; | |
| } | |
| return len; | |
| } | |
| 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); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment