From 11edea38804e2a7021c27e9d905bb001f3fce156 Mon Sep 17 00:00:00 2001 From: Tomoyuki Sahara Date: Fri, 8 Mar 2013 09:45:04 +0900 Subject: add "strip" family to String. --- mrbgems/mruby-string-ext/mrblib/string.rb | 38 ++++++++++++++++++++++ mrbgems/mruby-string-ext/test/string.rb | 54 +++++++++++++++++++++++++++++++ 2 files changed, 92 insertions(+) create mode 100644 mrbgems/mruby-string-ext/mrblib/string.rb 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 -- cgit v1.2.3 From 6c066302e2200f91847befd56b4ddbb555f41925 Mon Sep 17 00:00:00 2001 From: Kouki Ooyatsu Date: Fri, 8 Mar 2013 10:02:31 +0900 Subject: add mrbgems/ext/mruby-numeric, and method: Integer#chr --- build_config.rb | 3 +++ mrbgems/mruby-numeric-ext/mrbgem.rake | 4 ++++ mrbgems/mruby-numeric-ext/src/numeric_ext.c | 30 +++++++++++++++++++++++++++++ mrbgems/mruby-numeric-ext/test/numeric.rb | 7 +++++++ 4 files changed, 44 insertions(+) create mode 100644 mrbgems/mruby-numeric-ext/mrbgem.rake create mode 100644 mrbgems/mruby-numeric-ext/src/numeric_ext.c create mode 100644 mrbgems/mruby-numeric-ext/test/numeric.rb diff --git a/build_config.rb b/build_config.rb index c54c9d323..f697ec97d 100644 --- a/build_config.rb +++ b/build_config.rb @@ -26,6 +26,9 @@ MRuby::Build.new do |conf| # Use extensional String class conf.gem 'mrbgems/mruby-string-ext' + # Use extensional Numeric class + conf.gem 'mrbgems/mruby-numeric-ext' + # Generate binaries # conf.bins = %w(mrbc mruby mirb) 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..cafa0f1aa --- /dev/null +++ b/mrbgems/mruby-numeric-ext/test/numeric.rb @@ -0,0 +1,7 @@ +## +# Numeric(Ext) Test + +assert('Integer#chr') do + assert_equal(65.chr, "A") + assert_equal(0x42.chr, "B") +end -- cgit v1.2.3 From 6dfc276230d52b7fed7f86b94473cd6a8bb88cc0 Mon Sep 17 00:00:00 2001 From: Kouki Ooyatsu Date: Fri, 8 Mar 2013 10:36:54 +0900 Subject: add Integer#chr test case: multibyte (raise RangeError) --- mrbgems/mruby-numeric-ext/test/numeric.rb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/mrbgems/mruby-numeric-ext/test/numeric.rb b/mrbgems/mruby-numeric-ext/test/numeric.rb index cafa0f1aa..0132e5559 100644 --- a/mrbgems/mruby-numeric-ext/test/numeric.rb +++ b/mrbgems/mruby-numeric-ext/test/numeric.rb @@ -4,4 +4,7 @@ assert('Integer#chr') do assert_equal(65.chr, "A") assert_equal(0x42.chr, "B") + + # encoding multibyte (not support yet) + assert_raise(RangeError) { 12345.chr } end -- cgit v1.2.3 From d3ff90f5fc99e06433ccb6213461b906101cb623 Mon Sep 17 00:00:00 2001 From: Kouki Ooyatsu Date: Fri, 8 Mar 2013 10:41:38 +0900 Subject: fix comment --- mrbgems/mruby-numeric-ext/test/numeric.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mrbgems/mruby-numeric-ext/test/numeric.rb b/mrbgems/mruby-numeric-ext/test/numeric.rb index 0132e5559..6c1cf0fce 100644 --- a/mrbgems/mruby-numeric-ext/test/numeric.rb +++ b/mrbgems/mruby-numeric-ext/test/numeric.rb @@ -5,6 +5,6 @@ assert('Integer#chr') do assert_equal(65.chr, "A") assert_equal(0x42.chr, "B") - # encoding multibyte (not support yet) + # multibyte encoding (not support yet) assert_raise(RangeError) { 12345.chr } end -- cgit v1.2.3 From d1359e7a6fe8a61ec628ea39e5caa5f4f450dae0 Mon Sep 17 00:00:00 2001 From: Yuichiro MASUI Date: Fri, 8 Mar 2013 21:37:27 +0900 Subject: Fixed #969 --- tasks/mruby_build_commands.rake | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tasks/mruby_build_commands.rake b/tasks/mruby_build_commands.rake index ec8a8d3e3..1e806e5da 100644 --- a/tasks/mruby_build_commands.rake +++ b/tasks/mruby_build_commands.rake @@ -69,9 +69,9 @@ module MRuby @out_ext = build.exts.object if build_dir.include? "mrbgems/" - generated_file_matcher = Regexp.new("^#{build_dir}/(.*)#{Regexp.escape out_ext}$") + generated_file_matcher = Regexp.new("^#{Regexp.escape build_dir}/(.*)#{Regexp.escape out_ext}$") else - generated_file_matcher = Regexp.new("^#{build_dir}/(?!mrbgems/.+/)(.*)#{Regexp.escape out_ext}$") + generated_file_matcher = Regexp.new("^#{Regexp.escape build_dir}/(?!mrbgems/.+/)(.*)#{Regexp.escape out_ext}$") end source_exts.each do |ext, compile| rule generated_file_matcher => [ -- cgit v1.2.3