15#ifndef HARDWARE_INTERFACE__LEXICAL_CASTS_HPP_
16#define HARDWARE_INTERFACE__LEXICAL_CASTS_HPP_
33double stod(
const std::string & s);
35bool parse_bool(
const std::string & bool_string);
38std::vector<T> parse_array(
const std::string & array_string)
41 const std::regex array_regex(R
"(^\[\s*([^\[\]]*\s*(,\s*[^\[\]]+\s*)*)?\]$)");
42 if (!std::regex_match(array_string, array_regex))
44 throw std::invalid_argument(
45 "String must be a flat array: starts with '[' and ends with ']', no nested arrays");
49 const std::regex empty_or_spaces_regex(R
"(^\[\s*\]$)");
50 if (std::regex_match(array_string, empty_or_spaces_regex))
57 const std::regex comma_separated_regex(R
"(^\[\s*([^,\s]+(\s*,\s*[^,\s]+)*)?\s*\]$)");
58 if (!std::regex_match(array_string, comma_separated_regex))
60 throw std::invalid_argument(
61 "String must be a flat array with comma-separated values and no spaces between them");
64 std::vector<T> result = {};
65 if (array_string ==
"[]")
72 const std::regex value_regex(R
"([^\s,\[\]]+)");
73 auto begin = std::sregex_iterator(array_string.begin(), array_string.end(), value_regex);
74 auto end = std::sregex_iterator();
76 for (
auto it = begin; it != end; ++it)
78 const std::string value_str = it->str();
79 if constexpr (std::is_same_v<T, std::string>)
81 result.push_back(value_str);
83 else if constexpr (std::is_same_v<T, bool>)
85 result.push_back(parse_bool(value_str));
87 else if constexpr (std::is_floating_point_v<T> || std::is_integral_v<T>)
92 result.push_back(value);
94 catch (
const std::exception &)
96 throw std::invalid_argument(
97 "Failed converting string to floating point or integer: " + value_str);
102 throw std::invalid_argument(
"Unsupported type for parsing: " + std::string(
typeid(T).name()));
108std::vector<std::string> parse_string_array(
const std::string & string_array_string);
Definition actuator.hpp:22
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