This repository was archived by the owner on Jan 23, 2023. It is now read-only.
File tree 2 files changed +37
-4
lines changed
src/System.Private.CoreLib/shared/System
2 files changed +37
-4
lines changed Original file line number Diff line number Diff line change 2
2
// The .NET Foundation licenses this file to you under the MIT license.
3
3
// See the LICENSE file in the project root for more information.
4
4
5
+ using System . Diagnostics ;
6
+
5
7
namespace System
6
8
{
7
9
public readonly struct Index : IEquatable < Index >
@@ -28,10 +30,15 @@ public override int GetHashCode()
28
30
return _value ;
29
31
}
30
32
31
- public override string ToString ( )
33
+ public override string ToString ( ) => FromEnd ? ToStringFromEnd ( ) : ( ( uint ) Value ) . ToString ( ) ;
34
+
35
+ private string ToStringFromEnd ( )
32
36
{
33
- string str = Value . ToString ( ) ;
34
- return FromEnd ? "^" + str : str ;
37
+ Span < char > span = stackalloc char [ 11 ] ; // 1 for ^ and 10 for longest possible uint value
38
+ bool formatted = ( ( uint ) Value ) . TryFormat ( span . Slice ( 1 ) , out int charsWritten ) ;
39
+ Debug . Assert ( formatted ) ;
40
+ span [ 0 ] = '^' ;
41
+ return new string ( span . Slice ( 0 , charsWritten + 1 ) ) ;
35
42
}
36
43
37
44
public static implicit operator Index ( int value )
Original file line number Diff line number Diff line change 2
2
// The .NET Foundation licenses this file to you under the MIT license.
3
3
// See the LICENSE file in the project root for more information.
4
4
5
+ using System . Diagnostics ;
6
+
5
7
namespace System
6
8
{
7
9
public readonly struct Range : IEquatable < Range >
@@ -35,7 +37,31 @@ public override int GetHashCode()
35
37
36
38
public override string ToString ( )
37
39
{
38
- return Start + ".." + End ;
40
+ Span < char > span = stackalloc char [ 2 + ( 2 * 11 ) ] ; // 2 for "..", then for each index 1 for '^' and 10 for longest possible uint
41
+ int charsWritten ;
42
+ int pos = 0 ;
43
+
44
+ if ( Start . FromEnd )
45
+ {
46
+ span [ 0 ] = '^' ;
47
+ pos = 1 ;
48
+ }
49
+ bool formatted = ( ( uint ) Start . Value ) . TryFormat ( span . Slice ( pos ) , out charsWritten ) ;
50
+ Debug . Assert ( formatted ) ;
51
+ pos += charsWritten ;
52
+
53
+ span [ pos ++ ] = '.' ;
54
+ span [ pos ++ ] = '.' ;
55
+
56
+ if ( End . FromEnd )
57
+ {
58
+ span [ pos ++ ] = '^' ;
59
+ }
60
+ formatted = ( ( uint ) End . Value ) . TryFormat ( span . Slice ( pos ) , out charsWritten ) ;
61
+ Debug . Assert ( formatted ) ;
62
+ pos += charsWritten ;
63
+
64
+ return new string ( span . Slice ( 0 , pos ) ) ;
39
65
}
40
66
41
67
public static Range Create ( Index start , Index end ) => new Range ( start , end ) ;
You can’t perform that action at this time.
0 commit comments