dlang-sqlite inital
This commit is contained in:
commit
ce0a6c08e4
160 files changed
+362540
No files matched your search
@@ -0,0 +1,73 @@
|
||||
#ifndef DLANG_LIBS_REGEX_HPP
|
||||
#define DLANG_LIBS_REGEX_HPP
|
||||
|
||||
#include <regex>
|
||||
#include <string>
|
||||
#include "../vm.hpp"
|
||||
#include "../helpers.hpp"
|
||||
|
||||
static void NativeRegexMatch(DlangVM* vm)
|
||||
{
|
||||
auto pattern = dlang_tostring(vm);
|
||||
auto text = dlang_tostring(vm);
|
||||
|
||||
try {
|
||||
std::regex rx(pattern);
|
||||
bool matched = std::regex_match(text, rx);
|
||||
dlang_pushnumber(vm, matched ? 1.0 : 0.0);
|
||||
}
|
||||
catch (...) {
|
||||
dlang_pushnumber(vm, 0.0);
|
||||
}
|
||||
}
|
||||
|
||||
static void NativeRegexSearch(DlangVM* vm)
|
||||
{
|
||||
auto pattern = dlang_tostring(vm);
|
||||
auto text = dlang_tostring(vm);
|
||||
|
||||
try {
|
||||
std::regex rx(pattern);
|
||||
bool found = std::regex_search(text, rx);
|
||||
dlang_pushnumber(vm, found ? 1.0 : 0.0);
|
||||
}
|
||||
catch (...) {
|
||||
dlang_pushnumber(vm, 0.0);
|
||||
}
|
||||
}
|
||||
|
||||
static void NativeRegexReplace(DlangVM* vm)
|
||||
{
|
||||
auto fmt = dlang_tostring(vm);
|
||||
auto pattern = dlang_tostring(vm);
|
||||
auto text = dlang_tostring(vm);
|
||||
|
||||
try {
|
||||
std::regex rx(pattern);
|
||||
std::string result = std::regex_replace(text, rx, fmt);
|
||||
dlang_pushstring(vm, result);
|
||||
dlang_pushindex(vm);
|
||||
}
|
||||
catch (...) {
|
||||
dlang_pushstring(vm, text);
|
||||
dlang_pushindex(vm);
|
||||
}
|
||||
}
|
||||
|
||||
inline void openlib_regex(DlangVM* vm)
|
||||
{
|
||||
dlang_newtable(vm);
|
||||
|
||||
dlang_pushcfunction(vm, NativeRegexMatch);
|
||||
dlang_setfield(vm, "match");
|
||||
|
||||
dlang_pushcfunction(vm, NativeRegexSearch);
|
||||
dlang_setfield(vm, "search");
|
||||
|
||||
dlang_pushcfunction(vm, NativeRegexReplace);
|
||||
dlang_setfield(vm, "replace");
|
||||
|
||||
dlang_setglobal(vm, "regex");
|
||||
}
|
||||
|
||||
#endif
|
||||
Reference in new issue
Block a user