15#ifndef HARDWARE_INTERFACE__LEXICAL_CASTS_HPP_
16#define HARDWARE_INTERFACE__LEXICAL_CASTS_HPP_
33double stod(
const std::string & s);
48bool parse_bool(
const std::string & bool_string);
51std::vector<T> parse_array(
const std::string & array_string)
54 const std::regex array_regex(R
"(^\[\s*([^\[\]]*\s*(,\s*[^\[\]]+\s*)*)?\]$)");
55 if (!std::regex_match(array_string, array_regex))
57 throw std::invalid_argument(
58 "String must be a flat array: starts with '[' and ends with ']', no nested arrays");
62 const std::regex empty_or_spaces_regex(R
"(^\[\s*\]$)");
63 if (std::regex_match(array_string, empty_or_spaces_regex))
70 const std::regex comma_separated_regex(R
"(^\[\s*([^,\s]+(\s*,\s*[^,\s]+)*)?\s*\]$)");
71 if (!std::regex_match(array_string, comma_separated_regex))
73 throw std::invalid_argument(
74 "String must be a flat array with comma-separated values and no spaces between them");
77 std::vector<T> result = {};
78 if (array_string ==
"[]")
85 const std::regex value_regex(R
"([^\s,\[\]]+)");
86 auto begin = std::sregex_iterator(array_string.begin(), array_string.end(), value_regex);
87 auto end = std::sregex_iterator();
89 for (
auto it = begin; it != end; ++it)
91 const std::string value_str = it->str();
92 if constexpr (std::is_same_v<T, std::string>)
94 result.push_back(value_str);
96 else if constexpr (std::is_same_v<T, bool>)
100 else if constexpr (std::is_floating_point_v<T> || std::is_integral_v<T>)
105 result.push_back(value);
107 catch (
const std::exception &)
109 throw std::invalid_argument(
110 "Failed converting string to floating point or integer: " + value_str);
115 throw std::invalid_argument(
"Unsupported type for parsing: " + std::string(
typeid(T).name()));
121std::vector<std::string> parse_string_array(
const std::string & string_array_string);
Definition actuator.hpp:22
std::string to_lower_case(const std::string &string)
Convert a string to lower case.
Definition lexical_casts.cpp:65
double stod(const std::string &s)
Helper function to convert a std::string to double in a locale-independent way.
Definition lexical_casts.cpp:56
bool parse_bool(const std::string &bool_string)
Parse a boolean value from a string.
Definition lexical_casts.cpp:74