summaryrefslogtreecommitdiffhomepage
path: root/mrbgems
diff options
context:
space:
mode:
Diffstat (limited to 'mrbgems')
-rw-r--r--mrbgems/mruby-range-ext/src/range.c8
-rw-r--r--mrbgems/mruby-sprintf/src/sprintf.c4
-rw-r--r--mrbgems/mruby-string-ext/mrblib/string.rb22
-rw-r--r--mrbgems/mruby-string-ext/test/string.rb20
4 files changed, 44 insertions, 10 deletions
diff --git a/mrbgems/mruby-range-ext/src/range.c b/mrbgems/mruby-range-ext/src/range.c
index 4e17dac8c..c980ecffd 100644
--- a/mrbgems/mruby-range-ext/src/range.c
+++ b/mrbgems/mruby-range-ext/src/range.c
@@ -7,7 +7,7 @@ r_le(mrb_state *mrb, mrb_value a, mrb_value b)
mrb_value r = mrb_funcall(mrb, a, "<=>", 1, b); /* compare result */
/* output :a < b => -1, a = b => 0, a > b => +1 */
- if (mrb_type(r) == MRB_TT_FIXNUM) {
+ if (mrb_fixnum_p(r)) {
mrb_int c = mrb_fixnum(r);
if (c == 0 || c == -1) return TRUE;
}
@@ -21,11 +21,7 @@ r_lt(mrb_state *mrb, mrb_value a, mrb_value b)
mrb_value r = mrb_funcall(mrb, a, "<=>", 1, b);
/* output :a < b => -1, a = b => 0, a > b => +1 */
- if (mrb_type(r) == MRB_TT_FIXNUM) {
- if (mrb_fixnum(r) == -1) return TRUE;
- }
-
- return FALSE;
+ return mrb_fixnum_p(r) && mrb_fixnum(r) == -1;
}
/*
diff --git a/mrbgems/mruby-sprintf/src/sprintf.c b/mrbgems/mruby-sprintf/src/sprintf.c
index 6479b19bc..5485f5098 100644
--- a/mrbgems/mruby-sprintf/src/sprintf.c
+++ b/mrbgems/mruby-sprintf/src/sprintf.c
@@ -15,10 +15,6 @@
#include <math.h>
#include <ctype.h>
-#ifdef HAVE_IEEEFP_H
-#include <ieeefp.h>
-#endif
-
#define BIT_DIGITS(N) (((N)*146)/485 + 1) /* log2(10) =~ 146/485 */
#define BITSPERDIG (sizeof(mrb_int)*CHAR_BIT)
#define EXTENDSIGN(n, l) (((~0 << (n)) >> (((n)*(l)) % BITSPERDIG)) & ~(~0 << (n)))
diff --git a/mrbgems/mruby-string-ext/mrblib/string.rb b/mrbgems/mruby-string-ext/mrblib/string.rb
index 005438b38..a517aa209 100644
--- a/mrbgems/mruby-string-ext/mrblib/string.rb
+++ b/mrbgems/mruby-string-ext/mrblib/string.rb
@@ -49,4 +49,26 @@ class String
def casecmp(str)
self.downcase <=> str.downcase
end
+
+ def partition(sep)
+ raise TypeError, "type mismatch: #{sep.class} given" unless sep.is_a? String
+ n = index(sep)
+ unless n.nil?
+ m = n + sep.size
+ [ slice(0, n), sep, slice(m, size - m) ]
+ else
+ [ self, "", "" ]
+ end
+ end
+
+ def rpartition(sep)
+ raise TypeError, "type mismatch: #{sep.class} given" unless sep.is_a? String
+ n = rindex(sep)
+ unless n.nil?
+ m = n + sep.size
+ [ slice(0, n), sep, slice(m, size - m) ]
+ else
+ [ "", "", self ]
+ end
+ end
end
diff --git a/mrbgems/mruby-string-ext/test/string.rb b/mrbgems/mruby-string-ext/test/string.rb
index 3ab959437..67ed628c0 100644
--- a/mrbgems/mruby-string-ext/test/string.rb
+++ b/mrbgems/mruby-string-ext/test/string.rb
@@ -114,3 +114,23 @@ assert('String#end_with?') do
assert_true !"ng".end_with?("ing", "mng")
assert_raise TypeError do "hello".end_with?(true) end
end
+
+assert('String#partition') do
+ assert_equal ["a", "x", "axa"], "axaxa".partition("x")
+ assert_equal ["aaaaa", "", ""], "aaaaa".partition("x")
+ assert_equal ["", "", "aaaaa"], "aaaaa".partition("")
+ assert_equal ["", "a", "aaaa"], "aaaaa".partition("a")
+ assert_equal ["aaaa", "b", ""], "aaaab".partition("b")
+ assert_equal ["", "b", "aaaa"], "baaaa".partition("b")
+ assert_equal ["", "", ""], "".partition("a")
+end
+
+assert('String#rpartition') do
+ assert_equal ["axa", "x", "a"], "axaxa".rpartition("x")
+ assert_equal ["", "", "aaaaa"], "aaaaa".rpartition("x")
+ assert_equal ["aaaaa", "", ""], "aaaaa".rpartition("")
+ assert_equal ["aaaa", "a", ""], "aaaaa".rpartition("a")
+ assert_equal ["aaaa", "b", ""], "aaaab".rpartition("b")
+ assert_equal ["", "b", "aaaa"], "baaaa".rpartition("b")
+ assert_equal ["", "", ""], "".rpartition("a")
+end