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.
138 lines
4.2 KiB
138 lines
4.2 KiB
using Apewer;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Text;
|
|
|
|
namespace Apewer.Models
|
|
{
|
|
|
|
/// <summary></summary>
|
|
[Serializable]
|
|
public class StringPairs : List<KeyValuePair<string, string>>
|
|
{
|
|
|
|
/// <summary>添加项。返回错误信息。</summary>
|
|
public string Add(string key, string value)
|
|
{
|
|
if (string.IsNullOrEmpty(key)) return "参数 Name 无效。";
|
|
// if (string.IsNullOrEmpty(value)) return "参数 Value 无效。";
|
|
|
|
var kvp = new KeyValuePair<string, string>(key, value);
|
|
Add(kvp);
|
|
return null;
|
|
}
|
|
|
|
/// <summary>获取所有 Key。</summary>
|
|
public string[] GetAllKeys()
|
|
{
|
|
var keys = new string[Count];
|
|
for (var i = 0; i < Count; i++) keys[i] = this[i].Key;
|
|
return keys;
|
|
}
|
|
|
|
/// <summary>获取匹配 Key 的所有 Value,不存在 Key 或 Value 时返回空数组。</summary>
|
|
public string[] GetValues(string key, bool ignoreCase = true, bool withEmpty = true)
|
|
{
|
|
if (string.IsNullOrEmpty(key)) return new string[0];
|
|
|
|
var lowerArg = TextUtility.ToLower(key);
|
|
var values = new List<string>();
|
|
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.ToLower(item.Key);
|
|
if (lowerItem == lowerArg)
|
|
{
|
|
if (withEmpty || !string.IsNullOrEmpty(item.Value)) values.Add(item.Value);
|
|
continue;
|
|
}
|
|
}
|
|
}
|
|
return values.ToArray();
|
|
}
|
|
|
|
/// <summary>获取匹配 Key 的 Value。不存在 Key 时返回 NULL 值。</summary>
|
|
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.ToLower(key);
|
|
foreach (var item in this)
|
|
{
|
|
if (string.IsNullOrEmpty(item.Key)) continue;
|
|
var lowerKey = TextUtility.ToLower(item.Key);
|
|
if (lowerKey == lowerArg)
|
|
{
|
|
if (string.IsNullOrEmpty(item.Value)) continue;
|
|
return item.Value;
|
|
}
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
/// <summary>对 Key 升序排序。</summary>
|
|
public void Sort()
|
|
{
|
|
SortAsc();
|
|
}
|
|
|
|
/// <summary>对 Key 升序排序。</summary>
|
|
public void SortAsc()
|
|
{
|
|
base.Sort(new Comparison<KeyValuePair<string, string>>((a, b) => a.Key.CompareTo(b.Key)));
|
|
}
|
|
|
|
/// <summary>对 Key 降序排序。</summary>
|
|
public void SortDesc()
|
|
{
|
|
base.Sort(new Comparison<KeyValuePair<string, string>>((b, a) => a.Key.CompareTo(b.Key)));
|
|
}
|
|
|
|
/// <summary>检查拥有指定的 Key。</summary>
|
|
public bool HasKey(string key, bool ignoreCase = false)
|
|
{
|
|
var a = ignoreCase ? key.SafeLower() : 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.SafeLower() : k;
|
|
if (a == b) return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|