From 933a4f85604e21445c9bac8272d64cf3e6f65e00 Mon Sep 17 00:00:00 2001 From: Ben Harris Date: Wed, 22 Apr 2020 16:28:51 -0400 Subject: rename to IrcSharp also tidy up formatting with vs tools --- IrcStates/Casemap.cs | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 IrcStates/Casemap.cs (limited to 'IrcStates/Casemap.cs') diff --git a/IrcStates/Casemap.cs b/IrcStates/Casemap.cs new file mode 100644 index 0000000..484c490 --- /dev/null +++ b/IrcStates/Casemap.cs @@ -0,0 +1,43 @@ +using System; + +namespace IrcStates +{ + public static class Casemap + { + public enum CaseMapping + { + Rfc1459, + Ascii + } + + private const string AsciiUpperChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; + private const string AsciiLowerChars = "abcdefghijklmnopqrstuvwxyz"; + private const string Rfc1459UpperChars = AsciiUpperChars + @"[]~\"; + private const string Rfc1459LowerChars = AsciiLowerChars + @"{}^|"; + + private static string Replace(string s, string upper, string lower) + { + for (var i = 0; i < upper.Length; ++i) + { + s = s.Replace(upper[i], lower[i]); + } + + return s; + } + + public static string CaseFold(CaseMapping mapping, string s) + { + if (s != null) + { + return mapping switch + { + CaseMapping.Rfc1459 => Replace(s, Rfc1459UpperChars, Rfc1459LowerChars), + CaseMapping.Ascii => Replace(s, AsciiUpperChars, AsciiLowerChars), + _ => throw new ArgumentOutOfRangeException(nameof(mapping), mapping, null) + }; + } + + return string.Empty; + } + } +} -- cgit 1.4.1