Merge pull request #963 from skandhas/pr-add-mrbgems-mruby-string-ext

add mrbgems/mruby-string-ext, and method: String#getbyte
This commit is contained in:
Yukihiro "Matz" Matsumoto
2013-03-07 06:21:52 -08:00
4 changed files with 52 additions and 0 deletions
+3
View File
@@ -23,6 +23,9 @@ MRuby::Build.new do |conf|
# Use standard Kernel#sprintf method
conf.gem 'mrbgems/mruby-sprintf'
# Use extensional String class
conf.gem 'mrbgems/mruby-string-ext'
# Generate binaries
# conf.bins = %w(mrbc mruby mirb)
+4
View File
@@ -0,0 +1,4 @@
MRuby::Gem::Specification.new('mruby-string-ext') do |spec|
spec.license = 'MIT'
spec.authors = 'mruby developers'
end
+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)
{
}
+15
View File
@@ -0,0 +1,15 @@
##
# String(Ext) Test
assert('String#getbyte') do
str1 = "hello"
bytes1 = [104, 101, 108, 108, 111]
assert_equal bytes1[0], str1.getbyte(0)
assert_equal bytes1[-1], str1.getbyte(-1)
assert_equal bytes1[6], str1.getbyte(6)
str2 = "\xFF"
bytes2 = [0xFF]
assert_equal bytes2[0], str2.getbyte(0)
end