From 4ed253d66e8b97d261374259cefe9351db4ce93d Mon Sep 17 00:00:00 2001 From: Matthew Flatt Date: Mon, 3 Feb 2020 15:59:07 -0700 Subject: [PATCH] fix line counting with mixed CR and LF A CR by itself could be incorrectly treated as preceding a later LF, which would form a CRLF combination if they had been adjacent. Closes #3033 --- pkgs/racket-test-core/tests/racket/port.rktl | 15 +++++++++++++++ racket/src/racket/src/port.c | 1 + 2 files changed, 16 insertions(+) diff --git a/pkgs/racket-test-core/tests/racket/port.rktl b/pkgs/racket-test-core/tests/racket/port.rktl index 6b55a1c255..cf1802e26e 100644 --- a/pkgs/racket-test-core/tests/racket/port.rktl +++ b/pkgs/racket-test-core/tests/racket/port.rktl @@ -683,6 +683,21 @@ (test 0 syntax-column stx) (test 1026 syntax-position stx)))) +;; Test provided by @wcs4217 +(let ([p (open-input-bytes #"\rx\ny")]) + (port-count-lines! p) + (test-values '(1 0 1) (lambda () (port-next-location p))) + (test #\return read-char p) + (test-values '(2 0 2) (lambda () (port-next-location p))) + (test #\x read-char p) + (test-values '(2 1 3) (lambda () (port-next-location p))) + (test #\newline read-char p) + (test-values '(3 0 4) (lambda () (port-next-location p))) + (test #\y read-char p) + (test-values '(3 1 5) (lambda () (port-next-location p))) + (test eof read-char p) + (test-values '(3 1 5) (lambda () (port-next-location p)))) + ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Check that if the initial commit thread is killed, then ;; another commit thread is broken, that the second doesn't diff --git a/racket/src/racket/src/port.c b/racket/src/racket/src/port.c index 583e280c20..1c30ca2a66 100644 --- a/racket/src/racket/src/port.c +++ b/racket/src/racket/src/port.c @@ -1043,6 +1043,7 @@ XFORM_NONGCING static void do_count_lines(Scheme_Port *ip, const char *buffer, i ip->column = 0; } else { ip->charsSinceNewline += c; + ip->was_cr = 0; } /* Do the last line to get the column count right and to