Enhancement

- Modified leftJustify and rightJustify to handle UTF8.
This commit is contained in:
Paul Beckingham 2011-04-29 01:10:58 -04:00
parent 937f2d9c8f
commit 66afc7c057
2 changed files with 5 additions and 3 deletions

View file

@ -728,7 +728,7 @@ std::string leftJustify (const int input, const int width)
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
std::string leftJustify (const std::string& input, const int width) std::string leftJustify (const std::string& input, const int width)
{ {
return input + std::string (width - input.length (), ' '); return input + std::string (width - characters (input), ' ');
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
@ -742,7 +742,7 @@ std::string rightJustify (const int input, const int width)
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
std::string rightJustify (const std::string& input, const int width) std::string rightJustify (const std::string& input, const int width)
{ {
return std::string (width - input.length (), ' ') + input; return std::string (width - characters (input), ' ') + input;
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////

View file

@ -34,7 +34,7 @@ Context context;
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
int main (int argc, char** argv) int main (int argc, char** argv)
{ {
UnitTest t (220); UnitTest t (222);
// void wrapText (std::vector <std::string>& lines, const std::string& text, const int width) // 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."; std::string text = "This is a test of the line wrapping code.";
@ -425,6 +425,7 @@ int main (int argc, char** argv)
t.is (leftJustify ("foo", 3), "foo", "leftJustify foo,3 -> 'foo'"); t.is (leftJustify ("foo", 3), "foo", "leftJustify foo,3 -> 'foo'");
t.is (leftJustify ("foo", 4), "foo ", "leftJustify foo,4 -> 'foo '"); t.is (leftJustify ("foo", 4), "foo ", "leftJustify foo,4 -> 'foo '");
t.is (leftJustify ("foo", 5), "foo ", "leftJustify foo,5 -> 'foo '"); t.is (leftJustify ("foo", 5), "foo ", "leftJustify foo,5 -> 'foo '");
t.is (leftJustify ("föo", 5), "föo ", "leftJustify föo,5 -> 'föo '");
// std::string rightJustify (const std::string&, const int); // std::string rightJustify (const std::string&, const int);
t.is (rightJustify (123, 3), "123", "rightJustify 123,3 -> '123'"); t.is (rightJustify (123, 3), "123", "rightJustify 123,3 -> '123'");
@ -435,6 +436,7 @@ int main (int argc, char** argv)
t.is (rightJustify ("foo", 3), "foo", "rightJustify foo,3 -> 'foo'"); t.is (rightJustify ("foo", 3), "foo", "rightJustify foo,3 -> 'foo'");
t.is (rightJustify ("foo", 4), " foo", "rightJustify foo,4 -> ' foo'"); t.is (rightJustify ("foo", 4), " foo", "rightJustify foo,4 -> ' foo'");
t.is (rightJustify ("foo", 5), " foo", "rightJustify foo,5 -> ' foo'"); t.is (rightJustify ("foo", 5), " foo", "rightJustify foo,5 -> ' foo'");
t.is (rightJustify ("föo", 5), " föo", "rightJustify föo,5 -> ' föo'");
return 0; return 0;
} }