-
Notifications
You must be signed in to change notification settings - Fork 849
regex_revalidate c++, lifecycle messages, MISS support #6455
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
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| /** @file | ||
|
|
||
| PCRE support wrapper. | ||
|
|
||
| @section license License | ||
|
|
||
| Licensed to the Apache Software Foundation (ASF) under one | ||
| or more contributor license agreements. See the NOTICE file | ||
| distributed with this work for additional information | ||
| regarding copyright ownership. The ASF licenses this file | ||
| to you under the Apache License, Version 2.0 (the | ||
| "License"); you may not use this file except in compliance | ||
| with the License. You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| #include "regex.h" | ||
|
|
||
| #ifdef PCRE_CONFIG_JIT | ||
| #include <pthread.h> | ||
|
|
||
| struct RegexThreadKey { | ||
| RegexThreadKey() { pthread_key_create(&this->key, reinterpret_cast<void (*)(void *)>(&pcre_jit_stack_free)); } | ||
| pthread_key_t key; | ||
| }; | ||
|
|
||
| static RegexThreadKey k; | ||
|
|
||
| static pcre_jit_stack * | ||
| get_jit_stack(void *) | ||
| { | ||
| pcre_jit_stack *jit_stack; | ||
|
|
||
| if ((jit_stack = static_cast<pcre_jit_stack *>(pthread_getspecific(k.key))) == nullptr) { | ||
| jit_stack = pcre_jit_stack_alloc(8192, 1024 * 1024); // 1 page min and 1MB max | ||
| pthread_setspecific(k.key, (void *)jit_stack); | ||
| } | ||
|
|
||
| return jit_stack; | ||
| } | ||
| #endif | ||
|
|
||
| bool | ||
| Regex::compile(const char *pattern, const unsigned flags) | ||
| { | ||
| const char *error; | ||
| int erroffset; | ||
| int options = 0; | ||
| int study_opts = 0; | ||
|
|
||
| if (nullptr != this->regex) { | ||
| return false; | ||
| } | ||
|
|
||
| if (flags & CASE_INSENSITIVE) { | ||
| options |= PCRE_CASELESS; | ||
| } | ||
|
|
||
| if (flags & ANCHORED) { | ||
| options |= PCRE_ANCHORED; | ||
| } | ||
|
|
||
| this->regex = pcre_compile(pattern, options, &error, &erroffset, nullptr); | ||
| if (error) { | ||
| this->regex = nullptr; | ||
| return false; | ||
| } | ||
|
|
||
| #ifdef PCRE_CONFIG_JIT | ||
| study_opts |= PCRE_STUDY_JIT_COMPILE; | ||
| #endif | ||
|
|
||
| this->regex_extra = pcre_study(this->regex, study_opts, &error); | ||
|
|
||
| #ifdef PCRE_CONFIG_JIT | ||
| if (nullptr != this->regex_extra) { | ||
| pcre_assign_jit_stack(this->regex_extra, &get_jit_stack, nullptr); | ||
| } | ||
| #endif | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| bool | ||
| Regex::matches(std::string_view const &src) const | ||
| { | ||
| return 0 <= exec(src, nullptr, 0); | ||
| } | ||
|
|
||
| int | ||
| Regex::exec(std::string_view const &src, int *ovector, int const ovecsize) const | ||
| { | ||
| return pcre_exec(this->regex, this->regex_extra, src.data(), src.size(), 0, 0, ovector, ovecsize); | ||
| } | ||
|
|
||
| Regex::~Regex() | ||
| { | ||
| if (regex_extra) { | ||
| #ifdef PCRE_CONFIG_JIT | ||
| pcre_free_study(regex_extra); | ||
| #else | ||
| pcre_free(regex_extra); | ||
| #endif | ||
| } | ||
| if (regex) { | ||
| pcre_free(regex); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| /** @file | ||
|
|
||
| Wrapper to make PCRE handling easier. | ||
|
|
||
| @section license License | ||
|
|
||
| Licensed to the Apache Software Foundation (ASF) under one | ||
| or more contributor license agreements. See the NOTICE file | ||
| distributed with this work for additional information | ||
| regarding copyright ownership. The ASF licenses this file | ||
| to you under the Apache License, Version 2.0 (the | ||
| "License"); you may not use this file except in compliance | ||
| with the License. You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <pcre.h> | ||
| #include <algorithm> | ||
| #include <string_view> | ||
|
|
||
| class Regex | ||
| { | ||
| public: | ||
| enum Flag { | ||
| CASE_INSENSITIVE = 0x0001, // default is case sensitive | ||
| UNANCHORED = 0x0002, // default (for DFA) is to anchor at the first matching position | ||
| ANCHORED = 0x0004, // default (for Regex) is unanchored | ||
| }; | ||
|
|
||
| Regex() = default; | ||
| Regex(Regex const &) = delete; | ||
| Regex &operator=(Regex const &) = delete; | ||
| Regex(Regex &&that); | ||
| Regex &operator=(Regex &&that); | ||
|
|
||
| bool compile(const char *pattern, const unsigned flags = 0); | ||
|
|
||
| // check if valid regex | ||
| bool is_valid() const; | ||
|
|
||
| // check for simple match | ||
| bool matches(std::string_view const &src) const; | ||
|
|
||
| // match returning substring positions. | ||
| int exec(std::string_view const &src, int *ovector, int const ovecsize) const; | ||
|
|
||
| ~Regex(); | ||
|
|
||
| private: | ||
| pcre *regex = nullptr; | ||
| pcre_extra *regex_extra = nullptr; | ||
| }; | ||
|
|
||
| inline Regex::Regex(Regex &&that) | ||
| { | ||
| std::swap(regex, that.regex); | ||
| std::swap(regex_extra, that.regex_extra); | ||
| } | ||
|
|
||
| inline Regex & | ||
| Regex::operator=(Regex &&that) | ||
| { | ||
| if (&that != this) { | ||
| std::swap(regex, that.regex); | ||
| std::swap(regex_extra, that.regex_extra); | ||
| } | ||
| return *this; | ||
| } | ||
|
|
||
| inline bool | ||
| Regex::is_valid() const | ||
| { | ||
| return nullptr != regex; | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.