+
+#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