-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathParamList.cs
144 lines (125 loc) · 4.08 KB
/
ParamList.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
namespace DigitalPlatform.Text
{
/// <summary>
/// 能保持原序的参数分析类
/// </summary>
public class ParamList
{
class Parameter
{
public string Name = "";
public string Value = "";
}
List<Parameter> _table = new List<Parameter>();
void Set(string strName, string strValue)
{
Parameter parameter = Find(strName);
if (parameter == null)
{
parameter = new Parameter();
parameter.Name = strName;
this._table.Add(parameter);
}
parameter.Value = strValue;
}
Parameter Find(string strName)
{
foreach (Parameter parameter in this._table)
{
if (parameter.Name == strName)
return parameter;
}
return null;
}
public string this[string strName]
{
get
{
return GetValue(strName);
}
set
{
this.Set(strName, value);
}
}
public string GetValue(string strName)
{
Parameter parameter = Find(strName);
if (parameter == null)
return null;
return parameter.Value;
}
// 删除一个条目
public bool Remove(string strName)
{
Parameter parameter = Find(strName);
if (parameter == null)
return false; // 本来就不存在
this._table.Remove(parameter);
return true;
}
// 将逗号间隔的参数表解析到Hashtable中
// parameters:
// strText 字符串。形态如 "名1=值1,名2=值2"
public static ParamList Build(string strText,
char chSegChar,
char chEqualChar,
string strDecodeStyle = "")
{
ParamList list = new ParamList();
if (string.IsNullOrEmpty(strText) == true)
return list;
string[] parts = strText.Split(new char[] { chSegChar }); // ','
for (int i = 0; i < parts.Length; i++)
{
string strPart = parts[i].Trim();
if (String.IsNullOrEmpty(strPart) == true)
continue;
string strName = "";
string strValue = "";
int nRet = strPart.IndexOf(chEqualChar); // '='
if (nRet == -1)
{
strName = strPart;
strValue = "";
}
else
{
strName = strPart.Substring(0, nRet).Trim();
strValue = strPart.Substring(nRet + 1).Trim();
}
if (String.IsNullOrEmpty(strName) == true
&& String.IsNullOrEmpty(strValue) == true)
continue;
if (strDecodeStyle == "url")
strValue = HttpUtility.UrlDecode(strValue);
list.Set(strName, strValue);
}
return list;
}
// 按照指定的 key 名字集合顺序和个数输出
public string ToString(
char chSegChar = ',',
char chEqualChar = '=',
string strEncodeStyle = "")
{
StringBuilder result = new StringBuilder(4096);
foreach (Parameter parameter in this._table)
{
if (result.Length > 0)
result.Append(chSegChar);
string strValue = parameter.Value;
if (strEncodeStyle == "url")
result.Append(parameter.Name + new string(chEqualChar, 1) + HttpUtility.UrlEncode(strValue));
else
result.Append(parameter.Name + new string(chEqualChar, 1) + strValue);
}
return result.ToString();
}
}
}