Support MRB_WIHTOUT_FLOAT to mruby-sprintf
MODIFY
mrbgems/mruby-sprintf/src/sprintf.c
mrbgems/mruby-sprintf/src/sprintf.c
#include <mruby/string.h>
#include <mruby/hash.h>
#include <mruby/numeric.h>
+#ifndef MRB_WITHOUT_FLOAT
#include <math.h>
+#endif
#include <ctype.h>
#define BIT_DIGITS(N) (((N)*146)/485 + 1) /* log2(10) =~ 146/485 */
#define EXTENDSIGN(n, l) (((~0U << (n)) >> (((n)*(l)) % BITSPERDIG)) & ~(~0U << (n)))
mrb_value mrb_str_format(mrb_state *, mrb_int, const mrb_value *, mrb_value);
+#ifndef MRB_WITHOUT_FLOAT
static void fmt_setup(char*,size_t,int,int,mrb_int,mrb_int);
+#endif
static char*
remove_sign_bits(char *str, int base)
@@ -814,10 +818,12 @@ retry:
bin_retry:
switch (mrb_type(val)) {
+#ifndef MRB_WITHOUT_FLOAT
case MRB_TT_FLOAT:
val = mrb_flo_to_fixnum(mrb, val);
if (mrb_fixnum_p(val)) goto bin_retry;
break;
+#endif
case MRB_TT_STRING:
val = mrb_str_to_inum(mrb, val, 0, TRUE);
goto bin_retry;
@@ -990,6 +996,7 @@ retry:
}
break;
+#ifndef MRB_WITHOUT_FLOAT
case 'f':
case 'g':
case 'G':
@@ -1069,6 +1076,7 @@ retry:
blen += n;
}
break;
+#endif
}
flags = FNONE;
}
@@ -1088,6 +1096,7 @@ retry:
return result;
}
+#ifndef MRB_WITHOUT_FLOAT
static void
fmt_setup(char *buf, size_t size, int c, int flags, mrb_int width, mrb_int prec)
{
@@ -1114,3 +1123,4 @@ fmt_setup(char *buf, size_t size, int c, int flags, mrb_int width, mrb_int prec)
*buf++ = c;
*buf = '\0';
}
+#endif

MODIFY
mrbgems/mruby-sprintf/test/sprintf.rb
mrbgems/mruby-sprintf/test/sprintf.rb
assert('String#%') do
assert_equal "one=1", "one=%d" % 1
- assert_equal "1 one 1.0", "%d %s %3.1f" % [ 1, "one", 1.01 ]
+ assert_equal "1 one", "%d %s" % [ 1, "one" ]
+ assert_equal "1.0", "%3.1f" % 1.01 if class_defined?("Float")
assert_equal "123 < 456", "%{num} < %<str>s" % { num: 123, str: "456" }
assert_equal 15, ("%b" % (1<<14)).size
end
@@ -34,7 +35,7 @@ assert('String#% with inf') do
assert_equal " Inf", "% 3f" % inf
assert_equal " Inf", "% 4f" % inf
assert_equal " Inf", "% 5f" % inf
-end
+end if class_defined?("Float")
assert('String#% with nan') do
nan = Float::NAN
@@ -62,7 +63,7 @@ assert('String#% with nan') do
assert_equal " NaN", "% 3f" % nan
assert_equal " NaN", "% 4f" % nan
assert_equal " NaN", "% 5f" % nan
-end
+end if class_defined?("Float")
assert("String#% with invalid chr") do
begin

Support MRB_WIHTOUT_FLOAT to mruby-sprintf