Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Format.HasNested checks for existing Placeholder in Items #416

Merged
merged 2 commits into from
Jun 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/SmartFormat.Tests/Core/ParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -888,4 +888,11 @@ public void ParsingErrors_Serialization()
var exception = ser.ReadObject(stream) as ParsingErrors;
Assert.That(exception, Is.TypeOf<ParsingErrors>());
}

[Test]
public void Initialize_Format()
{
Assert.That(() => { _ = new Format().Initialize(new SmartSettings(), string.Empty, 0, 0, false); },
Throws.Nothing, "Overload is marked as obsolete");
}
}
16 changes: 6 additions & 10 deletions src/SmartFormat/Core/Parsing/Format.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,12 @@ public Format Initialize(SmartSettings smartSettings, string baseString, int sta
/// <param name="baseString">The base format string-</param>
/// <param name="startIndex">The start index within the format base string.</param>
/// <param name="endIndex">The end index within the format base string.</param>
/// <param name="hasNested"><see langword="true"/> if the nested formats exist.</param>
/// <param name="hasNested"><see langword="true"/> if the format at least one nested <see cref="Placeholder"/>.</param>
/// <returns>This <see cref="Format"/> instance.</returns>
[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;
}

Expand All @@ -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
Expand Down Expand Up @@ -143,11 +140,11 @@ public void ReturnToPool()
/// Gets the <see cref="List{T}"/> of <see cref="FormatItem"/>s.
/// </summary>
public List<FormatItem> Items { get; } = new();

/// <summary>
/// Returns <see langword="true"/>, if the <see cref="Format"/> is nested.
/// Returns <see langword="true"/>, if the <see cref="Items"/> contain at least one nested <see cref="Placeholder"/>.
/// </summary>
public bool HasNested { get; internal set; }
public bool HasNested => Items.Exists(i => i is Placeholder);

#endregion

Expand Down Expand Up @@ -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);
Expand Down
1 change: 0 additions & 1 deletion src/SmartFormat/Core/Parsing/Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/SmartFormat/Extensions/ListFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down