Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

String optimizations #122

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/HtmlAgilityPack.Shared/HtmlAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ private string GetRelativeXpath()

i++;
}
return "@" + Name + "[" + i + "]";
return "@" + Name + "[" + i.ToString() + "]";
}

#endregion
Expand Down
9 changes: 5 additions & 4 deletions src/HtmlAgilityPack.Shared/HtmlDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1604,8 +1604,9 @@ private void Parse()
// check buffer end
if ((_currentnode._namelength + 3) <= (Text.Length - (_index - 1)))
{
if (string.Compare(Text.Substring(_index - 1, _currentnode._namelength + 2),
"</" + _currentnode.Name, StringComparison.OrdinalIgnoreCase) == 0)
if (Text[_index - 1] == '<' &&
Text[_index] == '/' &&
string.Compare(Text, _index + 1, _currentnode.Name, 0, _currentnode._namelength, StringComparison.OrdinalIgnoreCase) == 0)
{
int c = Text[_index - 1 + 2 + _currentnode.Name.Length];
if ((c == '>') || (IsWhiteSpace(c)))
Expand Down Expand Up @@ -1850,7 +1851,7 @@ private bool PushNodeEnd(int index, bool close)
if ((close) || (!_currentnode._starttag))
{
if ((OptionStopperNodeName != null) && (_remainder == null) &&
(string.Compare(_currentnode.Name, OptionStopperNodeName, StringComparison.OrdinalIgnoreCase) == 0))
(string.Equals(_currentnode.Name, OptionStopperNodeName, StringComparison.OrdinalIgnoreCase)))
{
_remainderOffset = index;
_remainder = Text.Substring(_remainderOffset);
Expand Down Expand Up @@ -1904,7 +1905,7 @@ private void ReadDocumentEncoding(HtmlNode node)
HtmlAttribute att = node.Attributes["http-equiv"];
if (att == null)
return;
if (string.Compare(att.Value, "content-type", StringComparison.OrdinalIgnoreCase) != 0)
if (!string.Equals(att.Value, "content-type", StringComparison.OrdinalIgnoreCase))
return;
HtmlAttribute content = node.Attributes["content"];
if (content != null)
Expand Down
25 changes: 18 additions & 7 deletions src/HtmlAgilityPack.Shared/HtmlEntity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -646,7 +646,9 @@ public static string DeEntitize(string text)
}
catch
{
sb.Append("&#" + e + ";");
sb.Append("&#")
.Append(e)
.Append(';');
}
}
else
Expand All @@ -656,7 +658,9 @@ public static string DeEntitize(string text)
if (!_entityValue.TryGetValue(entity.ToString(), out code))
{
// nope
sb.Append("&" + entity + ";");
sb.Append('&')
.Append(entity)
.Append(';');
}
else
{
Expand All @@ -671,7 +675,8 @@ public static string DeEntitize(string text)

case '&':
// new entity start without end, it was not an entity...
sb.Append("&" + entity);
sb.Append('&')
.Append(entity);
entity.Remove(0, entity.Length);
break;

Expand All @@ -681,7 +686,8 @@ public static string DeEntitize(string text)
{
// unknown stuff, just don't touch it
state = ParseState.Text;
sb.Append("&" + entity);
sb.Append('&')
.Append(entity);
entity.Remove(0, entity.Length);
}
break;
Expand All @@ -693,7 +699,8 @@ public static string DeEntitize(string text)
// finish the work
if (state == ParseState.EntityStart)
{
sb.Append("&" + entity);
sb.Append('&')
.Append(entity);
}
return sb.ToString();
}
Expand Down Expand Up @@ -784,11 +791,15 @@ public static string Entitize(string text, bool useNames, bool entitizeQuotAmpAn

if ((entity == null) || (!useNames))
{
sb.Append("&#" + code + ";");
sb.Append("&#")
.Append(code)
.Append(';');
}
else
{
sb.Append("&" + entity + ";");
sb.Append('&')
.Append(entity)
.Append(';');
}
}
else
Expand Down
6 changes: 3 additions & 3 deletions src/HtmlAgilityPack.Shared/HtmlNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1548,7 +1548,7 @@ public void WriteTo(TextWriter outText, int level=0)
if (_ownerdocument.OptionOutputAsXml)
{
var commentNode = (HtmlCommentNode)this;
if (!_ownerdocument.BackwardCompatibility && commentNode.Comment.ToLowerInvariant().StartsWith("<!doctype"))
if (!_ownerdocument.BackwardCompatibility && commentNode.Comment.StartsWith("<!doctype", StringComparison.OrdinalIgnoreCase))
{
outText.Write(commentNode.Comment);
}
Expand Down Expand Up @@ -1949,7 +1949,7 @@ internal void WriteAttributes(TextWriter outText, bool closing)
int i = 0;
foreach (HtmlNode n in ChildNodes)
{
WriteAttribute(outText, _ownerdocument.CreateAttribute("_child_" + i,
WriteAttribute(outText, _ownerdocument.CreateAttribute("_child_" + i.ToString(),
n.Name));
i++;
}
Expand Down Expand Up @@ -1990,7 +1990,7 @@ private string GetRelativeXpath()

i++;
}
return Name + "[" + i + "]";
return Name + "[" + i.ToString() + "]";
}

private bool IsSingleElementNode()
Expand Down
28 changes: 14 additions & 14 deletions src/HtmlAgilityPack.Shared/HtmlNodeNavigator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ public override bool HasAttributes
{
get
{
InternalTrace(">" + (_currentnode.Attributes.Count > 0));
InternalTrace(">" + (_currentnode.Attributes.Count > 0).ToString());
return (_currentnode.Attributes.Count > 0);
}
}
Expand All @@ -247,7 +247,7 @@ public override bool HasChildren
{
get
{
InternalTrace(">" + (_currentnode.ChildNodes.Count > 0));
InternalTrace(">" + (_currentnode.ChildNodes.Count > 0).ToString());
return (_currentnode.ChildNodes.Count > 0);
}
}
Expand All @@ -259,7 +259,7 @@ public override bool IsEmptyElement
{
get
{
InternalTrace(">" + !HasChildren);
InternalTrace(">" + (!HasChildren).ToString());
// REVIEW: is this ok?
return !HasChildren;
}
Expand Down Expand Up @@ -329,31 +329,31 @@ public override XPathNodeType NodeType
switch (_currentnode.NodeType)
{
case HtmlNodeType.Comment:
InternalTrace(">" + XPathNodeType.Comment);
InternalTrace(">" + XPathNodeType.Comment.ToString());
return XPathNodeType.Comment;

case HtmlNodeType.Document:
InternalTrace(">" + XPathNodeType.Root);
InternalTrace(">" + XPathNodeType.Root.ToString());
return XPathNodeType.Root;

case HtmlNodeType.Text:
InternalTrace(">" + XPathNodeType.Text);
InternalTrace(">" + XPathNodeType.Text.ToString());
return XPathNodeType.Text;

case HtmlNodeType.Element:
{
if (_attindex != -1)
{
InternalTrace(">" + XPathNodeType.Attribute);
InternalTrace(">" + XPathNodeType.Attribute.ToString());
return XPathNodeType.Attribute;
}
InternalTrace(">" + XPathNodeType.Element);
InternalTrace(">" + XPathNodeType.Element.ToString());
return XPathNodeType.Element;
}

default:
throw new NotImplementedException("Internal error: Unhandled HtmlNodeType: " +
_currentnode.NodeType);
_currentnode.NodeType.ToString());
}
}
}
Expand All @@ -378,7 +378,7 @@ public override string Value
{
get
{
InternalTrace("nt=" + _currentnode.NodeType);
InternalTrace("nt=" + _currentnode.NodeType.ToString());
switch (_currentnode.NodeType)
{
case HtmlNodeType.Comment:
Expand All @@ -405,7 +405,7 @@ public override string Value

default:
throw new NotImplementedException("Internal error: Unhandled HtmlNodeType: " +
_currentnode.NodeType);
_currentnode.NodeType.ToString());
}
}
}
Expand Down Expand Up @@ -481,7 +481,7 @@ public override bool IsSamePosition(XPathNavigator other)
InternalTrace(">false");
return false;
}
InternalTrace(">" + (nav._currentnode == _currentnode));
InternalTrace(">" + (nav._currentnode == _currentnode).ToString());
return (nav._currentnode == _currentnode);
}

Expand All @@ -498,9 +498,9 @@ public override bool MoveTo(XPathNavigator other)
InternalTrace(">false (nav is not an HtmlNodeNavigator)");
return false;
}
InternalTrace("moveto oid=" + nav.GetHashCode()
InternalTrace("moveto oid=" + nav.GetHashCode().ToString()
+ ", n:" + nav._currentnode.Name
+ ", a:" + nav._attindex);
+ ", a:" + nav._attindex.ToString());

if (nav._doc == _doc)
{
Expand Down
40 changes: 20 additions & 20 deletions src/HtmlAgilityPack.Shared/HtmlWeb.cs
Original file line number Diff line number Diff line change
Expand Up @@ -927,9 +927,7 @@ public static string GetContentTypeForExtension(string extension, string def)
string contentType = "";
if (!helper.GetIsRegistryAvailable())
{
if (MimeTypes.ContainsKey(extension))
contentType = MimeTypes[extension];
else
if (!MimeTypes.TryGetValue(extension, out contentType))
contentType = def;
}

Expand Down Expand Up @@ -1648,9 +1646,6 @@ private HttpStatusCode Get(Uri uri, string method, string path, HtmlDocument doc
_requestDuration = Environment.TickCount - tc;
_responseUri = resp.ResponseUri;

bool html = IsHtmlContent(resp.ContentType);
bool isUnknown = string.IsNullOrEmpty(resp.ContentType);

Encoding respenc = !string.IsNullOrEmpty(resp.ContentEncoding)
? Encoding.GetEncoding(resp.ContentEncoding)
: null;
Expand Down Expand Up @@ -1718,6 +1713,10 @@ private HttpStatusCode Get(Uri uri, string method, string path, HtmlDocument doc
else
{
// try to work in-memory

bool html = IsHtmlContent(resp.ContentType);
bool isUnknown = string.IsNullOrEmpty(resp.ContentType);

if (doc != null && html)
{
if (respenc == null)
Expand Down Expand Up @@ -1876,14 +1875,6 @@ private HttpStatusCode Get(Uri uri, string method, string path, HtmlDocument doc
_requestDuration = Environment.TickCount - tc;
_responseUri = response.RequestMessage.RequestUri;

bool isUnknown = response.Content.Headers.ContentType == null;
bool html = !isUnknown && IsHtmlContent(response.Content.Headers.ContentType.MediaType);

var encoding = response.Content.Headers.ContentEncoding.FirstOrDefault();
Encoding respenc = !string.IsNullOrEmpty(encoding)
? Encoding.GetEncoding(encoding)
: null;

if(CaptureRedirect)
{
// Found == 302
Expand Down Expand Up @@ -1936,6 +1927,15 @@ private HttpStatusCode Get(Uri uri, string method, string path, HtmlDocument doc
else
{
// try to work in-memory

bool isUnknown = response.Content.Headers.ContentType == null;
bool html = !isUnknown && IsHtmlContent(response.Content.Headers.ContentType.MediaType);

var encoding = response.Content.Headers.ContentEncoding.FirstOrDefault();
Encoding respenc = !string.IsNullOrEmpty(encoding)
? Encoding.GetEncoding(encoding)
: null;

if ((doc != null) && (html))
{
if (respenc != null)
Expand Down Expand Up @@ -2011,14 +2011,14 @@ private bool IsCacheHtmlContent(string path)
}
#endif

private bool IsHtmlContent(string contentType)
private static bool IsHtmlContent(string contentType)
{
return contentType.ToLower().StartsWith("text/html");
return contentType.StartsWith("text/html", StringComparison.OrdinalIgnoreCase);
}

private bool IsGZipEncoding(string contentEncoding)
private static bool IsGZipEncoding(string contentEncoding)
{
return contentEncoding.ToLower().StartsWith("gzip");
return contentEncoding.StartsWith("gzip", StringComparison.OrdinalIgnoreCase);
}

#if !NETSTANDARD
Expand Down Expand Up @@ -2062,7 +2062,7 @@ private void SaveCacheHeaders(Uri requestUri, HttpWebResponse resp)
XmlNode cache = doc.FirstChild;
foreach (string header in resp.Headers)
{
XmlNode entry = doc.CreateElement("h");
XmlElement entry = doc.CreateElement("h");
XmlAttribute att = doc.CreateAttribute("n");
att.Value = header;
entry.Attributes.Append(att);
Expand All @@ -2087,7 +2087,7 @@ private void SaveCacheHeaders(Uri requestUri, HttpResponseMessage resp)
XmlNode cache = doc.FirstChild;
foreach (var header in resp.Headers)
{
XmlNode entry = doc.CreateElement("h");
XmlElement entry = doc.CreateElement("h");
XmlAttribute att = doc.CreateAttribute("n");
att.Value = header.Key;
entry.Attributes.Append(att);
Expand Down
4 changes: 2 additions & 2 deletions src/HtmlAgilityPack.Shared/MixedCodeDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ private void Parse()
case ParseState.Text:
if (_index + TokenCodeStart.Length < _text.Length)
{
if (_text.Substring(_index - 1, TokenCodeStart.Length) == TokenCodeStart)
if (string.CompareOrdinal(_text, _index - 1, TokenCodeStart, 0, TokenCodeStart.Length) == 0)
{
_state = ParseState.Code;
_currentfragment.Length = _index - 1 - _currentfragment.Index;
Expand All @@ -449,7 +449,7 @@ private void Parse()
case ParseState.Code:
if (_index + TokenCodeEnd.Length < _text.Length)
{
if (_text.Substring(_index - 1, TokenCodeEnd.Length) == TokenCodeEnd)
if (string.CompareOrdinal(_text, _index - 1, TokenCodeEnd, 0, TokenCodeEnd.Length) == 0)
{
_state = ParseState.Text;
_currentfragment.Length = _index + TokenCodeEnd.Length - _currentfragment.Index;
Expand Down