Skip to content

Commit

Permalink
Extract slice.go
Browse files Browse the repository at this point in the history
  • Loading branch information
pellared committed Jan 19, 2024
1 parent f66f390 commit 9fdb679
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 28 deletions.
20 changes: 0 additions & 20 deletions log/record.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,23 +172,3 @@ func countInvalidAttrs(as []KeyValue) int {
}
return n
}

// sliceGrow increases the slice's capacity, if necessary, to guarantee space
// for another n elements. After Grow(n), at least n elements can be appended
// to the slice without another allocation. If n is negative or too large to
// allocate the memory, Grow panics.
//
// This is a copy from https://pkg.go.dev/slices as it is not available in Go 1.20.
func sliceGrow[S ~[]E, E any](s S, n int) S {
if n -= cap(s) - len(s); n > 0 {
s = append(s[:cap(s)], make([]E, n)...)[:len(s)]
}
return s
}

// sliceClip removes unused capacity from the slice, returning s[:len(s):len(s)].
//
// This is a copy from https://pkg.go.dev/slices as it is not available in Go 1.20.
func sliceClip[S ~[]E, E any](s S) S {
return s[:len(s):len(s)]
}
48 changes: 48 additions & 0 deletions log/slice.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

// Copyright 2022 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package log // import "go.opentelemetry.io/otel/log"

// sliceGrow increases the slice's capacity, if necessary, to guarantee space
// for another n elements. After Grow(n), at least n elements can be appended
// to the slice without another allocation. If n is negative or too large to
// allocate the memory, Grow panics.
//
// This is a copy from https://pkg.go.dev/slices as it is not available in Go 1.20.
func sliceGrow[S ~[]E, E any](s S, n int) S {
if n -= cap(s) - len(s); n > 0 {
s = append(s[:cap(s)], make([]E, n)...)[:len(s)]
}
return s
}

// sliceClip removes unused capacity from the slice, returning s[:len(s):len(s)].
//
// This is a copy from https://pkg.go.dev/slices as it is not available in Go 1.20.
func sliceClip[S ~[]E, E any](s S) S {
return s[:len(s):len(s)]
}

// sliceEqualFunc reports whether two slices are equal using an equality
// function on each pair of elements. If the lengths are different,
// EqualFunc returns false. Otherwise, the elements are compared in
// increasing index order, and the comparison stops at the first index
// for which eq returns false.
//
// This is a copy from https://pkg.go.dev/slices as it is not available in Go 1.20.
func sliceEqualFunc[S1 ~[]E1, S2 ~[]E2, E1, E2 any](s1 S1, s2 S2, eq func(E1, E2) bool) bool {
if len(s1) != len(s2) {
return false
}
for i, v1 := range s1 {
v2 := s2[i]
if !eq(v1, v2) {
return false
}
}
return true
}
9 changes: 1 addition & 8 deletions log/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ package log // import "go.opentelemetry.io/otel/log"
import (
"fmt"
"math"
"slices"
"strconv"
"unsafe"
)
Expand Down Expand Up @@ -81,8 +80,6 @@ func (v Value) Kind() Kind {
}
}

//////////////// Constructors

// StringValue returns a new [Value] for a string.
func StringValue(value string) Value {
return Value{num: uint64(len(value)), any: stringptr(unsafe.StringData(value))}
Expand Down Expand Up @@ -146,8 +143,6 @@ func countEmptyGroups(as []KeyValue) int {
return n
}

//////////////// Accessors

// Any returns v's value as an any.
func (v Value) Any() any {
switch v.Kind() {
Expand Down Expand Up @@ -243,8 +238,6 @@ func (v Value) group() []KeyValue {
return unsafe.Slice((*KeyValue)(v.any.(groupptr)), v.num)
}

//////////////// Other

// Empty reports whether the value is empty (coresponds to nil).
func (v Value) Empty() bool {
return v.Kind() == KindEmpty
Expand All @@ -265,7 +258,7 @@ func (v Value) Equal(w Value) bool {
case KindFloat64:
return v.float() == w.float()
case KindGroup:
return slices.EqualFunc(v.group(), w.group(), KeyValue.Equal)
return sliceEqualFunc(v.group(), w.group(), KeyValue.Equal)
case KindEmpty:
return k1 == k2
default:
Expand Down

0 comments on commit 9fdb679

Please sign in to comment.