From 07669f9eb40bffca762e159359e46cb6a13598f1 Mon Sep 17 00:00:00 2001 From: Shivaram Lingamneni Date: Mon, 3 Feb 2025 21:13:57 -0500 Subject: [PATCH] remove hashset from mode parsing --- irc/handlers.go | 6 +++--- irc/modes.go | 2 +- irc/modes/modes.go | 14 ++++---------- irc/modes/modes_test.go | 10 ++++++---- 4 files changed, 14 insertions(+), 18 deletions(-) diff --git a/irc/handlers.go b/irc/handlers.go index 88890510..296a518b 100644 --- a/irc/handlers.go +++ b/irc/handlers.go @@ -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 { diff --git a/irc/modes.go b/irc/modes.go index e6079e24..cd7581ba 100644 --- a/irc/modes.go +++ b/irc/modes.go @@ -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) diff --git a/irc/modes/modes.go b/irc/modes/modes.go index c9483bb4..fc902ceb 100644 --- a/irc/modes/modes.go +++ b/irc/modes/modes.go @@ -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) } } } diff --git a/irc/modes/modes_test.go b/irc/modes/modes_test.go index 67d28c2f..03f42c33 100644 --- a/irc/modes/modes_test.go +++ b/irc/modes/modes_test.go @@ -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{