Skip to content

Commit

Permalink
Allow overriding the max key length in the scanner
Browse files Browse the repository at this point in the history
  • Loading branch information
EdwardCooke committed Sep 1, 2024
1 parent 5240bf7 commit 3732b1c
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
17 changes: 17 additions & 0 deletions YamlDotNet.Test/Core/ScannerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,23 @@ public void VerifyTokensOnExample14()
StreamEnd);
}

[Fact]
public void SupportRealyLongStrings()
{
var longKey = string.Concat(Enumerable.Repeat("x", 1500));
var yamlString = $"{longKey}: value";
var scanner = new Scanner(new StringReader(yamlString), true, 1500);
AssertSequenceOfTokensFrom(scanner,
StreamStart,
BlockMappingStart,
Key,
PlainScalar(longKey),
Value,
PlainScalar("value"),
BlockEnd,
StreamEnd);
}

[Fact]
public void CommentsAreReturnedWhenRequested()
{
Expand Down
17 changes: 15 additions & 2 deletions YamlDotNet/Core/Scanner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ public class Scanner : IScanner
private Token? previous;
private Anchor? previousAnchor;
private Scalar? lastScalar = null;
private readonly int maxKeySize;

private bool IsDocumentStart() =>
!analyzer.EndOfInput &&
Expand Down Expand Up @@ -116,11 +117,23 @@ public Token? Current
/// </summary>
/// <param name="input">The input.</param>
/// <param name="skipComments">Indicates whether comments should be ignored</param>
public Scanner(TextReader input, bool skipComments = true)
public Scanner(TextReader input, bool skipComments = true):
this(input, skipComments, 1024)
{
}

/// <summary>
/// Initializes a new instance of the <see cref="Scanner"/> class.
/// </summary>
/// <param name="input">The input.</param>
/// <param name="skipComments">Indicates whether comments should be ignored</param>
/// <param name="maxKeySize">Override the default of 1024 characters for the max key size</param>
public Scanner(TextReader input, bool skipComments, int maxKeySize)
{
analyzer = new CharacterAnalyzer<LookAheadBuffer>(new LookAheadBuffer(input, 1024));
cursor = new Cursor();
SkipComments = skipComments;
this.maxKeySize = maxKeySize;
}

/// <summary>
Expand Down Expand Up @@ -265,7 +278,7 @@ private void StaleSimpleKeys()
// - is shorter than 1024 characters.


if (key.IsPossible && (key.Line < cursor.Line || key.Index + 1024 < cursor.Index))
if (key.IsPossible && (key.Line < cursor.Line || key.Index + maxKeySize < cursor.Index))
{

// Check if the potential simple key to be removed is required.
Expand Down

0 comments on commit 3732b1c

Please sign in to comment.