* Remove boxing and ToString allocations from RangeHeaderValue.ToString
The current code uses ToString(object), boxng the long? and then calling ToString on it. By instead first checking whether the value is null and only calling ToString(long), we avoid both the box and the string allocations, for both From and To, relying on StringBuilder's ability to format a long directly into its buffer.
* Avoid StringBuilder/char[]/string allocation/copy in DumpHeaders
We can just write into the existing StringBuilder rather than creating a new one, appending to that, copying it to a string, and then appending that string to the original.
Commit migrated from https://github.com/dotnet/corefx/commit/
8c0487bfeff9229beca93dc480028b83d8e39705
return true;
}
- internal static string DumpHeaders(params HttpHeaders[] headers)
+ internal static void DumpHeaders(StringBuilder sb, params HttpHeaders[] headers)
{
- // Return all headers as string similar to:
+ // Appends all headers as string similar to:
// {
// HeaderName1: Value1
// HeaderName1: Value2
// HeaderName2: Value1
// ...
// }
- StringBuilder sb = new StringBuilder();
sb.Append("{\r\n");
for (int i = 0; i < headers.Length; i++)
}
sb.Append('}');
-
- return sb.ToString();
}
internal static bool IsValidEmailAddress(string value)
sb.Append(", ");
}
- sb.Append(range.From);
+ if (range.From.HasValue) sb.Append(range.From.GetValueOrDefault());
sb.Append('-');
- sb.Append(range.To);
+ if (range.To.HasValue) sb.Append(range.To.GetValueOrDefault());
}
}
sb.Append(_content == null ? "<null>" : _content.GetType().ToString());
sb.Append(", Headers:\r\n");
- sb.Append(HeaderUtilities.DumpHeaders(_headers, _content == null ? null : _content.Headers));
+ HeaderUtilities.DumpHeaders(sb, _headers, _content?.Headers);
return sb.ToString();
}
sb.Append(_content == null ? "<null>" : _content.GetType().ToString());
sb.Append(", Headers:\r\n");
- sb.Append(HeaderUtilities.DumpHeaders(_headers, _content == null ? null : _content.Headers));
+ HeaderUtilities.DumpHeaders(sb, _headers, _content?.Headers);
return sb.ToString();
}