Skip to content
Snippets Groups Projects
Commit 1b6ae54b authored by Eric Cano's avatar Eric Cano
Browse files

Added utils functions allowing ellipsing strings when too long

The targetted use case is truncation of long log parameters.
parent cf227d94
No related branches found
No related tags found
No related merge requests found
......@@ -648,4 +648,14 @@ TEST_F(cta_UtilsTest, testCopyString) {
ASSERT_EQ(0, strcmp(dst, src));
}
TEST_F(cta_UtilsTest, ellipses) {
using namespace cta::utils;
ASSERT_EQ("1234567890", postEllipsis("1234567890", 12));
ASSERT_EQ("1234567[...]", postEllipsis("1234567890ABCDEF", 12));
ASSERT_EQ("1234567890", midEllipsis("1234567890", 12));
ASSERT_EQ("123[...]CDEF", midEllipsis("1234567890ABCDEF", 12));
ASSERT_EQ("1[...]ABCDEF", midEllipsis("1234567890ABCDEF", 12, 1));
}
} // namespace unitTests
......@@ -259,6 +259,32 @@ std::string trimString(const std::string &s) throw() {
return std::string(it1, it2);
}
//------------------------------------------------------------------------------
// postEllipsis
//------------------------------------------------------------------------------
std::string postEllipsis(const std::string &s, size_t maxSize) {
std::string ellipsis = "[...]";
if (maxSize < ellipsis.size())
throw cta::exception::Exception("In cta::utils::postEllipsis(): maxSize cannot be smaller than ellipsis size");
if (s.size() <= maxSize)
return s;
return s.substr(0, maxSize - ellipsis.size()) + ellipsis;
}
//------------------------------------------------------------------------------
// midEllipsis
//------------------------------------------------------------------------------
std::string midEllipsis(const std::string &s, size_t maxSize, size_t beginingSize) {
std::string ellipsis = "[...]";
if (maxSize < ellipsis.size() + beginingSize)
throw cta::exception::Exception("In cta::utils::midEllipsis(): maxSize cannot be smaller than ellipsis size + beginingSize");
if (s.size() <= maxSize)
return s;
if (!beginingSize)
beginingSize = (maxSize - ellipsis.size()) / 2;
return s.substr(0, beginingSize) + ellipsis + s.substr(s.size() - maxSize + ellipsis.size() + beginingSize);
}
//------------------------------------------------------------------------------
// singleSpaceString
//------------------------------------------------------------------------------
......
......@@ -109,8 +109,31 @@ namespace utils {
* @param s The string to be trimmed.
* @return The result of trimming the string.
*/
std::string trimString(const std::string &s) throw();
std::string trimString(const std::string &s) noexcept;
/**
* Returns a string with an ellipsis in the end if necessary so that the
* string plus ellipsis does not exceed the maxSize. The returned string is
* identical to s if it fits the maximum size.
* @param s the string
* @param maxSize
* @return the ellipsed string
*/
std::string postEllipsis(const std::string &s, size_t maxSize);
/**
* Returns a string with an ellipsis in the middle if necessary so that the
* string plus ellipsis does not exceed the maxSize. The returned string is
* identical to s if it fits the maximum size. The parts before and after the
* ellipsis are evenly distributed, unless beginingSize is set to a non zero
* value.
* @param s the string
* @param maxSize
* @param beginingSize
* @return the ellipsed string
*/
std::string midEllipsis(const std::string &s, size_t maxSize, size_t beginingSize = 0);
/**
* Returns uuid in the form of a string.
*
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment