diff --git a/src/libslic3r/GCode/GCodeProcessor.cpp b/src/libslic3r/GCode/GCodeProcessor.cpp index 6baee30403..690a0ed359 100644 --- a/src/libslic3r/GCode/GCodeProcessor.cpp +++ b/src/libslic3r/GCode/GCodeProcessor.cpp @@ -2887,15 +2887,15 @@ void GCodeProcessor::process_G1(const std::array, 4>& axes void GCodeProcessor::process_G2_G3(const GCodeReader::GCodeLine& line, bool clockwise) { enum class EFitting { None, IJ, R }; - const char *axis_pos_I = nullptr; - const char *axis_pos_J = nullptr; + std::string_view axis_pos_I; + std::string_view axis_pos_J; EFitting fitting = EFitting::None; if (line.has('R')) { fitting = EFitting::R; } else { axis_pos_I = line.axis_pos('I'); axis_pos_J = line.axis_pos('J'); - if (axis_pos_I || axis_pos_J) + if (! axis_pos_I.empty() || ! axis_pos_J.empty()) fitting = EFitting::IJ; } @@ -2931,9 +2931,9 @@ void GCodeProcessor::process_G2_G3(const GCodeReader::GCodeLine& line, bool cloc } else { assert(fitting == EFitting::IJ); - if (axis_pos_I && ! line.has_value(axis_pos_I, rel_center.x())) + if (! axis_pos_I.empty() && ! line.has_value(axis_pos_I, rel_center.x())) return; - if (axis_pos_J && ! line.has_value(axis_pos_J, rel_center.y())) + if (! axis_pos_J.empty() && ! line.has_value(axis_pos_J, rel_center.y())) return; } diff --git a/src/libslic3r/GCodeReader.cpp b/src/libslic3r/GCodeReader.cpp index ed10f0de8c..9cee82d048 100644 --- a/src/libslic3r/GCodeReader.cpp +++ b/src/libslic3r/GCodeReader.cpp @@ -237,20 +237,22 @@ const char* GCodeReader::axis_pos(const char *raw_str, char axis) bool GCodeReader::GCodeLine::has(char axis) const { - return this->axis_pos(axis); -} - -const char* GCodeReader::GCodeLine::axis_pos(char axis) const -{ return GCodeReader::axis_pos(this->raw().c_str(), axis); } -static bool GCodeReader::GCodeLine::has_value(const char *axis_pos, float &value) +std::string_view GCodeReader::GCodeLine::axis_pos(char axis) const +{ + const std::string &s = this->raw(); + const char *c = GCodeReader::axis_pos(this->raw().c_str(), axis); + return c ? std::string_view{ c, s.size() - (c - s.data()) } : std::string_view(); +} + +bool GCodeReader::GCodeLine::has_value(std::string_view axis_pos, float &value) { - if (const char *c = axis_pos; c) { + if (const char *c = axis_pos.data(); c) { // Try to parse the numeric value. double v = 0.; - const char *end = m_raw.c_str() + m_raw.size(); + const char *end = axis_pos.data() + axis_pos.size(); auto [pend, ec] = fast_float::from_chars(++ c, end, v); if (pend != c && is_end_of_word(*pend)) { // The axis value has been parsed correctly. @@ -267,9 +269,9 @@ bool GCodeReader::GCodeLine::has_value(char axis, float &value) const return this->has_value(this->axis_pos(axis), value); } -static bool GCodeReader::GCodeLine::has_value(const char *axis_pos, int &value) +bool GCodeReader::GCodeLine::has_value(std::string_view axis_pos, int &value) { - if (const char *c = axis_pos; c) { + if (const char *c = axis_pos.data(); c) { // Try to parse the numeric value. char *pend = nullptr; long v = strtol(++ c, &pend, 10); diff --git a/src/libslic3r/GCodeReader.hpp b/src/libslic3r/GCodeReader.hpp index 0919043f44..38e1a91d82 100644 --- a/src/libslic3r/GCodeReader.hpp +++ b/src/libslic3r/GCodeReader.hpp @@ -31,15 +31,15 @@ public: { size_t pos = m_raw.find(';'); return (pos == std::string::npos) ? std::string_view() : std::string_view(m_raw).substr(pos + 1); } // Return position in this->raw() string starting with the "axis" character. - const char* axis_pos(char axis) const; + std::string_view axis_pos(char axis) const; bool has(Axis axis) const { return (m_mask & (1 << int(axis))) != 0; } float value(Axis axis) const { return m_axis[axis]; } bool has(char axis) const; bool has_value(char axis, float &value) const; bool has_value(char axis, int &value) const; // Parse value of an axis from raw string starting at axis_pos. - static bool has_value(const char *axis_pos, float &value); - static bool has_value(const char *axis_pos, int &value); + static bool has_value(std::string_view axis_pos, float &value); + static bool has_value(std::string_view axis_pos, int &value); float new_X(const GCodeReader &reader) const { return this->has(X) ? this->x() : reader.x(); } float new_Y(const GCodeReader &reader) const { return this->has(Y) ? this->y() : reader.y(); } float new_Z(const GCodeReader &reader) const { return this->has(Z) ? this->z() : reader.z(); }