The Labs \ Source Viewer \ SSCLI \ System.Collections.Specialized \ BackCompatibleStringComparer

  1. //------------------------------------------------------------------------------
  2. // <copyright file="CompatibleComparer.cs" company="Microsoft">
  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. // </copyright>
  14. //------------------------------------------------------------------------------
  15. /*
  16. * This class is used to create hashcodes that are Everett Compatible.
  17. *
  18. * Copyright (c) 2004 Microsoft Corporation
  19. */
  20. namespace System.Collections.Specialized
  21. {
  22.    
  23.     using Microsoft.Win32;
  24.     using System.Collections;
  25.     using System.Runtime.Serialization;
  26.     using System.Globalization;
  27.    
  28.     internal class BackCompatibleStringComparer : IEqualityComparer
  29.     {
  30.        
  31.         static internal IEqualityComparer Default = new BackCompatibleStringComparer();
  32.        
  33.         internal BackCompatibleStringComparer()
  34.         {
  35.         }
  36.        
  37.         //This comes from VS# 434837 and is specifically written to get backcompat
  38.         public static int GetHashCode(string obj)
  39.         {
  40.             unsafe {
  41.                 fixed (char* src = obj) {
  42.                     int hash = 5381;
  43.                     int c;
  44.                     char* szStr = src;
  45.                    
  46.                     while ((c = *szStr) != 0) {
  47.                         hash = ((hash << 5) + hash) ^ c;
  48.                         ++szStr;
  49.                     }
  50.                     return hash;
  51.                 }
  52.             }
  53.         }
  54.        
  55.         bool IEqualityComparer.Equals(object a, object b)
  56.         {
  57.             return Object.Equals(a, b);
  58.         }
  59.        
  60.         public virtual int GetHashCode(object o)
  61.         {
  62.             string obj = o as string;
  63.             if (obj == null) {
  64.                 return o.GetHashCode();
  65.             }
  66.            
  67.             return BackCompatibleStringComparer.GetHashCode(obj);
  68.         }
  69.     }
  70. }

Developer Fusion