3
3
using Microsoft . Diagnostics . Runtime . Utilities . Pdb ;
4
4
using System ;
5
5
using System . Collections . Generic ;
6
+ using System . Diagnostics ;
6
7
using System . IO ;
7
8
8
9
namespace BenchmarkDotNet . Disassemblers
@@ -24,8 +25,9 @@ internal static IEnumerable<Sharp> GetSource(ClrMethod method, ILToNativeMap map
24
25
continue ;
25
26
26
27
var text = sourceLine + Environment . NewLine
27
- + GetSmartPrefix ( sourceLine , sourceLocation . ColStart - 1 )
28
- + new string ( '^' , sourceLocation . ColEnd - sourceLocation . ColStart ) ;
28
+ + GetSmartPointer ( sourceLine ,
29
+ start : line == sourceLocation . LineNumber ? sourceLocation . ColStart - 1 : default ( int ? ) ,
30
+ end : line == sourceLocation . LineNumberEnd ? sourceLocation . ColEnd - 1 : default ( int ? ) ) ;
29
31
30
32
yield return new Sharp
31
33
{
@@ -54,17 +56,32 @@ private static string ReadSourceLine(string file, int line)
54
56
: null ; // "nop" can have no corresponding c# code ;)
55
57
}
56
58
57
- private static string GetSmartPrefix ( string sourceLine , int length )
59
+ private static string GetSmartPointer ( string sourceLine , int ? start , int ? end )
58
60
{
59
- if ( length <= 0 )
60
- return string . Empty ;
61
+ Debug . Assert ( start is null || start < sourceLine . Length ) ;
62
+ Debug . Assert ( end is null || end <= sourceLine . Length ) ;
61
63
62
- var prefix = new char [ length ] ;
63
- for ( int i = 0 ; i < length ; i ++ )
64
+ var prefix = new char [ end ?? sourceLine . Length ] ;
65
+ var index = 0 ;
66
+
67
+ // write offset using whitespaces
68
+ while ( index < ( start ?? prefix . Length ) )
64
69
{
65
- char sourceChar = i < sourceLine . Length ? sourceLine [ i ] : ' ' ;
66
- prefix [ i ] = sourceChar == '\t ' ? sourceChar : ' ' ;
70
+ prefix [ index ] =
71
+ sourceLine . Length > index &&
72
+ sourceLine [ index ] == '\t '
73
+ ? '\t '
74
+ : ' ' ;
75
+ index ++ ;
67
76
}
77
+
78
+ // write smart pointer
79
+ while ( index < prefix . Length )
80
+ {
81
+ prefix [ index ] = '^' ;
82
+ index ++ ;
83
+ }
84
+
68
85
return new string ( prefix ) ;
69
86
}
70
87
}
0 commit comments