You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

set.go 2.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. utils.BitsetInitialize(newSet[:])
  15. newSet.Enable(capabs...)
  16. return &newSet
  17. }
  18. // Enable enables the given capabilities.
  19. func (s *Set) Enable(capabs ...Capability) {
  20. asSlice := s[:]
  21. for _, capab := range capabs {
  22. utils.BitsetSet(asSlice, uint(capab), true)
  23. }
  24. }
  25. // Disable disables the given capabilities.
  26. func (s *Set) Disable(capabs ...Capability) {
  27. asSlice := s[:]
  28. for _, capab := range capabs {
  29. utils.BitsetSet(asSlice, uint(capab), false)
  30. }
  31. }
  32. // Add adds the given capabilities to this set.
  33. // this is just a wrapper to allow more clear use.
  34. func (s *Set) Add(capabs ...Capability) {
  35. s.Enable(capabs...)
  36. }
  37. // Remove removes the given capabilities from this set.
  38. // this is just a wrapper to allow more clear use.
  39. func (s *Set) Remove(capabs ...Capability) {
  40. s.Disable(capabs...)
  41. }
  42. // Has returns true if this set has the given capability.
  43. func (s *Set) Has(capab Capability) bool {
  44. return utils.BitsetGet(s[:], uint(capab))
  45. }
  46. // Union adds all the capabilities of another set to this set.
  47. func (s *Set) Union(other *Set) {
  48. utils.BitsetUnion(s[:], other[:])
  49. }
  50. // Empty returns whether the set is empty.
  51. func (s *Set) Empty() bool {
  52. return utils.BitsetEmpty(s[:])
  53. }
  54. // String returns all of our enabled capabilities as a string.
  55. func (s *Set) String(version Version, values *Values) string {
  56. var strs sort.StringSlice
  57. var capab Capability
  58. asSlice := s[:]
  59. for capab = 0; capab < numCapabs; capab++ {
  60. // skip any capabilities that are not enabled
  61. if !utils.BitsetGet(asSlice, uint(capab)) {
  62. continue
  63. }
  64. capString := capab.Name()
  65. if version == Cap302 {
  66. val, exists := values.Get(capab)
  67. if exists {
  68. capString += "=" + val
  69. }
  70. }
  71. strs = append(strs, capString)
  72. }
  73. // sort the cap string before we send it out
  74. sort.Sort(strs)
  75. return strings.Join(strs, " ")
  76. }