NormalizeMap interprets zv as a map body and returns an equivalent map body that is normalized according to the BSUP specification (i.e., the tag-counted value of each entry's key is lexicographically greater than that of the preceding entry).
(zv scode.Bytes)
| 125 | // value of each entry's key is lexicographically greater than that of the |
| 126 | // preceding entry). |
| 127 | func NormalizeMap(zv scode.Bytes) scode.Bytes { |
| 128 | elements := make([]keyval, 0, 8) |
| 129 | for it := zv.Iter(); !it.Done(); { |
| 130 | key := it.NextTagAndBody() |
| 131 | val := it.NextTagAndBody() |
| 132 | elements = append(elements, keyval{key, val}) |
| 133 | } |
| 134 | if len(elements) < 2 { |
| 135 | return zv |
| 136 | } |
| 137 | sort.Slice(elements, func(i, j int) bool { |
| 138 | return bytes.Compare(elements[i].key, elements[j].key) == -1 |
| 139 | }) |
| 140 | norm := make(scode.Bytes, 0, len(zv)) |
| 141 | norm = append(norm, elements[0].key...) |
| 142 | norm = append(norm, elements[0].val...) |
| 143 | for i := 1; i < len(elements); i++ { |
| 144 | // Skip duplicates. |
| 145 | if !bytes.Equal(elements[i].key, elements[i-1].key) { |
| 146 | norm = append(norm, elements[i].key...) |
| 147 | norm = append(norm, elements[i].val...) |
| 148 | } |
| 149 | } |
| 150 | return norm |
| 151 | } |
| 152 | |
| 153 | type TypeNamed struct { |
| 154 | id int |