Correct is_space fix for Windows compatibility

Signed-off-by: Alexander Borsuk <me@alex.bio>
This commit is contained in:
Alexander Borsuk
2025-08-13 17:19:28 +03:00
committed by Konstantin Pastbin
parent 826b56cabc
commit 55dc1e17e6
7 changed files with 28 additions and 17 deletions

View File

@@ -173,7 +173,7 @@ bool ParseMaxspeedTag(std::string const & maxspeedValue, routing::SpeedInUnits &
speedStr += maxspeedValue[i]; speedStr += maxspeedValue[i];
} }
while (i < maxspeedValue.size() && isspace(maxspeedValue[i])) while (i < maxspeedValue.size() && strings::IsASCIISpace(maxspeedValue[i]))
++i; ++i;
if (maxspeedValue.size() == i || maxspeedValue.substr(i).starts_with("kmh")) if (maxspeedValue.size() == i || maxspeedValue.substr(i).starts_with("kmh"))

View File

@@ -11,6 +11,7 @@
#include <fast_double_parser.h> #include <fast_double_parser.h>
#include <boost/algorithm/string/trim.hpp> #include <boost/algorithm/string/trim.hpp>
#include <string>
namespace strings namespace strings
{ {
@@ -227,15 +228,15 @@ void AsciiToUpper(std::string & s)
void Trim(std::string & s) void Trim(std::string & s)
{ {
boost::trim_if(s, ::isspace); boost::trim_if(s, IsASCIISpace<std::string::value_type>);
} }
void Trim(std::string_view & sv) void Trim(std::string_view & sv)
{ {
auto const beg = std::find_if(sv.cbegin(), sv.cend(), [](auto c) { return !std::isspace(c); }); auto const beg = std::find_if(sv.cbegin(), sv.cend(), [](auto c) { return !IsASCIISpace(c); });
if (beg != sv.end()) if (beg != sv.end())
{ {
auto const end = std::find_if(sv.crbegin(), sv.crend(), [](auto c) { return !std::isspace(c); }).base(); auto const end = std::find_if(sv.crbegin(), sv.crend(), [](auto c) { return !IsASCIISpace(c); }).base();
sv = std::string_view(sv.data() + std::distance(sv.begin(), beg), std::distance(beg, end)); sv = std::string_view(sv.data() + std::distance(sv.begin(), beg), std::distance(beg, end));
} }
else else
@@ -355,11 +356,6 @@ bool IsASCIIDigit(UniChar c)
return c >= '0' && c <= '9'; return c >= '0' && c <= '9';
} }
bool IsASCIISpace(UniChar c)
{
return c == ' ' || c == '\f' || c == '\n' || c == '\r' || c == '\t' || c == '\v';
}
bool IsASCIILatin(UniChar c) bool IsASCIILatin(UniChar c)
{ {
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'); return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');

View File

@@ -105,9 +105,7 @@ size_t CountNormLowerSymbols(UniString const & s, UniString const & lowStr);
void AsciiToLower(std::string & s); void AsciiToLower(std::string & s);
void AsciiToUpper(std::string & s); void AsciiToUpper(std::string & s);
// All triming functions return a reference on an input string. // All trimming functions do in-place trimming. Only ASCII whitespaces are trimmed.
// They do in-place trimming. In general, it does not work for any unicode whitespace except
// ASCII U+0020 one.
void Trim(std::string & s); void Trim(std::string & s);
void Trim(std::string_view & sv); void Trim(std::string_view & sv);
/// Remove any characters that contain in "anyOf" on left and right side of string s /// Remove any characters that contain in "anyOf" on left and right side of string s
@@ -156,7 +154,23 @@ inline bool IsASCIINumeric(char const * s)
{ {
return IsASCIINumeric(std::string_view(s)); return IsASCIINumeric(std::string_view(s));
} }
bool IsASCIISpace(UniChar c);
// std::isspace is locale-dependent and fails for trailing UTF-8 characters.
template <std::integral T>
inline constexpr bool IsASCIISpace(T c)
{
switch (c)
{
case ' ':
case '\f':
case '\n':
case '\r':
case '\t':
case '\v': return true;
default: return false;
}
}
bool IsASCIILatin(UniChar c); bool IsASCIILatin(UniChar c);
inline std::string DebugPrint(UniString const & s) inline std::string DebugPrint(UniString const & s)

View File

@@ -240,7 +240,7 @@ public:
{ {
std::string shieldText(rawText); std::string shieldText(rawText);
std::erase_if(shieldText, [](char c) { return c == '-' || ::isspace(c); }); std::erase_if(shieldText, [](char c) { return c == '-' || strings::IsASCIISpace(c); });
if (shieldText.size() <= 2) if (shieldText.size() <= 2)
return RoadShield(RoadShieldType::Default, rawText); return RoadShield(RoadShieldType::Default, rawText);

View File

@@ -279,7 +279,7 @@ bool OSMDistanceToMeters(std::string const & osmRawValue, double & outMeters)
case ';': return false; case ';': return false;
} }
while (*stop && isspace(*stop)) while (*stop && strings::IsASCIISpace(*stop))
++stop; ++stop;
// Default units - meters. // Default units - meters.

View File

@@ -11,6 +11,7 @@
#include <algorithm> #include <algorithm>
#include <array> #include <array>
#include <sstream> #include <sstream>
#include <string>
#include <utility> #include <utility>
namespace routing namespace routing
@@ -312,7 +313,7 @@ bool ParseLanes(string lanesString, vector<SingleLaneInfo> & lanes)
return false; return false;
lanes.clear(); lanes.clear();
strings::AsciiToLower(lanesString); strings::AsciiToLower(lanesString);
base::EraseIf(lanesString, [](char c) { return isspace(c); }); base::EraseIf(lanesString, strings::IsASCIISpace<std::string::value_type>);
vector<string> SplitLanesStrings; vector<string> SplitLanesStrings;
SingleLaneInfo lane; SingleLaneInfo lane;

View File

@@ -86,7 +86,7 @@ void RemoveStopWordsIfNeeded(QueryTokens & tokens, strings::UniString & prefix)
void TrimLeadingSpaces(string & s) void TrimLeadingSpaces(string & s)
{ {
while (!s.empty() && isspace(s.front())) while (!s.empty() && strings::IsASCIISpace(s.front()))
s = s.substr(1); s = s.substr(1);
} }