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

@@ -11,6 +11,7 @@
#include <fast_double_parser.h>
#include <boost/algorithm/string/trim.hpp>
#include <string>
namespace strings
{
@@ -227,15 +228,15 @@ void AsciiToUpper(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)
{
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())
{
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));
}
else
@@ -355,11 +356,6 @@ bool IsASCIIDigit(UniChar c)
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)
{
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 AsciiToUpper(std::string & s);
// All triming functions return a reference on an input string.
// They do in-place trimming. In general, it does not work for any unicode whitespace except
// ASCII U+0020 one.
// All trimming functions do in-place trimming. Only ASCII whitespaces are trimmed.
void Trim(std::string & s);
void Trim(std::string_view & sv);
/// 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));
}
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);
inline std::string DebugPrint(UniString const & s)