From 13fa2326bda9cded4adceb8e0f238c4f433b4002 Mon Sep 17 00:00:00 2001 From: Azareal Date: Thu, 11 Jul 2019 20:44:18 +1000 Subject: [PATCH] Avoid a few allocations for mentions. Add six new parser test cases. Stop treating lone double forward slashes as URLs. Fixes #52 --- common/parser.go | 17 +++++++++++++++-- parser_test.go | 14 ++++++++++---- 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/common/parser.go b/common/parser.go index bbddb4ff..40e47b7a 100644 --- a/common/parser.go +++ b/common/parser.go @@ -16,6 +16,7 @@ import ( // TODO: Somehow localise these? var SpaceGap = []byte(" ") var httpProtBytes = []byte("http://") +var DoubleForwardSlash = []byte("//") var InvalidURL = []byte("[Invalid URL]") var InvalidTopic = []byte("[Invalid Topic]") var InvalidProfile = []byte("[Invalid Profile]") @@ -531,7 +532,8 @@ func ParseMessage(msg string, sectionID int, sectionType string /*, user User*/) sb.Write(bytesSinglequote) sb.Write(urlMention) sb.Write(bytesGreaterthan) - sb.WriteString("@" + menUser.Name) + sb.WriteByte('@') + sb.WriteString(menUser.Name) sb.Write(URLClose) lastItem = i i-- @@ -560,11 +562,22 @@ func ParseMessage(msg string, sectionID int, sectionType string /*, user User*/) urlLen, ok := PartialURLStringLen(msg[i:]) if len(msg) < i+urlLen { //fmt.Println("o1") - sb.Write(InvalidURL) + if urlLen == 2 { + sb.Write(DoubleForwardSlash) + } else { + sb.Write(InvalidURL) + } i += len(msg) - 1 lastItem = i break } + if urlLen == 2 { + sb.Write(DoubleForwardSlash) + i += urlLen + lastItem = i + i-- + continue + } //fmt.Println("msg[i:i+urlLen]:", "'"+msg[i:i+urlLen]+"'") if !ok { //fmt.Printf("o2: i = %d; i+urlLen = %d\n",i,i+urlLen) diff --git a/parser_test.go b/parser_test.go index 21d88a54..20b601e3 100644 --- a/parser_test.go +++ b/parser_test.go @@ -145,22 +145,23 @@ func TestParser(t *testing.T) { msgList.Add("", "") msgList.Add("haha", "haha") msgList.Add("t", "t") - msgList.Add("//", "[Invalid URL]") + msgList.Add("//", "//") msgList.Add("http://", "[Invalid URL]") msgList.Add("https://", "[Invalid URL]") msgList.Add("ftp://", "[Invalid URL]") msgList.Add("git://", "[Invalid URL]") msgList.Add("ssh://", "ssh://") - msgList.Add("// ", "[Invalid URL] ") - msgList.Add("// //", "[Invalid URL] [Invalid URL]") + msgList.Add("// ", "// ") + msgList.Add("// //", "// //") + msgList.Add("// // //", "// // //") msgList.Add("http:// ", "[Invalid URL] ") msgList.Add("https:// ", "[Invalid URL] ") msgList.Add("ftp:// ", "[Invalid URL] ") msgList.Add("git:// ", "[Invalid URL] ") msgList.Add("ssh:// ", "ssh:// ") - msgList.Add("// t", "[Invalid URL] t") + msgList.Add("// t", "// t") msgList.Add("http:// t", "[Invalid URL] t") msgList.Add("http:", "http:") @@ -183,6 +184,11 @@ func TestParser(t *testing.T) { msgList.Add("haha\nhaha\nhaha", "haha
haha
haha") msgList.Add("//"+url, "//"+url+"") msgList.Add("//a", "//a") + msgList.Add(" //a", " //a") + msgList.Add("//a ", "//a ") + msgList.Add(" //a ", " //a ") + msgList.Add("d //a ", "d //a ") + msgList.Add("ddd ddd //a ", "ddd ddd //a ") msgList.Add("https://"+url, "https://"+url+"") msgList.Add("https://t", "https://t") msgList.Add("http://"+url, "http://"+url+"")