Skip to content

Commit

Permalink
testing scalar encoding supprt
Browse files Browse the repository at this point in the history
  • Loading branch information
tenderlove committed Jan 5, 2010
1 parent 37e694a commit 965de3c
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 9 deletions.
5 changes: 5 additions & 0 deletions ext/psych/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand All @@ -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)
);
Expand Down Expand Up @@ -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;
Expand Down
20 changes: 20 additions & 0 deletions ext/psych/psych.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,31 @@
#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>
#include <yaml_tree.h>

extern VALUE mPsych;


#endif
10 changes: 1 addition & 9 deletions lib/psych.rb
Original file line number Diff line number Diff line change
@@ -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'
Expand Down
12 changes: 12 additions & 0 deletions lib/psych/nodes.rb
Original file line number Diff line number Diff line change
@@ -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
12 changes: 12 additions & 0 deletions test/psych/test_scalar.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 965de3c

Please sign in to comment.