list.go raw

   1  // Package number implements a simple number list, used with relayinfo package
   2  // for NIP support lists.
   3  package number
   4  
   5  import "fmt"
   6  
   7  // List is a simple list of numbers with a sort implementation and number match.
   8  type List []int
   9  
  10  func (l List) Len() int           { return len(l) }
  11  func (l List) Less(i, j int) bool { return l[i] < l[j] }
  12  func (l List) Swap(i, j int)      { l[i], l[j] = l[j], l[i] }
  13  
  14  // HasNumber returns true if the list contains a given number
  15  func (l List) HasNumber(n int) (idx int, has bool) {
  16  	for idx = range l {
  17  		if l[idx] == n {
  18  			has = true
  19  			return
  20  		}
  21  	}
  22  	return
  23  }
  24  
  25  // String outputs a number.List as a minified JSON array.
  26  func (l List) String() (s string) {
  27  	s += "["
  28  	for i := range l {
  29  		if i > 0 {
  30  			s += ","
  31  		}
  32  		s += fmt.Sprint(l[i])
  33  	}
  34  	s += "]"
  35  	return
  36  }
  37