internal.go 1.0 KB

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