Skip to content

Commit 0f95d17

Browse files
committed
fixes
1 parent a274207 commit 0f95d17

2 files changed

Lines changed: 11 additions & 7 deletions

File tree

runtime-light/stdlib/string/regex-functions.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,8 @@ bool compile_regex(RegexInfo& regex_info) noexcept {
118118
}
119119

120120
// check runtime cache
121-
if (const auto it{regex_state.regex_pcre2_code_cache.find(regex_info.regex)}; it != regex_state.regex_pcre2_code_cache.end()) {
121+
if (const auto it{regex_state.regex_pcre2_code_cache.find({regex_info.regex.data(), static_cast<string::size_type>(regex_info.regex.size())})};
122+
it != regex_state.regex_pcre2_code_cache.end()) {
122123
auto& [compile_options, regex_code] = it->second;
123124
regex_info.compile_options = compile_options;
124125
regex_info.regex_code = regex_code;
@@ -695,7 +696,7 @@ std::optional<array<mixed>> split_regex(RegexInfo& regex_info, int64_t limit_val
695696
auto match_start{offsets[0]};
696697
auto match_end{offsets[1]};
697698

698-
make_output_val(regex_info, no_empty, offset_capture, offset, match_start).transform([&output, &limit_val](auto&& output_val)noexcept {
699+
make_output_val(regex_info, no_empty, offset_capture, offset, match_start).transform([&output, &limit_val](auto&& output_val) noexcept {
699700
output.push_back(std::move(output_val));
700701
if (limit_val != kphp::regex::PREG_NOLIMIT) {
701702
limit_val--;
@@ -706,7 +707,7 @@ std::optional<array<mixed>> split_regex(RegexInfo& regex_info, int64_t limit_val
706707
if (delim_capture) {
707708
for (auto i{1uz}; i < regex_info.match_count; i++) {
708709
auto j{2 * i};
709-
make_output_val(regex_info, no_empty, offset_capture, offsets[j], offsets[j + 1]).transform([&output](auto&& output_val)noexcept {
710+
make_output_val(regex_info, no_empty, offset_capture, offsets[j], offsets[j + 1]).transform([&output](auto&& output_val) noexcept {
710711
output.push_back(std::move(output_val));
711712
return 0;
712713
});

runtime-light/stdlib/string/regex-state.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,16 @@
99

1010
#include "common/mixin/not_copyable.h"
1111
#include "runtime-common/core/allocator/script-allocator.h"
12+
#include "runtime-common/core/runtime-core.h"
1213
#include "runtime-common/core/std/containers.h"
1314
#include "runtime-light/stdlib/string/regex-include.h"
1415
#include "runtime-light/utils/concepts.h"
1516

1617
struct RegexInstanceState final : private vk::not_copyable {
1718
private:
18-
template<hashable Key, typename Value>
19-
using unordered_map = kphp::stl::unordered_map<Key, Value, kphp::memory::script_allocator>;
19+
template<typename Value>
20+
using string_unordered_map =
21+
kphp::stl::unordered_map<string, Value, kphp::memory::script_allocator, decltype([](const string& s) noexcept { return static_cast<size_t>(s.hash()); })>;
2022

2123
static constexpr size_t MAX_SUBPATTERNS_COUNT = 512;
2224

@@ -34,12 +36,13 @@ struct RegexInstanceState final : private vk::not_copyable {
3436
const regex_pcre2_compile_context_t compile_context;
3537
const regex_pcre2_match_context_t match_context;
3638
regex_pcre2_match_data_t regex_pcre2_match_data;
37-
unordered_map<std::string_view, compiled_regex_cache_entry> regex_pcre2_code_cache;
39+
string_unordered_map<compiled_regex_cache_entry> regex_pcre2_code_cache;
3840

3941
RegexInstanceState() noexcept;
4042

4143
void add_compiled_code(std::string_view regex, uint32_t compile_options, regex_pcre2_code_t regex_code) noexcept {
42-
regex_pcre2_code_cache.emplace(regex, compiled_regex_cache_entry{.compile_options = compile_options, .regex_code = regex_code});
44+
regex_pcre2_code_cache.emplace(string{regex.data(), static_cast<string::size_type>(regex.size())},
45+
compiled_regex_cache_entry{.compile_options = compile_options, .regex_code = regex_code});
4346
}
4447

4548
static RegexInstanceState& get() noexcept;

0 commit comments

Comments
 (0)