diff options
author | Aaron Patterson <aaron.patterson@gmail.com> | 2012-02-09 10:46:50 -0800 |
---|---|---|
committer | Aaron Patterson <aaron.patterson@gmail.com> | 2012-02-09 10:46:50 -0800 |
commit | 86e02603780a10107e64b2572347e85b257005c2 (patch) | |
tree | 81f769e0a04045c6ac9b6a587ee34ca73f8f6e88 | |
parent | e2fcf9af9e95535401f816bc893839b9ad743a9e (diff) | |
download | psych-86e02603780a10107e64b2572347e85b257005c2.zip |
* ext/psych/parser.c: removed external encoding setter, allow parser
to be reused.
* ext/psych/lib/psych/parser.rb: added external encoding setter.
* test/psych/test_parser.rb: test parser reuse
-rw-r--r-- | CHANGELOG.rdoc | 7 | ||||
-rw-r--r-- | ext/psych/parser.c | 28 | ||||
-rw-r--r-- | lib/psych/parser.rb | 4 | ||||
-rw-r--r-- | test/psych/test_parser.rb | 15 |
4 files changed, 21 insertions, 33 deletions
diff --git a/CHANGELOG.rdoc b/CHANGELOG.rdoc index 120e32f..b554dde 100644 --- a/CHANGELOG.rdoc +++ b/CHANGELOG.rdoc @@ -1,3 +1,10 @@ +Fri Feb 10 03:41:31 2012 Aaron Patterson <aaron@tenderlovemaking.com> + + * ext/psych/parser.c: removed external encoding setter, allow parser + to be reused. + * ext/psych/lib/psych/parser.rb: added external encoding setter. + * test/psych/test_parser.rb: test parser reuse + Wed Jan 18 12:49:15 2012 Aaron Patterson <aaron@tenderlovemaking.com> * ext/psych/lib/psych/visitors/to_ruby.rb: Added support for loading diff --git a/ext/psych/parser.c b/ext/psych/parser.c index 70a5865..b0f4d97 100644 --- a/ext/psych/parser.c +++ b/ext/psych/parser.c @@ -106,6 +106,10 @@ static VALUE parse(int argc, VALUE *argv, VALUE self) Data_Get_Struct(self, yaml_parser_t, parser); + yaml_parser_delete(parser); + yaml_parser_initialize(parser); + yaml_parser_set_encoding(parser, NUM2INT(rb_iv_get(self, "@external_encoding"))); + if (OBJ_TAINTED(yaml)) tainted = 1; if(rb_respond_to(yaml, id_read)) { @@ -328,29 +332,6 @@ static VALUE parse(int argc, VALUE *argv, VALUE self) /* * call-seq: - * parser.external_encoding=(encoding) - * - * Set the encoding for this parser to +encoding+ - */ -static VALUE set_external_encoding(VALUE self, VALUE encoding) -{ - yaml_parser_t * parser; - VALUE exception; - - Data_Get_Struct(self, yaml_parser_t, parser); - - if(parser->encoding) { - exception = rb_const_get_at(mPsych, rb_intern("Exception")); - rb_raise(exception, "don't set the encoding twice!"); - } - - yaml_parser_set_encoding(parser, NUM2INT(encoding)); - - return encoding; -} - -/* - * call-seq: * parser.mark # => #<Psych::Parser::Mark> * * Returns a Psych::Parser::Mark object that contains line, column, and index @@ -397,7 +378,6 @@ void Init_psych_parser() rb_define_method(cPsychParser, "parse", parse, -1); rb_define_method(cPsychParser, "mark", mark, 0); - rb_define_method(cPsychParser, "external_encoding=", set_external_encoding, 1); id_read = rb_intern("read"); id_path = rb_intern("path"); diff --git a/lib/psych/parser.rb b/lib/psych/parser.rb index 5d75605..84085f1 100644 --- a/lib/psych/parser.rb +++ b/lib/psych/parser.rb @@ -36,12 +36,16 @@ module Psych # The handler on which events will be called attr_accessor :handler + # Set the encoding for this parser to +encoding+ + attr_writer :external_encoding + ### # Creates a new Psych::Parser instance with +handler+. YAML events will # be called on +handler+. See Psych::Parser for more details. def initialize handler = Handler.new @handler = handler + @external_encoding = ANY end end end diff --git a/test/psych/test_parser.rb b/test/psych/test_parser.rb index b607514..a491d7f 100644 --- a/test/psych/test_parser.rb +++ b/test/psych/test_parser.rb @@ -32,6 +32,12 @@ module Psych @handler.parser = @parser end + def test_multiparse + 3.times do + @parser.parse '--- foo' + end + end + def test_filename ex = assert_raises(Psych::SyntaxError) do @parser.parse '--- `', 'omg!' @@ -87,15 +93,6 @@ module Psych assert_equal 19, @parser.mark.index end - def test_set_encoding_twice - @parser.external_encoding = Psych::Parser::UTF16LE - - e = assert_raises(Psych::Exception) do - @parser.external_encoding = Psych::Parser::UTF16LE - end - assert_equal "don't set the encoding twice!", e.message - end - def test_bom tadpole = 'おたまじゃくし' |