Skip to content
Open

gaps #10

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
126 changes: 122 additions & 4 deletions c-gaps/credit.c
Original file line number Diff line number Diff line change
@@ -1,9 +1,127 @@
#include <lua.h>
#include <stdlib.h>
#include <lauxlib.h>

// implement it here
int TTT(lua_State* L) {
luaL_argcheck(L, lua_type(L, 1) == LUA_TTABLE , 1, "Must table");
int i = 0, j = 0;
size_t len1, len2, len3;

int luaopen_gaps(lua_State* L) {
// return it here
return 0;
lua_rawgeti(L, 1, 1);
lua_rawgeti(L, 1, 2);
lua_rawgeti(L, 1, 3);

if ((lua_type(L, 3) == LUA_TNIL)&& (lua_type(L, 4) == LUA_TNIL)&& (lua_type(L, 2) != LUA_TNIL)) {
luaL_argcheck(L, lua_type(L, 2) == LUA_TSTRING , 2, "Must string");
const char* str1 = luaL_checklstring(L, 2, &len1);
char* charD1 = malloc(len1);
for (i = 0; i < len1; i++) {
if ((str1[i] == '-')) {

} else {
charD1[j] = str1[i];
j = j + 1;
}
}
for (i = j; i < len1; i++) {
charD1[i] = ' ';
}
lua_newtable(L);
lua_pushlstring(L, charD1, j);
lua_rawseti(L, 5, 1);
free(charD1);



} else if ((lua_type(L, 3) == LUA_TNIL)&& (lua_type(L, 4) == LUA_TNIL)&& (lua_type(L, 2) == LUA_TNIL)){
lua_newtable(L);



} else if ((lua_type(L, 4) == LUA_TNIL)&& (lua_type(L, 2) != LUA_TNIL)&&(lua_type(L, 3) != LUA_TNIL)) {
luaL_argcheck(L, lua_type(L, 2) == LUA_TSTRING , 2, "Must string");
const char* str1 = luaL_checklstring(L, 2, &len1);

luaL_argcheck(L, lua_type(L, 2) == LUA_TSTRING , 3, "Must string");
const char* str2 = luaL_checklstring(L, 3, &len2);
luaL_argcheck(L, len1 == len2, 1, "Strings must be equal");
char* charD1 = malloc(len1);
char* charD2 = malloc(len1);


for (i = 0; i < len1; i++) {
if ((str1[i] == '-') && (str2[i] == '-')) {

} else {
charD1[j] = str1[i];
charD2[j] = str2[i];
j = j + 1;
}
}
for (i = j; i < len1; i++) {
charD1[i] = ' ';
charD2[i] = ' ';

}
lua_newtable(L);
lua_pushlstring(L, charD1, j);
lua_rawseti(L, 5, 1);
lua_pushlstring(L, charD2, j);
lua_rawseti(L, 5, 2);

free(charD1);
free(charD2);



} else {
luaL_argcheck(L, lua_type(L, 2) == LUA_TSTRING , 2, "Must string");
const char* str1 = luaL_checklstring(L, 2, &len1);


luaL_argcheck(L, lua_type(L, 3) == LUA_TSTRING , 3, "Must string");
const char* str2 = luaL_checklstring(L, 3, &len2);
luaL_argcheck(L, lua_type(L, 4) == LUA_TSTRING , 4, "Must string");
const char* str3 = luaL_checklstring(L, 4, &len3);
luaL_argcheck(L, len1 == len2, 1, "Strings must be equal");
luaL_argcheck(L, len2 == len3, 1, "Strings must be equal");
char* charD1 = malloc(len1);
char* charD2 = malloc(len1);
char* charD3 = malloc(len1);
for (i = 0; i < len1; i++) {
if ((str1[i] == '-') && (str2[i] == '-') && (str3[i] == '-')) {

} else {
charD1[j] = str1[i];
charD2[j] = str2[i];
charD3[j] = str3[i];
j = j + 1;
}
}
for (i = j; i < len1; i++) {
charD1[i] = ' ';
charD2[i] = ' ';
charD3[i] = ' ';
}
lua_newtable(L);
lua_pushlstring(L, charD1, j);
lua_rawseti(L, 5, 1);
lua_pushlstring(L, charD2, j);
lua_rawseti(L, 5, 2);
lua_pushlstring(L, charD3, j);
lua_rawseti(L, 5, 3);


free(charD1);
free(charD2);
free(charD3);

}

return 1;
}

int luaopen_gaps(lua_State* L){
lua_pushcfunction(L, &TTT);
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