wasSharp – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 zed 1 ///////////////////////////////////////////////////////////////////////////
2 // Copyright (C) Wizardry and Steamworks 2013 - License: GNU GPLv3 //
3 // Please see: http://www.gnu.org/licenses/gpl.html for legal details, //
4 // rights of fair usage, the disclaimer and warranty conditions. //
5 ///////////////////////////////////////////////////////////////////////////
6  
7 using System;
8 using System.IO;
9 using System.Linq;
10 using System.Text;
11  
12 namespace wasSharp
13 {
14 public class Web
15 {
16 ///////////////////////////////////////////////////////////////////////////
17 // Copyright (C) Wizardry and Steamworks 2014 - License: GNU GPLv3 //
18 ///////////////////////////////////////////////////////////////////////////
19 /// <summary>RFC3986 URI Escapes a string</summary>
20 /// <param name="data">a string to escape</param>
21 /// <returns>an RFC3986 escaped string</returns>
22 public static string wasURIEscapeDataString(string data)
23 {
24 // Uri.EscapeDataString can only handle 32766 characters at a time
25 return string.Join("", Enumerable.Range(0, (data.Length + 32765)/32766)
26 .Select(o => Uri.EscapeDataString(data.Substring(o*32766, Math.Min(32766, data.Length - (o*32766)))))
27 .ToArray());
28 }
29  
30 ///////////////////////////////////////////////////////////////////////////
31 // Copyright (C) Wizardry and Steamworks 2014 - License: GNU GPLv3 //
32 ///////////////////////////////////////////////////////////////////////////
33 /// <summary>URI unescapes an RFC3986 URI escaped string</summary>
34 /// <param name="data">a string to unescape</param>
35 /// <returns>the resulting string</returns>
36 public static string wasURIUnescapeDataString(string data)
37 {
38 // Uri.UnescapeDataString can only handle 32766 characters at a time
39 return string.Join("", Enumerable.Range(0, (data.Length + 32765)/32766)
40 .Select(o => Uri.UnescapeDataString(data.Substring(o*32766, Math.Min(32766, data.Length - (o*32766)))))
41 .ToArray());
42 }
43  
44 ///////////////////////////////////////////////////////////////////////////
45 // Copyright (C) Wizardry and Steamworks 2015 - License: GNU GPLv3 //
46 ///////////////////////////////////////////////////////////////////////////
47 /// <summary>RFC1738 URL Escapes a string</summary>
48 /// <param name="data">a string to escape</param>
49 /// <returns>an RFC1738 escaped string</returns>
50 public static string wasURLEscapeDataString(string data)
51 {
52 //return HttpUtility.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 }
151  
152 ///////////////////////////////////////////////////////////////////////////
153 // Copyright (C) Wizardry and Steamworks 2015 - License: GNU GPLv3 //
154 ///////////////////////////////////////////////////////////////////////////
155 /// <summary>RFC1738 URL Unescape a string</summary>
156 /// <param name="data">a string to unescape</param>
157 /// <returns>an RFC1738 unescaped string</returns>
158 public static string wasURLUnescapeDataString(string data)
159 {
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();
257 }
258 }
259 }