@@ -9,7 +9,7 @@ namespace Python.Runtime
9
9
/// Abstract class defining boiler plate methods that
10
10
/// Custom Marshalers will use.
11
11
/// </summary>
12
- public abstract class MarshalerBase : ICustomMarshaler
12
+ internal abstract class MarshalerBase : ICustomMarshaler
13
13
{
14
14
public object MarshalNativeToManaged ( IntPtr pNativeData )
15
15
{
@@ -39,9 +39,9 @@ public int GetNativeDataSize()
39
39
/// Custom Marshaler to deal with Managed String to Native
40
40
/// conversion differences on UCS2/UCS4.
41
41
/// </summary>
42
- public class StrMarshaler : MarshalerBase
42
+ internal class UcsMarshaler : MarshalerBase
43
43
{
44
- private static readonly MarshalerBase Instance = new StrMarshaler ( ) ;
44
+ private static readonly MarshalerBase Instance = new UcsMarshaler ( ) ;
45
45
private static readonly Encoding PyEncoding = Runtime . PyEncoding ;
46
46
47
47
public override IntPtr MarshalManagedToNative ( object managedObj )
@@ -72,14 +72,41 @@ public static ICustomMarshaler GetInstance(string cookie)
72
72
{
73
73
return Instance ;
74
74
}
75
+
76
+ public static string PtrToStringUni ( IntPtr p )
77
+ {
78
+ if ( p == IntPtr . Zero )
79
+ {
80
+ return null ;
81
+ }
82
+
83
+ int size = GetUnicodeByteLength ( p ) ;
84
+ var buffer = new byte [ size ] ;
85
+ Marshal . Copy ( p , buffer , 0 , size ) ;
86
+ return PyEncoding . GetString ( buffer , 0 , size ) ;
87
+ }
88
+
89
+ public static int GetUnicodeByteLength ( IntPtr p )
90
+ {
91
+ var len = 0 ;
92
+ while ( true )
93
+ {
94
+ int c = Runtime . UCS == 2
95
+ ? Marshal . ReadInt16 ( p , len * 2 )
96
+ : Marshal . ReadInt32 ( p , len * 4 ) ;
97
+
98
+ if ( c == 0 ) return len * Runtime . UCS ;
99
+ checked { ++ len ; }
100
+ }
101
+ }
75
102
}
76
103
77
104
78
105
/// <summary>
79
106
/// Custom Marshaler to deal with Managed String Arrays to Native
80
107
/// conversion differences on UCS2/UCS4.
81
108
/// </summary>
82
- public class StrArrayMarshaler : MarshalerBase
109
+ internal class StrArrayMarshaler : MarshalerBase
83
110
{
84
111
private static readonly MarshalerBase Instance = new StrArrayMarshaler ( ) ;
85
112
private static readonly Encoding PyEncoding = Runtime . PyEncoding ;
@@ -134,7 +161,7 @@ public static ICustomMarshaler GetInstance(string cookie)
134
161
/// If instead we used `MarshalAs(UnmanagedType.LPWStr)` the output to
135
162
/// `foo` would be `f\x00o\x00o\x00`.
136
163
/// </remarks>
137
- public class Utf8Marshaler : MarshalerBase
164
+ internal class Utf8Marshaler : MarshalerBase
138
165
{
139
166
private static readonly MarshalerBase Instance = new Utf8Marshaler ( ) ;
140
167
private static readonly Encoding PyEncoding = Encoding . UTF8 ;
0 commit comments