Avoid a few allocations for mentions.

Add six new parser test cases.
Stop treating lone double forward slashes as URLs.

Fixes #52
This commit is contained in:
Azareal 2019-07-11 20:44:18 +10:00
parent 462464e47e
commit 13fa2326bd
2 changed files with 25 additions and 6 deletions

View File

@ -16,6 +16,7 @@ import (
// TODO: Somehow localise these?
var SpaceGap = []byte(" ")
var httpProtBytes = []byte("http://")
var DoubleForwardSlash = []byte("//")
var InvalidURL = []byte("<red>[Invalid URL]</red>")
var InvalidTopic = []byte("<red>[Invalid Topic]</red>")
var InvalidProfile = []byte("<red>[Invalid Profile]</red>")
@ -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")
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)

View File

@ -145,22 +145,23 @@ func TestParser(t *testing.T) {
msgList.Add("", "")
msgList.Add("haha", "haha")
msgList.Add("<b>t</b>", "<b>t</b>")
msgList.Add("//", "<red>[Invalid URL]</red>")
msgList.Add("//", "//")
msgList.Add("http://", "<red>[Invalid URL]</red>")
msgList.Add("https://", "<red>[Invalid URL]</red>")
msgList.Add("ftp://", "<red>[Invalid URL]</red>")
msgList.Add("git://", "<red>[Invalid URL]</red>")
msgList.Add("ssh://", "ssh://")
msgList.Add("// ", "<red>[Invalid URL]</red> ")
msgList.Add("// //", "<red>[Invalid URL]</red> <red>[Invalid URL]</red>")
msgList.Add("// ", "// ")
msgList.Add("// //", "// //")
msgList.Add("// // //", "// // //")
msgList.Add("http:// ", "<red>[Invalid URL]</red> ")
msgList.Add("https:// ", "<red>[Invalid URL]</red> ")
msgList.Add("ftp:// ", "<red>[Invalid URL]</red> ")
msgList.Add("git:// ", "<red>[Invalid URL]</red> ")
msgList.Add("ssh:// ", "ssh:// ")
msgList.Add("// t", "<red>[Invalid URL]</red> t")
msgList.Add("// t", "// t")
msgList.Add("http:// t", "<red>[Invalid URL]</red> t")
msgList.Add("http:", "http:")
@ -183,6 +184,11 @@ func TestParser(t *testing.T) {
msgList.Add("haha\nhaha\nhaha", "haha<br>haha<br>haha")
msgList.Add("//"+url, "<a href='//"+url+"'>//"+url+"</a>")
msgList.Add("//a", "<a href='//a'>//a</a>")
msgList.Add(" //a", " <a href='//a'>//a</a>")
msgList.Add("//a ", "<a href='//a'>//a</a> ")
msgList.Add(" //a ", " <a href='//a'>//a</a> ")
msgList.Add("d //a ", "d <a href='//a'>//a</a> ")
msgList.Add("ddd ddd //a ", "ddd ddd <a href='//a'>//a</a> ")
msgList.Add("https://"+url, "<a href='https://"+url+"'>https://"+url+"</a>")
msgList.Add("https://t", "<a href='https://t'>https://t</a>")
msgList.Add("http://"+url, "<a href='http://"+url+"'>http://"+url+"</a>")