From 69c1e6e6deeb5b239ec63e3de4a845f37b6aa483 Mon Sep 17 00:00:00 2001 From: Daniel Grunwald Date: Tue, 4 Sep 2012 14:40:01 +0200 Subject: [PATCH] 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). --- .../Documentation/XmlDocumentationProvider.cs | 21 +++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/ICSharpCode.NRefactory/Documentation/XmlDocumentationProvider.cs b/ICSharpCode.NRefactory/Documentation/XmlDocumentationProvider.cs index e16b6c0f03..dd3d325f13 100644 --- a/ICSharpCode.NRefactory/Documentation/XmlDocumentationProvider.cs +++ b/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); string memberAttr = reader.GetAttribute("name"); if (memberAttr != null) - indexList.Add(new IndexEntry(memberAttr.GetHashCode(), pos)); + indexList.Add(new IndexEntry(GetHashCode(memberAttr), pos)); reader.Skip(); } break; } } } + + /// + /// 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) + /// + static int GetHashCode(string key) + { + unchecked { + int h = 0; + foreach (char c in key) { + h = (h << 5) - h + c; + } + return h; + } + } #endregion #region GetDocumentation @@ -277,7 +294,7 @@ namespace ICSharpCode.NRefactory.Documentation if (key == null) throw new ArgumentNullException("key"); - int hashcode = key.GetHashCode(); + int hashcode = GetHashCode(key); // index is sorted, so we can use binary search int m = Array.BinarySearch(index, new IndexEntry(hashcode, 0)); if (m < 0)