using Apewer; using System; using System.Collections.Generic; using System.Collections.Specialized; using System.Text; namespace Apewer { /// [Serializable] public sealed class StringPairs : List>, IToJson { /// 使用 key 选择器时忽略大小写。 /// 默认值:true public bool IngoreCase { get; set; } /// public StringPairs() : base() { IngoreCase = true; } /// public StringPairs(int capacity) : base(capacity) { IngoreCase = true; } /// public string this[string key] { get { return GetValue(key); } set { SetValue(key, value, true); } } /// 添加项。返回错误信息。 public string Add(string key, string value) { if (string.IsNullOrEmpty(key)) return "参数 Key 无效。"; // if (string.IsNullOrEmpty(value)) return "参数 Value 无效。"; var kvp = new KeyValuePair(key, value); Add(kvp); return null; } /// 获取所有 Key。 public string[] GetAllKeys() { var keys = new string[Count]; for (var i = 0; i < Count; i++) keys[i] = this[i].Key; return keys; } /// 获取匹配 Key 的所有 Value,不存在 Key 或 Value 时返回空数组。 public string[] GetValues(string key, bool ignoreCase = true, bool withEmpty = true) { if (string.IsNullOrEmpty(key)) return new string[0]; var lowerArg = TextUtility.Lower(key); var values = new List(); foreach (var item in this) { if (item.Key == key) { if (withEmpty || !string.IsNullOrEmpty(item.Value)) values.Add(item.Value); continue; } if (ignoreCase) { var lowerItem = TextUtility.Lower(item.Key); if (lowerItem == lowerArg) { if (withEmpty || !string.IsNullOrEmpty(item.Value)) values.Add(item.Value); continue; } } } return values.ToArray(); } /// 获取匹配 Key 的 Value。不存在 Key 时返回 NULL 值。 public string GetValue(string key, bool ignoreCase = true) { if (string.IsNullOrEmpty(key)) return null; // 先精准匹配。 foreach (var item in this) { if (string.IsNullOrEmpty(item.Key)) continue; if (item.Key == key) { if (string.IsNullOrEmpty(item.Value)) continue; return item.Value; } } // 再模糊匹配。 if (ignoreCase) { var lowerArg = TextUtility.Lower(key); foreach (var item in this) { if (string.IsNullOrEmpty(item.Key)) continue; var lowerKey = TextUtility.Lower(item.Key); if (lowerKey == lowerArg) { if (string.IsNullOrEmpty(item.Value)) continue; return item.Value; } } } return null; } /// 设置 Key 的 Value。不存在 Key 时添加新元素。 public void SetValue(string key, string value, bool ignoreCase = true) { if (string.IsNullOrEmpty(key)) return; // 先精准匹配。 for (var i = 0; i < Count; i++) { var item = this[i]; if (item.Key == key) { this[i] = new KeyValuePair(key, value); return; } } // 再模糊匹配。 if (ignoreCase) { var lower = TextUtility.Lower(key); for (var i = 0; i < Count; i++) { var item = this[i]; if (TextUtility.Lower(item.Key) == lower) { this[i] = new KeyValuePair(key, value); return; } } } Add(new KeyValuePair(key, value)); } /// 对 Key 升序排序。 public new void Sort() { SortAsc(); } /// 对 Key 升序排序。 public void SortAsc() { base.Sort(new Comparison>((a, b) => a.Key.CompareTo(b.Key))); } /// 对 Key 降序排序。 public void SortDesc() { base.Sort(new Comparison>((b, a) => a.Key.CompareTo(b.Key))); } /// 检查拥有指定的 Key。 public bool HasKey(string key, bool ignoreCase = false) { var a = ignoreCase ? key.Lower() : key; if (string.IsNullOrEmpty(a)) { foreach (var k in GetAllKeys()) { if (string.IsNullOrEmpty(k)) return true; } } else { foreach (var k in GetAllKeys()) { var b = ignoreCase ? k.Lower() : k; if (a == b) return true; } } return false; } /// public Json ToJson() => ToJson(false); /// public Json ToJson(bool lowerKey) { var array = Json.NewArray(); foreach (var pair in this) { var item = Json.NewObject(); item[lowerKey ? "key" : "Key"] = pair.Key ?? ""; item[lowerKey ? "value" : "Value"] = pair.Value ?? ""; array.AddItem(item); } return array; } /// 从指定对象解析,生成 StringPairs 对象实例为副本。 public static StringPairs From(NameValueCollection collection) { var sp = new StringPairs(); if (collection != null) { sp.Capacity = collection.Count; foreach (var key in collection.AllKeys) { sp.Add(key, collection[key]); } sp.Capacity = sp.Count; } return sp; } } }