From 922f2d613ff53d6de09e1756525226de06db6a9c Mon Sep 17 00:00:00 2001 From: Thomas ten Cate Date: Thu, 15 Nov 2018 12:34:08 +0100 Subject: [PATCH] Fix return value of get_total_character_count Also document that it only counts visible characters. Fixes #23720 --- doc/classes/Label.xml | 2 +- scene/gui/label.cpp | 5 ++--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/doc/classes/Label.xml b/doc/classes/Label.xml index 1e78a196b18..90547b7c2f4 100644 --- a/doc/classes/Label.xml +++ b/doc/classes/Label.xml @@ -30,7 +30,7 @@ - Returns the total length of the text. + Returns the total number of printable characters in the text (excluding spaces and newlines). diff --git a/scene/gui/label.cpp b/scene/gui/label.cpp index 8009a96a4f5..a7f88514e02 100644 --- a/scene/gui/label.cpp +++ b/scene/gui/label.cpp @@ -393,7 +393,7 @@ void Label::regenerate_word_cache() { WordCache *last = NULL; - for (int i = 0; i < xl_text.size() + 1; i++) { + for (int i = 0; i <= xl_text.length(); i++) { CharType current = i < xl_text.length() ? xl_text[i] : ' '; //always a space at the end, so the algo works @@ -429,12 +429,11 @@ void Label::regenerate_word_cache() { if (current == '\n') { insert_newline = true; - } else { + } else if (current != ' ') { total_char_cache++; } if (i < xl_text.length() && xl_text[i] == ' ') { - total_char_cache--; // do not count spaces if (line_width > 0 || last == NULL || last->char_pos != WordCache::CHAR_WRAPLINE) { space_count++; line_width += space_width;