From 6941786490dbf70662eadf830595b34af08eeb5d Mon Sep 17 00:00:00 2001 From: Azareal Date: Wed, 19 Sep 2018 16:59:07 +1000 Subject: [PATCH] Reduce the amount of boilerplate for images in the parser. --- common/parser.go | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/common/parser.go b/common/parser.go index c4b4806b..26cb01e6 100644 --- a/common/parser.go +++ b/common/parser.go @@ -428,6 +428,7 @@ func ParseMessage(msg string, sectionID int, sectionType string /*, user User*/) msgbytes = append(msgbytes, SpaceGap...) var lastItem = 0 var i = 0 + // TODO: Use a bytes buffer or a string builder here for ; len(msgbytes) > (i + 1); i++ { if (i == 0 && (msgbytes[0] > 32)) || ((msgbytes[i] < 33) && (msgbytes[i+1] > 32)) { if (i != 0) || msgbytes[i] < 33 { @@ -563,24 +564,22 @@ func ParseMessage(msg string, sectionID int, sectionType string /*, user User*/) continue } + var addImage = func(url string) { + outbytes = append(outbytes, imageOpen...) + outbytes = append(outbytes, []byte(url)...) + outbytes = append(outbytes, imageOpen2...) + outbytes = append(outbytes, []byte(url)...) + outbytes = append(outbytes, imageClose...) + i += urlLen + lastItem = i + } + // TODO: Reduce the amount of code duplication if media.Type == "attach" { - outbytes = append(outbytes, imageOpen...) - outbytes = append(outbytes, []byte(media.URL+"?sectionID="+strconv.Itoa(sectionID)+"§ionType="+sectionType)...) - outbytes = append(outbytes, imageOpen2...) - outbytes = append(outbytes, []byte(media.URL+"?sectionID="+strconv.Itoa(sectionID)+"§ionType="+sectionType)...) - outbytes = append(outbytes, imageClose...) - i += urlLen - lastItem = i + addImage(media.URL+"?sectionID="+strconv.Itoa(sectionID)+"§ionType="+sectionType) continue } else if media.Type == "image" { - outbytes = append(outbytes, imageOpen...) - outbytes = append(outbytes, []byte(media.URL)...) - outbytes = append(outbytes, imageOpen2...) - outbytes = append(outbytes, []byte(media.URL)...) - outbytes = append(outbytes, imageClose...) - i += urlLen - lastItem = i + addImage(media.URL) continue } else if media.Type == "raw" { outbytes = append(outbytes, []byte(media.Body)...)