We need you! We're working hard on the next version of Developer Fusion - Let us know what you think we should be up to!

The Labs \ Source Viewer \ SSCLI \ System.Resources \ FastResourceComparer

  1. // ==++==
  2. //
  3. //
  4. // Copyright (c) 2006 Microsoft Corporation. All rights reserved.
  5. //
  6. // The use and distribution terms for this software are contained in the file
  7. // named license.txt, which can be found in the root of this distribution.
  8. // By using this software in any fashion, you are agreeing to be bound by the
  9. // terms of this license.
  10. //
  11. // You must not remove this notice, or any other, from this software.
  12. //
  13. //
  14. // ==--==
  15. /*============================================================
  16. **
  17. ** Class:  FastResourceComparer
  18. **
  19. **
  20. ** Purpose: A collection of quick methods for comparing
  21. **          resource keys (strings)
  22. **
  23. **
  24. ===========================================================*/
  25. namespace System.Resources
  26. {
  27.     using System;
  28.     using System.Collections;
  29.     using System.Collections.Generic;
  30.    
  31.     internal sealed class FastResourceComparer : IComparer, IEqualityComparer, IComparer<string>, IEqualityComparer<string>
  32.     {
  33.         static internal readonly FastResourceComparer Default = new FastResourceComparer();
  34.        
  35.         // Implements IHashCodeProvider too, due to Hashtable requirements.
  36.         public int GetHashCode(object key)
  37.         {
  38.             string s = (string)key;
  39.             return FastResourceComparer.HashFunction(s);
  40.         }
  41.        
  42.         public int GetHashCode(string key)
  43.         {
  44.             return FastResourceComparer.HashFunction(key);
  45.         }
  46.        
  47.         // This hash function MUST be publically documented with the resource
  48.         // file format, AND we may NEVER change this hash function's return
  49.         // value (without changing the file format).
  50.         static internal int HashFunction(string key)
  51.         {
  52.             // Never change this hash function. We must standardize it so that
  53.             // others can read & write our .resources files. Additionally, we
  54.             // have a copy of it in InternalResGen as well.
  55.             uint hash = 5381;
  56.             for (int i = 0; i < key.Length; i++)
  57.                 hash = ((hash << 5) + hash) ^ key[i];
  58.             return (int)hash;
  59.         }
  60.        
  61.         // Compares Strings quickly in a case-sensitive way
  62.         public int Compare(object a, object b)
  63.         {
  64.             if (a == b)
  65.                 return 0;
  66.             string sa = (string)a;
  67.             string sb = (string)b;
  68.             return String.CompareOrdinal(sa, sb);
  69.         }
  70.        
  71.         public int Compare(string a, string b)
  72.         {
  73.             return String.CompareOrdinal(a, b);
  74.         }
  75.        
  76.         public bool Equals(string a, string b)
  77.         {
  78.             return String.Equals(a, b);
  79.         }
  80.        
  81.         public new bool Equals(object a, object b)
  82.         {
  83.             if (a == b)
  84.                 return true;
  85.             string sa = (string)a;
  86.             string sb = (string)b;
  87.             return String.Equals(sa, sb);
  88.         }
  89.        
  90.         // Input is one string to compare with, and a byte[] containing chars in
  91.         // little endian unicode. Pass in the number of valid chars.
  92.         unsafe public static int CompareOrdinal(string a, byte[] bytes, int bCharLength)
  93.         {
  94.             BCLDebug.Assert(a != null && bytes != null, "FastResourceComparer::CompareOrdinal must have non-null params");
  95.             BCLDebug.Assert(bCharLength * 2 <= bytes.Length, "FastResourceComparer::CompareOrdinal - numChars is too big!");
  96.             // This is a managed version of strcmp, but I can't take advantage
  97.             // of a terminating 0, unlike strcmp in C.
  98.             int i = 0;
  99.             int r = 0;
  100.             // Compare the min length # of characters, then return length diffs.
  101.             int numChars = a.Length;
  102.             if (numChars > bCharLength)
  103.                 numChars = bCharLength;
  104.             if (bCharLength == 0)
  105.                 // Can't use fixed on a 0-element array.
  106.                 return (a.Length == 0) ? 0 : -1;
  107.             fixed (byte* pb = bytes) {
  108.                
  109.                 byte* pChar = pb;
  110.                 while (i < numChars && r == 0) {
  111.                     // little endian format
  112.                     int b = pChar[0] | pChar[1] << 8;
  113.                     r = a[i++] - b;
  114.                     pChar += sizeof(char);
  115.                 }
  116.             }
  117.             if (r != 0)
  118.                 return r;
  119.             return a.Length - bCharLength;
  120.         }
  121.        
  122.         public static int CompareOrdinal(byte[] bytes, int aCharLength, string b)
  123.         {
  124.             return -CompareOrdinal(b, bytes, aCharLength);
  125.         }
  126.        
  127.         // This method is to handle potentially misaligned data accesses.
  128.         // The byte* must point to little endian Unicode characters.
  129.         unsafe static internal int CompareOrdinal(byte* a, int byteLen, string b)
  130.         {
  131.             BCLDebug.Assert((byteLen & 1) == 0, "CompareOrdinal is expecting a UTF-16 string length, which must be even!");
  132.             BCLDebug.Assert(a != null && b != null, "Null args not allowed.");
  133.             BCLDebug.Assert(byteLen >= 0, "byteLen must be non-negative.");
  134.            
  135.             int r = 0;
  136.             int i = 0;
  137.             // Compare the min length # of characters, then return length diffs.
  138.             int numChars = byteLen >> 1;
  139.             if (numChars > b.Length)
  140.                 numChars = b.Length;
  141.             while (i < numChars && r == 0) {
  142.                 // Must compare character by character, not byte by byte.
  143.                 char aCh = (char)(*a++ | (*a++ << 8));
  144.                 r = aCh - b[i++];
  145.             }
  146.             if (r != 0)
  147.                 return r;
  148.             return byteLen - b.Length * 2;
  149.         }
  150.     }
  151. }

Developer Fusion