diff --git a/c-gaps/credit.c b/c-gaps/credit.c index 2a60e39..aff4476 100644 --- a/c-gaps/credit.c +++ b/c-gaps/credit.c @@ -1,9 +1,72 @@ #include #include +#include -// 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; } diff --git a/c-gaps/spec.lua b/c-gaps/spec.lua index 2129000..498c005 100644 --- a/c-gaps/spec.lua +++ b/c-gaps/spec.lua @@ -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({''})) @@ -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({'-', '-'})) @@ -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() @@ -41,7 +41,7 @@ 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() @@ -49,7 +49,7 @@ describe("gaps (implementation)", function() 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()