gitlab.com/gitlab-org/labkit@v1.21.0/correlation/base62.go (about)

     1  package correlation
     2  
     3  import "bytes"
     4  
     5  const base62Chars string = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
     6  
     7  // encodeReverseBase62 encodes num into its Base62 reversed representation.
     8  // The most significant value is at the end of the string.
     9  //
    10  // Appending is faster than prepending and this is enough for the purpose of a random ID.
    11  func encodeReverseBase62(num int64) string {
    12  	if num == 0 {
    13  		return "0"
    14  	}
    15  
    16  	encoded := bytes.Buffer{}
    17  	for q := num; q > 0; q /= 62 {
    18  		encoded.Write([]byte{base62Chars[q%62]})
    19  	}
    20  
    21  	return encoded.String()
    22  }