diff --git a/YamlDotNet.Test/Core/ScannerTests.cs b/YamlDotNet.Test/Core/ScannerTests.cs index eb3cd93a..5431879a 100644 --- a/YamlDotNet.Test/Core/ScannerTests.cs +++ b/YamlDotNet.Test/Core/ScannerTests.cs @@ -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() { diff --git a/YamlDotNet/Core/Scanner.cs b/YamlDotNet/Core/Scanner.cs index 7b98a270..da0f58c5 100644 --- a/YamlDotNet/Core/Scanner.cs +++ b/YamlDotNet/Core/Scanner.cs @@ -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 && @@ -117,10 +118,22 @@ public Token? Current /// The input. /// Indicates whether comments should be ignored public Scanner(TextReader input, bool skipComments = true) + : this(input, skipComments, 1024) + { + } + + /// + /// Initializes a new instance of the class. + /// + /// The input. + /// Indicates whether comments should be ignored + /// Override the default of 1024 characters for the max key size + public Scanner(TextReader input, bool skipComments, int maxKeySize) { analyzer = new CharacterAnalyzer(new LookAheadBuffer(input, 1024)); cursor = new Cursor(); SkipComments = skipComments; + this.maxKeySize = maxKeySize; } /// @@ -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.