3
0
mirror of https://github.com/ergochat/ergo.git synced 2025-04-04 23:08:06 +02:00

remove hashset from mode parsing

This commit is contained in:
Shivaram Lingamneni 2025-02-03 21:13:57 -05:00
parent 4dfb7cc7ae
commit 07669f9eb4
4 changed files with 14 additions and 18 deletions

View File

@ -1852,11 +1852,11 @@ func cmodeHandler(server *Server, client *Client, msg ircmsg.Message, rb *Respon
if 1 < len(msg.Params) {
// parse out real mode changes
params := msg.Params[1:]
var unknown map[rune]bool
var unknown []rune
changes, unknown = modes.ParseChannelModeChanges(params...)
// alert for unknown mode changes
for char := range unknown {
for _, char := range unknown {
rb.Add(nil, server.name, ERR_UNKNOWNMODE, client.nick, string(char), client.t("is an unknown mode character to me"))
}
if len(unknown) == 1 && len(changes) == 0 {
@ -1943,7 +1943,7 @@ func umodeHandler(server *Server, client *Client, msg ircmsg.Message, rb *Respon
changes, unknown := modes.ParseUserModeChanges(params...)
// alert for unknown mode changes
for char := range unknown {
for _, char := range unknown {
rb.Add(nil, server.name, ERR_UNKNOWNMODE, cDetails.nick, string(char), client.t("is an unknown mode character to me"))
}
if len(unknown) == 1 && len(changes) == 0 {

View File

@ -116,7 +116,7 @@ func ApplyUserModeChanges(client *Client, changes modes.ModeChanges, force bool,
}
// parseDefaultModes uses the provided mode change parser to parse the rawModes.
func parseDefaultModes(rawModes string, parser func(params ...string) (modes.ModeChanges, map[rune]bool)) modes.Modes {
func parseDefaultModes(rawModes string, parser func(params ...string) (modes.ModeChanges, []rune)) modes.Modes {
modeChangeStrings := strings.Fields(rawModes)
modeChanges, _ := parser(modeChangeStrings...)
defaultModes := make(modes.Modes, 0)

View File

@ -190,10 +190,7 @@ func GetLowestChannelModePrefix(prefixes string) (lowest Mode) {
//
// ParseUserModeChanges returns the valid changes, and the list of unknown chars.
func ParseUserModeChanges(params ...string) (ModeChanges, map[rune]bool) {
changes := make(ModeChanges, 0)
unknown := make(map[rune]bool)
func ParseUserModeChanges(params ...string) (changes ModeChanges, unknown []rune) {
op := List
if 0 < len(params) {
@ -223,7 +220,7 @@ func ParseUserModeChanges(params ...string) (ModeChanges, map[rune]bool) {
if slices.Contains(SupportedUserModes, Mode(mode)) {
changes = append(changes, change)
} else {
unknown[mode] = true
unknown = append(unknown, mode)
}
}
}
@ -232,10 +229,7 @@ func ParseUserModeChanges(params ...string) (ModeChanges, map[rune]bool) {
}
// ParseChannelModeChanges returns the valid changes, and the list of unknown chars.
func ParseChannelModeChanges(params ...string) (ModeChanges, map[rune]bool) {
changes := make(ModeChanges, 0)
unknown := make(map[rune]bool)
func ParseChannelModeChanges(params ...string) (changes ModeChanges, unknown []rune) {
op := List
if 0 < len(params) {
@ -300,7 +294,7 @@ func ParseChannelModeChanges(params ...string) (ModeChanges, map[rune]bool) {
if slices.Contains(SupportedChannelModes, Mode(mode)) || slices.Contains(ChannelUserModes, Mode(mode)) {
changes = append(changes, change)
} else {
unknown[mode] = true
unknown = append(unknown, mode)
}
}
}

View File

@ -5,6 +5,7 @@ package modes
import (
"reflect"
"slices"
"strings"
"testing"
)
@ -16,7 +17,7 @@ func assertEqual(supplied, expected interface{}, t *testing.T) {
}
func TestParseUserModeChanges(t *testing.T) {
emptyUnknown := make(map[rune]bool)
var emptyUnknown []rune
changes, unknown := ParseUserModeChanges("+i")
assertEqual(unknown, emptyUnknown, t)
assertEqual(changes, ModeChanges{ModeChange{Op: Add, Mode: Invisible}}, t)
@ -48,10 +49,11 @@ func TestParseUserModeChanges(t *testing.T) {
}
func TestIssue874(t *testing.T) {
emptyUnknown := make(map[rune]bool)
var emptyModeChanges ModeChanges
var emptyUnknown []rune
modes, unknown := ParseChannelModeChanges("+k")
assertEqual(unknown, emptyUnknown, t)
assertEqual(modes, ModeChanges{}, t)
assertEqual(modes, emptyModeChanges, t)
modes, unknown = ParseChannelModeChanges("+k", "beer")
assertEqual(unknown, emptyUnknown, t)
@ -151,7 +153,7 @@ func TestParseChannelModeChanges(t *testing.T) {
}
modes, unknown = ParseChannelModeChanges("+tx")
if len(unknown) != 1 || !unknown['x'] {
if len(unknown) != 1 || !slices.Contains(unknown, 'x') {
t.Errorf("expected that x is an unknown mode, instead: %v", unknown)
}
expected = ModeChange{