review fixes
This commit is contained in:
@@ -39,8 +39,8 @@ PRIVATE_DEFINITION_START(PIRegularExpression)
|
||||
|
||||
PIString getNEString(const void * ptr, uint32_t max_size) {
|
||||
PIString ret;
|
||||
auto * cptr = (PIChar *)ptr;
|
||||
uint32_t sz = 0;
|
||||
const auto * cptr = static_cast<const PIChar *>(ptr);
|
||||
uint32_t sz = 0;
|
||||
while (*cptr != PIChar()) {
|
||||
ret.append(*cptr);
|
||||
cptr++;
|
||||
@@ -64,15 +64,14 @@ PRIVATE_DEFINITION_START(PIRegularExpression)
|
||||
bool compile(PIString & pat, Options opt) {
|
||||
free();
|
||||
if (pat.isEmpty()) return false;
|
||||
auto * pat_ptr = &(pat[0]);
|
||||
int error_number = 0;
|
||||
compiled = pcre2_compile((PCRE2_SPTR)pat_ptr, pat.size(), convertOptions(opt), &error_number, &error_offset, nullptr);
|
||||
const auto * pat_ptr = &pat.front();
|
||||
int error_number = 0;
|
||||
compiled = pcre2_compile((PCRE2_SPTR)pat_ptr, pat.size(), convertOptions(opt), &error_number, &error_offset, nullptr);
|
||||
if (!compiled) {
|
||||
PIChar buffer[256];
|
||||
int sz = pcre2_get_error_message(error_number, (PCRE2_UCHAR16 *)buffer, sizeof(buffer));
|
||||
error_msg = PIString(buffer, sz);
|
||||
const int sz = pcre2_get_error_message(error_number, reinterpret_cast<PCRE2_UCHAR16 *>(buffer), sizeof(buffer));
|
||||
error_msg = PIString(buffer, sz);
|
||||
return false;
|
||||
// printf("PCRE2 compilation failed at offset %d: %s\n", (int)erroroffset, buffer);
|
||||
}
|
||||
error_msg.clear();
|
||||
match_data = pcre2_match_data_create_from_pattern(compiled, nullptr);
|
||||
@@ -86,7 +85,7 @@ PRIVATE_DEFINITION_START(PIRegularExpression)
|
||||
capture_count = cap_cout;
|
||||
auto tabptr = name_table;
|
||||
for (uint32_t i = 0; i < namecount; i++) {
|
||||
int gnum = *(ushort *)tabptr;
|
||||
const int gnum = *tabptr;
|
||||
PIString gname = getNEString(tabptr + 1, name_entry_size);
|
||||
named_group_index[gname] = gnum;
|
||||
named_group_name[gnum] = gname;
|
||||
@@ -97,26 +96,25 @@ PRIVATE_DEFINITION_START(PIRegularExpression)
|
||||
}
|
||||
|
||||
void match(Matcher & ret) {
|
||||
int rc = pcre2_match(compiled,
|
||||
(PCRE2_SPTR)ret.subjectPtr(),
|
||||
ret.subject->size(),
|
||||
ret.start_offset,
|
||||
PCRE2_NO_UTF_CHECK,
|
||||
match_data,
|
||||
nullptr);
|
||||
const int rc = pcre2_match(compiled,
|
||||
(PCRE2_SPTR)ret.subjectPtr(),
|
||||
ret.subject->size(),
|
||||
ret.start_offset,
|
||||
PCRE2_NO_UTF_CHECK,
|
||||
match_data,
|
||||
nullptr);
|
||||
ret.has_match = ret.is_error = false;
|
||||
ret.groups.clear();
|
||||
if (rc == PCRE2_ERROR_NOMATCH) return;
|
||||
if (rc < 0) {
|
||||
ret.is_error = true;
|
||||
} else {
|
||||
ret.has_match = true;
|
||||
auto ovector = pcre2_get_ovector_pointer(match_data);
|
||||
ret.has_match = true;
|
||||
const auto ovector = pcre2_get_ovector_pointer(match_data);
|
||||
for (int i = 0; i < rc; i++) {
|
||||
Matcher::Group g;
|
||||
g.index = ovector[2 * i];
|
||||
g.size = ovector[2 * i + 1] - ovector[2 * i];
|
||||
// g.string = PIString(&(sub_ptr[g.index]), g.size);
|
||||
ret.groups << g;
|
||||
}
|
||||
ret.start_offset = ovector[1];
|
||||
@@ -194,7 +192,7 @@ int PIRegularExpression::captureGroupIndex(const PIString & gname) const {
|
||||
}
|
||||
|
||||
|
||||
PIRegularExpression::Matcher PIRegularExpression::makeMatcher(PIString & subject, size_t offset) {
|
||||
PIRegularExpression::Matcher PIRegularExpression::matchIterator(PIString & subject, size_t offset) {
|
||||
PIRegularExpression::Matcher ret(this);
|
||||
ret.start_offset = offset;
|
||||
ret.subject = &subject;
|
||||
@@ -202,7 +200,16 @@ PIRegularExpression::Matcher PIRegularExpression::makeMatcher(PIString & subject
|
||||
}
|
||||
|
||||
|
||||
PIRegularExpression::Matcher PIRegularExpression::makeMatcher(const PIString & subject, size_t offset) {
|
||||
PIRegularExpression::Matcher PIRegularExpression::matchIterator(PIString && subject, size_t offset) {
|
||||
PIRegularExpression::Matcher ret(this);
|
||||
ret.start_offset = offset;
|
||||
ret.subject_own = std::move(subject);
|
||||
ret.subject = &ret.subject_own;
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
PIRegularExpression::Matcher PIRegularExpression::matchIterator(const PIString & subject, size_t offset) {
|
||||
PIRegularExpression::Matcher ret(this);
|
||||
ret.start_offset = offset;
|
||||
ret.subject_own = subject;
|
||||
@@ -212,14 +219,21 @@ PIRegularExpression::Matcher PIRegularExpression::makeMatcher(const PIString & s
|
||||
|
||||
|
||||
PIRegularExpression::Matcher PIRegularExpression::match(PIString & subject, size_t offset) {
|
||||
PIRegularExpression::Matcher ret = makeMatcher(subject, offset);
|
||||
PIRegularExpression::Matcher ret = matchIterator(subject, offset);
|
||||
PRIVATE->match(ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
PIRegularExpression::Matcher PIRegularExpression::match(PIString && subject, size_t offset) {
|
||||
PIRegularExpression::Matcher ret = matchIterator(std::move(subject), offset);
|
||||
PRIVATE->match(ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
PIRegularExpression::Matcher PIRegularExpression::match(const PIString & subject, size_t offset) {
|
||||
PIRegularExpression::Matcher ret = makeMatcher(subject, offset);
|
||||
PIRegularExpression::Matcher ret = matchIterator(subject, offset);
|
||||
PRIVATE->match(ret);
|
||||
return ret;
|
||||
}
|
||||
@@ -230,7 +244,7 @@ PIRegularExpression::Matcher::Matcher(PIRegularExpression * p): parent(p) {}
|
||||
|
||||
PIChar * PIRegularExpression::Matcher::subjectPtr() const {
|
||||
if (!subject) return nullptr;
|
||||
return &(*subject)[0];
|
||||
return &subject->front();
|
||||
}
|
||||
|
||||
|
||||
@@ -248,8 +262,9 @@ bool PIRegularExpression::Matcher::next() {
|
||||
PIStringList PIRegularExpression::Matcher::matchedStrings() const {
|
||||
if (!subject) return {};
|
||||
PIStringList ret;
|
||||
for (const auto & g: groups)
|
||||
for (const auto & g: groups) {
|
||||
ret << subject->mid(g.index, g.size);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
@@ -304,19 +319,19 @@ PIRegularExpression PIRegularExpression::fromPOSIX(const PIString & pattern, Opt
|
||||
|
||||
void PIRegularExpression::convertFrom(const PIString & pattern, uint type, Options opt) {
|
||||
if (pattern.isEmpty()) return;
|
||||
PIChar * cptr = &((PIString &)pattern)[0];
|
||||
const auto cptr = &const_cast<PIString &>(pattern).front();
|
||||
PCRE2_UCHAR * out = nullptr;
|
||||
PCRE2_SIZE out_size = 0;
|
||||
int rc = pcre2_pattern_convert((PCRE2_SPTR)cptr,
|
||||
pattern.size_s(),
|
||||
type | PCRE2_CONVERT_UTF | PCRE2_CONVERT_NO_UTF_CHECK,
|
||||
&out,
|
||||
&out_size,
|
||||
nullptr);
|
||||
const int rc = pcre2_pattern_convert((PCRE2_SPTR)cptr,
|
||||
pattern.size_s(),
|
||||
type | PCRE2_CONVERT_UTF | PCRE2_CONVERT_NO_UTF_CHECK,
|
||||
&out,
|
||||
&out_size,
|
||||
nullptr);
|
||||
if (rc != 0) {
|
||||
piCout << "PIRegularExpression::convertFrom error" << rc;
|
||||
} else {
|
||||
setPattern(PIString((PIChar *)out, out_size), opt);
|
||||
setPattern(PIString(reinterpret_cast<PIChar *>(out), out_size), opt);
|
||||
}
|
||||
pcre2_converted_pattern_free(out);
|
||||
}
|
||||
|
||||
@@ -23,10 +23,10 @@
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef piregularexpression_h
|
||||
#define piregularexpression_h
|
||||
#ifndef PIREGULAREXPRESSION_H
|
||||
#define PIREGULAREXPRESSION_H
|
||||
|
||||
#include <pistring.h>
|
||||
#include "pistring.h"
|
||||
|
||||
class PIP_EXPORT PIRegularExpression {
|
||||
public:
|
||||
@@ -105,9 +105,11 @@ public:
|
||||
|
||||
Matcher match(const PIString & subject, size_t offset = 0);
|
||||
Matcher match(PIString & subject, size_t offset = 0);
|
||||
Matcher match(PIString && subject, size_t offset = 0);
|
||||
|
||||
Matcher makeMatcher(const PIString & subject, size_t offset = 0);
|
||||
Matcher makeMatcher(PIString & subject, size_t offset = 0);
|
||||
Matcher matchIterator(const PIString & subject, size_t offset = 0);
|
||||
Matcher matchIterator(PIString & subject, size_t offset = 0);
|
||||
Matcher matchIterator(PIString && subject, size_t offset = 0);
|
||||
|
||||
static PIRegularExpression fromGlob(const PIString & pattern, Options opt = None);
|
||||
static PIRegularExpression fromPOSIX(const PIString & pattern, Options opt = None);
|
||||
@@ -116,8 +118,8 @@ private:
|
||||
void convertFrom(const PIString & pattern, uint type, Options opt);
|
||||
|
||||
PRIVATE_DECLARATION(PIP_EXPORT)
|
||||
PIString pat_, subj_own;
|
||||
PIString pat_;
|
||||
Options opt_;
|
||||
};
|
||||
|
||||
#endif
|
||||
#endif // PIREGULAREXPRESSION_H
|
||||
|
||||
Reference in New Issue
Block a user