Skip to content

Commit

Permalink
Adding extensions used for the .NET Runtime Compiler (v1.1)
Browse files Browse the repository at this point in the history
  • Loading branch information
zzzprojects committed Aug 3, 2015
1 parent 61188c6 commit 5ef1cea
Show file tree
Hide file tree
Showing 32 changed files with 1,833 additions and 0 deletions.
56 changes: 56 additions & 0 deletions src/Z.Core/System.Text.StringBuilder/Extract/ExtractChar.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using System;
using System.Text;

public static partial class Extensions
{
/// <summary>A StringBuilder extension method that extracts the character described by @this.</summary>
/// <param name="this">The @this to act on.</param>
/// <returns>The extracted character.</returns>
public static char ExtractChar(this StringBuilder @this)
{
return @this.ExtractChar(0);
}

/// <summary>A StringBuilder extension method that extracts the character described by @this.</summary>
/// <param name="this">The @this to act on.</param>
/// <param name="endIndex">[out] The end index.</param>
/// <returns>The extracted character.</returns>
public static char ExtractChar(this StringBuilder @this, out int endIndex)
{
return @this.ExtractChar(0, out endIndex);
}

/// <summary>A StringBuilder extension method that extracts the character described by @this.</summary>
/// <param name="this">The @this to act on.</param>
/// <param name="startIndex">The start index.</param>
/// <returns>The extracted character.</returns>
public static char ExtractChar(this StringBuilder @this, int startIndex)
{
int endIndex;
return @this.ExtractChar(startIndex, out endIndex);
}

/// <summary>A StringBuilder extension method that extracts the character described by @this.</summary>
/// <exception cref="Exception">Thrown when an exception error condition occurs.</exception>
/// <param name="this">The @this to act on.</param>
/// <param name="startIndex">The start index.</param>
/// <param name="endIndex">[out] The end index.</param>
/// <returns>The extracted character.</returns>
public static char ExtractChar(this StringBuilder @this, int startIndex, out int endIndex)
{
if (@this.Length > startIndex + 1)
{
var ch1 = @this[startIndex];
var ch2 = @this[startIndex + 1];
var ch3 = @this[startIndex + 2];

if (ch1 == '\'' && ch3 == '\'')
{
endIndex = startIndex + 2;
return ch2;
}
}

throw new Exception("Invalid char at position: " + startIndex);
}
}
64 changes: 64 additions & 0 deletions src/Z.Core/System.Text.StringBuilder/Extract/ExtractComment.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using System.Text;

public static partial class Extensions
{
/// <summary>A StringBuilder extension method that extracts the comment described by @this.</summary>
/// <param name="this">The @this to act on.</param>
/// <returns>The extracted comment.</returns>
public static StringBuilder ExtractComment(this StringBuilder @this)
{
return @this.ExtractComment(0);
}

/// <summary>A StringBuilder extension method that extracts the comment described by @this.</summary>
/// <param name="this">The @this to act on.</param>
/// <param name="endIndex">[out] The end index.</param>
/// <returns>The extracted comment.</returns>
public static StringBuilder ExtractComment(this StringBuilder @this, out int endIndex)
{
return @this.ExtractComment(0, out endIndex);
}

/// <summary>A StringBuilder extension method that extracts the comment described by @this.</summary>
/// <param name="this">The @this to act on.</param>
/// <param name="startIndex">The start index.</param>
/// <returns>The extracted comment.</returns>
public static StringBuilder ExtractComment(this StringBuilder @this, int startIndex)
{
int endIndex;
return @this.ExtractComment(startIndex, out endIndex);
}

/// <summary>A StringBuilder extension method that extracts the comment described by @this.</summary>
/// <param name="this">The @this to act on.</param>
/// <param name="startIndex">The start index.</param>
/// <param name="endIndex">[out] The end index.</param>
/// <returns>The extracted comment.</returns>
public static StringBuilder ExtractComment(this StringBuilder @this, int startIndex, out int endIndex)
{
if (@this.Length > startIndex + 1)
{
var ch1 = @this[startIndex];
var ch2 = @this[startIndex + 1];

if (ch1 == '/' && ch2 == '/')
{
// Single line comment

return @this.ExtractCommentSingleLine(startIndex, out endIndex);
}

if (ch1 == '/' && ch2 == '*')
{
/*
* Multi-line comment
*/

return @this.ExtractCommentMultiLine(startIndex, out endIndex);
}
}

endIndex = -1;
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
using System.Text;

public static partial class Extensions
{
/// <summary>
/// A StringBuilder extension method that extracts the comment multi line described by
/// @this.
/// </summary>
/// <param name="this">The @this to act on.</param>
/// <returns>The extracted comment multi line.</returns>
public static StringBuilder ExtractCommentMultiLine(this StringBuilder @this)
{
return @this.ExtractCommentMultiLine(0);
}

/// <summary>
/// A StringBuilder extension method that extracts the comment multi line described by
/// @this.
/// </summary>
/// <param name="this">The @this to act on.</param>
/// <param name="endIndex">[out] The end index.</param>
/// <returns>The extracted comment multi line.</returns>
public static StringBuilder ExtractCommentMultiLine(this StringBuilder @this, out int endIndex)
{
return @this.ExtractCommentMultiLine(0, out endIndex);
}

/// <summary>
/// A StringBuilder extension method that extracts the comment multi line described by
/// @this.
/// </summary>
/// <param name="this">The @this to act on.</param>
/// <param name="startIndex">The start index.</param>
/// <returns>The extracted comment multi line.</returns>
public static StringBuilder ExtractCommentMultiLine(this StringBuilder @this, int startIndex)
{
int endIndex;
return @this.ExtractCommentMultiLine(startIndex, out endIndex);
}

/// <summary>
/// A StringBuilder extension method that extracts the comment multi line described by
/// @this.
/// </summary>
/// <param name="this">The @this to act on.</param>
/// <param name="startIndex">The start index.</param>
/// <param name="endIndex">[out] The end index.</param>
/// <returns>The extracted comment multi line.</returns>
public static StringBuilder ExtractCommentMultiLine(this StringBuilder @this, int startIndex, out int endIndex)
{
var sb = new StringBuilder();

if (@this.Length > startIndex + 1)
{
var ch1 = @this[startIndex];
var ch2 = @this[startIndex + 1];

if (ch1 == '/' && ch2 == '*')
{
/*
* Multi-line comment
*/

sb.Append(ch1);
sb.Append(ch2);
var pos = startIndex + 2;

while (pos < @this.Length)
{
var ch = @this[pos];
pos++;

if (ch == '*' && pos < @this.Length && @this[pos] == '/')
{
sb.Append(ch);
sb.Append(@this[pos]);
endIndex = pos;
return sb;
}

sb.Append(ch);
}

endIndex = pos;
return sb;
}
}

endIndex = -1;
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
using System.Text;

public static partial class Extensions
{
/// <summary>
/// A StringBuilder extension method that extracts the comment single line described by
/// @this.
/// </summary>
/// <param name="this">The @this to act on.</param>
/// <returns>The extracted comment single line.</returns>
public static StringBuilder ExtractCommentSingleLine(this StringBuilder @this)
{
return @this.ExtractCommentSingleLine(0);
}

/// <summary>
/// A StringBuilder extension method that extracts the comment single line described by
/// @this.
/// </summary>
/// <param name="this">The @this to act on.</param>
/// <param name="endIndex">[out] The end index.</param>
/// <returns>The extracted comment single line.</returns>
public static StringBuilder ExtractCommentSingleLine(this StringBuilder @this, out int endIndex)
{
return @this.ExtractCommentSingleLine(0, out endIndex);
}

/// <summary>
/// A StringBuilder extension method that extracts the comment single line described by
/// @this.
/// </summary>
/// <param name="this">The @this to act on.</param>
/// <param name="startIndex">The start index.</param>
/// <returns>The extracted comment single line.</returns>
public static StringBuilder ExtractCommentSingleLine(this StringBuilder @this, int startIndex)
{
int endIndex;
return @this.ExtractCommentSingleLine(startIndex, out endIndex);
}

/// <summary>
/// A StringBuilder extension method that extracts the comment single line described by
/// @this.
/// </summary>
/// <param name="this">The @this to act on.</param>
/// <param name="startIndex">The start index.</param>
/// <param name="endIndex">[out] The end index.</param>
/// <returns>The extracted comment single line.</returns>
public static StringBuilder ExtractCommentSingleLine(this StringBuilder @this, int startIndex, out int endIndex)
{
var sb = new StringBuilder();

if (@this.Length > startIndex + 1)
{
var ch1 = @this[startIndex];
var ch2 = @this[startIndex + 1];

if (ch1 == '/' && ch2 == '/')
{
// Single line comment

sb.Append(ch1);
sb.Append(ch2);
var pos = startIndex + 2;

while (pos < @this.Length)
{
var ch = @this[pos];
pos++;

if (ch == '\r' && pos < @this.Length && @this[pos] == '\n')
{
endIndex = pos - 1;
return sb;
}

sb.Append(ch);
}

endIndex = pos;
return sb;
}
}

endIndex = -1;
return null;
}
}
90 changes: 90 additions & 0 deletions src/Z.Core/System.Text.StringBuilder/Extract/ExtractHexadecimal.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
using System.Text;

public static partial class Extensions
{
/// <summary>A StringBuilder extension method that extracts the hexadecimal described by @this.</summary>
/// <param name="this">The @this to act on.</param>
/// <returns>The extracted hexadecimal.</returns>
public static StringBuilder ExtractHexadecimal(this StringBuilder @this)
{
return @this.ExtractHexadecimal(0);
}

/// <summary>A StringBuilder extension method that extracts the hexadecimal described by @this.</summary>
/// <param name="this">The @this to act on.</param>
/// <param name="endIndex">[out] The end index.</param>
/// <returns>The extracted hexadecimal.</returns>
public static StringBuilder ExtractHexadecimal(this StringBuilder @this, out int endIndex)
{
return @this.ExtractHexadecimal(0, out endIndex);
}

/// <summary>A StringBuilder extension method that extracts the hexadecimal described by @this.</summary>
/// <param name="this">The @this to act on.</param>
/// <param name="startIndex">The start index.</param>
/// <returns>The extracted hexadecimal.</returns>
public static StringBuilder ExtractHexadecimal(this StringBuilder @this, int startIndex)
{
int endIndex;
return @this.ExtractHexadecimal(startIndex, out endIndex);
}

/// <summary>A StringBuilder extension method that extracts the hexadecimal described by @this.</summary>
/// <param name="this">The @this to act on.</param>
/// <param name="startIndex">The start index.</param>
/// <param name="endIndex">[out] The end index.</param>
/// <returns>The extracted hexadecimal.</returns>
public static StringBuilder ExtractHexadecimal(this StringBuilder @this, int startIndex, out int endIndex)
{
// WARNING: This method support all kind of suffix for .NET Runtime Compiler
// An operator can be any sequence of supported operator character

if (startIndex + 1 < @this.Length && @this[startIndex] == '0'
&& (@this[startIndex + 1] == 'x' || @this[startIndex + 1] == 'X'))
{
var sb = new StringBuilder();

var hasNumber = false;
var hasSuffix = false;

sb.Append(@this[startIndex]);
sb.Append(@this[startIndex + 1]);

var pos = startIndex + 2;

while (pos < @this.Length)
{
var ch = @this[pos];
pos++;

if (((ch >= '0' && ch <= '9')
|| (ch >= 'a' && ch <= 'f')
|| (ch >= 'A' && ch <= 'F'))
&& !hasSuffix)
{
hasNumber = true;
sb.Append(ch);
}
else if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))
{
hasSuffix = true;
sb.Append(ch);
}
else
{
pos -= 2;
break;
}
}

if (hasNumber)
{
endIndex = pos;
return sb;
}
}

endIndex = -1;
return null;
}
}
Loading

0 comments on commit 5ef1cea

Please sign in to comment.