Prusa Slicer 2.6.0
Loading...
Searching...
No Matches
Slic3r::GCodeFindReplace Class Reference

#include <src/libslic3r/GCode/FindReplace.hpp>

+ Collaboration diagram for Slic3r::GCodeFindReplace:

Classes

struct  Substitution
 

Public Member Functions

 GCodeFindReplace (const PrintConfig &print_config)
 
 GCodeFindReplace (const std::vector< std::string > &gcode_substitutions)
 
std::string process_layer (const std::string &gcode)
 

Private Attributes

std::vector< Substitutionm_substitutions
 

Detailed Description


Class Documentation

◆ Slic3r::GCodeFindReplace::Substitution

struct Slic3r::GCodeFindReplace::Substitution
+ Collaboration diagram for Slic3r::GCodeFindReplace::Substitution:
Class Members
bool case_insensitive { false }
string format
string plain_pattern
bool regexp { false }
regex regexp_pattern
bool single_line { false }
bool whole_word { false }

Constructor & Destructor Documentation

◆ GCodeFindReplace() [1/2]

Slic3r::GCodeFindReplace::GCodeFindReplace ( const PrintConfig &  print_config)
inline
12: GCodeFindReplace(print_config.gcode_substitutions.values) {}
GCodeFindReplace(const PrintConfig &print_config)
Definition FindReplace.hpp:12

◆ GCodeFindReplace() [2/2]

Slic3r::GCodeFindReplace::GCodeFindReplace ( const std::vector< std::string > &  gcode_substitutions)
26{
27 if ((gcode_substitutions.size() % 4) != 0)
28 throw RuntimeError("Invalid length of gcode_substitutions parameter");
29
30 m_substitutions.reserve(gcode_substitutions.size() / 4);
31 for (size_t i = 0; i < gcode_substitutions.size(); i += 4) {
32 Substitution out;
33 try {
34 out.plain_pattern = gcode_substitutions[i];
35 out.format = gcode_substitutions[i + 1];
36 const std::string &params = gcode_substitutions[i + 2];
37 out.regexp = strchr(params.c_str(), 'r') != nullptr || strchr(params.c_str(), 'R') != nullptr;
38 out.case_insensitive = strchr(params.c_str(), 'i') != nullptr || strchr(params.c_str(), 'I') != nullptr;
39 out.whole_word = strchr(params.c_str(), 'w') != nullptr || strchr(params.c_str(), 'W') != nullptr;
40 out.single_line = strchr(params.c_str(), 's') != nullptr || strchr(params.c_str(), 'S') != nullptr;
41 if (out.regexp) {
42 out.regexp_pattern.assign(
43 out.whole_word ?
44 std::string("\\b") + out.plain_pattern + "\\b" :
45 out.plain_pattern,
46 (out.case_insensitive ? boost::regex::icase : 0) | boost::regex::optimize);
47 } else {
48 unescape_extended_search_mode(out.plain_pattern);
50 }
51 } catch (const std::exception &ex) {
52 throw RuntimeError(std::string("Invalid gcode_substitutions parameter, failed to compile regular expression: ") + ex.what());
53 }
54 m_substitutions.emplace_back(std::move(out));
55 }
56}
std::vector< Substitution > m_substitutions
Definition FindReplace.hpp:30
gcode_substitutions((ConfigOptionString, layer_gcode))((ConfigOptionFloat
const void unescape_extended_search_mode(std::string &s)
Definition FindReplace.cpp:10
Definition args.hpp:18

References Slic3r::GCodeFindReplace::Substitution::case_insensitive, Slic3r::GCodeFindReplace::Substitution::format, Slic3r::gcode_substitutions(), m_substitutions, Slic3r::GCodeFindReplace::Substitution::plain_pattern, Slic3r::GCodeFindReplace::Substitution::regexp, Slic3r::GCodeFindReplace::Substitution::regexp_pattern, Slic3r::GCodeFindReplace::Substitution::single_line, Slic3r::unescape_extended_search_mode(), and Slic3r::GCodeFindReplace::Substitution::whole_word.

+ Here is the call graph for this function:

Member Function Documentation

◆ process_layer()

std::string Slic3r::GCodeFindReplace::process_layer ( const std::string &  gcode)
109{
110 std::string out;
111 const std::string *in = &ain;
112 std::string temp;
113 temp.reserve(in->size());
114
115 for (const Substitution &substitution : m_substitutions) {
116 if (substitution.regexp) {
117 temp.clear();
118 temp.reserve(in->size());
119 boost::regex_replace(ToStringIterator(temp), in->begin(), in->end(),
120 substitution.regexp_pattern, substitution.format,
121 (substitution.single_line ? boost::match_single_line | boost::match_default : boost::match_not_dot_newline | boost::match_default) | boost::format_all);
122 std::swap(out, temp);
123 } else {
124 if (in == &ain)
125 out = ain;
126 // Plain substitution
127 if (substitution.case_insensitive) {
128 if (substitution.whole_word)
129 find_and_replace_whole_word(out, substitution.plain_pattern, substitution.format,
130 [](const std::string &str, size_t start_pos, const std::string &match) {
131 auto begin = str.begin() + start_pos;
132 boost::iterator_range<std::string::const_iterator> r1(begin, str.end());
133 boost::iterator_range<std::string::const_iterator> r2(match.begin(), match.end());
134 auto res = boost::ifind_first(r1, r2);
135 return res ? std::make_pair(size_t(res.begin() - str.begin()), size_t(res.end() - str.begin())) : std::make_pair(std::string::npos, std::string::npos);
136 });
137 else
138 boost::ireplace_all(out, substitution.plain_pattern, substitution.format);
139 } else {
140 if (substitution.whole_word)
141 find_and_replace_whole_word(out, substitution.plain_pattern, substitution.format,
142 [](const std::string &str, size_t start_pos, const std::string &match) {
143 size_t pos = str.find(match, start_pos);
144 return std::make_pair(pos, pos + (pos == std::string::npos ? 0 : match.size()));
145 });
146 else
147 boost::replace_all(out, substitution.plain_pattern, substitution.format);
148 }
149 }
150 in = &out;
151 }
152
153 return out;
154}
static void find_and_replace_whole_word(std::string &inout, const std::string &match, const std::string &replace, FindFn find_fn)
Definition FindReplace.cpp:86

References Slic3r::find_and_replace_whole_word(), and m_substitutions.

+ Here is the call graph for this function:

Member Data Documentation

◆ m_substitutions

std::vector<Substitution> Slic3r::GCodeFindReplace::m_substitutions
private

Referenced by GCodeFindReplace(), and process_layer().


The documentation for this class was generated from the following files: