1// Copyright 2015 The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5// Package internal contains non-exported functionality that are used by
6// packages in the text repository.
7package internal // import "golang.org/x/text/internal"
8
9import (
10 "sort"
11
12 "golang.org/x/text/language"
13)
14
15// SortTags sorts tags in place.
16func SortTags(tags []language.Tag) {
17 sort.Sort(sorter(tags))
18}
19
20type sorter []language.Tag
21
22func (s sorter) Len() int {
23 return len(s)
24}
25
26func (s sorter) Swap(i, j int) {
27 s[i], s[j] = s[j], s[i]
28}
29
30func (s sorter) Less(i, j int) bool {
31 return s[i].String() < s[j].String()
32}
33
34// UniqueTags sorts and filters duplicate tags in place and returns a slice with
35// only unique tags.
36func UniqueTags(tags []language.Tag) []language.Tag {
37 if len(tags) <= 1 {
38 return tags
39 }
40 SortTags(tags)
41 k := 0
42 for i := 1; i < len(tags); i++ {
43 if tags[k].String() < tags[i].String() {
44 k++
45 tags[k] = tags[i]
46 }
47 }
48 return tags[:k+1]
49}