summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ChangeLog10
-rw-r--r--parse.y9
-rw-r--r--test/ruby/test_m17n.rb18
3 files changed, 32 insertions, 5 deletions
diff --git a/ChangeLog b/ChangeLog
index 8debe45ab2..a169af8add 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,13 @@
+Fri Aug 01 18:27:15 2008 Yuki Sonoda (Yugui) <yugui@yugui.jp>
+
+ * parse.y (parser_yylex): removed an useless conditional, and magic
+ comment are ignored unless at the first of line.
+
+ * test/ruby/test_m17n.rb (test_magic_comment_vim): added.
+
+ * test/ruby/test_m17n.rb (test_magic_comment_at_variaous_positions):
+ added.
+
Fri Aug 1 14:54:42 2008 Nobuyoshi Nakada <nobu@ruby-lang.org>
* win32/win32.c (rb_w32_seekdir): no need to rewind to seek forward.
diff --git a/parse.y b/parse.y
index 05f3a91b60..6abc2da09e 100644
--- a/parse.y
+++ b/parse.y
@@ -6076,12 +6076,11 @@ parser_yylex(struct parser_params *parser)
goto retry;
case '#': /* it's a comment */
- if (!parser->has_shebang || parser->line_count != 1) {
- /* no magic_comment in shebang line */
+ /* no magic_comment in shebang line */
+ if (parser->line_count == (parser->has_shebang ? 2 : 1)
+ && (lex_p - lex_pbeg) == 1) {
if (!parser_magic_comment(parser, lex_p, lex_pend - lex_p)) {
- if (parser->line_count == (parser->has_shebang ? 2 : 1)) {
- set_file_encoding(parser, lex_p, lex_pend);
- }
+ set_file_encoding(parser, lex_p, lex_pend);
}
}
lex_p = lex_pend;
diff --git a/test/ruby/test_m17n.rb b/test/ruby/test_m17n.rb
index b2a67686aa..f01b838732 100644
--- a/test/ruby/test_m17n.rb
+++ b/test/ruby/test_m17n.rb
@@ -1192,6 +1192,24 @@ class TestM17N < Test::Unit::TestCase
assert_equal(Encoding::ASCII_8BIT, eval("# -*- encoding: ASCII-8BIT -*-\n__ENCODING__".force_encoding("US-ASCII")))
end
+ def test_magic_comment_vim
+ assert_equal(Encoding::US_ASCII, eval("# vim: filetype=ruby, fileencoding: US-ASCII, ts=3, sw=3\n__ENCODING__".force_encoding("ASCII-8BIT")))
+ assert_equal(Encoding::ASCII_8BIT, eval("# vim: filetype=ruby, fileencoding: ASCII-8BIT, ts=3, sw=3\n__ENCODING__".force_encoding("US-ASCII")))
+ end
+
+ def test_magic_comment_at_various_positions
+ # after shebang
+ assert_equal(Encoding::US_ASCII, eval("#!/usr/bin/ruby\n# -*- encoding: US-ASCII -*-\n__ENCODING__".force_encoding("ASCII-8BIT")))
+ assert_equal(Encoding::ASCII_8BIT, eval("#!/usr/bin/ruby\n# -*- encoding: ASCII-8BIT -*-\n__ENCODING__".force_encoding("US-ASCII")))
+ # wrong position
+ assert_equal(Encoding::ASCII_8BIT, eval("\n# -*- encoding: US-ASCII -*-\n__ENCODING__".force_encoding("ASCII-8BIT")))
+ assert_equal(Encoding::US_ASCII, eval("\n# -*- encoding: ASCII-8BIT -*-\n__ENCODING__".force_encoding("US-ASCII")))
+
+ # leading expressions
+ assert_equal(Encoding::ASCII_8BIT, eval("1+1 # -*- encoding: US-ASCII -*-\n__ENCODING__".force_encoding("ASCII-8BIT")))
+ assert_equal(Encoding::US_ASCII, eval("1+1 # -*- encoding: ASCII-8BIT -*-\n__ENCODING__".force_encoding("US-ASCII")))
+ end
+
def test_regexp_usascii
assert_regexp_usascii_literal('//', Encoding::US_ASCII)
assert_regexp_usascii_literal('/#{}/', Encoding::US_ASCII)