Remove some StringBuilder-related allocations from System.Net.Http (#… · dotnet/corefx@8c0487b (original) (raw)

This repository was archived by the owner on Jan 23, 2023. It is now read-only.

File tree

4 files changed

lines changed

4 files changed

lines changed

Original file line number Diff line number Diff line change
@@ -369,16 +369,15 @@ internal static bool TryParseInt64(string value, int offset, int length, out lon
369 369 return true;
370 370 }
371 371
372 -internal static string DumpHeaders(params HttpHeaders[] headers)
372 +internal static void DumpHeaders(StringBuilder sb, params HttpHeaders[] headers)
373 373 {
374 -// Return all headers as string similar to:
374 +// Appends all headers as string similar to:
375 375 // {
376 376 // HeaderName1: Value1
377 377 // HeaderName1: Value2
378 378 // HeaderName2: Value1
379 379 // ...
380 380 // }
381 -StringBuilder sb = new StringBuilder();
382 381 sb.Append("{\r\n");
383 382
384 383 for (int i = 0; i < headers.Length; i++)
@@ -400,8 +399,6 @@ internal static string DumpHeaders(params HttpHeaders[] headers)
400 399 }
401 400
402 401 sb.Append('}');
403 -
404 -return sb.ToString();
405 402 }
406 403
407 404 internal static bool IsValidEmailAddress(string value)
Original file line number Diff line number Diff line change
@@ -82,9 +82,9 @@ public override string ToString()
82 82 sb.Append(", ");
83 83 }
84 84
85 -sb.Append(range.From);
85 +if (range.From.HasValue) sb.Append(range.From.GetValueOrDefault());
86 86 sb.Append('-');
87 -sb.Append(range.To);
87 +if (range.To.HasValue) sb.Append(range.To.GetValueOrDefault());
88 88 }
89 89 }
90 90
Original file line number Diff line number Diff line change
@@ -174,7 +174,7 @@ public override string ToString()
174 174 sb.Append(_content == null ? "" : _content.GetType().ToString());
175 175
176 176 sb.Append(", Headers:\r\n");
177 -sb.Append(HeaderUtilities.DumpHeaders(_headers, _content == null ? null : _content.Headers));
177 +HeaderUtilities.DumpHeaders(sb, _headers, _content?.Headers);
178 178
179 179 return sb.ToString();
180 180 }
Original file line number Diff line number Diff line change
@@ -182,7 +182,7 @@ public override string ToString()
182 182 sb.Append(_content == null ? "" : _content.GetType().ToString());
183 183
184 184 sb.Append(", Headers:\r\n");
185 -sb.Append(HeaderUtilities.DumpHeaders(_headers, _content == null ? null : _content.Headers));
185 +HeaderUtilities.DumpHeaders(sb, _headers, _content?.Headers);
186 186
187 187 return sb.ToString();
188 188 }