github.com/mckael/restic@v0.8.3/internal/restic/ids.go (about)

     1  package restic
     2  
     3  import (
     4  	"encoding/hex"
     5  	"fmt"
     6  )
     7  
     8  // IDs is an ordered list of IDs that implements sort.Interface.
     9  type IDs []ID
    10  
    11  func (ids IDs) Len() int {
    12  	return len(ids)
    13  }
    14  
    15  func (ids IDs) Less(i, j int) bool {
    16  	if len(ids[i]) < len(ids[j]) {
    17  		return true
    18  	}
    19  
    20  	for k, b := range ids[i] {
    21  		if b == ids[j][k] {
    22  			continue
    23  		}
    24  
    25  		if b < ids[j][k] {
    26  			return true
    27  		}
    28  
    29  		return false
    30  	}
    31  
    32  	return false
    33  }
    34  
    35  func (ids IDs) Swap(i, j int) {
    36  	ids[i], ids[j] = ids[j], ids[i]
    37  }
    38  
    39  // Uniq returns list without duplicate IDs. The returned list retains the order
    40  // of the original list so that the order of the first occurrence of each ID
    41  // stays the same.
    42  func (ids IDs) Uniq() (list IDs) {
    43  	seen := NewIDSet()
    44  
    45  	for _, id := range ids {
    46  		if seen.Has(id) {
    47  			continue
    48  		}
    49  
    50  		list = append(list, id)
    51  		seen.Insert(id)
    52  	}
    53  
    54  	return list
    55  }
    56  
    57  type shortID ID
    58  
    59  func (id shortID) String() string {
    60  	return hex.EncodeToString(id[:shortStr])
    61  }
    62  
    63  func (ids IDs) String() string {
    64  	elements := make([]shortID, 0, len(ids))
    65  	for _, id := range ids {
    66  		elements = append(elements, shortID(id))
    67  	}
    68  	return fmt.Sprintf("%v", elements)
    69  }