Skip to content
This repository was archived by the owner on Oct 1, 2024. It is now read-only.

Commit ef5993e

Browse files
committed
[Build] Add back runtime checks..
As there is no way to determine the runtime target on Mac/Windows, we have to do this too. The build mono/.NET can be different than the target runtime
1 parent 1f3121d commit ef5993e

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

glib/Marshaller.cs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,18 @@ public static string FilenamePtrToStringGFree (IntPtr ptr)
7676
return ret;
7777
}
7878

79+
#if HAVE_NET_4_6
80+
static bool hasFastGetStringOverload = typeof (System.Text.Encoding).GetMethod ("GetString", new [] { typeof (byte*), typeof (int) }) != null;
81+
static string Utf8PtrToStringFast (IntPtr ptr, int len)
82+
{
83+
unsafe
84+
{
85+
var p = (byte*)ptr;
86+
return System.Text.Encoding.UTF8.GetString (p, len);
87+
}
88+
}
89+
#endif
90+
7991
[DllImport("glibsharpglue-2", CallingConvention=CallingConvention.Cdecl)]
8092
static extern UIntPtr glibsharp_strlen (IntPtr mem);
8193
public static string Utf8PtrToString (IntPtr ptr)
@@ -85,16 +97,12 @@ public static string Utf8PtrToString (IntPtr ptr)
8597

8698
int len = (int) (uint)glibsharp_strlen (ptr);
8799
#if HAVE_NET_4_6
88-
unsafe
89-
{
90-
var p = (byte*)ptr;
91-
return System.Text.Encoding.UTF8.GetString (p, len);
92-
}
93-
#else
100+
if (hasFastGetStringOverload)
101+
return Utf8PtrToStringFast (ptr, len);
102+
#endif
94103
byte [] bytes = new byte [len];
95104
Marshal.Copy (ptr, bytes, 0, len);
96105
return System.Text.Encoding.UTF8.GetString (bytes);
97-
#endif
98106
}
99107

100108
public static string[] Utf8PtrToString (IntPtr[] ptrs) {

0 commit comments

Comments
 (0)