I have a list of words that I need to format to specific casing per word, however the current method I'm using is highly inefficient.
The list of words and the needed casing per word:
private static readonly string[] ProperCaseHeaders = { "Accept", "Accept-Encoding", "Accept-Language", "Alert-Info", "Allow", "Authentication-Info", "Authorization", "Call-ID", "Call-Info", "Contact", "Content-Disposition", "Content-Encoding", "Content-Language", "Content-Length", "Content-Type", "CSeq", "Date", "Error-Info", "Expires", "From", "In-Reply-To", "Max-Forwards", "Min-Expires", "MIME-Version", "Organization", "Priority", "Proxy-Authenticate", "Proxy-Authorization", "Proxy-Require", "Record-Route", "Reply-To", "Require", "Retry-After", "Route", "Server", "Subject", "Supported", "Timestamp", "To", "Unsupported", "User-Agent", "Via", "Warning", "WWW-Authenticate" };
The method I'm using:
public static string ToProperCaseHeader(this string header)
{
foreach (string properCaseHeader in ProperCaseHeaders)
if (header.Equals(properCaseHeader, StringComparison.InvariantCultureIgnoreCase))
return properCaseHeader;
return header;
}
And the performance profiling results:
As you can see, the majority of the time is spent on the comparaison using InvariantCultureIgnoreCase.
Its possible for headers to arrive in a variety of casings, an in all cases, I want them to be normalized. For example:
CALL-ID, call-id, Call-Id, CALL-id should all be normalized to Call-ID. CONTENT-TYPE, content-type, Content-type, CONTENT-type should all be normalized to Content-Type.
It's not necessarily a simple Proper Case conversion.
Anyone have any ideas?