about summary refs log blame commit diff
path: root/IrcStates/Casemap.cs
blob: 67867c55ca34d0023c4af10e8d88313777e6aa8d (plain) (tree)


















                                                                            
                                                                                     






                                                                    


                                                                                            

                                                                                                                
                  




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