Skip to content

Commit 0e18840

Browse files
committed
减少 AddForm(string? encodedForm)的分配
1 parent 27ae5bb commit 0e18840

File tree

1 file changed

+15
-14
lines changed

1 file changed

+15
-14
lines changed

WebApiClientCore/HttpContents/FormContent.cs

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -111,15 +111,18 @@ public void AddFormField(IEnumerable<KeyValue> keyValues)
111111
/// 添加已编码的原始内容表单
112112
/// </summary>
113113
/// <param name="encodedForm">表单内容</param>
114-
public void AddForm(string? encodedForm)
114+
public void AddForm(ReadOnlySpan<char> encodedForm)
115115
{
116-
if (encodedForm == null)
116+
this.EnsureNotBuffered();
117+
118+
if (encodedForm.Length > 0)
117119
{
118-
return;
120+
if (this.bufferWriter.WrittenCount > 0)
121+
{
122+
this.bufferWriter.Write((byte)'&');
123+
}
124+
httpEncoding.GetBytes(encodedForm, this.bufferWriter);
119125
}
120-
121-
var formBytes = httpEncoding.GetBytes(encodedForm);
122-
this.AddForm(formBytes);
123126
}
124127

125128
/// <summary>
@@ -130,16 +133,14 @@ public void AddForm(ReadOnlySpan<byte> encodedForm)
130133
{
131134
this.EnsureNotBuffered();
132135

133-
if (encodedForm.IsEmpty == true)
136+
if (encodedForm.Length > 0)
134137
{
135-
return;
136-
}
137-
138-
if (this.bufferWriter.WrittenCount > 0)
139-
{
140-
this.bufferWriter.Write((byte)'&');
138+
if (this.bufferWriter.WrittenCount > 0)
139+
{
140+
this.bufferWriter.Write((byte)'&');
141+
}
142+
this.bufferWriter.Write(encodedForm);
141143
}
142-
this.bufferWriter.Write(encodedForm);
143144
}
144145

145146
/// <summary>

0 commit comments

Comments
 (0)