summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ext/psych/parser.c5
-rw-r--r--ext/psych/psych.h20
-rw-r--r--lib/psych.rb10
-rw-r--r--lib/psych/nodes.rb12
-rw-r--r--test/psych/test_scalar.rb12
5 files changed, 50 insertions, 9 deletions
diff --git a/ext/psych/parser.c b/ext/psych/parser.c
index 7755c80..2b84dbc 100644
--- a/ext/psych/parser.c
+++ b/ext/psych/parser.c
@@ -45,6 +45,7 @@ static VALUE parse(VALUE self, VALUE yaml)
}
int done = 0;
+ int encoding = YAML_ANY_ENCODING;
VALUE handler = rb_iv_get(self, "@handler");
@@ -60,6 +61,8 @@ static VALUE parse(VALUE self, VALUE yaml)
switch(event.type) {
case YAML_STREAM_START_EVENT:
+ encoding = event.data.stream_start.encoding;
+
rb_funcall(handler, rb_intern("start_stream"), 1,
INT2NUM((long)event.data.stream_start.encoding)
);
@@ -114,6 +117,8 @@ static VALUE parse(VALUE self, VALUE yaml)
(long)event.data.scalar.length
);
+ PSYCH_ASSOCIATE_ENCODING(val, encoding);
+
VALUE anchor = event.data.scalar.anchor ?
rb_str_new2((const char *)event.data.scalar.anchor) :
Qnil;
diff --git a/ext/psych/psych.h b/ext/psych/psych.h
index 76a826b..aafb17f 100644
--- a/ext/psych/psych.h
+++ b/ext/psych/psych.h
@@ -4,6 +4,25 @@
#include <ruby.h>
#include <yaml.h>
+#define PSYCH_ASSOCIATE_ENCODING(_value, _encoding) \
+ ({ \
+ switch(_encoding) { \
+ case YAML_ANY_ENCODING: \
+ break; \
+ case YAML_UTF8_ENCODING: \
+ rb_enc_associate_index(_value, rb_enc_find_index("UTF-8"));\
+ break; \
+ case YAML_UTF16LE_ENCODING: \
+ rb_enc_associate_index(_value, rb_enc_find_index("UTF-16LE"));\
+ break; \
+ case YAML_UTF16BE_ENCODING: \
+ rb_enc_associate_index(_value, rb_enc_find_index("UTF-16BE"));\
+ break; \
+ default:\
+ break; \
+ }\
+ })
+
#include <parser.h>
#include <emitter.h>
#include <to_ruby.h>
@@ -11,4 +30,5 @@
extern VALUE mPsych;
+
#endif
diff --git a/lib/psych.rb b/lib/psych.rb
index 02925be..9833782 100644
--- a/lib/psych.rb
+++ b/lib/psych.rb
@@ -1,15 +1,7 @@
require 'psych/visitable'
-require 'psych/nodes/node'
-require 'psych/nodes/stream'
-require 'psych/nodes/document'
-require 'psych/nodes/sequence'
-require 'psych/nodes/scalar'
-require 'psych/nodes/mapping'
-require 'psych/nodes/alias'
-
+require 'psych/nodes'
require 'psych/visitors'
-
require 'psych/handler'
require 'psych/tree_builder'
require 'psych/parser'
diff --git a/lib/psych/nodes.rb b/lib/psych/nodes.rb
new file mode 100644
index 0000000..ebd383a
--- /dev/null
+++ b/lib/psych/nodes.rb
@@ -0,0 +1,12 @@
+require 'psych/nodes/node'
+require 'psych/nodes/stream'
+require 'psych/nodes/document'
+require 'psych/nodes/sequence'
+require 'psych/nodes/scalar'
+require 'psych/nodes/mapping'
+require 'psych/nodes/alias'
+
+module Psych
+ module Nodes
+ end
+end
diff --git a/test/psych/test_scalar.rb b/test/psych/test_scalar.rb
new file mode 100644
index 0000000..033f8b1
--- /dev/null
+++ b/test/psych/test_scalar.rb
@@ -0,0 +1,12 @@
+# -*- coding: utf-8 -*-
+
+require 'minitest/autorun'
+require 'psych'
+
+module Psych
+ class TestScalar < MiniTest::Unit::TestCase
+ def test_utf_8
+ assert_equal "日本語", Psych.load("--- 日本語")
+ end
+ end
+end