private ScanResultWord GetNextWord(string Text, Span currentSpan, int
StartPos, ref bool HasComplex)
{
SpanDefinition spanDefinition = currentSpan.spanDefinition;
#region ComplexFind
int BestComplexPos = - 1;
Pattern BestComplexPattern = null;
string BestComplexToken = "";
var complexword = new ScanResultWord();
if (HasComplex)
{
foreach (Pattern pattern in spanDefinition.ComplexPatterns)
{
PatternScanResult scanres = pattern.IndexIn(Text, StartPos,
pattern.Parent.CaseSensitive, Separators);
if (scanres.Token != "")
{
if (scanres.Index < BestComplexPos || BestComplexPos == - 1)
{
BestComplexPos = scanres.Index;
BestComplexPattern = pattern;
BestComplexToken = scanres.Token;
}
}
}
if (BestComplexPattern != null)
{
complexword.HasContent = true;
complexword.ParentList = BestComplexPattern.Parent;
complexword.Pattern = BestComplexPattern;
complexword.Position = BestComplexPos;
complexword.Token = BestComplexToken;
}
else
{
HasComplex = false;
}
}
#endregion
#region SimpleFind
var simpleword = new ScanResultWord();
for (int i = StartPos; i < Text.Length; i++)
{
//bailout if we found a complex pattern before this char pos
if (i > complexword.Position && complexword.HasContent)
break;
#region 3+ char pattern
if (i <= Text.Length - 3)
{
string key = Text.Substring(i, 3).ToLowerInvariant();
var patterns2 = (PatternCollection)
spanDefinition.LookupTable[key];
//ok , there are patterns that start with this char
if (patterns2 != null)
{
foreach (Pattern pattern in patterns2)
{
int len = pattern.StringPattern.Length;
if (i + len > Text.Length)
continue;
char lastpatternchar = char.ToLower(pattern.StringPattern[len -
1]);
char lasttextchar = char.ToLower(Text[i + len - 1]);
#region Case Insensitive
if (lastpatternchar == lasttextchar)
{
if (!pattern.IsKeyword || (pattern.IsKeyword &&
pattern.HasSeparators(Text, i)))
{
if (!pattern.Parent.CaseSensitive)
{
string s = Text.Substring(i, len).ToLowerInvariant();
if (s == pattern.StringPattern.ToLowerInvariant())
{
simpleword.HasContent = true;
simpleword.ParentList = pattern.Parent;
simpleword.Pattern = pattern;
simpleword.Position = i;
simpleword.Token = pattern.Parent.NormalizeCase ? pattern.StringPattern : Text.Substring(i, len);
break;
}
}
else
{
string s = Text.Substring(i, len);
if (s == pattern.StringPattern)
{
simpleword.HasContent = true;
simpleword.ParentList = pattern.Parent;
simpleword.Pattern = pattern;
simpleword.Position = i;
simpleword.Token = pattern.StringPattern;
break;
}
}
}
}
}
#endregion
}
}
#endregion
if (simpleword.HasContent)
break;
#region single char pattern
char c = Text[i];
var patterns = (PatternCollection) spanDefinition.LookupTable[c];
if (patterns != null)
{
//ok , there are patterns that start with this char
foreach (Pattern pattern in patterns)
{
int len = pattern.StringPattern.Length;
if (i + len > Text.Length)
continue;
char lastpatternchar = pattern.StringPattern[len - 1];
char lasttextchar = Text[i + len - 1];
if (!pattern.Parent.CaseSensitive)
{
#region Case Insensitive
if (char.ToLower(lastpatternchar) == char.ToLower(lasttextchar))
{
if (!pattern.IsKeyword || (pattern.IsKeyword &&
pattern.HasSeparators(Text, i)))
{
string s = Text.Substring(i, len).ToLowerInvariant();
if (s == pattern.StringPattern.ToLowerInvariant())
{
simpleword.HasContent = true;
simpleword.ParentList = pattern.Parent;
simpleword.Pattern = pattern;
simpleword.Position = i;
simpleword.Token = pattern.Parent.NormalizeCase ? pattern.StringPattern : Text.Substring(i, len);
break;
}
}
}
#endregion
}
else
{
#region Case Sensitive
if (lastpatternchar == lasttextchar)
{
if (!pattern.IsKeyword || (pattern.IsKeyword &&
pattern.HasSeparators(Text, i)))
{
string s = Text.Substring(i, len);
if (s == pattern.StringPattern)
{
simpleword.HasContent = true;
simpleword.ParentList = pattern.Parent;
simpleword.Pattern = pattern;
simpleword.Position = i;
simpleword.Token = pattern.StringPattern;
break;
}
}
}
#endregion
}
}
if (simpleword.HasContent)
break;
}
#endregion
}
#endregion
if (complexword.HasContent && simpleword.HasContent)
{
if (simpleword.Position == complexword.Position)
{
if (simpleword.Token.Length >= complexword.Token.Length)
return simpleword;
return complexword;
}
if (simpleword.Position < complexword.Position)
return simpleword;
if (simpleword.Position > complexword.Position)
return complexword;
}
if (simpleword.HasContent)
return simpleword;
if (complexword.HasContent)
return complexword;
return new ScanResultWord();
}