diff --git a/src/Table.cpp b/src/Table.cpp index 34773df9a..c0caba856 100644 --- a/src/Table.cpp +++ b/src/Table.cpp @@ -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 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 = ""; diff --git a/src/Table.h b/src/Table.h index 0b791d91b..01d8e3cf8 100644 --- a/src/Table.h +++ b/src/Table.h @@ -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&); diff --git a/src/tests/text.t.cpp b/src/tests/text.t.cpp index 90b33f588..3abb9c082 100644 --- a/src/tests/text.t.cpp +++ b/src/tests/text.t.cpp @@ -34,7 +34,7 @@ Context context; //////////////////////////////////////////////////////////////////////////////// int main (int argc, char** argv) { - UnitTest t (191); + UnitTest t (194); // void wrapText (std::vector & 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; } diff --git a/src/text.cpp b/src/text.cpp index 657db2ce6..9548e59ed 100644 --- a/src/text.cpp +++ b/src/text.cpp @@ -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; +} + +//////////////////////////////////////////////////////////////////////////////// diff --git a/src/text.h b/src/text.h index d17263b23..5356ef626 100644 --- a/src/text.h +++ b/src/text.h @@ -31,7 +31,7 @@ #include #include "../auto.h" -// text.cpp +// text.cpp, Non-UTF-8 aware. void wrapText (std::vector &, 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 ////////////////////////////////////////////////////////////////////////////////