IRC parsing, tokenization, and state handling in C#
1// ReSharper disable IdentifierTypo
2
3using System.Text;
4
5namespace IRCStates;
6
7public static class Casemap
8{
9 public enum CaseMapping
10 {
11 Rfc1459,
12 Ascii
13 }
14
15 private const string AsciiUpperChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
16 private const string AsciiLowerChars = "abcdefghijklmnopqrstuvwxyz";
17 private const string Rfc1459UpperChars = AsciiUpperChars + @"[]~\";
18 private const string Rfc1459LowerChars = AsciiLowerChars + "{}^|";
19
20 private static string Replace(string s, string upperSet, string lowerSet)
21 {
22 StringBuilder sb = new(s);
23 for (var i = 0; i < upperSet.Length; i++)
24 {
25 sb.Replace(upperSet[i], lowerSet[i]);
26 }
27
28 return sb.ToString();
29 }
30
31 public static string CaseFold(CaseMapping mapping, string s)
32 {
33 return s == null
34 ? string.Empty
35 : mapping switch
36 {
37 CaseMapping.Rfc1459 => Replace(s, Rfc1459UpperChars, Rfc1459LowerChars),
38 CaseMapping.Ascii => Replace(s, AsciiUpperChars, AsciiLowerChars),
39 _ => throw new ArgumentOutOfRangeException(nameof(mapping), mapping, null)
40 };
41 }
42}