Enhancement

- Added integer versions of leftJustify, rightJustify.
This commit is contained in:
Paul Beckingham 2011-04-29 01:01:08 -04:00
parent d0a91acf28
commit 937f2d9c8f
3 changed files with 31 additions and 1 deletions

View file

@ -28,6 +28,7 @@
#include <algorithm>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <string>
#include <strings.h>
@ -715,12 +716,29 @@ std::string format (double value, int width, int precision)
return s.str ();
}
////////////////////////////////////////////////////////////////////////////////
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 - input.length (), ' ');
}
////////////////////////////////////////////////////////////////////////////////
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)
{

View file

@ -66,7 +66,9 @@ std::string format (int);
std::string formatHex (int);
std::string format (float, int, int);
std::string format (double, int, int);
std::string leftJustify (const int, const int);
std::string leftJustify (const std::string&, const int);
std::string rightJustify (const int, const int);
std::string rightJustify (const std::string&, const int);
// UTF-8 aware.

View file

@ -34,7 +34,7 @@ Context context;
////////////////////////////////////////////////////////////////////////////////
int main (int argc, char** argv)
{
UnitTest t (214);
UnitTest t (220);
// void wrapText (std::vector <std::string>& lines, const std::string& text, const int width)
std::string text = "This is a test of the line wrapping code.";
@ -416,11 +416,21 @@ int main (int argc, char** argv)
// std::string format (double, int, int);
// std::string leftJustify (const std::string&, const int);
t.is (leftJustify (123, 3), "123", "leftJustify 123,3 -> '123'");
t.is (leftJustify (123, 4), "123 ", "leftJustify 123,4 -> '123 '");
t.is (leftJustify (123, 5), "123 ", "leftJustify 123,5 -> '123 '");
// std::string leftJustify (const std::string&, const int);
t.is (leftJustify ("foo", 3), "foo", "leftJustify foo,3 -> 'foo'");
t.is (leftJustify ("foo", 4), "foo ", "leftJustify foo,4 -> 'foo '");
t.is (leftJustify ("foo", 5), "foo ", "leftJustify foo,5 -> 'foo '");
// std::string rightJustify (const std::string&, const int);
t.is (rightJustify (123, 3), "123", "rightJustify 123,3 -> '123'");
t.is (rightJustify (123, 4), " 123", "rightJustify 123,4 -> ' 123'");
t.is (rightJustify (123, 5), " 123", "rightJustify 123,5 -> ' 123'");
// std::string rightJustify (const std::string&, const int);
t.is (rightJustify ("foo", 3), "foo", "rightJustify foo,3 -> 'foo'");
t.is (rightJustify ("foo", 4), " foo", "rightJustify foo,4 -> ' foo'");