3
0
mirror of https://github.com/ergochat/ergo.git synced 2025-10-12 21:47:23 +02:00

use slices.SortFunc for modes

This commit is contained in:
Shivaram Lingamneni 2025-02-05 00:47:23 -05:00
parent 8fa6e19c2e
commit 4851825d4f

View File

@ -8,7 +8,6 @@ package modes
import ( import (
"fmt" "fmt"
"slices" "slices"
"sort"
"strings" "strings"
"github.com/ergochat/ergo/irc/utils" "github.com/ergochat/ergo/irc/utils"
@ -401,28 +400,22 @@ func (set *ModeSet) HighestChannelUserMode() (result Mode) {
return return
} }
type ByCodepoint Modes
func (a ByCodepoint) Len() int { return len(a) }
func (a ByCodepoint) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a ByCodepoint) Less(i, j int) bool { return a[i] < a[j] }
func RplMyInfo() (param1, param2, param3 string) { func RplMyInfo() (param1, param2, param3 string) {
userModes := make(Modes, len(SupportedUserModes), len(SupportedUserModes)+1) userModes := make(Modes, len(SupportedUserModes), len(SupportedUserModes)+1)
copy(userModes, SupportedUserModes) copy(userModes, SupportedUserModes)
// TLS is not in SupportedUserModes because it can't be modified // TLS is not in SupportedUserModes because it can't be modified
userModes = append(userModes, TLS) userModes = append(userModes, TLS)
sort.Sort(ByCodepoint(userModes)) slices.Sort(userModes)
channelModes := make(Modes, len(SupportedChannelModes)+len(ChannelUserModes)) channelModes := make(Modes, len(SupportedChannelModes)+len(ChannelUserModes))
copy(channelModes, SupportedChannelModes) copy(channelModes, SupportedChannelModes)
copy(channelModes[len(SupportedChannelModes):], ChannelUserModes) copy(channelModes[len(SupportedChannelModes):], ChannelUserModes)
sort.Sort(ByCodepoint(channelModes)) slices.Sort(channelModes)
// XXX enumerate these by hand, i can't see any way to DRY this // XXX enumerate these by hand, i can't see any way to DRY this
channelParametrizedModes := Modes{BanMask, ExceptMask, InviteMask, Key, UserLimit, Forward} channelParametrizedModes := Modes{BanMask, ExceptMask, InviteMask, Key, UserLimit, Forward}
channelParametrizedModes = append(channelParametrizedModes, ChannelUserModes...) channelParametrizedModes = append(channelParametrizedModes, ChannelUserModes...)
sort.Sort(ByCodepoint(channelParametrizedModes)) slices.Sort(channelParametrizedModes)
return userModes.String(), channelModes.String(), channelParametrizedModes.String() return userModes.String(), channelModes.String(), channelParametrizedModes.String()
} }
@ -438,10 +431,10 @@ func ChanmodesToken() (result string) {
// type D: modes without parameters // type D: modes without parameters
D := Modes{InviteOnly, Moderated, NoOutside, OpOnlyTopic, ChanRoleplaying, Secret, NoCTCP, RegisteredOnly, RegisteredOnlySpeak, Auditorium, OpModerated} D := Modes{InviteOnly, Moderated, NoOutside, OpOnlyTopic, ChanRoleplaying, Secret, NoCTCP, RegisteredOnly, RegisteredOnlySpeak, Auditorium, OpModerated}
sort.Sort(ByCodepoint(A)) slices.Sort(A)
sort.Sort(ByCodepoint(B)) slices.Sort(B)
sort.Sort(ByCodepoint(C)) slices.Sort(C)
sort.Sort(ByCodepoint(D)) slices.Sort(D)
return fmt.Sprintf("%s,%s,%s,%s", A.String(), B.String(), C.String(), D.String()) return fmt.Sprintf("%s,%s,%s,%s", A.String(), B.String(), C.String(), D.String())
} }