github.com/GeniusesGroup/libgo@v0.0.0-20220929090155-5ff932cb408e/uuid/rfc-4122/utility.go (about)

     1  /* For license and copyright information please see LEGAL file in repository */
     2  
     3  package uuid
     4  
     5  import (
     6  	"bytes"
     7  	"encoding/hex"
     8  )
     9  
    10  // Equal returns true if uuid1 and uuid2 equals
    11  func Equal(uuid1, uuid2 [16]byte) bool {
    12  	return bytes.Equal(uuid1[:], uuid2[:])
    13  }
    14  
    15  // ToString returns canonical string representation of UUID:
    16  // xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx.
    17  func ToString(uuid [16]byte) string {
    18  	buf := make([]byte, 36)
    19  	hex.Encode(buf[0:8], uuid[0:4])
    20  	buf[8] = '-'
    21  	hex.Encode(buf[9:13], uuid[4:6])
    22  	buf[13] = '-'
    23  	hex.Encode(buf[14:18], uuid[6:8])
    24  	buf[18] = '-'
    25  	hex.Encode(buf[19:23], uuid[8:10])
    26  	buf[23] = '-'
    27  	hex.Encode(buf[24:], uuid[10:])
    28  
    29  	return string(buf)
    30  }
    31  
    32  // FromString will parsing UUID from string input
    33  func FromString(s string) (uuid [16]byte) {
    34  	return
    35  }
    36  
    37  // GetFirstUint64 use to get first 64bit of UUID as uint64
    38  func GetFirstUint64(uuid [16]byte) (id uint64) {
    39  	id = uint64(uuid[0]) | uint64(uuid[1])<<8 | uint64(uuid[2])<<16 | uint64(uuid[3])<<24 |
    40  		uint64(uuid[4])<<32 | uint64(uuid[5])<<40 | uint64(uuid[6])<<48 | uint64(uuid[7])<<56
    41  	return
    42  }
    43  
    44  // GetLastUint64 use to get last 64bit of UUID as uint64
    45  func GetLastUint64(uuid [16]byte) (id uint64) {
    46  	id = uint64(uuid[8]) | uint64(uuid[9])<<8 | uint64(uuid[10])<<16 | uint64(uuid[11])<<24 |
    47  		uint64(uuid[12])<<32 | uint64(uuid[13])<<40 | uint64(uuid[14])<<48 | uint64(uuid[15])<<56
    48  	return
    49  }
    50  
    51  // GetFirstUint32 use to get first 32bit of UUID as uint32
    52  func GetFirstUint32(uuid [16]byte) (id uint32) {
    53  	id = uint32(uuid[0]) | uint32(uuid[1])<<8 | uint32(uuid[2])<<16 | uint32(uuid[3])<<24
    54  	return
    55  }