Skip to content

Commit

Permalink
Correctly cast char before using isspace
Browse files Browse the repository at this point in the history
As specified in https://en.cppreference.com/w/cpp/string/byte/isspace
we must cast char to `unsigned char` before using isspace.

Else behaviour is undefined and it is an assert on Windows.
  • Loading branch information
mgautierfr committed Aug 15, 2024
1 parent 70e6703 commit de3401e
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/tools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,14 @@ uint32_t zim::countWords(const std::string& text)
unsigned int i = 0;

// Find first word
while ( i < length && std::isspace(text[i]) ) i++;
while ( i < length && std::isspace(static_cast<unsigned char>(text[i])) ) i++;

while ( i < length ) {
// Find end of word
while ( i < length && !std::isspace(text[i]) ) i++;
while ( i < length && !std::isspace(static_cast<unsigned char>(text[i])) ) i++;
numWords++;
// Find start of next word
while ( i < length && std::isspace(text[i]) ) i++;
while ( i < length && std::isspace(static_cast<unsigned char>(text[i])) ) i++;
}
return numWords;
}
Expand Down

0 comments on commit de3401e

Please sign in to comment.