Efficiently copies labels input slice. To be used in cases where input slice can be reused, but long-term copy is needed.
(input []LabelAdapter)
| 73 | // Efficiently copies labels input slice. To be used in cases where input slice |
| 74 | // can be reused, but long-term copy is needed. |
| 75 | func CopyLabels(input []LabelAdapter) labels.Labels { |
| 76 | builder := labels.NewBuilder(labels.EmptyLabels()) |
| 77 | |
| 78 | size := 0 |
| 79 | for _, l := range input { |
| 80 | size += len(l.Name) |
| 81 | size += len(l.Value) |
| 82 | } |
| 83 | |
| 84 | // Copy all strings into the buffer, and use 'yoloString' to convert buffer |
| 85 | // slices to strings. |
| 86 | buf := make([]byte, size) |
| 87 | var name, value string |
| 88 | |
| 89 | for _, l := range input { |
| 90 | name, buf = copyStringToBuffer(l.Name, buf) |
| 91 | value, buf = copyStringToBuffer(l.Value, buf) |
| 92 | builder.Set(name, value) |
| 93 | } |
| 94 | return builder.Labels() |
| 95 | } |
| 96 | |
| 97 | // Copies string to buffer (which must be big enough), and converts buffer slice containing |
| 98 | // the string copy into new string. |
no test coverage detected