GlobalObject.cs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.Linq;
  5. using System.Text;
  6. using Jint.Collections;
  7. using Jint.Native.Object;
  8. using Jint.Native.String;
  9. using Jint.Runtime;
  10. using Jint.Runtime.Descriptors;
  11. using Jint.Runtime.Descriptors.Specialized;
  12. using Jint.Runtime.Interop;
  13. namespace Jint.Native.Global
  14. {
  15. public sealed class GlobalObject : ObjectInstance
  16. {
  17. private readonly StringBuilder _stringBuilder = new StringBuilder();
  18. private GlobalObject(Engine engine) : base(engine)
  19. {
  20. }
  21. public static GlobalObject CreateGlobalObject(Engine engine)
  22. {
  23. var global = new GlobalObject(engine)
  24. {
  25. _prototype = null,
  26. _properties = new StringDictionarySlim<PropertyDescriptor>(35)
  27. };
  28. return global;
  29. }
  30. protected override void Initialize()
  31. {
  32. // Global object properties
  33. _properties["Object"] = new PropertyDescriptor(Engine.Object, true, false, true);
  34. _properties["Function"] = new PropertyDescriptor(Engine.Function, true, false, true);
  35. _properties["Symbol"] = new PropertyDescriptor(Engine.Symbol, true, false, true);
  36. _properties["Array"] = new PropertyDescriptor(Engine.Array, true, false, true);
  37. _properties["Map"] = new PropertyDescriptor(Engine.Map, true, false, true);
  38. _properties["Set"] = new PropertyDescriptor(Engine.Set, true, false, true);
  39. _properties["String"] = new PropertyDescriptor(Engine.String, true, false, true);
  40. _properties["RegExp"] = new PropertyDescriptor(Engine.RegExp, true, false, true);
  41. _properties["Number"] = new PropertyDescriptor(Engine.Number, true, false, true);
  42. _properties["Boolean"] = new PropertyDescriptor(Engine.Boolean, true, false, true);
  43. _properties["Date"] = new PropertyDescriptor(Engine.Date, true, false, true);
  44. _properties["Math"] = new PropertyDescriptor(Engine.Math, true, false, true);
  45. _properties["JSON"] = new PropertyDescriptor(Engine.Json, true, false, true);
  46. _properties["Error"] = new LazyPropertyDescriptor(() => Engine.Error, true, false, true);
  47. _properties["EvalError"] = new LazyPropertyDescriptor(() => Engine.EvalError, true, false, true);
  48. _properties["Proxy"] = new LazyPropertyDescriptor(() => Engine.Proxy, true, false, true);
  49. _properties["RangeError"] = new LazyPropertyDescriptor(() => Engine.RangeError, true, false, true);
  50. _properties["ReferenceError"] = new LazyPropertyDescriptor(() => Engine.ReferenceError, true, false, true);
  51. _properties["Reflect"] = new LazyPropertyDescriptor(() => Engine.Reflect, true, false, true);
  52. _properties["SyntaxError"] = new LazyPropertyDescriptor(() => Engine.SyntaxError, true, false, true);
  53. _properties["TypeError"] = new LazyPropertyDescriptor(() => Engine.TypeError, true, false, true);
  54. _properties["URIError"] = new LazyPropertyDescriptor(() => Engine.UriError, true, false, true);
  55. _properties["NaN"] = new PropertyDescriptor(double.NaN, false, false, false);
  56. _properties["Infinity"] = new PropertyDescriptor(double.PositiveInfinity, false, false, false);
  57. _properties["undefined"] = new PropertyDescriptor(Undefined, false, false, false);
  58. _properties["parseInt"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "parseInt", ParseInt, 2, PropertyFlag.Configurable), true, false, true);
  59. _properties["parseFloat"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "parseFloat", ParseFloat, 1, PropertyFlag.Configurable), true, false, true);
  60. _properties["isNaN"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "isNaN", IsNaN, 1), true, false, true);
  61. _properties["isFinite"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "isFinite", IsFinite, 1), true, false, true);
  62. _properties["decodeURI"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "decodeURI", DecodeUri, 1, PropertyFlag.Configurable), true, false, true);
  63. _properties["decodeURIComponent"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "decodeURIComponent", DecodeUriComponent, 1, PropertyFlag.Configurable), true, false, true);
  64. _properties["encodeURI"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "encodeURI", EncodeUri, 1, PropertyFlag.Configurable), true, false, true);
  65. _properties["encodeURIComponent"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "encodeURIComponent", EncodeUriComponent, 1, PropertyFlag.Configurable), true, false, true);
  66. _properties["escape"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "escape", Escape, 1), true, false, true);
  67. _properties["unescape"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "unescape", Unescape, 1), true, false, true);
  68. }
  69. /// <summary>
  70. /// http://www.ecma-international.org/ecma-262/5.1/#sec-15.1.2.2
  71. /// </summary>
  72. public JsValue ParseInt(JsValue thisObject, JsValue[] arguments)
  73. {
  74. string inputString = TypeConverter.ToString(arguments.At(0));
  75. var s = StringPrototype.TrimEx(inputString);
  76. var sign = 1;
  77. if (!System.String.IsNullOrEmpty(s))
  78. {
  79. if (s[0] == '-')
  80. {
  81. sign = -1;
  82. }
  83. if (s[0] == '-' || s[0] == '+')
  84. {
  85. s = s.Substring(1);
  86. }
  87. }
  88. var stripPrefix = true;
  89. int radix = arguments.Length > 1 ? TypeConverter.ToInt32(arguments[1]) : 0;
  90. if (radix == 0)
  91. {
  92. if (s.Length >= 2 && s.StartsWith("0x") || s.StartsWith("0X"))
  93. {
  94. radix = 16;
  95. }
  96. else
  97. {
  98. radix = 10;
  99. }
  100. }
  101. else if (radix < 2 || radix > 36)
  102. {
  103. return JsNumber.DoubleNaN;
  104. }
  105. else if (radix != 16)
  106. {
  107. stripPrefix = false;
  108. }
  109. if (stripPrefix && s.Length >= 2 && s.StartsWith("0x") || s.StartsWith("0X"))
  110. {
  111. s = s.Substring(2);
  112. }
  113. try
  114. {
  115. return sign * Parse(s, radix);
  116. }
  117. catch
  118. {
  119. return JsNumber.DoubleNaN;
  120. }
  121. }
  122. private static double Parse(string number, int radix)
  123. {
  124. if (number == "")
  125. {
  126. return double.NaN;
  127. }
  128. double result = 0;
  129. double pow = 1;
  130. for (int i = number.Length - 1; i >= 0; i--)
  131. {
  132. double index = double.NaN;
  133. char digit = number[i];
  134. if (digit >= '0' && digit <= '9')
  135. {
  136. index = digit - '0';
  137. }
  138. else if (digit >= 'a' && digit <= 'z')
  139. {
  140. index = digit - 'a' + 10;
  141. }
  142. else if (digit >= 'A' && digit <= 'Z')
  143. {
  144. index = digit - 'A' + 10;
  145. }
  146. if (double.IsNaN(index) || index >= radix)
  147. {
  148. return Parse(number.Substring(0, i), radix);
  149. }
  150. result += index * pow;
  151. pow = pow * radix;
  152. }
  153. return result;
  154. }
  155. /// <summary>
  156. /// http://www.ecma-international.org/ecma-262/5.1/#sec-15.1.2.3
  157. /// </summary>
  158. public JsValue ParseFloat(JsValue thisObject, JsValue[] arguments)
  159. {
  160. var inputString = TypeConverter.ToString(arguments.At(0));
  161. var trimmedString = StringPrototype.TrimStartEx(inputString);
  162. var sign = 1;
  163. if (trimmedString.Length > 0)
  164. {
  165. if (trimmedString[0] == '-')
  166. {
  167. sign = -1;
  168. trimmedString = trimmedString.Substring(1);
  169. }
  170. else if (trimmedString[0] == '+')
  171. {
  172. trimmedString = trimmedString.Substring(1);
  173. }
  174. }
  175. if (trimmedString.StartsWith("Infinity"))
  176. {
  177. return sign * double.PositiveInfinity;
  178. }
  179. if (trimmedString.StartsWith("NaN"))
  180. {
  181. return JsNumber.DoubleNaN;
  182. }
  183. var separator = (char)0;
  184. bool isNan = true;
  185. decimal number = 0;
  186. var i = 0;
  187. for (; i < trimmedString.Length; i++)
  188. {
  189. var c = trimmedString[i];
  190. if (c == '.')
  191. {
  192. i++;
  193. separator = '.';
  194. break;
  195. }
  196. if (c == 'e' || c == 'E')
  197. {
  198. i++;
  199. separator = 'e';
  200. break;
  201. }
  202. var digit = c - '0';
  203. if (digit >= 0 && digit <= 9)
  204. {
  205. isNan = false;
  206. number = number * 10 + digit;
  207. }
  208. else
  209. {
  210. break;
  211. }
  212. }
  213. decimal pow = 0.1m;
  214. if (separator == '.')
  215. {
  216. for (; i < trimmedString.Length; i++)
  217. {
  218. var c = trimmedString[i];
  219. var digit = c - '0';
  220. if (digit >= 0 && digit <= 9)
  221. {
  222. isNan = false;
  223. number += digit * pow;
  224. pow *= 0.1m;
  225. }
  226. else if (c == 'e' || c == 'E')
  227. {
  228. i++;
  229. separator = 'e';
  230. break;
  231. }
  232. else
  233. {
  234. break;
  235. }
  236. }
  237. }
  238. var exp = 0;
  239. var expSign = 1;
  240. if (separator == 'e')
  241. {
  242. if (i < trimmedString.Length)
  243. {
  244. if (trimmedString[i] == '-')
  245. {
  246. expSign = -1;
  247. i++;
  248. }
  249. else if (trimmedString[i] == '+')
  250. {
  251. i++;
  252. }
  253. }
  254. for (; i < trimmedString.Length; i++)
  255. {
  256. var c = trimmedString[i];
  257. var digit = c - '0';
  258. if (digit >= 0 && digit <= 9)
  259. {
  260. exp = exp * 10 + digit;
  261. }
  262. else
  263. {
  264. break;
  265. }
  266. }
  267. }
  268. if (isNan)
  269. {
  270. return JsNumber.DoubleNaN;
  271. }
  272. for (var k = 1; k <= exp; k++)
  273. {
  274. if (expSign > 0)
  275. {
  276. number *= 10;
  277. }
  278. else
  279. {
  280. number /= 10;
  281. }
  282. }
  283. return (double)(sign * number);
  284. }
  285. /// <summary>
  286. /// http://www.ecma-international.org/ecma-262/5.1/#sec-15.1.2.4
  287. /// </summary>
  288. public static JsValue IsNaN(JsValue thisObject, JsValue[] arguments)
  289. {
  290. var x = TypeConverter.ToNumber(arguments.At(0));
  291. return double.IsNaN(x);
  292. }
  293. /// <summary>
  294. /// http://www.ecma-international.org/ecma-262/5.1/#sec-15.1.2.5
  295. /// </summary>
  296. public static JsValue IsFinite(JsValue thisObject, JsValue[] arguments)
  297. {
  298. if (arguments.Length != 1)
  299. {
  300. return false;
  301. }
  302. var n = TypeConverter.ToNumber(arguments.At(0));
  303. if (double.IsNaN(n) || double.IsInfinity(n))
  304. {
  305. return false;
  306. }
  307. return true;
  308. }
  309. private static readonly HashSet<char> UriReserved = new HashSet<char>
  310. {
  311. ';', '/', '?', ':', '@', '&', '=', '+', '$', ','
  312. };
  313. private static readonly HashSet<char> UriUnescaped = new HashSet<char>
  314. {
  315. 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
  316. 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R',
  317. 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '-', '_', '.', '!',
  318. '~', '*', '\'', '(', ')'
  319. };
  320. private static readonly HashSet<char> UnescapedUriSet = new HashSet<char>(UriReserved.Concat(UriUnescaped).Concat(new[] { '#' }));
  321. private static readonly HashSet<char> ReservedUriSet = new HashSet<char>(UriReserved.Concat(new[] { '#' }));
  322. private const string HexaMap = "0123456789ABCDEF";
  323. private static bool IsValidHexaChar(char c)
  324. {
  325. return
  326. c >= '0' && c <= '9' ||
  327. c >= 'a' && c <= 'f' ||
  328. c >= 'A' && c <= 'F';
  329. }
  330. /// <summary>
  331. /// http://www.ecma-international.org/ecma-262/5.1/#sec-15.1.3.2
  332. /// </summary>
  333. /// <param name="thisObject"></param>
  334. /// <param name="arguments"></param>
  335. /// <returns></returns>
  336. public JsValue EncodeUri(JsValue thisObject, JsValue[] arguments)
  337. {
  338. var uriString = TypeConverter.ToString(arguments.At(0));
  339. return Encode(uriString, UnescapedUriSet);
  340. }
  341. /// <summary>
  342. /// http://www.ecma-international.org/ecma-262/5.1/#sec-15.1.3.4
  343. /// </summary>
  344. /// <param name="thisObject"></param>
  345. /// <param name="arguments"></param>
  346. /// <returns></returns>
  347. public JsValue EncodeUriComponent(JsValue thisObject, JsValue[] arguments)
  348. {
  349. var uriString = TypeConverter.ToString(arguments.At(0));
  350. return Encode(uriString, UriUnescaped);
  351. }
  352. private string Encode(string uriString, HashSet<char> unescapedUriSet)
  353. {
  354. var strLen = uriString.Length;
  355. _stringBuilder.EnsureCapacity(uriString.Length);
  356. _stringBuilder.Clear();
  357. for (var k = 0; k < strLen; k++)
  358. {
  359. var c = uriString[k];
  360. if (unescapedUriSet != null && unescapedUriSet.Contains(c))
  361. {
  362. _stringBuilder.Append(c);
  363. }
  364. else
  365. {
  366. if (c >= 0xDC00 && c <= 0xDBFF)
  367. {
  368. ExceptionHelper.ThrowUriError(_engine);
  369. }
  370. int v;
  371. if (c < 0xD800 || c > 0xDBFF)
  372. {
  373. v = c;
  374. }
  375. else
  376. {
  377. k++;
  378. if (k == strLen)
  379. {
  380. ExceptionHelper.ThrowUriError(_engine);
  381. }
  382. var kChar = (int)uriString[k];
  383. if (kChar < 0xDC00 || kChar > 0xDFFF)
  384. {
  385. ExceptionHelper.ThrowUriError(_engine);
  386. }
  387. v = (c - 0xD800) * 0x400 + (kChar - 0xDC00) + 0x10000;
  388. }
  389. byte[] octets = ArrayExt.Empty<byte>();
  390. if (v >= 0 && v <= 0x007F)
  391. {
  392. // 00000000 0zzzzzzz -> 0zzzzzzz
  393. octets = new[] { (byte)v };
  394. }
  395. else if (v <= 0x07FF)
  396. {
  397. // 00000yyy yyzzzzzz -> 110yyyyy ; 10zzzzzz
  398. octets = new[]
  399. {
  400. (byte)(0xC0 | (v >> 6)),
  401. (byte)(0x80 | (v & 0x3F))
  402. };
  403. }
  404. else if (v <= 0xD7FF)
  405. {
  406. // xxxxyyyy yyzzzzzz -> 1110xxxx; 10yyyyyy; 10zzzzzz
  407. octets = new[]
  408. {
  409. (byte)(0xE0 | (v >> 12)),
  410. (byte)(0x80 | ((v >> 6) & 0x3F)),
  411. (byte)(0x80 | (v & 0x3F))
  412. };
  413. }
  414. else if (v <= 0xDFFF)
  415. {
  416. ExceptionHelper.ThrowUriError(_engine);
  417. }
  418. else if (v <= 0xFFFF)
  419. {
  420. octets = new[]
  421. {
  422. (byte) (0xE0 | (v >> 12)),
  423. (byte) (0x80 | ((v >> 6) & 0x3F)),
  424. (byte) (0x80 | (v & 0x3F))
  425. };
  426. }
  427. else
  428. {
  429. octets = new[]
  430. {
  431. (byte) (0xF0 | (v >> 18)),
  432. (byte) (0x80 | (v >> 12 & 0x3F)),
  433. (byte) (0x80 | (v >> 6 & 0x3F)),
  434. (byte) (0x80 | (v >> 0 & 0x3F))
  435. };
  436. }
  437. for (var j = 0; j < octets.Length; j++)
  438. {
  439. var jOctet = octets[j];
  440. var x1 = HexaMap[jOctet / 16];
  441. var x2 = HexaMap[jOctet % 16];
  442. _stringBuilder.Append('%').Append(x1).Append(x2);
  443. }
  444. }
  445. }
  446. return _stringBuilder.ToString();
  447. }
  448. public JsValue DecodeUri(JsValue thisObject, JsValue[] arguments)
  449. {
  450. var uriString = TypeConverter.ToString(arguments.At(0));
  451. return Decode(uriString, ReservedUriSet);
  452. }
  453. public JsValue DecodeUriComponent(JsValue thisObject, JsValue[] arguments)
  454. {
  455. var componentString = TypeConverter.ToString(arguments.At(0));
  456. return Decode(componentString, null);
  457. }
  458. private string Decode(string uriString, HashSet<char> reservedSet)
  459. {
  460. var strLen = uriString.Length;
  461. _stringBuilder.EnsureCapacity(strLen);
  462. _stringBuilder.Clear();
  463. var octets = ArrayExt.Empty<byte>();
  464. for (var k = 0; k < strLen; k++)
  465. {
  466. var C = uriString[k];
  467. if (C != '%')
  468. {
  469. _stringBuilder.Append(C);
  470. }
  471. else
  472. {
  473. var start = k;
  474. if (k + 2 >= strLen)
  475. {
  476. ExceptionHelper.ThrowUriError(_engine);
  477. }
  478. if (!IsValidHexaChar(uriString[k + 1]) || !IsValidHexaChar(uriString[k + 2]))
  479. {
  480. ExceptionHelper.ThrowUriError(_engine);
  481. }
  482. var B = Convert.ToByte(uriString[k + 1].ToString() + uriString[k + 2], 16);
  483. k += 2;
  484. if ((B & 0x80) == 0)
  485. {
  486. C = (char)B;
  487. if (reservedSet == null || !reservedSet.Contains(C))
  488. {
  489. _stringBuilder.Append(C);
  490. }
  491. else
  492. {
  493. _stringBuilder.Append(uriString, start, k - start + 1);
  494. }
  495. }
  496. else
  497. {
  498. var n = 0;
  499. for (; ((B << n) & 0x80) != 0; n++) ;
  500. if (n == 1 || n > 4)
  501. {
  502. ExceptionHelper.ThrowUriError(_engine);
  503. }
  504. octets = octets.Length == n
  505. ? octets
  506. : new byte[n];
  507. octets[0] = B;
  508. if (k + (3 * (n - 1)) >= strLen)
  509. {
  510. ExceptionHelper.ThrowUriError(_engine);
  511. }
  512. for (var j = 1; j < n; j++)
  513. {
  514. k++;
  515. if (uriString[k] != '%')
  516. {
  517. ExceptionHelper.ThrowUriError(_engine);
  518. }
  519. if (!IsValidHexaChar(uriString[k + 1]) || !IsValidHexaChar(uriString[k + 2]))
  520. {
  521. ExceptionHelper.ThrowUriError(_engine);
  522. }
  523. B = Convert.ToByte(uriString[k + 1].ToString() + uriString[k + 2], 16);
  524. // B & 11000000 != 10000000
  525. if ((B & 0xC0) != 0x80)
  526. {
  527. ExceptionHelper.ThrowUriError(_engine);
  528. }
  529. k += 2;
  530. octets[j] = B;
  531. }
  532. _stringBuilder.Append(Encoding.UTF8.GetString(octets, 0, octets.Length));
  533. }
  534. }
  535. }
  536. return _stringBuilder.ToString();
  537. }
  538. /// <summary>
  539. /// http://www.ecma-international.org/ecma-262/5.1/#sec-B.2.1
  540. /// </summary>
  541. public JsValue Escape(JsValue thisObject, JsValue[] arguments)
  542. {
  543. const string whiteList = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789@*_ + -./";
  544. var uriString = TypeConverter.ToString(arguments.At(0));
  545. var strLen = uriString.Length;
  546. _stringBuilder.EnsureCapacity(strLen);
  547. _stringBuilder.Clear();
  548. for (var k = 0; k < strLen; k++)
  549. {
  550. var c = uriString[k];
  551. if (whiteList.IndexOf(c) != -1)
  552. {
  553. _stringBuilder.Append(c);
  554. }
  555. else if (c < 256)
  556. {
  557. _stringBuilder.Append($"%{((int) c):X2}");
  558. }
  559. else
  560. {
  561. _stringBuilder.Append($"%u{((int) c):X4}");
  562. }
  563. }
  564. return _stringBuilder.ToString();
  565. }
  566. /// <summary>
  567. /// http://www.ecma-international.org/ecma-262/5.1/#sec-B.2.2
  568. /// </summary>
  569. public JsValue Unescape(JsValue thisObject, JsValue[] arguments)
  570. {
  571. var uriString = TypeConverter.ToString(arguments.At(0));
  572. var strLen = uriString.Length;
  573. _stringBuilder.EnsureCapacity(strLen);
  574. _stringBuilder.Clear();
  575. for (var k = 0; k < strLen; k++)
  576. {
  577. var c = uriString[k];
  578. if (c == '%')
  579. {
  580. if (k < strLen - 6
  581. && uriString[k + 1] == 'u'
  582. && uriString.Skip(k + 2).Take(4).All(IsValidHexaChar))
  583. {
  584. c = (char)int.Parse(
  585. string.Join(string.Empty, uriString.Skip(k + 2).Take(4)),
  586. NumberStyles.AllowHexSpecifier);
  587. k += 5;
  588. }
  589. else if (k < strLen - 3
  590. && uriString.Skip(k + 1).Take(2).All(IsValidHexaChar))
  591. {
  592. c = (char)int.Parse(
  593. string.Join(string.Empty, uriString.Skip(k + 1).Take(2)),
  594. NumberStyles.AllowHexSpecifier);
  595. k += 2;
  596. }
  597. }
  598. _stringBuilder.Append(c);
  599. }
  600. return _stringBuilder.ToString();
  601. }
  602. }
  603. }