1
Fork 0
mirror of https://git.savannah.gnu.org/git/guile.git synced 2025-05-20 11:40:18 +02:00

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.
This commit is contained in:
Andy Wingo 2011-03-31 14:46:21 +02:00
parent d8f699e8eb
commit 8a12aeb919
3 changed files with 120 additions and 11 deletions

View file

@ -1746,22 +1746,26 @@ scm_i_scan_for_encoding (SCM port)
pos = encoding_start;
while (pos >= header)
{
if (*pos == '\n')
{
/* 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)
in_comment = 1;
break;
}
if (*pos == ';')
{
in_comment = 1;
break;
}
pos --;
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)
in_comment = 1;
break;
}
else
{
pos --;
continue;
}
}
if (!in_comment)
/* This wasn't in a comment */

View file

@ -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 \

View file

@ -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")))