Permalink
Browse files

Added stubs for Encoding::Converter primitives.

  • Loading branch information...
1 parent dd510a8 commit 95e788ddf51b204bf7321b3dcb432286236eca1d @brixen brixen committed Aug 15, 2012
View
@@ -32,6 +32,11 @@ class Converter
attr_accessor :destination_encoding
attr_accessor :replacement
+ def self.allocate
+ Rubinius.primitive :encoding_converter_allocate
+ raise PrimitiveFailure, "Encoding::Converter.allocate primitive failed"
+ end
+
def self.asciicompat_encoding(string_or_encoding)
encoding = Rubinius::Type.try_convert_to_encoding string_or_encoding
@@ -131,6 +136,61 @@ def initialize(from, to, options=undefined)
end
end
+ def convert(str)
+ str = StringValue(str)
+
+ dest = ""
+ status = primitive_convert str, dest, nil, nil, PARTIAL_INPUT
+
+ if status == :invalid_byte_sequence or
+ status == :undefined_conversion or
+ status == :incomplete_input
+ # raise an exception
+ end
+
+ if status == :finished
+ raise ArgumentError, "converter already finished"
+ end
+
+ if status != :source_buffer_empty
+ raise RuntimeError, "unexpected result of Encoding::Converter#primitive_convert"
+ end
+
+ dest
+ end
+
+ def primitive_convert(source, target, offset=nil, size=nil, options=undefined)
+ Rubinius.primitive :encoding_converter_primitive_convert
+
+ if options.equal? undefined
+ options = 0
+ elsif !options.kind_of? Fixnum
+ opts = Rubinius::Type.coerce_to options, Hash, :to_hash
+
+ options = 0
+ options |= PARTIAL_INPUT if opts[:partial_input]
+ options |= AFTER_OUTPUT if opts[:after_output]
+ end
+
+ primitive_convert source, target, offset, size, options
+ end
+
+ def putback(maxbytes=nil)
+ Rubinius.primitive :encoding_converter_putback
+
+ putback maxbytes
+ end
+
+ def finish
+ Rubinius.primitive :encoding_converter_finish
+ raise PrimitiveFailure, "Encoding::Converter#finish primitive failed"
+ end
+
+ def last_error
+ Rubinius.primitive :encoding_converter_last_error
+ raise PrimitiveFailure, "Encoding::Converter#last_error primitive failed"
+ end
+
def convpath
path = []
a = 0
@@ -151,9 +211,6 @@ def convpath
path
end
- def convert(str)
- end
-
def self.search_convpath(from, to, options=undefined)
new(from, to, options).convpath
end
@@ -1,4 +1,3 @@
-fails:Encoding::Converter#last_error returns nil when the no conversion has been attempted
fails:Encoding::Converter#last_error returns nil when the last conversion did not produce an error
fails:Encoding::Converter#last_error returns nil when #primitive_convert last returned :destination_buffer_full
fails:Encoding::Converter#last_error returns nil when #primitive_convert last returned :finished
@@ -1,16 +1,8 @@
fails:Encoding::Converter#primitive_convert accepts a nil source buffer
-fails:Encoding::Converter#primitive_convert accepts a String as the source buffer
-fails:Encoding::Converter#primitive_convert accepts nil for the destination byte offset
-fails:Encoding::Converter#primitive_convert accepts an integer for the destination byte offset
fails:Encoding::Converter#primitive_convert raises an ArgumentError if the destination byte offset is greater than the bytesize of the destination buffer
fails:Encoding::Converter#primitive_convert uses the destination byte offset to determine where to write the result in the destination buffer
-fails:Encoding::Converter#primitive_convert accepts nil for the destination bytesize
-fails:Encoding::Converter#primitive_convert accepts an integer for the destination bytesize
-fails:Encoding::Converter#primitive_convert allows a destination bytesize value greater than the bytesize of the source buffer
-fails:Encoding::Converter#primitive_convert allows a destination bytesize value less than the bytesize of the source buffer
fails:Encoding::Converter#primitive_convert uses destination bytesize as the maximumn bytesize of the destination buffer
fails:Encoding::Converter#primitive_convert allows a destination buffer of unlimited size if destination bytesize is nil
-fails:Encoding::Converter#primitive_convert accepts an options hash
fails:Encoding::Converter#primitive_convert sets the destination buffer's encoding to the destination encoding if the conversion suceeded
fails:Encoding::Converter#primitive_convert sets the destination buffer's encoding to the destination encoding if the conversion failed
fails:Encoding::Converter#primitive_convert returns :incomplete_input when source buffer ends unexpectedly and :partial_input isn't specified
View
@@ -388,6 +388,10 @@ namespace rubinius {
return as<Class>(G(encoding)->get_const(state, state->symbol("Transcoding")));
}
+ Class* Encoding::converter_class(STATE) {
+ return as<Class>(G(encoding)->get_const(state, state->symbol("Converter")));
+ }
+
LookupTable* Encoding::encoding_map(STATE) {
return as<LookupTable>(internal_class(state)->get_const(
state, state->symbol("EncodingMap")));
@@ -602,4 +606,30 @@ namespace rubinius {
cls->set_const(state, "XML_ATTR_QUOTE_DECORATOR",
Fixnum::from(CONVERTER_XML_ATTR_QUOTE_DECORATOR));
}
+
+ Converter* Converter::allocate(STATE, Object* self) {
+ Class* cls = Encoding::converter_class(state);
+ Converter* c = state->new_object<Converter>(cls);
+
+ c->klass(state, as<Class>(self));
+
+ return c;
+ }
+
+ Symbol* Converter::primitive_convert(STATE, String* source, String* target,
+ Object* offset, Object* size, Fixnum* options) {
+ return nil<Symbol>();
+ }
+
+ String* Converter::finish(STATE) {
+ return nil<String>();
+ }
+
+ Exception* Converter::last_error(STATE) {
+ return nil<Exception>();
+ }
+
+ String* Converter::putback(STATE, Object* maxbytes) {
+ return nil<String>();
+ }
}
View
@@ -41,6 +41,7 @@ namespace rubinius {
static Class* internal_class(STATE);
static Class* transcoding_class(STATE);
+ static Class* converter_class(STATE);
static LookupTable* encoding_map(STATE);
static LookupTable* transcoding_map(STATE);
static Array* encoding_list(STATE);
@@ -134,6 +135,21 @@ namespace rubinius {
static void init(STATE);
+ // Rubinius.primitive :encoding_converter_allocate
+ static Converter* allocate(STATE, Object* self);
+
+ // Rubinius.primitive :encoding_converter_primitive_convert
+ Symbol* primitive_convert(STATE, String* source, String* target, Object* offset, Object* size, Fixnum* options);
+
+ // Rubinius.primitive :encoding_converter_finish
+ String* finish(STATE);
+
+ // Rubinius.primitive :encoding_converter_last_error
+ Exception* last_error(STATE);
+
+ // Rubinius.primitive :encoding_converter_putback
+ String* putback(STATE, Object* maxbytes);
+
class Info : public TypeInfo {
public:
BASIC_TYPEINFO(TypeInfo)

0 comments on commit 95e788d

Please sign in to comment.