diff options
Diffstat (limited to 'mrbgems/mruby-string-ext')
| -rw-r--r-- | mrbgems/mruby-string-ext/mrbgem.rake | 4 | ||||
| -rw-r--r-- | mrbgems/mruby-string-ext/mrblib/string.rb | 38 | ||||
| -rw-r--r-- | mrbgems/mruby-string-ext/src/string.c | 31 | ||||
| -rw-r--r-- | mrbgems/mruby-string-ext/test/string.rb | 72 |
4 files changed, 145 insertions, 0 deletions
diff --git a/mrbgems/mruby-string-ext/mrbgem.rake b/mrbgems/mruby-string-ext/mrbgem.rake new file mode 100644 index 000000000..83db97eb4 --- /dev/null +++ b/mrbgems/mruby-string-ext/mrbgem.rake @@ -0,0 +1,4 @@ +MRuby::Gem::Specification.new('mruby-string-ext') do |spec| + spec.license = 'MIT' + spec.authors = 'mruby developers' +end diff --git a/mrbgems/mruby-string-ext/mrblib/string.rb b/mrbgems/mruby-string-ext/mrblib/string.rb new file mode 100644 index 000000000..142a63882 --- /dev/null +++ b/mrbgems/mruby-string-ext/mrblib/string.rb @@ -0,0 +1,38 @@ +class String + def lstrip + a = 0 + z = self.size - 1 + a += 1 while " \f\n\r\t\v".include?(self[a]) and a <= z + (z >= 0) ? self[a..z] : "" + end + + def rstrip + a = 0 + z = self.size - 1 + z -= 1 while " \f\n\r\t\v\0".include?(self[z]) and a <= z + (z >= 0) ? self[a..z] : "" + end + + def strip + a = 0 + z = self.size - 1 + a += 1 while " \f\n\r\t\v".include?(self[a]) and a <= z + z -= 1 while " \f\n\r\t\v\0".include?(self[z]) and a <= z + (z >= 0) ? self[a..z] : "" + end + + def lstrip! + s = self.lstrip + (s == self) ? nil : self.replace(s) + end + + def rstrip! + s = self.rstrip + (s == self) ? nil : self.replace(s) + end + + def strip! + s = self.strip + (s == self) ? nil : self.replace(s) + end +end diff --git a/mrbgems/mruby-string-ext/src/string.c b/mrbgems/mruby-string-ext/src/string.c new file mode 100644 index 000000000..b10b021a2 --- /dev/null +++ b/mrbgems/mruby-string-ext/src/string.c @@ -0,0 +1,31 @@ +#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, "dump", mrb_str_dump, ARGS_NONE()); + mrb_define_method(mrb, s, "getbyte", mrb_str_getbyte, ARGS_REQ(1)); +} + +void +mrb_mruby_string_ext_gem_final(mrb_state* mrb) +{ +} diff --git a/mrbgems/mruby-string-ext/test/string.rb b/mrbgems/mruby-string-ext/test/string.rb new file mode 100644 index 000000000..eaff81890 --- /dev/null +++ b/mrbgems/mruby-string-ext/test/string.rb @@ -0,0 +1,72 @@ +## +# 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 + +assert('String#dump') do + "foo".dump == "\"foo\"" +end + +assert('String#strip') do + s = " abc " + s.strip + "".strip == "" and " \t\r\n\f\v".strip == "" and + "\0a\0".strip == "\0a" and + "abc".strip == "abc" and + " abc".strip == "abc" and + "abc ".strip == "abc" and + " abc ".strip == "abc" and + s == " abc " +end + +assert('String#lstrip') do + s = " abc " + s.lstrip + "".lstrip == "" and " \t\r\n\f\v".lstrip == "" and + "\0a\0".lstrip == "\0a\0" and + "abc".lstrip == "abc" and + " abc".lstrip == "abc" and + "abc ".lstrip == "abc " and + " abc ".lstrip == "abc " and + s == " abc " +end + +assert('String#rstrip') do + s = " abc " + s.rstrip + "".rstrip == "" and " \t\r\n\f\v".rstrip == "" and + "\0a\0".rstrip == "\0a" and + "abc".rstrip == "abc" and + " abc".rstrip == " abc" and + "abc ".rstrip == "abc" and + " abc ".rstrip == " abc" and + s == " abc " +end + +assert('String#strip!') do + s = " abc " + t = "abc" + s.strip! == "abc" and s == "abc" and t.strip! == nil +end + +assert('String#lstrip!') do + s = " abc " + t = "abc " + s.lstrip! == "abc " and s == "abc " and t.lstrip! == nil +end + +assert('String#rstrip!') do + s = " abc " + t = " abc" + s.rstrip! == " abc" and s == " abc" and t.rstrip! == nil +end |
