forked from NewLifeX/X
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
236 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
#if NETFRAMEWORK || NETSTANDARD2_0 | ||
using System.Runtime.CompilerServices; | ||
|
||
namespace System; | ||
|
||
/// <summary></summary> | ||
public readonly struct Index : IEquatable<Index> | ||
{ | ||
/// <summary></summary> | ||
private readonly Int32 _value; | ||
|
||
/// <summary></summary> | ||
public static Index Start => new(0); | ||
|
||
/// <summary></summary> | ||
public static Index End => new(-1); | ||
|
||
/// <summary></summary> | ||
public Int32 Value => _value < 0 ? ~_value : _value; | ||
|
||
/// <summary></summary> | ||
public Boolean IsFromEnd => _value < 0; | ||
|
||
/// <summary></summary> | ||
/// <param name="value"></param> | ||
/// <param name="fromEnd"></param> | ||
/// <exception cref="ArgumentOutOfRangeException"></exception> | ||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public Index(Int32 value, Boolean fromEnd = false) | ||
{ | ||
if (value < 0) | ||
{ | ||
throw new ArgumentOutOfRangeException("value", "value must be non-negative"); | ||
} | ||
_value = fromEnd ? ~value : value; | ||
} | ||
|
||
private Index(Int32 value) => _value = value; | ||
|
||
/// <summary></summary> | ||
/// <param name="value"></param> | ||
/// <returns></returns> | ||
/// <exception cref="ArgumentOutOfRangeException"></exception> | ||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public static Index FromStart(Int32 value) | ||
{ | ||
if (value < 0) | ||
{ | ||
throw new ArgumentOutOfRangeException("value", "value must be non-negative"); | ||
} | ||
return new Index(value); | ||
} | ||
|
||
/// <summary></summary> | ||
/// <param name="value"></param> | ||
/// <returns></returns> | ||
/// <exception cref="ArgumentOutOfRangeException"></exception> | ||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public static Index FromEnd(Int32 value) | ||
{ | ||
if (value < 0) | ||
{ | ||
throw new ArgumentOutOfRangeException("value", "value must be non-negative"); | ||
} | ||
return new Index(~value); | ||
} | ||
|
||
/// <summary></summary> | ||
/// <param name="length"></param> | ||
/// <returns></returns> | ||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public Int32 GetOffset(Int32 length) | ||
{ | ||
var offset = _value; | ||
if (IsFromEnd) | ||
{ | ||
offset += length + 1; | ||
} | ||
return offset; | ||
} | ||
|
||
/// <summary></summary> | ||
/// <param name="value"></param> | ||
/// <returns></returns> | ||
public override Boolean Equals(Object value) => value is Index index && _value == index._value; | ||
|
||
/// <summary></summary> | ||
/// <param name="other"></param> | ||
/// <returns></returns> | ||
public Boolean Equals(Index other) => _value == other._value; | ||
|
||
/// <summary></summary> | ||
/// <returns></returns> | ||
public override Int32 GetHashCode() => _value; | ||
|
||
/// <summary></summary> | ||
/// <param name="value"></param> | ||
public static implicit operator Index(Int32 value) => FromStart(value); | ||
|
||
/// <summary></summary> | ||
/// <returns></returns> | ||
public override String ToString() | ||
{ | ||
if (IsFromEnd) | ||
{ | ||
return "^" + (UInt32)Value; | ||
} | ||
return ((UInt32)Value).ToString(); | ||
} | ||
} | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
#if NETFRAMEWORK || NETSTANDARD2_0 | ||
using System.Runtime.CompilerServices; | ||
|
||
namespace System; | ||
|
||
/// <summary></summary> | ||
public readonly struct Range : IEquatable<Range> | ||
{ | ||
/// <summary></summary> | ||
public Index Start { get; } | ||
|
||
/// <summary></summary> | ||
public Index End { get; } | ||
|
||
/// <summary></summary> | ||
public static Range All => new(Index.Start, Index.End); | ||
|
||
/// <summary></summary> | ||
/// <param name="start"></param> | ||
/// <param name="end"></param> | ||
public Range(Index start, Index end) | ||
{ | ||
Start = start; | ||
End = end; | ||
} | ||
|
||
/// <summary></summary> | ||
/// <param name="value"></param> | ||
/// <returns></returns> | ||
public override Boolean Equals(Object value) | ||
{ | ||
if (value is Range r) | ||
{ | ||
if (r.Start.Equals(Start)) | ||
{ | ||
return r.End.Equals(End); | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
/// <summary></summary> | ||
/// <param name="other"></param> | ||
/// <returns></returns> | ||
public Boolean Equals(Range other) => other.Start.Equals(Start) && other.End.Equals(End); | ||
|
||
/// <summary></summary> | ||
/// <returns></returns> | ||
public override Int32 GetHashCode() => Start.GetHashCode() * 31 + End.GetHashCode(); | ||
|
||
/// <summary></summary> | ||
/// <returns></returns> | ||
public override String ToString() => Start.ToString() + ".." + End; | ||
|
||
/// <summary></summary> | ||
/// <param name="start"></param> | ||
/// <returns></returns> | ||
public static Range StartAt(Index start) => new(start, Index.End); | ||
|
||
/// <summary></summary> | ||
/// <param name="end"></param> | ||
/// <returns></returns> | ||
public static Range EndAt(Index end) => new(Index.Start, end); | ||
|
||
/// <summary></summary> | ||
/// <param name="length"></param> | ||
/// <returns></returns> | ||
/// <exception cref="ArgumentOutOfRangeException"></exception> | ||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
//[CLSCompliant(false)] | ||
public (Int32 Offset, Int32 Length) GetOffsetAndLength(Int32 length) | ||
{ | ||
var startIndex = Start; | ||
var start = ((!startIndex.IsFromEnd) ? startIndex.Value : (length - startIndex.Value)); | ||
var endIndex = End; | ||
var end = ((!endIndex.IsFromEnd) ? endIndex.Value : (length - endIndex.Value)); | ||
if ((UInt32)end > (UInt32)length || (UInt32)start > (UInt32)end) | ||
{ | ||
throw new ArgumentOutOfRangeException("length"); | ||
} | ||
return (start, end - start); | ||
} | ||
} | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters