Skip to content

Commit

Permalink
feat: Format.HasNested checks for existing Placeholder in Items (axun…
Browse files Browse the repository at this point in the history
…o#416)

* feat: Format.HasNested checks for existing Placeholder

Closes  axuno#415
* `Format.HasNested` checks Items for existing `Placeholder`
* Update Format Class Initialization (mark overload with arg `bool hasNested` as obsolete
  • Loading branch information
axunonb authored Jun 9, 2024
1 parent cbb27ee commit e28d6a8
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 12 deletions.
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

0 comments on commit e28d6a8

Please sign in to comment.