|
3 | 3 | using System.Security;
|
4 | 4 | using System.Text;
|
5 | 5 |
|
6 |
| -#if UCS4 |
7 |
| -using Mono.Unix; |
8 |
| -#endif |
9 |
| - |
10 | 6 | namespace Python.Runtime
|
11 | 7 | {
|
12 | 8 | [SuppressUnmanagedCodeSecurity()]
|
@@ -1656,10 +1652,32 @@ internal unsafe static extern IntPtr
|
1656 | 1652 | ExactSpelling = true)]
|
1657 | 1653 | internal unsafe static extern IntPtr
|
1658 | 1654 | PyUnicode_FromKindAndString(int kind,
|
1659 |
| - [MarshalAs(UnmanagedType.CustomMarshaler, |
1660 |
| - MarshalTypeRef = typeof(Utf32Marshaler))] string s, |
| 1655 | + IntPtr s, |
1661 | 1656 | int size);
|
1662 | 1657 |
|
| 1658 | + internal static unsafe IntPtr PyUnicode_FromKindAndString(int kind, |
| 1659 | + string s, |
| 1660 | + int size) |
| 1661 | + { |
| 1662 | + var bufLength = Math.Max(s.Length, size) * 4; |
| 1663 | + |
| 1664 | + IntPtr mem = Marshal.AllocHGlobal(bufLength); |
| 1665 | + try |
| 1666 | + { |
| 1667 | + fixed (char* ps = s) |
| 1668 | + { |
| 1669 | + Encoding.UTF32.GetBytes(ps, s.Length, (byte*)mem, bufLength); |
| 1670 | + } |
| 1671 | + |
| 1672 | + var result = PyUnicode_FromKindAndString(kind, mem, size); |
| 1673 | + return result; |
| 1674 | + } |
| 1675 | + finally |
| 1676 | + { |
| 1677 | + Marshal.FreeHGlobal(mem); |
| 1678 | + } |
| 1679 | + } |
| 1680 | + |
1663 | 1681 | internal static IntPtr PyUnicode_FromUnicode(string s, int size)
|
1664 | 1682 | {
|
1665 | 1683 | return PyUnicode_FromKindAndString(4, s, size);
|
@@ -1702,9 +1720,28 @@ internal unsafe static extern IntPtr
|
1702 | 1720 | EntryPoint = "PyUnicodeUCS4_FromUnicode",
|
1703 | 1721 | ExactSpelling = true)]
|
1704 | 1722 | internal unsafe static extern IntPtr
|
1705 |
| - PyUnicode_FromUnicode( |
1706 |
| - [MarshalAs(UnmanagedType.CustomMarshaler, |
1707 |
| - MarshalTypeRef = typeof(Utf32Marshaler))] string s, int size); |
| 1723 | + PyUnicode_FromUnicode(IntPtr s, int size); |
| 1724 | + |
| 1725 | + internal static unsafe IntPtr PyUnicode_FromUnicode(string s, int size) |
| 1726 | + { |
| 1727 | + var bufLength = Math.Max(s.Length, size) * 4; |
| 1728 | + |
| 1729 | + IntPtr mem = Marshal.AllocHGlobal(bufLength); |
| 1730 | + try |
| 1731 | + { |
| 1732 | + fixed (char* ps = s) |
| 1733 | + { |
| 1734 | + Encoding.UTF32.GetBytes(ps, s.Length, (byte*)mem, bufLength); |
| 1735 | + } |
| 1736 | + |
| 1737 | + var result = PyUnicode_FromUnicode(mem, size); |
| 1738 | + return result; |
| 1739 | + } |
| 1740 | + finally |
| 1741 | + { |
| 1742 | + Marshal.FreeHGlobal(mem); |
| 1743 | + } |
| 1744 | + } |
1708 | 1745 |
|
1709 | 1746 | [DllImport(Runtime.dll, CallingConvention = CallingConvention.Cdecl,
|
1710 | 1747 | EntryPoint = "PyUnicodeUCS4_GetSize",
|
|
0 commit comments