Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change Mark to be readonly struct #647

Merged
merged 1 commit into from
Nov 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion YamlDotNet/Core/AnchorNotFoundException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public AnchorNotFoundException(string message)
/// <summary>
/// Initializes a new instance of the <see cref="AnchorNotFoundException"/> class.
/// </summary>
public AnchorNotFoundException(Mark start, Mark end, string message)
public AnchorNotFoundException(in Mark start, in Mark end, string message)
: base(start, end, message)
{
}
Expand Down
2 changes: 1 addition & 1 deletion YamlDotNet/Core/Events/DocumentStart.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public DocumentStart(VersionDirective? version, TagDirectiveCollection? tags, bo
/// </summary>
/// <param name="start">The start position of the event.</param>
/// <param name="end">The end position of the event.</param>
public DocumentStart(Mark start, Mark end)
public DocumentStart(in Mark start, in Mark end)
: this(null, null, true, start, end)
{
}
Expand Down
2 changes: 1 addition & 1 deletion YamlDotNet/Core/Events/MappingEnd.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public class MappingEnd : ParsingEvent
/// </summary>
/// <param name="start">The start position of the event.</param>
/// <param name="end">The end position of the event.</param>
public MappingEnd(Mark start, Mark end)
public MappingEnd(in Mark start, in Mark end)
: base(start, end)
{
}
Expand Down
6 changes: 3 additions & 3 deletions YamlDotNet/Core/Events/ParsingEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ public abstract class ParsingEvent
/// </summary>
/// <param name="start">The start position of the event.</param>
/// <param name="end">The end position of the event.</param>
internal ParsingEvent(Mark start, Mark end)
internal ParsingEvent(in Mark start, in Mark end)
{
this.Start = start ?? throw new System.ArgumentNullException(nameof(start));
this.End = end ?? throw new System.ArgumentNullException(nameof(end));
this.Start = start;
this.End = end;
}
}
}
2 changes: 1 addition & 1 deletion YamlDotNet/Core/Events/SequenceEnd.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public sealed class SequenceEnd : ParsingEvent
/// </summary>
/// <param name="start">The start position of the event.</param>
/// <param name="end">The end position of the event.</param>
public SequenceEnd(Mark start, Mark end)
public SequenceEnd(in Mark start, in Mark end)
: base(start, end)
{
}
Expand Down
2 changes: 1 addition & 1 deletion YamlDotNet/Core/Events/StreamEnd.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ internal override EventType Type
/// </summary>
/// <param name="start">The start position of the event.</param>
/// <param name="end">The end position of the event.</param>
public StreamEnd(Mark start, Mark end)
public StreamEnd(in Mark start, in Mark end)
: base(start, end)
{
}
Expand Down
2 changes: 1 addition & 1 deletion YamlDotNet/Core/Events/StreamStart.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public StreamStart()
/// </summary>
/// <param name="start">The start position of the event.</param>
/// <param name="end">The end position of the event.</param>
public StreamStart(Mark start, Mark end)
public StreamStart(in Mark start, in Mark end)
: base(start, end)
{
}
Expand Down
2 changes: 1 addition & 1 deletion YamlDotNet/Core/ForwardAnchorNotSupportedException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public ForwardAnchorNotSupportedException(string message)
/// <summary>
/// Initializes a new instance of the <see cref="AnchorNotFoundException"/> class.
/// </summary>
public ForwardAnchorNotSupportedException(Mark start, Mark end, string message)
public ForwardAnchorNotSupportedException(in Mark start, in Mark end, string message)
: base(start, end, message)
{
}
Expand Down
37 changes: 11 additions & 26 deletions YamlDotNet/Core/Mark.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,19 @@
// SOFTWARE.

using System;
using YamlDotNet.Helpers;

namespace YamlDotNet.Core
{
/// <summary>
/// Represents a location inside a file
/// </summary>
public sealed class Mark : IEquatable<Mark>, IComparable<Mark>, IComparable
public readonly struct Mark : IEquatable<Mark>, IComparable<Mark>, IComparable
{
/// <summary>
/// Gets a <see cref="Mark"/> with empty values.
/// </summary>
public static readonly Mark Empty = new Mark();
public static readonly Mark Empty = new Mark(0, 1, 1);

/// <summary>
/// Gets / sets the absolute offset in the file
Expand All @@ -48,25 +49,19 @@ public sealed class Mark : IEquatable<Mark>, IComparable<Mark>, IComparable
/// </summary>
public int Column { get; }

public Mark()
{
Line = 1;
Column = 1;
}

public Mark(int index, int line, int column)
{
if (index < 0)
{
throw new ArgumentOutOfRangeException(nameof(index), "Index must be greater than or equal to zero.");
ThrowHelper.ThrowArgumentOutOfRangeException(nameof(index), "Index must be greater than or equal to zero.");
}
if (line < 1)
{
throw new ArgumentOutOfRangeException(nameof(line), "Line must be greater than or equal to 1.");
ThrowHelper.ThrowArgumentOutOfRangeException(nameof(line), "Line must be greater than or equal to 1.");
}
if (column < 1)
{
throw new ArgumentOutOfRangeException(nameof(column), "Column must be greater than or equal to 1.");
ThrowHelper.ThrowArgumentOutOfRangeException(nameof(column), "Column must be greater than or equal to 1.");
}

Index = index;
Expand All @@ -88,14 +83,13 @@ public override string ToString()
/// <summary />
public override bool Equals(object? obj)
{
return Equals(obj as Mark);
return Equals((Mark)(obj ?? Empty));
}

/// <summary />
public bool Equals(Mark? other)
public bool Equals(Mark other)
{
return other != null
&& Index == other.Index
return Index == other.Index
&& Line == other.Line
&& Column == other.Column;
}
Expand All @@ -115,21 +109,12 @@ public override int GetHashCode()
/// <summary />
public int CompareTo(object? obj)
{
if (obj == null)
{
throw new ArgumentNullException(nameof(obj));
}
return CompareTo(obj as Mark);
return CompareTo((Mark)(obj ?? Empty));
}

/// <summary />
public int CompareTo(Mark? other)
public int CompareTo(Mark other)
{
if (other == null)
{
throw new ArgumentNullException(nameof(other));
}

var cmp = Line.CompareTo(other.Line);
if (cmp == 0)
{
Expand Down
2 changes: 1 addition & 1 deletion YamlDotNet/Core/MaximumRecursionLevelReachedException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public MaximumRecursionLevelReachedException(string message)
/// <summary>
/// Initializes a new instance of the <see cref="MaximumRecursionLevelReachedException"/> class.
/// </summary>
public MaximumRecursionLevelReachedException(Mark start, Mark end, string message)
public MaximumRecursionLevelReachedException(in Mark start, in Mark end, string message)
: base(start, end, message)
{
}
Expand Down
2 changes: 1 addition & 1 deletion YamlDotNet/Core/Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ private ParsingEvent ParseDocumentContent()
/// <summary>
/// Generate an empty scalar event.
/// </summary>
private static ParsingEvent ProcessEmptyScalar(Mark position)
private static ParsingEvent ProcessEmptyScalar(in Mark position)
{
return new Events.Scalar(AnchorName.Empty, TagName.Empty, string.Empty, ScalarStyle.Plain, true, false, position, position);
}
Expand Down
10 changes: 5 additions & 5 deletions YamlDotNet/Core/Scanner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2207,7 +2207,7 @@ private void RemoveSimpleKey()
/// %TAG !yaml! tag:yaml.org,2002: \n
/// ^^^
/// </summary>
private string ScanDirectiveName(Mark start)
private string ScanDirectiveName(in Mark start)
{
var name = new StringBuilder();

Expand Down Expand Up @@ -2252,7 +2252,7 @@ private void SkipWhitespaces()
/// %YAML 1.1 # a comment \n
/// ^^^^^^
/// </summary>
private Token ScanVersionDirectiveValue(Mark start)
private Token ScanVersionDirectiveValue(in Mark start)
{
SkipWhitespaces();

Expand Down Expand Up @@ -2283,7 +2283,7 @@ private Token ScanVersionDirectiveValue(Mark start)
/// %TAG !yaml! tag:yaml.org,2002: \n
/// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
/// </summary>
private Token ScanTagDirectiveValue(Mark start)
private Token ScanTagDirectiveValue(in Mark start)
{
SkipWhitespaces();

Expand Down Expand Up @@ -2371,7 +2371,7 @@ private string ScanTagUri(string? head, Mark start)
/// Decode an URI-escape sequence corresponding to a single UTF-8 character.
/// </summary>

private string ScanUriEscapes(Mark start)
private string ScanUriEscapes(in Mark start)
{
// Decode the required number of characters.

Expand Down Expand Up @@ -2495,7 +2495,7 @@ private string ScanTagHandle(bool isDirective, Mark start)
/// %YAML 1.1 # a comment \n
/// ^
/// </summary>
private int ScanVersionDirectiveNumber(Mark start)
private int ScanVersionDirectiveNumber(in Mark start)
{
var value = 0;
var length = 0;
Expand Down
2 changes: 1 addition & 1 deletion YamlDotNet/Core/SemanticErrorException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public SemanticErrorException(string message)
/// <summary>
/// Initializes a new instance of the <see cref="SemanticErrorException"/> class.
/// </summary>
public SemanticErrorException(Mark start, Mark end, string message)
public SemanticErrorException(in Mark start, in Mark end, string message)
: base(start, end, message)
{
}
Expand Down
2 changes: 1 addition & 1 deletion YamlDotNet/Core/SyntaxErrorException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public SyntaxErrorException(string message)
/// <summary>
/// Initializes a new instance of the <see cref="SyntaxErrorException"/> class.
/// </summary>
public SyntaxErrorException(Mark start, Mark end, string message)
public SyntaxErrorException(in Mark start, in Mark end, string message)
: base(start, end, message)
{
}
Expand Down
2 changes: 1 addition & 1 deletion YamlDotNet/Core/Tokens/BlockEnd.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public BlockEnd()
/// </summary>
/// <param name="start">The start position of the token.</param>
/// <param name="end">The end position of the token.</param>
public BlockEnd(Mark start, Mark end)
public BlockEnd(in Mark start, in Mark end)
: base(start, end)
{
}
Expand Down
2 changes: 1 addition & 1 deletion YamlDotNet/Core/Tokens/BlockEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public BlockEntry()
/// </summary>
/// <param name="start">The start position of the token.</param>
/// <param name="end">The end position of the token.</param>
public BlockEntry(Mark start, Mark end)
public BlockEntry(in Mark start, in Mark end)
: base(start, end)
{
}
Expand Down
2 changes: 1 addition & 1 deletion YamlDotNet/Core/Tokens/BlockMappingStart.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public BlockMappingStart()
/// </summary>
/// <param name="start">The start position of the token.</param>
/// <param name="end">The end position of the token.</param>
public BlockMappingStart(Mark start, Mark end)
public BlockMappingStart(in Mark start, in Mark end)
: base(start, end)
{
}
Expand Down
2 changes: 1 addition & 1 deletion YamlDotNet/Core/Tokens/BlockSequenceStart.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public BlockSequenceStart()
/// </summary>
/// <param name="start">The start position of the token.</param>
/// <param name="end">The end position of the token.</param>
public BlockSequenceStart(Mark start, Mark end)
public BlockSequenceStart(in Mark start, in Mark end)
: base(start, end)
{
}
Expand Down
2 changes: 1 addition & 1 deletion YamlDotNet/Core/Tokens/DocumentEnd.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public DocumentEnd()
/// </summary>
/// <param name="start">The start position of the token.</param>
/// <param name="end">The end position of the token.</param>
public DocumentEnd(Mark start, Mark end)
public DocumentEnd(in Mark start, in Mark end)
: base(start, end)
{
}
Expand Down
2 changes: 1 addition & 1 deletion YamlDotNet/Core/Tokens/DocumentStart.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public DocumentStart()
/// </summary>
/// <param name="start">The start position of the token.</param>
/// <param name="end">The end position of the token.</param>
public DocumentStart(Mark start, Mark end)
public DocumentStart(in Mark start, in Mark end)
: base(start, end)
{
}
Expand Down
2 changes: 1 addition & 1 deletion YamlDotNet/Core/Tokens/FlowEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public FlowEntry()
/// </summary>
/// <param name="start">The start position of the token.</param>
/// <param name="end">The end position of the token.</param>
public FlowEntry(Mark start, Mark end)
public FlowEntry(in Mark start, in Mark end)
: base(start, end)
{
}
Expand Down
2 changes: 1 addition & 1 deletion YamlDotNet/Core/Tokens/FlowMappingEnd.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public FlowMappingEnd()
/// </summary>
/// <param name="start">The start position of the token.</param>
/// <param name="end">The end position of the token.</param>
public FlowMappingEnd(Mark start, Mark end)
public FlowMappingEnd(in Mark start, in Mark end)
: base(start, end)
{
}
Expand Down
2 changes: 1 addition & 1 deletion YamlDotNet/Core/Tokens/FlowMappingStart.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public FlowMappingStart()
/// </summary>
/// <param name="start">The start position of the token.</param>
/// <param name="end">The end position of the token.</param>
public FlowMappingStart(Mark start, Mark end)
public FlowMappingStart(in Mark start, in Mark end)
: base(start, end)
{
}
Expand Down
2 changes: 1 addition & 1 deletion YamlDotNet/Core/Tokens/FlowSequenceEnd.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public FlowSequenceEnd()
/// </summary>
/// <param name="start">The start position of the token.</param>
/// <param name="end">The end position of the token.</param>
public FlowSequenceEnd(Mark start, Mark end)
public FlowSequenceEnd(in Mark start, in Mark end)
: base(start, end)
{
}
Expand Down
2 changes: 1 addition & 1 deletion YamlDotNet/Core/Tokens/FlowSequenceStart.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public FlowSequenceStart()
/// </summary>
/// <param name="start">The start position of the token.</param>
/// <param name="end">The end position of the token.</param>
public FlowSequenceStart(Mark start, Mark end)
public FlowSequenceStart(in Mark start, in Mark end)
: base(start, end)
{
}
Expand Down
2 changes: 1 addition & 1 deletion YamlDotNet/Core/Tokens/Key.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public Key()
/// </summary>
/// <param name="start">The start position of the token.</param>
/// <param name="end">The end position of the token.</param>
public Key(Mark start, Mark end)
public Key(in Mark start, in Mark end)
: base(start, end)
{
}
Expand Down
2 changes: 1 addition & 1 deletion YamlDotNet/Core/Tokens/StreamEnd.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public StreamEnd()
/// </summary>
/// <param name="start">The start position of the token.</param>
/// <param name="end">The end position of the token.</param>
public StreamEnd(Mark start, Mark end)
public StreamEnd(in Mark start, in Mark end)
: base(start, end)
{
}
Expand Down
2 changes: 1 addition & 1 deletion YamlDotNet/Core/Tokens/StreamStart.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public StreamStart()
/// </summary>
/// <param name="start">The start position of the token.</param>
/// <param name="end">The end position of the token.</param>
public StreamStart(Mark start, Mark end)
public StreamStart(in Mark start, in Mark end)
: base(start, end)
{
}
Expand Down
8 changes: 3 additions & 5 deletions YamlDotNet/Core/Tokens/Token.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

using System;

namespace YamlDotNet.Core.Tokens
{
/// <summary>
Expand All @@ -43,10 +41,10 @@ public abstract class Token
/// </summary>
/// <param name="start">The start position of the token.</param>
/// <param name="end">The end position of the token.</param>
protected Token(Mark start, Mark end)
protected Token(in Mark start, in Mark end)
{
this.Start = start ?? throw new ArgumentNullException(nameof(start));
this.End = end ?? throw new ArgumentNullException(nameof(end));
this.Start = start;
this.End = end;
}
}
}