Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add regex library #767

Merged
merged 1 commit into from
Jul 12, 2024
Merged
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
35 changes: 35 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 @@ -762,6 +763,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 @@ -835,6 +838,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 @@ -920,11 +924,42 @@
<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\RegexTransitionsVector.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\os.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 @@ -846,6 +853,99 @@
<ClInclude Include="src\vendor\Soup\soup\memProtFlags.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 @@ -861,4 +961,4 @@
<UniqueIdentifier>{f0adda43-f311-40e5-b4ec-284f248bad46}</UniqueIdentifier>
</Filter>
</ItemGroup>
</Project>
</Project>
2 changes: 1 addition & 1 deletion src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ PLATS= guess aix bsd freebsd generic linux linux-readline macosx posix solaris
LUA_A= libplutostatic.a
LUA_SO= libpluto.so
CORE_O= lapi.o lcode.o lctype.o ldebug.o ldo.o ldump.o lfunc.o lgc.o llex.o lmem.o lobject.o lopcodes.o lparser.o lstate.o lstring.o ltable.o ltm.o lundump.o lvm.o lzio.o
LIB_O= lauxlib.o lbaselib.o lcorolib.o ldblib.o liolib.o lmathlib.o loadlib.o loslib.o lstrlib.o lcryptolib.o ltablib.o lutf8lib.o lassertlib.o lvector3lib.o lbase32.o lbase64.o ljson.o lurllib.o linit.o lstarlib.o lcatlib.o lhttplib.o lschedulerlib.o lsocketlib.o lbigint.o lxml.o
LIB_O= lauxlib.o lbaselib.o lcorolib.o ldblib.o liolib.o lmathlib.o loadlib.o loslib.o lstrlib.o lcryptolib.o ltablib.o lutf8lib.o lassertlib.o lvector3lib.o lbase32.o lbase64.o ljson.o lurllib.o linit.o lstarlib.o lcatlib.o lhttplib.o lschedulerlib.o lsocketlib.o lbigint.o lxml.o lregex.o
BASE_O= $(CORE_O) $(LIB_O) $(MYOBJS)

LUA_T= pluto
Expand Down
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
69 changes: 69 additions & 0 deletions src/vendor/Soup/soup/BigBitset.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#pragma once

#include <cstdint>
#include <cstring> // memcpy

#include "base.hpp"

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)
}
2 changes: 1 addition & 1 deletion src/vendor/Soup/soup/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ CFLAGS=-c -Wall -DSOUP_USE_INTRIN

LIBNAME=libsoup.a
# echo $(ls *.cpp | sed 's/.cpp/.o/g')
OBJS=adler32.o aes.o alloc.o Asn1Identifier.o Asn1Sequence.o base32.o base64.o base.o Bigint.o Capture.o cat.o CpuInfo.o crc32.o Curve25519.o deflate.o DetachedScheduler.o dnsHttpResolver.o dnsName.o dnsRawResolver.o dnsSmartResolver.o dnsUdpResolver.o dns_records.o dnsResolver.o ecc.o filesystem.o HttpRequest.o HttpRequestTask.o IpAddr.o joaat.o JsonArray.o JsonBool.o json.o JsonFloat.o JsonInt.o JsonNode.o JsonNull.o JsonObject.o JsonString.o log.o MimeMessage.o netConfig.o netConnectTask.o netStatus.o Oid.o pem.o Promise.o rand.o rsa.o Scheduler.o SelfDeletingThread.o sha1.o sha256.o sha384.o sha512.o Socket.o SocketTlsEncrypter.o SocketTlsHandshaker.o spaceship.o string.o Task.o Thread.o time.o TrustStore.o unicode.o Uri.o urlenc.o version_compare.o Worker.o X509Certchain.o X509Certificate.o X509RelativeDistinguishedName.o xml.o Reader.o Writer.o DefaultRngInterface.o HardwareRng.o Server.o os.o
OBJS=adler32.o aes.o alloc.o Asn1Identifier.o Asn1Sequence.o base32.o base64.o base.o Bigint.o Capture.o cat.o CpuInfo.o crc32.o Curve25519.o deflate.o DetachedScheduler.o dnsHttpResolver.o dnsName.o dnsRawResolver.o dnsSmartResolver.o dnsUdpResolver.o dns_records.o dnsResolver.o ecc.o filesystem.o HttpRequest.o HttpRequestTask.o IpAddr.o joaat.o JsonArray.o JsonBool.o json.o JsonFloat.o JsonInt.o JsonNode.o JsonNull.o JsonObject.o JsonString.o log.o MimeMessage.o netConfig.o netConnectTask.o netStatus.o Oid.o pem.o Promise.o rand.o rsa.o Scheduler.o SelfDeletingThread.o sha1.o sha256.o sha384.o sha512.o Socket.o SocketTlsEncrypter.o SocketTlsHandshaker.o spaceship.o string.o Task.o Thread.o time.o TrustStore.o unicode.o Uri.o urlenc.o version_compare.o Worker.o X509Certchain.o X509Certificate.o X509RelativeDistinguishedName.o xml.o Reader.o Writer.o DefaultRngInterface.o HardwareRng.o Server.o os.o Regex.o RegexGroup.o

all: $(LIBNAME)

Expand Down
Loading