Skip to content

Commit ae2c225

Browse files
committed
add std regex search wrapper to improve script performance.
1 parent d07f0f4 commit ae2c225

File tree

6 files changed

+74
-3
lines changed

6 files changed

+74
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Bug fixes:
1212
Improvements:
1313
* icpp: automatically generate the standard module pcm file in user local system;
1414
* icpp: add qemu tb buffer to 128MB per thread;
15+
* icpp: add std regex search wrapper to improve script performance;
1516
* imod: add assets configuration to support icpp module extra payloads;
1617
* imod: make an os independent module if there's no extra include paths;
1718

runtime/include/icpp.hpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
// c style
3838
#include <filesystem>
3939
#include <format>
40+
#include <regex>
4041
#include <string>
4142
#include <string_view>
4243
#else
@@ -153,4 +154,21 @@ template <size_t N> std::string rand_string() {
153154
return {tstr.data(), N};
154155
}
155156

157+
struct regex {
158+
regex(std::string_view pattern, int flags = std::regex_constants::ECMAScript |
159+
std::regex_constants::icase) {
160+
init(pattern, flags);
161+
}
162+
~regex() { deinit(); }
163+
regex() = delete;
164+
165+
// return true if str matches the initial pattern
166+
bool search(std::string_view str) const;
167+
168+
private:
169+
void init(std::string_view pattern, int flags);
170+
void deinit();
171+
void *context_;
172+
};
173+
156174
} // namespace icpp

src/loader.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,20 @@ struct ModuleLoader {
296296
{apisym(
297297
__ZN4icpp10result_setERKNSt3__117basic_string_viewIcNS0_11char_traitsIcEEEE),
298298
reinterpret_cast<const void *>(&api::result_sets)});
299+
300+
auto regexInit = &api::regex::init;
301+
auto regexDeinit = &api::regex::deinit;
302+
auto regexSearch = &api::regex::search;
303+
syms_.insert(
304+
{apisym(
305+
__ZN4icpp5regex4initENSt3__117basic_string_viewIcNS1_11char_traitsIcEEEEi),
306+
*(const void **)(&regexInit)});
307+
syms_.insert(
308+
{apisym(__ZN4icpp5regex6deinitEv), *(const void **)(&regexDeinit)});
309+
syms_.insert(
310+
{apisym(
311+
__ZNK4icpp5regex6searchENSt3__117basic_string_viewIcNS1_11char_traitsIcEEEE),
312+
*(const void **)(&regexSearch)});
299313
#endif
300314
}
301315

src/object-llvm.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1774,9 +1774,13 @@ void Object::parseSections() {
17741774
reinterpret_cast<uint64_t>(expContent->data())});
17751775
// file rva relative to text[0] section
17761776
news.frva = news.vm - textsects_[0].vm;
1777+
if (ofile_->isELF())
1778+
news.vrva = news.frva - textsects_[0].frva;
17771779
if (0) {
1778-
log_print(Develop, "Section {} frva={:x}, vmrva={:x} vm={:x} size={}.",
1779-
name.data(), news.frva, news.vrva, news.vm, news.size);
1780+
log_print(Develop,
1781+
"Section index={} frva={:x} vmrva={:x} vm={:x} size={} {}.",
1782+
news.index, news.frva, news.vrva, news.vm, news.size,
1783+
name.data());
17801784
}
17811785
} else if (s.isBSS() || name.ends_with("bss") || name.ends_with("common")) {
17821786
dynsects_.push_back(

src/runtime.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,22 @@ std::string_view rand_string(char *buff, int length) {
202202
return {buff, static_cast<size_t>(length)};
203203
}
204204

205+
void regex::init(std::string_view pattern, int flags) {
206+
auto rflags = static_cast<std::regex_constants::syntax_option_type>(flags);
207+
context_ = new std::regex(pattern.data(), rflags);
208+
}
209+
210+
void regex::deinit() {
211+
auto preg = static_cast<std::regex *>(context_);
212+
delete preg;
213+
}
214+
215+
// return true if str matches the initial pattern
216+
bool regex::search(std::string_view str) const {
217+
auto preg = static_cast<std::regex *>(context_);
218+
return std::regex_search(str.data(), str.data() + str.size(), *preg);
219+
}
220+
205221
} // namespace api
206222

207223
} // namespace icpp

src/runtime.h

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#include "utils.h"
1010
#include <map>
11+
#include <regex>
1112
#include <vector>
1213

1314
namespace com {
@@ -155,10 +156,27 @@ will be built by clang-icpp, so the std::string may be defined in a different
155156
way, to avoid the type mismatch, herein gives it an old C style one.
156157
157158
As of this, if you want to extend icpp runtime with native modules, the type
158-
mismatch situation must be considered on Windows.
159+
mismatch situation must be considered on Windows.
159160
*/
160161
std::string_view rand_string(char *buff, int length);
161162

163+
struct regex {
164+
regex(std::string_view pattern, int flags = std::regex_constants::ECMAScript |
165+
std::regex_constants::icase) {
166+
init(pattern, flags);
167+
}
168+
~regex() { deinit(); }
169+
regex() = delete;
170+
171+
// return true if str matches the initial pattern
172+
bool search(std::string_view str) const;
173+
174+
// private:
175+
void init(std::string_view pattern, int flags);
176+
void deinit();
177+
void *context_;
178+
};
179+
162180
} // namespace api
163181

164182
} // namespace icpp

0 commit comments

Comments
 (0)