Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Refactor GetManagedString
  • Loading branch information
vmuriart committed Feb 28, 2017
commit 07f87de2ae0456d7e5c09333a09ab6bbbc11cf1f
34 changes: 22 additions & 12 deletions src/runtime/runtime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1725,7 +1725,20 @@ internal static IntPtr PyUnicode_FromString(string s)
return PyUnicode_FromUnicode(s, (s.Length));
}

internal unsafe static string GetManagedString(IntPtr op)
/// <summary>
/// Function to access the internal PyUnicode/PyString object and
/// convert it to a managed string with the correct encoding.
/// </summary>
/// <remarks>
/// We can't easily do this through through the CustomMarshaler's on
/// the returns because will have access to the IntPtr but not size.
/// <para />
/// For PyUnicodeType, we can't convert with Marshal.PtrToStringUni
/// since it only works for UCS2.
/// </remarks>
/// <param name="op">PyStringType or PyUnicodeType object to convert</param>
/// <returns>Managed String</returns>
internal static string GetManagedString(IntPtr op)
{
IntPtr type = PyObject_TYPE(op);

Expand All @@ -1741,18 +1754,15 @@ internal unsafe static string GetManagedString(IntPtr op)

if (type == Runtime.PyUnicodeType)
{
#if UCS4
IntPtr p = Runtime.PyUnicode_AsUnicode(op);
int length = Runtime.PyUnicode_GetSize(op);
int size = length * 4;
byte[] buffer = new byte[size];
Encoding encoding = UCS == 2 ? Encoding.Unicode : Encoding.UTF32;

IntPtr p = PyUnicode_AsUnicode(op);
int length = PyUnicode_GetSize(op);

int size = length * UCS;
var buffer = new byte[size];
Marshal.Copy(p, buffer, 0, size);
return Encoding.UTF32.GetString(buffer, 0, size);
#elif UCS2
IntPtr p = Runtime.PyUnicode_AsUnicode(op);
int length = Runtime.PyUnicode_GetSize(op);
return Marshal.PtrToStringUni(p, length);
#endif
return encoding.GetString(buffer, 0, size);
}

return null;
Expand Down