Skip to content

Commit 05ee913

Browse files
committed
Fix CI
1 parent 40ac58e commit 05ee913

7 files changed

Lines changed: 146 additions & 4 deletions

File tree

include/Stl/charconv.hpp

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#pragma once
2+
3+
#include <charconv>
4+
#include <clocale>
5+
#include <concepts>
6+
#include <cstdlib>
7+
#include <memory>
8+
#include <string>
9+
#include <system_error>
10+
11+
#if defined(__APPLE__)
12+
# include <xlocale.h>
13+
#endif
14+
15+
namespace Stl
16+
{
17+
using std::chars_format;
18+
using std::from_chars_result;
19+
20+
#if defined(__APPLE__)
21+
template<class T>
22+
requires std::integral<T>
23+
[[nodiscard]] inline auto from_chars(const char* first, const char* last, T& value, int base = 10) -> from_chars_result
24+
{
25+
const auto chunk = std::string(first, last);
26+
char* end = nullptr;
27+
28+
if constexpr (std::signed_integral<T>)
29+
{
30+
const auto result = std::strtoll(chunk.c_str(), std::addressof(end), base);
31+
value = static_cast<T>(result);
32+
}
33+
else
34+
{
35+
const auto result = std::strtoull(chunk.c_str(), std::addressof(end), base);
36+
value = static_cast<T>(result);
37+
}
38+
39+
if (end == chunk.c_str())
40+
return {first, std::errc::invalid_argument};
41+
42+
return {first + (end - chunk.c_str()), std::errc{}};
43+
}
44+
45+
template<class T>
46+
requires std::floating_point<T>
47+
[[nodiscard]] inline auto from_chars(const char* first, const char* last, T& value, [[maybe_unused]] chars_format fmt = chars_format::general) -> from_chars_result
48+
{
49+
const auto chunk = std::string(first, last);
50+
char* end = nullptr;
51+
static locale_t c_locale = newlocale(LC_ALL_MASK, "C", nullptr);
52+
53+
if constexpr (std::same_as<T, float>)
54+
value = strtof_l(chunk.c_str(), std::addressof(end), c_locale);
55+
else if constexpr (std::same_as<T, double>)
56+
value = strtod_l(chunk.c_str(), std::addressof(end), c_locale);
57+
else if constexpr (std::same_as<T, long double>)
58+
value = strtold_l(chunk.c_str(), std::addressof(end), c_locale);
59+
60+
if (end == chunk.c_str())
61+
return {first, std::errc::invalid_argument};
62+
63+
return {first + (end - chunk.c_str()), std::errc{}};
64+
}
65+
#else
66+
using std::from_chars;
67+
#endif
68+
}

modules/Language/CSV/Mapping.mpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
module;
2+
3+
#include <CppUtils/System/OS.hpp>
4+
5+
#if defined(OS_MACOS)
6+
# include <Stl/charconv.hpp>
7+
#endif
8+
19
export module CppUtils.Language.CSV.Mapping;
210

311
import std;
@@ -33,7 +41,11 @@ export namespace CppUtils::Language::CSV
3341
else if constexpr (std::same_as<MemberType, std::string_view> or std::same_as<MemberType, std::string>)
3442
member = value;
3543
else if constexpr (std::integral<MemberType> or std::floating_point<MemberType>)
44+
#if defined(OS_MACOS)
45+
Stl::from_chars(std::data(value), std::data(value) + std::size(value), member);
46+
#else
3647
std::from_chars(std::data(value), std::data(value) + std::size(value), member);
48+
#endif
3749
}
3850
}(csvPairs), ...);
3951
}, typename CSVMapping::Values{});

modules/Language/JSON/Parsing.mpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
module;
2+
3+
#include <CppUtils/System/OS.hpp>
4+
5+
#if defined(OS_MACOS)
6+
# include <Stl/charconv.hpp>
7+
#endif
8+
19
export module CppUtils.Language.JSON.Parsing;
210

311
import std;
@@ -75,7 +83,11 @@ export namespace CppUtils::Language::JSON
7583
[[nodiscard]] inline auto parseNumber() -> std::expected<double, Error>
7684
{
7785
auto value = 0.0;
86+
#if defined(OS_MACOS)
87+
auto result = Stl::from_chars(std::data(m_json) + m_position, std::data(m_json) + std::size(m_json), value);
88+
#else
7889
auto result = std::from_chars(std::data(m_json) + m_position, std::data(m_json) + std::size(m_json), value);
90+
#endif
7991
if (result.ec != std::errc())
8092
return std::unexpected{Error{"Invalid number format", m_position}};
8193

modules/Network/DNS.mpp

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,15 @@ export namespace CppUtils::Network::DNS
2424
[[nodiscard]] inline auto getHostName() -> std::string
2525
{
2626
#if defined(OS_WINDOWS)
27-
Socket::WindowsSocketApi::ensureIsinitialized();
27+
WindowsSocketApi::ensureIsinitialized();
2828
#endif
2929
auto hostname = std::string(MaxHostNameLength, '\0');
30-
if (::gethostname(std::data(hostname), std::size(hostname)) != 0)
30+
#if defined(OS_WINDOWS)
31+
const auto length = static_cast<int>(std::size(hostname));
32+
#else
33+
const auto length = std::size(hostname);
34+
#endif
35+
if (::gethostname(std::data(hostname), length) != 0)
3136
return "unknown";
3237

3338
if (const auto position = hostname.find('\0'); position != std::string::npos)
@@ -38,7 +43,7 @@ export namespace CppUtils::Network::DNS
3843
[[nodiscard]] inline auto resolve(std::string_view hostname) -> std::vector<std::string>
3944
{
4045
#if defined(OS_WINDOWS)
41-
Socket::WindowsSocketApi::ensureIsinitialized();
46+
WindowsSocketApi::ensureIsinitialized();
4247
#endif
4348
auto hints = addrinfo{};
4449
hints.ai_family = AF_UNSPEC; // IPv4 or IPv6
@@ -60,7 +65,12 @@ export namespace CppUtils::Network::DNS
6065
else
6166
address = std::addressof(reinterpret_cast<sockaddr_in6*>(entry->ai_addr)->sin6_addr);
6267

63-
::inet_ntop(entry->ai_family, address, std::data(ipString), static_cast<socklen_t>(std::size(ipString)));
68+
#if defined(OS_WINDOWS)
69+
const auto ipStringSize = std::size(ipString);
70+
#else
71+
const auto ipStringSize = static_cast<socklen_t>(std::size(ipString));
72+
#endif
73+
::inet_ntop(entry->ai_family, address, std::data(ipString), ipStringSize);
6474
ips.emplace_back(std::data(ipString));
6575
}
6676

modules/Network/Http/Client.mpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
module;
2+
3+
#include <CppUtils/System/OS.hpp>
4+
5+
#if defined(OS_MACOS)
6+
# include <Stl/charconv.hpp>
7+
#endif
8+
19
export module CppUtils.Network.Http.Client;
210

311
import std;
@@ -95,7 +103,11 @@ export namespace CppUtils::Network::Http
95103
{
96104
auto codeString = statusLine.substr(firstSpace + 1, secondSpace - firstSpace - 1);
97105
auto code = 0uz;
106+
#if defined(OS_MACOS)
107+
Stl::from_chars(std::data(codeString), std::data(codeString) + std::size(codeString), code);
108+
#else
98109
std::from_chars(std::data(codeString), std::data(codeString) + std::size(codeString), code);
110+
#endif
99111
response.status = static_cast<Status>(code);
100112
}
101113
}

modules/Network/Http/Common.mpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
module;
2+
3+
#include <CppUtils/System/OS.hpp>
4+
5+
#if defined(OS_MACOS)
6+
# include <Stl/charconv.hpp>
7+
#endif
8+
19
export module CppUtils.Network.Http.Common;
210

311
import std;
@@ -245,7 +253,11 @@ export namespace CppUtils::Network::Http
245253
{
246254
auto contentLength = 0uz;
247255
if (const auto it = std::ranges::find_if(headers, [](const auto& header) { return header.name == "Content-Length"; }); it != std::end(headers))
256+
#if defined(OS_MACOS)
257+
Stl::from_chars(std::data(it->value), std::data(it->value) + std::size(it->value), contentLength);
258+
#else
248259
std::from_chars(std::data(it->value), std::data(it->value) + std::size(it->value), contentLength);
260+
#endif
249261
return contentLength;
250262
}
251263

tests/Language/CSV/Mapping.mpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
module;
2+
3+
#include <CppUtils/System/OS.hpp>
4+
5+
#if defined(OS_MACOS)
6+
# include <Stl/charconv.hpp>
7+
#endif
8+
19
export module CppUtils.UnitTests.Language.CSV.Mapping;
210

311
import std;
@@ -94,7 +102,11 @@ namespace CppUtils::UnitTest::Language::CSV::Mapping
94102
std::size_t factor;
95103
inline auto operator()(auto& member, std::string_view value) const -> void
96104
{
105+
#if defined(OS_MACOS)
106+
Stl::from_chars(std::data(value), std::data(value) + std::size(value), member);
107+
#else
97108
std::from_chars(std::data(value), std::data(value) + std::size(value), member);
109+
#endif
98110
member *= factor;
99111
}
100112
};
@@ -163,7 +175,11 @@ namespace CppUtils::UnitTest::Language::CSV::Mapping
163175
using StructMapping = UnitTest::Language::CSV::Mapping::StructMapping;
164176
using FunctionMapping = CppUtils::Type::Mapping<
165177
CppUtils::Type::Pair<"Unsigned integer"_token, [](auto& member, std::string_view value) {
178+
#if defined(OS_MACOS)
179+
Stl::from_chars(std::data(value), std::data(value) + std::size(value), member);
180+
#else
166181
std::from_chars(std::data(value), std::data(value) + std::size(value), member);
182+
#endif
167183
member *= 2;
168184
}>>;
169185
};

0 commit comments

Comments
 (0)