您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

set.go 2.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. // Subtract removes all the capabilities of another set from this set.
  51. func (s *Set) Subtract(other *Set) {
  52. utils.BitsetSubtract(s[:], other[:])
  53. }
  54. // Empty returns whether the set is empty.
  55. func (s *Set) Empty() bool {
  56. return utils.BitsetEmpty(s[:])
  57. }
  58. // String returns all of our enabled capabilities as a string.
  59. func (s *Set) String(version Version, values *Values) string {
  60. var strs sort.StringSlice
  61. var capab Capability
  62. asSlice := s[:]
  63. for capab = 0; capab < numCapabs; capab++ {
  64. // skip any capabilities that are not enabled
  65. if !utils.BitsetGet(asSlice, uint(capab)) {
  66. continue
  67. }
  68. capString := capab.Name()
  69. if version == Cap302 {
  70. val, exists := values.Get(capab)
  71. if exists {
  72. capString += "=" + val
  73. }
  74. }
  75. strs = append(strs, capString)
  76. }
  77. // sort the cap string before we send it out
  78. sort.Sort(strs)
  79. return strings.Join(strs, " ")
  80. }