2012-05-02 19:15:11 -04:00
|
|
|
#include "rr.h"
|
|
|
|
|
|
|
|
namespace rr {
|
2012-05-03 18:21:25 -04:00
|
|
|
|
|
|
|
void String::Init() {
|
2012-06-08 04:56:04 -04:00
|
|
|
ClassBuilder("String", Primitive::Class).
|
2012-05-03 18:21:25 -04:00
|
|
|
defineSingletonMethod("New", &New).
|
2012-06-08 09:51:10 -04:00
|
|
|
defineSingletonMethod("NewSymbol", &NewSymbol).
|
2012-05-09 14:12:44 -04:00
|
|
|
defineSingletonMethod("Concat", &Concat).
|
2012-05-08 17:04:47 -04:00
|
|
|
defineMethod("Utf8Value", &Utf8Value).
|
|
|
|
store(&Class);
|
2012-05-03 18:21:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
VALUE String::New(VALUE StringClass, VALUE string) {
|
2012-05-15 13:23:06 -04:00
|
|
|
return String(v8::String::New(RSTRING_PTR(string), (int)RSTRING_LEN(string)));
|
2012-05-03 18:21:25 -04:00
|
|
|
}
|
|
|
|
|
2012-06-08 09:51:10 -04:00
|
|
|
VALUE String::NewSymbol(VALUE self, VALUE string) {
|
|
|
|
return String(v8::String::NewSymbol(RSTRING_PTR(string), (int)RSTRING_LEN(string)));
|
|
|
|
}
|
|
|
|
|
2012-05-03 18:21:25 -04:00
|
|
|
VALUE String::Utf8Value(VALUE self) {
|
|
|
|
String str(self);
|
2012-06-06 04:52:53 -04:00
|
|
|
#ifdef HAVE_RUBY_ENCODING_H
|
|
|
|
return rb_enc_str_new(*v8::String::Utf8Value(*str), str->Utf8Length(), rb_enc_find("utf-8"));
|
|
|
|
#else
|
|
|
|
return rb_str_new(*v8::String::Utf8Value(*str), str->Utf8Length());
|
|
|
|
#endif
|
2012-05-02 19:15:11 -04:00
|
|
|
}
|
|
|
|
|
2012-05-09 14:12:44 -04:00
|
|
|
VALUE String::Concat(VALUE self, VALUE left, VALUE right) {
|
2012-05-15 17:49:01 -04:00
|
|
|
return String(v8::String::Concat(String(left), String(right)));
|
2012-05-09 14:12:44 -04:00
|
|
|
}
|
|
|
|
|
2012-06-14 23:34:38 -04:00
|
|
|
String::operator v8::Handle<v8::String>() const {
|
|
|
|
switch (TYPE(value)) {
|
|
|
|
case T_STRING:
|
|
|
|
return v8::String::New(RSTRING_PTR(value), (int)RSTRING_LEN(value));
|
|
|
|
case T_DATA:
|
|
|
|
return Ref<v8::String>::operator v8::Handle<v8::String>();
|
|
|
|
default:
|
|
|
|
VALUE string = rb_funcall(value, rb_intern("to_s"), 0);
|
|
|
|
return v8::String::New(RSTRING_PTR(string), (int)RSTRING_LEN(string));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-03 17:34:51 -04:00
|
|
|
} //namespace rr
|