remove warning
[mono.git] / mcs / class / corlib / System / String.cs
index 476784596e16a3f2fe122fa7bbf971725df7ed04..df5b4451f3e772ea0dca6578dce2014ac27318ec 100644 (file)
@@ -216,12 +216,12 @@ namespace System
                        if (count == 1) 
                                return new String[1] { ToString() };
 
-                       return InternalSplit (separator, count);
+                       return InternalSplit (separator, count, 0);
                }
 
 #if NET_2_0
                [ComVisible (false)]
-               [MonoTODO]
+               [MonoDocumentationNote ("code should be moved to managed")]
                public String[] Split (char[] separator, int count, StringSplitOptions options)
                {
                        if (separator == null || separator.Length == 0)
@@ -232,28 +232,10 @@ namespace System
                        if ((options != StringSplitOptions.None) && (options != StringSplitOptions.RemoveEmptyEntries))
                                throw new ArgumentException ("options must be one of the values in the StringSplitOptions enumeration", "options");
 
-                       bool removeEmpty = (options & StringSplitOptions.RemoveEmptyEntries) == StringSplitOptions.RemoveEmptyEntries;
+                       if (count == 0)
+                               return new string [0];
 
-                       if (!removeEmpty)
-                               return Split (separator, count);
-                       else {
-                               /* FIXME: Optimize this */
-                               String[] res = Split (separator, count);
-                               int n = 0;
-                               for (int i = 0; i < res.Length; ++i)
-                                       if (res [i] == String.Empty)
-                                               n ++;
-                               if (n > 0) {
-                                       String[] arr = new String [res.Length - n];
-                                       int pos = 0;
-                                       for (int i = 0; i < res.Length; ++i)
-                                               if (res [i] != String.Empty)
-                                                       arr [pos ++] = res [i];
-                                       return arr;
-                               }
-                               else
-                                       return res;
-                       }
+                       return InternalSplit (separator, count, (int)options);
                }
 
                [ComVisible (false)]
@@ -582,9 +564,28 @@ namespace System
                        return Compare (this, strB, false);
                }
 
-               public static int CompareOrdinal (String strA, String strB)
+               public static unsafe int CompareOrdinal (String strA, String strB)
                {
-                       return CompareOrdinal (strA, strB, CompareOptions.Ordinal);
+                       if (strA == null) {
+                               if (strB == null)
+                                       return 0;
+                               else
+                                       return -1;
+                       } else if (strB == null) {
+                               return 1;
+                       }
+                       fixed (char* aptr = strA, bptr = strB) {
+                               char* ap = aptr;
+                               char* end = ap + Math.Min (strA.Length, strB.Length);
+                               char* bp = bptr;
+                               while (ap < end) {
+                                       if (*ap != *bp)
+                                               return *ap - *bp;
+                                       ap++;
+                                       bp++;
+                               }
+                               return strA.Length - strB.Length;
+                       }
                }
 
                internal static int CompareOrdinal (String strA, String strB, CompareOptions options)
@@ -594,8 +595,7 @@ namespace System
                                        return 0;
                                else
                                        return -1;
-                       }
-                       else if (strB == null) {
+                       } else if (strB == null) {
                                return 1;
                        }
 
@@ -1174,6 +1174,7 @@ namespace System
                }
 
 #if NET_2_0
+               [ComVisible (false)]
                public bool StartsWith (string value, StringComparison comparisonType)
                {
                        switch (comparisonType) {
@@ -1194,6 +1195,7 @@ namespace System
                        }
                }
 
+               [ComVisible (false)]
                public bool EndsWith (string value, StringComparison comparisonType)
                {
                        switch (comparisonType) {
@@ -2043,6 +2045,7 @@ namespace System
 #if NET_2_0
                [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.MayFail)]
 #endif
+               // When modifying it, GetCaseInsensitiveHashCode() should be modified as well.
                public unsafe override int GetHashCode ()
                {
                        fixed (char * c = this) {
@@ -2060,6 +2063,24 @@ namespace System
                        }
                }
 
+               internal unsafe int GetCaseInsensitiveHashCode ()
+               {
+                       TextInfo ti = CultureInfo.InvariantCulture.TextInfo;
+                       fixed (char * c = this) {
+                               char * cc = c;
+                               char * end = cc + length - 1;
+                               int h = 0;
+                               for (;cc < end; cc += 2) {
+                                       h = (h << 5) - h + ti.ToUpper (*cc);
+                                       h = (h << 5) - h + ti.ToUpper (cc [1]);
+                               }
+                               ++end;
+                               if (cc < end)
+                                       h = (h << 5) - h + ti.ToUpper (*cc);
+                               return h;
+                       }
+               }
+
                // Certain constructors are redirected to CreateString methods with
                // matching argument list. The this pointer should not be used.
 
@@ -2140,6 +2161,98 @@ namespace System
                        return enc.GetString (bytes);
                }
 
+               unsafe string CreateString (char *value)
+               {
+                       if (value == null)
+                               return string.Empty;
+                       char *p = value;
+                       int i = 0;
+                       while (*p != 0) {
+                               ++i;
+                               ++p;
+                       }
+                       string result = InternalAllocateStr (i);
+
+                       if (i != 0) {
+                               fixed (char *dest = result) {
+                                       memcpy ((byte*)dest, (byte*)value, i * 2);
+                               }
+                       }
+                       return result;
+               }
+
+               unsafe string CreateString (char *value, int startIndex, int length)
+               {
+                       if (length == 0)
+                               return string.Empty;
+                       if (value == null)
+                               throw new ArgumentNullException ("value");
+                       if (startIndex < 0)
+                               throw new ArgumentOutOfRangeException ("startIndex");
+                       if (length < 0)
+                               throw new ArgumentOutOfRangeException ("length");
+
+                       string result = InternalAllocateStr (length);
+
+                       fixed (char *dest = result) {
+                               memcpy ((byte*)dest, (byte*)(value + startIndex), length * 2);
+                       }
+                       return result;
+               }
+
+               unsafe string CreateString (char [] val, int startIndex, int length)
+               {
+                       if (val == null)
+                               throw new ArgumentNullException ("val");
+                       if (startIndex < 0)
+                               throw new ArgumentOutOfRangeException ("startIndex");
+                       if (length < 0)
+                               throw new ArgumentOutOfRangeException ("length");
+                       if (startIndex > val.Length - length)
+                               throw new ArgumentOutOfRangeException ("Out of range");
+                       if (length == 0)
+                               return string.Empty;
+
+                       string result = InternalAllocateStr (length);
+
+                       fixed (char *dest = result, src = val) {
+                               memcpy ((byte*)dest, (byte*)(src + startIndex), length * 2);
+                       }
+                       return result;
+               }
+
+               unsafe string CreateString (char [] val)
+               {
+                       if (val == null)
+                               return string.Empty;
+                       if (val.Length == 0)
+                               return string.Empty;
+                       string result = InternalAllocateStr (val.Length);
+
+                       fixed (char *dest = result, src = val) {
+                               memcpy ((byte*)dest, (byte*)src, val.Length * 2);
+                       }
+                       return result;
+               }
+
+               unsafe string CreateString (char c, int count)
+               {
+                       if (count < 0)
+                               throw new ArgumentOutOfRangeException ("count");
+                       if (count == 0)
+                               return string.Empty;
+                       string result = InternalAllocateStr (count);
+                       fixed (char *dest = result) {
+                               char *p = dest;
+                               char *end = p + count;
+                               while (p < end) {
+                                       *p = c;
+                                       p++;
+                               }
+                       }
+                       return result;
+               }
+
                /* helpers used by the runtime as well as above or eslewhere in corlib */
                internal static unsafe void memset (byte *dest, int val, int len)
                {
@@ -2323,9 +2436,6 @@ namespace System
                [MethodImplAttribute (MethodImplOptions.InternalCall)]
                private extern static string InternalJoin (string separator, string[] value, int sIndex, int count);
 
-               //[MethodImplAttribute (MethodImplOptions.InternalCall)]
-               //private extern String InternalReplace (char oldChar, char newChar, int startIndex);
-
                [MethodImplAttribute (MethodImplOptions.InternalCall)]
                private extern String InternalReplace (String oldValue, string newValue, CompareInfo comp);
 
@@ -2333,14 +2443,11 @@ namespace System
                private extern void InternalCopyTo (int sIndex, char[] dest, int destIndex, int count);
 
                [MethodImplAttribute (MethodImplOptions.InternalCall)]
-               private extern String[] InternalSplit (char[] separator, int count);
+               private extern String[] InternalSplit (char[] separator, int count, int options);
 
                [MethodImplAttribute (MethodImplOptions.InternalCall)]
                private extern String InternalTrim (char[] chars, int typ);
 
-               //[MethodImplAttribute (MethodImplOptions.InternalCall)]
-               //private extern int InternalIndexOfAny (char [] arr, int sIndex, int count);
-
                [MethodImplAttribute (MethodImplOptions.InternalCall)]
                private extern int InternalLastIndexOfAny (char [] anyOf, int sIndex, int count);