Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

set.go 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // Copyright (c) 2017 Daniel Oaks <daniel@danieloaks.net>
  2. // released under the MIT license
  3. package caps
  4. import (
  5. "sort"
  6. "strings"
  7. "github.com/oragono/oragono/irc/utils"
  8. )
  9. // Set holds a set of enabled capabilities.
  10. type Set [bitsetLen]uint64
  11. // NewSet returns a new Set, with the given capabilities enabled.
  12. func NewSet(capabs ...Capability) *Set {
  13. var newSet Set
  14. newSet.Enable(capabs...)
  15. return &newSet
  16. }
  17. // Enable enables the given capabilities.
  18. func (s *Set) Enable(capabs ...Capability) {
  19. asSlice := s[:]
  20. for _, capab := range capabs {
  21. utils.BitsetSet(asSlice, uint(capab), true)
  22. }
  23. }
  24. // Disable disables the given capabilities.
  25. func (s *Set) Disable(capabs ...Capability) {
  26. asSlice := s[:]
  27. for _, capab := range capabs {
  28. utils.BitsetSet(asSlice, uint(capab), false)
  29. }
  30. }
  31. // Add adds the given capabilities to this set.
  32. // this is just a wrapper to allow more clear use.
  33. func (s *Set) Add(capabs ...Capability) {
  34. s.Enable(capabs...)
  35. }
  36. // Remove removes the given capabilities from this set.
  37. // this is just a wrapper to allow more clear use.
  38. func (s *Set) Remove(capabs ...Capability) {
  39. s.Disable(capabs...)
  40. }
  41. // Has returns true if this set has the given capability.
  42. func (s *Set) Has(capab Capability) bool {
  43. return utils.BitsetGet(s[:], uint(capab))
  44. }
  45. // Union adds all the capabilities of another set to this set.
  46. func (s *Set) Union(other *Set) {
  47. utils.BitsetUnion(s[:], other[:])
  48. }
  49. // Subtract removes all the capabilities of another set from this set.
  50. func (s *Set) Subtract(other *Set) {
  51. utils.BitsetSubtract(s[:], other[:])
  52. }
  53. // Empty returns whether the set is empty.
  54. func (s *Set) Empty() bool {
  55. return utils.BitsetEmpty(s[:])
  56. }
  57. // String returns all of our enabled capabilities as a string.
  58. func (s *Set) String(version Version, values *Values) string {
  59. var strs sort.StringSlice
  60. var capab Capability
  61. asSlice := s[:]
  62. for capab = 0; capab < numCapabs; capab++ {
  63. // skip any capabilities that are not enabled
  64. if !utils.BitsetGet(asSlice, uint(capab)) {
  65. continue
  66. }
  67. capString := capab.Name()
  68. if version == Cap302 {
  69. val, exists := values.Get(capab)
  70. if exists {
  71. capString += "=" + val
  72. }
  73. }
  74. strs = append(strs, capString)
  75. }
  76. // sort the cap string before we send it out
  77. sort.Sort(strs)
  78. return strings.Join(strs, " ")
  79. }