From ef5993e6da4b35d9b5c8c3b208f307977044b372 Mon Sep 17 00:00:00 2001 From: Marius Ungureanu Date: Thu, 15 Sep 2016 12:08:33 +0300 Subject: [PATCH] [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 --- glib/Marshaller.cs | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/glib/Marshaller.cs b/glib/Marshaller.cs index c18c3ba55..ce98a485a 100644 --- a/glib/Marshaller.cs +++ b/glib/Marshaller.cs @@ -76,6 +76,18 @@ public static string FilenamePtrToStringGFree (IntPtr ptr) return ret; } +#if HAVE_NET_4_6 + static bool hasFastGetStringOverload = typeof (System.Text.Encoding).GetMethod ("GetString", new [] { typeof (byte*), typeof (int) }) != null; + static string Utf8PtrToStringFast (IntPtr ptr, int len) + { + unsafe + { + var p = (byte*)ptr; + return System.Text.Encoding.UTF8.GetString (p, len); + } + } +#endif + [DllImport("glibsharpglue-2", CallingConvention=CallingConvention.Cdecl)] static extern UIntPtr glibsharp_strlen (IntPtr mem); public static string Utf8PtrToString (IntPtr ptr) @@ -85,16 +97,12 @@ public static string Utf8PtrToString (IntPtr ptr) int len = (int) (uint)glibsharp_strlen (ptr); #if HAVE_NET_4_6 - unsafe - { - var p = (byte*)ptr; - return System.Text.Encoding.UTF8.GetString (p, len); - } -#else + if (hasFastGetStringOverload) + return Utf8PtrToStringFast (ptr, len); +#endif byte [] bytes = new byte [len]; Marshal.Copy (ptr, bytes, 0, len); return System.Text.Encoding.UTF8.GetString (bytes); -#endif } public static string[] Utf8PtrToString (IntPtr[] ptrs) {