From: Andy Wingo Date: Thu, 31 Mar 2011 12:46:21 +0000 (+0200) Subject: fix problems detecting coding: in block comments X-Git-Url: https://git.hcoop.net/bpt/guile.git/commitdiff_plain/8a12aeb9193a498c0b85c2de4d2ee1543ccb720d fix problems detecting coding: in block comments * libguile/read.c (scm_i_scan_for_encoding): Fix for coding on first line #! and for !# immediately following the coding. * test-suite/Makefile.am: * test-suite/tests/coding.test: Add tests. --- diff --git a/libguile/read.c b/libguile/read.c index e0f3cf815..5be3bd99d 100644 --- a/libguile/read.c +++ b/libguile/read.c @@ -1746,22 +1746,26 @@ scm_i_scan_for_encoding (SCM port) pos = encoding_start; while (pos >= header) { - if (*pos == '\n') + if (*pos == ';') + { + in_comment = 1; + break; + } + else if (*pos == '\n' || pos == header) { /* This wasn't in a semicolon comment. Check for a hash-bang comment. */ char *beg = strstr (header, "#!"); char *end = strstr (header, "!#"); - if (beg < encoding_start && encoding_start + encoding_length < end) + if (beg < encoding_start && encoding_start + encoding_length <= end) in_comment = 1; break; } - if (*pos == ';') - { - in_comment = 1; - break; - } - pos --; + else + { + pos --; + continue; + } } if (!in_comment) /* This wasn't in a comment */ diff --git a/test-suite/Makefile.am b/test-suite/Makefile.am index 9273406e6..8ee570b32 100644 --- a/test-suite/Makefile.am +++ b/test-suite/Makefile.am @@ -34,6 +34,7 @@ SCM_TESTS = tests/00-initial-env.test \ tests/bytevectors.test \ tests/c-api.test \ tests/chars.test \ + tests/coding.test \ tests/common-list.test \ tests/control.test \ tests/continuations.test \ diff --git a/test-suite/tests/coding.test b/test-suite/tests/coding.test new file mode 100644 index 000000000..4152af86a --- /dev/null +++ b/test-suite/tests/coding.test @@ -0,0 +1,104 @@ +;;;; coding.test --- test suite for coding declarations. -*- mode: scheme -*- +;;;; +;;;; Copyright (C) 2011 Free Software Foundation, Inc. +;;;; +;;;; This library is free software; you can redistribute it and/or +;;;; modify it under the terms of the GNU Lesser General Public +;;;; License as published by the Free Software Foundation; either +;;;; version 3 of the License, or (at your option) any later version. +;;;; +;;;; This library is distributed in the hope that it will be useful, +;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of +;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +;;;; Lesser General Public License for more details. +;;;; +;;;; You should have received a copy of the GNU Lesser General Public +;;;; License along with this library; if not, write to the Free Software +;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + +(define-module (test-coding) + #:use-module (test-suite lib)) + +(define (with-temp-file proc) + (let* ((name (string-copy "/tmp/coding-test.XXXXXX")) + (port (mkstemp! name))) + (let ((res (with-throw-handler + #t + (lambda () + (proc name port)) + (lambda _ + (delete-file name))))) + (delete-file name) + res))) + +(define (scan-coding str) + (with-temp-file + (lambda (name port) + (display str port) + (close port) + ;; We don't simply seek back and rescan, because the encoding scan + ;; relies on the opportunistic filling of the input buffer, which + ;; doesn't happen after a seek. + (let* ((port (open-input-file name)) + (res (port-encoding port))) + (close-port port) + res)))) + +(with-test-prefix "block comments" + + (pass-if "first line" + (equal? (scan-coding "#! coding: iso-8859-1 !#") + "ISO-8859-1")) + + (pass-if "first line no whitespace" + (equal? (scan-coding "#!coding:iso-8859-1!#") + "ISO-8859-1")) + + (pass-if "second line" + (equal? (scan-coding "#! \n coding: iso-8859-1 !#") + "ISO-8859-1")) + + (pass-if "second line no whitespace" + (equal? (scan-coding "#!\ncoding:iso-8859-1!#") + "ISO-8859-1")) + + (pass-if "third line" + (equal? (scan-coding "#! \n coding: iso-8859-1 \n !#") + "ISO-8859-1")) + + (pass-if "third line no whitespace" + (equal? (scan-coding "#!\ncoding:iso-8859-1\n!#") + "ISO-8859-1"))) + +(with-test-prefix "line comments" + (pass-if "first line, no whitespace, no nl" + (equal? (scan-coding ";coding:iso-8859-1") + "ISO-8859-1")) + + (pass-if "first line, whitespace, no nl" + (equal? (scan-coding "; coding: iso-8859-1 ") + "ISO-8859-1")) + + (pass-if "first line, no whitespace, nl" + (equal? (scan-coding ";coding:iso-8859-1\n") + "ISO-8859-1")) + + (pass-if "first line, whitespace, nl" + (equal? (scan-coding "; coding: iso-8859-1 \n") + "ISO-8859-1")) + + (pass-if "second line, no whitespace, no nl" + (equal? (scan-coding "\n;coding:iso-8859-1") + "ISO-8859-1")) + + (pass-if "second line, whitespace, no nl" + (equal? (scan-coding "\n; coding: iso-8859-1 ") + "ISO-8859-1")) + + (pass-if "second line, no whitespace, nl" + (equal? (scan-coding "\n;coding:iso-8859-1\n") + "ISO-8859-1")) + + (pass-if "second line, whitespace, nl" + (equal? (scan-coding "\n; coding: iso-8859-1 \n") + "ISO-8859-1")))