1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 11:28:12 +00:00

LibGUI: Add number_of_words() to TextEditors

Returns the total number of words in a document.
This commit is contained in:
thankyouverycool 2021-09-18 16:31:47 -04:00 committed by Andreas Kling
parent aa06012249
commit 46043b71cb
2 changed files with 24 additions and 0 deletions

View file

@ -1335,6 +1335,29 @@ size_t TextEditor::number_of_selected_words() const
return word_count;
}
size_t TextEditor::number_of_words() const
{
if (document().is_empty())
return 0;
size_t word_count = 0;
bool in_word = false;
auto text = this->text();
for (char c : text) {
if (in_word && is_ascii_space(c)) {
in_word = false;
word_count++;
continue;
}
if (!in_word && !is_ascii_space(c))
in_word = true;
}
if (in_word)
word_count++;
return word_count;
}
void TextEditor::delete_selection()
{
auto selection = normalized_selection();