Beispiel #1
0
/*
 *  call-seq:
 *     str.prepend(other_str)  -> str
 *
 *  Prepend---Prepend the given string to <i>str</i>.
 *
 *     a = "world"
 *     a.prepend("hello ") #=> "hello world"
 *     a                   #=> "hello world"
 */
static mrb_value
mrb_str_prepend(mrb_state *mrb, mrb_value self)
{
  struct RString *s1 = mrb_str_ptr(self), *s2, *temp_s;
  mrb_int len;
  mrb_value other, temp_str;

  mrb_get_args(mrb, "S", &other);

  mrb_str_modify(mrb, s1);
  if (!mrb_string_p(other)) {
    other = mrb_str_to_str(mrb, other);
  }
  s2 = mrb_str_ptr(other);
  len = RSTR_LEN(s1) + RSTR_LEN(s2);
  temp_str = mrb_str_new(mrb, NULL, RSTR_LEN(s1));
  temp_s = mrb_str_ptr(temp_str);
  memcpy(RSTR_PTR(temp_s), RSTR_PTR(s1), RSTR_LEN(s1));
  if (RSTRING_CAPA(self) < len) {
    mrb_str_resize(mrb, self, len);
  }
  memcpy(RSTR_PTR(s1), RSTR_PTR(s2), RSTR_LEN(s2));
  memcpy(RSTR_PTR(s1) + RSTR_LEN(s2), RSTR_PTR(temp_s), RSTR_LEN(temp_s));
  RSTR_SET_LEN(s1, len);
  RSTR_PTR(s1)[len] = '\0';
  return self;
}
Beispiel #2
0
static mrb_value
mrb_LZ4_compress_default(mrb_state *mrb, mrb_value self)
{
  char *source;
  mrb_int source_size;

  mrb_get_args(mrb, "s", &source, &source_size);

  int maxDestSize = LZ4_COMPRESSBOUND(source_size);
  if (likely(maxDestSize)) {
    mrb_value dest = mrb_str_buf_new(mrb, maxDestSize);
    int destSize = LZ4_compress_default(source, RSTRING_PTR(dest), source_size, RSTRING_CAPA(dest));
    if (likely(destSize)) {
      return mrb_str_resize(mrb, dest, destSize);
    } else {
      mrb_raise(mrb, E_LZ4_ERROR, "cannot compress");
    }
  } else {
    mrb_raise(mrb, E_ARGUMENT_ERROR, "source_size is too large");
  }
}
Beispiel #3
0
static mrb_value
mrb_http_parser_execute(mrb_state *mrb, mrb_value self)
{
  mrb_value arg_data;
  mrb_value value_context;
  mrb_http_parser_context* context;

  value_context = mrb_iv_get(mrb, self, mrb_intern(mrb, "context"));
  Data_Get_Struct(mrb, value_context, &http_parser_context_type, context);
  if (!context) {
    mrb_raise(mrb, E_ARGUMENT_ERROR, "invalid argument");
  }

  mrb_get_args(mrb, "o", &arg_data);
  if (mrb_nil_p(arg_data)) {
    mrb_raise(mrb, E_ARGUMENT_ERROR, "invalid argument");
  }

  http_parser_execute(&context->parser, &context->settings, (char*) RSTRING_PTR(arg_data), RSTRING_CAPA(arg_data));

  return mrb_nil_value();
}
Beispiel #4
0
static mrb_value
mrb_LZ4_decompress_safe(mrb_state *mrb, mrb_value self)
{
  char *source;
  mrb_int compressedSize, maxDecompressedSize;

  mrb_get_args(mrb, "si", &source, &compressedSize, &maxDecompressedSize);

  if (unlikely(maxDecompressedSize < 0)) {
    mrb_raise(mrb, E_ARGUMENT_ERROR, "maxDecompressedSize mustn't be negative");
  }

  mrb_value dest = mrb_str_buf_new(mrb, maxDecompressedSize);

  int decompressedSize = LZ4_decompress_safe(source, RSTRING_PTR(dest), compressedSize, RSTRING_CAPA(dest));
  if (decompressedSize >= 0) {
    return mrb_str_resize(mrb, dest, decompressedSize);
  } else {
    mrb_raise(mrb, E_LZ4_ERROR, "cannot decompress, source may be malformed or maxDecompressedSize is too small");
  }
}
Beispiel #5
0
static mrb_value
mrb_http_parser_parse(mrb_state *mrb, mrb_value self)
{
    mrb_value arg_data;
    mrb_value value_context;
    mrb_http_parser_context* context;
    struct RProc *b = NULL;

    value_context = mrb_iv_get(mrb, self, mrb_intern(mrb, "parser_context"));
    Data_Get_Struct(mrb, value_context, &http_parser_context_type, context);
    if (!context) {
        mrb_raise(mrb, E_ARGUMENT_ERROR, "invalid argument");
    }

    mrb_get_args(mrb, "bo", &b, &arg_data);
    if (mrb_nil_p(arg_data)) {
        mrb_raise(mrb, E_ARGUMENT_ERROR, "invalid argument");
    }
    mrb_iv_set(mrb, self, mrb_intern(mrb, "complete_cb"), b ? mrb_obj_value(b) : mrb_nil_value());

    context->parser.data = context;
    context->was_header_value = TRUE;
    CTXV_SET(context, "headers", mrb_hash_new(mrb, 32));

    http_parser_init(&context->parser, HTTP_REQUEST);

    context->settings.on_url = parser_settings_on_url;
    context->settings.on_header_field = parser_settings_on_header_field;
    context->settings.on_header_value = parser_settings_on_header_value;
    context->settings.on_headers_complete = parser_settings_on_headers_complete;
    if (b) {
        context->settings.on_message_complete = parser_settings_on_message_complete;
    }

    if (RSTRING_LEN(arg_data) > 0) {
        http_parser_execute(&context->parser, &context->settings, (char*) RSTRING_PTR(arg_data), RSTRING_CAPA(arg_data));
    }

    return mrb_nil_value();
}