'How to improve for loop performance for xml coloring
I am doing xml coloring for attributes, element, value, comment through code.
For this, I am using nested for loops which degrading the performance for large xml files because with this changes, it is checking character by character and due to this it is taking much time to recognize the target area to add coloring.
Here is the code snippet which I written for recognizing xml attributes, values, e.t.c for coloring.
protected override void OnPropertyChanged(string propertyName)
{
#region MyRegion
var pageTextLines = PageText.Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.None);
for (int i = 0; i < pageTextLines.Length; i++)
{
int startIndex = -1;
int endIndex = -1;
var thisLine = pageTextLines[i];
for (int j = 0; j < thisLine.Length; j++)
{
bool isComment = false;
bool isValue = false;
if (thisLine[j] == '<')
startIndex = j;
if (thisLine[j] == '>')
endIndex = j;
// Check if we have something like <!-- to flag that we have a comment
if (thisLine[j] == '-')
{
if (j - 2 >= 0 && j + 1 < pageTextLines.Length)
{
if (thisLine[j - 2] == '<' && thisLine[j - 1] == '!' && thisLine[j + 1] == '-')
{
j = j + 1;
startIndex = j - 2;
isComment = true;
}
}
// Check if we have something like --> to see if we're closing a comment
// or if we're at the end
if (j + 2 < thisLine.Length)
{
if (thisLine[j] == '-')
{
if (thisLine[j + 1] == '-' && thisLine[j + 2] == '>')
{
isComment = true;
j += 2;
endIndex = j;
}
}
}
}
if (startIndex != -1 && endIndex != -1)
{
if (isComment)
{
// Coloring the xml comment.
ProcessDataSelection(SelectionLayer.CommentColor,
new DataSelection(new Point(startIndex + 1, i + 1), new Point(endIndex + 1, i + 1), PositionFormat.Characters, true));
startIndex = endIndex = -1;
}
else
{
// Coloring the xml node.
ProcessDataSelection(SelectionLayer.NodeColor,
new DataSelection(new Point(startIndex + 1, i + 1), new Point(endIndex + 1, i + 1), PositionFormat.Characters, true));
startIndex = endIndex = -1;
for (int k = 0; k < thisLine.Length; k++)
{
if (thisLine[k] == ' ')
startIndex = k;
if (thisLine[k] == '=')
endIndex = k;
if (startIndex != -1 && endIndex != -1)
{
// Coloring the xml attribute.
ProcessDataSelection(SelectionLayer.AttributeColor,
new DataSelection(new Point(startIndex + 1, i + 1), new Point(endIndex + 1, i + 1), PositionFormat.Characters, true));
startIndex = endIndex = -1;
}
}
for (int l = 0; l < thisLine.Length; l++)
{
if (thisLine[l] == '>' && l < thisLine.Length - 1)
{
startIndex = l + 1;
isValue = true;
}
if (thisLine[l] == '<' && thisLine[l + 1] == '/')
{
endIndex = l - 1;
isValue = true;
}
if (startIndex != -1 && endIndex != -1)
{
if (isValue)
// Coloring the xml value.
ProcessDataSelection(SelectionLayer.ValueColor,
new DataSelection(new Point(startIndex + 1, i + 1), new Point(endIndex + 1, i + 1), PositionFormat.Characters, true));
startIndex = endIndex = -1;
}
}
}
startIndex = endIndex = -1;
}
}
#endregion
}
Can anyone please suggest me a better way to do this or better way to improve these loops ?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
