Skip to content
Permalink
Browse files

Make wrap_rows not wrap inside utf-8 multibyte sequences

Also count multibyte sequences as "one" character.
Adds unittest for the bug reporter's case.
Fixes #2796.
  • Loading branch information...
est31 committed Jun 17, 2015
1 parent 6dcf549 commit 45a9bb310ee0584cacb9993ed9805da5006c07dc
Showing with 21 additions and 2 deletions.
  1. +8 −0 src/unittest/test_utilities.cpp
  2. +13 −2 src/util/string.h
@@ -242,6 +242,14 @@ void TestUtilities::testUTF8()
void TestUtilities::testWrapRows()
{
UASSERT(wrap_rows("12345678",4) == "1234\n5678");
// test that wrap_rows doesn't wrap inside multibyte sequences
const unsigned char s[] = {
0x2f, 0x68, 0x6f, 0x6d, 0x65, 0x2f, 0x72, 0x61, 0x70, 0x74, 0x6f,
0x72, 0x2f, 0xd1, 0x82, 0xd0, 0xb5, 0xd1, 0x81, 0xd1, 0x82, 0x2f,
0x6d, 0x69, 0x6e, 0x65, 0x74, 0x65, 0x73, 0x74, 0x2f, 0x62, 0x69,
0x6e, 0x2f, 0x2e, 0x2e, 0};
std::string str((char *)s);
UASSERT(utf8_to_wide(wrap_rows(str, 20)) != L"<invalid UTF-8 string>");
}


@@ -32,6 +32,11 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#define STRINGIFY(x) #x
#define TOSTRING(x) STRINGIFY(x)

// Checks whether a byte is an inner byte for an utf-8 multibyte sequence
#define IS_UTF8_MULTB_INNER(x) (((unsigned char)x >= 0x80) && ((unsigned char)x <= 0xc0))
// Checks whether a byte is a start byte for an utf-8 multibyte sequence
#define IS_UTF8_MULTB_START(x) ((unsigned char)x >= 0xc0)

typedef std::map<std::string, std::string> StringMap;

struct FlagDesc {
@@ -411,7 +416,10 @@ inline bool string_allowed_blacklist(const std::string &str,
* every \p row_len characters whether it breaks a word or not. It is
* intended to be used for, for example, showing paths in the GUI.
*
* @param from The string to be wrapped into rows.
* @note This function doesn't wrap inside utf-8 multibyte sequences and also
* counts multibyte sequences correcly as single characters.
*
* @param from The (utf-8) string to be wrapped into rows.
* @param row_len The row length (in characters).
* @return A new string with the wrapping applied.
*/
@@ -420,9 +428,12 @@ inline std::string wrap_rows(const std::string &from,
{
std::string to;

size_t character_idx = 0;
for (size_t i = 0; i < from.size(); i++) {
if (i != 0 && i % row_len == 0)
if (character_idx > 0 && character_idx % row_len == 0)
to += '\n';
if (!IS_UTF8_MULTB_INNER(from[i]))
character_idx++;
to += from[i];
}

0 comments on commit 45a9bb3

Please sign in to comment.
You can’t perform that action at this time.