Fork of the espurna firmware for `mhsw` switches
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

92 lines
1.7 KiB

  1. /*
  2. Part of the API MODULE
  3. Copyright (C) 2021 by Maxim Prokhorov <prokhorov dot max at outlook dot com>
  4. */
  5. #pragma once
  6. #include <Arduino.h>
  7. #include <vector>
  8. // -----------------------------------------------------------------------------
  9. struct PathPart {
  10. enum class Type {
  11. Unknown,
  12. Value,
  13. SingleWildcard,
  14. MultiWildcard
  15. };
  16. Type type;
  17. size_t offset;
  18. size_t length;
  19. };
  20. struct PathParts {
  21. using Parts = std::vector<PathPart>;
  22. PathParts() = delete;
  23. PathParts(const PathParts&) = default;
  24. PathParts(PathParts&&) noexcept = default;
  25. explicit PathParts(const String& path);
  26. explicit operator bool() const {
  27. return _ok;
  28. }
  29. void clear() {
  30. _parts.clear();
  31. }
  32. void reserve(size_t size) {
  33. _parts.reserve(size);
  34. }
  35. String operator[](size_t index) const {
  36. auto& part = _parts[index];
  37. return _path.substring(part.offset, part.offset + part.length);
  38. }
  39. const String& path() const {
  40. return _path;
  41. }
  42. const Parts& parts() const {
  43. return _parts;
  44. }
  45. size_t size() const {
  46. return _parts.size();
  47. }
  48. Parts::const_iterator begin() const {
  49. return _parts.begin();
  50. }
  51. Parts::const_iterator end() const {
  52. return _parts.end();
  53. }
  54. bool match(const PathParts& path) const;
  55. bool match(const String& path) const {
  56. return match(PathParts(path));
  57. }
  58. private:
  59. PathPart& emplace_back(PathPart::Type type, size_t offset, size_t length) {
  60. PathPart part { type, offset, length };
  61. _parts.push_back(std::move(part));
  62. return _parts.back();
  63. }
  64. const String& _path;
  65. Parts _parts;
  66. bool _ok { false };
  67. };