From 937f2d9c8f9d681c900be2b3b082a642c56ead31 Mon Sep 17 00:00:00 2001 From: Paul Beckingham Date: Fri, 29 Apr 2011 01:01:08 -0400 Subject: [PATCH] Enhancement - Added integer versions of leftJustify, rightJustify. --- src/text.cpp | 18 ++++++++++++++++++ src/text.h | 2 ++ test/text.t.cpp | 12 +++++++++++- 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/src/text.cpp b/src/text.cpp index de6d4efb9..2ab433208 100644 --- a/src/text.cpp +++ b/src/text.cpp @@ -28,6 +28,7 @@ #include #include #include +#include #include #include #include @@ -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) { diff --git a/src/text.h b/src/text.h index 20a287a88..882a8b2e8 100644 --- a/src/text.h +++ b/src/text.h @@ -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. diff --git a/test/text.t.cpp b/test/text.t.cpp index e6e3c33c2..896caa2e2 100644 --- a/test/text.t.cpp +++ b/test/text.t.cpp @@ -34,7 +34,7 @@ Context context; //////////////////////////////////////////////////////////////////////////////// int main (int argc, char** argv) { - UnitTest t (214); + UnitTest t (220); // void wrapText (std::vector & 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'");