From 7138e76151dfdb8f54b0119ea3bbba8971aa93a0 Mon Sep 17 00:00:00 2001 From: Shivaram Lingamneni Date: Sun, 25 May 2025 01:59:55 -0400 Subject: [PATCH] fix #2147 use strings.CutPrefix when possible --- irc/bunt/bunt_datastore.go | 5 +++-- irc/handlers.go | 11 ++++------- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/irc/bunt/bunt_datastore.go b/irc/bunt/bunt_datastore.go index 76c831e3..869c6461 100644 --- a/irc/bunt/bunt_datastore.go +++ b/irc/bunt/bunt_datastore.go @@ -44,10 +44,11 @@ func (b *buntdbDatastore) GetAll(table datastore.Table) (result []datastore.KV, tablePrefix := fmt.Sprintf("%x ", table) err = b.db.View(func(tx *buntdb.Tx) error { err := tx.AscendGreaterOrEqual("", tablePrefix, func(key, value string) bool { - if !strings.HasPrefix(key, tablePrefix) { + encUUID, ok := strings.CutPrefix(key, tablePrefix) + if !ok { return false } - uuid, err := utils.DecodeUUID(strings.TrimPrefix(key, tablePrefix)) + uuid, err := utils.DecodeUUID(encUUID) if err == nil { result = append(result, datastore.KV{UUID: uuid, Value: []byte(value)}) } else { diff --git a/irc/handlers.go b/irc/handlers.go index 538c2126..0883b3b4 100644 --- a/irc/handlers.go +++ b/irc/handlers.go @@ -149,11 +149,8 @@ func (server *Server) sendLoginSnomask(nickMask, accountName string) { // to indicate that it should be removed from the list func acceptHandler(server *Server, client *Client, msg ircmsg.Message, rb *ResponseBuffer) bool { for _, tNick := range strings.Split(msg.Params[0], ",") { - add := true - if strings.HasPrefix(tNick, "-") { - add = false - tNick = strings.TrimPrefix(tNick, "-") - } + tNick, negPrefix := strings.CutPrefix(tNick, "-") + add := !negPrefix target := server.clients.Get(tNick) if target == nil { @@ -4118,9 +4115,9 @@ func zncHandler(server *Server, client *Client, msg ircmsg.Message, rb *Response // fake handler for unknown commands func unknownCommandHandler(server *Server, client *Client, msg ircmsg.Message, rb *ResponseBuffer) bool { var message string - if strings.HasPrefix(msg.Command, "/") { + if trimmedCmd, initialSlash := strings.CutPrefix(msg.Command, "/"); initialSlash { message = fmt.Sprintf(client.t("Unknown command; if you are using /QUOTE, the correct syntax is /QUOTE %[1]s, not /QUOTE %[2]s"), - strings.TrimPrefix(msg.Command, "/"), msg.Command) + trimmedCmd, msg.Command) } else { message = client.t("Unknown command") }