mirror of
https://github.com/ETLCPP/etl.git
synced 2026-04-30 19:09:10 +08:00
Added update_size() to string API to fix string lengths after insertion of characters by C code.
This commit is contained in:
parent
f145b53e80
commit
0ee3574a14
@ -2184,6 +2184,23 @@ namespace etl
|
||||
virtual void repair() = 0;
|
||||
#endif
|
||||
|
||||
//*********************************************************************
|
||||
/// Update the size to the distnace to the first null, or max size.
|
||||
//*********************************************************************
|
||||
void update_size()
|
||||
{
|
||||
p_buffer[max_size()] = 0; // Ensure a terminating null.
|
||||
|
||||
size_t i = 0U;
|
||||
|
||||
while ((i != max_size()) && (p_buffer[i] != 0))
|
||||
{
|
||||
++i;
|
||||
}
|
||||
|
||||
current_size = i;
|
||||
}
|
||||
|
||||
protected:
|
||||
|
||||
//*********************************************************************
|
||||
@ -2200,7 +2217,7 @@ namespace etl
|
||||
//*********************************************************************
|
||||
void initialise()
|
||||
{
|
||||
current_size = 0;
|
||||
current_size = 0U;
|
||||
cleanup();
|
||||
p_buffer[0] = 0;
|
||||
#if ETL_STRING_TRUNCATION_CHECKS_ENABLED
|
||||
|
||||
@ -4156,7 +4156,42 @@ namespace
|
||||
CHECK(text3.is_secure());
|
||||
CHECK(text4.is_secure());
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
//*************************************************************************
|
||||
TEST_FIXTURE(SetupFixture, test_update_after_c_string_max_size)
|
||||
{
|
||||
Text text;
|
||||
|
||||
text.resize(text.max_size());
|
||||
std::fill(text.data(), text.data() + text.max_size(), STR('A'));
|
||||
text.update_size();
|
||||
|
||||
CHECK_EQUAL(text.max_size(), text.size());
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST_FIXTURE(SetupFixture, test_update_after_c_string_shorter_size)
|
||||
{
|
||||
Text text;
|
||||
|
||||
text.resize(text.max_size());
|
||||
std::fill(text.data(), text.data() + text.max_size() - 1, STR('A'));
|
||||
text.update_size();
|
||||
|
||||
CHECK_EQUAL(text.max_size() - 1, text.size());
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST_FIXTURE(SetupFixture, test_update_after_c_string_greater_size)
|
||||
{
|
||||
Text text;
|
||||
|
||||
text.resize(text.max_size());
|
||||
std::fill(text.data(), text.data() + text.max_size() + 1, STR('A')); // Overwrites to terminating null.
|
||||
text.update_size();
|
||||
|
||||
CHECK_EQUAL(text.max_size(), text.size());
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@ -4519,5 +4519,44 @@ namespace
|
||||
CHECK(text4.is_secure());
|
||||
}
|
||||
#endif
|
||||
|
||||
//*************************************************************************
|
||||
TEST_FIXTURE(SetupFixture, test_update_after_c_string_max_size)
|
||||
{
|
||||
TextBuffer buffer;
|
||||
Text text(buffer.data(), buffer.size());
|
||||
|
||||
text.resize(text.max_size());
|
||||
std::fill(text.data(), text.data() + text.max_size(), STR('A'));
|
||||
text.update_size();
|
||||
|
||||
CHECK_EQUAL(text.max_size(), text.size());
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST_FIXTURE(SetupFixture, test_update_after_c_string_shorter_size)
|
||||
{
|
||||
TextBuffer buffer;
|
||||
Text text(buffer.data(), buffer.size());
|
||||
|
||||
text.resize(text.max_size());
|
||||
std::fill(text.data(), text.data() + text.max_size() - 1, STR('A'));
|
||||
text.update_size();
|
||||
|
||||
CHECK_EQUAL(text.max_size() - 1, text.size());
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST_FIXTURE(SetupFixture, test_update_after_c_string_greater_size)
|
||||
{
|
||||
TextBuffer buffer;
|
||||
Text text(buffer.data(), buffer.size());
|
||||
|
||||
text.resize(text.max_size());
|
||||
std::fill(text.data(), text.data() + text.max_size() + 1, STR('A')); // Overwrites to terminating null.
|
||||
text.update_size();
|
||||
|
||||
CHECK_EQUAL(text.max_size(), text.size());
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@ -4157,5 +4157,41 @@ namespace
|
||||
CHECK(text4.is_secure());
|
||||
}
|
||||
#endif
|
||||
|
||||
//*************************************************************************
|
||||
TEST_FIXTURE(SetupFixture, test_update_after_c_string_max_size)
|
||||
{
|
||||
Text text;
|
||||
|
||||
text.resize(text.max_size());
|
||||
std::fill(text.data(), text.data() + text.max_size(), STR('A'));
|
||||
text.update_size();
|
||||
|
||||
CHECK_EQUAL(text.max_size(), text.size());
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST_FIXTURE(SetupFixture, test_update_after_c_string_shorter_size)
|
||||
{
|
||||
Text text;
|
||||
|
||||
text.resize(text.max_size());
|
||||
std::fill(text.data(), text.data() + text.max_size() - 1, STR('A'));
|
||||
text.update_size();
|
||||
|
||||
CHECK_EQUAL(text.max_size() - 1, text.size());
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST_FIXTURE(SetupFixture, test_update_after_c_string_greater_size)
|
||||
{
|
||||
Text text;
|
||||
|
||||
text.resize(text.max_size());
|
||||
std::fill(text.data(), text.data() + text.max_size() + 1, STR('A')); // Overwrites to terminating null.
|
||||
text.update_size();
|
||||
|
||||
CHECK_EQUAL(text.max_size(), text.size());
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@ -4519,5 +4519,44 @@ namespace
|
||||
CHECK(text4.is_secure());
|
||||
}
|
||||
#endif
|
||||
|
||||
//*************************************************************************
|
||||
TEST_FIXTURE(SetupFixture, test_update_after_c_string_max_size)
|
||||
{
|
||||
TextBuffer buffer;
|
||||
Text text(buffer.data(), buffer.size());
|
||||
|
||||
text.resize(text.max_size());
|
||||
std::fill(text.data(), text.data() + text.max_size(), STR('A'));
|
||||
text.update_size();
|
||||
|
||||
CHECK_EQUAL(text.max_size(), text.size());
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST_FIXTURE(SetupFixture, test_update_after_c_string_shorter_size)
|
||||
{
|
||||
TextBuffer buffer;
|
||||
Text text(buffer.data(), buffer.size());
|
||||
|
||||
text.resize(text.max_size());
|
||||
std::fill(text.data(), text.data() + text.max_size() - 1, STR('A'));
|
||||
text.update_size();
|
||||
|
||||
CHECK_EQUAL(text.max_size() - 1, text.size());
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST_FIXTURE(SetupFixture, test_update_after_c_string_greater_size)
|
||||
{
|
||||
TextBuffer buffer;
|
||||
Text text(buffer.data(), buffer.size());
|
||||
|
||||
text.resize(text.max_size());
|
||||
std::fill(text.data(), text.data() + text.max_size() + 1, STR('A')); // Overwrites to terminating null.
|
||||
text.update_size();
|
||||
|
||||
CHECK_EQUAL(text.max_size(), text.size());
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@ -4157,5 +4157,41 @@ namespace
|
||||
CHECK(text4.is_secure());
|
||||
}
|
||||
#endif
|
||||
|
||||
//*************************************************************************
|
||||
TEST_FIXTURE(SetupFixture, test_update_after_c_string_max_size)
|
||||
{
|
||||
Text text;
|
||||
|
||||
text.resize(text.max_size());
|
||||
std::fill(text.data(), text.data() + text.max_size(), STR('A'));
|
||||
text.update_size();
|
||||
|
||||
CHECK_EQUAL(text.max_size(), text.size());
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST_FIXTURE(SetupFixture, test_update_after_c_string_shorter_size)
|
||||
{
|
||||
Text text;
|
||||
|
||||
text.resize(text.max_size());
|
||||
std::fill(text.data(), text.data() + text.max_size() - 1, STR('A'));
|
||||
text.update_size();
|
||||
|
||||
CHECK_EQUAL(text.max_size() - 1, text.size());
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST_FIXTURE(SetupFixture, test_update_after_c_string_greater_size)
|
||||
{
|
||||
Text text;
|
||||
|
||||
text.resize(text.max_size());
|
||||
std::fill(text.data(), text.data() + text.max_size() + 1, STR('A')); // Overwrites to terminating null.
|
||||
text.update_size();
|
||||
|
||||
CHECK_EQUAL(text.max_size(), text.size());
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@ -4502,5 +4502,44 @@ namespace
|
||||
CHECK(text4.is_secure());
|
||||
}
|
||||
#endif
|
||||
|
||||
//*************************************************************************
|
||||
TEST_FIXTURE(SetupFixture, test_update_after_c_string_max_size)
|
||||
{
|
||||
TextBuffer buffer;
|
||||
Text text(buffer.data(), buffer.size());
|
||||
|
||||
text.resize(text.max_size());
|
||||
std::fill(text.data(), text.data() + text.max_size(), STR('A'));
|
||||
text.update_size();
|
||||
|
||||
CHECK_EQUAL(text.max_size(), text.size());
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST_FIXTURE(SetupFixture, test_update_after_c_string_shorter_size)
|
||||
{
|
||||
TextBuffer buffer;
|
||||
Text text(buffer.data(), buffer.size());
|
||||
|
||||
text.resize(text.max_size());
|
||||
std::fill(text.data(), text.data() + text.max_size() - 1, STR('A'));
|
||||
text.update_size();
|
||||
|
||||
CHECK_EQUAL(text.max_size() - 1, text.size());
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST_FIXTURE(SetupFixture, test_update_after_c_string_greater_size)
|
||||
{
|
||||
TextBuffer buffer;
|
||||
Text text(buffer.data(), buffer.size());
|
||||
|
||||
text.resize(text.max_size());
|
||||
std::fill(text.data(), text.data() + text.max_size() + 1, STR('A')); // Overwrites to terminating null.
|
||||
text.update_size();
|
||||
|
||||
CHECK_EQUAL(text.max_size(), text.size());
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@ -4157,5 +4157,41 @@ namespace
|
||||
CHECK(text4.is_secure());
|
||||
}
|
||||
#endif
|
||||
|
||||
//*************************************************************************
|
||||
TEST_FIXTURE(SetupFixture, test_update_after_c_string_max_size)
|
||||
{
|
||||
Text text;
|
||||
|
||||
text.resize(text.max_size());
|
||||
std::fill(text.data(), text.data() + text.max_size(), STR('A'));
|
||||
text.update_size();
|
||||
|
||||
CHECK_EQUAL(text.max_size(), text.size());
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST_FIXTURE(SetupFixture, test_update_after_c_string_shorter_size)
|
||||
{
|
||||
Text text;
|
||||
|
||||
text.resize(text.max_size());
|
||||
std::fill(text.data(), text.data() + text.max_size() - 1, STR('A'));
|
||||
text.update_size();
|
||||
|
||||
CHECK_EQUAL(text.max_size() - 1, text.size());
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST_FIXTURE(SetupFixture, test_update_after_c_string_greater_size)
|
||||
{
|
||||
Text text;
|
||||
|
||||
text.resize(text.max_size());
|
||||
std::fill(text.data(), text.data() + text.max_size() + 1, STR('A')); // Overwrites to terminating null.
|
||||
text.update_size();
|
||||
|
||||
CHECK_EQUAL(text.max_size(), text.size());
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@ -4502,5 +4502,44 @@ namespace
|
||||
CHECK(text4.is_secure());
|
||||
}
|
||||
#endif
|
||||
|
||||
//*************************************************************************
|
||||
TEST_FIXTURE(SetupFixture, test_update_after_c_string_max_size)
|
||||
{
|
||||
TextBuffer buffer;
|
||||
Text text(buffer.data(), buffer.size());
|
||||
|
||||
text.resize(text.max_size());
|
||||
std::fill(text.data(), text.data() + text.max_size(), STR('A'));
|
||||
text.update_size();
|
||||
|
||||
CHECK_EQUAL(text.max_size(), text.size());
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST_FIXTURE(SetupFixture, test_update_after_c_string_shorter_size)
|
||||
{
|
||||
TextBuffer buffer;
|
||||
Text text(buffer.data(), buffer.size());
|
||||
|
||||
text.resize(text.max_size());
|
||||
std::fill(text.data(), text.data() + text.max_size() - 1, STR('A'));
|
||||
text.update_size();
|
||||
|
||||
CHECK_EQUAL(text.max_size() - 1, text.size());
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST_FIXTURE(SetupFixture, test_update_after_c_string_greater_size)
|
||||
{
|
||||
TextBuffer buffer;
|
||||
Text text(buffer.data(), buffer.size());
|
||||
|
||||
text.resize(text.max_size());
|
||||
std::fill(text.data(), text.data() + text.max_size() + 1, STR('A')); // Overwrites to terminating null.
|
||||
text.update_size();
|
||||
|
||||
CHECK_EQUAL(text.max_size(), text.size());
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user