Common: Added ::leftJustify and ::rightJustify variants

This commit is contained in:
Paul Beckingham 2016-01-05 16:45:52 -05:00
parent 33dce5de7b
commit 4a2ac6f4fc
2 changed files with 48 additions and 0 deletions

View file

@ -27,6 +27,7 @@
#include <cmake.h>
#include <text.h>
#include <utf8.h>
#include <sstream>
#include <iomanip>
#include <math.h>
@ -310,3 +311,44 @@ void replace_positional (
}
////////////////////////////////////////////////////////////////////////////////
std::string leftJustify (const int input, const int width)
{
std::stringstream s;
s << input;
std::string output = s.str ();
return output + std::string (width - output.length (), ' ');
}
////////////////////////////////////////////////////////////////////////////////
std::string leftJustify (const std::string& input, const int width)
{
return input + std::string (width - utf8_text_width (input), ' ');
}
////////////////////////////////////////////////////////////////////////////////
std::string rightJustifyZero (const int input, const int width)
{
std::stringstream s;
s << std::setw (width) << std::setfill ('0') << input;
return s.str ();
}
////////////////////////////////////////////////////////////////////////////////
std::string rightJustify (const int input, const int width)
{
std::stringstream s;
s << std::setw (width) << std::setfill (' ') << input;
return s.str ();
}
////////////////////////////////////////////////////////////////////////////////
std::string rightJustify (const std::string& input, const int width)
{
unsigned int len = utf8_text_width (input);
return (((unsigned int) width > len)
? std::string (width - len, ' ')
: "")
+ input;
}
////////////////////////////////////////////////////////////////////////////////

View file

@ -75,6 +75,12 @@ const std::string format (const std::string& fmt, Args... args)
return format (1, fmt, args...);
}
std::string leftJustify (const int, const int);
std::string leftJustify (const std::string&, const int);
std::string rightJustifyZero (const int, const int);
std::string rightJustify (const int, const int);
std::string rightJustify (const std::string&, const int);
// List operations.
template <class T> void listDiff (
const T& left, const T& right, T& leftOnly, T& rightOnly)