using System; using System.Collections.Generic; using System.Linq; namespace XFEExtension.FormatExtension { /// /// XFE条目 /// public class XFEEntry { /// /// 头 /// public string Header { get; set; } /// /// 内容 /// public string Content { get; set; } /// /// 转换为字符串 /// /// public override string ToString() { return $"|{{+-[+-{Header.Replace("[+", "[++").Replace("+]", "++]").Replace("|{+", "|{++").Replace("+}|", "++}|")}-+][+-{Content.Replace("[+", "[++").Replace("+]", "++]").Replace("|{+", "|{++").Replace("+}|", "++}|")}-+]-+}}|"; } /// /// 将字符串转化为条目对象 /// /// /// public static XFEEntry ToEntry(string EntryString) { string[] messages = EntryString.Split(new string[] { "|{+-", "-+}|" }, StringSplitOptions.None); foreach (string message in messages) { string[] xFEDictionaryString = message.Split(new string[] { "[+-", "-+]" }, StringSplitOptions.None); if (xFEDictionaryString.Length == 5) { return new XFEEntry(xFEDictionaryString[1].Replace("[++", "[+").Replace("++]", "+]").Replace("|{++", "|{+").Replace("++}|", "+}|"), xFEDictionaryString[3].Replace("[++", "[+").Replace("++]", "+]").Replace("|{++", "|{+").Replace("++}|", "+}|")); } } return null; } /// /// XFE条目 /// /// 头 /// 内容 public XFEEntry(string Header, string Content) { this.Header = Header; this.Content = Content; } } /// /// XFE唯一字典存储 /// public class XFEDictionary : ICollection { private List xFEDictionaryList = new List(); /// /// 字典中的条目数 /// public int Count => xFEDictionaryList.Count; /// /// 只读 /// public bool IsReadOnly => false; /// /// 从字符串中加载字典 /// /// public void Load(string DictionaryString) { xFEDictionaryList = ToList(DictionaryString); } /// /// 追加条目 /// /// 条目对象 /// public void Add(XFEEntry entry) { if (Contains(entry.Header)) throw new XFEDictionaryException("Header的值应唯一!"); xFEDictionaryList.Add(entry); } /// /// 追加条目 /// /// 条目字符串 /// public void Add(string EntryString) { var entry = XFEEntry.ToEntry(EntryString); if (Contains(entry.Header)) throw new XFEDictionaryException("Header的值应唯一!"); xFEDictionaryList.Add(entry); } /// /// 追加条目 /// /// 头 /// 内容 /// public void Add(string Header, string Content) { if (Contains(Header)) throw new XFEDictionaryException("Header的值应唯一!"); xFEDictionaryList.Add(new XFEEntry(Header, Content)); } /// /// 追加字典 /// /// 字典对象 /// public void AddRange(List collection) { var duplicates = collection.GroupBy(entry => entry.Header) .Where(group => group.Count() > 1) .Select(group => group.Key); if (duplicates.Any()) throw new XFEDictionaryException("Header的值应唯一"); xFEDictionaryList.AddRange(collection); } /// /// 追加字典 /// /// 字典字符串 public void AddRange(string DictionaryString) { xFEDictionaryList.AddRange(ToList(DictionaryString)); } /// /// 插入条目 /// /// 插入位置 /// 条目 /// public void Insert(int index, XFEEntry entry) { if (Contains(entry.Header)) throw new XFEDictionaryException("Header的值应唯一!"); xFEDictionaryList.Insert(index, entry); } /// /// 插入条目 /// /// 插入位置 /// 条目 /// public void Insert(int index, string EntryString) { var entry = XFEEntry.ToEntry(EntryString); if (Contains(entry.Header)) throw new XFEDictionaryException("Header的值应唯一!"); xFEDictionaryList.Insert(index, entry); } /// /// 插入条目 /// /// 插入位置 /// 头 /// 内容 /// public void Insert(int index, string Header, string Content) { if (Contains(Header)) throw new XFEDictionaryException("Header的值应唯一!"); xFEDictionaryList.Insert(index, new XFEEntry(Header, Content)); } /// /// 清空字典 /// public void Clear() { xFEDictionaryList.Clear(); } /// /// 检测是否包含 /// /// 条目对象 /// public bool Contains(XFEEntry item) { return xFEDictionaryList.Contains(item); } /// /// 检测是否包含头 /// /// 指定头 /// public bool Contains(string HeaderString) { return xFEDictionaryList.Find(x => x.Header == HeaderString) != null; } /// /// 检测是否包含内容 /// /// 指定内容 /// public bool ContainContent(string ContentString) { return xFEDictionaryList.Find(x => x.Content == ContentString) != null; } /// /// 复制 /// /// 条目数组 /// 索引 public void CopyTo(XFEEntry[] array, int arrayIndex) { xFEDictionaryList.CopyTo(array, arrayIndex); } /// /// 获取索引器 /// /// public IEnumerator GetEnumerator() { return xFEDictionaryList.GetEnumerator(); } /// /// 移除指定的条目 /// /// 条目对象 /// public bool Remove(XFEEntry item) { return xFEDictionaryList.Remove(item); } /// /// 移除指定的条目 /// /// 头 /// public bool Remove(string HeaderString) { return xFEDictionaryList.Remove(xFEDictionaryList.Find(x => x.Header == HeaderString)); } /// /// 移除指定范围的数组 /// /// 起始位置 /// 移除长度 public void RemoveRange(int startIndex, int count) { xFEDictionaryList.RemoveRange(startIndex, count); } /// /// 索引器 /// /// 头 /// public string this[string HeaderString] { get { return xFEDictionaryList.Find(x => x.Header == HeaderString)?.Content; } set { xFEDictionaryList.Find(x => x.Header == HeaderString).Content = value; } } System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return xFEDictionaryList.GetEnumerator(); } /// /// 转换为字符串 /// /// public override string ToString() { string result = ""; foreach (var item in xFEDictionaryList) { result += item.ToString(); } return result; } /// /// 将字符串转换为XFEEntry的List /// /// 字符串 /// /// protected static List ToList(string DictionaryString) { List xFEList = new List(); string[] messages = DictionaryString.Split(new string[] { "|{+-", "-+}|" }, StringSplitOptions.None); foreach (string message in messages) { string[] xFEDictionaryString = message.Split(new string[] { "[+-", "-+]" }, StringSplitOptions.None); if (xFEDictionaryString.Length == 5) { xFEList.Add(new XFEEntry(xFEDictionaryString[1].Replace("[++", "[+").Replace("++]", "+]").Replace("|{++", "|{+").Replace("++}|", "+}|"), xFEDictionaryString[3].Replace("[++", "[+").Replace("++]", "+]").Replace("|{++", "|{+").Replace("++}|", "+}|"))); } } var duplicates = xFEList.GroupBy(entry => entry.Header) .Where(group => group.Count() > 1) .Select(group => group.Key); if (duplicates.Any()) throw new XFEDictionaryException("Header的值应唯一"); return xFEList; } /// /// 将字符串转换为XFEDictionary /// /// 字符串 /// /// public static XFEDictionary ToDictionary(string DictionaryString) { return new XFEDictionary(ToList(DictionaryString)); } /// /// XFE字典存储 /// /// 条目列表 public XFEDictionary(List xFEDictionaryList) { this.xFEDictionaryList = xFEDictionaryList; } /// /// XFE字典存储 /// /// /// public XFEDictionary(params string[] HeaderAndContentStrings) { if (!(HeaderAndContentStrings.Length % 2 == 0)) throw new XFEDictionaryException("输入的字符串数组格式不正确,应为Header,Content,Header,Content..."); for (int i = 0; i < HeaderAndContentStrings.Length; i += 2) { xFEDictionaryList.Add(new XFEEntry(HeaderAndContentStrings[i], HeaderAndContentStrings[i + 1])); } } /// /// XFE字典存储 /// /// 字典字符串 public XFEDictionary(string DictionaryString) { xFEDictionaryList = ToList(DictionaryString); } /// /// XFE字典存储 /// public XFEDictionary() { } } /// /// XFE可重复字典存储 /// public class XFEMultiDictionary : ICollection { private List xFEMultiDictionaryList = new List(); /// /// 字典中的条目数 /// public int Count => xFEMultiDictionaryList.Count; /// /// 只读 /// public bool IsReadOnly => false; /// /// 从字符串中加载字典 /// /// public void Load(string DictionaryString) { xFEMultiDictionaryList = ToList(DictionaryString); } /// /// 追加条目 /// /// 条目对象 public void Add(XFEEntry entry) { xFEMultiDictionaryList.Add(entry); } /// /// 追加条目 /// /// 条目字符串 public void Add(string EntryString) { xFEMultiDictionaryList.Add(XFEEntry.ToEntry(EntryString)); } /// /// 追加条目 /// /// 头 /// 内容 public void Add(string Header, string Content) { xFEMultiDictionaryList.Add(new XFEEntry(Header, Content)); } /// /// 追加字典 /// /// 字典对象 public void AddRange(List collection) { xFEMultiDictionaryList.AddRange(collection); } /// /// 追加字典 /// /// 字典字符串 public void AddRange(string DictionaryString) { xFEMultiDictionaryList.AddRange(ToList(DictionaryString)); } /// /// 插入条目 /// /// 插入位置 /// 条目 public void Insert(int index, XFEEntry entry) { xFEMultiDictionaryList.Insert(index, entry); } /// /// 插入条目 /// /// 插入位置 /// 条目 public void Insert(int index, string EntryString) { xFEMultiDictionaryList.Insert(index, XFEEntry.ToEntry(EntryString)); } /// /// 插入条目 /// /// 插入位置 /// 头 /// 内容 public void Insert(int index, string Header, string Content) { xFEMultiDictionaryList.Insert(index, new XFEEntry(Header, Content)); } /// /// 清空字典 /// public void Clear() { xFEMultiDictionaryList.Clear(); } /// /// 检测是否包含 /// /// 条目对象 /// public bool Contains(XFEEntry item) { return xFEMultiDictionaryList.Contains(item); } /// /// 检测是否包含头 /// /// 指定头 /// public bool Contains(string HeaderString) { return xFEMultiDictionaryList.Find(x => x.Header == HeaderString) != null; } /// /// 检测是否包含内容 /// /// 指定内容 /// public bool ContainContent(string ContentString) { return xFEMultiDictionaryList.Find(x => x.Content == ContentString) != null; } /// /// 复制 /// /// 条目数组 /// 索引 public void CopyTo(XFEEntry[] array, int arrayIndex) { xFEMultiDictionaryList.CopyTo(array, arrayIndex); } /// /// 获取内容 /// /// 头 /// public List GetContents(string header) { var list = new List(); xFEMultiDictionaryList.FindAll(x => x.Header == header)?.ForEach(y => list.Add(y.Content)); return list; } /// /// 获取索引器 /// /// public IEnumerator GetEnumerator() { return xFEMultiDictionaryList.GetEnumerator(); } /// /// 移除指定的条目 /// /// 条目对象 /// public bool Remove(XFEEntry item) { return xFEMultiDictionaryList.Remove(item); } /// /// 移除指定的条目 /// /// 头 /// public bool Remove(string HeaderString) { return xFEMultiDictionaryList.Remove(xFEMultiDictionaryList.Find(x => x.Header == HeaderString)); } /// /// 移除指定范围的数组 /// /// 起始位置 /// 移除长度 public void RemoveRange(int startIndex, int count) { xFEMultiDictionaryList.RemoveRange(startIndex, count); } /// /// 索引器 /// /// 头 /// public string this[string HeaderString] { get { return xFEMultiDictionaryList.Find(x => x.Header == HeaderString)?.Content; } set { xFEMultiDictionaryList.Find(x => x.Header == HeaderString).Content = value; } } System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return xFEMultiDictionaryList.GetEnumerator(); } /// /// 转换为字符串 /// /// public override string ToString() { string result = ""; foreach (var entry in xFEMultiDictionaryList) { result += entry.ToString(); } return result; } /// /// 将字符串转换为XFEEntry的List /// /// 字符串 /// protected static List ToList(string DictionaryString) { List xFEList = new List(); string[] messages = DictionaryString.Split(new string[] { "|{+-", "-+}|" }, StringSplitOptions.None); foreach (string message in messages) { string[] xFEDictionaryString = message.Split(new string[] { "[+-", "-+]" }, StringSplitOptions.None); if (xFEDictionaryString.Length == 5) { xFEList.Add(new XFEEntry(xFEDictionaryString[1].Replace("[++", "[+").Replace("++]", "+]").Replace("|{++", "|{+").Replace("++}|", "+}|"), xFEDictionaryString[3].Replace("[++", "[+").Replace("++]", "+]").Replace("|{++", "|{+").Replace("++}|", "+}|"))); } } return xFEList; } /// /// 将字符串转换为XFEMultiDictionary /// /// 字符串 /// public static XFEMultiDictionary ToMultiDictionary(string DictionaryString) { return new XFEMultiDictionary(ToList(DictionaryString)); } /// /// XFE字典存储 /// /// 条目列表 public XFEMultiDictionary(List xFEDictionaryList) { this.xFEMultiDictionaryList = xFEDictionaryList; } /// /// XFE字典存储 /// /// /// public XFEMultiDictionary(params string[] HeaderAndContentStrings) { if (!(HeaderAndContentStrings.Length % 2 == 0)) throw new XFEDictionaryException("输入的字符串数组格式不正确,应为Header,Content,Header,Content..."); for (int i = 0; i < HeaderAndContentStrings.Length; i += 2) { xFEMultiDictionaryList.Add(new XFEEntry(HeaderAndContentStrings[i], HeaderAndContentStrings[i + 1])); } } /// /// XFE字典存储 /// /// 字典字符串 public XFEMultiDictionary(string DictionaryString) { xFEMultiDictionaryList = ToList(DictionaryString); } /// /// XFE字典存储 /// public XFEMultiDictionary() { } } }