github.com/cnboonhan/delve@v0.0.0-20230908061759-363f2388c2fb/pkg/dwarf/leb128/encode.go (about)

     1  package leb128
     2  
     3  import (
     4  	"io"
     5  )
     6  
     7  // EncodeUnsigned encodes x to the unsigned Little Endian Base 128 format.
     8  func EncodeUnsigned(out io.ByteWriter, x uint64) {
     9  	for {
    10  		b := byte(x & 0x7f)
    11  		x = x >> 7
    12  		if x != 0 {
    13  			b = b | 0x80
    14  		}
    15  		out.WriteByte(b)
    16  		if x == 0 {
    17  			break
    18  		}
    19  	}
    20  }
    21  
    22  // EncodeSigned encodes x to the signed Little Endian Base 128 format.
    23  func EncodeSigned(out io.ByteWriter, x int64) {
    24  	for {
    25  		b := byte(x & 0x7f)
    26  		x >>= 7
    27  
    28  		signb := b & 0x40
    29  
    30  		last := false
    31  		if (x == 0 && signb == 0) || (x == -1 && signb != 0) {
    32  			last = true
    33  		} else {
    34  			b = b | 0x80
    35  		}
    36  		out.WriteByte(b)
    37  
    38  		if last {
    39  			break
    40  		}
    41  	}
    42  }