[corlib] Fixed StringBuilder construction bugs in marshalling caused by changes to...
[mono.git] / mcs / class / corlib / System / String.cs
index 6a8c9309e71afb01fc0ad431cbe7d8c84cc45c70..ca183385132f872af210c1d7c521b292c80d9680 100644 (file)
@@ -51,6 +51,7 @@ using System.Runtime.ConstrainedExecution;
 using System.Runtime.InteropServices;
 using Mono.Globalization.Unicode;
 
+using System.Diagnostics.Contracts;
 
 namespace System
 {
@@ -66,6 +67,12 @@ namespace System
 
                internal static readonly int LOS_limit = GetLOSLimit ();
 
+               internal static bool LegacyMode {
+                       get {
+                               return false;
+                       }
+               }
+
                public static unsafe bool Equals (string a, string b)
                {
                        if ((a as object) == (b as object))
@@ -3146,6 +3153,11 @@ namespace System
                        memcpy4 ((byte*)dest, (byte*)src, count * 2);
                }
 
+               internal static unsafe void wstrcpy (char *dmem, char *smem, int charCount)
+               {
+                       CharCopy (dmem, smem, charCount);
+               }
+
                internal static unsafe void CharCopyReverse (char *dest, char *src, int count)
                {
                        dest += count;
@@ -3216,5 +3228,51 @@ namespace System
 
                [MethodImplAttribute (MethodImplOptions.InternalCall)]
                private extern static int GetLOSLimit ();
+
+#region "from referencesource" // and actually we replaced some parts.
+
+        // Helper for encodings so they can talk to our buffer directly
+        // stringLength must be the exact size we'll expect
+        [System.Security.SecurityCritical]  // auto-generated
+        unsafe static internal String CreateStringFromEncoding(
+            byte* bytes, int byteLength, Encoding encoding)
+        {
+            Contract.Requires(bytes != null);
+            Contract.Requires(byteLength >= 0);
+
+            // Get our string length
+            int stringLength = encoding.GetCharCount(bytes, byteLength, null);
+            Contract.Assert(stringLength >= 0, "stringLength >= 0");
+            
+            // They gave us an empty string if they needed one
+            // 0 bytelength might be possible if there's something in an encoder
+            if (stringLength == 0)
+                return String.Empty;
+            
+            String s = FastAllocateString(stringLength);
+            fixed(char* pTempChars = &s.start_char)
+            {
+                int doubleCheck = encoding.GetChars(bytes, byteLength, pTempChars, stringLength, null);
+                Contract.Assert(stringLength == doubleCheck, 
+                    "Expected encoding.GetChars to return same length as encoding.GetCharCount");
+            }
+
+            return s;
+        }
+
+               // our own implementation for CLR icall.
+               unsafe internal static int nativeCompareOrdinalIgnoreCaseWC (string name, sbyte *strBBytes)
+               {
+                       for (int i = 0; i < name.Length; i++) {
+                               sbyte b = *(strBBytes + i);
+                               if (b < 0)
+                                       throw new ArgumentException ();
+                               int ret = char.ToUpper ((char) b) - char.ToUpper (name [i]);
+                               if (ret != 0)
+                                       return ret;
+                       }
+                       return 0;
+               }
+#endregion
        }
 }