internal.go 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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. // Package internal contains non-exported functionality that are used by
  5. // packages in the text repository.
  6. package internal // import "golang.org/x/text/internal"
  7. import (
  8. "sort"
  9. "golang.org/x/text/language"
  10. )
  11. // SortTags sorts tags in place.
  12. func SortTags(tags []language.Tag) {
  13. sort.Sort(sorter(tags))
  14. }
  15. type sorter []language.Tag
  16. func (s sorter) Len() int {
  17. return len(s)
  18. }
  19. func (s sorter) Swap(i, j int) {
  20. s[i], s[j] = s[j], s[i]
  21. }
  22. func (s sorter) Less(i, j int) bool {
  23. return s[i].String() < s[j].String()
  24. }
  25. // UniqueTags sorts and filters duplicate tags in place and returns a slice with
  26. // only unique tags.
  27. func UniqueTags(tags []language.Tag) []language.Tag {
  28. if len(tags) <= 1 {
  29. return tags
  30. }
  31. SortTags(tags)
  32. k := 0
  33. for i := 1; i < len(tags); i++ {
  34. if tags[k].String() < tags[i].String() {
  35. k++
  36. tags[k] = tags[i]
  37. }
  38. }
  39. return tags[:k+1]
  40. }