Skip to content

Commit 38f7ca1

Browse files
authored
Add DefaultInterpolatedStringHandler.Text/Clear (#112171)
1 parent f99e58b commit 38f7ca1

File tree

3 files changed

+40
-8
lines changed

3 files changed

+40
-8
lines changed

src/libraries/System.Private.CoreLib/src/System/Runtime/CompilerServices/DefaultInterpolatedStringHandler.cs

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,8 @@ internal static int GetDefaultLength(int literalLength, int formattedCount) =>
104104
/// <remarks>
105105
/// This releases any resources used by the handler. The method should be invoked only
106106
/// once and as the last thing performed on the handler. Subsequent use is erroneous, ill-defined,
107-
/// and may destabilize the process, as may using any other copies of the handler after ToStringAndClear
108-
/// is called on any one of them.
107+
/// and may destabilize the process, as may using any other copies of the handler after
108+
/// <see cref="ToStringAndClear" /> is called on any one of them.
109109
/// </remarks>
110110
public string ToStringAndClear()
111111
{
@@ -114,20 +114,31 @@ public string ToStringAndClear()
114114
return result;
115115
}
116116

117-
/// <summary>Clears the handler, returning any rented array to the pool.</summary>
118-
[MethodImpl(MethodImplOptions.AggressiveInlining)] // used only on a few hot paths
119-
internal void Clear()
117+
/// <summary>Clears the handler.</summary>
118+
/// <remarks>
119+
/// This releases any resources used by the handler. The method should be invoked only
120+
/// once and as the last thing performed on the handler. Subsequent use is erroneous, ill-defined,
121+
/// and may destabilize the process, as may using any other copies of the handler after <see cref="Clear"/>
122+
/// is called on any one of them.
123+
/// </remarks>
124+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
125+
public void Clear()
120126
{
121127
char[]? toReturn = _arrayToReturnToPool;
122-
this = default; // defensive clear
128+
129+
// Defensive clear
130+
_arrayToReturnToPool = null;
131+
_chars = default;
132+
_pos = 0;
133+
123134
if (toReturn is not null)
124135
{
125136
ArrayPool<char>.Shared.Return(toReturn);
126137
}
127138
}
128139

129-
/// <summary>Gets a span of the written characters thus far.</summary>
130-
internal ReadOnlySpan<char> Text => _chars.Slice(0, _pos);
140+
/// <summary>Gets a span of the characters appended to the handler.</summary>
141+
public ReadOnlySpan<char> Text => _chars.Slice(0, _pos);
131142

132143
/// <summary>Writes the specified string to the handler.</summary>
133144
/// <param name="value">The string to write.</param>

src/libraries/System.Runtime/ref/System.Runtime.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13422,6 +13422,8 @@ public void AppendFormatted<T>(T value, int alignment) { }
1342213422
public void AppendFormatted<T>(T value, int alignment, string? format) { }
1342313423
public void AppendFormatted<T>(T value, string? format) { }
1342413424
public void AppendLiteral(string value) { }
13425+
public void Clear() { }
13426+
public System.ReadOnlySpan<char> Text { get { throw null; } }
1342513427
public override string ToString() { throw null; }
1342613428
public string ToStringAndClear() { throw null; }
1342713429
}

src/libraries/System.Runtime/tests/System.Runtime.Tests/System/Runtime/CompilerServices/DefaultInterpolatedStringHandlerTests.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,16 @@ public void ToStringAndClear_Clears()
5353
Assert.Equal(string.Empty, handler.ToStringAndClear());
5454
}
5555

56+
[Fact]
57+
public void Clear_Clears()
58+
{
59+
DefaultInterpolatedStringHandler handler = new DefaultInterpolatedStringHandler(0, 0);
60+
handler.AppendLiteral("hi");
61+
Assert.Equal("hi", handler.Text.ToString());
62+
handler.Clear();
63+
Assert.Equal(string.Empty, handler.Text.ToString());
64+
}
65+
5666
[Fact]
5767
public void AppendLiteral()
5868
{
@@ -65,6 +75,7 @@ public void AppendLiteral()
6575
actual.AppendLiteral(s);
6676
}
6777

78+
Assert.Equal(expected.ToString(), actual.Text.ToString());
6879
Assert.Equal(expected.ToString(), actual.ToStringAndClear());
6980
}
7081

@@ -96,6 +107,7 @@ public void AppendFormatted_ReadOnlySpanChar()
96107
}
97108
}
98109

110+
Assert.Equal(expected.ToString(), actual.Text.ToString());
99111
Assert.Equal(expected.ToString(), actual.ToStringAndClear());
100112
}
101113

@@ -127,6 +139,7 @@ public void AppendFormatted_String()
127139
}
128140
}
129141

142+
Assert.Equal(expected.ToString(), actual.Text.ToString());
130143
Assert.Equal(expected.ToString(), actual.ToStringAndClear());
131144
}
132145

@@ -157,6 +170,7 @@ public void AppendFormatted_String_ICustomFormatter()
157170
actual.AppendFormatted(s, -3, "X2");
158171
}
159172

173+
Assert.Equal(expected.ToString(), actual.Text.ToString());
160174
Assert.Equal(expected.ToString(), actual.ToStringAndClear());
161175
}
162176

@@ -217,6 +231,7 @@ public void AppendFormatted_ReferenceTypes()
217231
}
218232
}
219233

234+
Assert.Equal(expected.ToString(), actual.Text.ToString());
220235
Assert.Equal(expected.ToString(), actual.ToStringAndClear());
221236
}
222237

@@ -289,6 +304,7 @@ void AssertTss(IHasToStringState tss, string format)
289304
}
290305
}
291306

307+
Assert.Equal(expected.ToString(), actual.Text.ToString());
292308
Assert.Equal(expected.ToString(), actual.ToStringAndClear());
293309
}
294310

@@ -327,6 +343,7 @@ void Test<T>(T t)
327343
AssertModeMatchesType(((IHasToStringState)t));
328344
}
329345

346+
Assert.Equal(expected.ToString(), actual.Text.ToString());
330347
Assert.Equal(expected.ToString(), actual.ToStringAndClear());
331348
}
332349

@@ -406,6 +423,7 @@ void AssertTss(T tss, string format)
406423
actual.AppendFormatted(t, -3, "X2");
407424
AssertTss(t, "X2");
408425

426+
Assert.Equal(expected.ToString(), actual.Text.ToString());
409427
Assert.Equal(expected.ToString(), actual.ToStringAndClear());
410428
}
411429

@@ -434,6 +452,7 @@ public void Grow_Large(bool useScratch)
434452
expected.AppendFormat("{0,3}", i);
435453
}
436454

455+
Assert.Equal(expected.ToString(), handler.Text.ToString());
437456
Assert.Equal(expected.ToString(), handler.ToStringAndClear());
438457
}
439458

0 commit comments

Comments
 (0)