summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAaron Patterson <aaron.patterson@gmail.com>2010-01-09 12:03:01 -0800
committerAaron Patterson <aaron.patterson@gmail.com>2010-01-09 12:03:01 -0800
commitdb443b52843b05e47a2f803725ee19bfb1190fa9 (patch)
tree22db9422dcc6b43a37942f518be72a8e1c285cf4
parent3c1be5322fbf775ac4e807e510f7bc50fcd5e305 (diff)
downloadpsych-db443b52843b05e47a2f803725ee19bfb1190fa9.zip
anchors and aliases have an encoding
-rw-r--r--ext/psych/parser.c38
-rw-r--r--test/psych/test_encoding.rb60
2 files changed, 84 insertions, 14 deletions
diff --git a/ext/psych/parser.c b/ext/psych/parser.c
index e4a6e98..76ca4dd 100644
--- a/ext/psych/parser.c
+++ b/ext/psych/parser.c
@@ -132,11 +132,15 @@ static VALUE parse(VALUE self, VALUE yaml)
);
break;
case YAML_ALIAS_EVENT:
- rb_funcall(handler, id_alias, 1,
- event.data.alias.anchor ?
- rb_str_new2((const char *)event.data.alias.anchor) :
- Qnil
- );
+ {
+ VALUE alias = Qnil;
+ if(event.data.alias.anchor) {
+ alias = rb_str_new2((const char *)event.data.alias.anchor);
+ rb_enc_associate_index(alias, encoding);
+ }
+
+ rb_funcall(handler, id_alias, 1, alias);
+ }
break;
case YAML_SCALAR_EVENT:
{
@@ -147,13 +151,17 @@ static VALUE parse(VALUE self, VALUE yaml)
rb_enc_associate_index(val, encoding);
- VALUE anchor = event.data.scalar.anchor ?
- rb_str_new2((const char *)event.data.scalar.anchor) :
- Qnil;
+ VALUE anchor = Qnil;
+ if(event.data.scalar.anchor) {
+ anchor = rb_str_new2((const char *)event.data.scalar.anchor);
+ rb_enc_associate_index(anchor, encoding);
+ }
- VALUE tag = event.data.scalar.tag ?
- rb_str_new2((const char *)event.data.scalar.tag) :
- Qnil;
+ VALUE tag = Qnil;
+ if(event.data.scalar.tag) {
+ tag = rb_str_new2((const char *)event.data.scalar.tag);
+ rb_enc_associate_index(tag, encoding);
+ }
VALUE plain_implicit =
event.data.scalar.plain_implicit == 0 ? Qfalse : Qtrue;
@@ -173,9 +181,11 @@ static VALUE parse(VALUE self, VALUE yaml)
rb_str_new2((const char *)event.data.sequence_start.anchor) :
Qnil;
- VALUE tag = event.data.sequence_start.tag ?
- rb_str_new2((const char *)event.data.sequence_start.tag) :
- Qnil;
+ VALUE tag = Qnil;
+ if(event.data.sequence_start.tag) {
+ tag = rb_str_new2((const char *)event.data.sequence_start.tag);
+ rb_enc_associate_index(tag, encoding);
+ }
VALUE implicit =
event.data.sequence_start.implicit == 0 ? Qfalse : Qtrue;
diff --git a/test/psych/test_encoding.rb b/test/psych/test_encoding.rb
new file mode 100644
index 0000000..d97856f
--- /dev/null
+++ b/test/psych/test_encoding.rb
@@ -0,0 +1,60 @@
+# -*- coding: utf-8 -*-
+
+require 'minitest/autorun'
+require 'psych'
+
+module Psych
+ class TestEncoding < MiniTest::Unit::TestCase
+ class EncodingCatcher < Handler
+ attr_reader :strings
+ def initialize
+ @strings = []
+ end
+
+ (Handler.instance_methods(true) -
+ Object.instance_methods).each do |m|
+ class_eval %{
+ def #{m} *args
+ @strings += args.flatten.find_all { |a|
+ String === a
+ }
+ end
+ }
+ end
+ end
+
+ def setup
+ super
+ @handler = EncodingCatcher.new
+ @parser = Psych::Parser.new @handler
+ @utf8 = Encoding.find('UTF-8')
+ end
+
+ def test_scalar
+ @parser.parse("--- a")
+ assert_encodings @utf8, @handler.strings
+ end
+
+ def test_alias
+ @parser.parse(<<-eoyml)
+%YAML 1.1
+---
+!!seq [
+ !!str "Without properties",
+ &A !!str "Anchored",
+ !!str "Tagged",
+ *A,
+ !!str "",
+]
+ eoyml
+ assert_encodings @utf8, @handler.strings
+ end
+
+ private
+ def assert_encodings encoding, strings
+ strings.each do |str|
+ assert_equal encoding, str.encoding, str
+ end
+ end
+ end
+end