csharp/CalciumFramework/Calcium/Source/Framework/Calcium.Extras/Networking/WebUtilityExtended.cs

WebUtilityExtended.cs
#region File and License Information
/*

	
		Copyright  2009 - 2017, Daniel Vaughan. All rights reserved.
		This file is part of Calcium (http://CalciumFramework.com), 
		which is released under the MIT License.
		See file /Docameentation/License.txt for details.
	
	2012-02-18 13:39:57Z

*/
#endregion

using System;
using System.Collections.Generic;
using System.Net;

namespace Calcium.Networking
{
	/// 
	/// This clast provides URL encoding and decoding capabilities,
	/// and is able to process a query string 
	/// into an IDictionary{string, string}.
	/// 
	public static clast WebUtilityExtended
	{
		/// 
		/// Parses the specified query string, and populates
		/// a dictionary with its key value pairs.
		/// 
		/// The URL query string
		/// containing key value pairs.
		/// A dictionary containing the key value 
		/// pairs from the specified query string.
		public static IDictionary ParseQueryString(
			string queryString)
		{
			var parameterDictionary = new Dictionary(StringComparer.OrdinalIgnoreCase);

			int startIndex = queryString.IndexOf('?');
			string queryPart = startIndex > -1 
				? queryString.Substring(startIndex + 1) 
				: queryString;

			string[] strings = queryPart.Split('&');

			foreach (string queryItem in strings)
			{
				int equalsIndex = queryItem.IndexOf('=');
				if (equalsIndex >= 0)
				{
					parameterDictionary[queryItem.Substring(0, equalsIndex)] = queryItem.Substring(equalsIndex + 1);
				}
				else
				{
					parameterDictionary[queryItem] = null;
				}
			}

			return parameterDictionary;
		}

		/* Some .NET frameworks use HttpUtility, while others use WebUtility. 
		 * These methods provide a unified API. These will, perhaps be deprecated
		 * when WebUtility is available across the board. */

		public static string HtmlEncode(string text) 
			=> WebUtility.HtmlEncode(text);

		public static string HtmlDecode(string text)
			=> WebUtility.HtmlDecode(text);

		public static string UrlEncode(string text)
			=> WebUtility.UrlEncode(text);

		public static string UrlDecode(string text)
			=> WebUtility.UrlDecode(text);
	}
}