Use string_view

This commit is contained in:
LAGonauta 2021-02-22 06:18:53 -03:00
parent ca0cab0e36
commit 13e0a5c0c4
6 changed files with 20 additions and 31 deletions

View file

@ -1,7 +1,7 @@
#pragma once #pragma once
#include <glib.h>
#include <string> #include <string>
#include <string_view>
std::wstring char_to_wstr(const gchar* str); std::wstring sview_to_wstr(const std::string_view str);
char* wstr_to_char(const std::wstring& wstr); char* wsview_to_char(const std::wstring_view wstr);

View file

@ -3,7 +3,7 @@
#include "converter.hpp" #include "converter.hpp"
// Convert a wide Unicode string to an UTF8 string // Convert a wide Unicode string to an UTF8 string
std::string wstr_to_str(const std::wstring& wstr) std::string wstr_to_str(const std::wstring_view wstr)
{ {
if(wstr.empty()) if(wstr.empty())
{ {
@ -15,7 +15,7 @@ std::string wstr_to_str(const std::wstring& wstr)
return strTo; return strTo;
} }
char* wstr_to_char(const std::wstring& wstr) char* wsview_to_char(const std::wstring_view wstr)
{ {
if(wstr.empty()) if(wstr.empty())
{ {
@ -28,7 +28,7 @@ char* wstr_to_char(const std::wstring& wstr)
} }
// Convert an UTF8 string to a wide Unicode String // Convert an UTF8 string to a wide Unicode String
std::wstring std_to_wstr(const std::string &str) std::wstring sview_to_wstr(const std::string_view str)
{ {
if(str.empty()) if(str.empty())
{ {
@ -39,15 +39,3 @@ std::wstring std_to_wstr(const std::string &str)
MultiByteToWideChar(CP_UTF8, 0, str.data(), (int)str.size(), wstrTo.data(), final_size); MultiByteToWideChar(CP_UTF8, 0, str.data(), (int)str.size(), wstrTo.data(), final_size);
return wstrTo; return wstrTo;
} }
std::wstring char_to_wstr(const char* str) // TODO: how to be safe from non-null terminated strings?
{
if(str == nullptr)
{
return std::wstring();
}
int final_size = MultiByteToWideChar(CP_UTF8, 0, str, strlen(str), nullptr, 0);
std::wstring wstrTo(final_size, 0);
MultiByteToWideChar(CP_UTF8, 0, str, strlen(str), wstrTo.data(), final_size);
return wstrTo;
}

View file

@ -12,6 +12,7 @@
#include <iostream> #include <iostream>
#include <string> #include <string>
#include <string_view>
#include <vector> #include <vector>
#include <tuple> #include <tuple>
@ -147,7 +148,7 @@ winrtWindowsUINotificationsToastNotification* winrt_windows_ui_notifications_toa
auto ret = static_cast<winrtWindowsUINotificationsToastNotification*>(g_object_new (WINRT_TYPE_WINDOWS_UI_NOTIFICATIONS_TOAST_NOTIFICATION, NULL)); auto ret = static_cast<winrtWindowsUINotificationsToastNotification*>(g_object_new (WINRT_TYPE_WINDOWS_UI_NOTIFICATIONS_TOAST_NOTIFICATION, NULL));
winrt::Windows::Data::Xml::Dom::XmlDocument xmlDoc; winrt::Windows::Data::Xml::Dom::XmlDocument xmlDoc;
xmlDoc.LoadXml(char_to_wstr(doc)); xmlDoc.LoadXml(sview_to_wstr(doc));
winrt_windows_ui_notifications_toast_notification_set_internal(ret, winrt::Windows::UI::Notifications::ToastNotification{ xmlDoc }); winrt_windows_ui_notifications_toast_notification_set_internal(ret, winrt::Windows::UI::Notifications::ToastNotification{ xmlDoc });
return ret; return ret;
} }
@ -170,7 +171,7 @@ void winrt_windows_ui_notifications_toast_notification_set_Tag(winrtWindowsUINot
{ {
g_return_if_fail (WINRT_IS_WINDOWS_UI_NOTIFICATIONS_TOAST_NOTIFICATION (self)); g_return_if_fail (WINRT_IS_WINDOWS_UI_NOTIFICATIONS_TOAST_NOTIFICATION (self));
winrt_windows_ui_notifications_toast_notification_get_internal(self)->Tag(char_to_wstr(value)); winrt_windows_ui_notifications_toast_notification_get_internal(self)->Tag(sview_to_wstr(value));
} }
/** /**
@ -185,14 +186,14 @@ char* winrt_windows_ui_notifications_toast_notification_get_Tag(winrtWindowsUINo
{ {
g_return_val_if_fail (WINRT_IS_WINDOWS_UI_NOTIFICATIONS_TOAST_NOTIFICATION (self), FALSE); g_return_val_if_fail (WINRT_IS_WINDOWS_UI_NOTIFICATIONS_TOAST_NOTIFICATION (self), FALSE);
return wstr_to_char(std::wstring(winrt_windows_ui_notifications_toast_notification_get_internal(self)->Tag())); return wsview_to_char(winrt_windows_ui_notifications_toast_notification_get_internal(self)->Tag());
} }
void winrt_windows_ui_notifications_toast_notification_set_Group(winrtWindowsUINotificationsToastNotification* self, const char* value) void winrt_windows_ui_notifications_toast_notification_set_Group(winrtWindowsUINotificationsToastNotification* self, const char* value)
{ {
g_return_if_fail (WINRT_IS_WINDOWS_UI_NOTIFICATIONS_TOAST_NOTIFICATION (self)); g_return_if_fail (WINRT_IS_WINDOWS_UI_NOTIFICATIONS_TOAST_NOTIFICATION (self));
winrt_windows_ui_notifications_toast_notification_get_internal(self)->Group(char_to_wstr(value)); winrt_windows_ui_notifications_toast_notification_get_internal(self)->Group(sview_to_wstr(value));
} }
/** /**
@ -207,7 +208,7 @@ char* winrt_windows_ui_notifications_toast_notification_get_Group(winrtWindowsUI
{ {
g_return_val_if_fail (WINRT_IS_WINDOWS_UI_NOTIFICATIONS_TOAST_NOTIFICATION (self), FALSE); g_return_val_if_fail (WINRT_IS_WINDOWS_UI_NOTIFICATIONS_TOAST_NOTIFICATION (self), FALSE);
return wstr_to_char(std::wstring(winrt_windows_ui_notifications_toast_notification_get_internal(self)->Group())); return wsview_to_char(winrt_windows_ui_notifications_toast_notification_get_internal(self)->Group());
} }
winrtEventToken* winrt_windows_ui_notifications_toast_notification_Activated(winrtWindowsUINotificationsToastNotification* self, NotificationCallbackActivated callback, void* context, void(*free)(void*)) winrtEventToken* winrt_windows_ui_notifications_toast_notification_Activated(winrtWindowsUINotificationsToastNotification* self, NotificationCallbackActivated callback, void* context, void(*free)(void*))
@ -248,7 +249,7 @@ winrtEventToken* winrt_windows_ui_notifications_toast_notification_Activated(win
} }
std::cout << "Notification activated!" << std::endl; std::cout << "Notification activated!" << std::endl;
priv->activated.callback(wstr_to_char(arguments.data()), nullptr, user_input.size(), priv->activated.context); priv->activated.callback(wsview_to_char(arguments.data()), nullptr /* user_input */ , 0 /* user_input.size() */, priv->activated.context);
}); });
priv->activated.token = winrt_event_token_new_from_token(&token); priv->activated.token = winrt_event_token_new_from_token(&token);
return priv->activated.token; return priv->activated.token;

View file

@ -150,7 +150,7 @@ extern "C"
{ {
gboolean TryCreateShortcut(const gchar* aumid) gboolean TryCreateShortcut(const gchar* aumid)
{ {
auto result = char_to_wstr(aumid); auto result = sview_to_wstr(aumid);
if (result.empty()) if (result.empty())
{ {
return FALSE; return FALSE;

View file

@ -6,22 +6,22 @@
std::optional<std::wstring> GetCurrentModulePath() std::optional<std::wstring> GetCurrentModulePath()
{ {
std::array<wchar_t, MAX_PATH> exePath; std::wstring exePath(MAX_PATH, 0);
auto charWritten = GetModuleFileName(nullptr, exePath.data(), exePath.size()); auto charWritten = GetModuleFileName(nullptr, exePath.data(), exePath.size());
if (charWritten > 0) if (charWritten > 0)
{ {
return std::wstring(exePath.data()); return exePath;
} }
return std::nullopt; return std::nullopt;
} }
std::optional<std::wstring> GetShortcutPath() std::optional<std::wstring> GetShortcutPath()
{ {
std::array<wchar_t, MAX_PATH> shortcutPath; std::wstring shortcutPath(MAX_PATH, 0);
auto charWritten = GetEnvironmentVariable(L"APPDATA", shortcutPath.data(), shortcutPath.size()); auto charWritten = GetEnvironmentVariable(L"APPDATA", shortcutPath.data(), shortcutPath.size());
if (charWritten > 0) if (charWritten > 0)
{ {
return std::wstring(shortcutPath.data()); return shortcutPath;
} }
return std::nullopt; return std::nullopt;
} }
@ -49,7 +49,7 @@ extern "C"
gboolean SetAppModelID(const gchar* aumid) gboolean SetAppModelID(const gchar* aumid)
{ {
auto result = char_to_wstr(aumid); auto result = sview_to_wstr(aumid);
if (result.empty()) if (result.empty())
{ {
return FALSE; return FALSE;

View file

@ -27,7 +27,7 @@ public class Plugin : RootInterface, Object {
{ {
var m = new winrt.Windows.UI.Notifications.ToastNotification("Test"); var m = new winrt.Windows.UI.Notifications.ToastNotification("Test");
var token = m.Activated(() => { var token = m.Activated((c, d) => {
var i = 2; var i = 2;
}); });
m.RemoveActivatedAction(token); m.RemoveActivatedAction(token);