<commit_before>#include <stdlib.h>
#include <assert.h>

#define LUA_LIB
#include <lua.h>
#include <lauxlib.h>

typedef struct {
    // ptr points to int[2*len] (starts + lengths)
    int* ptr_;
    int len_;
} Row;

static int* Row_starts(Row* self) {
    return self->ptr_;
}

static int* Row_lengths(Row* self) {
    return self->ptr_ + self->len_;
}

static void Row_constructor(Row* self, const char* text,
                            int row_length) {
    assert(row_length > 0);
    // count groups
    int groups = 0;
    char prev = '-';
    int bp;
    for (bp = 0; bp < row_length; bp++) {
        if (prev == '-' && text[bp] != '-') {
            groups += 1;
        }
        prev = text[bp];
    }
    groups += 1; // last pseudo-group stores lengths
    // * 2 because two arrays in one (starts + lengths)
    self->ptr_ = malloc(sizeof(int) * groups * 2);
    self->len_ = groups;
    int* starts = Row_starts(self);
    int* lengths = Row_lengths(self);
    // fill starts and lengths
    prev = '-';
    int group = 0;
    lengths[0] = 0;
    for (bp = 0; bp < row_length; bp++) {
        if (prev == '-' && text[bp] != '-') {
            // new nongap opened
            starts[group] = bp;
            lengths[group + 1] = lengths[group] + 1;
            group += 1;
        } else if (text[bp] != '-') {
            // increase length of last group of nongaps
            lengths[group] += 1;
        }
        prev = text[bp];
    }
    starts[group] = row_length;
}

// closure, first upvalue is metatable of Row instance
static int lua_Row_constructor(lua_State *L) {
    int args = lua_gettop(L);
    if (args != 2) {
        return luaL_error(L,
                "Row's constructor must be called "
                "with one argument (text of row)");
    }
    if (!lua_isstring(L, 2)) {
        return luaL_error(L,
                "Row's constructor must be called "
                 "with a string");
    }
    size_t len;
    const char* text = lua_tolstring(L, 2, &len);
    if (len == 0) {
        return luaL_error(L, "Row('') called");
    }
    Row* row = lua_newuserdata(L, sizeof(Row));
    Row_constructor(row, text, len);
    // get metatable of Row
    lua_pushvalue(L, lua_upvalueindex(1));
    lua_setmetatable(L, -2);
    return 1; // Row instance
}

static void Row_free(Row* self) {
    free(self->ptr_);
}

static int lua_Row_free(lua_State *L) {
    Row* row = lua_touserdata(L, 1);
    Row_free(row);
    return 0;
}

// bool
static int arrays_equal(int len, int* x, int* y) {
    int i;
    for (i = 0; i < len; i++) {
        if (x[i] != y[i]) {
            return 0;
        }
    }
    return 1;
}

// bool
static int Row_eq(Row* x, Row* y) {
    int len = x->len_;
    return x->len_ == y->len_ &&
        arrays_equal(len, Row_starts(x), Row_starts(y)) &&
        arrays_equal(len, Row_lengths(x), Row_lengths(y));
}

static int lua_Row_eq(lua_State *L) {
    int args = lua_gettop(L);
    if (args != 2) {
        return luaL_error(L, "to compare rows, I need 2 rows");
    }
    if (!lua_isuserdata(L, 2)) {
        return luaL_error(L, "compare a Row to Row");
    }
    Row* x = lua_touserdata(L, 1);
    Row* y = lua_touserdata(L, 2);
    int result = Row_eq(x, y);
    lua_pushboolean(L, result);
    return 1;
}

static int lua_Row_type(lua_State *L) {
    lua_pushstring(L, "Row");
    return 1;
}

static int Row_length(Row* self) {
    int last_index = self->len_ - 1;
    return Row_starts(self)[last_index];
}

static int lua_Row_length(lua_State *L) {<commit_msg>remove useless checks in Row.c<commit_after>#include <stdlib.h>
#include <assert.h>

#define LUA_LIB
#include <lua.h>
#include <lauxlib.h>

typedef struct {
    // ptr points to int[2*len] (starts + lengths)
    int* ptr_;
    int len_;
} Row;

static int* Row_starts(Row* self) {
    return self->ptr_;
}

static int* Row_lengths(Row* self) {
    return self->ptr_ + self->len_;
}

static void Row_constructor(Row* self, const char* text,
                            int row_length) {
    assert(row_length > 0);
    // count groups
    int groups = 0;
    char prev = '-';
    int bp;
    for (bp = 0; bp < row_length; bp++) {
        if (prev == '-' && text[bp] != '-') {
            groups += 1;
        }
        prev = text[bp];
    }
    groups += 1; // last pseudo-group stores lengths
    // * 2 because two arrays in one (starts + lengths)
    self->ptr_ = malloc(sizeof(int) * groups * 2);
    self->len_ = groups;
    int* starts = Row_starts(self);
    int* lengths = Row_lengths(self);
    // fill starts and lengths
    prev = '-';
    int group = 0;
    lengths[0] = 0;
    for (bp = 0; bp < row_length; bp++) {
        if (prev == '-' && text[bp] != '-') {
            // new nongap opened
            starts[group] = bp;
            lengths[group + 1] = lengths[group] + 1;
            group += 1;
        } else if (text[bp] != '-') {
            // increase length of last group of nongaps
            lengths[group] += 1;
        }
        prev = text[bp];
    }
    starts[group] = row_length;
}

// closure, first upvalue is metatable of Row instance
static int lua_Row_constructor(lua_State *L) {
    int args = lua_gettop(L);
    if (args != 2) {
        return luaL_error(L,
                "Row's constructor must be called "
                "with one argument (text of row)");
    }
    if (!lua_isstring(L, 2)) {
        return luaL_error(L,
                "Row's constructor must be called "
                 "with a string");
    }
    size_t len;
    const char* text = lua_tolstring(L, 2, &len);
    if (len == 0) {
        return luaL_error(L, "Row('') called");
    }
    Row* row = lua_newuserdata(L, sizeof(Row));
    Row_constructor(row, text, len);
    // get metatable of Row
    lua_pushvalue(L, lua_upvalueindex(1));
    lua_setmetatable(L, -2);
    return 1; // Row instance
}

static void Row_free(Row* self) {
    free(self->ptr_);
}

static int lua_Row_free(lua_State *L) {
    Row* row = lua_touserdata(L, 1);
    Row_free(row);
    return 0;
}

// bool
static int arrays_equal(int len, int* x, int* y) {
    int i;
    for (i = 0; i < len; i++) {
        if (x[i] != y[i]) {
            return 0;
        }
    }
    return 1;
}

// bool
static int Row_eq(Row* x, Row* y) {
    int len = x->len_;
    return x->len_ == y->len_ &&
        arrays_equal(len, Row_starts(x), Row_starts(y)) &&
        arrays_equal(len, Row_lengths(x), Row_lengths(y));
}

static int lua_Row_eq(lua_State *L) {
    int args = lua_gettop(L);
    Row* x = lua_touserdata(L, 1);
    Row* y = lua_touserdata(L, 2);
    int result = Row_eq(x, y);
    lua_pushboolean(L, result);
    return 1;
}

static int lua_Row_type(lua_State *L) {
    lua_pushstring(L, "Row");
    return 1;
}

static int Row_length(Row* self) {
    int last_index = self->len_ - 1;
    return Row_starts(self)[last_index];
}

static int lua_Row_length(lua_State *L) {
{'fraction_non_alphanumeric': 0.14685822086779857, 'fraction_numerical': 0.01001306051371354, 'mean_word_length': 2.189264229523369, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 33, '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': '312808', 'n_tokens_mistral': 2581, 'n_tokens_neox': 2459, 'n_words': 672}