You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

229 lines
7.0 KiB

4 years ago
3 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
4 years ago
3 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
3 years ago
4 years ago
4 years ago
4 years ago
  1. using Apewer;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Collections.Specialized;
  5. using System.Text;
  6. namespace Apewer
  7. {
  8. /// <summary></summary>
  9. [Serializable]
  10. public sealed class StringPairs : List<KeyValuePair<string, string>>, IToJson
  11. {
  12. /// <summary>使用 key 选择器时忽略大小写。</summary>
  13. /// <remarks>默认值:true</remarks>
  14. public bool IngoreCase { get; set; }
  15. /// <summary></summary>
  16. public StringPairs() : base()
  17. {
  18. IngoreCase = true;
  19. }
  20. /// <summary></summary>
  21. public StringPairs(int capacity) : base(capacity)
  22. {
  23. IngoreCase = true;
  24. }
  25. /// <summary></summary>
  26. public string this[string key]
  27. {
  28. get { return GetValue(key); }
  29. set { SetValue(key, value, true); }
  30. }
  31. /// <summary>添加项。返回错误信息。</summary>
  32. public string Add(string key, string value)
  33. {
  34. if (string.IsNullOrEmpty(key)) return "参数 Key 无效。";
  35. // if (string.IsNullOrEmpty(value)) return "参数 Value 无效。";
  36. var kvp = new KeyValuePair<string, string>(key, value);
  37. Add(kvp);
  38. return null;
  39. }
  40. /// <summary>获取所有 Key。</summary>
  41. public string[] GetAllKeys()
  42. {
  43. var keys = new string[Count];
  44. for (var i = 0; i < Count; i++) keys[i] = this[i].Key;
  45. return keys;
  46. }
  47. /// <summary>获取匹配 Key 的所有 Value,不存在 Key 或 Value 时返回空数组。</summary>
  48. public string[] GetValues(string key, bool ignoreCase = true, bool withEmpty = true)
  49. {
  50. if (string.IsNullOrEmpty(key)) return new string[0];
  51. var lowerArg = TextUtility.Lower(key);
  52. var values = new List<string>();
  53. foreach (var item in this)
  54. {
  55. if (item.Key == key)
  56. {
  57. if (withEmpty || !string.IsNullOrEmpty(item.Value)) values.Add(item.Value);
  58. continue;
  59. }
  60. if (ignoreCase)
  61. {
  62. var lowerItem = TextUtility.Lower(item.Key);
  63. if (lowerItem == lowerArg)
  64. {
  65. if (withEmpty || !string.IsNullOrEmpty(item.Value)) values.Add(item.Value);
  66. continue;
  67. }
  68. }
  69. }
  70. return values.ToArray();
  71. }
  72. /// <summary>获取匹配 Key 的 Value。不存在 Key 时返回 NULL 值。</summary>
  73. public string GetValue(string key, bool ignoreCase = true)
  74. {
  75. if (string.IsNullOrEmpty(key)) return null;
  76. // 先精准匹配。
  77. foreach (var item in this)
  78. {
  79. if (string.IsNullOrEmpty(item.Key)) continue;
  80. if (item.Key == key)
  81. {
  82. if (string.IsNullOrEmpty(item.Value)) continue;
  83. return item.Value;
  84. }
  85. }
  86. // 再模糊匹配。
  87. if (ignoreCase)
  88. {
  89. var lowerArg = TextUtility.Lower(key);
  90. foreach (var item in this)
  91. {
  92. if (string.IsNullOrEmpty(item.Key)) continue;
  93. var lowerKey = TextUtility.Lower(item.Key);
  94. if (lowerKey == lowerArg)
  95. {
  96. if (string.IsNullOrEmpty(item.Value)) continue;
  97. return item.Value;
  98. }
  99. }
  100. }
  101. return null;
  102. }
  103. /// <summary>设置 Key 的 Value。不存在 Key 时添加新元素。</summary>
  104. public void SetValue(string key, string value, bool ignoreCase = true)
  105. {
  106. if (string.IsNullOrEmpty(key)) return;
  107. // 先精准匹配。
  108. for (var i = 0; i < Count; i++)
  109. {
  110. var item = this[i];
  111. if (item.Key == key)
  112. {
  113. this[i] = new KeyValuePair<string, string>(key, value);
  114. return;
  115. }
  116. }
  117. // 再模糊匹配。
  118. if (ignoreCase)
  119. {
  120. var lower = TextUtility.Lower(key);
  121. for (var i = 0; i < Count; i++)
  122. {
  123. var item = this[i];
  124. if (TextUtility.Lower(item.Key) == lower)
  125. {
  126. this[i] = new KeyValuePair<string, string>(key, value);
  127. return;
  128. }
  129. }
  130. }
  131. Add(new KeyValuePair<string, string>(key, value));
  132. }
  133. /// <summary>对 Key 升序排序。</summary>
  134. public new void Sort()
  135. {
  136. SortAsc();
  137. }
  138. /// <summary>对 Key 升序排序。</summary>
  139. public void SortAsc()
  140. {
  141. base.Sort(new Comparison<KeyValuePair<string, string>>((a, b) => a.Key.CompareTo(b.Key)));
  142. }
  143. /// <summary>对 Key 降序排序。</summary>
  144. public void SortDesc()
  145. {
  146. base.Sort(new Comparison<KeyValuePair<string, string>>((b, a) => a.Key.CompareTo(b.Key)));
  147. }
  148. /// <summary>检查拥有指定的 Key。</summary>
  149. public bool HasKey(string key, bool ignoreCase = false)
  150. {
  151. var a = ignoreCase ? key.Lower() : key;
  152. if (string.IsNullOrEmpty(a))
  153. {
  154. foreach (var k in GetAllKeys())
  155. {
  156. if (string.IsNullOrEmpty(k)) return true;
  157. }
  158. }
  159. else
  160. {
  161. foreach (var k in GetAllKeys())
  162. {
  163. var b = ignoreCase ? k.Lower() : k;
  164. if (a == b) return true;
  165. }
  166. }
  167. return false;
  168. }
  169. /// <summary></summary>
  170. public Json ToJson() => ToJson(false);
  171. /// <summary></summary>
  172. public Json ToJson(bool lowerKey)
  173. {
  174. var array = Json.NewArray();
  175. foreach (var pair in this)
  176. {
  177. var item = Json.NewObject();
  178. item[lowerKey ? "key" : "Key"] = pair.Key ?? "";
  179. item[lowerKey ? "value" : "Value"] = pair.Value ?? "";
  180. array.AddItem(item);
  181. }
  182. return array;
  183. }
  184. /// <summary>从指定对象解析,生成 StringPairs 对象实例为副本。</summary>
  185. public static StringPairs From(NameValueCollection collection)
  186. {
  187. var sp = new StringPairs();
  188. if (collection != null)
  189. {
  190. sp.Capacity = collection.Count;
  191. foreach (var key in collection.AllKeys)
  192. {
  193. sp.Add(key, collection[key]);
  194. }
  195. sp.Capacity = sp.Count;
  196. }
  197. return sp;
  198. }
  199. }
  200. }