appendImmutable adds data that is immutable and eligible for string interning.
(final []byte, positions map[string]int)
| 8 | |
| 9 | // appendImmutable adds data that is immutable and eligible for string interning. |
| 10 | func (data *Data) appendImmutable(final []byte, positions map[string]int) []byte { |
| 11 | keys := make([]string, 0, len(data.Immutable)) |
| 12 | |
| 13 | for key := range data.Immutable { |
| 14 | keys = append(keys, key) |
| 15 | } |
| 16 | |
| 17 | sort.SliceStable(keys, func(i, j int) bool { |
| 18 | a := data.Immutable[keys[i]] |
| 19 | b := data.Immutable[keys[j]] |
| 20 | |
| 21 | if len(a) != len(b) { |
| 22 | return len(a) > len(b) |
| 23 | } |
| 24 | |
| 25 | return slices.Compare(a, b) == -1 |
| 26 | }) |
| 27 | |
| 28 | for _, key := range keys { |
| 29 | raw := data.Immutable[key] |
| 30 | position := bytes.Index(final, raw) |
| 31 | |
| 32 | if position != -1 { |
| 33 | positions[key] = position |
| 34 | } else { |
| 35 | positions[key] = len(final) |
| 36 | final = append(final, raw...) |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | return final |
| 41 | } |