Blender V5.3
NOD_string_pattern.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2026 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#pragma once
10
11#include <variant>
12
13#include "BLI_string_ref.hh"
14
15#include "RNA_types.hh"
16
17namespace blender::nodes {
18
21 Exact = 0,
24};
25
30class StringPattern {
31 private:
32 struct Exact {
33 StringRef pattern;
34
35 bool match(const StringRef query) const
36 {
37 return pattern == query;
38 }
39 };
40
41 struct Wildcard {
42 StringRef prefix;
43 StringRef suffix;
44
45 bool match(const StringRef query) const
46 {
47 return query.startswith(prefix) && query.endswith(suffix);
48 }
49 };
50
51 using PatternVariant = std::variant<Exact, Wildcard>;
52 StringRef full_pattern_;
53 PatternVariant variant_;
54
55 StringPattern(StringRef full_pattern, PatternVariant variant)
56 : full_pattern_(full_pattern), variant_(std::move(variant))
57 {
58 }
59
60 public:
65 static std::optional<StringPattern> from_str(StringPatternMode mode,
66 StringRef pattern,
67 std::string &r_error);
68
70 bool match(StringRef query) const;
71
73 std::optional<StringRef> exact_pattern() const
74 {
75 if (const Exact *exact = std::get_if<Exact>(&variant_)) {
76 return exact->pattern;
77 }
78 return std::nullopt;
79 }
80
82 {
83 return full_pattern_;
84 }
85};
86
88
89} // namespace blender::nodes
constexpr bool startswith(StringRef prefix) const
constexpr bool endswith(StringRef suffix) const
bool match(StringRef query) const
static std::optional< StringPattern > from_str(StringPatternMode mode, StringRef pattern, std::string &r_error)
std::optional< StringRef > exact_pattern() const
const EnumPropertyItem string_pattern_mode_items[]