imposed_ensemble_matcher.h 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // Copyright 2018 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef COMPONENTS_ZUCCHINI_IMPOSED_ENSEMBLE_MATCHER_H_
  5. #define COMPONENTS_ZUCCHINI_IMPOSED_ENSEMBLE_MATCHER_H_
  6. #include <stddef.h>
  7. #include <string>
  8. #include <vector>
  9. #include "components/zucchini/buffer_view.h"
  10. #include "components/zucchini/element_detection.h"
  11. #include "components/zucchini/ensemble_matcher.h"
  12. namespace zucchini {
  13. // A class to parse imposed match format, which is either an empty string (no
  14. // imposed patch), or a string formatted as:
  15. // "#+#=#+#,#+#=#+#,..." (e.g., "1+2=3+4", "1+2=3+4,5+6=7+8"),
  16. // where "#+#=#+#" encodes a match as 4 unsigned integers:
  17. // [offset in "old", size in "old", offset in "new", size in "new"].
  18. class ImposedMatchParser {
  19. public:
  20. enum Status {
  21. kSuccess,
  22. kInvalidDelimiter,
  23. kParseError,
  24. kOutOfBound,
  25. kOverlapInNew,
  26. kTypeMismatch,
  27. };
  28. ImposedMatchParser();
  29. ImposedMatchParser(const ImposedMatchParser&) = delete;
  30. const ImposedMatchParser& operator=(const ImposedMatchParser&) = delete;
  31. ~ImposedMatchParser();
  32. // Parses |imposed_matches| and writes the results to member variables.
  33. // |old_image| and |new_image| are used for validation. Returns a Status value
  34. // to signal success or various error modes. |detector| is used to validate
  35. // Element types for matched pairs. This should only be called once for each
  36. // instance.
  37. Status Parse(std::string imposed_matches,
  38. ConstBufferView old_image,
  39. ConstBufferView new_image,
  40. ElementDetector&& detector);
  41. size_t num_identical() const { return num_identical_; }
  42. std::vector<ElementMatch>* mutable_matches() { return &matches_; }
  43. std::vector<ElementMatch>* mutable_bad_matches() { return &bad_matches_; }
  44. private:
  45. size_t num_identical_ = 0;
  46. std::vector<ElementMatch> matches_;
  47. // Stores "forgiven" bad matches, so the caller can impose matches for
  48. // unsupported image types (which will simply be ignored). Note that imposing
  49. // matches for known but incompatible image types would result in error.
  50. std::vector<ElementMatch> bad_matches_;
  51. };
  52. // An ensemble matcher that parses a format string that describes matches.
  53. class ImposedEnsembleMatcher : public EnsembleMatcher {
  54. public:
  55. // |imposed_matches| specifies imposed maches, using a format described below.
  56. // Validation is performed in RunMatch().
  57. explicit ImposedEnsembleMatcher(const std::string& imposed_matches);
  58. ImposedEnsembleMatcher(const ImposedEnsembleMatcher&) = delete;
  59. const ImposedEnsembleMatcher& operator=(const ImposedEnsembleMatcher&) =
  60. delete;
  61. ~ImposedEnsembleMatcher() override;
  62. // EnsembleMatcher:
  63. bool RunMatch(ConstBufferView old_image, ConstBufferView new_image) override;
  64. private:
  65. const std::string imposed_matches_;
  66. };
  67. } // namespace zucchini
  68. #endif // COMPONENTS_ZUCCHINI_IMPOSED_ENSEMBLE_MATCHER_H_