diff --git a/src/SmartFormat.Tests/Core/ParserTests.cs b/src/SmartFormat.Tests/Core/ParserTests.cs index df11ccdb..a6625451 100644 --- a/src/SmartFormat.Tests/Core/ParserTests.cs +++ b/src/SmartFormat.Tests/Core/ParserTests.cs @@ -888,4 +888,11 @@ public void ParsingErrors_Serialization() var exception = ser.ReadObject(stream) as ParsingErrors; Assert.That(exception, Is.TypeOf()); } + + [Test] + public void Initialize_Format() + { + Assert.That(() => { _ = new Format().Initialize(new SmartSettings(), string.Empty, 0, 0, false); }, + Throws.Nothing, "Overload is marked as obsolete"); + } } diff --git a/src/SmartFormat/Core/Parsing/Format.cs b/src/SmartFormat/Core/Parsing/Format.cs index 9f05f35c..1f59d78d 100644 --- a/src/SmartFormat/Core/Parsing/Format.cs +++ b/src/SmartFormat/Core/Parsing/Format.cs @@ -83,14 +83,12 @@ public Format Initialize(SmartSettings smartSettings, string baseString, int sta /// The base format string- /// The start index within the format base string. /// The end index within the format base string. - /// if the nested formats exist. + /// if the format at least one nested . /// This instance. + [Obsolete("Use the overload without 'hasNested' instead.")] public Format Initialize(SmartSettings smartSettings, string baseString, int startIndex, int endIndex, bool hasNested) { - base.Initialize(smartSettings, null, baseString, startIndex, endIndex); - ParentPlaceholder = null; - HasNested = hasNested; - + Initialize(smartSettings, baseString, startIndex, endIndex); return this; } @@ -104,7 +102,6 @@ public void ReturnToPool() Clear(); ParentPlaceholder = null; - HasNested = false; #pragma warning disable S3267 // Don't use LINQ in favor of less GC // Return and clear FormatItems we own @@ -143,11 +140,11 @@ public void ReturnToPool() /// Gets the of s. /// public List Items { get; } = new(); - + /// - /// Returns , if the is nested. + /// Returns , if the contain at least one nested . /// - public bool HasNested { get; internal set; } + public bool HasNested => Items.Exists(i => i is Placeholder); #endregion @@ -199,7 +196,6 @@ public Format Substring(int start, int length) else { // item is a placeholder -- we can't split a placeholder though. - substring.HasNested = true; } substring.Items.Add(newItem); diff --git a/src/SmartFormat/Core/Parsing/Parser.cs b/src/SmartFormat/Core/Parsing/Parser.cs index cd4cac42..a84dd29a 100644 --- a/src/SmartFormat/Core/Parsing/Parser.cs +++ b/src/SmartFormat/Core/Parsing/Parser.cs @@ -398,7 +398,6 @@ private void CreateNewPlaceholder(ref int nestedDepth, out Placeholder newPlaceh nestedDepth++; newPlaceholder = PlaceholderPool.Instance.Get().Initialize(_resultFormat, _index.Current, nestedDepth); _resultFormat.Items.Add(newPlaceholder); - _resultFormat.HasNested = true; _index.Operator = _index.SafeAdd(_index.Current, 1); _index.Selector = 0; _index.NamedFormatterStart = PositionUndefined; diff --git a/src/SmartFormat/Extensions/ListFormatter.cs b/src/SmartFormat/Extensions/ListFormatter.cs index b1b42ee1..f3fc6323 100644 --- a/src/SmartFormat/Extensions/ListFormatter.cs +++ b/src/SmartFormat/Extensions/ListFormatter.cs @@ -191,7 +191,7 @@ public bool TryEvaluateFormat(IFormattingInfo formattingInfo) // The format is not nested, // so we will treat it as an ItemFormat: var newItemFormat = FormatPool.Instance.Get().Initialize(_smartSettings, itemFormat.BaseString, - itemFormat.StartIndex, itemFormat.EndIndex, true); + itemFormat.StartIndex, itemFormat.EndIndex); itemFormat.ParentPlaceholder = formattingInfo.Placeholder; var newPlaceholder = PlaceholderPool.Instance.Get().Initialize(newItemFormat, itemFormat.StartIndex, 0);