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.

116 lines
3.6 KiB

  1. using Apewer;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. namespace Apewer.Models
  6. {
  7. /// <summary></summary>
  8. [Serializable]
  9. public class StringPairs : List<KeyValuePair<string, string>>
  10. {
  11. /// <summary>添加项。返回错误信息。</summary>
  12. public string Add(string key, string value)
  13. {
  14. if (string.IsNullOrEmpty(key)) return "参数 Name 无效。";
  15. if (string.IsNullOrEmpty(value)) return "参数 Value 无效。";
  16. var kvp = new KeyValuePair<string, string>(key, value);
  17. Add(kvp);
  18. return null;
  19. }
  20. /// <summary>获取所有 Key。</summary>
  21. public string[] GetAllKeys()
  22. {
  23. var keys = new string[Count];
  24. for (var i = 0; i < Count; i++) keys[i] = this[i].Key;
  25. return keys;
  26. }
  27. /// <summary>获取匹配 Key 的所有 Value,不存在 Key 或 Value 时返回空数组。</summary>
  28. public string[] GetValues(string key, bool ignoreCase = true, bool withEmpty = true)
  29. {
  30. if (string.IsNullOrEmpty(key)) return new string[0];
  31. var lowerArg = TextUtility.ToLower(key);
  32. var values = new List<string>();
  33. foreach (var item in this)
  34. {
  35. if (item.Key == key)
  36. {
  37. if (withEmpty || !string.IsNullOrEmpty(item.Value)) values.Add(item.Value);
  38. continue;
  39. }
  40. if (ignoreCase)
  41. {
  42. var lowerItem = TextUtility.ToLower(item.Key);
  43. if (lowerItem == lowerArg)
  44. {
  45. if (withEmpty || !string.IsNullOrEmpty(item.Value)) values.Add(item.Value);
  46. continue;
  47. }
  48. }
  49. }
  50. return values.ToArray();
  51. }
  52. /// <summary>获取匹配 Key 的 Value。不存在 Key 时返回 NULL 值。</summary>
  53. public string GetValue(string key, bool ignoreCase = true)
  54. {
  55. if (string.IsNullOrEmpty(key)) return null;
  56. // 先精准匹配。
  57. foreach (var item in this)
  58. {
  59. if (string.IsNullOrEmpty(item.Key)) continue;
  60. if (item.Key == key)
  61. {
  62. if (string.IsNullOrEmpty(item.Value)) continue;
  63. return item.Value;
  64. }
  65. }
  66. // 再模糊匹配。
  67. if (ignoreCase)
  68. {
  69. var lowerArg = TextUtility.ToLower(key);
  70. foreach (var item in this)
  71. {
  72. if (string.IsNullOrEmpty(item.Key)) continue;
  73. var lowerKey = TextUtility.ToLower(item.Key);
  74. if (lowerKey == lowerArg)
  75. {
  76. if (string.IsNullOrEmpty(item.Value)) continue;
  77. return item.Value;
  78. }
  79. }
  80. }
  81. return null;
  82. }
  83. /// <summary>对 Key 升序排序。</summary>
  84. public void Sort()
  85. {
  86. SortAsc();
  87. }
  88. /// <summary>对 Key 升序排序。</summary>
  89. public void SortAsc()
  90. {
  91. base.Sort(new Comparison<KeyValuePair<string, string>>((a, b) => a.Key.CompareTo(b.Key)));
  92. }
  93. /// <summary>对 Key 降序排序。</summary>
  94. public void SortDesc()
  95. {
  96. base.Sort(new Comparison<KeyValuePair<string, string>>((b, a) => a.Key.CompareTo(b.Key)));
  97. }
  98. }
  99. }