Files
SedPP/include/sedpp/Regex.h
T
2026-06-09 00:22:22 -05:00

38 lines
913 B
C++

#pragma once
#include <optional>
#include <regex.h>
#include <string>
#include <vector>
#include "sedpp/Options.h"
namespace sedpp {
struct Match {
std::vector<regmatch_t> groups;
};
// Small RAII wrapper for POSIX regex_t. POSIX regex reports match offsets
// relative to the searched pointer, so search() rebases groups to the original
// subject string before returning.
class Regex {
public:
Regex(const std::string &pattern, const Options &options,
bool ignoreCase = false, bool multiline = false);
Regex(const Regex &) = delete;
Regex &operator=(const Regex &) = delete;
Regex(Regex &&other) noexcept;
Regex &operator=(Regex &&other) noexcept;
~Regex();
[[nodiscard]] std::optional<Match> search(const std::string &text,
std::size_t start = 0) const;
private:
regex_t regex_{};
bool compiled_ = false;
};
} // namespace sedpp