Fix crash in search context generator

This commit is contained in:
Starbeamrainbowlabs 2018-09-25 20:30:39 +01:00
parent feaa5fc2de
commit 64025561e3
Signed by: sbrl
GPG key ID: 1BE5172E637709C2

View file

@ -171,7 +171,8 @@ namespace LibSearchBox
foreach ((int, string) token in tokenizer.IterateTokens())
{
ConcurrentDictionary<int, List<int>> tokenQuery = index.Query(token.Item2);
if (!tokenQuery.ContainsKey(pageId)) continue; // Don't bother if this page doesn't contain this token
if (tokenQuery == null || !tokenQuery.ContainsKey(pageId))
continue; // Don't bother if this page doesn't contain this token
offsets.AddRange(tokenQuery[pageId].Select((int offset) => new SearchOffset(token.Item2, offset)));
}
offsets.Sort((SearchOffset x, SearchOffset y) => x.Offset - y.Offset);
@ -246,9 +247,9 @@ namespace LibSearchBox
);
// Add the separator at the beginning and end if we aren't at the bounds of the source document
if (snippets.First().Item1 > 0)
if (snippets.Count > 0 && snippets.First().Item1 > 0)
snippetsText.Insert(0, "");
if (snippets.Last().Item2 < source.Length)
if (snippets.Count > 0 && snippets.Last().Item2 < source.Length)
snippetsText.Add("");
return string.Join(