internal.go raw

   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.
   7  package internal // import "golang.org/x/text/internal"
   8  
   9  import (
  10  	"sort"
  11  
  12  	"golang.org/x/text/language"
  13  )
  14  
  15  // SortTags sorts tags in place.
  16  func SortTags(tags []language.Tag) {
  17  	sort.Sort(sorter(tags))
  18  }
  19  
  20  type sorter []language.Tag
  21  
  22  func (s sorter) Len() int {
  23  	return len(s)
  24  }
  25  
  26  func (s sorter) Swap(i, j int) {
  27  	s[i], s[j] = s[j], s[i]
  28  }
  29  
  30  func (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.
  36  func 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  }
  50