Merge pull request #8228

1ce9e9c Remove dead code from parserse_base_utils and fix unit tests (Jeffrey)
This commit is contained in:
luigi1111 2022-04-06 00:22:23 -04:00
commit c11385591e
No known key found for this signature in database
GPG key ID: F4ACA0183641E010
3 changed files with 74 additions and 143 deletions

View file

@ -107,48 +107,10 @@ namespace misc_utils
*/ */
void match_string2(std::string::const_iterator& star_end_string, std::string::const_iterator buf_end, std::string& val); void match_string2(std::string::const_iterator& star_end_string, std::string::const_iterator buf_end, std::string& val);
inline bool match_string(std::string::const_iterator& star_end_string, std::string::const_iterator buf_end, std::string& val)
{
try
{
match_string2(star_end_string, buf_end, val);
return true;
}
catch(...)
{
return false;
}
}
void match_number2(std::string::const_iterator& star_end_string, std::string::const_iterator buf_end, boost::string_ref& val, bool& is_float_val, bool& is_signed_val); void match_number2(std::string::const_iterator& star_end_string, std::string::const_iterator buf_end, boost::string_ref& val, bool& is_float_val, bool& is_signed_val);
inline bool match_number(std::string::const_iterator& star_end_string, std::string::const_iterator buf_end, boost::string_ref& val)
{
try
{
bool is_v_float = false;bool is_signed_val = false;
match_number2(star_end_string, buf_end, val, is_v_float, is_signed_val);
return !is_v_float;
}
catch(...)
{
return false;
}
}
void match_word2(std::string::const_iterator& star_end_string, std::string::const_iterator buf_end, boost::string_ref& val); void match_word2(std::string::const_iterator& star_end_string, std::string::const_iterator buf_end, boost::string_ref& val);
inline bool match_word(std::string::const_iterator& star_end_string, std::string::const_iterator buf_end, boost::string_ref& val)
{
try
{
match_word2(star_end_string, buf_end, val);
return true;
}
catch(...)
{
return false;
}
}
bool match_word_with_extrasymb(std::string::const_iterator& star_end_string, std::string::const_iterator buf_end, std::string& val);
bool match_word_til_equal_mark(std::string::const_iterator& star_end_string, std::string::const_iterator buf_end, std::string::const_iterator& word_end);
} }
} }
} }

View file

@ -239,44 +239,6 @@ namespace misc_utils
} }
ASSERT_MES_AND_THROW("failed to match word number in json entry: " << std::string(star_end_string, buf_end)); ASSERT_MES_AND_THROW("failed to match word number in json entry: " << std::string(star_end_string, buf_end));
} }
bool match_word_with_extrasymb(std::string::const_iterator& star_end_string, std::string::const_iterator buf_end, std::string& val)
{
val.clear();
for(std::string::const_iterator it = star_end_string;it != buf_end;++it)
{
if(!isalnum(*it) && *it != '-' && *it != '_')
{
val.assign(star_end_string, it);
if(val.size())
{
star_end_string = --it;
return true;
}else
return false;
}
}
return false;
}
bool match_word_til_equal_mark(std::string::const_iterator& star_end_string, std::string::const_iterator buf_end, std::string::const_iterator& word_end)
{
word_end = star_end_string;
for(std::string::const_iterator it = star_end_string;it != buf_end;++it)
{
if(isspace(*it))
{
continue;
}else if( *it == '=' )
{
star_end_string = it;
word_end = it;
return true;
}
}
return false;
}
} }
} }
} }

View file

@ -1748,68 +1748,44 @@ TEST(parsing, isdigit)
TEST(parsing, number) TEST(parsing, number)
{ {
boost::string_ref val; struct match_number_test_data {
std::string s; std::string in_str;
std::string::const_iterator i; std::string expect_out_str;
bool expect_is_float;
bool expect_is_signed;
};
// Add test cases as needed
struct match_number_test_data test_data[] = {
{ "0 ", "0", false, false },
{ "000 ", "000", false, false },
{ "10x", "10", false, false },
{ "10.09/", "10.09", true, false },
{ "-1.r", "-1.", true, true },
{ "-49.;", "-49.", true, true },
{ "0.78/", "0.78", true, false },
{ "33E9$", "33E9", true, false },
{ ".34e2=", ".34e2", true, false },
{ "-9.34e-2=", "-9.34e-2", true, true },
{ "+9.34e+03=", "+9.34e+03", true, false }
};
// the parser expects another character to end the number, and accepts things // the parser expects another character to end the number, and accepts things
// that aren't numbers, as it's meant as a pre-filter for strto* functions, // that aren't numbers, as it's meant as a pre-filter for strto* functions,
// so we just check that numbers get accepted, but don't test non numbers // so we just check that numbers get accepted, but don't test non numbers
// We set is_float/signed_val to the opposite of what we expect the result to
// make sure that match_number2 is changing the bools as expected
s = "0 "; for (const auto& tdata : test_data) {
i = s.begin(); std::string::const_iterator it = tdata.in_str.begin();
epee::misc_utils::parse::match_number(i, s.end(), val); boost::string_ref out_val = "<unassigned>";
ASSERT_EQ(val, "0"); bool is_float_val = !tdata.expect_is_float;
bool is_signed_val = !tdata.expect_is_signed;
s = "000 "; epee::misc_utils::parse::match_number2(it, tdata.in_str.end(), out_val, is_float_val, is_signed_val);
i = s.begin(); EXPECT_EQ(out_val, tdata.expect_out_str);
epee::misc_utils::parse::match_number(i, s.end(), val); EXPECT_EQ(is_float_val, tdata.expect_is_float);
ASSERT_EQ(val, "000"); EXPECT_EQ(is_signed_val, tdata.expect_is_signed);
}
s = "10x";
i = s.begin();
epee::misc_utils::parse::match_number(i, s.end(), val);
ASSERT_EQ(val, "10");
s = "10.09/";
i = s.begin();
epee::misc_utils::parse::match_number(i, s.end(), val);
ASSERT_EQ(val, "10.09");
s = "-1.r";
i = s.begin();
epee::misc_utils::parse::match_number(i, s.end(), val);
ASSERT_EQ(val, "-1.");
s = "-49.;";
i = s.begin();
epee::misc_utils::parse::match_number(i, s.end(), val);
ASSERT_EQ(val, "-49.");
s = "0.78/";
i = s.begin();
epee::misc_utils::parse::match_number(i, s.end(), val);
ASSERT_EQ(val, "0.78");
s = "33E9$";
i = s.begin();
epee::misc_utils::parse::match_number(i, s.end(), val);
ASSERT_EQ(val, "33E9");
s = ".34e2=";
i = s.begin();
epee::misc_utils::parse::match_number(i, s.end(), val);
ASSERT_EQ(val, ".34e2");
s = "-9.34e-2=";
i = s.begin();
epee::misc_utils::parse::match_number(i, s.end(), val);
ASSERT_EQ(val, "-9.34e-2");
s = "+9.34e+03=";
i = s.begin();
epee::misc_utils::parse::match_number(i, s.end(), val);
ASSERT_EQ(val, "+9.34e+03");
} }
TEST(parsing, unicode) TEST(parsing, unicode)
@ -1818,13 +1794,44 @@ TEST(parsing, unicode)
std::string s; std::string s;
std::string::const_iterator si; std::string::const_iterator si;
s = "\"\""; si = s.begin(); ASSERT_TRUE(epee::misc_utils::parse::match_string(si, s.end(), bs)); ASSERT_EQ(bs, ""); s = "\"\"";
s = "\"\\u0000\""; si = s.begin(); ASSERT_TRUE(epee::misc_utils::parse::match_string(si, s.end(), bs)); ASSERT_EQ(bs, std::string(1, '\0')); si = s.begin();
s = "\"\\u0020\""; si = s.begin(); ASSERT_TRUE(epee::misc_utils::parse::match_string(si, s.end(), bs)); ASSERT_EQ(bs, " "); epee::misc_utils::parse::match_string2(si, s.end(), bs);
s = "\"\\u1\""; si = s.begin(); ASSERT_FALSE(epee::misc_utils::parse::match_string(si, s.end(), bs)); EXPECT_EQ(bs, "");
s = "\"\\u12\""; si = s.begin(); ASSERT_FALSE(epee::misc_utils::parse::match_string(si, s.end(), bs));
s = "\"\\u123\""; si = s.begin(); ASSERT_FALSE(epee::misc_utils::parse::match_string(si, s.end(), bs)); s = "\"\\u0000\"";
s = "\"\\u1234\""; si = s.begin(); ASSERT_TRUE(epee::misc_utils::parse::match_string(si, s.end(), bs)); ASSERT_EQ(bs, ""); si = s.begin();
s = "\"foo\\u1234bar\""; si = s.begin(); ASSERT_TRUE(epee::misc_utils::parse::match_string(si, s.end(), bs)); ASSERT_EQ(bs, "fooሴbar"); epee::misc_utils::parse::match_string2(si, s.end(), bs);
s = "\"\\u3042\\u307e\\u3084\\u304b\\u3059\""; si = s.begin(); ASSERT_TRUE(epee::misc_utils::parse::match_string(si, s.end(), bs)); ASSERT_EQ(bs, "あまやかす"); EXPECT_EQ(bs, std::string(1, '\0'));
s = "\"\\u0020\"";
si = s.begin();
epee::misc_utils::parse::match_string2(si, s.end(), bs);
EXPECT_EQ(bs, " ");
s = "\"\\u1\"";
si = s.begin();
EXPECT_THROW(epee::misc_utils::parse::match_string2(si, s.end(), bs), std::runtime_error);
s = "\"\\u12\"";
si = s.begin();
EXPECT_THROW(epee::misc_utils::parse::match_string2(si, s.end(), bs), std::runtime_error);
s = "\"\\u123\"";
si = s.begin();
EXPECT_THROW(epee::misc_utils::parse::match_string2(si, s.end(), bs), std::runtime_error);
s = "\"\\u1234\"";
si = s.begin();
epee::misc_utils::parse::match_string2(si, s.end(), bs);
EXPECT_EQ(bs, "");
s = "\"foo\\u1234bar\""; si = s.begin();
epee::misc_utils::parse::match_string2(si, s.end(), bs);
EXPECT_EQ(bs, "fooሴbar");
s = "\"\\u3042\\u307e\\u3084\\u304b\\u3059\"";
si = s.begin();
epee::misc_utils::parse::match_string2(si, s.end(), bs);
EXPECT_EQ(bs, "あまやかす");
} }