Skip to content

Commit

Permalink
Merge pull request #965 from EdwardCooke/ec-bigkey
Browse files Browse the repository at this point in the history
Support keys larger > 1024 characters
  • Loading branch information
EdwardCooke committed Sep 1, 2024
2 parents 62b7f59 + f8cbe26 commit 03f0e59
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
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
15 changes: 14 additions & 1 deletion 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 @@ -117,10 +118,22 @@ public Token? Current
/// <param name="input">The input.</param>
/// <param name="skipComments">Indicates whether comments should be ignored</param>
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 03f0e59

Please sign in to comment.