From 869cdcc7a11d95f1578e35be104d46471e8d3617 Mon Sep 17 00:00:00 2001 From: Ryan Lucia Date: Sun, 29 Apr 2018 17:19:02 -0400 Subject: [PATCH] Properly ignore ASS whitespace characters in character counter --- libaegisub/common/character_count.cpp | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/libaegisub/common/character_count.cpp b/libaegisub/common/character_count.cpp index 1276f74fb..4563157a1 100644 --- a/libaegisub/common/character_count.cpp +++ b/libaegisub/common/character_count.cpp @@ -31,6 +31,8 @@ struct utext_deleter { }; using utext_ptr = std::unique_ptr; +UChar32 ass_special_chars[] = {'n', 'N', 'h'}; + icu::BreakIterator& get_break_iterator(const char *ptr, size_t len) { static std::unique_ptr bi; static std::once_flag token; @@ -65,10 +67,25 @@ size_t count_in_range(Iterator begin, Iterator end, int mask) { UChar32 c; int i = 0; U8_NEXT_UNSAFE(begin + pos, i, c); - if ((U_GET_GC_MASK(c) & mask) == 0) - ++count; + if ((U_GET_GC_MASK(c) & mask) == 0) { + if (mask & U_GC_Z_MASK && pos != 0) { + UChar32 *result = std::find(std::begin(ass_special_chars), std::end(ass_special_chars), c); + if (result != std::end(ass_special_chars)) { + UChar32 c2; + i = 0; + U8_PREV_UNSAFE(begin + pos, i, c2); + if (c2 != (UChar32) '\\') + ++count; + else if (!(mask & U_GC_P_MASK)) + --count; + } + else + ++count; + } + else + ++count; + } } - } return count; }