Skip to content

Commit 6dac030

Browse files
authored
Merge pull request #140 from NuiCpp/feat/use-utf8cpp
Replaced std codecvt with utfcpp library
2 parents 52cd358 + 232bef9 commit 6dac030

File tree

9 files changed

+79
-43
lines changed

9 files changed

+79
-43
lines changed

CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
cmake_minimum_required(VERSION 3.16)
22

3-
project(nui VERSION 0.10.2)
3+
project(nui VERSION 1.1.0)
44

55
set(NUI_SOURCE_DIRECTORY ${CMAKE_CURRENT_LIST_DIR} CACHE INTERNAL "NUI source directory")
66

@@ -15,6 +15,7 @@ include(${CMAKE_CURRENT_LIST_DIR}/cmake/dependencies/fmt.cmake)
1515
include(${CMAKE_CURRENT_LIST_DIR}/cmake/dependencies/describe.cmake)
1616
include(${CMAKE_CURRENT_LIST_DIR}/cmake/dependencies/mp11.cmake)
1717
include(${CMAKE_CURRENT_LIST_DIR}/cmake/dependencies/nui_traits.cmake)
18+
include(${CMAKE_CURRENT_LIST_DIR}/cmake/dependencies/utfcpp.cmake)
1819
if (EMSCRIPTEN)
1920
include(${CMAKE_CURRENT_LIST_DIR}/cmake/frontend/emscripten.cmake)
2021
else()

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,4 @@ Nui uses the following dependencies:
5353
- portable-file-dialogs https://github.com/samhocevar/portable-file-dialogs - [WTFPL LICENSE](https://github.com/samhocevar/portable-file-dialogs/blob/main/COPYING)
5454
- 5cript/roar https://github.com/5cript/roar - [BSL-1.0 LICENSE](https://github.com/5cript/roar/blob/master/LICENSE)
5555
- Nui/traits https://github.com/NuiCpp/traits - [CC0-1.0 LICENSE](https://github.com/NuiCpp/traits/blob/main/LICENSE)
56+
- nemtrif/utfcpp https://github.com/nemtrif/utfcpp - [BSL-1.0 license](https://github.com/nemtrif/utfcpp/blob/master/LICENSE)

cmake/dependencies/utfcpp.cmake

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
option(NUI_FETCH_UTFCPP "Try FetchContent for utfcpp" ON)
2+
3+
set(NUI_UTFCPP_GIT_REPOSITORY "https://github.com/nemtrif/utfcpp.git" CACHE STRING "utfcpp git repository")
4+
set(NUI_UTFCPP_GIT_TAG "v4.0.8" CACHE STRING "utfcpp git tag")
5+
6+
include("${CMAKE_CURRENT_LIST_DIR}/../fetcher.cmake")
7+
8+
nui_fetch_dependency(
9+
LIBRARY_NAME utf8cpp
10+
FIND OFF # Developer does not support this library being found
11+
FETCH ${NUI_FETCH_UTFCPP}
12+
GIT_REPOSITORY ${NUI_UTFCPP_GIT_REPOSITORY}
13+
GIT_TAG ${NUI_UTFCPP_GIT_TAG}
14+
)

nui/include/nui/utility/utf.hpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#pragma once
2+
3+
#include <utf8.h>
4+
5+
#include <iterator>
6+
7+
namespace Nui
8+
{
9+
namespace Detail
10+
{
11+
template <typename T>
12+
concept IsStringCharTypeUtf16Compatible = requires(T str) {
13+
typename T::value_type;
14+
requires(sizeof(typename T::value_type) > 1);
15+
};
16+
17+
template <typename T>
18+
concept IsStringCharTypeUtf8Compatible = requires(T str) {
19+
typename T::value_type;
20+
requires(sizeof(typename T::value_type) == 1);
21+
};
22+
}
23+
24+
template <typename Utf16StringType, typename Utf8StringType>
25+
requires(Detail::IsStringCharTypeUtf16Compatible<Utf16StringType>) &&
26+
(Detail::IsStringCharTypeUtf8Compatible<Utf8StringType>)
27+
Utf16StringType utf8ToUtf16(Utf8StringType const& str)
28+
{
29+
Utf16StringType result;
30+
utf8::utf8to16(str.begin(), str.end(), std::back_inserter(result));
31+
return result;
32+
}
33+
34+
template <typename Utf16StringType, typename Utf8StringType>
35+
requires(Detail::IsStringCharTypeUtf16Compatible<Utf16StringType>) &&
36+
(Detail::IsStringCharTypeUtf8Compatible<Utf8StringType>)
37+
Utf8StringType utf16ToUtf8(Utf16StringType const& str)
38+
{
39+
Utf8StringType result;
40+
utf8::utf16to8(str.begin(), str.end(), std::back_inserter(result));
41+
return result;
42+
}
43+
}

nui/include/nui/utility/widen.hpp

Lines changed: 0 additions & 26 deletions
This file was deleted.

nui/src/nui/backend/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ target_link_libraries(
3030
nui-backend
3131
PRIVATE
3232
project-settings
33+
utf8cpp
3334
PUBLIC
3435
traits-library
3536
fmt

nui/src/nui/backend/environment_options_from_window_options.ipp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ namespace Nui
88

99
if (options.browserArguments)
1010
{
11-
const auto wideArgs = widenString(*options.browserArguments);
11+
const auto wideArgs = utf8ToUtf16<std::wstring, std::string>(*options.browserArguments);
1212
environmentOptions->put_AdditionalBrowserArguments(wideArgs.c_str());
1313
}
1414

1515
if (options.language)
1616
{
17-
const auto wideLanguage = widenString(*options.language);
17+
const auto wideLanguage = utf8ToUtf16<std::wstring, std::string>(*options.language);
1818
environmentOptions->put_Language(wideLanguage.c_str());
1919
}
2020

@@ -32,15 +32,15 @@ namespace Nui
3232

3333
for (const auto& customScheme : options.customSchemes)
3434
{
35-
wideSchemes.push_back(widenString(customScheme.scheme));
35+
wideSchemes.push_back(utf8ToUtf16<std::wstring, std::string>(customScheme.scheme));
3636
customSchemeRegistrations.push_back(
3737
Microsoft::WRL::Make<CoreWebView2CustomSchemeRegistration>(wideSchemes.back().c_str()));
3838
auto& customSchemeRegistration = customSchemeRegistrations.back();
3939

4040
allowedOrigins.push_back({});
4141
allowedOrigins.back().reserve(customScheme.allowedOrigins.size());
4242
for (const auto& allowedOrigin : customScheme.allowedOrigins)
43-
allowedOrigins.back().push_back(widenString(allowedOrigin));
43+
allowedOrigins.back().push_back(utf8ToUtf16<std::wstring, std::string>(allowedOrigin));
4444

4545
allowedOriginsRaw.push_back({});
4646
allowedOriginsRaw.back().reserve(allowedOrigins.back().size());

nui/src/nui/backend/window.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
#include <nui/backend/filesystem/special_paths.hpp>
44
#include <nui/backend/filesystem/file_dialog.hpp>
55
#include <nui/utility/scope_exit.hpp>
6-
#include <nui/utility/widen.hpp>
7-
#include <nui/utility/scope_exit.hpp>
6+
#include <nui/utility/utf.hpp>
87
#include <nui/data_structures/selectables_registry.hpp>
98
#include <nui/screen.hpp>
109
#include "load_file.hpp"
@@ -347,7 +346,7 @@ namespace Nui
347346
windowsServeThroughAuthority = std::string{windowsServeAuthority};
348347
const auto authority = *windowsServeThroughAuthority;
349348

350-
const std::wstring filter = widenString("https://"s + authority + "/index.html");
349+
const auto filter = utf8ToUtf16<std::wstring, std::string>("https://"s + authority + "/index.html");
351350

352351
auto result = webView->AddWebResourceRequestedFilter(
353352
filter.c_str(), static_cast<COREWEBVIEW2_WEB_RESOURCE_CONTEXT>(NuiCoreWebView2WebResourceContext::All));
@@ -370,7 +369,7 @@ namespace Nui
370369
webViewRequest->get_Uri(&uri);
371370
std::wstring uriW{uri};
372371
CoTaskMemFree(uri);
373-
return shortenString(uriW);
372+
return utf16ToUtf8<std::wstring, std::string>(uriW);
374373
}();
375374

376375
if (uri != compareUri)
@@ -697,8 +696,8 @@ namespace Nui
697696
break;
698697
}
699698

700-
const auto wideHost = widenString(hostName);
701-
const auto widePath = widenString(folderPath.string());
699+
const auto wideHost = utf8ToUtf16<std::wstring, std::string>(hostName);
700+
const auto widePath = utf8ToUtf16<std::wstring, std::string>(folderPath.string());
702701
wv23->SetVirtualHostNameToFolderMapping(wideHost.c_str(), widePath.c_str(), nativeAccessKind);
703702
#elif defined(__APPLE__)
704703
(void)accessKind;

nui/src/nui/backend/window_impl_win.ipp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ namespace Nui
3939

4040
for (auto const& customScheme : options.customSchemes)
4141
{
42-
const std::wstring filter = widenString(customScheme.scheme + ":*");
42+
const std::wstring filter = utf8ToUtf16<std::wstring, std::string>(customScheme.scheme + ":*");
4343

4444
auto result = webView->AddWebResourceRequestedFilter(
4545
filter.c_str(), static_cast<COREWEBVIEW2_WEB_RESOURCE_CONTEXT>(NuiCoreWebView2WebResourceContext::All));
@@ -79,7 +79,7 @@ namespace Nui
7979
webViewRequest->get_Uri(&uri);
8080
std::wstring uriW{uri};
8181
CoTaskMemFree(uri);
82-
return shortenString(uriW);
82+
return utf16ToUtf8<std::wstring, std::string>(uriW);
8383
}();
8484

8585
const auto customScheme = [&schemes, &uri]() -> std::optional<CustomScheme> {
@@ -126,7 +126,8 @@ namespace Nui
126126

127127
std::wstring responseHeaders;
128128
for (auto const& [key, value] : responseData.headers)
129-
responseHeaders += widenString(key) + L": " + widenString(value) + L"\r\n";
129+
responseHeaders += utf8ToUtf16<std::wstring, std::string>(key) + L": " +
130+
utf8ToUtf16<std::wstring, std::string>(value) + L"\r\n";
130131
if (!responseHeaders.empty())
131132
{
132133
responseHeaders.pop_back();
@@ -137,7 +138,7 @@ namespace Nui
137138
stream.Attach(SHCreateMemStream(
138139
reinterpret_cast<const BYTE*>(responseData.body.data()), static_cast<UINT>(responseData.body.size())));
139140

140-
const auto phrase = widenString(responseData.reasonPhrase);
141+
const auto phrase = utf8ToUtf16<std::wstring, std::string>(responseData.reasonPhrase);
141142
result = environment->CreateWebResourceResponse(
142143
stream.Get(), responseData.statusCode, phrase.c_str(), responseHeaders.c_str(), &response);
143144

@@ -191,7 +192,9 @@ namespace Nui
191192
CoTaskMemFree(name);
192193
CoTaskMemFree(value);
193194

194-
headersMap.emplace(shortenString(nameW), shortenString(valueW));
195+
headersMap.emplace(
196+
utf16ToUtf8<std::wstring, std::string>(nameW),
197+
utf16ToUtf8<std::wstring, std::string>(valueW));
195198

196199
BOOL hasNext = FALSE;
197200
if (FAILED(iterator->MoveNext(&hasNext)) || !hasNext)
@@ -206,7 +209,7 @@ namespace Nui
206209
webViewRequest->get_Method(&method);
207210
std::wstring methodW{method};
208211
CoTaskMemFree(method);
209-
return shortenString(methodW);
212+
return utf16ToUtf8<std::wstring, std::string>(methodW);
210213
}(),
211214
.resourceContext = static_cast<NuiCoreWebView2WebResourceContext>(resourceContext),
212215
};

0 commit comments

Comments
 (0)