29#include <sptk5/Strings.h>
30#include <sptk5/sptk-config.h>
31#include <sptk5/sptk.h>
40#define PCRE2_CODE_UNIT_WIDTH 8
44#define SPRE_CASELESS PCRE2_CASELESS
45#define SPRE_MULTILINE PCRE2_MULTILINE
46#define SPRE_DOTALL PCRE2_DOTALL
47#define SPRE_EXTENDED PCRE2_EXTENDED
48using pcre_offset_t = long;
55#define SPRE_CASELESS PCRE_CASELESS
56#define SPRE_MULTILINE PCRE_MULTILINE
57#define SPRE_DOTALL PCRE_DOTALL
58#define SPRE_EXTENDED PCRE_EXTENDED
59using pcre_offset_t = int;
62#if defined(HAVE_PCRE2) | defined(HAVE_PCRE)
76class SP_EXPORT RegularExpression
80 using PCREHandle = pcre2_code;
81 using PCREExtraHandle = uint8_t*;
83 using PCREHandle = pcre;
84 using PCREExtraHandle = pcre_extra;
100 Group(String value, pcre_offset_t start, pcre_offset_t end)
113 pcre_offset_t start {0};
114 pcre_offset_t end {0};
121 class SP_EXPORT Groups
123 friend class RegularExpression;
132 const Group& operator[](
int index)
const;
140 const Group& operator[](
const char* name)
const;
146 const std::vector<Group>& groups()
const
155 const std::map<String, Group>& namedGroups()
const
157 return m_namedGroups;
165 return m_groups.empty();
171 explicit operator bool()
const
173 return !m_groups.empty();
181 void grow(
size_t groupCount);
187 void add(Group&& group)
189 m_groups.push_back(std::move(group));
197 void add(
const String& name, Group&& group)
199 m_namedGroups[name] = std::move(group);
208 std::vector<Group> m_groups;
209 std::map<String, Group> m_namedGroups;
210 static const Group emptyGroup;
225 explicit RegularExpression(
const String& pattern,
const String& options =
"");
246 bool matches(
const String& text)
const;
253 Groups m(
const String& text)
const
256 return m(text, offset);
265 Groups m(
const String& text,
size_t& offset)
const;
273 String s(
const String& text,
const String& outputPattern)
const;
282 String s(
const String& text,
const std::function<String(
const String&)>& replace,
bool& replaced)
const;
289 Strings split(
const String& text)
const;
298 String replaceAll(
const String& text,
const String& outputPattern,
bool& replaced)
const;
307 String replaceAll(
const String& text,
const std::map<String, String>& substitutions,
bool& replaced)
const;
313 const String& pattern()
const;
317 bool m_global {
false};
320 std::shared_ptr<PCREHandle> m_pcre;
321 std::shared_ptr<PCREExtraHandle> m_pcreExtra;
323 uint32_t m_options {0};
324 size_t m_captureCount {0};
338 size_t nextMatch(
const String& text,
size_t& offset, MatchData& matchData)
const;
344 size_t getCaptureCount()
const;
350 size_t getNamedGroupCount()
const;
356 void getNameTable(
const char*& nameTable,
int& nameEntrySize)
const;
364 static size_t findNextPlaceholder(
size_t pos,
const String& outputPattern);
366 void extractNamedMatches(
const String& text, Groups& matchedStrings,
const MatchData& matchData,
367 size_t matchCount)
const;
370using SRegularExpression = std::shared_ptr<RegularExpression>;
SP_EXPORT bool operator==(const sptk::DateTime &dt1, const sptk::DateTime &dt2)
SP_EXPORT bool operator!=(const sptk::DateTime &dt1, const sptk::DateTime &dt2)