cmStringReplaceHelper.h 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #ifndef cmStringReplaceHelper_h
  4. #define cmStringReplaceHelper_h
  5. #include "cmsys/RegularExpression.hxx"
  6. #include <string>
  7. #include <vector>
  8. class cmMakefile;
  9. class cmStringReplaceHelper
  10. {
  11. public:
  12. cmStringReplaceHelper(const std::string& regex,
  13. const std::string& replace_expr,
  14. cmMakefile* makefile = nullptr);
  15. bool IsRegularExpressionValid() const
  16. {
  17. return this->RegularExpression.is_valid();
  18. }
  19. bool IsReplaceExpressionValid() const
  20. {
  21. return this->ValidReplaceExpression;
  22. }
  23. bool Replace(const std::string& input, std::string& output);
  24. const std::string& GetError() { return this->ErrorString; }
  25. private:
  26. class RegexReplacement
  27. {
  28. public:
  29. RegexReplacement(const char* s)
  30. : Number(-1)
  31. , Value(s)
  32. {
  33. }
  34. RegexReplacement(const std::string& s)
  35. : Number(-1)
  36. , Value(s)
  37. {
  38. }
  39. RegexReplacement(int n)
  40. : Number(n)
  41. , Value()
  42. {
  43. }
  44. RegexReplacement() {}
  45. int Number;
  46. std::string Value;
  47. };
  48. void ParseReplaceExpression();
  49. std::string ErrorString;
  50. std::string RegExString;
  51. cmsys::RegularExpression RegularExpression;
  52. bool ValidReplaceExpression = true;
  53. std::string ReplaceExpression;
  54. std::vector<RegexReplacement> Replacements;
  55. cmMakefile* Makefile = nullptr;
  56. };
  57. #endif