Code Cleanup

- Relocated the Table::getCharLength() from Johannes to text.cpp/characters()
  because it is a general-purpose function, and will be the start of the UTF8
  conversion of all text.cpp code.
- Added unit tests for characters().
This commit is contained in:
Paul Beckingham 2010-08-01 13:05:53 -04:00
parent 6e1aa42d1a
commit 2f1c582d7d
5 changed files with 35 additions and 28 deletions

View file

@ -108,7 +108,7 @@ void Table::setTableDashedUnderline ()
int Table::addColumn (const std::string& col)
{
mSpecifiedWidth.push_back (minimum);
mMaxDataWidth.push_back (col == "" ? 1 : getCharLength (col));
mMaxDataWidth.push_back (col == "" ? 1 : characters (col));
mCalculatedWidth.push_back (0);
mColumnPadding.push_back (0);
@ -193,11 +193,11 @@ void Table::addCell (const int row, const int col, const std::string& data)
std::vector <std::string> lines;
split (lines, data, "\n");
for (unsigned int i = 0; i < lines.size (); ++i)
if (getCharLength (lines[i]) > length)
length = getCharLength (lines[i]);
if (characters (lines[i]) > length)
length = characters (lines[i]);
}
else
length = getCharLength (data);
length = characters (data);
// Automatically maintain max width.
mMaxDataWidth[col] = max (mMaxDataWidth[col], length);
@ -435,25 +435,6 @@ Table::just Table::getHeaderJustification (int col)
: left;
}
////////////////////////////////////////////////////////////////////////////////
int Table::getCharLength (const std::string& str)
{
int byteLength = str.length ();
int charLength = byteLength;
const char* data = str.data ();
// decrement the number of bytes for each byte that matches 0b10??????
// this way only the first byte of any utf8 sequence is counted
for (int i = 0; i < byteLength; i++)
{
// extract the two MSB and check whether they are 10
if ((data[i] & 0xC0) == 0x80)
charLength--;
}
return charLength;
}
////////////////////////////////////////////////////////////////////////////////
// data One Data to be rendered
// width 8 Max data width for column/specified width
@ -478,7 +459,7 @@ const std::string Table::formatHeader (
std::string data = mColumns[col];
Color c = getHeaderUnderline (col);
int gap = width - strippedLength (data); // TODO Does this need Table::getCharLength too?
int gap = width - strippedLength (data); // TODO Does this need characters () too?
std::string pad = std::string (padding, ' ');
@ -561,7 +542,7 @@ void Table::formatCell (
for (size_t chunk = 0; chunk < chunks.size (); ++chunk)
{
// Place the data within the available space - justify.
int gap = width - getCharLength (chunks[chunk]);
int gap = width - characters (chunks[chunk]);
preJust = "";
postJust = "";

View file

@ -100,7 +100,6 @@ private:
void calculateColumnWidths ();
just getJustification (const int, const int);
just getHeaderJustification (const int);
int getCharLength (const std::string&);
const std::string formatHeader (const int, const int, const int);
const std::string formatHeaderDashedUnderline (const int, const int, const int);
void formatCell (const int, const int, const int, const int, const int, std::vector <std::string>&, std::string&);

View file

@ -34,7 +34,7 @@ Context context;
////////////////////////////////////////////////////////////////////////////////
int main (int argc, char** argv)
{
UnitTest t (191);
UnitTest t (194);
// 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.";
@ -387,6 +387,11 @@ int main (int argc, char** argv)
t.is (strippedLength (std::string ("\033[0m")), 0, "strippedLength ^[[0m -> 0");
t.is (strippedLength (std::string ("\033[1m\033[0m")), 0, "strippedLength ^[[1m^[[0m -> 0");
// int characters (const std::string&);
t.is (characters ("Çirçös"), 6, "characters (Çirçös) == 6");
t.is (characters ("ツネナラム"), 5, "characters (ツネナラム) == 6");
t.is (characters ("Zwölf Boxkämpfer"), 16, "characters (Zwölf Boxkämpfer) == 6");
return 0;
}

View file

@ -620,3 +620,22 @@ int strippedLength (const std::string& input)
}
////////////////////////////////////////////////////////////////////////////////
int characters (const std::string& str)
{
int byteLength = str.length ();
int charLength = byteLength;
const char* data = str.data ();
// decrement the number of bytes for each byte that matches 0b10??????
// this way only the first byte of any utf8 sequence is counted
for (int i = 0; i < byteLength; i++)
{
// extract the two MSB and check whether they are 10
if ((data[i] & 0xC0) == 0x80)
charLength--;
}
return charLength;
}
////////////////////////////////////////////////////////////////////////////////

View file

@ -31,7 +31,7 @@
#include <vector>
#include "../auto.h"
// text.cpp
// text.cpp, Non-UTF-8 aware.
void wrapText (std::vector <std::string>&, const std::string&, const int);
std::string trimLeft (const std::string& in, const std::string& t = " ");
std::string trimRight (const std::string& in, const std::string& t = " ");
@ -60,5 +60,8 @@ std::string::size_type find (const std::string&, const std::string&, bool sensit
std::string::size_type find (const std::string&, const std::string&, std::string::size_type, bool sensitive = true);
int strippedLength (const std::string&);
// UTF-8 aware.
int characters (const std::string&);
#endif
////////////////////////////////////////////////////////////////////////////////