testmark-go

Merge pull request #4 from mvdan/crlf-2

commit 9dc11b366b482d58f93724c256b48eb18ef3dbcd · parent 73c1a4a4ed · parent 97e3a9aab4 · browse this tree
Eric Myhre, 2021-09-24 17:00:28 UTC

fix parsing CRLF files, part 2
diff --git a/read.go b/read.go
index b20244c..c9a2a2f 100644
--- a/read.go
+++ b/read.go
@@ -51,9 +51,9 @@ func Parse(data []byte) (*Document, error) {
 	var expectCodeBlock bool
 	var codeBlockOffset int
 	hunkInProgress := DocHunk{LineStart: -1}
-	for i, line := range doc.Lines {
+	for i, origLine := range doc.Lines {
 		// Support CRLF line endings, for Windows.
-		line = bytes.TrimSuffix(line, sigilCarriageReturn)
+		line := bytes.TrimSuffix(origLine, sigilCarriageReturn)
 
 		// Check for transition in or out of codeblock.
 		if bytes.HasPrefix(line, sigilCodeBlock) {
@@ -61,7 +61,7 @@ func Parse(data []byte) (*Document, error) {
 			case false: // starting a block
 				if expectCodeBlock {
 					hunkInProgress.BlockTag = string(line[len(sigilCodeBlock):])
-					codeBlockOffset = offset + len(line) + 1
+					codeBlockOffset = offset + len(origLine) + 1
 				}
 				expectCodeBlock = false
 			case true: // ending a block
@@ -120,7 +120,7 @@ func Parse(data []byte) (*Document, error) {
 	next:
 		// Track total offset, so we can use it to subslice out document hunks.
 		// Mind: this is going to be off by one at the very end of the file... but that turns out never to matter to us.
-		offset += len(line) + 1
+		offset += len(origLine) + 1
 	}
 	return &doc, nil
 }