From 9fd6eb745e7d00d7998fa367a9a6a151b1ffb8d1 Mon Sep 17 00:00:00 2001 From: Julian Oes Date: Thu, 1 Oct 2026 15:46:52 +1300 Subject: [PATCH] fix(simulation): parse gz motor joint names without std::regex (#28941) Replace the std::regex match for "rotor__joint" in the motor failure plugin with a plain prefix/suffix/digit check to fix the build. Assisted-by: Claude:claude-opus-5-5 Signed-off-by: Julian Oes --- .../motor_failure/MotorFailureSystem.cpp | 44 ++++++++++++------- .../motor_failure/MotorFailureSystem.hpp | 1 - 2 files changed, 28 insertions(+), 17 deletions(-) diff --git a/src/modules/simulation/gz_plugins/motor_failure/MotorFailureSystem.cpp b/src/modules/simulation/gz_plugins/motor_failure/MotorFailureSystem.cpp index e6dd2955d78..a951b5c7c23 100644 --- a/src/modules/simulation/gz_plugins/motor_failure/MotorFailureSystem.cpp +++ b/src/modules/simulation/gz_plugins/motor_failure/MotorFailureSystem.cpp @@ -39,6 +39,9 @@ #include #include +#include +#include + using namespace gz; using namespace sim; using namespace systems; @@ -93,9 +96,9 @@ void MotorFailureSystem::FindMotorJoints(EntityComponentManager &_ecm) // Get all joints in the model auto joints = this->_model.Joints(_ecm); - // Regular expression to match rotor joints (e.g., "rotor_0_joint", "rotor_1_joint") - std::regex motorPattern("rotor_(\\d+)_joint"); - std::smatch match; + // Match rotor joints (e.g., "rotor_0_joint", "rotor_1_joint") + const std::string prefix = "rotor_"; + const std::string suffix = "_joint"; // Find rotor joints and sort by motor number std::map motorMap; @@ -104,21 +107,30 @@ void MotorFailureSystem::FindMotorJoints(EntityComponentManager &_ecm) auto nameComp = _ecm.Component(joint); if (nameComp) { - std::string jointName = nameComp->Data(); + const std::string &jointName = nameComp->Data(); - // Try to match the joint name against the pattern - if (std::regex_match(jointName, match, motorPattern)) { - // Extract motor number from the first capture group - try { - int motorNumber = std::stoi(match[1].str()); - motorMap[motorNumber] = joint; - gzdbg << "[MotorFailureSystem] Found motor " << motorNumber - << ": " << jointName << std::endl; + if (jointName.size() <= prefix.size() + suffix.size() + || jointName.compare(0, prefix.size(), prefix) != 0 + || jointName.compare(jointName.size() - suffix.size(), suffix.size(), suffix) != 0) { + continue; + } - } catch (const std::exception &e) { - gzwarn << "[MotorFailureSystem] Failed to parse motor number from: " - << jointName << std::endl; - } + const std::string digits = jointName.substr(prefix.size(), + jointName.size() - prefix.size() - suffix.size()); + + if (!std::all_of(digits.begin(), digits.end(), [](unsigned char c) { return std::isdigit(c); })) { + continue; + } + + try { + int motorNumber = std::stoi(digits); + motorMap[motorNumber] = joint; + gzdbg << "[MotorFailureSystem] Found motor " << motorNumber + << ": " << jointName << std::endl; + + } catch (const std::exception &e) { + gzwarn << "[MotorFailureSystem] Failed to parse motor number from: " + << jointName << std::endl; } } } diff --git a/src/modules/simulation/gz_plugins/motor_failure/MotorFailureSystem.hpp b/src/modules/simulation/gz_plugins/motor_failure/MotorFailureSystem.hpp index 1f3c2161286..cc47ccae3f2 100644 --- a/src/modules/simulation/gz_plugins/motor_failure/MotorFailureSystem.hpp +++ b/src/modules/simulation/gz_plugins/motor_failure/MotorFailureSystem.hpp @@ -41,7 +41,6 @@ #include #include #include -#include // Gazebo Transport includes #include