aboutsummaryrefslogtreecommitdiff
path: root/object/id/object_id_ops.go
blob: b97cb19ace3ffad6d2a113961ac9388bc00cfd9f (about) (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package id

import (
	"bytes"
	"encoding/hex"
)

// ObjectFormat returns the object ID's object format (hash algorithm).
func (id ObjectID) ObjectFormat() ObjectFormat {
	return id.objectFormat
}

// Bytes returns a copy of the object ID bytes.
func (id ObjectID) Bytes() []byte {
	size := id.ObjectFormat().Size()

	return append([]byte(nil), id.data[:size]...)
}

// RawBytes returns a direct byte slice view of the object ID bytes.
//
// Prefer [ObjectID.Bytes] except for when it is a performance bottleneck.
//
// Labels: Mut-No.
func (id *ObjectID) RawBytes() []byte {
	size := id.ObjectFormat().Size()

	return id.data[:size:size]
}

// Compare lexicographically compares two object IDs
// by their canonical byte representation.
func (id ObjectID) Compare(other ObjectID) int {
	return bytes.Compare(id.RawBytes(), other.RawBytes())
}

// String returns the canonical hex representation.
func (id ObjectID) String() string {
	size := id.ObjectFormat().Size()

	return hex.EncodeToString(id.data[:size])
}