Skip to content

Commit 5ef1cea

Browse files
author
zzzprojects
committed
Adding extensions used for the .NET Runtime Compiler (v1.1)
1 parent 61188c6 commit 5ef1cea

32 files changed

+1833
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
using System;
2+
using System.Text;
3+
4+
public static partial class Extensions
5+
{
6+
/// <summary>A StringBuilder extension method that extracts the character described by @this.</summary>
7+
/// <param name="this">The @this to act on.</param>
8+
/// <returns>The extracted character.</returns>
9+
public static char ExtractChar(this StringBuilder @this)
10+
{
11+
return @this.ExtractChar(0);
12+
}
13+
14+
/// <summary>A StringBuilder extension method that extracts the character described by @this.</summary>
15+
/// <param name="this">The @this to act on.</param>
16+
/// <param name="endIndex">[out] The end index.</param>
17+
/// <returns>The extracted character.</returns>
18+
public static char ExtractChar(this StringBuilder @this, out int endIndex)
19+
{
20+
return @this.ExtractChar(0, out endIndex);
21+
}
22+
23+
/// <summary>A StringBuilder extension method that extracts the character described by @this.</summary>
24+
/// <param name="this">The @this to act on.</param>
25+
/// <param name="startIndex">The start index.</param>
26+
/// <returns>The extracted character.</returns>
27+
public static char ExtractChar(this StringBuilder @this, int startIndex)
28+
{
29+
int endIndex;
30+
return @this.ExtractChar(startIndex, out endIndex);
31+
}
32+
33+
/// <summary>A StringBuilder extension method that extracts the character described by @this.</summary>
34+
/// <exception cref="Exception">Thrown when an exception error condition occurs.</exception>
35+
/// <param name="this">The @this to act on.</param>
36+
/// <param name="startIndex">The start index.</param>
37+
/// <param name="endIndex">[out] The end index.</param>
38+
/// <returns>The extracted character.</returns>
39+
public static char ExtractChar(this StringBuilder @this, int startIndex, out int endIndex)
40+
{
41+
if (@this.Length > startIndex + 1)
42+
{
43+
var ch1 = @this[startIndex];
44+
var ch2 = @this[startIndex + 1];
45+
var ch3 = @this[startIndex + 2];
46+
47+
if (ch1 == '\'' && ch3 == '\'')
48+
{
49+
endIndex = startIndex + 2;
50+
return ch2;
51+
}
52+
}
53+
54+
throw new Exception("Invalid char at position: " + startIndex);
55+
}
56+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
using System.Text;
2+
3+
public static partial class Extensions
4+
{
5+
/// <summary>A StringBuilder extension method that extracts the comment described by @this.</summary>
6+
/// <param name="this">The @this to act on.</param>
7+
/// <returns>The extracted comment.</returns>
8+
public static StringBuilder ExtractComment(this StringBuilder @this)
9+
{
10+
return @this.ExtractComment(0);
11+
}
12+
13+
/// <summary>A StringBuilder extension method that extracts the comment described by @this.</summary>
14+
/// <param name="this">The @this to act on.</param>
15+
/// <param name="endIndex">[out] The end index.</param>
16+
/// <returns>The extracted comment.</returns>
17+
public static StringBuilder ExtractComment(this StringBuilder @this, out int endIndex)
18+
{
19+
return @this.ExtractComment(0, out endIndex);
20+
}
21+
22+
/// <summary>A StringBuilder extension method that extracts the comment described by @this.</summary>
23+
/// <param name="this">The @this to act on.</param>
24+
/// <param name="startIndex">The start index.</param>
25+
/// <returns>The extracted comment.</returns>
26+
public static StringBuilder ExtractComment(this StringBuilder @this, int startIndex)
27+
{
28+
int endIndex;
29+
return @this.ExtractComment(startIndex, out endIndex);
30+
}
31+
32+
/// <summary>A StringBuilder extension method that extracts the comment described by @this.</summary>
33+
/// <param name="this">The @this to act on.</param>
34+
/// <param name="startIndex">The start index.</param>
35+
/// <param name="endIndex">[out] The end index.</param>
36+
/// <returns>The extracted comment.</returns>
37+
public static StringBuilder ExtractComment(this StringBuilder @this, int startIndex, out int endIndex)
38+
{
39+
if (@this.Length > startIndex + 1)
40+
{
41+
var ch1 = @this[startIndex];
42+
var ch2 = @this[startIndex + 1];
43+
44+
if (ch1 == '/' && ch2 == '/')
45+
{
46+
// Single line comment
47+
48+
return @this.ExtractCommentSingleLine(startIndex, out endIndex);
49+
}
50+
51+
if (ch1 == '/' && ch2 == '*')
52+
{
53+
/*
54+
* Multi-line comment
55+
*/
56+
57+
return @this.ExtractCommentMultiLine(startIndex, out endIndex);
58+
}
59+
}
60+
61+
endIndex = -1;
62+
return null;
63+
}
64+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
using System.Text;
2+
3+
public static partial class Extensions
4+
{
5+
/// <summary>
6+
/// A StringBuilder extension method that extracts the comment multi line described by
7+
/// @this.
8+
/// </summary>
9+
/// <param name="this">The @this to act on.</param>
10+
/// <returns>The extracted comment multi line.</returns>
11+
public static StringBuilder ExtractCommentMultiLine(this StringBuilder @this)
12+
{
13+
return @this.ExtractCommentMultiLine(0);
14+
}
15+
16+
/// <summary>
17+
/// A StringBuilder extension method that extracts the comment multi line described by
18+
/// @this.
19+
/// </summary>
20+
/// <param name="this">The @this to act on.</param>
21+
/// <param name="endIndex">[out] The end index.</param>
22+
/// <returns>The extracted comment multi line.</returns>
23+
public static StringBuilder ExtractCommentMultiLine(this StringBuilder @this, out int endIndex)
24+
{
25+
return @this.ExtractCommentMultiLine(0, out endIndex);
26+
}
27+
28+
/// <summary>
29+
/// A StringBuilder extension method that extracts the comment multi line described by
30+
/// @this.
31+
/// </summary>
32+
/// <param name="this">The @this to act on.</param>
33+
/// <param name="startIndex">The start index.</param>
34+
/// <returns>The extracted comment multi line.</returns>
35+
public static StringBuilder ExtractCommentMultiLine(this StringBuilder @this, int startIndex)
36+
{
37+
int endIndex;
38+
return @this.ExtractCommentMultiLine(startIndex, out endIndex);
39+
}
40+
41+
/// <summary>
42+
/// A StringBuilder extension method that extracts the comment multi line described by
43+
/// @this.
44+
/// </summary>
45+
/// <param name="this">The @this to act on.</param>
46+
/// <param name="startIndex">The start index.</param>
47+
/// <param name="endIndex">[out] The end index.</param>
48+
/// <returns>The extracted comment multi line.</returns>
49+
public static StringBuilder ExtractCommentMultiLine(this StringBuilder @this, int startIndex, out int endIndex)
50+
{
51+
var sb = new StringBuilder();
52+
53+
if (@this.Length > startIndex + 1)
54+
{
55+
var ch1 = @this[startIndex];
56+
var ch2 = @this[startIndex + 1];
57+
58+
if (ch1 == '/' && ch2 == '*')
59+
{
60+
/*
61+
* Multi-line comment
62+
*/
63+
64+
sb.Append(ch1);
65+
sb.Append(ch2);
66+
var pos = startIndex + 2;
67+
68+
while (pos < @this.Length)
69+
{
70+
var ch = @this[pos];
71+
pos++;
72+
73+
if (ch == '*' && pos < @this.Length && @this[pos] == '/')
74+
{
75+
sb.Append(ch);
76+
sb.Append(@this[pos]);
77+
endIndex = pos;
78+
return sb;
79+
}
80+
81+
sb.Append(ch);
82+
}
83+
84+
endIndex = pos;
85+
return sb;
86+
}
87+
}
88+
89+
endIndex = -1;
90+
return null;
91+
}
92+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
using System.Text;
2+
3+
public static partial class Extensions
4+
{
5+
/// <summary>
6+
/// A StringBuilder extension method that extracts the comment single line described by
7+
/// @this.
8+
/// </summary>
9+
/// <param name="this">The @this to act on.</param>
10+
/// <returns>The extracted comment single line.</returns>
11+
public static StringBuilder ExtractCommentSingleLine(this StringBuilder @this)
12+
{
13+
return @this.ExtractCommentSingleLine(0);
14+
}
15+
16+
/// <summary>
17+
/// A StringBuilder extension method that extracts the comment single line described by
18+
/// @this.
19+
/// </summary>
20+
/// <param name="this">The @this to act on.</param>
21+
/// <param name="endIndex">[out] The end index.</param>
22+
/// <returns>The extracted comment single line.</returns>
23+
public static StringBuilder ExtractCommentSingleLine(this StringBuilder @this, out int endIndex)
24+
{
25+
return @this.ExtractCommentSingleLine(0, out endIndex);
26+
}
27+
28+
/// <summary>
29+
/// A StringBuilder extension method that extracts the comment single line described by
30+
/// @this.
31+
/// </summary>
32+
/// <param name="this">The @this to act on.</param>
33+
/// <param name="startIndex">The start index.</param>
34+
/// <returns>The extracted comment single line.</returns>
35+
public static StringBuilder ExtractCommentSingleLine(this StringBuilder @this, int startIndex)
36+
{
37+
int endIndex;
38+
return @this.ExtractCommentSingleLine(startIndex, out endIndex);
39+
}
40+
41+
/// <summary>
42+
/// A StringBuilder extension method that extracts the comment single line described by
43+
/// @this.
44+
/// </summary>
45+
/// <param name="this">The @this to act on.</param>
46+
/// <param name="startIndex">The start index.</param>
47+
/// <param name="endIndex">[out] The end index.</param>
48+
/// <returns>The extracted comment single line.</returns>
49+
public static StringBuilder ExtractCommentSingleLine(this StringBuilder @this, int startIndex, out int endIndex)
50+
{
51+
var sb = new StringBuilder();
52+
53+
if (@this.Length > startIndex + 1)
54+
{
55+
var ch1 = @this[startIndex];
56+
var ch2 = @this[startIndex + 1];
57+
58+
if (ch1 == '/' && ch2 == '/')
59+
{
60+
// Single line comment
61+
62+
sb.Append(ch1);
63+
sb.Append(ch2);
64+
var pos = startIndex + 2;
65+
66+
while (pos < @this.Length)
67+
{
68+
var ch = @this[pos];
69+
pos++;
70+
71+
if (ch == '\r' && pos < @this.Length && @this[pos] == '\n')
72+
{
73+
endIndex = pos - 1;
74+
return sb;
75+
}
76+
77+
sb.Append(ch);
78+
}
79+
80+
endIndex = pos;
81+
return sb;
82+
}
83+
}
84+
85+
endIndex = -1;
86+
return null;
87+
}
88+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
using System.Text;
2+
3+
public static partial class Extensions
4+
{
5+
/// <summary>A StringBuilder extension method that extracts the hexadecimal described by @this.</summary>
6+
/// <param name="this">The @this to act on.</param>
7+
/// <returns>The extracted hexadecimal.</returns>
8+
public static StringBuilder ExtractHexadecimal(this StringBuilder @this)
9+
{
10+
return @this.ExtractHexadecimal(0);
11+
}
12+
13+
/// <summary>A StringBuilder extension method that extracts the hexadecimal described by @this.</summary>
14+
/// <param name="this">The @this to act on.</param>
15+
/// <param name="endIndex">[out] The end index.</param>
16+
/// <returns>The extracted hexadecimal.</returns>
17+
public static StringBuilder ExtractHexadecimal(this StringBuilder @this, out int endIndex)
18+
{
19+
return @this.ExtractHexadecimal(0, out endIndex);
20+
}
21+
22+
/// <summary>A StringBuilder extension method that extracts the hexadecimal described by @this.</summary>
23+
/// <param name="this">The @this to act on.</param>
24+
/// <param name="startIndex">The start index.</param>
25+
/// <returns>The extracted hexadecimal.</returns>
26+
public static StringBuilder ExtractHexadecimal(this StringBuilder @this, int startIndex)
27+
{
28+
int endIndex;
29+
return @this.ExtractHexadecimal(startIndex, out endIndex);
30+
}
31+
32+
/// <summary>A StringBuilder extension method that extracts the hexadecimal described by @this.</summary>
33+
/// <param name="this">The @this to act on.</param>
34+
/// <param name="startIndex">The start index.</param>
35+
/// <param name="endIndex">[out] The end index.</param>
36+
/// <returns>The extracted hexadecimal.</returns>
37+
public static StringBuilder ExtractHexadecimal(this StringBuilder @this, int startIndex, out int endIndex)
38+
{
39+
// WARNING: This method support all kind of suffix for .NET Runtime Compiler
40+
// An operator can be any sequence of supported operator character
41+
42+
if (startIndex + 1 < @this.Length && @this[startIndex] == '0'
43+
&& (@this[startIndex + 1] == 'x' || @this[startIndex + 1] == 'X'))
44+
{
45+
var sb = new StringBuilder();
46+
47+
var hasNumber = false;
48+
var hasSuffix = false;
49+
50+
sb.Append(@this[startIndex]);
51+
sb.Append(@this[startIndex + 1]);
52+
53+
var pos = startIndex + 2;
54+
55+
while (pos < @this.Length)
56+
{
57+
var ch = @this[pos];
58+
pos++;
59+
60+
if (((ch >= '0' && ch <= '9')
61+
|| (ch >= 'a' && ch <= 'f')
62+
|| (ch >= 'A' && ch <= 'F'))
63+
&& !hasSuffix)
64+
{
65+
hasNumber = true;
66+
sb.Append(ch);
67+
}
68+
else if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))
69+
{
70+
hasSuffix = true;
71+
sb.Append(ch);
72+
}
73+
else
74+
{
75+
pos -= 2;
76+
break;
77+
}
78+
}
79+
80+
if (hasNumber)
81+
{
82+
endIndex = pos;
83+
return sb;
84+
}
85+
}
86+
87+
endIndex = -1;
88+
return null;
89+
}
90+
}

0 commit comments

Comments
 (0)