add mrbgems/mruby-string-ext, and method: String#getbyte

This commit is contained in:
skandhas
2013-03-07 21:47:00 +08:00
parent 2e95f638cd
commit 7cd583ef27
4 changed files with 52 additions and 0 deletions
+30
View File
@@ -0,0 +1,30 @@
#include "mruby.h"
#include "mruby/string.h"
static mrb_value
mrb_str_getbyte(mrb_state *mrb, mrb_value str)
{
mrb_int pos;
mrb_get_args(mrb, "i", &pos);
if (pos < 0)
pos += RSTRING_LEN(str);
if (pos < 0 || RSTRING_LEN(str) <= pos)
return mrb_nil_value();
return mrb_fixnum_value((unsigned char)RSTRING_PTR(str)[pos]);
}
void
mrb_mruby_string_ext_gem_init(mrb_state* mrb)
{
struct RClass * s = mrb->string_class;
mrb_define_method(mrb, s, "getbyte", mrb_str_getbyte, ARGS_REQ(1));
}
void
mrb_mruby_string_ext_gem_final(mrb_state* mrb)
{
}