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

Commit

Permalink
[Build] Add back runtime checks..
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Therzok committed Sep 15, 2016
1 parent 1f3121d commit ef5993e
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions glib/Marshaller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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) {
Expand Down

0 comments on commit ef5993e

Please sign in to comment.