Merge topic 'isspace'

d9d9326e14 Source: Avoid out-of-range inputs to std::isspace()
14abdc8e2b cmXMLParser: Remove unused IsSpace method

Acked-by: Kitware Robot <kwrobot@kitware.com>
Acked-by: buildbot <buildbot@kitware.com>
Merge-request: !9162
diff --git a/Source/CTest/cmCTestGIT.cxx b/Source/CTest/cmCTestGIT.cxx
index 984c837..99c5a2b 100644
--- a/Source/CTest/cmCTestGIT.cxx
+++ b/Source/CTest/cmCTestGIT.cxx
@@ -2,7 +2,6 @@
    file Copyright.txt or https://cmake.org/licensing for details.  */
 #include "cmCTestGIT.h"
 
-#include <cctype>
 #include <cstdio>
 #include <cstdlib>
 #include <ctime>
@@ -414,14 +413,14 @@
 
   const char* ConsumeSpace(const char* c)
   {
-    while (*c && isspace(*c)) {
+    while (*c && cmIsSpace(*c)) {
       ++c;
     }
     return c;
   }
   const char* ConsumeField(const char* c)
   {
-    while (*c && !isspace(*c)) {
+    while (*c && !cmIsSpace(*c)) {
       ++c;
     }
     return c;
@@ -481,7 +480,7 @@
   {
     // Person Name <person@domain.com> 1234567890 +0000
     const char* c = str;
-    while (*c && isspace(*c)) {
+    while (*c && cmIsSpace(*c)) {
       ++c;
     }
 
@@ -490,7 +489,7 @@
       ++c;
     }
     const char* name_last = c;
-    while (name_last != name_first && isspace(*(name_last - 1))) {
+    while (name_last != name_first && cmIsSpace(*(name_last - 1))) {
       --name_last;
     }
     person.Name.assign(name_first, name_last - name_first);
diff --git a/Source/cmCMakeHostSystemInformationCommand.cxx b/Source/cmCMakeHostSystemInformationCommand.cxx
index 699e23b..e4160a1 100644
--- a/Source/cmCMakeHostSystemInformationCommand.cxx
+++ b/Source/cmCMakeHostSystemInformationCommand.cxx
@@ -178,7 +178,7 @@
         if (std::isalpha(ch) || ch == '_') {
           key += ch;
           state = PARSE_KEY;
-        } else if (!std::isspace(ch)) {
+        } else if (!cmIsSpace(ch)) {
           state = IGNORE_REST;
         }
         break;
@@ -238,7 +238,7 @@
         break;
 
       case PARSE_VALUE:
-        if (ch == '#' || std::isspace(ch)) {
+        if (ch == '#' || cmIsSpace(ch)) {
           state = IGNORE_REST;
         } else {
           value += ch;
diff --git a/Source/cmExecuteProcessCommand.cxx b/Source/cmExecuteProcessCommand.cxx
index 483a601..da6def9 100644
--- a/Source/cmExecuteProcessCommand.cxx
+++ b/Source/cmExecuteProcessCommand.cxx
@@ -2,7 +2,6 @@
    file Copyright.txt or https://cmake.org/licensing for details.  */
 #include "cmExecuteProcessCommand.h"
 
-#include <cctype> /* isspace */
 #include <cstdint>
 #include <cstdio>
 #include <iostream>
@@ -35,11 +34,7 @@
 namespace {
 bool cmExecuteProcessCommandIsWhitespace(char c)
 {
-  // isspace takes 'int' but documents that the value must be representable
-  // by 'unsigned char', or EOF.  Cast to 'unsigned char' to avoid sign
-  // extension while casting to 'int'.
-  return (isspace(static_cast<int>(static_cast<unsigned char>(c))) ||
-          c == '\n' || c == '\r');
+  return (cmIsSpace(c) || c == '\n' || c == '\r');
 }
 
 void cmExecuteProcessCommandFixText(std::vector<char>& output,
diff --git a/Source/cmMakefile.cxx b/Source/cmMakefile.cxx
index 6fb7734..2687afa 100644
--- a/Source/cmMakefile.cxx
+++ b/Source/cmMakefile.cxx
@@ -1438,8 +1438,8 @@
   for (std::string::size_type lpos = dflags.find(flag, 0);
        lpos != std::string::npos; lpos = dflags.find(flag, lpos)) {
     std::string::size_type rpos = lpos + len;
-    if ((lpos <= 0 || isspace(dflags[lpos - 1])) &&
-        (rpos >= dflags.size() || isspace(dflags[rpos]))) {
+    if ((lpos <= 0 || cmIsSpace(dflags[lpos - 1])) &&
+        (rpos >= dflags.size() || cmIsSpace(dflags[rpos]))) {
       dflags.erase(lpos, len);
     } else {
       ++lpos;
diff --git a/Source/cmRST.cxx b/Source/cmRST.cxx
index f48330d..3934a29 100644
--- a/Source/cmRST.cxx
+++ b/Source/cmRST.cxx
@@ -3,7 +3,6 @@
 #include "cmRST.h"
 
 #include <algorithm>
-#include <cctype>
 #include <cstddef>
 #include <iterator>
 #include <utility>
@@ -159,7 +158,7 @@
   // A line starting in .. is an explicit markup start.
   if (line == ".." ||
       (line.size() >= 3 && line[0] == '.' && line[1] == '.' &&
-       isspace(line[2]))) {
+       cmIsSpace(line[2]))) {
     this->Reset();
     this->MarkupType =
       (line.find_first_not_of(" \t", 2) == std::string::npos ? Markup::Empty
@@ -219,7 +218,7 @@
   }
   // Indented lines following an explicit markup start are explicit markup.
   else if (this->MarkupType != Markup::None &&
-           (line.empty() || isspace(line[0]))) {
+           (line.empty() || cmIsSpace(line[0]))) {
     this->MarkupType = Markup::Normal;
     // Record markup lines if the start line was recorded.
     if (!this->MarkupLines.empty()) {
diff --git a/Source/cmStringAlgorithms.h b/Source/cmStringAlgorithms.h
index 4a9840b..55a1e46 100644
--- a/Source/cmStringAlgorithms.h
+++ b/Source/cmStringAlgorithms.h
@@ -44,7 +44,10 @@
 /** Returns true if the character @a ch is a whitespace character.  **/
 inline bool cmIsSpace(char ch)
 {
-  return ((ch & 0x80) == 0) && std::isspace(ch);
+  // isspace takes 'int' but documents that the value must be representable
+  // by 'unsigned char', or be EOF.  Cast to 'unsigned char' to avoid sign
+  // extension while converting to 'int'.
+  return std::isspace(static_cast<unsigned char>(ch));
 }
 
 /** Returns a string that has whitespace removed from the start and the end. */
diff --git a/Source/cmStringCommand.cxx b/Source/cmStringCommand.cxx
index 5a64588..ab87f34 100644
--- a/Source/cmStringCommand.cxx
+++ b/Source/cmStringCommand.cxx
@@ -6,7 +6,6 @@
 #include "cmStringCommand.h"
 
 #include <algorithm>
-#include <cctype>
 #include <cstdio>
 #include <cstdlib>
 #include <limits>
@@ -660,7 +659,7 @@
   const char* ptr = stringValue.c_str();
   size_t cc;
   for (cc = 0; cc < inStringLength; ++cc) {
-    if (!isspace(*ptr)) {
+    if (!cmIsSpace(*ptr)) {
       if (startPos > inStringLength) {
         startPos = cc;
       }
diff --git a/Source/cmSystemTools.cxx b/Source/cmSystemTools.cxx
index 1b3dbe2..fca8186 100644
--- a/Source/cmSystemTools.cxx
+++ b/Source/cmSystemTools.cxx
@@ -486,7 +486,7 @@
   const char* c = command.c_str();
 
   // Skip leading whitespace.
-  while (isspace(static_cast<unsigned char>(*c))) {
+  while (cmIsSpace(*c)) {
     ++c;
   }
 
@@ -516,7 +516,7 @@
       in_double = true;
     } else if (*c == '\'') {
       in_single = true;
-    } else if (isspace(static_cast<unsigned char>(*c))) {
+    } else if (cmIsSpace(*c)) {
       break;
     } else {
       program += *c;
diff --git a/Source/cmXMLParser.cxx b/Source/cmXMLParser.cxx
index 24da8c6..f4433e3 100644
--- a/Source/cmXMLParser.cxx
+++ b/Source/cmXMLParser.cxx
@@ -2,7 +2,6 @@
    file Copyright.txt or https://cmake.org/licensing for details.  */
 #include "cmXMLParser.h"
 
-#include <cctype>
 #include <cstring>
 #include <iostream>
 #include <sstream>
@@ -143,11 +142,6 @@
 {
 }
 
-int cmXMLParser::IsSpace(char c)
-{
-  return isspace(c);
-}
-
 const char* cmXMLParser::FindAttribute(const char** atts,
                                        const char* attribute)
 {
diff --git a/Source/cmXMLParser.h b/Source/cmXMLParser.h
index 176252d..d35e44f 100644
--- a/Source/cmXMLParser.h
+++ b/Source/cmXMLParser.h
@@ -89,10 +89,6 @@
   /** Called by ReportXmlParseError with basic error info.  */
   virtual void ReportError(int line, int column, const char* msg);
 
-  //! Utility for convenience of subclasses.  Wraps isspace C library
-  // routine.
-  static int IsSpace(char c);
-
   //! Send the given buffer to the XML parser.
   virtual int ParseBuffer(const char* buffer, std::string::size_type length);