Skip to content

Commit

Permalink
fix guid append write offset (#846)
Browse files Browse the repository at this point in the history
  • Loading branch information
paulwe authored Oct 9, 2024
1 parent 86d95fa commit 9081e96
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
3 changes: 2 additions & 1 deletion utils/guid/id.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,11 @@ func Marshal[T livekit.Guid](id T) livekit.GuidBlock {
}

func MarshalAppend[T livekit.Guid](b []byte, id T) []byte {
off := len(b)
b = append(b, make([]byte, Size*3/4)...)
idb := []byte(id)[len(id)-Size:]
for i := 0; i < 3; i++ {
j := i * 3
j := i*3 + off
k := i * 4
b[j] = b57Index[idb[k]]<<2 | b57Index[idb[k+1]]>>4
b[j+1] = b57Index[idb[k+1]]<<4 | b57Index[idb[k+2]]>>2
Expand Down
10 changes: 10 additions & 0 deletions utils/guid/id_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@ func TestMarshalUnmarshal(t *testing.T) {
require.EqualValues(t, b0, b1)
}

func TestMarshalAppend(t *testing.T) {
id0 := livekit.RoomID(New(RoomPrefix))
id1 := livekit.RoomID(New(RoomPrefix))
var b []byte
b = MarshalAppend(b, id0)
b = MarshalAppend(b, id1)
require.Equal(t, id0, Unmarshal[livekit.RoomID](livekit.GuidBlock(b[0:])))
require.Equal(t, id1, Unmarshal[livekit.RoomID](livekit.GuidBlock(b[9:])))
}

func BenchmarkNew(b *testing.B) {
b.Run("new", func(b *testing.B) {
var guid string
Expand Down

0 comments on commit 9081e96

Please sign in to comment.