Blender V5.3
string_pattern.cc
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#include "BLT_translation.hh"
10
11#include "NOD_string_pattern.hh"
12
13namespace blender::nodes {
14
17 "EXACT",
18 0,
19 N_("Exact"),
20 N_("Remove the one attribute with the given name")},
22 "WILDCARD",
23 0,
24 N_("Wildcard"),
25 N_("Remove all attributes that match the pattern which is allowed to contain a single "
26 "wildcard (*)")},
27 {0, nullptr, 0, nullptr, nullptr},
28};
29
30std::optional<StringPattern> StringPattern::from_str(StringPatternMode mode,
31 StringRef pattern,
32 std::string &r_error)
33{
34 switch (mode) {
36 return StringPattern(pattern, Exact{pattern});
37 }
39 const int wildcard_count = Span(pattern.data(), pattern.size()).count('*');
40 if (wildcard_count == 0) {
41 return StringPattern(pattern, Exact{pattern});
42 }
43 if (wildcard_count >= 2) {
44 r_error = TIP_("Only one * is supported in the pattern");
45 return std::nullopt;
46 }
47 const int wildcard_index = pattern.find('*');
48 const StringRef prefix = StringRef(pattern).substr(0, wildcard_index);
49 const StringRef suffix = StringRef(pattern).substr(wildcard_index + 1);
50 return StringPattern(pattern, Wildcard{prefix, suffix});
51 }
52 }
53 r_error = TIP_("Invalid pattern");
54 return std::nullopt;
55}
56
57bool StringPattern::match(const StringRef query) const
58{
59 return std::visit([&](const auto &variant) { return variant.match(query); }, variant_);
60}
61
62} // namespace blender::nodes
#define TIP_(msgid)
constexpr Span()=default
constexpr StringRef()
constexpr int64_t size() const
constexpr int64_t find(char c, int64_t pos=0) const
constexpr StringRef substr(int64_t start, int64_t size) const
constexpr const char * data() const
bool match(StringRef query) const
static std::optional< StringPattern > from_str(StringPatternMode mode, StringRef pattern, std::string &r_error)
const EnumPropertyItem string_pattern_mode_items[]
#define N_(msgid)