diff options
| author | Yukihiro Matz Matsumoto <[email protected]> | 2013-03-09 00:39:45 +0900 |
|---|---|---|
| committer | Yukihiro Matz Matsumoto <[email protected]> | 2013-03-09 00:39:45 +0900 |
| commit | b783311ec442d4b27f67ecb287c413cac36df147 (patch) | |
| tree | 8f90cb67e53f2b949cd62b515dce981806f6a2e9 /mrbgems | |
| parent | 318ad9c802d010b398a9683407716b5c7ef32b00 (diff) | |
| parent | 3c5bf780e4a3dcd2e73edd2475140ce0c2a46268 (diff) | |
| download | mruby-b783311ec442d4b27f67ecb287c413cac36df147.tar.gz mruby-b783311ec442d4b27f67ecb287c413cac36df147.zip | |
Merge branch 'master' of github.com:mruby/mruby
Diffstat (limited to 'mrbgems')
| -rw-r--r-- | mrbgems/mruby-numeric-ext/mrbgem.rake | 4 | ||||
| -rw-r--r-- | mrbgems/mruby-numeric-ext/src/numeric_ext.c | 30 | ||||
| -rw-r--r-- | mrbgems/mruby-numeric-ext/test/numeric.rb | 10 | ||||
| -rw-r--r-- | mrbgems/mruby-string-ext/mrblib/string.rb | 38 | ||||
| -rw-r--r-- | mrbgems/mruby-string-ext/test/string.rb | 54 |
5 files changed, 136 insertions, 0 deletions
diff --git a/mrbgems/mruby-numeric-ext/mrbgem.rake b/mrbgems/mruby-numeric-ext/mrbgem.rake new file mode 100644 index 000000000..69c4fde4c --- /dev/null +++ b/mrbgems/mruby-numeric-ext/mrbgem.rake @@ -0,0 +1,4 @@ +MRuby::Gem::Specification.new('mruby-numeric-ext') do |spec| + spec.license = 'MIT' + spec.authors = 'mruby developers' +end diff --git a/mrbgems/mruby-numeric-ext/src/numeric_ext.c b/mrbgems/mruby-numeric-ext/src/numeric_ext.c new file mode 100644 index 000000000..b2c0e7986 --- /dev/null +++ b/mrbgems/mruby-numeric-ext/src/numeric_ext.c @@ -0,0 +1,30 @@ +#include "mruby.h" +#include "mruby/numeric.h" + +static mrb_value +mrb_int_chr(mrb_state *mrb, mrb_value x) +{ + mrb_int chr; + char c; + + chr = mrb_fixnum(x); + if (chr >= (1 << CHAR_BIT)) { + mrb_raisef(mrb, E_RANGE_ERROR, "%ld out of char range", chr); + } + c = (char)chr; + + return mrb_str_new(mrb, &c, 1); +} + +void +mrb_mruby_numeric_ext_gem_init(mrb_state* mrb) +{ + struct RClass *i = mrb_class_get(mrb, "Integer"); + + mrb_define_method(mrb, i, "chr", mrb_int_chr, ARGS_NONE()); +} + +void +mrb_mruby_numeric_ext_gem_final(mrb_state* mrb) +{ +} diff --git a/mrbgems/mruby-numeric-ext/test/numeric.rb b/mrbgems/mruby-numeric-ext/test/numeric.rb new file mode 100644 index 000000000..6c1cf0fce --- /dev/null +++ b/mrbgems/mruby-numeric-ext/test/numeric.rb @@ -0,0 +1,10 @@ +## +# Numeric(Ext) Test + +assert('Integer#chr') do + assert_equal(65.chr, "A") + assert_equal(0x42.chr, "B") + + # multibyte encoding (not support yet) + assert_raise(RangeError) { 12345.chr } +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/test/string.rb b/mrbgems/mruby-string-ext/test/string.rb index d61ece351..eaff81890 100644 --- a/mrbgems/mruby-string-ext/test/string.rb +++ b/mrbgems/mruby-string-ext/test/string.rb @@ -16,3 +16,57 @@ 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 |
