github.com/go-xe2/third@v1.0.3/golang.org/x/text/internal/internal.go (about)

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