From da947f20b66695143006b49f615437ac1e5b7cf1 Mon Sep 17 00:00:00 2001 From: morkt Date: Mon, 22 Jan 2018 11:36:31 +0400 Subject: [PATCH] (StringExtensions): added ToLowerAscii and ToLowerShiftJis methods. --- ArcFormats/ArcCommon.cs | 43 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/ArcFormats/ArcCommon.cs b/ArcFormats/ArcCommon.cs index ded88198..866692a3 100644 --- a/ArcFormats/ArcCommon.cs +++ b/ArcFormats/ArcCommon.cs @@ -151,6 +151,49 @@ namespace GameRes.Formats else return ext_start + 1; } + + /// + /// Returns copy of this string with ASCII characters converted to lower case. + /// + public static string ToLowerAscii (this string str) + { + int i; + for (i = 0; i < str.Length; ++i) + { + if (str[i] >= 'A' && str[i] <= 'Z') + break; + } + if (i == str.Length) + return str; + var builder = new System.Text.StringBuilder (str, 0, i, str.Length); + builder.Append ((char)(str[i++] | 0x20)); + while (i < str.Length) + { + char c = str[i]; + if (c >= 'A' && c <= 'Z') + c = (char)(c | 0x20); + builder.Append (c); + ++i; + } + return builder.ToString(); + } + + /// + /// Returns string in Shift-JIS encoding with ASCII characters converted to lower case. + /// + public static byte[] ToLowerShiftJis (this string text) + { + var text_bytes = Encodings.cp932.GetBytes (text); + for (int i = 0; i < text_bytes.Length; ++i) + { + byte c = text_bytes[i]; + if (c >= 'A' && c <= 'Z') + text_bytes[i] += 0x20; + else if (c > 0x7F && c < 0xA1 || c > 0xDF) + ++i; + } + return text_bytes; + } } ///