Fix for str_to_container if string value has whitespaces (#10224)

* Fix for str_to_container if string value has whitespaces

* Add test

* Add trim for leading and trailing whitespaces

* Apply comments

* Apply comments 2

* Apply comments 3
This commit is contained in:
Oleg Pipikin
2022-03-30 19:48:29 +03:00
committed by GitHub
parent c8720f122d
commit be6db5d69a
4 changed files with 21 additions and 3 deletions

View File

@@ -17,7 +17,7 @@ void RTInfoDeserializer::on_adapter(const std::string& name, ValueAccessor<void>
return;
if (auto a = as_type<AttributeAdapter<std::set<std::string>>>(&adapter)) {
std::set<std::string> ss;
str_to_container(val, ss);
str_to_set_of_strings(val, ss);
a->set(ss);
} else {
IE_THROW() << "Not implemented";

View File

@@ -98,4 +98,19 @@ bool get_dimension_from_attribute(const pugi::xml_node& node, const std::string&
return true;
}
void str_to_set_of_strings(const std::string& value, std::set<std::string>& res) {
std::stringstream ss(value);
std::string field;
while (getline(ss, field, ',')) {
// trim leading and trailing whitespaces
auto strBegin = field.find_first_not_of(" ");
if (strBegin == std::string::npos)
IE_THROW() << "Cannot get a set of strings from \"" << value << "\". Value \"" << field
<< "\" is incorrect";
auto strRange = field.find_last_not_of(" ") - strBegin + 1;
res.insert(field.substr(strBegin, strRange));
}
}
} // namespace ov

View File

@@ -32,6 +32,9 @@ void str_to_container(const std::string& value, T& res) {
res.insert(res.end(), val);
}
}
// separated function for set<string> to keep whitespaces in values
// because stringstream splits its values with whitespace delimiter
void str_to_set_of_strings(const std::string& value, std::set<std::string>& res);
template <class T>
bool getParameters(const pugi::xml_node& node, const std::string& name, std::vector<T>& value) {

View File

@@ -472,7 +472,7 @@ TEST_F(RTInfoDeserialization, NodeV11) {
<layer name="Round" id="1" type="Round" version="opset8">
<data mode="half_to_even"/>
<rt_info>
<attribute name="fused_names" version="0" value="Round1,Round2"/>
<attribute name="fused_names" version="0" value=" Round 1 , Round 2 "/>
</rt_info>
<input>
<port id="1" precision="FP32">
@@ -553,7 +553,7 @@ TEST_F(RTInfoDeserialization, NodeV11) {
auto result = f->get_result();
check_old_api_map_order(result->get_rt_info(), std::vector<uint64_t>({0, 3, 1, 2}));
auto round = result->get_input_node_ptr(0);
check_fused_names(round->get_rt_info(), "Round1,Round2");
check_fused_names(round->get_rt_info(), "Round 1,Round 2");
// read IR v11 with new API
{