Skip to content

Commit

Permalink
Add regex library with 'new' and 'match'
Browse files Browse the repository at this point in the history
  • Loading branch information
Sainan committed Apr 7, 2024
1 parent a77cea3 commit 9a419e8
Show file tree
Hide file tree
Showing 37 changed files with 3,652 additions and 1 deletion.
34 changes: 34 additions & 0 deletions Pluto.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -662,6 +662,7 @@
<ClCompile Include="src\lopcodes.cpp" />
<ClCompile Include="src\loslib.cpp" />
<ClCompile Include="src\lparser.cpp" />
<ClCompile Include="src\lregex.cpp" />
<ClCompile Include="src\lschedulerlib.cpp" />
<ClCompile Include="src\lsocketlib.cpp" />
<ClCompile Include="src\lstarlib.cpp" />
Expand Down Expand Up @@ -761,6 +762,8 @@
<ClCompile Include="src\vendor\Soup\soup\Promise.cpp" />
<ClCompile Include="src\vendor\Soup\soup\rand.cpp" />
<ClCompile Include="src\vendor\Soup\soup\Reader.cpp" />
<ClCompile Include="src\vendor\Soup\soup\Regex.cpp" />
<ClCompile Include="src\vendor\Soup\soup\RegexGroup.cpp" />
<ClCompile Include="src\vendor\Soup\soup\rsa.cpp" />
<ClCompile Include="src\vendor\Soup\soup\Scheduler.cpp" />
<ClCompile Include="src\vendor\Soup\soup\SelfDeletingThread.cpp" />
Expand Down Expand Up @@ -834,6 +837,7 @@
<ClInclude Include="src\vendor\Soup\soup\base.hpp" />
<ClInclude Include="src\vendor\Soup\soup\base32.hpp" />
<ClInclude Include="src\vendor\Soup\soup\base64.hpp" />
<ClInclude Include="src\vendor\Soup\soup\BigBitset.hpp" />
<ClInclude Include="src\vendor\Soup\soup\Bigint.hpp" />
<ClInclude Include="src\vendor\Soup\soup\bitmask.hpp" />
<ClInclude Include="src\vendor\Soup\soup\Bitset.hpp" />
Expand Down Expand Up @@ -917,11 +921,41 @@
<ClInclude Include="src\vendor\Soup\soup\Packet.hpp" />
<ClInclude Include="src\vendor\Soup\soup\pem.hpp" />
<ClInclude Include="src\vendor\Soup\soup\plusaes.hpp" />
<ClInclude Include="src\vendor\Soup\soup\PointerAndBool.hpp" />
<ClInclude Include="src\vendor\Soup\soup\PoppedNode.hpp" />
<ClInclude Include="src\vendor\Soup\soup\PrimitiveRaii.hpp" />
<ClInclude Include="src\vendor\Soup\soup\Promise.hpp" />
<ClInclude Include="src\vendor\Soup\soup\rand.hpp" />
<ClInclude Include="src\vendor\Soup\soup\Reader.hpp" />
<ClInclude Include="src\vendor\Soup\soup\Regex.hpp" />
<ClInclude Include="src\vendor\Soup\soup\RegexAlternative.hpp" />
<ClInclude Include="src\vendor\Soup\soup\RegexAnyCharConstraint.hpp" />
<ClInclude Include="src\vendor\Soup\soup\RegexCharConstraint.hpp" />
<ClInclude Include="src\vendor\Soup\soup\RegexCodepointConstraint.hpp" />
<ClInclude Include="src\vendor\Soup\soup\RegexConstraint.hpp" />
<ClInclude Include="src\vendor\Soup\soup\RegexConstraintLookbehind.hpp" />
<ClInclude Include="src\vendor\Soup\soup\RegexDummyConstraint.hpp" />
<ClInclude Include="src\vendor\Soup\soup\RegexEndConstraint.hpp" />
<ClInclude Include="src\vendor\Soup\soup\RegexExactQuantifierConstraint.hpp" />
<ClInclude Include="src\vendor\Soup\soup\RegexFlags.hpp" />
<ClInclude Include="src\vendor\Soup\soup\RegexGroup.hpp" />
<ClInclude Include="src\vendor\Soup\soup\RegexGroupConstraint.hpp" />
<ClInclude Include="src\vendor\Soup\soup\RegexMatchedGroup.hpp" />
<ClInclude Include="src\vendor\Soup\soup\RegexMatcher.hpp" />
<ClInclude Include="src\vendor\Soup\soup\RegexMatchResult.hpp" />
<ClInclude Include="src\vendor\Soup\soup\RegexNegativeLookaheadConstraint.hpp" />
<ClInclude Include="src\vendor\Soup\soup\RegexNegativeLookbehindConstraint.hpp" />
<ClInclude Include="src\vendor\Soup\soup\RegexOpenEndedRangeQuantifierConstraint.hpp" />
<ClInclude Include="src\vendor\Soup\soup\RegexOptConstraint.hpp" />
<ClInclude Include="src\vendor\Soup\soup\RegexPositiveLookaheadConstraint.hpp" />
<ClInclude Include="src\vendor\Soup\soup\RegexPositiveLookbehindConstraint.hpp" />
<ClInclude Include="src\vendor\Soup\soup\RegexRangeConstraint.hpp" />
<ClInclude Include="src\vendor\Soup\soup\RegexRangeQuantifierConstraint.hpp" />
<ClInclude Include="src\vendor\Soup\soup\RegexRecallConstraint.hpp" />
<ClInclude Include="src\vendor\Soup\soup\RegexRepeatConstraint.hpp" />
<ClInclude Include="src\vendor\Soup\soup\RegexStartConstraint.hpp" />
<ClInclude Include="src\vendor\Soup\soup\RegexWordBoundaryConstraint.hpp" />
<ClInclude Include="src\vendor\Soup\soup\RegexWordCharConstraint.hpp" />
<ClInclude Include="src\vendor\Soup\soup\ReuseTag.hpp" />
<ClInclude Include="src\vendor\Soup\soup\RngInterface.hpp" />
<ClInclude Include="src\vendor\Soup\soup\rsa.hpp" />
Expand Down
102 changes: 101 additions & 1 deletion Pluto.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,13 @@
<ClCompile Include="src\vendor\Soup\soup\alloc.cpp">
<Filter>vendor\Soup\soup</Filter>
</ClCompile>
<ClCompile Include="src\lregex.cpp" />
<ClCompile Include="src\vendor\Soup\soup\RegexGroup.cpp">
<Filter>vendor\Soup\soup</Filter>
</ClCompile>
<ClCompile Include="src\vendor\Soup\soup\Regex.cpp">
<Filter>vendor\Soup\soup</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="src\ltm.h" />
Expand Down Expand Up @@ -840,6 +847,99 @@
<ClInclude Include="src\vendor\Soup\soup\alloc.hpp">
<Filter>vendor\Soup\soup</Filter>
</ClInclude>
<ClInclude Include="src\vendor\Soup\soup\RegexFlags.hpp">
<Filter>vendor\Soup\soup</Filter>
</ClInclude>
<ClInclude Include="src\vendor\Soup\soup\RegexGroup.hpp">
<Filter>vendor\Soup\soup</Filter>
</ClInclude>
<ClInclude Include="src\vendor\Soup\soup\RegexGroupConstraint.hpp">
<Filter>vendor\Soup\soup</Filter>
</ClInclude>
<ClInclude Include="src\vendor\Soup\soup\RegexMatchedGroup.hpp">
<Filter>vendor\Soup\soup</Filter>
</ClInclude>
<ClInclude Include="src\vendor\Soup\soup\RegexMatcher.hpp">
<Filter>vendor\Soup\soup</Filter>
</ClInclude>
<ClInclude Include="src\vendor\Soup\soup\RegexMatchResult.hpp">
<Filter>vendor\Soup\soup</Filter>
</ClInclude>
<ClInclude Include="src\vendor\Soup\soup\RegexNegativeLookaheadConstraint.hpp">
<Filter>vendor\Soup\soup</Filter>
</ClInclude>
<ClInclude Include="src\vendor\Soup\soup\RegexNegativeLookbehindConstraint.hpp">
<Filter>vendor\Soup\soup</Filter>
</ClInclude>
<ClInclude Include="src\vendor\Soup\soup\RegexOpenEndedRangeQuantifierConstraint.hpp">
<Filter>vendor\Soup\soup</Filter>
</ClInclude>
<ClInclude Include="src\vendor\Soup\soup\RegexOptConstraint.hpp">
<Filter>vendor\Soup\soup</Filter>
</ClInclude>
<ClInclude Include="src\vendor\Soup\soup\RegexPositiveLookaheadConstraint.hpp">
<Filter>vendor\Soup\soup</Filter>
</ClInclude>
<ClInclude Include="src\vendor\Soup\soup\RegexPositiveLookbehindConstraint.hpp">
<Filter>vendor\Soup\soup</Filter>
</ClInclude>
<ClInclude Include="src\vendor\Soup\soup\RegexRangeConstraint.hpp">
<Filter>vendor\Soup\soup</Filter>
</ClInclude>
<ClInclude Include="src\vendor\Soup\soup\RegexRangeQuantifierConstraint.hpp">
<Filter>vendor\Soup\soup</Filter>
</ClInclude>
<ClInclude Include="src\vendor\Soup\soup\RegexRecallConstraint.hpp">
<Filter>vendor\Soup\soup</Filter>
</ClInclude>
<ClInclude Include="src\vendor\Soup\soup\RegexRepeatConstraint.hpp">
<Filter>vendor\Soup\soup</Filter>
</ClInclude>
<ClInclude Include="src\vendor\Soup\soup\RegexStartConstraint.hpp">
<Filter>vendor\Soup\soup</Filter>
</ClInclude>
<ClInclude Include="src\vendor\Soup\soup\RegexWordBoundaryConstraint.hpp">
<Filter>vendor\Soup\soup</Filter>
</ClInclude>
<ClInclude Include="src\vendor\Soup\soup\RegexWordCharConstraint.hpp">
<Filter>vendor\Soup\soup</Filter>
</ClInclude>
<ClInclude Include="src\vendor\Soup\soup\Regex.hpp">
<Filter>vendor\Soup\soup</Filter>
</ClInclude>
<ClInclude Include="src\vendor\Soup\soup\RegexAlternative.hpp">
<Filter>vendor\Soup\soup</Filter>
</ClInclude>
<ClInclude Include="src\vendor\Soup\soup\RegexAnyCharConstraint.hpp">
<Filter>vendor\Soup\soup</Filter>
</ClInclude>
<ClInclude Include="src\vendor\Soup\soup\RegexCharConstraint.hpp">
<Filter>vendor\Soup\soup</Filter>
</ClInclude>
<ClInclude Include="src\vendor\Soup\soup\RegexCodepointConstraint.hpp">
<Filter>vendor\Soup\soup</Filter>
</ClInclude>
<ClInclude Include="src\vendor\Soup\soup\RegexConstraint.hpp">
<Filter>vendor\Soup\soup</Filter>
</ClInclude>
<ClInclude Include="src\vendor\Soup\soup\RegexConstraintLookbehind.hpp">
<Filter>vendor\Soup\soup</Filter>
</ClInclude>
<ClInclude Include="src\vendor\Soup\soup\RegexDummyConstraint.hpp">
<Filter>vendor\Soup\soup</Filter>
</ClInclude>
<ClInclude Include="src\vendor\Soup\soup\RegexEndConstraint.hpp">
<Filter>vendor\Soup\soup</Filter>
</ClInclude>
<ClInclude Include="src\vendor\Soup\soup\RegexExactQuantifierConstraint.hpp">
<Filter>vendor\Soup\soup</Filter>
</ClInclude>
<ClInclude Include="src\vendor\Soup\soup\PointerAndBool.hpp">
<Filter>vendor\Soup\soup</Filter>
</ClInclude>
<ClInclude Include="src\vendor\Soup\soup\BigBitset.hpp">
<Filter>vendor\Soup\soup</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<Filter Include="vendor">
Expand All @@ -855,4 +955,4 @@
<UniqueIdentifier>{f0adda43-f311-40e5-b4ec-284f248bad46}</UniqueIdentifier>
</Filter>
</ItemGroup>
</Project>
</Project>
56 changes: 56 additions & 0 deletions src/lregex.cpp
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);
3 changes: 3 additions & 0 deletions src/lualib.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ namespace Pluto {
#endif
extern const PreloadedLibrary preloaded_bigint;
extern const PreloadedLibrary preloaded_xml;
extern const PreloadedLibrary preloaded_regex;

inline const PreloadedLibrary* const all_preloaded[] = {
&preloaded_crypto,
Expand All @@ -77,6 +78,7 @@ namespace Pluto {
#endif
&preloaded_bigint,
&preloaded_xml,
&preloaded_regex,
};
}

Expand All @@ -96,6 +98,7 @@ LUAMOD_API int (luaopen_socket) (lua_State *L);
#endif
LUAMOD_API int (luaopen_bigint) (lua_State *L);
LUAMOD_API int (luaopen_xml) (lua_State *L);
LUAMOD_API int (luaopen_regex) (lua_State *L);

/* open all previous libraries */
LUALIB_API void (luaL_openlibs) (lua_State *L);
Expand Down
67 changes: 67 additions & 0 deletions src/vendor/Soup/soup/BigBitset.hpp
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)
}

0 comments on commit 9a419e8

Please sign in to comment.