Last active
August 29, 2015 14:24
-
-
Save quux00/6c466c07b2c334d91352 to your computer and use it in GitHub Desktop.
Revised VarintEncode function
This file contains 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
func VarintEncode(w io.Writer, v uint64) error { | |
bs := make([]byte, 0, 10) | |
for (v & 0xffffffffffffff80) != 0 { | |
bs = append(bs, byte((v&0x7f)|0x80)) | |
v >>= 7 | |
} | |
bs = append(bs, byte(v&0x7f)) | |
n, err := w.Write(bs) | |
if err != nil { | |
return oerror.NewTrace(err) | |
} | |
if n != len(bs) { | |
return fmt.Errorf("Incorrect number of bytes written. "+ | |
"Expected %d. Actual %d", len(bs), n) | |
} | |
return nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment