summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--build_config.rb3
-rw-r--r--include/mruby/array.h7
-rw-r--r--mrbgems/mruby-numeric-ext/mrbgem.rake4
-rw-r--r--mrbgems/mruby-numeric-ext/src/numeric_ext.c30
-rw-r--r--mrbgems/mruby-numeric-ext/test/numeric.rb10
-rw-r--r--mrbgems/mruby-string-ext/mrblib/string.rb38
-rw-r--r--mrbgems/mruby-string-ext/src/string.c3
-rw-r--r--mrbgems/mruby-string-ext/test/string.rb57
-rw-r--r--src/array.c33
-rw-r--r--src/vm.c6
-rw-r--r--tools/mrbc/mrbc.c6
11 files changed, 163 insertions, 34 deletions
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/include/mruby/array.h b/include/mruby/array.h
index 5d78ccddd..7fba4a99c 100644
--- a/include/mruby/array.h
+++ b/include/mruby/array.h
@@ -38,12 +38,14 @@ struct RArray {
void mrb_ary_decref(mrb_state*, mrb_shared_array*);
mrb_value mrb_ary_new_capa(mrb_state*, mrb_int);
mrb_value mrb_ary_new(mrb_state *mrb);
-mrb_value mrb_ary_new_elts(mrb_state *mrb, mrb_int n, const mrb_value *elts);
+mrb_value mrb_ary_new_from_values(mrb_state *mrb, mrb_int size, const mrb_value *vals);
+/* compatibility macros - soon to be removed */
+#define mrb_ary_new_elts(mrb,size,vals) mrb_ary_new_from_values(mrb,size,vals)
+#define mrb_ary_new4(mrb,size,vals) mrb_ary_new_from_values(mrb,size,vals)
void mrb_ary_concat(mrb_state*, mrb_value, mrb_value);
mrb_value mrb_ary_splat(mrb_state*, mrb_value);
void mrb_ary_push(mrb_state*, mrb_value, mrb_value);
mrb_value mrb_ary_pop(mrb_state *mrb, mrb_value ary);
-mrb_value mrb_ary_new_from_values(mrb_state *mrb, mrb_int size, mrb_value *vals);
mrb_value mrb_ary_aget(mrb_state *mrb, mrb_value self);
mrb_value mrb_ary_ref(mrb_state *mrb, mrb_value ary, mrb_int n);
void mrb_ary_set(mrb_state *mrb, mrb_value ary, mrb_int n, mrb_value val);
@@ -51,7 +53,6 @@ mrb_int mrb_ary_len(mrb_state *mrb, mrb_value ary);
void mrb_ary_replace(mrb_state *mrb, mrb_value a, mrb_value b);
mrb_value mrb_check_array_type(mrb_state *mrb, mrb_value self);
mrb_value mrb_ary_unshift(mrb_state *mrb, mrb_value self, mrb_value item);
-mrb_value mrb_ary_new4(mrb_state *mrb, mrb_int n, const mrb_value *elts);
mrb_value mrb_assoc_new(mrb_state *mrb, mrb_value car, mrb_value cdr);
mrb_value mrb_ary_entry(mrb_value ary, mrb_int offset);
mrb_value mrb_ary_shift(mrb_state *mrb, mrb_value self);
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/src/string.c b/mrbgems/mruby-string-ext/src/string.c
index 175426638..b10b021a2 100644
--- a/mrbgems/mruby-string-ext/src/string.c
+++ b/mrbgems/mruby-string-ext/src/string.c
@@ -21,7 +21,8 @@ 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));
+ mrb_define_method(mrb, s, "dump", mrb_str_dump, ARGS_NONE());
+ mrb_define_method(mrb, s, "getbyte", mrb_str_getbyte, ARGS_REQ(1));
}
void
diff --git a/mrbgems/mruby-string-ext/test/string.rb b/mrbgems/mruby-string-ext/test/string.rb
index 9a7579131..eaff81890 100644
--- a/mrbgems/mruby-string-ext/test/string.rb
+++ b/mrbgems/mruby-string-ext/test/string.rb
@@ -13,3 +13,60 @@ assert('String#getbyte') do
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
diff --git a/src/array.c b/src/array.c
index b8cd436f0..97f76f8da 100644
--- a/src/array.c
+++ b/src/array.c
@@ -89,21 +89,6 @@ array_copy(mrb_value *dst, const mrb_value *src, size_t size)
}
}
-
-mrb_value
-mrb_ary_new_from_values(mrb_state *mrb, mrb_int size, mrb_value *vals)
-{
- mrb_value ary;
- struct RArray *a;
-
- ary = mrb_ary_new_capa(mrb, size);
- a = mrb_ary_ptr(ary);
- array_copy(a->ptr, vals, size);
- a->len = size;
-
- return ary;
-}
-
mrb_value
mrb_assoc_new(mrb_state *mrb, mrb_value car, mrb_value cdr)
{
@@ -433,25 +418,19 @@ mrb_ary_reverse(mrb_state *mrb, mrb_value self)
}
mrb_value
-mrb_ary_new4(mrb_state *mrb, mrb_int n, const mrb_value *elts)
+mrb_ary_new_from_values(mrb_state *mrb, mrb_int size, const mrb_value *vals)
{
mrb_value ary;
+ struct RArray *a;
- ary = mrb_ary_new_capa(mrb, n);
- if (n > 0 && elts) {
- array_copy(RARRAY_PTR(ary), elts, n);
- RARRAY_LEN(ary) = n;
- }
+ ary = mrb_ary_new_capa(mrb, size);
+ a = mrb_ary_ptr(ary);
+ array_copy(a->ptr, vals, size);
+ a->len = size;
return ary;
}
-mrb_value
-mrb_ary_new_elts(mrb_state *mrb, mrb_int n, const mrb_value *elts)
-{
- return mrb_ary_new4(mrb, n, elts);
-}
-
void
mrb_ary_push(mrb_state *mrb, mrb_value ary, mrb_value elem) /* mrb_ary_push */
{
diff --git a/src/vm.c b/src/vm.c
index e33f08a90..dd8edf4bc 100644
--- a/src/vm.c
+++ b/src/vm.c
@@ -1061,7 +1061,7 @@ mrb_run(mrb_state *mrb, struct RProc *proc, mrb_value self)
stack = e->stack + 1;
}
if (r == 0) {
- regs[a] = mrb_ary_new_elts(mrb, m1+m2, stack);
+ regs[a] = mrb_ary_new_from_values(mrb, m1+m2, stack);
}
else {
mrb_value *pp = NULL;
@@ -1149,7 +1149,7 @@ mrb_run(mrb_state *mrb, struct RProc *proc, mrb_value self)
memmove(&regs[1], argv, sizeof(mrb_value)*(m1+o)); /* m1 + o */
}
if (r) { /* r */
- regs[m1+o+1] = mrb_ary_new_elts(mrb, argc-m1-o-m2, argv+m1+o);
+ regs[m1+o+1] = mrb_ary_new_from_values(mrb, argc-m1-o-m2, argv+m1+o);
}
if (m2) {
memmove(&regs[m1+o+r+1], &argv[argc-m2], sizeof(mrb_value)*m2);
@@ -1731,7 +1731,7 @@ mrb_run(mrb_state *mrb, struct RProc *proc, mrb_value self)
int i;
if (len > pre + post) {
- regs[a++] = mrb_ary_new_elts(mrb, len - pre - post, ary->ptr+pre);
+ regs[a++] = mrb_ary_new_from_values(mrb, len - pre - post, ary->ptr+pre);
while (post--) {
regs[a++] = ary->ptr[len-post-1];
}
diff --git a/tools/mrbc/mrbc.c b/tools/mrbc/mrbc.c
index bc551fffc..698586ec0 100644
--- a/tools/mrbc/mrbc.c
+++ b/tools/mrbc/mrbc.c
@@ -83,6 +83,12 @@ parse_args(mrb_state *mrb, int argc, char **argv, struct _args *args)
switch ((*argv)[1]) {
case 'o':
+ if (outfile) {
+ printf("%s: An output file is already specified. (%s)\n",
+ *origargv, outfile);
+ result = -5;
+ goto exit;
+ }
outfile = get_outfilename((*argv) + 2, "");
break;
case 'B':