cmake/Source/cmStringReplaceHelper.h

66 lines
1.4 KiB
C
Raw Normal View History

2018-08-09 18:06:22 +02:00
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
file Copyright.txt or https://cmake.org/licensing for details. */
2021-09-14 00:13:48 +02:00
#pragma once
2018-08-09 18:06:22 +02:00
#include <string>
2019-11-11 23:01:05 +01:00
#include <utility>
2018-08-09 18:06:22 +02:00
#include <vector>
2020-02-01 23:06:01 +01:00
#include "cmsys/RegularExpression.hxx"
2018-08-09 18:06:22 +02:00
class cmMakefile;
class cmStringReplaceHelper
{
public:
2019-11-11 23:01:05 +01:00
cmStringReplaceHelper(const std::string& regex, std::string replace_expr,
2018-08-09 18:06:22 +02:00
cmMakefile* makefile = nullptr);
bool IsRegularExpressionValid() const
{
return this->RegularExpression.is_valid();
}
bool IsReplaceExpressionValid() const
{
return this->ValidReplaceExpression;
}
bool Replace(const std::string& input, std::string& output);
const std::string& GetError() { return this->ErrorString; }
private:
class RegexReplacement
{
public:
RegexReplacement(const char* s)
: Number(-1)
, Value(s)
{
}
2019-11-11 23:01:05 +01:00
RegexReplacement(std::string s)
2018-08-09 18:06:22 +02:00
: Number(-1)
2019-11-11 23:01:05 +01:00
, Value(std::move(s))
2018-08-09 18:06:22 +02:00
{
}
RegexReplacement(int n)
: Number(n)
{
}
2019-11-11 23:01:05 +01:00
RegexReplacement() = default;
2018-08-09 18:06:22 +02:00
int Number;
std::string Value;
};
void ParseReplaceExpression();
std::string ErrorString;
std::string RegExString;
cmsys::RegularExpression RegularExpression;
bool ValidReplaceExpression = true;
std::string ReplaceExpression;
std::vector<RegexReplacement> Replacements;
cmMakefile* Makefile = nullptr;
};