shithub: rgbds

Download patch

ref: a060f135b83a1574e20a52d8aef8b21dd193238a
parent: bad66e54fad5c9d3773d30ae14eda94db44b4894
author: dbrotz <[email protected]>
date: Sun Dec 2 15:43:20 EST 2018

Only add newlines to file if necessary

--- a/src/asm/lexer.c
+++ b/src/asm/lexer.c
@@ -155,10 +155,7 @@
 
 	size = fread(pBuffer->pBuffer, sizeof(uint8_t), size, f);
 
-	pBuffer->pBuffer[size] = '\n';
-	pBuffer->pBuffer[size + 1] = '\n'; /* in case the file ends with \ */
-	pBuffer->pBuffer[size + 2] = 0;
-	pBuffer->nBufferSize = size + 2;
+	pBuffer->pBuffer[size] = 0;
 
 	/* Convert all line endings to LF and spaces */
 
@@ -218,6 +215,30 @@
 			}
 		}
 	}
+
+	pBuffer->nBufferSize = size;
+
+	/* Add newline if file doesn't end with one */
+	if (size == 0 || pBuffer->pBuffer[size - 1] != '\n') {
+		pBuffer->pBuffer[pBuffer->nBufferSize] = '\n';
+		pBuffer->nBufferSize++;
+	}
+
+	/* Add newline if \ will eat the last newline */
+	if (pBuffer->nBufferSize >= 2) {
+		size_t pos = pBuffer->nBufferSize - 2;
+
+		/* Skip spaces */
+		while (pos > 0 && pBuffer->pBuffer[pos] == ' ')
+			pos--;
+
+		if (pBuffer->pBuffer[pos] == '\\') {
+			pBuffer->pBuffer[pBuffer->nBufferSize] = '\n';
+			pBuffer->nBufferSize++;
+		}
+	}
+
+	pBuffer->pBuffer[pBuffer->nBufferSize] = 0;
 
 	pBuffer->oAtLineStart = 1;
 	return pBuffer;