From 6192682829a06cdb6a278df6ecd4095f7500085e Mon Sep 17 00:00:00 2001 From: Vlad Brezae Date: Tue, 30 Jan 2024 14:52:17 +0200 Subject: [PATCH] [mono][eglib] Avoid utf16 conversion if all characters are ASCII --- src/mono/mono/eglib/gfile.c | 70 +++++++++++++++++++++++++------------ src/mono/mono/eglib/glib.h | 1 + 2 files changed, 48 insertions(+), 23 deletions(-) diff --git a/src/mono/mono/eglib/gfile.c b/src/mono/mono/eglib/gfile.c index 19331a98aba65..4d5390c193294 100644 --- a/src/mono/mono/eglib/gfile.c +++ b/src/mono/mono/eglib/gfile.c @@ -105,6 +105,18 @@ g_file_error_from_errno (gint err_no) } } +#ifdef HOST_WIN32 +static gboolean +is_ascii_string (const gchar *str) +{ + while (*str) { + if (!g_isascii (*str)) + return FALSE; + } + return TRUE; +} +#endif + FILE* g_fopen (const gchar *path, const gchar *mode) { @@ -114,15 +126,19 @@ g_fopen (const gchar *path, const gchar *mode) return NULL; #ifdef HOST_WIN32 - gunichar2 *wPath = g_utf8_to_utf16 (path, -1, 0, 0, 0); - gunichar2 *wMode = g_utf8_to_utf16 (mode, -1, 0, 0, 0); - - if (!wPath || !wMode) - return NULL; - - fp = _wfopen ((wchar_t *) wPath, (wchar_t *) wMode); - g_free (wPath); - g_free (wMode); + if (is_ascii_string (path) && is_ascii_string (mode)) { + fp = fopen (path, mode); + } else { + gunichar2 *wPath = g_utf8_to_utf16 (path, -1, 0, 0, 0); + gunichar2 *wMode = g_utf8_to_utf16 (mode, -1, 0, 0, 0); + + if (!wPath || !wMode) + return NULL; + + fp = _wfopen ((wchar_t *) wPath, (wchar_t *) wMode); + g_free (wPath); + g_free (wMode); + } #else fp = fopen (path, mode); #endif @@ -134,17 +150,21 @@ int g_rename (const gchar *src_path, const gchar *dst_path) { #ifdef HOST_WIN32 - gunichar2 *wSrcPath = g_utf8_to_utf16 (src_path, -1, 0, 0, 0); - gunichar2 *wDstPath = g_utf8_to_utf16 (dst_path, -1, 0, 0, 0); + if (is_ascii_string (src_path) && is_ascii_string (dst_path)) { + return rename (src_path, dst_path); + } else { + gunichar2 *wSrcPath = g_utf8_to_utf16 (src_path, -1, 0, 0, 0); + gunichar2 *wDstPath = g_utf8_to_utf16 (dst_path, -1, 0, 0, 0); - if (!wSrcPath || !wDstPath) - return -1; + if (!wSrcPath || !wDstPath) + return -1; - int ret = _wrename ((wchar_t *) wSrcPath, (wchar_t *) wDstPath); - g_free (wSrcPath); - g_free (wDstPath); + int ret = _wrename ((wchar_t *) wSrcPath, (wchar_t *) wDstPath); + g_free (wSrcPath); + g_free (wDstPath); - return ret; + return ret; + } #else return rename (src_path, dst_path); #endif @@ -154,15 +174,19 @@ int g_unlink (const gchar *path) { #ifdef HOST_WIN32 - gunichar2 *wPath = g_utf8_to_utf16 (path, -1, 0, 0, 0); + if (is_ascii_string (path)) { + return unlink (path); + } else { + gunichar2 *wPath = g_utf8_to_utf16 (path, -1, 0, 0, 0); - if (!wPath) - return -1; + if (!wPath) + return -1; - int ret = _wunlink ((wchar_t *) wPath); - g_free (wPath); + int ret = _wunlink ((wchar_t *) wPath); + g_free (wPath); - return ret; + return ret; + } #else return unlink (path); #endif diff --git a/src/mono/mono/eglib/glib.h b/src/mono/mono/eglib/glib.h index 9a8474310bede..7ec9dcde9c8e6 100644 --- a/src/mono/mono/eglib/glib.h +++ b/src/mono/mono/eglib/glib.h @@ -391,6 +391,7 @@ gint g_ascii_xdigit_value (gchar c); #define g_ascii_isalpha(c) (isalpha (c) != 0) #define g_ascii_isprint(c) (isprint (c) != 0) #define g_ascii_isxdigit(c) (isxdigit (c) != 0) +#define g_isascii(c) (isascii (c) != 0) /* FIXME: g_strcasecmp supports utf8 unicode stuff */ #ifdef _MSC_VER