Skip to content

Commit

Permalink
[mono][eglib] Avoid utf16 conversion if all characters are ASCII
Browse files Browse the repository at this point in the history
  • Loading branch information
BrzVlad committed Jan 30, 2024
1 parent 0c03b43 commit 6192682
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 23 deletions.
70 changes: 47 additions & 23 deletions src/mono/mono/eglib/gfile.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/mono/mono/eglib/glib.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 6192682

Please sign in to comment.