-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add regex library with 'new' and 'match'
- Loading branch information
Showing
37 changed files
with
3,652 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
#define LUA_LIB | ||
#include "lualib.h" | ||
|
||
#include "vendor/Soup/soup/Regex.hpp" | ||
|
||
static soup::Regex* checkregex (lua_State *L, int i) { | ||
return (soup::Regex*)luaL_checkudata(L, i, "pluto:regex"); | ||
} | ||
|
||
static int regex_new (lua_State *L) { | ||
new (lua_newuserdata(L, sizeof(soup::Regex))) soup::Regex{ soup::Regex::fromFullString(pluto_checkstring(L, 1)) }; | ||
if (luaL_newmetatable(L, "pluto:regex")) { | ||
lua_pushliteral(L, "__index"); | ||
luaL_loadbuffer(L, "return require\"pluto:regex\"", 27, 0); | ||
lua_call(L, 0, 1); | ||
lua_settable(L, -3); | ||
lua_pushliteral(L, "__gc"); | ||
lua_pushcfunction(L, [](lua_State *L) { | ||
std::destroy_at<>(checkregex(L, 1)); | ||
return 0; | ||
}); | ||
lua_settable(L, -3); | ||
} | ||
lua_setmetatable(L, -2); | ||
return 1; | ||
} | ||
|
||
static int regex_match (lua_State *L) { | ||
size_t len; | ||
const char *str = luaL_checklstring(L, 2, &len); | ||
auto res = checkregex(L, 1)->match(str, str + len); | ||
if (res.isSuccess()) { | ||
lua_newtable(L); | ||
for (size_t i = 0; i != res.groups.size(); ++i) { | ||
if (res.groups[i].has_value()) { | ||
if (res.groups[i]->name.empty()) | ||
lua_pushinteger(L, i); | ||
else | ||
pluto_pushstring(L, res.groups[i]->name); | ||
lua_pushlstring(L, res.groups[i]->begin, res.groups[i]->length()); | ||
lua_settable(L, -3); | ||
} | ||
} | ||
} | ||
else { | ||
luaL_pushfail(L); | ||
} | ||
return 1; | ||
} | ||
|
||
static const luaL_Reg funcs_regex[] = { | ||
{"new", regex_new}, | ||
{"match", regex_match}, | ||
{nullptr, nullptr} | ||
}; | ||
PLUTO_NEWLIB(regex); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
#pragma once | ||
|
||
#include <cstdint> | ||
#include <cstring> // memcpy | ||
|
||
namespace soup | ||
{ | ||
#pragma pack(push, 1) | ||
template <size_t Bytes> | ||
struct BigBitset | ||
{ | ||
uint8_t data[Bytes]{}; | ||
|
||
BigBitset() = default; | ||
|
||
BigBitset(const BigBitset<Bytes>& b) | ||
{ | ||
memcpy(data, b.data, sizeof(data)); | ||
} | ||
|
||
[[nodiscard]] static BigBitset<Bytes>* at(void* dp) noexcept | ||
{ | ||
return reinterpret_cast<BigBitset<Bytes>*>(dp); | ||
} | ||
|
||
[[nodiscard]] static const BigBitset<Bytes>* at(const void* dp) noexcept | ||
{ | ||
return reinterpret_cast<const BigBitset<Bytes>*>(dp); | ||
} | ||
|
||
[[nodiscard]] constexpr bool get(const size_t i) const noexcept | ||
{ | ||
const auto j = (i / 8); | ||
const auto k = (i % 8); | ||
|
||
return (data[j] >> k) & 1; | ||
} | ||
|
||
constexpr void set(const size_t i, const bool v) noexcept | ||
{ | ||
const auto j = (i / 8); | ||
const auto k = (i % 8); | ||
|
||
const uint8_t mask = (1 << k); | ||
|
||
data[j] &= ~mask; | ||
data[j] |= (mask * v); | ||
} | ||
|
||
constexpr void enable(const size_t i) noexcept | ||
{ | ||
const auto j = (i / 8); | ||
const auto k = (i % 8); | ||
|
||
data[j] |= (1 << k); | ||
} | ||
|
||
constexpr void disable(const size_t i) noexcept | ||
{ | ||
const auto j = (i / 8); | ||
const auto k = (i % 8); | ||
|
||
data[j] &= ~(1 << k); | ||
} | ||
}; | ||
#pragma pack(pop) | ||
} |
Oops, something went wrong.