diff options
author | figec <raptor.ext@gmail.com> | 2015-06-18 21:34:17 +0300 |
---|---|---|
committer | est31 <MTest31@outlook.com> | 2015-06-18 23:39:23 +0200 |
commit | 3b65a6a36c3e910359c69cd3e3e3fd89e50ba23e (patch) | |
tree | bda855f40761027cff71191505df2131a0c8f63f /src/util/string.h | |
parent | e45ecad3ab401e169d1c8c9ba9448360b215b3e5 (diff) | |
download | minetest-3b65a6a36c3e910359c69cd3e3e3fd89e50ba23e.tar.gz minetest-3b65a6a36c3e910359c69cd3e3e3fd89e50ba23e.tar.bz2 minetest-3b65a6a36c3e910359c69cd3e3e3fd89e50ba23e.zip |
Fix wrap_rows at inner byte of multibyte sequence
Also fix UTF-8 inner byte bounds and make unittest for case this fixes.
Diffstat (limited to 'src/util/string.h')
-rw-r--r-- | src/util/string.h | 14 |
1 files changed, 11 insertions, 3 deletions
diff --git a/src/util/string.h b/src/util/string.h index 72d3c6075..b4ce5743d 100644 --- a/src/util/string.h +++ b/src/util/string.h @@ -33,7 +33,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #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)) +#define IS_UTF8_MULTB_INNER(x) (((unsigned char)x >= 0x80) && ((unsigned char)x < 0xc0)) typedef std::map<std::string, std::string> StringMap; @@ -426,12 +426,20 @@ inline std::string wrap_rows(const std::string &from, { std::string to; + bool need_to_wrap = false; + size_t character_idx = 0; for (size_t i = 0; i < from.size(); i++) { if (character_idx > 0 && character_idx % row_len == 0) - to += '\n'; - if (!IS_UTF8_MULTB_INNER(from[i])) + need_to_wrap = true; + if (!IS_UTF8_MULTB_INNER(from[i])) { + // Wrap string if needed before next char started + if (need_to_wrap) { + to += '\n'; + need_to_wrap = false; + } character_idx++; + } to += from[i]; } |