NormalizeSet interprets zv as a set body and returns an equivalent set body that is normalized according to the BSUP specification (i.e., each element's tag-counted value is lexicographically greater than that of the preceding element).
(zv scode.Bytes)
| 277 | // tag-counted value is lexicographically greater than that of the preceding |
| 278 | // element). |
| 279 | func NormalizeSet(zv scode.Bytes) scode.Bytes { |
| 280 | elements := make([]scode.Bytes, 0, 8) |
| 281 | for it := zv.Iter(); !it.Done(); { |
| 282 | elements = append(elements, it.NextTagAndBody()) |
| 283 | } |
| 284 | if len(elements) < 2 { |
| 285 | return zv |
| 286 | } |
| 287 | sort.Slice(elements, func(i, j int) bool { |
| 288 | return bytes.Compare(elements[i], elements[j]) == -1 |
| 289 | }) |
| 290 | norm := make(scode.Bytes, 0, len(zv)) |
| 291 | norm = append(norm, elements[0]...) |
| 292 | for i := 1; i < len(elements); i++ { |
| 293 | // Skip duplicates. |
| 294 | if !bytes.Equal(elements[i], elements[i-1]) { |
| 295 | norm = append(norm, elements[i]...) |
| 296 | } |
| 297 | } |
| 298 | return norm |
| 299 | } |
| 300 | |
| 301 | type TypeFusion struct { |
| 302 | id int |
no test coverage detected