New test.
[mono.git] / mcs / class / System.XML / System.Xml / NameTable.cs
old mode 100755 (executable)
new mode 100644 (file)
index d4bc31e..6436263
@@ -9,10 +9,32 @@
 // (C) 2003 Ben Maurer
 //
 
+//
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+// 
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+// 
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+//
+
 using System;
 using System.Collections;
 
 namespace System.Xml {
+
        //
        // This class implements the name table as a simple
        // hashtable, using buckets and a linked list.
@@ -51,7 +73,8 @@ namespace System.Xml {
                        
                        int h = 0;
                        // This is from the String.Gethash () icall
-                       for (int i = start; i < len; i++)
+                       int end = start + len;
+                       for (int i = start; i < end; i++)
                                h = (h << 5) - h + key [i];
                        // h must be be >= 0
                        h &= 0x7FFFFFFF;
@@ -66,11 +89,14 @@ namespace System.Xml {
                
                public override string Add (string key)
                {
-                       if (key == String.Empty) return String.Empty;
-                               
+                       if (key == null) throw new ArgumentNullException ("key");
+
+                       int keyLen = key.Length;
+                       if (keyLen == 0) return String.Empty;
+
                        int h = 0;
                        // This is from the String.Gethash () icall
-                       for (int i = 0; i < key.Length; i++)
+                       for (int i = 0; i < keyLen; i++)
                                h = (h << 5) - h + key [i];
                        
                        // h must be be >= 0
@@ -94,7 +120,8 @@ namespace System.Xml {
                        
                        int h = 0;
                        // This is from the String.Gethash () icall
-                       for (int i = start; i < len; i++)
+                       int end = start + len;
+                       for (int i = start; i < end; i++)
                                h = (h << 5) - h + key [i];
                        // h must be be >= 0
                        h &= 0x7FFFFFFF;
@@ -108,11 +135,14 @@ namespace System.Xml {
                }
                
                public override string Get (string value) {
-                       if (value == String.Empty) return value;
-                       
+                       if (value == null) throw new ArgumentNullException ("value");
+
+                       int valLen = value.Length;
+                       if (valLen == 0) return String.Empty;
+                               
                        int h = 0;
                        // This is from the String.Gethash () icall
-                       for (int i = 0; i < value.Length; i++)
+                       for (int i = 0; i < valLen; i++)
                                h = (h << 5) - h + value [i];
                        // h must be be >= 0
                        h &= 0x7FFFFFFF;
@@ -136,7 +166,8 @@ namespace System.Xml {
                                int csub1 = count - 1;
                                
                                Entry [] newBuckets = new Entry [count];
-                               foreach (Entry root in buckets) {
+                               for (int i = 0; i < buckets.Length; i++) {
+                                       Entry root = buckets [i];
                                        Entry e = root;
                                        while (e != null) {
                                                int newLoc = e.hash & csub1;
@@ -155,10 +186,19 @@ namespace System.Xml {
        
                static bool StrEqArray (string str, char [] str2, int start)
                {
-                       for (int i = 0; i < str.Length; i++) {
-                               if (str [i] != str2 [start + i])
+                       int i = str.Length;
+                       i --;
+                       start += i;
+                       do
+                       {
+                               if (str[i] != str2[start])
                                        return false;
+
+                               i--;
+                               start--;
                        }
+                       while(i >= 0);
+
                        return true;
                }
        }