Skip to content

Commit

Permalink
fix: '//' inside a line don't trigger a match
Browse files Browse the repository at this point in the history
  • Loading branch information
aiq committed Jan 13, 2025
1 parent bf8d5b8 commit 009fe5f
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 11 deletions.
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
module github.com/aiq/go-mxt

go 1.15
go 1.15

require github.com/google/go-cmp v0.6.0 // indirect
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
13 changes: 10 additions & 3 deletions reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ func NewReader(r io.Reader) *Reader {

//***************************************************************** Reader.func

func readString(r *bufio.Reader, delim []byte) (string, error) {
func readString(r *bufio.Reader, delim []byte, isContent bool) (string, error) {
delimByte := delim[0]
delimByteStr := string([]byte{delimByte})
delimTail := delim[1:]
var builder strings.Builder
search := true
Expand All @@ -39,6 +40,12 @@ func readString(r *bufio.Reader, delim []byte) (string, error) {
return builder.String(), err
}

if isContent &&
builder.Len() != 1 &&
!strings.HasSuffix(tmpStr, "\n"+delimByteStr) {
continue
}

buf, err := r.Peek(len(delimTail))
if err != nil {
return builder.String(), err
Expand Down Expand Up @@ -85,7 +92,7 @@ func parseComment(rawComment string) string {

func (r *Reader) readHeader() (Header, error) {
header := Header{"", ""}
str, err := readString(r.Reader, []byte("-->"))
str, err := readString(r.Reader, []byte("-->"), false)
if err != nil {
return header, err
}
Expand Down Expand Up @@ -131,7 +138,7 @@ func (r *Reader) readContent() (string, error) {
delim = delim + r.expPatt
}

cnt, err := readString(r.Reader, []byte(delim))
cnt, err := readString(r.Reader, []byte(delim), true)
if err != nil && err != io.EOF {
return "", err
}
Expand Down
32 changes: 25 additions & 7 deletions reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ package mxt
import (
"strings"
"testing"

"github.com/google/go-cmp/cmp"
)

func TestReadExample(t *testing.T) {
func TestReadString(t *testing.T) {
cases := []struct {
inp string
exp Chunks
Expand Down Expand Up @@ -100,6 +102,24 @@ int main(void) {
},
},
},
{
inp: `// base64 -->
hQEMA8p144+Gi+YpAQf/VeFG9Zb+8w9aldWll8n2g3jqpE613LKg2XAJgwXQmSQL
R4O+TlQakJ+//5vM4IxxubPgYCyt6cyL7qM3oJIuk7vsqMbl5t7c/dOfXjj7goIC
// last -->
tsal`,
exp: Chunks{
{
Name: "base64",
Content: `hQEMA8p144+Gi+YpAQf/VeFG9Zb+8w9aldWll8n2g3jqpE613LKg2XAJgwXQmSQL
R4O+TlQakJ+//5vM4IxxubPgYCyt6cyL7qM3oJIuk7vsqMbl5t7c/dOfXjj7goIC`,
},
{
Name: "last",
Content: "tsal",
},
},
},
}

for i, c := range cases {
Expand All @@ -114,14 +134,12 @@ int main(void) {
}

for ci, cv := range chunks {
expName := c.exp[ci].Name
if expName != cv.Name {
t.Errorf("%v - @%v - unexpected name: %q != %q", i, ci, expName, cv.Name)
if diff := cmp.Diff(c.exp[ci].Name, cv.Name); diff != "" {
t.Errorf("%v - @%v - %v", i, ci, diff)
break
}
expContent := c.exp[ci].Content
if expContent != cv.Content {
t.Errorf("%v - @%v - unexpected content: %q != %q", i, ci, expContent, cv.Content)
if diff := cmp.Diff(c.exp[ci].Content, cv.Content); diff != "" {
t.Errorf("%v - @%v - %v", i, ci, diff)
break
}
}
Expand Down

0 comments on commit 009fe5f

Please sign in to comment.