diff --git a/YamlDotNet/Core/AnchorNotFoundException.cs b/YamlDotNet/Core/AnchorNotFoundException.cs index 187b603a..d9218f7e 100644 --- a/YamlDotNet/Core/AnchorNotFoundException.cs +++ b/YamlDotNet/Core/AnchorNotFoundException.cs @@ -40,7 +40,7 @@ public AnchorNotFoundException(string message) /// /// Initializes a new instance of the class. /// - public AnchorNotFoundException(Mark start, Mark end, string message) + public AnchorNotFoundException(in Mark start, in Mark end, string message) : base(start, end, message) { } diff --git a/YamlDotNet/Core/Events/DocumentStart.cs b/YamlDotNet/Core/Events/DocumentStart.cs index 82cd21d1..8725eeb0 100644 --- a/YamlDotNet/Core/Events/DocumentStart.cs +++ b/YamlDotNet/Core/Events/DocumentStart.cs @@ -92,7 +92,7 @@ public DocumentStart(VersionDirective? version, TagDirectiveCollection? tags, bo /// /// The start position of the event. /// The end position of the event. - public DocumentStart(Mark start, Mark end) + public DocumentStart(in Mark start, in Mark end) : this(null, null, true, start, end) { } diff --git a/YamlDotNet/Core/Events/MappingEnd.cs b/YamlDotNet/Core/Events/MappingEnd.cs index 8194ad11..8f083437 100644 --- a/YamlDotNet/Core/Events/MappingEnd.cs +++ b/YamlDotNet/Core/Events/MappingEnd.cs @@ -43,7 +43,7 @@ public class MappingEnd : ParsingEvent /// /// The start position of the event. /// The end position of the event. - public MappingEnd(Mark start, Mark end) + public MappingEnd(in Mark start, in Mark end) : base(start, end) { } diff --git a/YamlDotNet/Core/Events/ParsingEvent.cs b/YamlDotNet/Core/Events/ParsingEvent.cs index 6ec57307..ba6a3172 100644 --- a/YamlDotNet/Core/Events/ParsingEvent.cs +++ b/YamlDotNet/Core/Events/ParsingEvent.cs @@ -59,10 +59,10 @@ public abstract class ParsingEvent /// /// The start position of the event. /// The end position of the event. - 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; } } } diff --git a/YamlDotNet/Core/Events/SequenceEnd.cs b/YamlDotNet/Core/Events/SequenceEnd.cs index 63a0ef41..217d2117 100644 --- a/YamlDotNet/Core/Events/SequenceEnd.cs +++ b/YamlDotNet/Core/Events/SequenceEnd.cs @@ -43,7 +43,7 @@ public sealed class SequenceEnd : ParsingEvent /// /// The start position of the event. /// The end position of the event. - public SequenceEnd(Mark start, Mark end) + public SequenceEnd(in Mark start, in Mark end) : base(start, end) { } diff --git a/YamlDotNet/Core/Events/StreamEnd.cs b/YamlDotNet/Core/Events/StreamEnd.cs index 66b79281..296da6fe 100644 --- a/YamlDotNet/Core/Events/StreamEnd.cs +++ b/YamlDotNet/Core/Events/StreamEnd.cs @@ -55,7 +55,7 @@ internal override EventType Type /// /// The start position of the event. /// The end position of the event. - public StreamEnd(Mark start, Mark end) + public StreamEnd(in Mark start, in Mark end) : base(start, end) { } diff --git a/YamlDotNet/Core/Events/StreamStart.cs b/YamlDotNet/Core/Events/StreamStart.cs index 55202005..e2f7c7b7 100644 --- a/YamlDotNet/Core/Events/StreamStart.cs +++ b/YamlDotNet/Core/Events/StreamStart.cs @@ -63,7 +63,7 @@ public StreamStart() /// /// The start position of the event. /// The end position of the event. - public StreamStart(Mark start, Mark end) + public StreamStart(in Mark start, in Mark end) : base(start, end) { } diff --git a/YamlDotNet/Core/ForwardAnchorNotSupportedException.cs b/YamlDotNet/Core/ForwardAnchorNotSupportedException.cs index 0ddcbc4f..ed107831 100644 --- a/YamlDotNet/Core/ForwardAnchorNotSupportedException.cs +++ b/YamlDotNet/Core/ForwardAnchorNotSupportedException.cs @@ -41,7 +41,7 @@ public ForwardAnchorNotSupportedException(string message) /// /// Initializes a new instance of the class. /// - public ForwardAnchorNotSupportedException(Mark start, Mark end, string message) + public ForwardAnchorNotSupportedException(in Mark start, in Mark end, string message) : base(start, end, message) { } diff --git a/YamlDotNet/Core/Mark.cs b/YamlDotNet/Core/Mark.cs index ac038adf..e9b6a61f 100644 --- a/YamlDotNet/Core/Mark.cs +++ b/YamlDotNet/Core/Mark.cs @@ -20,18 +20,19 @@ // SOFTWARE. using System; +using YamlDotNet.Helpers; namespace YamlDotNet.Core { /// /// Represents a location inside a file /// - public sealed class Mark : IEquatable, IComparable, IComparable + public readonly struct Mark : IEquatable, IComparable, IComparable { /// /// Gets a with empty values. /// - public static readonly Mark Empty = new Mark(); + public static readonly Mark Empty = new Mark(0, 1, 1); /// /// Gets / sets the absolute offset in the file @@ -48,25 +49,19 @@ public sealed class Mark : IEquatable, IComparable, IComparable /// 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; @@ -88,14 +83,13 @@ public override string ToString() /// public override bool Equals(object? obj) { - return Equals(obj as Mark); + return Equals((Mark)(obj ?? Empty)); } /// - 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; } @@ -115,21 +109,12 @@ public override int GetHashCode() /// public int CompareTo(object? obj) { - if (obj == null) - { - throw new ArgumentNullException(nameof(obj)); - } - return CompareTo(obj as Mark); + return CompareTo((Mark)(obj ?? Empty)); } /// - 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) { diff --git a/YamlDotNet/Core/MaximumRecursionLevelReachedException.cs b/YamlDotNet/Core/MaximumRecursionLevelReachedException.cs index 1a28acf0..7287b8df 100644 --- a/YamlDotNet/Core/MaximumRecursionLevelReachedException.cs +++ b/YamlDotNet/Core/MaximumRecursionLevelReachedException.cs @@ -40,7 +40,7 @@ public MaximumRecursionLevelReachedException(string message) /// /// Initializes a new instance of the class. /// - public MaximumRecursionLevelReachedException(Mark start, Mark end, string message) + public MaximumRecursionLevelReachedException(in Mark start, in Mark end, string message) : base(start, end, message) { } diff --git a/YamlDotNet/Core/Parser.cs b/YamlDotNet/Core/Parser.cs index 59742ad6..d3b812f0 100644 --- a/YamlDotNet/Core/Parser.cs +++ b/YamlDotNet/Core/Parser.cs @@ -419,7 +419,7 @@ private ParsingEvent ParseDocumentContent() /// /// Generate an empty scalar event. /// - 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); } diff --git a/YamlDotNet/Core/Scanner.cs b/YamlDotNet/Core/Scanner.cs index 2cf2da56..05082d7c 100644 --- a/YamlDotNet/Core/Scanner.cs +++ b/YamlDotNet/Core/Scanner.cs @@ -2230,7 +2230,7 @@ private void RemoveSimpleKey() /// %TAG !yaml! tag:yaml.org,2002: \n /// ^^^ /// - private string ScanDirectiveName(Mark start) + private string ScanDirectiveName(in Mark start) { using var nameBuilder = StringBuilderPool.Rent(); var name = nameBuilder.Builder; @@ -2276,7 +2276,7 @@ private void SkipWhitespaces() /// %YAML 1.1 # a comment \n /// ^^^^^^ /// - private Token ScanVersionDirectiveValue(Mark start) + private Token ScanVersionDirectiveValue(in Mark start) { SkipWhitespaces(); @@ -2307,7 +2307,7 @@ private Token ScanVersionDirectiveValue(Mark start) /// %TAG !yaml! tag:yaml.org,2002: \n /// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ /// - private Token ScanTagDirectiveValue(Mark start) + private Token ScanTagDirectiveValue(in Mark start) { SkipWhitespaces(); @@ -2397,7 +2397,7 @@ private string ScanTagUri(string? head, Mark start) /// Decode an URI-escape sequence corresponding to a single UTF-8 character. /// - private string ScanUriEscapes(Mark start) + private string ScanUriEscapes(in Mark start) { // Decode the required number of characters. @@ -2522,7 +2522,7 @@ private string ScanTagHandle(bool isDirective, Mark start) /// %YAML 1.1 # a comment \n /// ^ /// - private int ScanVersionDirectiveNumber(Mark start) + private int ScanVersionDirectiveNumber(in Mark start) { var value = 0; var length = 0; diff --git a/YamlDotNet/Core/SemanticErrorException.cs b/YamlDotNet/Core/SemanticErrorException.cs index 39e86833..5209cc86 100644 --- a/YamlDotNet/Core/SemanticErrorException.cs +++ b/YamlDotNet/Core/SemanticErrorException.cs @@ -40,7 +40,7 @@ public SemanticErrorException(string message) /// /// Initializes a new instance of the class. /// - public SemanticErrorException(Mark start, Mark end, string message) + public SemanticErrorException(in Mark start, in Mark end, string message) : base(start, end, message) { } diff --git a/YamlDotNet/Core/SyntaxErrorException.cs b/YamlDotNet/Core/SyntaxErrorException.cs index d2c5fbba..21744d15 100644 --- a/YamlDotNet/Core/SyntaxErrorException.cs +++ b/YamlDotNet/Core/SyntaxErrorException.cs @@ -40,7 +40,7 @@ public SyntaxErrorException(string message) /// /// Initializes a new instance of the class. /// - public SyntaxErrorException(Mark start, Mark end, string message) + public SyntaxErrorException(in Mark start, in Mark end, string message) : base(start, end, message) { } diff --git a/YamlDotNet/Core/Tokens/BlockEnd.cs b/YamlDotNet/Core/Tokens/BlockEnd.cs index f0413676..b0252180 100644 --- a/YamlDotNet/Core/Tokens/BlockEnd.cs +++ b/YamlDotNet/Core/Tokens/BlockEnd.cs @@ -39,7 +39,7 @@ public BlockEnd() /// /// The start position of the token. /// The end position of the token. - public BlockEnd(Mark start, Mark end) + public BlockEnd(in Mark start, in Mark end) : base(start, end) { } diff --git a/YamlDotNet/Core/Tokens/BlockEntry.cs b/YamlDotNet/Core/Tokens/BlockEntry.cs index 872519ab..6f36b4e4 100644 --- a/YamlDotNet/Core/Tokens/BlockEntry.cs +++ b/YamlDotNet/Core/Tokens/BlockEntry.cs @@ -39,7 +39,7 @@ public BlockEntry() /// /// The start position of the token. /// The end position of the token. - public BlockEntry(Mark start, Mark end) + public BlockEntry(in Mark start, in Mark end) : base(start, end) { } diff --git a/YamlDotNet/Core/Tokens/BlockMappingStart.cs b/YamlDotNet/Core/Tokens/BlockMappingStart.cs index 4cf95a98..edec2e96 100644 --- a/YamlDotNet/Core/Tokens/BlockMappingStart.cs +++ b/YamlDotNet/Core/Tokens/BlockMappingStart.cs @@ -39,7 +39,7 @@ public BlockMappingStart() /// /// The start position of the token. /// The end position of the token. - public BlockMappingStart(Mark start, Mark end) + public BlockMappingStart(in Mark start, in Mark end) : base(start, end) { } diff --git a/YamlDotNet/Core/Tokens/BlockSequenceStart.cs b/YamlDotNet/Core/Tokens/BlockSequenceStart.cs index c07c3c5f..a4157cdd 100644 --- a/YamlDotNet/Core/Tokens/BlockSequenceStart.cs +++ b/YamlDotNet/Core/Tokens/BlockSequenceStart.cs @@ -39,7 +39,7 @@ public BlockSequenceStart() /// /// The start position of the token. /// The end position of the token. - public BlockSequenceStart(Mark start, Mark end) + public BlockSequenceStart(in Mark start, in Mark end) : base(start, end) { } diff --git a/YamlDotNet/Core/Tokens/DocumentEnd.cs b/YamlDotNet/Core/Tokens/DocumentEnd.cs index 583a5317..d1892522 100644 --- a/YamlDotNet/Core/Tokens/DocumentEnd.cs +++ b/YamlDotNet/Core/Tokens/DocumentEnd.cs @@ -39,7 +39,7 @@ public DocumentEnd() /// /// The start position of the token. /// The end position of the token. - public DocumentEnd(Mark start, Mark end) + public DocumentEnd(in Mark start, in Mark end) : base(start, end) { } diff --git a/YamlDotNet/Core/Tokens/DocumentStart.cs b/YamlDotNet/Core/Tokens/DocumentStart.cs index 209939e3..0fe3ad83 100644 --- a/YamlDotNet/Core/Tokens/DocumentStart.cs +++ b/YamlDotNet/Core/Tokens/DocumentStart.cs @@ -39,7 +39,7 @@ public DocumentStart() /// /// The start position of the token. /// The end position of the token. - public DocumentStart(Mark start, Mark end) + public DocumentStart(in Mark start, in Mark end) : base(start, end) { } diff --git a/YamlDotNet/Core/Tokens/FlowEntry.cs b/YamlDotNet/Core/Tokens/FlowEntry.cs index b82399ed..103334e9 100644 --- a/YamlDotNet/Core/Tokens/FlowEntry.cs +++ b/YamlDotNet/Core/Tokens/FlowEntry.cs @@ -39,7 +39,7 @@ public FlowEntry() /// /// The start position of the token. /// The end position of the token. - public FlowEntry(Mark start, Mark end) + public FlowEntry(in Mark start, in Mark end) : base(start, end) { } diff --git a/YamlDotNet/Core/Tokens/FlowMappingEnd.cs b/YamlDotNet/Core/Tokens/FlowMappingEnd.cs index a21f5ffd..23770b3f 100644 --- a/YamlDotNet/Core/Tokens/FlowMappingEnd.cs +++ b/YamlDotNet/Core/Tokens/FlowMappingEnd.cs @@ -39,7 +39,7 @@ public FlowMappingEnd() /// /// The start position of the token. /// The end position of the token. - public FlowMappingEnd(Mark start, Mark end) + public FlowMappingEnd(in Mark start, in Mark end) : base(start, end) { } diff --git a/YamlDotNet/Core/Tokens/FlowMappingStart.cs b/YamlDotNet/Core/Tokens/FlowMappingStart.cs index 941efa5a..c75d070e 100644 --- a/YamlDotNet/Core/Tokens/FlowMappingStart.cs +++ b/YamlDotNet/Core/Tokens/FlowMappingStart.cs @@ -39,7 +39,7 @@ public FlowMappingStart() /// /// The start position of the token. /// The end position of the token. - public FlowMappingStart(Mark start, Mark end) + public FlowMappingStart(in Mark start, in Mark end) : base(start, end) { } diff --git a/YamlDotNet/Core/Tokens/FlowSequenceEnd.cs b/YamlDotNet/Core/Tokens/FlowSequenceEnd.cs index 93a7e7de..f7deaf65 100644 --- a/YamlDotNet/Core/Tokens/FlowSequenceEnd.cs +++ b/YamlDotNet/Core/Tokens/FlowSequenceEnd.cs @@ -39,7 +39,7 @@ public FlowSequenceEnd() /// /// The start position of the token. /// The end position of the token. - public FlowSequenceEnd(Mark start, Mark end) + public FlowSequenceEnd(in Mark start, in Mark end) : base(start, end) { } diff --git a/YamlDotNet/Core/Tokens/FlowSequenceStart.cs b/YamlDotNet/Core/Tokens/FlowSequenceStart.cs index 8d96ff08..35080ace 100644 --- a/YamlDotNet/Core/Tokens/FlowSequenceStart.cs +++ b/YamlDotNet/Core/Tokens/FlowSequenceStart.cs @@ -39,7 +39,7 @@ public FlowSequenceStart() /// /// The start position of the token. /// The end position of the token. - public FlowSequenceStart(Mark start, Mark end) + public FlowSequenceStart(in Mark start, in Mark end) : base(start, end) { } diff --git a/YamlDotNet/Core/Tokens/Key.cs b/YamlDotNet/Core/Tokens/Key.cs index 42d5f85f..fba6896f 100644 --- a/YamlDotNet/Core/Tokens/Key.cs +++ b/YamlDotNet/Core/Tokens/Key.cs @@ -39,7 +39,7 @@ public Key() /// /// The start position of the token. /// The end position of the token. - public Key(Mark start, Mark end) + public Key(in Mark start, in Mark end) : base(start, end) { } diff --git a/YamlDotNet/Core/Tokens/StreamEnd.cs b/YamlDotNet/Core/Tokens/StreamEnd.cs index 5886bba2..3169056d 100644 --- a/YamlDotNet/Core/Tokens/StreamEnd.cs +++ b/YamlDotNet/Core/Tokens/StreamEnd.cs @@ -39,7 +39,7 @@ public StreamEnd() /// /// The start position of the token. /// The end position of the token. - public StreamEnd(Mark start, Mark end) + public StreamEnd(in Mark start, in Mark end) : base(start, end) { } diff --git a/YamlDotNet/Core/Tokens/StreamStart.cs b/YamlDotNet/Core/Tokens/StreamStart.cs index 4579d385..6a8194cd 100644 --- a/YamlDotNet/Core/Tokens/StreamStart.cs +++ b/YamlDotNet/Core/Tokens/StreamStart.cs @@ -39,7 +39,7 @@ public StreamStart() /// /// The start position of the token. /// The end position of the token. - public StreamStart(Mark start, Mark end) + public StreamStart(in Mark start, in Mark end) : base(start, end) { } diff --git a/YamlDotNet/Core/Tokens/Token.cs b/YamlDotNet/Core/Tokens/Token.cs index 715442d4..d4cb81fe 100644 --- a/YamlDotNet/Core/Tokens/Token.cs +++ b/YamlDotNet/Core/Tokens/Token.cs @@ -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 { /// @@ -43,10 +41,10 @@ public abstract class Token /// /// The start position of the token. /// The end position of the token. - 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; } } } diff --git a/YamlDotNet/Core/Tokens/Value.cs b/YamlDotNet/Core/Tokens/Value.cs index 6aa917ad..ab8dfd8e 100644 --- a/YamlDotNet/Core/Tokens/Value.cs +++ b/YamlDotNet/Core/Tokens/Value.cs @@ -39,7 +39,7 @@ public Value() /// /// The start position of the token. /// The end position of the token. - public Value(Mark start, Mark end) + public Value(in Mark start, in Mark end) : base(start, end) { } diff --git a/YamlDotNet/Core/YamlException.cs b/YamlDotNet/Core/YamlException.cs index 3a207d09..7f24feb1 100644 --- a/YamlDotNet/Core/YamlException.cs +++ b/YamlDotNet/Core/YamlException.cs @@ -50,7 +50,7 @@ public YamlException(string message) /// /// Initializes a new instance of the class. /// - public YamlException(Mark start, Mark end, string message) + public YamlException(in Mark start, in Mark end, string message) : this(start, end, message, null) { } @@ -58,7 +58,7 @@ public YamlException(Mark start, Mark end, string message) /// /// Initializes a new instance of the class. /// - public YamlException(Mark start, Mark end, string message, Exception? innerException) + public YamlException(in Mark start, in Mark end, string message, Exception? innerException) : base(message, innerException) { Start = start; diff --git a/YamlDotNet/Helpers/ThrowHelper.cs b/YamlDotNet/Helpers/ThrowHelper.cs new file mode 100644 index 00000000..babccb72 --- /dev/null +++ b/YamlDotNet/Helpers/ThrowHelper.cs @@ -0,0 +1,35 @@ +// This file is part of YamlDotNet - A .NET library for YAML. +// Copyright (c) Antoine Aubry and contributors +// +// Permission is hereby granted, free of charge, to any person obtaining a copy of +// this software and associated documentation files (the "Software"), to deal in +// the Software without restriction, including without limitation the rights to +// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies +// of the Software, and to permit persons to whom the Software is furnished to do +// so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. + +using System; +using System.Runtime.CompilerServices; + +namespace YamlDotNet.Helpers +{ + internal static class ThrowHelper + { + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentOutOfRangeException(string paramName, string message) + { + throw new ArgumentOutOfRangeException(paramName, message); + } + } +}