Browse Source

Use custom GetHashCode() implementation in XmlDocumentationProvider as the hash codes may get serialized.

The normal .NET string.GetHashCode() isn't guaranteed to be stable across multiple runs of the program (e.g. with .NET 4.5 hash randomization).
newNRvisualizers
Daniel Grunwald 14 years ago
parent
commit
69c1e6e6de
  1. 21
      ICSharpCode.NRefactory/Documentation/XmlDocumentationProvider.cs

21
ICSharpCode.NRefactory/Documentation/XmlDocumentationProvider.cs

@ -259,13 +259,30 @@ namespace ICSharpCode.NRefactory.Documentation
int pos = linePosMapper.GetPositionForLine(reader.LineNumber) + Math.Max(reader.LinePosition - 2, 0); int pos = linePosMapper.GetPositionForLine(reader.LineNumber) + Math.Max(reader.LinePosition - 2, 0);
string memberAttr = reader.GetAttribute("name"); string memberAttr = reader.GetAttribute("name");
if (memberAttr != null) if (memberAttr != null)
indexList.Add(new IndexEntry(memberAttr.GetHashCode(), pos)); indexList.Add(new IndexEntry(GetHashCode(memberAttr), pos));
reader.Skip(); reader.Skip();
} }
break; break;
} }
} }
} }
/// <summary>
/// Hash algorithm used for the index.
/// This is a custom implementation so that old index files work correctly
/// even when the .NET string.GetHashCode implementation changes
/// (e.g. due to .NET 4.5 hash randomization)
/// </summary>
static int GetHashCode(string key)
{
unchecked {
int h = 0;
foreach (char c in key) {
h = (h << 5) - h + c;
}
return h;
}
}
#endregion #endregion
#region GetDocumentation #region GetDocumentation
@ -277,7 +294,7 @@ namespace ICSharpCode.NRefactory.Documentation
if (key == null) if (key == null)
throw new ArgumentNullException("key"); throw new ArgumentNullException("key");
int hashcode = key.GetHashCode(); int hashcode = GetHashCode(key);
// index is sorted, so we can use binary search // index is sorted, so we can use binary search
int m = Array.BinarySearch(index, new IndexEntry(hashcode, 0)); int m = Array.BinarySearch(index, new IndexEntry(hashcode, 0));
if (m < 0) if (m < 0)

Loading…
Cancel
Save