PatternMatcher.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #pragma once
  2. /*
  3. * Copyright (c) Contributors to the Open 3D Engine Project.
  4. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  5. *
  6. * SPDX-License-Identifier: Apache-2.0 OR MIT
  7. *
  8. */
  9. #include <AzCore/RTTI/RTTI.h>
  10. #include <AzCore/std/string/regex.h>
  11. #include <AzCore/std/string/string.h>
  12. #include <AzCore/JSON/document.h>
  13. #include <SceneAPI/SceneCore/SceneCoreConfiguration.h>
  14. namespace AZ
  15. {
  16. class ReflectContext;
  17. namespace SceneAPI
  18. {
  19. namespace SceneCore
  20. {
  21. // PatternMatcher stores a pattern and a matching approach for later use.
  22. // Strings can then be checked against the stored pattern.
  23. // The supported approaches are:
  24. // Prefix - Matches if the string starts with the stored pattern.
  25. // Postfix - Matches if the string ends with the stored pattern.
  26. // Regex - Matches if the string matches the given regular expression.
  27. class PatternMatcher
  28. {
  29. public:
  30. AZ_RTTI(PatternMatcher, "{F043EC4E-FA29-4A5E-BEF6-13C661048FC4}");
  31. virtual ~PatternMatcher() = default;
  32. enum class MatchApproach
  33. {
  34. PreFix,
  35. PostFix,
  36. Regex
  37. };
  38. PatternMatcher() = default;
  39. SCENE_CORE_API PatternMatcher(const char* pattern, MatchApproach matcher);
  40. SCENE_CORE_API PatternMatcher(const AZStd::string& pattern, MatchApproach matcher);
  41. SCENE_CORE_API PatternMatcher(AZStd::string&& pattern, MatchApproach matcher);
  42. SCENE_CORE_API PatternMatcher(const AZStd::span<const AZStd::string_view> patterns, MatchApproach matcher);
  43. SCENE_CORE_API PatternMatcher(const PatternMatcher& rhs);
  44. SCENE_CORE_API PatternMatcher(PatternMatcher&& rhs);
  45. SCENE_CORE_API PatternMatcher& operator=(const PatternMatcher& rhs);
  46. SCENE_CORE_API PatternMatcher& operator=(PatternMatcher&& rhs);
  47. SCENE_CORE_API bool LoadFromJson(rapidjson::Document::ConstMemberIterator member);
  48. SCENE_CORE_API bool MatchesPattern(const char* name, size_t nameLength) const;
  49. SCENE_CORE_API bool MatchesPattern(AZStd::string_view name) const;
  50. SCENE_CORE_API const AZStd::string& GetPattern() const;
  51. SCENE_CORE_API MatchApproach GetMatchApproach() const;
  52. static void Reflect(AZ::ReflectContext* context);
  53. private:
  54. AZStd::vector<AZStd::string> m_patterns;
  55. MatchApproach m_matcher = MatchApproach::PostFix;
  56. mutable AZStd::vector<AZStd::unique_ptr<AZStd::regex>> m_regexMatchers;
  57. };
  58. } // SceneCore
  59. } // SceneAPI
  60. } // AZ