diff options
| author | Yukihiro Matsumoto <[email protected]> | 2012-05-31 16:33:03 +0900 |
|---|---|---|
| committer | Yukihiro Matsumoto <[email protected]> | 2012-05-31 16:33:03 +0900 |
| commit | 5450f925dab709efd426f1b7af9e4947077aef42 (patch) | |
| tree | 4ab64638fb1270f95534a7fe9c18b6ddc807a1f3 | |
| parent | b05014f6a9666b66935c33543edd9fcf451ae3a1 (diff) | |
| download | mruby-5450f925dab709efd426f1b7af9e4947077aef42.tar.gz mruby-5450f925dab709efd426f1b7af9e4947077aef42.zip | |
reimplement String#<=>
| -rw-r--r-- | src/string.c | 26 |
1 files changed, 25 insertions, 1 deletions
diff --git a/src/string.c b/src/string.c index 3417b63d6..774277d20 100644 --- a/src/string.c +++ b/src/string.c @@ -464,7 +464,31 @@ mrb_str_cmp(mrb_state *mrb, mrb_value str1, mrb_value str2) static mrb_value mrb_str_cmp_m(mrb_state *mrb, mrb_value str1) { - return mrb_nil_value(); + mrb_value str2; + mrb_int result; + + mrb_get_args(mrb, "o", &str2); + if (mrb_type(str2) != MRB_TT_STRING) { + if (!mrb_respond_to(mrb, str2, mrb_intern(mrb, "to_s"))) { + return mrb_nil_value(); + } + else if (!mrb_respond_to(mrb, str2, mrb_intern(mrb, "<=>"))) { + return mrb_nil_value(); + } + else { + mrb_value tmp = mrb_funcall(mrb, str2, "<=>", 1, str1); + + if (mrb_nil_p(tmp)) return mrb_nil_value(); + if (!mrb_fixnum(tmp)) { + return mrb_funcall(mrb, mrb_fixnum_value(0), "-", 1, tmp); + } + result = -mrb_fixnum(tmp); + } + } + else { + result = mrb_str_cmp(mrb, str1, str2); + } + return mrb_fixnum_value(result); } static int |
