Skip to content

Commit

Permalink
Synced up the HtmlTokenizer with HtmlKit to get the new optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
jstedfast committed Oct 19, 2019
1 parent 99831c3 commit ecfcc83
Show file tree
Hide file tree
Showing 13 changed files with 121 additions and 10 deletions.
1 change: 1 addition & 0 deletions MimeKit/MimeKit.Android.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@
<Compile Include="IO\Filters\TrailingWhitespaceFilter.cs" />
<Compile Include="IO\Filters\Unix2DosFilter.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Text\CharBuffer.cs" />
<Compile Include="Text\FlowedToHtml.cs" />
<Compile Include="Text\FlowedToText.cs" />
<Compile Include="Text\HeaderFooterFormat.cs" />
Expand Down
1 change: 1 addition & 0 deletions MimeKit/MimeKit.Mac.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@
<Compile Include="IO\Filters\TrailingWhitespaceFilter.cs" />
<Compile Include="IO\Filters\Unix2DosFilter.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Text\CharBuffer.cs" />
<Compile Include="Text\FlowedToHtml.cs" />
<Compile Include="Text\FlowedToText.cs" />
<Compile Include="Text\HeaderFooterFormat.cs" />
Expand Down
1 change: 1 addition & 0 deletions MimeKit/MimeKit.Net45.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@
<Compile Include="IO\Filters\TrailingWhitespaceFilter.cs" />
<Compile Include="IO\Filters\Unix2DosFilter.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Text\CharBuffer.cs" />
<Compile Include="Text\FlowedToHtml.cs" />
<Compile Include="Text\FlowedToText.cs" />
<Compile Include="Text\HeaderFooterFormat.cs" />
Expand Down
1 change: 1 addition & 0 deletions MimeKit/MimeKit.NetStandard.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@
<Compile Include="IO\Filters\TrailingWhitespaceFilter.cs" />
<Compile Include="IO\Filters\Unix2DosFilter.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Text\CharBuffer.cs" />
<Compile Include="Text\FlowedToHtml.cs" />
<Compile Include="Text\FlowedToText.cs" />
<Compile Include="Text\HeaderFooterFormat.cs" />
Expand Down
1 change: 1 addition & 0 deletions MimeKit/MimeKit.TvOS.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@
<Compile Include="IO\Filters\TrailingWhitespaceFilter.cs" />
<Compile Include="IO\Filters\Unix2DosFilter.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Text\CharBuffer.cs" />
<Compile Include="Text\FlowedToHtml.cs" />
<Compile Include="Text\FlowedToText.cs" />
<Compile Include="Text\HeaderFooterFormat.cs" />
Expand Down
1 change: 1 addition & 0 deletions MimeKit/MimeKit.WatchOS.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@
<Compile Include="IO\Filters\TrailingWhitespaceFilter.cs" />
<Compile Include="IO\Filters\Unix2DosFilter.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Text\CharBuffer.cs" />
<Compile Include="Text\FlowedToHtml.cs" />
<Compile Include="Text\FlowedToText.cs" />
<Compile Include="Text\HeaderFooterFormat.cs" />
Expand Down
1 change: 1 addition & 0 deletions MimeKit/MimeKit.iOS.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@
<Compile Include="IO\Filters\TrailingWhitespaceFilter.cs" />
<Compile Include="IO\Filters\Unix2DosFilter.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Text\CharBuffer.cs" />
<Compile Include="Text\FlowedToHtml.cs" />
<Compile Include="Text\FlowedToText.cs" />
<Compile Include="Text\HeaderFooterFormat.cs" />
Expand Down
1 change: 1 addition & 0 deletions MimeKit/MimeKitLite.Android.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@
<Compile Include="IO\Filters\TrailingWhitespaceFilter.cs" />
<Compile Include="IO\Filters\Unix2DosFilter.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Text\CharBuffer.cs" />
<Compile Include="Text\FlowedToHtml.cs" />
<Compile Include="Text\FlowedToText.cs" />
<Compile Include="Text\HeaderFooterFormat.cs" />
Expand Down
1 change: 1 addition & 0 deletions MimeKit/MimeKitLite.Net45.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
<Compile Include="IO\Filters\TrailingWhitespaceFilter.cs" />
<Compile Include="IO\Filters\Unix2DosFilter.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Text\CharBuffer.cs" />
<Compile Include="Text\FlowedToHtml.cs" />
<Compile Include="Text\FlowedToText.cs" />
<Compile Include="Text\HeaderFooterFormat.cs" />
Expand Down
1 change: 1 addition & 0 deletions MimeKit/MimeKitLite.NetStandard.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
<Compile Include="IO\Filters\TrailingWhitespaceFilter.cs" />
<Compile Include="IO\Filters\Unix2DosFilter.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Text\CharBuffer.cs" />
<Compile Include="Text\FlowedToHtml.cs" />
<Compile Include="Text\FlowedToText.cs" />
<Compile Include="Text\HeaderFooterFormat.cs" />
Expand Down
1 change: 1 addition & 0 deletions MimeKit/MimeKitLite.iOS.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
<Compile Include="IO\Filters\TrailingWhitespaceFilter.cs" />
<Compile Include="IO\Filters\Unix2DosFilter.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Text\CharBuffer.cs" />
<Compile Include="Text\FlowedToHtml.cs" />
<Compile Include="Text\FlowedToText.cs" />
<Compile Include="Text\HeaderFooterFormat.cs" />
Expand Down
93 changes: 93 additions & 0 deletions MimeKit/Text/CharBuffer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
//
// CharBuffer.cs
//
// Author: Jeffrey Stedfast <jestedfa@microsoft.com>
//
// Copyright (c) 2013-2019 Xamarin Inc. (www.xamarin.com)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//

using System;
using System.Runtime.CompilerServices;

namespace MimeKit.Text {
class CharBuffer
{
char[] buffer;

public CharBuffer (int capacity)
{
buffer = new char[capacity];
}

public int Length {
[MethodImpl (MethodImplOptions.AggressiveInlining)]
get;
[MethodImpl (MethodImplOptions.AggressiveInlining)]
set;
}

public char this[int index] {
[MethodImpl (MethodImplOptions.AggressiveInlining)]
get { return buffer[index]; }
[MethodImpl (MethodImplOptions.AggressiveInlining)]
set { buffer[index] = value; }
}

[MethodImpl (MethodImplOptions.AggressiveInlining)]
void EnsureCapacity (int length)
{
if (length < buffer.Length)
return;

int capacity = buffer.Length << 1;
while (capacity <= length)
capacity <<= 1;

Array.Resize (ref buffer, capacity);
}

[MethodImpl (MethodImplOptions.AggressiveInlining)]
public void Append (char c)
{
EnsureCapacity (Length + 1);
buffer[Length++] = c;
}

[MethodImpl (MethodImplOptions.AggressiveInlining)]
public void Append (string str)
{
EnsureCapacity (Length + str.Length);
str.CopyTo (0, buffer, Length, str.Length);
Length += str.Length;
}

[MethodImpl (MethodImplOptions.AggressiveInlining)]
public override string ToString ()
{
return new string (buffer, 0, Length);
}

public static implicit operator string (CharBuffer buffer)
{
return buffer.ToString ();
}
}
}
27 changes: 17 additions & 10 deletions MimeKit/Text/HtmlTokenizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
//

using System.IO;
using System.Text;
using System.Runtime.CompilerServices;

namespace MimeKit.Text {
/// <summary>
Expand All @@ -40,8 +40,8 @@ public class HtmlTokenizer
const string CData = "[CDATA[";

readonly HtmlEntityDecoder entity = new HtmlEntityDecoder ();
readonly StringBuilder data = new StringBuilder ();
readonly StringBuilder name = new StringBuilder ();
readonly CharBuffer data = new CharBuffer (2048);
readonly CharBuffer name = new CharBuffer (32);
readonly char[] cdata = new char[3];
HtmlDocTypeToken doctype;
HtmlAttribute attribute;
Expand Down Expand Up @@ -231,19 +231,26 @@ protected virtual HtmlAttribute CreateAttribute (string name)
return new HtmlAttribute (name);
}

static bool IsAlphaNumeric (char c)
[MethodImpl (MethodImplOptions.AggressiveInlining)]
static bool IsAlphaNumeric (int c)
{
return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9');
return ((uint) (c - 'A') <= 'Z' - 'A') || ((uint) (c - 'a') <= 'z' - 'a') || ((uint) (c - '0') <= '9' - '0');
}

static bool IsAsciiLetter (char c)
[MethodImpl (MethodImplOptions.AggressiveInlining)]
static bool IsAsciiLetter (int c)
{
return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z');
return ((uint) (c - 'A') <= 'Z' - 'A') || ((uint) (c - 'a') <= 'z' - 'a');
}

static char ToLower (char c)
[MethodImpl (MethodImplOptions.AggressiveInlining)]
static char ToLower (int c)
{
return (c >= 'A' && c <= 'Z') ? (char) (c + 0x20) : c;
// check if the char is within the uppercase range
if ((uint) (c - 'A') <= 'Z' - 'A')
return (char) (c + 0x20);

return (char) c;
}

int Peek ()
Expand Down Expand Up @@ -299,7 +306,7 @@ HtmlToken EmitCommentToken (string comment, bool bogus = false)
return token;
}

HtmlToken EmitCommentToken (StringBuilder comment, bool bogus = false)
HtmlToken EmitCommentToken (CharBuffer comment, bool bogus = false)
{
return EmitCommentToken (comment.ToString (), bogus);
}
Expand Down

0 comments on commit ecfcc83

Please sign in to comment.