Skip to content

Commit

Permalink
Alternative unicode_strlen for macOS
Browse files Browse the repository at this point in the history
  • Loading branch information
mmd-osm committed Sep 10, 2024
1 parent 00d866c commit e0f4ff1
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions include/cgimap/util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,30 @@
#include <fmt/ranges.h>


#if __APPLE__
// NOTE: <codecvt> is deprecated in C++17 and removed in C++26 (see P2871R3).

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated"

#include <codecvt>
#include <iomanip>
#include <stdexcept>

inline std::size_t unicode_strlen(const std::string& utf8)
{

try {
return std::wstring_convert< std::codecvt_utf8<char32_t>, char32_t >{}.from_bytes(utf8).size();
} catch(std::range_error) {
throw http::bad_request("Invalid UTF-8 string encountered");
}
}

#pragma clang diagnostic pop

#else

inline size_t unicode_strlen(const std::string & s)
{
const char* mbstr = s.c_str();
Expand All @@ -44,6 +68,8 @@ inline size_t unicode_strlen(const std::string & s)
return len;
}

#endif

inline std::string escape(std::string_view input) {

int n = 0;
Expand Down

0 comments on commit e0f4ff1

Please sign in to comment.