mirror of
https://github.com/fumiama/go-simple-protobuf.git
synced 2026-06-09 12:30:25 +08:00
feat: add writer
This commit is contained in:
36
utils.go
Normal file
36
utils.go
Normal file
@@ -0,0 +1,36 @@
|
||||
package spb
|
||||
|
||||
import "io"
|
||||
|
||||
func ReadNum(r io.Reader) (n uint32, cnt uint32, err error) {
|
||||
var buf [1]byte
|
||||
for cnt < 5 {
|
||||
_, err = r.Read(buf[:])
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
n |= uint32(buf[0]&0x7f) << (7 * cnt)
|
||||
cnt++
|
||||
if buf[0]&0x80 == 0 {
|
||||
break
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func WriteNum(w io.Writer, n uint32) (cnt uint32, err error) {
|
||||
var buf [1]byte
|
||||
for n > 0 {
|
||||
buf[0] = uint8(n & 0x7f)
|
||||
if n>>7 > 0 {
|
||||
buf[0] |= 0x80
|
||||
}
|
||||
_, err = w.Write(buf[:])
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
n >>= 7
|
||||
cnt++
|
||||
}
|
||||
return
|
||||
}
|
||||
Reference in New Issue
Block a user