Skip to content

Commit

Permalink
Only compile closing boundary regexp once
Browse files Browse the repository at this point in the history
  • Loading branch information
jhillyerd committed Dec 31, 2017
1 parent 0c0d1bc commit 68e689d
Showing 1 changed file with 9 additions and 10 deletions.
19 changes: 9 additions & 10 deletions part.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,10 @@ func splitEpilogue(
epilogue *bytes.Buffer,
err error,
) {
boundaryRegexp, err := regexp.Compile("^--" + boundary + "--.*")
if err != nil {
return nil, nil, err
}
body = new(bytes.Buffer)
epilogue = new(bytes.Buffer)
eof := false
Expand All @@ -206,18 +210,13 @@ func splitEpilogue(
}
eof = true
}
isClosingLine, err := regexp.MatchString("^--"+boundary+"--.*", string(line))
if err != nil {
if _, err = body.Write(line); err != nil {
return nil, nil, err
}
body.Write(line)
if err != nil {
return nil, nil, err
}
if isClosingLine {
// here we read what is left after the closing boundary
_, err = io.Copy(epilogue, br)
if err != nil {
// Check if this is the closing boundary line
if boundaryRegexp.Match(line) {
// Copy what is left after the closing boundary
if _, err = io.Copy(epilogue, br); err != nil {
return nil, nil, err
}
break
Expand Down

0 comments on commit 68e689d

Please sign in to comment.