Commit 051d19f1 authored by Purves, Murray's avatar Purves, Murray
Browse files

Adding make_length function to stringfunctions

parent 6dee28d8
Pipeline #16160 failed with stages
in 8 minutes and 25 seconds
......@@ -23,7 +23,7 @@ std::string trim_string(const std::string &indata)
{
std::string data(indata);
data.erase(0, data.find_first_not_of(" \t\n\r")); // prefixing spaces
data.erase(data.find_last_not_of(" \t\n\r") + 1); // surfixing spaces
data.erase(data.find_last_not_of(" \t\n\r") + 1); // suffixing spaces
return data;
} // trim_string
......@@ -151,4 +151,30 @@ struct std::tm utc_to_time(const std::string &time, int &zone, int &daylight)
int ordinal(char c) { return (unsigned char)c; }
std::string make_length(const std::string &string, const int length)
{
std::string output;
if (string.length() == length)
{
output = string;
}
else if (string.length() > length)
{
output = string.substr(0, length);
}
else
{
std::stringstream ss;
ss << string;
for (int i = string.length(); i < length; ++i)
{
ss << " ";
}
output = ss.str();
}
return output;
}
} // namespace radix
......@@ -107,6 +107,17 @@ std::string RADIX_PUBLIC remove_substring(const char *msg, const char *sub);
*/
std::string RADIX_PUBLIC remove_extra_whitespace(const std::string &str);
/**
* @brief make_length Force a string to be of a specified length. If string is
* longer than length, returned string is trimmed to length. If string is
* shorter than length, whitespace is added.
* @param string The string to make a specific length
* @param length The required length of the returned string
* @return A string based on the input string, with the specified length
*/
std::string RADIX_PUBLIC make_length(const std::string &string,
const int length);
} // namespace radix
//! Templated functions
......
......@@ -151,3 +151,16 @@ TEST(Radix, OrdinalItoa)
EXPECT_EQ(140, i);
}
}
TEST(StringFunctions, MakeLength)
{
std::string shortString = "this string is too short";
std::string testString = make_length(shortString, 30);
EXPECT_EQ(30, testString.length());
EXPECT_STREQ("this string is too short ", testString.c_str());
std::string longString = "this string is too long";
testString = make_length(longString, 15);
EXPECT_EQ(15, testString.length());
EXPECT_STREQ("this string is ", testString.c_str());
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment