<commit_before>#include "ruby.h"
#include "snappy.h"

static VALUE rb_mSnappy;
static VALUE rb_eSnappy;

static VALUE
snappy_deflate(int argc, VALUE *argv, VALUE self)
{
    VALUE src, dst;
    size_t  output_length;

    rb_scan_args(argc, argv, "11", &src, &dst);
    StringValue(src);

    output_length = snappy::MaxCompressedLength(RSTRING_LEN(src));

    if (NIL_P(dst)) {
        dst = rb_str_new(NULL, output_length);
    } else {
    	StringValue(dst);
    	rb_str_resize(dst, output_length);
    }

    snappy::RawCompress(RSTRING_PTR(src), RSTRING_LEN(src), RSTRING_PTR(dst), &output_length);
    rb_str_resize(dst, output_length);

    return dst;
}

static VALUE
snappy_inflate(int argc, VALUE *argv, VALUE self)
{
    VALUE src, dst;
    size_t output_length;

    rb_scan_args(argc, argv, "11", &src, &dst);
    StringValue(src);

    if (!snappy::GetUncompressedLength(RSTRING_PTR(src), RSTRING_LEN(src), &output_length)) {
        rb_raise(rb_eSnappy, "snappy::GetUncompressedLength");
    }

    if (NIL_P(dst)) {
        dst = rb_str_new(NULL, output_length);
    } else {
    	StringValue(dst);
    	rb_str_resize(dst, output_length);
    }

    if (!snappy::RawUncompress(RSTRING_PTR(src), RSTRING_LEN(src), RSTRING_PTR(dst))) {
        rb_raise(rb_eSnappy, "snappy::RawUncompress");
    }

    return dst;
}

extern "C" {
void Init_snappy()
{
    rb_mSnappy = rb_define_module("Snappy");
    rb_eSnappy = rb_define_class_under(rb_mSnappy, "Error", rb_eStandardError);
    rb_define_singleton_method(rb_mSnappy, "deflate", (VALUE (*)(...))snappy_deflate, -1);
    rb_define_singleton_method(rb_mSnappy, "inflate", (VALUE (*)(...))snappy_inflate, -1);

    rb_define_singleton_method(rb_mSnappy, "compress", (VALUE (*)(...))snappy_deflate, -1);
    rb_define_singleton_method(rb_mSnappy, "uncompress", (VALUE (*)(...))snappy_inflate, -1);

    rb_require("snappy/writer");
    rb_require("snappy/reader");
}
}<commit_msg>Use alias instead define for native functions<commit_after>#include "ruby.h"
#include "snappy.h"

static VALUE rb_mSnappy;
static VALUE rb_eSnappy;

static VALUE
snappy_deflate(int argc, VALUE *argv, VALUE self)
{
    VALUE src, dst;
    size_t  output_length;

    rb_scan_args(argc, argv, "11", &src, &dst);
    StringValue(src);

    output_length = snappy::MaxCompressedLength(RSTRING_LEN(src));

    if (NIL_P(dst)) {
        dst = rb_str_new(NULL, output_length);
    } else {
    	StringValue(dst);
    	rb_str_resize(dst, output_length);
    }

    snappy::RawCompress(RSTRING_PTR(src), RSTRING_LEN(src), RSTRING_PTR(dst), &output_length);
    rb_str_resize(dst, output_length);

    return dst;
}

static VALUE
snappy_inflate(int argc, VALUE *argv, VALUE self)
{
    VALUE src, dst;
    size_t output_length;

    rb_scan_args(argc, argv, "11", &src, &dst);
    StringValue(src);

    if (!snappy::GetUncompressedLength(RSTRING_PTR(src), RSTRING_LEN(src), &output_length)) {
        rb_raise(rb_eSnappy, "snappy::GetUncompressedLength");
    }

    if (NIL_P(dst)) {
        dst = rb_str_new(NULL, output_length);
    } else {
    	StringValue(dst);
    	rb_str_resize(dst, output_length);
    }

    if (!snappy::RawUncompress(RSTRING_PTR(src), RSTRING_LEN(src), RSTRING_PTR(dst))) {
        rb_raise(rb_eSnappy, "snappy::RawUncompress");
    }

    return dst;
}

extern "C" {
void Init_snappy()
{
    rb_mSnappy = rb_define_module("Snappy");
    rb_eSnappy = rb_define_class_under(rb_mSnappy, "Error", rb_eStandardError);
    rb_define_singleton_method(rb_mSnappy, "deflate", (VALUE (*)(...))snappy_deflate, -1);
    rb_define_singleton_method(rb_mSnappy, "inflate", (VALUE (*)(...))snappy_inflate, -1);

    VALUE rb_mSnappy_singleton = rb_singleton_class(rb_mSnappy);

    rb_define_alias(rb_mSnappy_singleton, "compress", "deflate");
    rb_define_alias(rb_mSnappy_singleton, "load", "deflate");

    rb_define_alias(rb_mSnappy_singleton, "uncompress", "inflate");
    rb_define_alias(rb_mSnappy_singleton, "dump", "inflate");
    
    rb_require("snappy/writer");
    rb_require("snappy/reader");
}
}
{'fraction_non_alphanumeric': 0.14296278037959084, 'fraction_numerical': 0.0034508257333004683, 'mean_word_length': 4.47638326585695, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5056024', 'n_tokens_mistral': 1577, 'n_tokens_neox': 1541, 'n_words': 255}