summaryrefslogtreecommitdiff
path: root/ext/psych
diff options
context:
space:
mode:
authortenderlove <tenderlove@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2012-02-09 18:44:27 +0000
committertenderlove <tenderlove@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2012-02-09 18:44:27 +0000
commit59a13448083080407e33c90ca0f02166ce88c01c (patch)
treed565965a8f0bf7325e28ecac6fee2e8a09d816d2 /ext/psych
parent14c97cf9b6721d55f8f5afa0e7be7e7e80311aea (diff)
* 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 git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@34527 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'ext/psych')
-rw-r--r--ext/psych/lib/psych/parser.rb4
-rw-r--r--ext/psych/parser.c28
2 files changed, 8 insertions, 24 deletions
diff --git a/ext/psych/lib/psych/parser.rb b/ext/psych/lib/psych/parser.rb
index 5d75605d49..84085f1fb0 100644
--- a/ext/psych/lib/psych/parser.rb
+++ b/ext/psych/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/ext/psych/parser.c b/ext/psych/parser.c
index 70a5865edb..b0f4d97916 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");