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