wasSharp – Diff between revs 1 and 3

Subversion Repositories:
Rev:
Show entire fileIgnore whitespace
Rev 1 Rev 3
Line 3... Line 3...
3 // Please see: http://www.gnu.org/licenses/gpl.html for legal details, // 3 // Please see: http://www.gnu.org/licenses/gpl.html for legal details, //
4 // rights of fair usage, the disclaimer and warranty conditions. // 4 // rights of fair usage, the disclaimer and warranty conditions. //
5 /////////////////////////////////////////////////////////////////////////// 5 ///////////////////////////////////////////////////////////////////////////
Line 6... Line 6...
6   6  
7 using System; -  
8 using System.IO; 7 using System;
9 using System.Linq; 8 using System.Linq;
Line 10... Line 9...
10 using System.Text; 9 using System.Net;
11   10  
12 namespace wasSharp 11 namespace wasSharp
13 { 12 {
Line 47... Line 46...
47 /// <summary>RFC1738 URL Escapes a string</summary> 46 /// <summary>RFC1738 URL Escapes a string</summary>
48 /// <param name="data">a string to escape</param> 47 /// <param name="data">a string to escape</param>
49 /// <returns>an RFC1738 escaped string</returns> 48 /// <returns>an RFC1738 escaped string</returns>
50 public static string wasURLEscapeDataString(string data) 49 public static string wasURLEscapeDataString(string data)
51 { 50 {
52 //return HttpUtility.UrlEncode(data); 51 return WebUtility.UrlEncode(data);
53 StringBuilder result = new StringBuilder(); -  
54   -  
55 char[] hex = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}; -  
56   -  
57 foreach (char c in data) -  
58 { -  
59 switch (c) -  
60 { -  
61 case '0': -  
62 case '1': -  
63 case '2': -  
64 case '3': -  
65 case '4': -  
66 case '5': -  
67 case '6': -  
68 case '7': -  
69 case '8': -  
70 case '9': -  
71 case 'a': -  
72 case 'b': -  
73 case 'c': -  
74 case 'd': -  
75 case 'e': -  
76 case 'f': -  
77 case 'g': -  
78 case 'h': -  
79 case 'i': -  
80 case 'j': -  
81 case 'k': -  
82 case 'l': -  
83 case 'm': -  
84 case 'n': -  
85 case 'o': -  
86 case 'p': -  
87 case 'q': -  
88 case 'r': -  
89 case 's': -  
90 case 't': -  
91 case 'u': -  
92 case 'v': -  
93 case 'w': -  
94 case 'x': -  
95 case 'y': -  
96 case 'z': -  
97 case 'A': -  
98 case 'B': -  
99 case 'C': -  
100 case 'D': -  
101 case 'E': -  
102 case 'F': -  
103 case 'G': -  
104 case 'H': -  
105 case 'I': -  
106 case 'J': -  
107 case 'K': -  
108 case 'L': -  
109 case 'M': -  
110 case 'N': -  
111 case 'O': -  
112 case 'P': -  
113 case 'Q': -  
114 case 'R': -  
115 case 'S': -  
116 case 'T': -  
117 case 'U': -  
118 case 'V': -  
119 case 'W': -  
120 case 'X': -  
121 case 'Y': -  
122 case 'Z': -  
123 case '!': -  
124 case '\'': -  
125 case '(': -  
126 case ')': -  
127 case '*': -  
128 case '-': -  
129 case '.': -  
130 case '_': -  
131 result.Append(c); -  
132 break; -  
133 case ' ': -  
134 result.Append('+'); -  
135 break; -  
136 default: -  
137 StringBuilder uCode = new StringBuilder(); -  
138 foreach (var b in Encoding.UTF8.GetBytes(new[] {c})) -  
139 { -  
140 uCode.Append('%'); -  
141 uCode.Append(hex[b >> 4]); -  
142 uCode.Append(hex[b & 0x0F]); -  
143 } -  
144 result.Append(uCode); -  
145 break; -  
146 } -  
147 } -  
148   -  
149 return result.ToString(); -  
150 } 52 }
Line 151... Line 53...
151   53  
152 /////////////////////////////////////////////////////////////////////////// 54 ///////////////////////////////////////////////////////////////////////////
153 // Copyright (C) Wizardry and Steamworks 2015 - License: GNU GPLv3 // 55 // Copyright (C) Wizardry and Steamworks 2015 - License: GNU GPLv3 //
154 /////////////////////////////////////////////////////////////////////////// 56 ///////////////////////////////////////////////////////////////////////////
155 /// <summary>RFC1738 URL Unescape a string</summary> 57 /// <summary>RFC1738 URL Unescape a string</summary>
156 /// <param name="data">a string to unescape</param> 58 /// <param name="data">a string to unescape</param>
157 /// <returns>an RFC1738 unescaped string</returns> 59 /// <returns>an RFC1738 unescaped string</returns>
158 public static string wasURLUnescapeDataString(string data) 60 public static string wasURLUnescapeDataString(string data)
159 { 61 {
160 //return HttpUtility.UrlDecode(data); -  
161 StringBuilder result = new StringBuilder(); -  
162   -  
163 int c; -  
164   -  
165 Func<byte, int> GetInt = o => -  
166 { -  
167 switch ((char) o) -  
168 { -  
169 case '0': -  
170 case '1': -  
171 case '2': -  
172 case '3': -  
173 case '4': -  
174 case '5': -  
175 case '6': -  
176 case '7': -  
177 case '8': -  
178 case '9': -  
179 return o - '0'; -  
180 case 'a': -  
181 case 'b': -  
182 case 'c': -  
183 case 'd': -  
184 case 'e': -  
185 case 'f': -  
186 return o - 'a' + 10; -  
187 case 'A': -  
188 case 'B': -  
189 case 'C': -  
190 case 'D': -  
191 case 'E': -  
192 case 'F': -  
193 return o - 'A' + 10; -  
194 default: -  
195 return -1; -  
196 } -  
197 }; -  
198   -  
199 Func<string, int, int, int> GetCharString = (s, o, l) => -  
200 { -  
201 int v = 0; -  
202 int e = l + o; -  
203 for (int i = o; i < e; ++i) -  
204 { -  
205 c = GetInt((byte) s[i]); -  
206 if (c.Equals(-1)) return -1; -  
207 v = (v << 4) + c; -  
208 } -  
209 return v; -  
210 }; -  
211   -  
212 using (MemoryStream bytes = new MemoryStream()) -  
213 { -  
214 for (int x = 0; x < data.Length; ++x) -  
215 { -  
216 if (data[x].Equals('%') && !data[x + 1].Equals('%') && x + 2 < data.Length) -  
217 { -  
218 c = GetCharString(data, x + 1, 2); -  
219 switch (c) -  
220 { -  
221 case -1: -  
222 result.Append('%'); -  
223 break; -  
224 default: -  
225 bytes.WriteByte((byte) c); -  
226 x += 2; -  
227 break; -  
228 } -  
229 continue; -  
230 } -  
231   -  
232 if (!bytes.Length.Equals(0)) -  
233 { -  
234 result.Append(Encoding.UTF8.GetChars(bytes.ToArray())); -  
235 bytes.SetLength(0); -  
236 } -  
237   -  
238 switch (data[x].Equals('+')) -  
239 { -  
240 case true: -  
241 result.Append(' '); -  
242 break; -  
243 default: -  
244 result.Append(data[x]); -  
245 break; -  
246 } -  
247 } -  
248   -  
249 if (!bytes.Length.Equals(0)) -  
250 { -  
251 result.Append(Encoding.UTF8.GetChars(bytes.ToArray())); -  
252 bytes.SetLength(0); -  
253 } -  
254 } -  
255   -  
256 return result.ToString(); 62 return WebUtility.UrlDecode(data);
257 } 63 }
258 } 64 }
259 } 65 }