Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 66 additions & 3 deletions c-gaps/credit.c
Original file line number Diff line number Diff line change
@@ -1,9 +1,72 @@
#include <lua.h>
#include <lauxlib.h>
#include <stdlib.h>

// implement it here
int removeGaps(lua_State* L) {
luaL_argcheck(L, lua_type(L, 1) == LUA_TTABLE, 1, "Please, input table");

size_t table_len = lua_objlen(L, -1);

if (table_len == 0) {
lua_newtable(L);
return 1;
}

const char** table = malloc(sizeof(char*) * table_len);
char** new_table = malloc(sizeof(char*) * table_len);

size_t len;
lua_rawgeti(L, 1, 1);
table[0] = luaL_checklstring(L, -1, &len);
luaL_argcheck(L, lua_type(L, -1) == LUA_TSTRING, -1, "Table members should be strings");
lua_pop(L, 1);

int i = 0;
int j = 0;
int k = 0;

for (i = 0; i < table_len; i++) {
new_table[i] = malloc(sizeof(char) * len);
}

for (i = 2; i <= table_len; i++) {
size_t local_len;
lua_rawgeti(L, 1, i);
table[i - 1] = luaL_checklstring(L, -1, &local_len);
luaL_argcheck(L, local_len == len, -1, "Strings should have equal length");
lua_pop(L, 1);
}

for (j = 0; j < len; j++) {
char gap_column = "smth";
for (i = 0; i < table_len; i++) {
if (table[i][j] != '-') {
gap_column = NULL;
}
}
if (! gap_column) {
for (i = 0; i < table_len; i++) {
new_table[i][k] = table[i][j];
}
k = k + 1;
}
}

lua_newtable(L);
for (i = 0; i < table_len; i++) {
lua_pushlstring(L, new_table[i], k);
lua_rawseti(L, -2, i + 1);
}

for (i = 0; i < table_len; i++) {
free(new_table[i]);
}
free(table);
free(new_table);
return 1;
}

int luaopen_gaps(lua_State* L) {
// return it here
return 0;
lua_pushcfunction(L, removeGaps);
return 1;
}
12 changes: 6 additions & 6 deletions c-gaps/spec.lua
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
describe("gaps (implementation)", function()
pending("loads module", function()
it("loads module", function()
local f = require 'gaps'
end)

pending("doesn't change good alignment", function()
it("doesn't change good alignment", function()
local f = require 'gaps'
assert.same({}, f({}))
assert.same({''}, f({''}))
Expand All @@ -14,7 +14,7 @@ describe("gaps (implementation)", function()
assert.same({'A'}, f({'A-'}))
end)

pending("removes gap columns", function()
it("removes gap columns", function()
local f = require 'gaps'
assert.same({''}, f({'-'}))
assert.same({'', ''}, f({'-', '-'}))
Expand All @@ -30,7 +30,7 @@ describe("gaps (implementation)", function()
}))
end)

pending("throws error if input is not table",
it("throws error if input is not table",
function()
local f = require 'gaps'
assert.has_error(function()
Expand All @@ -41,15 +41,15 @@ describe("gaps (implementation)", function()
end)
end)

pending("throws error if table members are not strings",
it("throws error if table members are not strings",
function()
local f = require 'gaps'
assert.has_error(function()
f({{1,2,3}, {}})
end)
end)

pending("throws error if strings' lengths differ",
it("throws error if strings' lengths differ",
function()
local f = require 'gaps'
assert.has_error(function()
Expand Down