Implement `Kernel#print` and `Kernel#puts` in C.
MODIFY
mrbgems/mruby-print/mrblib/print.rb
mrbgems/mruby-print/mrblib/print.rb
#
# ISO 15.3.1
module Kernel
- ##
- # Invoke method +print+ on STDOUT and passing +*args+
- #
- # ISO 15.3.1.2.10
- def print(*args)
- i = 0
- len = args.size
- while i < len
- __printstr__ args[i].to_s
- i += 1
- end
- end
-
- ##
- # Invoke method +puts+ on STDOUT and passing +*args*+
- #
- # ISO 15.3.1.2.11
- def puts(*args)
- i = 0
- len = args.size
- while i < len
- s = args[i].to_s
- __printstr__ s
- __printstr__ "\n" if (s[-1] != "\n")
- i += 1
- end
- __printstr__ "\n" if len == 0
- nil
- end
-
##
# Print human readable object description
#
+ # ISO 15.3.1.2.9
# ISO 15.3.1.3.34
def p(*args)
i = 0

MODIFY
mrbgems/mruby-print/src/print.c
mrbgems/mruby-print/src/print.c
#endif
static void
-printstr(mrb_state *mrb, mrb_value obj)
+printstr(mrb_state *mrb, const char *p, size_t len)
{
- if (mrb_string_p(obj)) {
#if defined(_WIN32)
if (isatty(fileno(stdout))) {
DWORD written;
- int mlen = (int)RSTRING_LEN(obj);
- char* utf8 = RSTRING_PTR(obj);
- int wlen = MultiByteToWideChar(CP_UTF8, 0, utf8, mlen, NULL, 0);
+ int wlen = MultiByteToWideChar(CP_UTF8, 0, p, len, NULL, 0);
wchar_t* utf16 = (wchar_t*)mrb_malloc(mrb, (wlen+1) * sizeof(wchar_t));
- if (MultiByteToWideChar(CP_UTF8, 0, utf8, mlen, utf16, wlen) > 0) {
+ if (MultiByteToWideChar(CP_UTF8, 0, p, len, utf16, wlen) > 0) {
utf16[wlen] = 0;
WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE),
utf16, wlen, &written, NULL);
@@ -35,21 +32,55 @@ printstr(mrb_state *mrb, mrb_value obj)
mrb_free(mrb, utf16);
} else
#endif
- fwrite(RSTRING_PTR(obj), RSTRING_LEN(obj), 1, stdout);
+ fwrite(p, len, 1, stdout);
fflush(stdout);
}
-}
-/* 15.3.1.2.9 */
-/* 15.3.1.3.34 */
-mrb_value
+static mrb_value
mrb_printstr(mrb_state *mrb, mrb_value self)
{
- mrb_value argv = mrb_get_arg1(mrb);
+ mrb_value s = mrb_get_arg1(mrb);
+
+ if (mrb_string_p(s)) {
+ printstr(mrb, RSTRING_PTR(s), RSTRING_LEN(s));
+ }
+ return s;
+}
+
+/* 15.3.1.2.10 */
+/* 15.3.1.3.35 */
+static mrb_value
+mrb_print(mrb_state *mrb, mrb_value self)
+{
+ mrb_int argc, i;
+ mrb_value *argv;
- printstr(mrb, argv);
+ mrb_get_args(mrb, "*", &argv, &argc);
+ for (i=0; i<argc; i++) {
+ mrb_value s = mrb_str_to_str(mrb, argv[i]);
+ printstr(mrb, RSTRING_PTR(s), RSTRING_LEN(s));
+ }
+ return mrb_nil_value();
+}
+
+/* 15.3.1.2.11 */
+/* 15.3.1.3.39 */
+static mrb_value
+mrb_puts(mrb_state *mrb, mrb_value self)
+{
+ mrb_int argc, i;
+ mrb_value *argv;
- return argv;
+ mrb_get_args(mrb, "*", &argv, &argc);
+ for (i=0; i<argc; i++) {
+ mrb_value s = mrb_str_to_str(mrb, argv[i]);
+ mrb_int len = RSTRING_LEN(s);
+ printstr(mrb, RSTRING_PTR(s), len);
+ if (len > 0 && RSTRING_PTR(s)[len-1] != '\n') {
+ printstr(mrb, "\n", 1);
+ }
+ }
+ return mrb_nil_value();
}
void
@@ -58,6 +89,8 @@ mrb_mruby_print_gem_init(mrb_state* mrb)
struct RClass *krn;
krn = mrb->kernel_module;
mrb_define_method(mrb, krn, "__printstr__", mrb_printstr, MRB_ARGS_REQ(1));
+ mrb_define_method(mrb, krn, "print", mrb_print, MRB_ARGS_ANY());
+ mrb_define_method(mrb, krn, "puts", mrb_puts, MRB_ARGS_ANY());
}
void

Implement `Kernel#print` and `Kernel#puts` in C.