MCPcopy Create free account
hub / github.com/CLIUtils/CLI11 / string narrow_impl

Method string narrow_impl

include/CLI/impl/Encoding_inl.hpp:59–91  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

57#endif // !CLI11_HAS_CODECVT
58
59CLI11_DIAGNOSTIC_PUSH
60CLI11_DIAGNOSTIC_IGNORE_DEPRECATED
61
62CLI11_INLINE std::string narrow_impl(const wchar_t *str, std::size_t str_size) {
63#if CLI11_HAS_CODECVT
64#ifdef _WIN32
65 return std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>>().to_bytes(str, str + str_size);
66
67#else
68 return std::wstring_convert<std::codecvt_utf8<wchar_t>>().to_bytes(str, str + str_size);
69
70#endif // _WIN32
71#else // CLI11_HAS_CODECVT
72 (void)str_size;
73 std::mbstate_t state = std::mbstate_t();
74 const wchar_t *it = str;
75
76 std::string old_locale = std::setlocale(LC_ALL, nullptr);
77 auto sg = scope_guard([&] { std::setlocale(LC_ALL, old_locale.c_str()); });
78 set_unicode_locale();
79
80 std::size_t new_size = std::wcsrtombs(nullptr, &it, 0, &state);
81 if(new_size == static_cast<std::size_t>(-1)) {
82 throw std::runtime_error("CLI::narrow: conversion error in std::wcsrtombs at offset " +
83 std::to_string(it - str));
84 }
85 std::string result(new_size, '\0');
86 std::wcsrtombs(const_cast<char *>(result.data()), &str, new_size, &state);
87
88 return result;
89
90#endif // CLI11_HAS_CODECVT
91}
92
93CLI11_INLINE std::wstring widen_impl(const char *str, std::size_t str_size) {
94#if CLI11_HAS_CODECVT

Callers

nothing calls this directly

Calls 3

set_unicode_localeFunction · 0.85
to_stringFunction · 0.85
c_strMethod · 0.80

Tested by

no test coverage detected