| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package pprofile // import "go.opentelemetry.io/collector/pdata/pprofile" |
| 5 |
|
| 6 |
import ( |
| 7 |
"errors" |
| 8 |
"math" |
| 9 |
|
| 10 |
"go.opentelemetry.io/collector/pdata/pcommon" |
| 11 |
) |
| 12 |
|
| 13 |
var errTooManyStringTableEntries = errors.New("too many entries in StringTable") |
| 14 |
|
| 15 |
// SetString updates a StringTable, adding or providing a value and returns its index. |
| 16 |
func SetString(table pcommon.StringSlice, val string) (int32, error) { |
| 17 |
for j, v := range table.All() { |
| 18 |
if v == val { |
| 19 |
if j > math.MaxInt32 { |
| 20 |
return 0, errTooManyStringTableEntries |
| 21 |
} |
| 22 |
// Return the index of the existing value. |
| 23 |
return int32(j), nil |
| 24 |
} |
| 25 |
} |
| 26 |
|
| 27 |
if table.Len() >= math.MaxInt32 { |
| 28 |
return 0, errTooManyMappingTableEntries |
| 29 |
} |
| 30 |
|
| 31 |
table.Append(val) |
| 32 |
return int32(table.Len() - 1), nil |
| 33 |
} |