예제 #1
0
static int
trap_signm(mrb_state *mrb, mrb_value vsig)
{
  int sig = -1;
  const char *s;

  switch (mrb_type(vsig)) {
    case MRB_TT_FIXNUM:
      sig = mrb_fixnum(vsig);
      if (sig < 0 || sig >= NSIG) {
        mrb_raisef(mrb, E_ARGUMENT_ERROR, "invalid signal number (%S)", vsig);
      }
      break;
    case MRB_TT_SYMBOL:
      s = mrb_sym2name(mrb, mrb_symbol(vsig));
      if (!s) mrb_raise(mrb, E_ARGUMENT_ERROR, "bad signal");
      goto str_signal;
    default:
      vsig = mrb_string_type(mrb, vsig);
      s = RSTRING_PTR(vsig);

str_signal:
      if (memcmp("SIG", s, 3) == 0)
        s += 3;
      sig = signm2signo(s);
      if (sig == 0 && strcmp(s, "EXIT") != 0)
        mrb_raise(mrb, E_ARGUMENT_ERROR, "unsupported signal");
      break;
  }
  return sig;
}
예제 #2
0
파일: string.c 프로젝트: Everysick/mruby
/*
 *  call-seq:
 *     str << integer       -> str
 *     str.concat(integer)  -> str
 *     str << obj           -> str
 *     str.concat(obj)      -> str
 *
 *  Append---Concatenates the given object to <i>str</i>. If the object is a
 *  <code>Integer</code>, it is considered as a codepoint, and is converted
 *  to a character before concatenation.
 *
 *     a = "hello "
 *     a << "world"   #=> "hello world"
 *     a.concat(33)   #=> "hello world!"
 */
static mrb_value
mrb_str_concat2(mrb_state *mrb, mrb_value self)
{
  mrb_value str;

  mrb_get_args(mrb, "o", &str);
  if (mrb_fixnum_p(str))
    str = mrb_fixnum_chr(mrb, str);
  else
    str = mrb_string_type(mrb, str);
  mrb_str_concat(mrb, self, str);
  return self;
}
예제 #3
0
파일: string.c 프로젝트: Asmod4n/mruby
/*
 *  call-seq:
 *     str.start_with?([prefixes]+)   -> true or false
 *
 *  Returns true if +str+ starts with one of the +prefixes+ given.
 *
 *    "hello".start_with?("hell")               #=> true
 *
 *    # returns true if one of the prefixes matches.
 *    "hello".start_with?("heaven", "hell")     #=> true
 *    "hello".start_with?("heaven", "paradise") #=> false
 *    "h".start_with?("heaven", "hell")         #=> false
 */
static mrb_value
mrb_str_start_with(mrb_state *mrb, mrb_value self)
{
  mrb_value *argv, sub;
  mrb_int argc, i;
  mrb_get_args(mrb, "*", &argv, &argc);

  for (i = 0; i < argc; i++) {
    size_t len_l, len_r;
    int ai = mrb_gc_arena_save(mrb);
    sub = mrb_string_type(mrb, argv[i]);
    mrb_gc_arena_restore(mrb, ai);
    len_l = RSTRING_LEN(self);
    len_r = RSTRING_LEN(sub);
    if (len_l >= len_r) {
      if (memcmp(RSTRING_PTR(self), RSTRING_PTR(sub), len_r) == 0) {
        return mrb_true_value();
      }
    }
  }
  return mrb_false_value();
}