将选定的单词转换为特定的大小写

时间:2021-02-02 17:22:39

标签: c# .net .net-5 c#-8.0

我有一个单词列表,我需要将其格式化为每个单词的特定大小写,但是我当前使用的方法效率极低。

单词列表和每个单词所需的大小写:

    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" };

我使用的方法:

    public static string ToProperCaseHeader(this string header)
    {
        foreach (string properCaseHeader in ProperCaseHeaders)
            if (header.Equals(properCaseHeader, StringComparison.InvariantCultureIgnoreCase))
                return properCaseHeader;
        return header;
    }

以及性能分析结果:

enter image description here

如您所见,大部分时间都花在了使用 InvariantCultureIgnoreCase 的比较上。

标题可能出现在各种外壳中,在所有情况下,我都希望它们被标准化。例如:

CALL-ID、call-id、Call-Id、CALL-id 都应该标准化为 Call-ID。 CONTENT-TYPE、content-type、Content-type、CONTENT-type都应该归一化为Content-Type。

这不一定是简单的适当大小写转换。

有人有什么想法吗?

1 个答案:

答案 0 :(得分:1)

以下是您可以用代码替换以提高性能的代码。

class Program
{
    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" };

    static void Main(string[] args)
    {
        Dictionary<string, string> headers = new Dictionary<string, string>();

        foreach (var item in ProperCaseHeaders)
            headers.Add(item.ToLower(), item);

        Stopwatch stopwatch = Stopwatch.StartNew();
        Console.WriteLine($"Specific header for aleRt-Info {ProperCaseHeaders[3].ToProperCaseHeader(ProperCaseHeaders)}");
        Console.WriteLine($"Using Old Method End {stopwatch.ElapsedMilliseconds}");

        Console.WriteLine();

        stopwatch = Stopwatch.StartNew();
        Console.WriteLine($"Specific header for aleRt-Info {ProperCaseHeaders[3].GetProperCaseHeader(headers)}");
        Console.WriteLine($"Using New Method End {stopwatch.ElapsedMilliseconds}");

        Console.ReadLine();
    }

}

public static class Extension
{
    public static string ToProperCaseHeader(this string header, string[] ProperCaseHeaders)
    {
        foreach (string properCaseHeader in ProperCaseHeaders)
            if (header.Equals(properCaseHeader, StringComparison.InvariantCultureIgnoreCase))
                return properCaseHeader;
        return header;
    }

    public static string GetProperCaseHeader(this string header, Dictionary<string, string> headers)
    {
        var response = string.Empty;
        headers.TryGetValue(header.ToLower(), out response);
        return response;
    }
}

结果如下。

Specific header for aleRt-Info Alert-Info
Using Old Method End 48

Specific header for aleRt-Info Alert-Info
Using New Method End 1
相关问题