From d3f23f4ea7180f438b8d4f1e80c024be9d13f1dd Mon Sep 17 00:00:00 2001 From: Shivaram Lingamneni Date: Fri, 12 Jul 2019 18:25:06 -0400 Subject: [PATCH] trim to newline bytes more aggressively --- ircmsg/message.go | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/ircmsg/message.go b/ircmsg/message.go index d7e17b8..c6afb29 100644 --- a/ircmsg/message.go +++ b/ircmsg/message.go @@ -147,14 +147,6 @@ func ParseLineStrict(line string, fromClient bool, truncateLen int) (ircmsg IrcM return parseLine(line, maxTagDataLength, truncateLen) } -// slice off any amount of '\r' or '\n' from the end of the string -func trimFinalNewlines(str string) string { - var i int - for i = len(str) - 1; 0 <= i && (str[i] == '\r' || str[i] == '\n'); i -= 1 { - } - return str[:i+1] -} - // slice off any amount of ' ' from the front of the string func trimInitialSpaces(str string) string { var i int @@ -169,7 +161,15 @@ func parseLine(line string, maxTagDataLength int, truncateLen int) (ircmsg IrcMe return } - line = trimFinalNewlines(line) + // trim to the first appearance of either '\r' or '\n': + lineEnd := strings.IndexByte(line, '\r') + newlineIndex := strings.IndexByte(line, '\n') + if newlineIndex != -1 && (lineEnd == -1 || newlineIndex < lineEnd) { + lineEnd = newlineIndex + } + if lineEnd != -1 { + line = line[:lineEnd] + } if len(line) < 1 { return ircmsg, ErrorLineIsEmpty @@ -223,7 +223,7 @@ func parseLine(line string, maxTagDataLength int, truncateLen int) (ircmsg IrcMe paramStart = len(line) } // normalize command to uppercase: - ircmsg.Command = strings.ToUpper(strings.TrimSpace(line[:commandEnd])) + ircmsg.Command = strings.ToUpper(line[:commandEnd]) if len(ircmsg.Command) == 0 { return ircmsg, ErrorLineIsEmpty }