@@ -9,7 +9,7 @@ namespace Python.Runtime
99 /// Abstract class defining boiler plate methods that
1010 /// Custom Marshalers will use.
1111 /// </summary>
12- public abstract class MarshalerBase : ICustomMarshaler
12+ internal abstract class MarshalerBase : ICustomMarshaler
1313 {
1414 public object MarshalNativeToManaged ( IntPtr pNativeData )
1515 {
@@ -39,9 +39,9 @@ public int GetNativeDataSize()
3939 /// Custom Marshaler to deal with Managed String to Native
4040 /// conversion differences on UCS2/UCS4.
4141 /// </summary>
42- public class StrMarshaler : MarshalerBase
42+ internal class UcsMarshaler : MarshalerBase
4343 {
44- private static readonly MarshalerBase Instance = new StrMarshaler ( ) ;
44+ private static readonly MarshalerBase Instance = new UcsMarshaler ( ) ;
4545 private static readonly Encoding PyEncoding = Runtime . PyEncoding ;
4646
4747 public override IntPtr MarshalManagedToNative ( object managedObj )
@@ -72,14 +72,41 @@ public static ICustomMarshaler GetInstance(string cookie)
7272 {
7373 return Instance ;
7474 }
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+ }
75102 }
76103
77104
78105 /// <summary>
79106 /// Custom Marshaler to deal with Managed String Arrays to Native
80107 /// conversion differences on UCS2/UCS4.
81108 /// </summary>
82- public class StrArrayMarshaler : MarshalerBase
109+ internal class StrArrayMarshaler : MarshalerBase
83110 {
84111 private static readonly MarshalerBase Instance = new StrArrayMarshaler ( ) ;
85112 private static readonly Encoding PyEncoding = Runtime . PyEncoding ;
@@ -134,7 +161,7 @@ public static ICustomMarshaler GetInstance(string cookie)
134161 /// If instead we used `MarshalAs(UnmanagedType.LPWStr)` the output to
135162 /// `foo` would be `f\x00o\x00o\x00`.
136163 /// </remarks>
137- public class Utf8Marshaler : MarshalerBase
164+ internal class Utf8Marshaler : MarshalerBase
138165 {
139166 private static readonly MarshalerBase Instance = new Utf8Marshaler ( ) ;
140167 private static readonly Encoding PyEncoding = Encoding . UTF8 ;
0 commit comments