Welcome to mirror list, hosted at ThFree Co, Russian Federation.

node_fn_replace_string.cc « nodes « function « nodes « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: afe516a521427156f10a15fcc76a6e2f008a835d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/*
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

#include "BLI_string_utf8.h"

#include "node_function_util.hh"

namespace blender::nodes::node_fn_replace_string_cc {

static void fn_node_replace_string_declare(NodeDeclarationBuilder &b)
{
  b.add_input<decl::String>(N_("String"));
  b.add_input<decl::String>(N_("Find")).description(N_("The string to find in the input string"));
  b.add_input<decl::String>(N_("Replace"))
      .description(N_("The string to replace each match with"));
  b.add_output<decl::String>(N_("String"));
}

static std::string replace_all(std::string str, const std::string &from, const std::string &to)
{
  if (from.length() <= 0) {
    return str;
  }
  const size_t step = to.length() > 0 ? to.length() : 1;

  size_t offset = 0;
  while ((offset = str.find(from, offset)) != std::string::npos) {
    str.replace(offset, from.length(), to);
    offset += step;
  }
  return str;
}

static void fn_node_replace_string_build_multi_function(NodeMultiFunctionBuilder &builder)
{
  static fn::CustomMF_SI_SI_SI_SO<std::string, std::string, std::string, std::string> substring_fn{
      "Replace", [](const std::string &str, const std::string &find, const std::string &replace) {
        return replace_all(str, find, replace);
      }};
  builder.set_matching_fn(&substring_fn);
}

}  // namespace blender::nodes::node_fn_replace_string_cc

void register_node_type_fn_replace_string()
{
  namespace file_ns = blender::nodes::node_fn_replace_string_cc;

  static bNodeType ntype;

  fn_node_type_base(&ntype, FN_NODE_REPLACE_STRING, "Replace String", NODE_CLASS_CONVERTER);
  ntype.declare = file_ns::fn_node_replace_string_declare;
  ntype.build_multi_function = file_ns::fn_node_replace_string_build_multi_function;
  nodeRegisterType(&ntype);
}